1 //===- OptParserEmitter.cpp - Table Driven Command Line Parsing -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/TableGen/Error.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/TableGen/Record.h"
15 #include "llvm/TableGen/TableGenBackend.h"
20 static int StrCmpOptionName(const char *A, const char *B) {
30 if (a == '\0') // A is a prefix of B.
32 if (b == '\0') // B is a prefix of A.
35 // Otherwise lexicographic.
36 return (a < b) ? -1 : 1;
39 static int CompareOptionRecords(const void *Av, const void *Bv) {
40 const Record *A = *(const Record*const*) Av;
41 const Record *B = *(const Record*const*) Bv;
43 // Sentinel options precede all others and are only ordered by precedence.
44 bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel");
45 bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel");
47 return ASent ? -1 : 1;
49 // Compare options by name, unless they are sentinels.
51 if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(),
52 B->getValueAsString("Name").c_str()))
56 std::vector<std::string> APrefixes = A->getValueAsListOfStrings("Prefixes");
57 std::vector<std::string> BPrefixes = B->getValueAsListOfStrings("Prefixes");
59 for (std::vector<std::string>::const_iterator APre = APrefixes.begin(),
60 AEPre = APrefixes.end(),
61 BPre = BPrefixes.begin(),
62 BEPre = BPrefixes.end();
66 if (int Cmp = StrCmpOptionName(APre->c_str(), BPre->c_str()))
71 // Then by the kind precedence;
72 int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence");
73 int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence");
75 A->getValueAsListOfStrings("Prefixes") ==
76 B->getValueAsListOfStrings("Prefixes")) {
77 PrintError(A->getLoc(), Twine("Option is equivilent to"));
78 PrintError(B->getLoc(), Twine("Other defined here"));
79 PrintFatalError("Equivalent Options found.");
81 return APrec < BPrec ? -1 : 1;
84 static const std::string getOptionName(const Record &R) {
85 // Use the record name unless EnumName is defined.
86 if (isa<UnsetInit>(R.getValueInit("EnumName")))
89 return R.getValueAsString("EnumName");
92 static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
94 OS.write_escaped(Str);
99 /// OptParserEmitter - This tablegen backend takes an input .td file
100 /// describing a list of options and emits a data structure for parsing and
101 /// working with those options when given an input command line.
103 void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) {
104 // Get the option groups and options.
105 const std::vector<Record*> &Groups =
106 Records.getAllDerivedDefinitions("OptionGroup");
107 std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option");
109 emitSourceFileHeader("Option Parsing Definitions", OS);
111 array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
112 // Generate prefix groups.
113 typedef SmallVector<SmallString<2>, 2> PrefixKeyT;
114 typedef std::map<PrefixKeyT, std::string> PrefixesT;
116 Prefixes.insert(std::make_pair(PrefixKeyT(), "prefix_0"));
117 unsigned CurPrefix = 0;
118 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
119 const Record &R = *Opts[i];
120 std::vector<std::string> prf = R.getValueAsListOfStrings("Prefixes");
121 PrefixKeyT prfkey(prf.begin(), prf.end());
122 unsigned NewPrefix = CurPrefix + 1;
123 if (Prefixes.insert(std::make_pair(prfkey, (Twine("prefix_") +
124 Twine(NewPrefix)).str())).second)
125 CurPrefix = NewPrefix;
131 OS << "// Prefixes\n\n";
132 OS << "#ifdef PREFIX\n";
133 OS << "#define COMMA ,\n";
134 for (PrefixesT::const_iterator I = Prefixes.begin(), E = Prefixes.end();
143 for (PrefixKeyT::const_iterator PI = I->first.begin(),
144 PE = I->first.end(); PI != PE; ++PI) {
145 OS << "\"" << *PI << "\" COMMA ";
149 OS << "#undef COMMA\n";
153 OS << "// Groups\n\n";
154 OS << "#ifdef OPTION\n";
155 for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
156 const Record &R = *Groups[i];
158 // Start a single option entry.
161 // The option prefix;
164 // The option string.
165 OS << ", \"" << R.getValueAsString("Name") << '"';
167 // The option identifier name.
168 OS << ", "<< getOptionName(R);
173 // The containing option group (if any).
175 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
176 OS << getOptionName(*DI->getDef());
180 // The other option arguments (unused for groups).
181 OS << ", INVALID, 0, 0";
183 // The option help text.
184 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
187 write_cstring(OS, R.getValueAsString("HelpText"));
191 // The option meta-variable name (unused).
196 OS << "//////////\n";
197 OS << "// Options\n\n";
198 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
199 const Record &R = *Opts[i];
201 // Start a single option entry.
204 // The option prefix;
205 std::vector<std::string> prf = R.getValueAsListOfStrings("Prefixes");
206 OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", ";
208 // The option string.
209 write_cstring(OS, R.getValueAsString("Name"));
211 // The option identifier name.
212 OS << ", "<< getOptionName(R);
215 OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
217 // The containing option group (if any).
219 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
220 OS << getOptionName(*DI->getDef());
224 // The option alias (if any).
226 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias")))
227 OS << getOptionName(*DI->getDef());
232 const ListInit *LI = R.getValueAsListInit("Flags");
237 for (unsigned i = 0, e = LI->size(); i != e; ++i) {
240 OS << cast<DefInit>(LI->getElement(i))->getDef()->getName();
244 // The option parameter field.
245 OS << ", " << R.getValueAsInt("NumArgs");
247 // The option help text.
248 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
251 write_cstring(OS, R.getValueAsString("HelpText"));
255 // The option meta-variable name.
257 if (!isa<UnsetInit>(R.getValueInit("MetaVarName")))
258 write_cstring(OS, R.getValueAsString("MetaVarName"));
266 } // end namespace llvm