]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - utils/TableGen/OptParserEmitter.cpp
Update LLVM to 91430.
[FreeBSD/FreeBSD.git] / 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 "OptParserEmitter.h"
11 #include "Record.h"
12 #include "llvm/ADT/STLExtras.h"
13 using namespace llvm;
14
15 static int StrCmpOptionName(const char *A, const char *B) {
16   char a = *A, b = *B;
17   while (a == b) {
18     if (a == '\0')
19       return 0;
20
21     a = *++A;
22     b = *++B;
23   }
24
25   if (a == '\0') // A is a prefix of B.
26     return 1;
27   if (b == '\0') // B is a prefix of A.
28     return -1;
29
30   // Otherwise lexicographic.
31   return (a < b) ? -1 : 1;
32 }
33
34 static int CompareOptionRecords(const void *Av, const void *Bv) {
35   const Record *A = *(Record**) Av;
36   const Record *B = *(Record**) Bv;
37
38   // Sentinel options preceed all others and are only ordered by precedence.
39   bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel");
40   bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel");
41   if (ASent != BSent)
42     return ASent ? -1 : 1;
43
44   // Compare options by name, unless they are sentinels.
45   if (!ASent)
46     if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(),
47                                    B->getValueAsString("Name").c_str()))
48     return Cmp;
49
50   // Then by the kind precedence;
51   int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence");
52   int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence");
53   assert(APrec != BPrec && "Options are equivalent!");
54   return APrec < BPrec ? -1 : 1;
55 }
56
57 static const std::string getOptionName(const Record &R) {
58   // Use the record name unless EnumName is defined.
59   if (dynamic_cast<UnsetInit*>(R.getValueInit("EnumName")))
60     return R.getName();
61
62   return R.getValueAsString("EnumName");
63 }
64
65 static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
66   OS << '"';
67   OS.write_escaped(Str);
68   OS << '"';
69   return OS;
70 }
71
72 void OptParserEmitter::run(raw_ostream &OS) {
73   // Get the option groups and options.
74   const std::vector<Record*> &Groups =
75     Records.getAllDerivedDefinitions("OptionGroup");
76   std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option");
77
78   if (GenDefs) {
79     OS << "\
80 //=== TableGen'erated File - Option Parsing Definitions ---------*- C++ -*-===//\n \
81 //\n\
82 // Option Parsing Definitions\n\
83 //\n\
84 // Automatically generated file, do not edit!\n\
85 //\n\
86 //===----------------------------------------------------------------------===//\n";
87   } else {
88     OS << "\
89 //=== TableGen'erated File - Option Parsing Table ---------------*- C++ -*-===//\n \
90 //\n\
91 // Option Parsing Definitions\n\
92 //\n\
93 // Automatically generated file, do not edit!\n\
94 //\n\
95 //===----------------------------------------------------------------------===//\n";
96   }
97   OS << "\n";
98
99   array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
100   if (GenDefs) {
101     OS << "#ifndef OPTION\n";
102     OS << "#error \"Define OPTION prior to including this file!\"\n";
103     OS << "#endif\n\n";
104
105     OS << "/////////\n";
106     OS << "// Groups\n\n";
107     for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
108       const Record &R = *Groups[i];
109
110       // Start a single option entry.
111       OS << "OPTION(";
112
113       // The option string.
114       OS << '"' << R.getValueAsString("Name") << '"';
115
116       // The option identifier name.
117       OS  << ", "<< getOptionName(R);
118
119       // The option kind.
120       OS << ", Group";
121
122       // The containing option group (if any).
123       OS << ", ";
124       if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group")))
125         OS << getOptionName(*DI->getDef());
126       else
127         OS << "INVALID";
128
129       // The other option arguments (unused for groups).
130       OS << ", INVALID, 0, 0";
131
132       // The option help text.
133       if (!dynamic_cast<UnsetInit*>(R.getValueInit("HelpText"))) {
134         OS << ",\n";
135         OS << "       ";
136         write_cstring(OS, R.getValueAsString("HelpText"));
137       } else
138         OS << ", 0";
139
140       // The option meta-variable name (unused).
141       OS << ", 0)\n";
142     }
143     OS << "\n";
144
145     OS << "//////////\n";
146     OS << "// Options\n\n";
147     for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
148       const Record &R = *Opts[i];
149
150       // Start a single option entry.
151       OS << "OPTION(";
152
153       // The option string.
154       write_cstring(OS, R.getValueAsString("Name"));
155
156       // The option identifier name.
157       OS  << ", "<< getOptionName(R);
158
159       // The option kind.
160       OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
161
162       // The containing option group (if any).
163       OS << ", ";
164       if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group")))
165         OS << getOptionName(*DI->getDef());
166       else
167         OS << "INVALID";
168
169       // The option alias (if any).
170       OS << ", ";
171       if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Alias")))
172         OS << getOptionName(*DI->getDef());
173       else
174         OS << "INVALID";
175
176       // The option flags.
177       const ListInit *LI = R.getValueAsListInit("Flags");
178       if (LI->empty()) {
179         OS << ", 0";
180       } else {
181         OS << ", ";
182         for (unsigned i = 0, e = LI->size(); i != e; ++i) {
183           if (i)
184             OS << " | ";
185           OS << dynamic_cast<DefInit*>(LI->getElement(i))->getDef()->getName();
186         }
187       }
188
189       // The option parameter field.
190       OS << ", " << R.getValueAsInt("NumArgs");
191
192       // The option help text.
193       if (!dynamic_cast<UnsetInit*>(R.getValueInit("HelpText"))) {
194         OS << ",\n";
195         OS << "       ";
196         write_cstring(OS, R.getValueAsString("HelpText"));
197       } else
198         OS << ", 0";
199
200       // The option meta-variable name.
201       OS << ", ";
202       if (!dynamic_cast<UnsetInit*>(R.getValueInit("MetaVarName")))
203         write_cstring(OS, R.getValueAsString("MetaVarName"));
204       else
205         OS << "0";
206
207       OS << ")\n";
208     }
209   }
210 }