Merge changes to clang's Driver code into LLVM's Option library
[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/StringRef.h"
15 #include "llvm/Option/OptSpecifier.h"
16 #include "llvm/Option/Option.h"
17 #include <list>
18 #include <string>
19 #include <vector>
20
21 namespace llvm {
22 namespace opt {
23 class Arg;
24 class ArgList;
25 class Option;
26
27 /// arg_iterator - Iterates through arguments stored inside an ArgList.
28 class arg_iterator {
29   /// The current argument.
30   SmallVectorImpl<Arg*>::const_iterator Current;
31
32   /// The argument list we are iterating over.
33   const ArgList &Args;
34
35   /// Optional filters on the arguments which will be match. Most clients
36   /// should never want to iterate over arguments without filters, so we won't
37   /// bother to factor this into two separate iterator implementations.
38   //
39   // FIXME: Make efficient; the idea is to provide efficient iteration over
40   // all arguments which match a particular id and then just provide an
41   // iterator combinator which takes multiple iterators which can be
42   // efficiently compared and returns them in order.
43   OptSpecifier Id0, Id1, Id2;
44
45   void SkipToNextArg();
46
47 public:
48   typedef Arg * const *                 value_type;
49   typedef Arg * const &                 reference;
50   typedef Arg * const *                 pointer;
51   typedef std::forward_iterator_tag   iterator_category;
52   typedef std::ptrdiff_t              difference_type;
53
54   arg_iterator(SmallVectorImpl<Arg*>::const_iterator it,
55                 const ArgList &_Args, OptSpecifier _Id0 = 0U,
56                 OptSpecifier _Id1 = 0U, OptSpecifier _Id2 = 0U)
57     : Current(it), Args(_Args), Id0(_Id0), Id1(_Id1), Id2(_Id2) {
58     SkipToNextArg();
59   }
60
61   operator const Arg*() { return *Current; }
62   reference operator*() const { return *Current; }
63   pointer operator->() const { return Current; }
64
65   arg_iterator &operator++() {
66     ++Current;
67     SkipToNextArg();
68     return *this;
69   }
70
71   arg_iterator operator++(int) {
72     arg_iterator tmp(*this);
73     ++(*this);
74     return tmp;
75   }
76
77   friend bool operator==(arg_iterator LHS, arg_iterator RHS) {
78     return LHS.Current == RHS.Current;
79   }
80   friend bool operator!=(arg_iterator LHS, arg_iterator RHS) {
81     return !(LHS == RHS);
82   }
83 };
84
85 /// ArgList - Ordered collection of driver arguments.
86 ///
87 /// The ArgList class manages a list of Arg instances as well as
88 /// auxiliary data and convenience methods to allow Tools to quickly
89 /// check for the presence of Arg instances for a particular Option
90 /// and to iterate over groups of arguments.
91 class ArgList {
92 private:
93   ArgList(const ArgList &) LLVM_DELETED_FUNCTION;
94   void operator=(const ArgList &) LLVM_DELETED_FUNCTION;
95
96 public:
97   typedef SmallVector<Arg*, 16> arglist_type;
98   typedef arglist_type::iterator iterator;
99   typedef arglist_type::const_iterator const_iterator;
100   typedef arglist_type::reverse_iterator reverse_iterator;
101   typedef arglist_type::const_reverse_iterator const_reverse_iterator;
102
103 private:
104   /// The internal list of arguments.
105   arglist_type Args;
106
107 protected:
108   ArgList();
109
110 public:
111   virtual ~ArgList();
112
113   /// @name Arg Access
114   /// @{
115
116   /// append - Append \p A to the arg list.
117   void append(Arg *A);
118
119   arglist_type &getArgs() { return Args; }
120   const arglist_type &getArgs() const { return Args; }
121
122   unsigned size() const { return Args.size(); }
123
124   /// @}
125   /// @name Arg Iteration
126   /// @{
127
128   iterator begin() { return Args.begin(); }
129   iterator end() { return Args.end(); }
130
131   reverse_iterator rbegin() { return Args.rbegin(); }
132   reverse_iterator rend() { return Args.rend(); }
133
134   const_iterator begin() const { return Args.begin(); }
135   const_iterator end() const { return Args.end(); }
136
137   const_reverse_iterator rbegin() const { return Args.rbegin(); }
138   const_reverse_iterator rend() const { return Args.rend(); }
139
140   arg_iterator filtered_begin(OptSpecifier Id0 = 0U, OptSpecifier Id1 = 0U,
141                               OptSpecifier Id2 = 0U) const {
142     return arg_iterator(Args.begin(), *this, Id0, Id1, Id2);
143   }
144   arg_iterator filtered_end() const {
145     return arg_iterator(Args.end(), *this);
146   }
147
148   /// @}
149   /// @name Arg Removal
150   /// @{
151
152   /// eraseArg - Remove any option matching \p Id.
153   void eraseArg(OptSpecifier Id);
154
155   /// @}
156   /// @name Arg Access
157   /// @{
158
159   /// hasArg - Does the arg list contain any option matching \p Id.
160   ///
161   /// \p Claim Whether the argument should be claimed, if it exists.
162   bool hasArgNoClaim(OptSpecifier Id) const {
163     return getLastArgNoClaim(Id) != 0;
164   }
165   bool hasArg(OptSpecifier Id) const {
166     return getLastArg(Id) != 0;
167   }
168   bool hasArg(OptSpecifier Id0, OptSpecifier Id1) const {
169     return getLastArg(Id0, Id1) != 0;
170   }
171   bool hasArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const {
172     return getLastArg(Id0, Id1, Id2) != 0;
173   }
174
175   /// getLastArg - Return the last argument matching \p Id, or null.
176   ///
177   /// \p Claim Whether the argument should be claimed, if it exists.
178   Arg *getLastArgNoClaim(OptSpecifier Id) const;
179   Arg *getLastArg(OptSpecifier Id) const;
180   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1) const;
181   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const;
182   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
183                   OptSpecifier Id3) const;
184   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
185                   OptSpecifier Id3, OptSpecifier Id4) const;
186   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
187                   OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5) const;
188   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
189                   OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
190                   OptSpecifier Id6) const;
191   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
192                   OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
193                   OptSpecifier Id6, OptSpecifier Id7) const;
194
195   /// getArgString - Return the input argument string at \p Index.
196   virtual const char *getArgString(unsigned Index) const = 0;
197
198   /// getNumInputArgStrings - Return the number of original argument strings,
199   /// which are guaranteed to be the first strings in the argument string
200   /// list.
201   virtual unsigned getNumInputArgStrings() const = 0;
202
203   /// @}
204   /// @name Argument Lookup Utilities
205   /// @{
206
207   /// getLastArgValue - Return the value of the last argument, or a default.
208   StringRef getLastArgValue(OptSpecifier Id,
209                                   StringRef Default = "") const;
210
211   /// getAllArgValues - Get the values of all instances of the given argument
212   /// as strings.
213   std::vector<std::string> getAllArgValues(OptSpecifier Id) const;
214
215   /// @}
216   /// @name Translation Utilities
217   /// @{
218
219   /// hasFlag - Given an option \p Pos and its negative form \p Neg, return
220   /// true if the option is present, false if the negation is present, and
221   /// \p Default if neither option is given. If both the option and its
222   /// negation are present, the last one wins.
223   bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default=true) const;
224
225   /// hasFlag - Given an option \p Pos, an alias \p PosAlias and its negative
226   /// form \p Neg, return true if the option or its alias is present, false if
227   /// the negation is present, and \p Default if none of the options are
228   /// given. If multiple options are present, the last one wins.
229   bool hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
230                bool Default = true) const;
231
232   /// AddLastArg - Render only the last argument match \p Id0, if present.
233   void AddLastArg(ArgStringList &Output, OptSpecifier Id0) const;
234   void AddLastArg(ArgStringList &Output, OptSpecifier Id0,
235                   OptSpecifier Id1) const;
236
237   /// AddAllArgs - Render all arguments matching the given ids.
238   void AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
239                   OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
240
241   /// AddAllArgValues - Render the argument values of all arguments
242   /// matching the given ids.
243   void AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
244                         OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
245
246   /// AddAllArgsTranslated - Render all the arguments matching the
247   /// given ids, but forced to separate args and using the provided
248   /// name instead of the first option value.
249   ///
250   /// \param Joined - If true, render the argument as joined with
251   /// the option specifier.
252   void AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
253                             const char *Translation,
254                             bool Joined = false) const;
255
256   /// ClaimAllArgs - Claim all arguments which match the given
257   /// option id.
258   void ClaimAllArgs(OptSpecifier Id0) const;
259
260   /// ClaimAllArgs - Claim all arguments.
261   ///
262   void ClaimAllArgs() const;
263
264   /// @}
265   /// @name Arg Synthesis
266   /// @{
267
268   /// MakeArgString - Construct a constant string pointer whose
269   /// lifetime will match that of the ArgList.
270   virtual const char *MakeArgString(StringRef Str) const = 0;
271   const char *MakeArgString(const char *Str) const {
272     return MakeArgString(StringRef(Str));
273   }
274   const char *MakeArgString(std::string Str) const {
275     return MakeArgString(StringRef(Str));
276   }
277   const char *MakeArgString(const Twine &Str) const;
278
279   /// \brief Create an arg string for (\p LHS + \p RHS), reusing the
280   /// string at \p Index if possible.
281   const char *GetOrMakeJoinedArgString(unsigned Index, StringRef LHS,
282                                         StringRef RHS) const;
283
284   /// @}
285 };
286
287 class InputArgList : public ArgList  {
288 private:
289   /// List of argument strings used by the contained Args.
290   ///
291   /// This is mutable since we treat the ArgList as being the list
292   /// of Args, and allow routines to add new strings (to have a
293   /// convenient place to store the memory) via MakeIndex.
294   mutable ArgStringList ArgStrings;
295
296   /// Strings for synthesized arguments.
297   ///
298   /// This is mutable since we treat the ArgList as being the list
299   /// of Args, and allow routines to add new strings (to have a
300   /// convenient place to store the memory) via MakeIndex.
301   mutable std::list<std::string> SynthesizedStrings;
302
303   /// The number of original input argument strings.
304   unsigned NumInputArgStrings;
305
306 public:
307   InputArgList(const char* const *ArgBegin, const char* const *ArgEnd);
308   ~InputArgList();
309
310   virtual const char *getArgString(unsigned Index) const {
311     return ArgStrings[Index];
312   }
313
314   virtual unsigned getNumInputArgStrings() const {
315     return NumInputArgStrings;
316   }
317
318   /// @name Arg Synthesis
319   /// @{
320
321 public:
322   /// MakeIndex - Get an index for the given string(s).
323   unsigned MakeIndex(StringRef String0) const;
324   unsigned MakeIndex(StringRef String0, StringRef String1) const;
325
326   virtual const char *MakeArgString(StringRef Str) const;
327
328   /// @}
329 };
330
331 /// DerivedArgList - An ordered collection of driver arguments,
332 /// whose storage may be in another argument list.
333 class DerivedArgList : public ArgList {
334   const InputArgList &BaseArgs;
335
336   /// The list of arguments we synthesized.
337   mutable arglist_type SynthesizedArgs;
338
339 public:
340   /// Construct a new derived arg list from \p BaseArgs.
341   DerivedArgList(const InputArgList &BaseArgs);
342   ~DerivedArgList();
343
344   virtual const char *getArgString(unsigned Index) const {
345     return BaseArgs.getArgString(Index);
346   }
347
348   virtual unsigned getNumInputArgStrings() const {
349     return BaseArgs.getNumInputArgStrings();
350   }
351
352   const InputArgList &getBaseArgs() const {
353     return BaseArgs;
354   }
355
356   /// @name Arg Synthesis
357   /// @{
358
359   /// AddSynthesizedArg - Add a argument to the list of synthesized arguments
360   /// (to be freed).
361   void AddSynthesizedArg(Arg *A) {
362     SynthesizedArgs.push_back(A);
363   }
364
365   virtual const char *MakeArgString(StringRef Str) const;
366
367   /// AddFlagArg - Construct a new FlagArg for the given option \p Id and
368   /// append it to the argument list.
369   void AddFlagArg(const Arg *BaseArg, const Option Opt) {
370     append(MakeFlagArg(BaseArg, Opt));
371   }
372
373   /// AddPositionalArg - Construct a new Positional arg for the given option
374   /// \p Id, with the provided \p Value and append it to the argument
375   /// list.
376   void AddPositionalArg(const Arg *BaseArg, const Option Opt,
377                         StringRef Value) {
378     append(MakePositionalArg(BaseArg, Opt, Value));
379   }
380
381
382   /// AddSeparateArg - Construct a new Positional arg for the given option
383   /// \p Id, with the provided \p Value and append it to the argument
384   /// list.
385   void AddSeparateArg(const Arg *BaseArg, const Option Opt,
386                       StringRef Value) {
387     append(MakeSeparateArg(BaseArg, Opt, Value));
388   }
389
390
391   /// AddJoinedArg - Construct a new Positional arg for the given option
392   /// \p Id, with the provided \p Value and append it to the argument list.
393   void AddJoinedArg(const Arg *BaseArg, const Option Opt,
394                     StringRef Value) {
395     append(MakeJoinedArg(BaseArg, Opt, Value));
396   }
397
398
399   /// MakeFlagArg - Construct a new FlagArg for the given option \p Id.
400   Arg *MakeFlagArg(const Arg *BaseArg, const Option Opt) const;
401
402   /// MakePositionalArg - Construct a new Positional arg for the
403   /// given option \p Id, with the provided \p Value.
404   Arg *MakePositionalArg(const Arg *BaseArg, const Option Opt,
405                           StringRef Value) const;
406
407   /// MakeSeparateArg - Construct a new Positional arg for the
408   /// given option \p Id, with the provided \p Value.
409   Arg *MakeSeparateArg(const Arg *BaseArg, const Option Opt,
410                         StringRef Value) const;
411
412   /// MakeJoinedArg - Construct a new Positional arg for the
413   /// given option \p Id, with the provided \p Value.
414   Arg *MakeJoinedArg(const Arg *BaseArg, const Option Opt,
415                       StringRef Value) const;
416
417   /// @}
418 };
419
420 } // end namespace opt
421 } // end namespace llvm
422
423 #endif