]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/utils/TableGen/OptParserEmitter.cpp
MFC r244628:
[FreeBSD/stable/9.git] / contrib / llvm / tools / clang / utils / TableGen / OptParserEmitter.cpp
1 //===- OptParserEmitter.cpp - Table Driven Command Line Parsing -----------===//
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 #include "llvm/TableGen/Error.h"
11 #include "llvm/TableGen/Record.h"
12 #include "llvm/TableGen/TableGenBackend.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/Twine.h"
16
17 #include <map>
18
19 using namespace llvm;
20
21 static int StrCmpOptionName(const char *A, const char *B) {
22   char a = *A, b = *B;
23   while (a == b) {
24     if (a == '\0')
25       return 0;
26
27     a = *++A;
28     b = *++B;
29   }
30
31   if (a == '\0') // A is a prefix of B.
32     return 1;
33   if (b == '\0') // B is a prefix of A.
34     return -1;
35
36   // Otherwise lexicographic.
37   return (a < b) ? -1 : 1;
38 }
39
40 static int CompareOptionRecords(const void *Av, const void *Bv) {
41   const Record *A = *(const Record*const*) Av;
42   const Record *B = *(const Record*const*) Bv;
43
44   // Sentinel options precede all others and are only ordered by precedence.
45   bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel");
46   bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel");
47   if (ASent != BSent)
48     return ASent ? -1 : 1;
49
50   // Compare options by name, unless they are sentinels.
51   if (!ASent)
52     if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(),
53                                    B->getValueAsString("Name").c_str()))
54     return Cmp;
55
56   if (!ASent) {
57     std::vector<std::string> APrefixes = A->getValueAsListOfStrings("Prefixes");
58     std::vector<std::string> BPrefixes = B->getValueAsListOfStrings("Prefixes");
59
60     for (std::vector<std::string>::const_iterator APre = APrefixes.begin(),
61                                                   AEPre = APrefixes.end(),
62                                                   BPre = BPrefixes.begin(),
63                                                   BEPre = BPrefixes.end();
64                                                   APre != AEPre &&
65                                                   BPre != BEPre;
66                                                   ++APre, ++BPre) {
67       if (int Cmp = StrCmpOptionName(APre->c_str(), BPre->c_str()))
68         return Cmp;
69     }
70   }
71
72   // Then by the kind precedence;
73   int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence");
74   int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence");
75   if (APrec == BPrec &&
76       A->getValueAsListOfStrings("Prefixes") ==
77       B->getValueAsListOfStrings("Prefixes")) {
78     PrintError(A->getLoc(), Twine("Option is equivilent to"));
79     PrintError(B->getLoc(), Twine("Other defined here"));
80     PrintFatalError("Equivalent Options found.");
81   }
82   return APrec < BPrec ? -1 : 1;
83 }
84
85 static const std::string getOptionName(const Record &R) {
86   // Use the record name unless EnumName is defined.
87   if (isa<UnsetInit>(R.getValueInit("EnumName")))
88     return R.getName();
89
90   return R.getValueAsString("EnumName");
91 }
92
93 static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
94   OS << '"';
95   OS.write_escaped(Str);
96   OS << '"';
97   return OS;
98 }
99
100 /// OptParserEmitter - This tablegen backend takes an input .td file
101 /// describing a list of options and emits a data structure for parsing and
102 /// working with those options when given an input command line.
103 namespace clang {
104 void EmitOptParser(RecordKeeper &Records, raw_ostream &OS, bool GenDefs) {
105   // Get the option groups and options.
106   const std::vector<Record*> &Groups =
107     Records.getAllDerivedDefinitions("OptionGroup");
108   std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option");
109
110   if (GenDefs)
111     emitSourceFileHeader("Option Parsing Definitions", OS);
112   else
113     emitSourceFileHeader("Option Parsing Table", OS);
114
115   array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
116   if (GenDefs) {
117     // Generate prefix groups.
118     typedef SmallVector<SmallString<2>, 2> PrefixKeyT;
119     typedef std::map<PrefixKeyT, std::string> PrefixesT;
120     PrefixesT Prefixes;
121     Prefixes.insert(std::make_pair(PrefixKeyT(), "prefix_0"));
122     unsigned CurPrefix = 0;
123     for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
124       const Record &R = *Opts[i];
125       std::vector<std::string> prf = R.getValueAsListOfStrings("Prefixes");
126       PrefixKeyT prfkey(prf.begin(), prf.end());
127       unsigned NewPrefix = CurPrefix + 1;
128       if (Prefixes.insert(std::make_pair(prfkey, (Twine("prefix_") +
129                                                Twine(NewPrefix)).str())).second)
130         CurPrefix = NewPrefix;
131     }
132
133     OS << "#ifndef PREFIX\n";
134     OS << "#error \"Define PREFIX prior to including this file!\"\n";
135     OS << "#endif\n\n";
136
137     // Dump prefixes.
138     OS << "/////////\n";
139     OS << "// Prefixes\n\n";
140     OS << "#define COMMA ,\n";
141     for (PrefixesT::const_iterator I = Prefixes.begin(), E = Prefixes.end();
142                                    I != E; ++I) {
143       OS << "PREFIX(";
144
145       // Prefix name.
146       OS << I->second;
147
148       // Prefix values.
149       OS << ", {";
150       for (PrefixKeyT::const_iterator PI = I->first.begin(),
151                                       PE = I->first.end(); PI != PE; ++PI) {
152         OS << "\"" << *PI << "\" COMMA ";
153       }
154       OS << "0})\n";
155     }
156     OS << "#undef COMMA\n";
157     OS << "\n";
158
159     OS << "#ifndef OPTION\n";
160     OS << "#error \"Define OPTION prior to including this file!\"\n";
161     OS << "#endif\n\n";
162
163     OS << "/////////\n";
164     OS << "// Groups\n\n";
165     for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
166       const Record &R = *Groups[i];
167
168       // Start a single option entry.
169       OS << "OPTION(";
170
171       // The option prefix;
172       OS << "0";
173
174       // The option string.
175       OS << ", \"" << R.getValueAsString("Name") << '"';
176
177       // The option identifier name.
178       OS  << ", "<< getOptionName(R);
179
180       // The option kind.
181       OS << ", Group";
182
183       // The containing option group (if any).
184       OS << ", ";
185       if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
186         OS << getOptionName(*DI->getDef());
187       else
188         OS << "INVALID";
189
190       // The other option arguments (unused for groups).
191       OS << ", INVALID, 0, 0";
192
193       // The option help text.
194       if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
195         OS << ",\n";
196         OS << "       ";
197         write_cstring(OS, R.getValueAsString("HelpText"));
198       } else
199         OS << ", 0";
200
201       // The option meta-variable name (unused).
202       OS << ", 0)\n";
203     }
204     OS << "\n";
205
206     OS << "//////////\n";
207     OS << "// Options\n\n";
208     for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
209       const Record &R = *Opts[i];
210
211       // Start a single option entry.
212       OS << "OPTION(";
213
214       // The option prefix;
215       std::vector<std::string> prf = R.getValueAsListOfStrings("Prefixes");
216       OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", ";
217
218       // The option string.
219       write_cstring(OS, R.getValueAsString("Name"));
220
221       // The option identifier name.
222       OS  << ", "<< getOptionName(R);
223
224       // The option kind.
225       OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
226
227       // The containing option group (if any).
228       OS << ", ";
229       if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
230         OS << getOptionName(*DI->getDef());
231       else
232         OS << "INVALID";
233
234       // The option alias (if any).
235       OS << ", ";
236       if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias")))
237         OS << getOptionName(*DI->getDef());
238       else
239         OS << "INVALID";
240
241       // The option flags.
242       const ListInit *LI = R.getValueAsListInit("Flags");
243       if (LI->empty()) {
244         OS << ", 0";
245       } else {
246         OS << ", ";
247         for (unsigned i = 0, e = LI->size(); i != e; ++i) {
248           if (i)
249             OS << " | ";
250           OS << cast<DefInit>(LI->getElement(i))->getDef()->getName();
251         }
252       }
253
254       // The option parameter field.
255       OS << ", " << R.getValueAsInt("NumArgs");
256
257       // The option help text.
258       if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
259         OS << ",\n";
260         OS << "       ";
261         write_cstring(OS, R.getValueAsString("HelpText"));
262       } else
263         OS << ", 0";
264
265       // The option meta-variable name.
266       OS << ", ";
267       if (!isa<UnsetInit>(R.getValueInit("MetaVarName")))
268         write_cstring(OS, R.getValueAsString("MetaVarName"));
269       else
270         OS << "0";
271
272       OS << ")\n";
273     }
274   }
275 }
276 } // end namespace clang