2 //***************************************************************************
8 // A representation of options for any program.
11 // 08/08/95 - adve - Created in the dHPF compiler
12 // 10/10/96 - mpal, dbaker - converted to const member functions.
13 // 10/19/96 - meven - slightly changed interface to accomodate
14 // arguments other than -X type options
15 // 07/15/01 - vadve - Copied to LLVM system and modified
17 //**************************************************************************/
19 #ifndef LLVM_SUPPORT_PROGRAMOPTIONS_H
20 #define LLVM_SUPPORT_PROGRAMOPTIONS_H
22 #include "llvm/Support/Unique.h"
26 template <> struct hash<string> {
27 size_t operator()(string const &str) const {
28 return hash<char const *>()(str.c_str());
34 //---------------------------------------------------------------------------
36 // Class: ProgramOptions
40 // Class Data Members:
41 // ProgramOptionsRepr* Internal representation of program options,
42 // accessible to derived classes.
44 // Base class for representing the set of options for a program.
46 //---------------------------------------------------------------------------
48 class ProgramOptions: public Unique {
50 /*ctor*/ ProgramOptions (int _argc,
53 /*dtor*/ ~ProgramOptions () {}
55 //--------------------------------------------------------------------
56 // Retrieving different kinds of arguments.
57 // The required argument is specified by the optionString.
58 //--------------------------------------------------------------------
60 const char* StringOptionValue(const char* optionString) const;
61 bool FlagOptionValue (const char* optionString) const;
62 double RealOptionValue (const char* optionString) const;
63 int IntOptionValue (const char* optionString) const;
65 bool OptionSpecified (const char* optionString) const;
67 //--------------------------------------------------------------------
68 // The name used to invoke this program.
69 //--------------------------------------------------------------------
70 const char* ProgramName () const;
72 //--------------------------------------------------------------------
73 // Access to unparsed arguments
74 //--------------------------------------------------------------------
75 int NumberOfOtherOptions() const;
76 const char* OtherOption(int i) const;
78 //--------------------------------------------------------------------
79 // Access to the original arguments
80 //--------------------------------------------------------------------
81 const char** GetOriginalArgs() const;
82 void PrintArgs(ostream &out) const;
84 //--------------------------------------------------------------------
85 // Derived classes may use PrintOptions in their own PrintUsage() fct
86 // to print information about optional, required, or additional
88 //--------------------------------------------------------------------
89 virtual void PrintOptions (ostream& stream) const;
90 virtual void Usage () const;
92 //--------------------------------------------------------------------
93 // Generate a human-friendly description of the options actually set.
94 // The vector returned contains a multiple of 3 of entries, entry 3n is
95 // the name of the option, entry 3n + 1 contains the description of
96 // the option and entry 3n + 2 contains the ascii value of the option.
97 // All entries are allocated using malloc and can be freed with 'free'.
98 //--------------------------------------------------------------------
99 virtual vector<string> GetDescription () const;
102 //--------------------------------------------------------------------
103 // Called by the subclass to register each possible option
104 // used by the program. Assumes ownership of the ProgramOption.
105 //--------------------------------------------------------------------
106 void Register (ProgramOption* option);
108 //--------------------------------------------------------------------
109 // Parses the options.
110 //--------------------------------------------------------------------
111 void ParseArgs (int argc,
115 inline ProgramOption* OptionHandler(const string &optString) {
116 hash_map<string, ProgramOption*>::iterator hp =
117 optionRegistry.find(optString);
118 return (hp != optionRegistry.end()) ? hp->second : 0;
120 inline const ProgramOption* OptionHandler(const string &optString) const {
121 hash_map<string, ProgramOption*>::const_iterator hp =
122 optionRegistry.find(optString);
123 return (hp != optionRegistry.end()) ? hp->second : 0;
126 //--------------------------------------------------------------------
127 // Functions that must be overridden by the subclass.
128 //--------------------------------------------------------------------
130 virtual void ParseExtraArgs () = 0; // called after successful ParseArgs
132 virtual void PrintUsage (ostream& stream) const = 0;
135 hash_map<string, ProgramOption*> optionRegistry;
142 //**************************************************************************/