Documentation warning: \param, not \parm
[oota-llvm.git] / include / llvm / Option / Option.h
1 //===--- Option.h - Abstract Driver Options ---------------------*- 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_OPTION_H
11 #define LLVM_OPTION_OPTION_H
12
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Option/OptTable.h"
16 #include "llvm/Support/ErrorHandling.h"
17
18 namespace llvm {
19 namespace opt {
20 class Arg;
21 class ArgList;
22 /// ArgStringList - Type used for constructing argv lists for subprocesses.
23 typedef SmallVector<const char*, 16> ArgStringList;
24
25 /// Base flags for all options. Custom flags may be added after.
26 enum DriverFlag {
27   HelpHidden       = (1 << 0),
28   RenderAsInput    = (1 << 1),
29   RenderJoined     = (1 << 2),
30   RenderSeparate   = (1 << 3)
31 };
32
33 /// Option - Abstract representation for a single form of driver
34 /// argument.
35 ///
36 /// An Option class represents a form of option that the driver
37 /// takes, for example how many arguments the option has and how
38 /// they can be provided. Individual option instances store
39 /// additional information about what group the option is a member
40 /// of (if any), if the option is an alias, and a number of
41 /// flags. At runtime the driver parses the command line into
42 /// concrete Arg instances, each of which corresponds to a
43 /// particular Option instance.
44 class Option {
45 public:
46   enum OptionClass {
47     GroupClass = 0,
48     InputClass,
49     UnknownClass,
50     FlagClass,
51     JoinedClass,
52     SeparateClass,
53     CommaJoinedClass,
54     MultiArgClass,
55     JoinedOrSeparateClass,
56     JoinedAndSeparateClass
57   };
58
59   enum RenderStyleKind {
60     RenderCommaJoinedStyle,
61     RenderJoinedStyle,
62     RenderSeparateStyle,
63     RenderValuesStyle
64   };
65
66 protected:
67   const OptTable::Info *Info;
68   const OptTable *Owner;
69
70 public:
71   Option(const OptTable::Info *Info, const OptTable *Owner);
72   ~Option();
73
74   bool isValid() const {
75     return Info != 0;
76   }
77
78   unsigned getID() const {
79     assert(Info && "Must have a valid info!");
80     return Info->ID;
81   }
82
83   OptionClass getKind() const {
84     assert(Info && "Must have a valid info!");
85     return OptionClass(Info->Kind);
86   }
87
88   /// \brief Get the name of this option without any prefix.
89   StringRef getName() const {
90     assert(Info && "Must have a valid info!");
91     return Info->Name;
92   }
93
94   const Option getGroup() const {
95     assert(Info && "Must have a valid info!");
96     assert(Owner && "Must have a valid owner!");
97     return Owner->getOption(Info->GroupID);
98   }
99
100   const Option getAlias() const {
101     assert(Info && "Must have a valid info!");
102     assert(Owner && "Must have a valid owner!");
103     return Owner->getOption(Info->AliasID);
104   }
105
106   /// \brief Get the default prefix for this option.
107   StringRef getPrefix() const {
108     const char *Prefix = *Info->Prefixes;
109     return Prefix ? Prefix : StringRef();
110   }
111
112   /// \brief Get the name of this option with the default prefix.
113   std::string getPrefixedName() const {
114     std::string Ret = getPrefix();
115     Ret += getName();
116     return Ret;
117   }
118
119   unsigned getNumArgs() const { return Info->Param; }
120
121   bool hasNoOptAsInput() const { return Info->Flags & RenderAsInput;}
122
123   RenderStyleKind getRenderStyle() const {
124     if (Info->Flags & RenderJoined)
125       return RenderJoinedStyle;
126     if (Info->Flags & RenderSeparate)
127       return RenderSeparateStyle;
128     switch (getKind()) {
129     case GroupClass:
130     case InputClass:
131     case UnknownClass:
132       return RenderValuesStyle;
133     case JoinedClass:
134     case JoinedAndSeparateClass:
135       return RenderJoinedStyle;
136     case CommaJoinedClass:
137       return RenderCommaJoinedStyle;
138     case FlagClass:
139     case SeparateClass:
140     case MultiArgClass:
141     case JoinedOrSeparateClass:
142       return RenderSeparateStyle;
143     }
144     llvm_unreachable("Unexpected kind!");
145   }
146
147   /// Test if this option has the flag \a Val.
148   bool hasFlag(unsigned Val) const {
149     return Info->Flags & Val;
150   }
151
152   /// getUnaliasedOption - Return the final option this option
153   /// aliases (itself, if the option has no alias).
154   const Option getUnaliasedOption() const {
155     const Option Alias = getAlias();
156     if (Alias.isValid()) return Alias.getUnaliasedOption();
157     return *this;
158   }
159
160   /// getRenderName - Return the name to use when rendering this
161   /// option.
162   StringRef getRenderName() const {
163     return getUnaliasedOption().getName();
164   }
165
166   /// matches - Predicate for whether this option is part of the
167   /// given option (which may be a group).
168   ///
169   /// Note that matches against options which are an alias should never be
170   /// done -- aliases do not participate in matching and so such a query will
171   /// always be false.
172   bool matches(OptSpecifier ID) const;
173
174   /// accept - Potentially accept the current argument, returning a
175   /// new Arg instance, or 0 if the option does not accept this
176   /// argument (or the argument is missing values).
177   ///
178   /// If the option accepts the current argument, accept() sets
179   /// Index to the position where argument parsing should resume
180   /// (even if the argument is missing values).
181   ///
182   /// \param ArgSize The number of bytes taken up by the matched Option prefix
183   ///                and name. This is used to determine where joined values
184   ///                start.
185   Arg *accept(const ArgList &Args, unsigned &Index, unsigned ArgSize) const;
186
187   void dump() const;
188 };
189
190 } // end namespace opt
191 } // end namespace llvm
192
193 #endif