Devirtualize ArgList's dtor now that -Wvirtual-dtor and C++11 allow a better way...
[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/Arg.h"
18 #include "llvm/Option/OptSpecifier.h"
19 #include "llvm/Option/Option.h"
20 #include <list>
21 #include <memory>
22 #include <string>
23 #include <vector>
24
25 namespace llvm {
26 namespace opt {
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() = default;
114   // Protect the dtor to ensure this type is never destroyed polymorphically.
115   ~ArgList() = default;
116
117 public:
118
119   /// @name Arg Access
120   /// @{
121
122   /// append - Append \p A to the arg list.
123   void append(Arg *A);
124
125   arglist_type &getArgs() { return Args; }
126   const arglist_type &getArgs() const { return Args; }
127
128   unsigned size() const { return Args.size(); }
129
130   /// @}
131   /// @name Arg Iteration
132   /// @{
133
134   iterator begin() { return Args.begin(); }
135   iterator end() { return Args.end(); }
136
137   reverse_iterator rbegin() { return Args.rbegin(); }
138   reverse_iterator rend() { return Args.rend(); }
139
140   const_iterator begin() const { return Args.begin(); }
141   const_iterator end() const { return Args.end(); }
142
143   const_reverse_iterator rbegin() const { return Args.rbegin(); }
144   const_reverse_iterator rend() const { return Args.rend(); }
145
146   arg_iterator filtered_begin(OptSpecifier Id0 = 0U, OptSpecifier Id1 = 0U,
147                               OptSpecifier Id2 = 0U) const {
148     return arg_iterator(Args.begin(), *this, Id0, Id1, Id2);
149   }
150   arg_iterator filtered_end() const {
151     return arg_iterator(Args.end(), *this);
152   }
153
154   iterator_range<arg_iterator> filtered(OptSpecifier Id0 = 0U,
155                                         OptSpecifier Id1 = 0U,
156                                         OptSpecifier Id2 = 0U) const {
157     return make_range(filtered_begin(Id0, Id1, Id2), filtered_end());
158   }
159
160   /// @}
161   /// @name Arg Removal
162   /// @{
163
164   /// eraseArg - Remove any option matching \p Id.
165   void eraseArg(OptSpecifier Id);
166
167   /// @}
168   /// @name Arg Access
169   /// @{
170
171   /// hasArg - Does the arg list contain any option matching \p Id.
172   ///
173   /// \p Claim Whether the argument should be claimed, if it exists.
174   bool hasArgNoClaim(OptSpecifier Id) const {
175     return getLastArgNoClaim(Id) != nullptr;
176   }
177   bool hasArg(OptSpecifier Id) const {
178     return getLastArg(Id) != nullptr;
179   }
180   bool hasArg(OptSpecifier Id0, OptSpecifier Id1) const {
181     return getLastArg(Id0, Id1) != nullptr;
182   }
183   bool hasArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const {
184     return getLastArg(Id0, Id1, Id2) != nullptr;
185   }
186
187   /// getLastArg - Return the last argument matching \p Id, or null.
188   ///
189   /// \p Claim Whether the argument should be claimed, if it exists.
190   Arg *getLastArgNoClaim(OptSpecifier Id) const;
191   Arg *getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1) const;
192   Arg *getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1,
193                          OptSpecifier Id2) const;
194   Arg *getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
195                          OptSpecifier Id3) const;
196   Arg *getLastArg(OptSpecifier Id) const;
197   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1) const;
198   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const;
199   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
200                   OptSpecifier Id3) const;
201   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
202                   OptSpecifier Id3, OptSpecifier Id4) const;
203   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
204                   OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5) const;
205   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
206                   OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
207                   OptSpecifier Id6) const;
208   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
209                   OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
210                   OptSpecifier Id6, OptSpecifier Id7) const;
211
212   /// getArgString - Return the input argument string at \p Index.
213   virtual const char *getArgString(unsigned Index) const = 0;
214
215   /// getNumInputArgStrings - Return the number of original argument strings,
216   /// which are guaranteed to be the first strings in the argument string
217   /// list.
218   virtual unsigned getNumInputArgStrings() const = 0;
219
220   /// @}
221   /// @name Argument Lookup Utilities
222   /// @{
223
224   /// getLastArgValue - Return the value of the last argument, or a default.
225   StringRef getLastArgValue(OptSpecifier Id,
226                                   StringRef Default = "") const;
227
228   /// getAllArgValues - Get the values of all instances of the given argument
229   /// as strings.
230   std::vector<std::string> getAllArgValues(OptSpecifier Id) const;
231
232   /// @}
233   /// @name Translation Utilities
234   /// @{
235
236   /// hasFlag - Given an option \p Pos and its negative form \p Neg, return
237   /// true if the option is present, false if the negation is present, and
238   /// \p Default if neither option is given. If both the option and its
239   /// negation are present, the last one wins.
240   bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default=true) const;
241
242   /// hasFlag - Given an option \p Pos, an alias \p PosAlias and its negative
243   /// form \p Neg, return true if the option or its alias is present, false if
244   /// the negation is present, and \p Default if none of the options are
245   /// given. If multiple options are present, the last one wins.
246   bool hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
247                bool Default = true) const;
248
249   /// AddLastArg - Render only the last argument match \p Id0, if present.
250   void AddLastArg(ArgStringList &Output, OptSpecifier Id0) const;
251   void AddLastArg(ArgStringList &Output, OptSpecifier Id0,
252                   OptSpecifier Id1) const;
253
254   /// AddAllArgs - Render all arguments matching the given ids.
255   void AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
256                   OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
257
258   /// AddAllArgValues - Render the argument values of all arguments
259   /// matching the given ids.
260   void AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
261                         OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
262
263   /// AddAllArgsTranslated - Render all the arguments matching the
264   /// given ids, but forced to separate args and using the provided
265   /// name instead of the first option value.
266   ///
267   /// \param Joined - If true, render the argument as joined with
268   /// the option specifier.
269   void AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
270                             const char *Translation,
271                             bool Joined = false) const;
272
273   /// ClaimAllArgs - Claim all arguments which match the given
274   /// option id.
275   void ClaimAllArgs(OptSpecifier Id0) const;
276
277   /// ClaimAllArgs - Claim all arguments.
278   ///
279   void ClaimAllArgs() const;
280
281   /// @}
282   /// @name Arg Synthesis
283   /// @{
284
285   /// Construct a constant string pointer whose
286   /// lifetime will match that of the ArgList.
287   virtual const char *MakeArgStringRef(StringRef Str) const = 0;
288   const char *MakeArgString(const Twine &Str) const {
289     SmallString<256> Buf;
290     return MakeArgStringRef(Str.toStringRef(Buf));
291   }
292
293   /// \brief Create an arg string for (\p LHS + \p RHS), reusing the
294   /// string at \p Index if possible.
295   const char *GetOrMakeJoinedArgString(unsigned Index, StringRef LHS,
296                                         StringRef RHS) const;
297
298   /// @}
299 };
300
301 class InputArgList final : public ArgList {
302 private:
303   /// List of argument strings used by the contained Args.
304   ///
305   /// This is mutable since we treat the ArgList as being the list
306   /// of Args, and allow routines to add new strings (to have a
307   /// convenient place to store the memory) via MakeIndex.
308   mutable ArgStringList ArgStrings;
309
310   /// Strings for synthesized arguments.
311   ///
312   /// This is mutable since we treat the ArgList as being the list
313   /// of Args, and allow routines to add new strings (to have a
314   /// convenient place to store the memory) via MakeIndex.
315   mutable std::list<std::string> SynthesizedStrings;
316
317   /// The number of original input argument strings.
318   unsigned NumInputArgStrings;
319
320 public:
321   InputArgList(const char* const *ArgBegin, const char* const *ArgEnd);
322   ~InputArgList();
323
324   const char *getArgString(unsigned Index) const override {
325     return ArgStrings[Index];
326   }
327
328   unsigned getNumInputArgStrings() const override {
329     return NumInputArgStrings;
330   }
331
332   /// @name Arg Synthesis
333   /// @{
334
335 public:
336   /// MakeIndex - Get an index for the given string(s).
337   unsigned MakeIndex(StringRef String0) const;
338   unsigned MakeIndex(StringRef String0, StringRef String1) const;
339
340   using ArgList::MakeArgString;
341   const char *MakeArgStringRef(StringRef Str) const override;
342
343   /// @}
344 };
345
346 /// DerivedArgList - An ordered collection of driver arguments,
347 /// whose storage may be in another argument list.
348 class DerivedArgList final : public ArgList {
349   const InputArgList &BaseArgs;
350
351   /// The list of arguments we synthesized.
352   mutable SmallVector<std::unique_ptr<Arg>, 16> SynthesizedArgs;
353
354 public:
355   /// Construct a new derived arg list from \p BaseArgs.
356   DerivedArgList(const InputArgList &BaseArgs);
357
358   const char *getArgString(unsigned Index) const override {
359     return BaseArgs.getArgString(Index);
360   }
361
362   unsigned getNumInputArgStrings() const override {
363     return BaseArgs.getNumInputArgStrings();
364   }
365
366   const InputArgList &getBaseArgs() const {
367     return BaseArgs;
368   }
369
370   /// @name Arg Synthesis
371   /// @{
372
373   /// AddSynthesizedArg - Add a argument to the list of synthesized arguments
374   /// (to be freed).
375   void AddSynthesizedArg(Arg *A);
376
377   using ArgList::MakeArgString;
378   const char *MakeArgStringRef(StringRef Str) const override;
379
380   /// AddFlagArg - Construct a new FlagArg for the given option \p Id and
381   /// append it to the argument list.
382   void AddFlagArg(const Arg *BaseArg, const Option Opt) {
383     append(MakeFlagArg(BaseArg, Opt));
384   }
385
386   /// AddPositionalArg - Construct a new Positional arg for the given option
387   /// \p Id, with the provided \p Value and append it to the argument
388   /// list.
389   void AddPositionalArg(const Arg *BaseArg, const Option Opt,
390                         StringRef Value) {
391     append(MakePositionalArg(BaseArg, Opt, Value));
392   }
393
394
395   /// AddSeparateArg - Construct a new Positional arg for the given option
396   /// \p Id, with the provided \p Value and append it to the argument
397   /// list.
398   void AddSeparateArg(const Arg *BaseArg, const Option Opt,
399                       StringRef Value) {
400     append(MakeSeparateArg(BaseArg, Opt, Value));
401   }
402
403
404   /// AddJoinedArg - Construct a new Positional arg for the given option
405   /// \p Id, with the provided \p Value and append it to the argument list.
406   void AddJoinedArg(const Arg *BaseArg, const Option Opt,
407                     StringRef Value) {
408     append(MakeJoinedArg(BaseArg, Opt, Value));
409   }
410
411
412   /// MakeFlagArg - Construct a new FlagArg for the given option \p Id.
413   Arg *MakeFlagArg(const Arg *BaseArg, const Option Opt) const;
414
415   /// MakePositionalArg - Construct a new Positional arg for the
416   /// given option \p Id, with the provided \p Value.
417   Arg *MakePositionalArg(const Arg *BaseArg, const Option Opt,
418                           StringRef Value) const;
419
420   /// MakeSeparateArg - Construct a new Positional arg for the
421   /// given option \p Id, with the provided \p Value.
422   Arg *MakeSeparateArg(const Arg *BaseArg, const Option Opt,
423                         StringRef Value) const;
424
425   /// MakeJoinedArg - Construct a new Positional arg for the
426   /// given option \p Id, with the provided \p Value.
427   Arg *MakeJoinedArg(const Arg *BaseArg, const Option Opt,
428                       StringRef Value) const;
429
430   /// @}
431 };
432
433 } // end namespace opt
434 } // end namespace llvm
435
436 #endif