blob: f8a964044fe8625b3d3d6dd23fe9c7624c05f5b7 [file] [log] [blame]
Mikhail Glushenkovfb37f392008-05-30 06:20:54 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000010// This tablegen backend is responsible for emitting LLVMC configuration code.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkovecbdcf22008-05-06 18:09:29 +000014#include "LLVMCConfigurationEmitter.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000015#include "Record.h"
16
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/StringMap.h"
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000021#include "llvm/ADT/StringSet.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000022#include <algorithm>
23#include <cassert>
24#include <functional>
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +000025#include <stdexcept>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000026#include <string>
Chris Lattner32a9e7a2008-06-04 04:46:14 +000027#include <typeinfo>
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +000028
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000029using namespace llvm;
30
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
32//===----------------------------------------------------------------------===//
33/// Typedefs
34
35typedef std::vector<Record*> RecordVector;
36typedef std::vector<std::string> StrVector;
37
38//===----------------------------------------------------------------------===//
39/// Constants
40
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +000041// Indentation.
Chris Lattnerec8e1b72009-11-03 18:30:31 +000042static const unsigned TabWidth = 4;
43static const unsigned Indent1 = TabWidth*1;
44static const unsigned Indent2 = TabWidth*2;
45static const unsigned Indent3 = TabWidth*3;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000046
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000047// Default help string.
Chris Lattnerec8e1b72009-11-03 18:30:31 +000048static const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000049
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000050// Name for the "sink" option.
Chris Lattnerec8e1b72009-11-03 18:30:31 +000051static const char * const SinkOptionName = "AutoGeneratedSinkOption";
52
53namespace {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000054
55//===----------------------------------------------------------------------===//
56/// Helper functions
57
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000058/// Id - An 'identity' function object.
59struct Id {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000060 template<typename T0>
61 void operator()(const T0&) const {
62 }
63 template<typename T0, typename T1>
64 void operator()(const T0&, const T1&) const {
65 }
66 template<typename T0, typename T1, typename T2>
67 void operator()(const T0&, const T1&, const T2&) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000068 }
69};
70
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000071int InitPtrToInt(const Init* ptr) {
72 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000073 return val.getValue();
74}
75
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000076const std::string& InitPtrToString(const Init* ptr) {
77 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
78 return val.getValue();
79}
80
81const ListInit& InitPtrToList(const Init* ptr) {
82 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
83 return val;
84}
85
86const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000087 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000088 return val;
89}
90
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000091const std::string GetOperatorName(const DagInit* D) {
92 return D->getOperator()->getAsString();
93}
94
95const std::string GetOperatorName(const DagInit& D) {
96 return GetOperatorName(&D);
97}
98
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000099// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +0000100// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +0000101void checkNumberOfArguments (const DagInit* d, unsigned minArgs) {
102 if (!d || d->getNumArgs() < minArgs)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000103 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000104}
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +0000105void checkNumberOfArguments (const DagInit& d, unsigned minArgs) {
106 checkNumberOfArguments(&d, minArgs);
107}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000108
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000109// isDagEmpty - is this DAG marked with an empty marker?
110bool isDagEmpty (const DagInit* d) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000111 return GetOperatorName(d) == "empty_dag_marker";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000112}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000113
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000114// EscapeVariableName - Escape commas and other symbols not allowed
115// in the C++ variable names. Makes it possible to use options named
116// like "Wa," (useful for prefix options).
117std::string EscapeVariableName(const std::string& Var) {
118 std::string ret;
119 for (unsigned i = 0; i != Var.size(); ++i) {
120 char cur_char = Var[i];
121 if (cur_char == ',') {
122 ret += "_comma_";
123 }
124 else if (cur_char == '+') {
125 ret += "_plus_";
126 }
127 else if (cur_char == '-') {
128 ret += "_dash_";
129 }
130 else {
131 ret.push_back(cur_char);
132 }
133 }
134 return ret;
135}
136
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000137/// oneOf - Does the input string contain this character?
138bool oneOf(const char* lst, char c) {
139 while (*lst) {
140 if (*lst++ == c)
141 return true;
142 }
143 return false;
144}
145
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000146template <class I, class S>
147void checkedIncrement(I& P, I E, S ErrorString) {
148 ++P;
149 if (P == E)
150 throw ErrorString;
151}
152
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000153// apply is needed because C++'s syntax doesn't let us construct a function
154// object and call it in the same statement.
155template<typename F, typename T0>
156void apply(F Fun, T0& Arg0) {
157 return Fun(Arg0);
158}
159
160template<typename F, typename T0, typename T1>
161void apply(F Fun, T0& Arg0, T1& Arg1) {
162 return Fun(Arg0, Arg1);
163}
164
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000165//===----------------------------------------------------------------------===//
166/// Back-end specific code
167
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000168
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000169/// OptionType - One of six different option types. See the
170/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000171namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000172
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000173 enum OptionType { Alias, Switch, Parameter, ParameterList,
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000174 Prefix, PrefixList};
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000175
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000176 bool IsAlias(OptionType t) {
177 return (t == Alias);
178 }
179
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000180 bool IsList (OptionType t) {
181 return (t == ParameterList || t == PrefixList);
182 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000183
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000184 bool IsSwitch (OptionType t) {
185 return (t == Switch);
186 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000187
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000188 bool IsParameter (OptionType t) {
189 return (t == Parameter || t == Prefix);
190 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000191
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000192}
193
194OptionType::OptionType stringToOptionType(const std::string& T) {
195 if (T == "alias_option")
196 return OptionType::Alias;
197 else if (T == "switch_option")
198 return OptionType::Switch;
199 else if (T == "parameter_option")
200 return OptionType::Parameter;
201 else if (T == "parameter_list_option")
202 return OptionType::ParameterList;
203 else if (T == "prefix_option")
204 return OptionType::Prefix;
205 else if (T == "prefix_list_option")
206 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000207 else
208 throw "Unknown option type: " + T + '!';
209}
210
211namespace OptionDescriptionFlags {
212 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000213 ReallyHidden = 0x4, Extern = 0x8,
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000214 OneOrMore = 0x10, ZeroOrOne = 0x20,
215 CommaSeparated = 0x40 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000216}
217
218/// OptionDescription - Represents data contained in a single
219/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000220struct OptionDescription {
221 OptionType::OptionType Type;
222 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000223 unsigned Flags;
224 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000225 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000226 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000227
228 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000229 const std::string& n = "",
230 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000231 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000232 {}
233
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000234 /// GenTypeDeclaration - Returns the C++ variable type of this
235 /// option.
236 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000237
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000238 /// GenVariableName - Returns the variable name used in the
239 /// generated C++ code.
240 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000241
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000242 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000243 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000244
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000245 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000246
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000247 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000248
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000249 bool isMultiVal() const;
250
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000251 bool isCommaSeparated() const;
252 void setCommaSeparated();
253
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000254 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000255 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000256
257 bool isRequired() const;
258 void setRequired();
259
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000260 bool isOneOrMore() const;
261 void setOneOrMore();
262
263 bool isZeroOrOne() const;
264 void setZeroOrOne();
265
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000266 bool isHidden() const;
267 void setHidden();
268
269 bool isReallyHidden() const;
270 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000271
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000272 bool isSwitch() const
273 { return OptionType::IsSwitch(this->Type); }
274
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000275 bool isParameter() const
276 { return OptionType::IsParameter(this->Type); }
277
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000278 bool isList() const
279 { return OptionType::IsList(this->Type); }
280
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000281};
282
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000283void OptionDescription::Merge (const OptionDescription& other)
284{
285 if (other.Type != Type)
286 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000287
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000288 if (Help == other.Help || Help == DefaultHelpString)
289 Help = other.Help;
290 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000291 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000292 " defined for option " + Name + "\n";
293 }
294
295 Flags |= other.Flags;
296}
297
298bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000299 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000300}
301
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000302bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000303 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000304}
305
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000306bool OptionDescription::isCommaSeparated() const {
307 return Flags & OptionDescriptionFlags::CommaSeparated;
308}
309void OptionDescription::setCommaSeparated() {
310 Flags |= OptionDescriptionFlags::CommaSeparated;
311}
312
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000313bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000314 return Flags & OptionDescriptionFlags::Extern;
315}
316void OptionDescription::setExtern() {
317 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000318}
319
320bool OptionDescription::isRequired() const {
321 return Flags & OptionDescriptionFlags::Required;
322}
323void OptionDescription::setRequired() {
324 Flags |= OptionDescriptionFlags::Required;
325}
326
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000327bool OptionDescription::isOneOrMore() const {
328 return Flags & OptionDescriptionFlags::OneOrMore;
329}
330void OptionDescription::setOneOrMore() {
331 Flags |= OptionDescriptionFlags::OneOrMore;
332}
333
334bool OptionDescription::isZeroOrOne() const {
335 return Flags & OptionDescriptionFlags::ZeroOrOne;
336}
337void OptionDescription::setZeroOrOne() {
338 Flags |= OptionDescriptionFlags::ZeroOrOne;
339}
340
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000341bool OptionDescription::isHidden() const {
342 return Flags & OptionDescriptionFlags::Hidden;
343}
344void OptionDescription::setHidden() {
345 Flags |= OptionDescriptionFlags::Hidden;
346}
347
348bool OptionDescription::isReallyHidden() const {
349 return Flags & OptionDescriptionFlags::ReallyHidden;
350}
351void OptionDescription::setReallyHidden() {
352 Flags |= OptionDescriptionFlags::ReallyHidden;
353}
354
355const char* OptionDescription::GenTypeDeclaration() const {
356 switch (Type) {
357 case OptionType::Alias:
358 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000359 case OptionType::PrefixList:
360 case OptionType::ParameterList:
361 return "cl::list<std::string>";
362 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000363 return "cl::opt<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000364 case OptionType::Parameter:
365 case OptionType::Prefix:
366 default:
367 return "cl::opt<std::string>";
368 }
369}
370
371std::string OptionDescription::GenVariableName() const {
372 const std::string& EscapedName = EscapeVariableName(Name);
373 switch (Type) {
374 case OptionType::Alias:
375 return "AutoGeneratedAlias_" + EscapedName;
376 case OptionType::PrefixList:
377 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000378 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000379 case OptionType::Switch:
380 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000381 case OptionType::Prefix:
382 case OptionType::Parameter:
383 default:
384 return "AutoGeneratedParameter_" + EscapedName;
385 }
386}
387
388/// OptionDescriptions - An OptionDescription array plus some helper
389/// functions.
390class OptionDescriptions {
391 typedef StringMap<OptionDescription> container_type;
392
393 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000394 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000395
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000396public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000397 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000398 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000399
400 // Wrappers for FindOption that throw an exception in case the option has a
401 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000402 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000403 const OptionDescription& FindParameter(const std::string& OptName) const;
404 const OptionDescription& FindList(const std::string& OptName) const;
405 const OptionDescription&
406 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000407
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000408 /// insertDescription - Insert new OptionDescription into
409 /// OptionDescriptions list
410 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000411
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000412 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000413 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000414 const_iterator begin() const { return Descriptions.begin(); }
415 const_iterator end() const { return Descriptions.end(); }
416};
417
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000418const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000419OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000420 const_iterator I = Descriptions.find(OptName);
421 if (I != Descriptions.end())
422 return I->second;
423 else
424 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000425}
426
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000427const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000428OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000429 const OptionDescription& OptDesc = this->FindOption(OptName);
430 if (!OptDesc.isSwitch())
431 throw OptName + ": incorrect option type - should be a switch!";
432 return OptDesc;
433}
434
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000435const OptionDescription&
436OptionDescriptions::FindList(const std::string& OptName) const {
437 const OptionDescription& OptDesc = this->FindOption(OptName);
438 if (!OptDesc.isList())
439 throw OptName + ": incorrect option type - should be a list!";
440 return OptDesc;
441}
442
443const OptionDescription&
444OptionDescriptions::FindParameter(const std::string& OptName) const {
445 const OptionDescription& OptDesc = this->FindOption(OptName);
446 if (!OptDesc.isParameter())
447 throw OptName + ": incorrect option type - should be a parameter!";
448 return OptDesc;
449}
450
451const OptionDescription&
452OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
453 const OptionDescription& OptDesc = this->FindOption(OptName);
454 if (!OptDesc.isList() && !OptDesc.isParameter())
455 throw OptName
456 + ": incorrect option type - should be a list or parameter!";
457 return OptDesc;
458}
459
460void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000461 container_type::iterator I = Descriptions.find(o.Name);
462 if (I != Descriptions.end()) {
463 OptionDescription& D = I->second;
464 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000465 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000466 else {
467 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000468 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000469}
470
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000471/// HandlerTable - A base class for function objects implemented as
472/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000473template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000474class HandlerTable {
475protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000476 // Implementation details.
477
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000478 /// HandlerMap - A map from property names to property handlers
479 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000480
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000481 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000482 static bool staticMembersInitialized_;
483
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000484public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000485
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000486 Handler GetHandler (const std::string& HandlerName) const {
487 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000488
489 if (method != Handlers_.end()) {
490 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000491 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000492 }
493 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000494 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000495 }
496 }
497
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000498 void AddHandler(const char* Property, Handler H) {
499 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000500 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000501
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000502};
503
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000504template <class FunctionObject>
505void InvokeDagInitHandler(FunctionObject* Obj, Init* i) {
506 typedef void (FunctionObject::*Handler) (const DagInit*);
507
508 const DagInit& property = InitPtrToDag(i);
509 const std::string& property_name = GetOperatorName(property);
510 Handler h = Obj->GetHandler(property_name);
511
512 ((Obj)->*(h))(&property);
513}
514
515template <typename H>
516typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
517
518template <typename H>
519bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000520
521
522/// CollectOptionProperties - Function object for iterating over an
523/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000524class CollectOptionProperties;
525typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
526(const DagInit*);
527
528class CollectOptionProperties
529: public HandlerTable<CollectOptionPropertiesHandler>
530{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000531private:
532
533 /// optDescs_ - OptionDescriptions table. This is where the
534 /// information is stored.
535 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000536
537public:
538
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000539 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000540 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000541 {
542 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000543 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000544 AddHandler("help", &CollectOptionProperties::onHelp);
545 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000546 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000547 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
548 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000549 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
550 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000551 AddHandler("zero_or_one", &CollectOptionProperties::onZeroOrOne);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000552 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000553
554 staticMembersInitialized_ = true;
555 }
556 }
557
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000558 /// operator() - Just forwards to the corresponding property
559 /// handler.
560 void operator() (Init* i) {
561 InvokeDagInitHandler(this, i);
562 }
563
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000564private:
565
566 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000567 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000568
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000569 void onExtern (const DagInit* d) {
570 checkNumberOfArguments(d, 0);
571 optDesc_.setExtern();
572 }
573
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000574 void onHelp (const DagInit* d) {
575 checkNumberOfArguments(d, 1);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000576 optDesc_.Help = InitPtrToString(d->getArg(0));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000577 }
578
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000579 void onHidden (const DagInit* d) {
580 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000581 optDesc_.setHidden();
582 }
583
584 void onReallyHidden (const DagInit* d) {
585 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000586 optDesc_.setReallyHidden();
587 }
588
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000589 void onCommaSeparated (const DagInit* d) {
590 checkNumberOfArguments(d, 0);
591 if (!optDesc_.isList())
592 throw "'comma_separated' is valid only on list options!";
593 optDesc_.setCommaSeparated();
594 }
595
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000596 void onRequired (const DagInit* d) {
597 checkNumberOfArguments(d, 0);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000598 if (optDesc_.isOneOrMore() || optDesc_.isZeroOrOne())
599 throw "Only one of (required), (zero_or_one) or "
600 "(one_or_more) properties is allowed!";
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000601 optDesc_.setRequired();
602 }
603
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000604 void onInit (const DagInit* d) {
605 checkNumberOfArguments(d, 1);
606 Init* i = d->getArg(0);
607 const std::string& str = i->getAsString();
608
609 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
610 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
611
612 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000613 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000614
615 optDesc_.InitVal = i;
616 }
617
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000618 void onOneOrMore (const DagInit* d) {
619 checkNumberOfArguments(d, 0);
620 if (optDesc_.isRequired() || optDesc_.isZeroOrOne())
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000621 throw "Only one of (required), (zero_or_one) or "
622 "(one_or_more) properties is allowed!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000623 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000624 llvm::errs() << "Warning: specifying the 'one_or_more' property "
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000625 "on a non-list option will have no effect.\n";
626 optDesc_.setOneOrMore();
627 }
628
629 void onZeroOrOne (const DagInit* d) {
630 checkNumberOfArguments(d, 0);
631 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000632 throw "Only one of (required), (zero_or_one) or "
633 "(one_or_more) properties is allowed!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000634 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000635 llvm::errs() << "Warning: specifying the 'zero_or_one' property"
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000636 "on a non-list option will have no effect.\n";
637 optDesc_.setZeroOrOne();
638 }
639
640 void onMultiVal (const DagInit* d) {
641 checkNumberOfArguments(d, 1);
642 int val = InitPtrToInt(d->getArg(0));
643 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000644 throw "Error in the 'multi_val' property: "
645 "the value must be greater than 1!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000646 if (!OptionType::IsList(optDesc_.Type))
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000647 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000648 optDesc_.MultiVal = val;
649 }
650
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000651};
652
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000653/// AddOption - A function object that is applied to every option
654/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000655class AddOption {
656private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000657 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000658
659public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000660 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000661 {}
662
663 void operator()(const Init* i) {
664 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000665 checkNumberOfArguments(&d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000666
667 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000668 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000669 const std::string& Name = InitPtrToString(d.getArg(0));
670
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000671 OptionDescription OD(Type, Name);
672
673 if (!OD.isExtern())
674 checkNumberOfArguments(&d, 2);
675
676 if (OD.isAlias()) {
677 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000678 OD.Help = InitPtrToString(d.getArg(1));
679 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000680 else if (!OD.isExtern()) {
681 processOptionProperties(&d, OD);
682 }
683 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000684 }
685
686private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000687 /// processOptionProperties - Go through the list of option
688 /// properties and call a corresponding handler for each.
689 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
690 checkNumberOfArguments(d, 2);
691 DagInit::const_arg_iterator B = d->arg_begin();
692 // Skip the first argument: it's always the option name.
693 ++B;
694 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000695 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000696
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000697};
698
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000699/// CollectOptionDescriptions - Collects option properties from all
700/// OptionLists.
701void CollectOptionDescriptions (RecordVector::const_iterator B,
702 RecordVector::const_iterator E,
703 OptionDescriptions& OptDescs)
704{
705 // For every OptionList:
706 for (; B!=E; ++B) {
707 RecordVector::value_type T = *B;
708 // Throws an exception if the value does not exist.
709 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000710
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000711 // For every option description in this list:
712 // collect the information and
713 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
714 }
715}
716
717// Tool information record
718
719namespace ToolFlags {
720 enum ToolFlags { Join = 0x1, Sink = 0x2 };
721}
722
723struct ToolDescription : public RefCountedBase<ToolDescription> {
724 std::string Name;
725 Init* CmdLine;
726 Init* Actions;
727 StrVector InLanguage;
728 std::string OutLanguage;
729 std::string OutputSuffix;
730 unsigned Flags;
731
732 // Various boolean properties
733 void setSink() { Flags |= ToolFlags::Sink; }
734 bool isSink() const { return Flags & ToolFlags::Sink; }
735 void setJoin() { Flags |= ToolFlags::Join; }
736 bool isJoin() const { return Flags & ToolFlags::Join; }
737
738 // Default ctor here is needed because StringMap can only store
739 // DefaultConstructible objects
740 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
741 ToolDescription (const std::string& n)
742 : Name(n), CmdLine(0), Actions(0), Flags(0)
743 {}
744};
745
746/// ToolDescriptions - A list of Tool information records.
747typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
748
749
750/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000751/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000752
753class CollectToolProperties;
754typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
755(const DagInit*);
756
757class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
758{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000759private:
760
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000761 /// toolDesc_ - Properties of the current Tool. This is where the
762 /// information is stored.
763 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000764
765public:
766
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000767 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000768 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000769 {
770 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000771
772 AddHandler("actions", &CollectToolProperties::onActions);
773 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
774 AddHandler("in_language", &CollectToolProperties::onInLanguage);
775 AddHandler("join", &CollectToolProperties::onJoin);
776 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
777 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
778 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000779
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000780 staticMembersInitialized_ = true;
781 }
782 }
783
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000784 void operator() (Init* i) {
785 InvokeDagInitHandler(this, i);
786 }
787
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000788private:
789
790 /// Property handlers --
791 /// Functions that extract information about tool properties from
792 /// DAG representation.
793
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000794 void onActions (const DagInit* d) {
795 checkNumberOfArguments(d, 1);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000796 Init* Case = d->getArg(0);
797 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000798 GetOperatorName(static_cast<DagInit*>(Case)) != "case")
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000799 throw
800 std::string("The argument to (actions) should be a 'case' construct!");
801 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000802 }
803
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000804 void onCmdLine (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000805 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000806 toolDesc_.CmdLine = d->getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000807 }
808
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000809 void onInLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000810 checkNumberOfArguments(d, 1);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000811 Init* arg = d->getArg(0);
812
813 // Find out the argument's type.
814 if (typeid(*arg) == typeid(StringInit)) {
815 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000816 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000817 }
818 else {
819 // It's a list.
820 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000821 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000822
823 // Copy strings to the output vector.
824 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
825 B != E; ++B) {
826 out.push_back(InitPtrToString(*B));
827 }
828
829 // Remove duplicates.
830 std::sort(out.begin(), out.end());
831 StrVector::iterator newE = std::unique(out.begin(), out.end());
832 out.erase(newE, out.end());
833 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000834 }
835
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000836 void onJoin (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000837 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000838 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000839 }
840
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000841 void onOutLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000842 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000843 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000844 }
845
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000846 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000847 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000848 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000849 }
850
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000851 void onSink (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000852 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000853 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000854 }
855
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000856};
857
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000858/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000859/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000860/// CollectToolProperties function object).
861void CollectToolDescriptions (RecordVector::const_iterator B,
862 RecordVector::const_iterator E,
863 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000864{
865 // Iterate over a properties list of every Tool definition
866 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000867 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000868 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000869 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000870
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000871 IntrusiveRefCntPtr<ToolDescription>
872 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000873
874 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000875 CollectToolProperties(*ToolDesc));
876 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000877 }
878}
879
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000880/// FillInEdgeVector - Merge all compilation graph definitions into
881/// one single edge list.
882void FillInEdgeVector(RecordVector::const_iterator B,
883 RecordVector::const_iterator E, RecordVector& Out) {
884 for (; B != E; ++B) {
885 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000886
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000887 for (unsigned i = 0; i < edges->size(); ++i)
888 Out.push_back(edges->getElementAsRecord(i));
889 }
890}
891
892/// CalculatePriority - Calculate the priority of this plugin.
893int CalculatePriority(RecordVector::const_iterator B,
894 RecordVector::const_iterator E) {
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000895 int priority = 0;
896
897 if (B != E) {
898 priority = static_cast<int>((*B)->getValueAsInt("priority"));
899
900 if (++B != E)
901 throw std::string("More than one 'PluginPriority' instance found: "
902 "most probably an error!");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000903 }
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000904
905 return priority;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000906}
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000907
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000908/// NotInGraph - Helper function object for FilterNotInGraph.
909struct NotInGraph {
910private:
911 const llvm::StringSet<>& ToolsInGraph_;
912
913public:
914 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
915 : ToolsInGraph_(ToolsInGraph)
916 {}
917
918 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
919 return (ToolsInGraph_.count(x->Name) == 0);
920 }
921};
922
923/// FilterNotInGraph - Filter out from ToolDescs all Tools not
924/// mentioned in the compilation graph definition.
925void FilterNotInGraph (const RecordVector& EdgeVector,
926 ToolDescriptions& ToolDescs) {
927
928 // List all tools mentioned in the graph.
929 llvm::StringSet<> ToolsInGraph;
930
931 for (RecordVector::const_iterator B = EdgeVector.begin(),
932 E = EdgeVector.end(); B != E; ++B) {
933
934 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000935 const std::string& NodeA = Edge->getValueAsString("a");
936 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000937
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000938 if (NodeA != "root")
939 ToolsInGraph.insert(NodeA);
940 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000941 }
942
943 // Filter ToolPropertiesList.
944 ToolDescriptions::iterator new_end =
945 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
946 NotInGraph(ToolsInGraph));
947 ToolDescs.erase(new_end, ToolDescs.end());
948}
949
950/// FillInToolToLang - Fills in two tables that map tool names to
951/// (input, output) languages. Helper function used by TypecheckGraph().
952void FillInToolToLang (const ToolDescriptions& ToolDescs,
953 StringMap<StringSet<> >& ToolToInLang,
954 StringMap<std::string>& ToolToOutLang) {
955 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
956 E = ToolDescs.end(); B != E; ++B) {
957 const ToolDescription& D = *(*B);
958 for (StrVector::const_iterator B = D.InLanguage.begin(),
959 E = D.InLanguage.end(); B != E; ++B)
960 ToolToInLang[D.Name].insert(*B);
961 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000962 }
963}
964
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000965/// TypecheckGraph - Check that names for output and input languages
966/// on all edges do match. This doesn't do much when the information
967/// about the whole graph is not available (i.e. when compiling most
968/// plugins).
969void TypecheckGraph (const RecordVector& EdgeVector,
970 const ToolDescriptions& ToolDescs) {
971 StringMap<StringSet<> > ToolToInLang;
972 StringMap<std::string> ToolToOutLang;
973
974 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
975 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
976 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
977
978 for (RecordVector::const_iterator B = EdgeVector.begin(),
979 E = EdgeVector.end(); B != E; ++B) {
980 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000981 const std::string& NodeA = Edge->getValueAsString("a");
982 const std::string& NodeB = Edge->getValueAsString("b");
983 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
984 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000985
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000986 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000987 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000988 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000989 + ": output->input language mismatch";
990 }
991
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000992 if (NodeB == "root")
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000993 throw std::string("Edges back to the root are not allowed!");
994 }
995}
996
997/// WalkCase - Walks the 'case' expression DAG and invokes
998/// TestCallback on every test, and StatementCallback on every
999/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001000/// combinators (that is, they are passed directly to TestCallback).
1001/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1002/// IndentLevel, bool FirstTest)'.
1003/// StatementCallback must have type 'void StatementCallback(const Init*,
1004/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001005template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001006void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1007 unsigned IndentLevel = 0)
1008{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001009 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001010
1011 // Error checks.
1012 if (GetOperatorName(d) != "case")
1013 throw std::string("WalkCase should be invoked only on 'case' expressions!");
1014
1015 if (d.getNumArgs() < 2)
1016 throw "There should be at least one clause in the 'case' expression:\n"
1017 + d.getAsString();
1018
1019 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001020 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001021 const unsigned numArgs = d.getNumArgs();
1022 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001023 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1024 B != E; ++B) {
1025 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001026
1027 if (!even)
1028 {
1029 // Handle test.
1030 const DagInit& Test = InitPtrToDag(arg);
1031
1032 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
1033 throw std::string("The 'default' clause should be the last in the"
1034 "'case' construct!");
1035 if (i == numArgs)
1036 throw "Case construct handler: no corresponding action "
1037 "found for the test " + Test.getAsString() + '!';
1038
1039 TestCallback(&Test, IndentLevel, (i == 1));
1040 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001041 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001042 {
1043 if (dynamic_cast<DagInit*>(arg)
1044 && GetOperatorName(static_cast<DagInit*>(arg)) == "case") {
1045 // Nested 'case'.
1046 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1047 }
1048
1049 // Handle statement.
1050 StatementCallback(arg, IndentLevel);
1051 }
1052
1053 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001054 even = !even;
1055 }
1056}
1057
1058/// ExtractOptionNames - A helper function object used by
1059/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1060class ExtractOptionNames {
1061 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001062
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001063 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001064 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001065 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001066 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001067 ActionName == "forward_value" ||
1068 ActionName == "forward_transformed_value" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001069 ActionName == "switch_on" || ActionName == "parameter_equals" ||
1070 ActionName == "element_in_list" || ActionName == "not_empty" ||
1071 ActionName == "empty") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001072 checkNumberOfArguments(&Stmt, 1);
1073 const std::string& Name = InitPtrToString(Stmt.getArg(0));
1074 OptionNames_.insert(Name);
1075 }
1076 else if (ActionName == "and" || ActionName == "or") {
1077 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001078 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001079 }
1080 }
1081 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001082
1083public:
1084 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1085 {}
1086
1087 void operator()(const Init* Statement) {
1088 if (typeid(*Statement) == typeid(ListInit)) {
1089 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1090 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1091 B != E; ++B)
1092 this->processDag(*B);
1093 }
1094 else {
1095 this->processDag(Statement);
1096 }
1097 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001098
1099 void operator()(const DagInit* Test, unsigned, bool) {
1100 this->operator()(Test);
1101 }
1102 void operator()(const Init* Statement, unsigned) {
1103 this->operator()(Statement);
1104 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001105};
1106
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001107/// CheckForSuperfluousOptions - Check that there are no side
1108/// effect-free options (specified only in the OptionList). Otherwise,
1109/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001110void CheckForSuperfluousOptions (const RecordVector& Edges,
1111 const ToolDescriptions& ToolDescs,
1112 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001113 llvm::StringSet<> nonSuperfluousOptions;
1114
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001115 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001116 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001117 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1118 E = ToolDescs.end(); B != E; ++B) {
1119 const ToolDescription& TD = *(*B);
1120 ExtractOptionNames Callback(nonSuperfluousOptions);
1121 if (TD.Actions)
1122 WalkCase(TD.Actions, Callback, Callback);
1123 }
1124
1125 // Add all options mentioned in the 'case' clauses of the
1126 // OptionalEdges of the compilation graph to the set of
1127 // non-superfluous options.
1128 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1129 B != E; ++B) {
1130 const Record* Edge = *B;
1131 DagInit* Weight = Edge->getValueAsDag("weight");
1132
1133 if (!isDagEmpty(Weight))
1134 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001135 }
1136
1137 // Check that all options in OptDescs belong to the set of
1138 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001139 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001140 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001141 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001142 if (!nonSuperfluousOptions.count(Val.Name)
1143 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001144 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001145 "Probable cause: this option is specified only in the OptionList.\n";
1146 }
1147}
1148
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001149/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1150bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1151 if (TestName == "single_input_file") {
1152 O << "InputFilenames.size() == 1";
1153 return true;
1154 }
1155 else if (TestName == "multiple_input_files") {
1156 O << "InputFilenames.size() > 1";
1157 return true;
1158 }
1159
1160 return false;
1161}
1162
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001163/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1164template <typename F>
1165void EmitListTest(const ListInit& L, const char* LogicOp,
1166 F Callback, raw_ostream& O)
1167{
1168 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1169 // of Dags...
1170 bool isFirst = true;
1171 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1172 if (isFirst)
1173 isFirst = false;
1174 else
1175 O << " || ";
1176 Callback(InitPtrToString(*B), O);
1177 }
1178}
1179
1180// Callbacks for use with EmitListTest.
1181
1182class EmitSwitchOn {
1183 const OptionDescriptions& OptDescs_;
1184public:
1185 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1186 {}
1187
1188 void operator()(const std::string& OptName, raw_ostream& O) const {
1189 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1190 O << OptDesc.GenVariableName();
1191 }
1192};
1193
1194class EmitEmptyTest {
1195 bool EmitNegate_;
1196 const OptionDescriptions& OptDescs_;
1197public:
1198 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1199 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1200 {}
1201
1202 void operator()(const std::string& OptName, raw_ostream& O) const {
1203 const char* Neg = (EmitNegate_ ? "!" : "");
1204 if (OptName == "o") {
1205 O << Neg << "OutputFilename.empty()";
1206 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001207 else if (OptName == "save-temps") {
1208 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1209 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001210 else {
1211 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1212 O << Neg << OptDesc.GenVariableName() << ".empty()";
1213 }
1214 }
1215};
1216
1217
1218/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1219bool EmitCaseTest1ArgList(const std::string& TestName,
1220 const DagInit& d,
1221 const OptionDescriptions& OptDescs,
1222 raw_ostream& O) {
1223 const ListInit& L = *static_cast<ListInit*>(d.getArg(0));
1224
1225 if (TestName == "any_switch_on") {
1226 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1227 return true;
1228 }
1229 else if (TestName == "switch_on") {
1230 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1231 return true;
1232 }
1233 else if (TestName == "any_not_empty") {
1234 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1235 return true;
1236 }
1237 else if (TestName == "any_empty") {
1238 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1239 return true;
1240 }
1241 else if (TestName == "not_empty") {
1242 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1243 return true;
1244 }
1245 else if (TestName == "empty") {
1246 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1247 return true;
1248 }
1249
1250 return false;
1251}
1252
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001253/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1254bool EmitCaseTest1ArgStr(const std::string& TestName,
1255 const DagInit& d,
1256 const OptionDescriptions& OptDescs,
1257 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001258 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001259
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001260 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001261 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001262 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001263 }
1264 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001265 O << "InLangs.count(\"" << OptName << "\") != 0";
1266 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001267 }
1268 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001269 // This works only for single-argument Tool::GenerateAction. Join
1270 // tools can process several files in different languages simultaneously.
1271
1272 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001273 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001274 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001275 }
1276 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001277 bool EmitNegate = (TestName == "not_empty");
1278 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001279 return true;
1280 }
1281
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001282 return false;
1283}
1284
1285/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1286bool EmitCaseTest1Arg(const std::string& TestName,
1287 const DagInit& d,
1288 const OptionDescriptions& OptDescs,
1289 raw_ostream& O) {
1290 checkNumberOfArguments(&d, 1);
1291 if (typeid(*d.getArg(0)) == typeid(ListInit))
1292 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1293 else
1294 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1295}
1296
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001297/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001298bool EmitCaseTest2Args(const std::string& TestName,
1299 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001300 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001301 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001302 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001303 checkNumberOfArguments(&d, 2);
1304 const std::string& OptName = InitPtrToString(d.getArg(0));
1305 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001306
1307 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001308 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001309 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1310 return true;
1311 }
1312 else if (TestName == "element_in_list") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001313 const OptionDescription& OptDesc = OptDescs.FindList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001314 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001315 O << "std::find(" << VarName << ".begin(),\n";
1316 O.indent(IndentLevel + Indent1)
1317 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001318 << OptArg << "\") != " << VarName << ".end()";
1319 return true;
1320 }
1321
1322 return false;
1323}
1324
1325// Forward declaration.
1326// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001327void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001328 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001329 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001330
1331/// EmitLogicalOperationTest - Helper function used by
1332/// EmitCaseConstructHandler.
1333void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001334 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001335 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001336 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001337 O << '(';
1338 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001339 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001340 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001341 if (j != NumArgs - 1) {
1342 O << ")\n";
1343 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1344 }
1345 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001346 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001347 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001348 }
1349}
1350
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001351void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001352 const OptionDescriptions& OptDescs, raw_ostream& O)
1353{
1354 checkNumberOfArguments(&d, 1);
1355 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1356 O << "! (";
1357 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1358 O << ")";
1359}
1360
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001361/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001362void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001363 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001364 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001365 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001366
1367 if (TestName == "and")
1368 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1369 else if (TestName == "or")
1370 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001371 else if (TestName == "not")
1372 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001373 else if (EmitCaseTest0Args(TestName, O))
1374 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001375 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1376 return;
1377 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1378 return;
1379 else
1380 throw TestName + ": unknown edge property!";
1381}
1382
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001383
1384/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1385class EmitCaseTestCallback {
1386 bool EmitElseIf_;
1387 const OptionDescriptions& OptDescs_;
1388 raw_ostream& O_;
1389public:
1390
1391 EmitCaseTestCallback(bool EmitElseIf,
1392 const OptionDescriptions& OptDescs, raw_ostream& O)
1393 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1394 {}
1395
1396 void operator()(const DagInit* Test, unsigned IndentLevel, bool FirstTest)
1397 {
1398 if (GetOperatorName(Test) == "default") {
1399 O_.indent(IndentLevel) << "else {\n";
1400 }
1401 else {
1402 O_.indent(IndentLevel)
1403 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
1404 EmitCaseTest(*Test, IndentLevel, OptDescs_, O_);
1405 O_ << ") {\n";
1406 }
1407 }
1408};
1409
1410/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1411template <typename F>
1412class EmitCaseStatementCallback {
1413 F Callback_;
1414 raw_ostream& O_;
1415public:
1416
1417 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1418 : Callback_(Callback), O_(O)
1419 {}
1420
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001421 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001422
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001423 // Ignore nested 'case' DAG.
1424 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001425 GetOperatorName(static_cast<const DagInit*>(Statement)) == "case")) {
1426 if (typeid(*Statement) == typeid(ListInit)) {
1427 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1428 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1429 B != E; ++B)
1430 Callback_(*B, (IndentLevel + Indent1), O_);
1431 }
1432 else {
1433 Callback_(Statement, (IndentLevel + Indent1), O_);
1434 }
1435 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001436 O_.indent(IndentLevel) << "}\n";
1437 }
1438
1439};
1440
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001441/// EmitCaseConstructHandler - Emit code that handles the 'case'
1442/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001443/// clause. Implemented on top of WalkCase.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001444/// Callback's type is void F(Init* Statement, unsigned IndentLevel,
1445/// raw_ostream& O).
1446/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001447/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1448/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001449template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001450void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001451 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001452 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001453 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001454 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1455 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001456}
1457
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001458/// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to
1459/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path"] .
1460/// Helper function used by EmitCmdLineVecFill and.
1461void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
1462 const char* Delimiters = " \t\n\v\f\r";
1463 enum TokenizerState
1464 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1465 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001466
1467 if (CmdLine.empty())
1468 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001469 Out.push_back("");
1470
1471 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1472 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001473
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001474 for (; B != E; ++B) {
1475 char cur_ch = CmdLine[B];
1476
1477 switch (cur_st) {
1478 case Normal:
1479 if (cur_ch == '$') {
1480 cur_st = SpecialCommand;
1481 break;
1482 }
1483 if (oneOf(Delimiters, cur_ch)) {
1484 // Skip whitespace
1485 B = CmdLine.find_first_not_of(Delimiters, B);
1486 if (B == std::string::npos) {
1487 B = E-1;
1488 continue;
1489 }
1490 --B;
1491 Out.push_back("");
1492 continue;
1493 }
1494 break;
1495
1496
1497 case SpecialCommand:
1498 if (oneOf(Delimiters, cur_ch)) {
1499 cur_st = Normal;
1500 Out.push_back("");
1501 continue;
1502 }
1503 if (cur_ch == '(') {
1504 Out.push_back("");
1505 cur_st = InsideSpecialCommand;
1506 continue;
1507 }
1508 break;
1509
1510 case InsideSpecialCommand:
1511 if (oneOf(Delimiters, cur_ch)) {
1512 continue;
1513 }
1514 if (cur_ch == '\'') {
1515 cur_st = InsideQuotationMarks;
1516 Out.push_back("");
1517 continue;
1518 }
1519 if (cur_ch == ')') {
1520 cur_st = Normal;
1521 Out.push_back("");
1522 }
1523 if (cur_ch == ',') {
1524 continue;
1525 }
1526
1527 break;
1528
1529 case InsideQuotationMarks:
1530 if (cur_ch == '\'') {
1531 cur_st = InsideSpecialCommand;
1532 continue;
1533 }
1534 break;
1535 }
1536
1537 Out.back().push_back(cur_ch);
1538 }
1539}
1540
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001541/// SubstituteSpecialCommands - Perform string substitution for $CALL
1542/// and $ENV. Helper function used by EmitCmdLineVecFill().
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001543StrVector::const_iterator SubstituteSpecialCommands
Daniel Dunbar1a551802009-07-03 00:10:29 +00001544(StrVector::const_iterator Pos, StrVector::const_iterator End, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001545{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001546
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001547 const std::string& cmd = *Pos;
1548
1549 if (cmd == "$CALL") {
1550 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1551 const std::string& CmdName = *Pos;
1552
1553 if (CmdName == ")")
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001554 throw std::string("$CALL invocation: empty argument list!");
1555
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001556 O << "hooks::";
1557 O << CmdName << "(";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001558
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001559
1560 bool firstIteration = true;
1561 while (true) {
1562 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1563 const std::string& Arg = *Pos;
1564 assert(Arg.size() != 0);
1565
1566 if (Arg[0] == ')')
1567 break;
1568
1569 if (firstIteration)
1570 firstIteration = false;
1571 else
1572 O << ", ";
1573
1574 O << '"' << Arg << '"';
1575 }
1576
1577 O << ')';
1578
1579 }
1580 else if (cmd == "$ENV") {
1581 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
1582 const std::string& EnvName = *Pos;
1583
1584 if (EnvName == ")")
1585 throw "$ENV invocation: empty argument list!";
1586
1587 O << "checkCString(std::getenv(\"";
1588 O << EnvName;
1589 O << "\"))";
1590
1591 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001592 }
1593 else {
1594 throw "Unknown special command: " + cmd;
1595 }
1596
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001597 const std::string& Leftover = *Pos;
1598 assert(Leftover.at(0) == ')');
1599 if (Leftover.size() != 1)
1600 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001601
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001602 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001603}
1604
1605/// EmitCmdLineVecFill - Emit code that fills in the command line
1606/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001607void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001608 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001609 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001610 StrVector StrVec;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001611 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1612
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001613 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001614 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001615
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001616 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1617
1618 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001619 assert(!StrVec[0].empty());
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001620 if (StrVec[0][0] == '$') {
1621 while (I != E && (*I)[0] != ')' )
1622 ++I;
1623
1624 // Skip the ')' symbol.
1625 ++I;
1626 }
1627 else {
1628 ++I;
1629 }
1630
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001631 bool hasINFILE = false;
1632
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001633 for (; I != E; ++I) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001634 const std::string& cmd = *I;
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001635 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001636 O.indent(IndentLevel);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001637 if (cmd.at(0) == '$') {
1638 if (cmd == "$INFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001639 hasINFILE = true;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001640 if (IsJoin) {
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001641 O << "for (PathVector::const_iterator B = inFiles.begin()"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001642 << ", E = inFiles.end();\n";
1643 O.indent(IndentLevel) << "B != E; ++B)\n";
1644 O.indent(IndentLevel + Indent1) << "vec.push_back(B->str());\n";
1645 }
1646 else {
Chris Lattner74382b72009-08-23 22:45:37 +00001647 O << "vec.push_back(inFile.str());\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001648 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001649 }
1650 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001651 O << "vec.push_back(\"\");\n";
1652 O.indent(IndentLevel) << "out_file_index = vec.size()-1;\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001653 }
1654 else {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001655 O << "vec.push_back(";
1656 I = SubstituteSpecialCommands(I, E, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001657 O << ");\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001658 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001659 }
1660 else {
1661 O << "vec.push_back(\"" << cmd << "\");\n";
1662 }
1663 }
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001664 if (!hasINFILE)
1665 throw "Tool '" + ToolName + "' doesn't take any input!";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001666
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001667 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001668 if (StrVec[0][0] == '$')
1669 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), O);
1670 else
1671 O << '"' << StrVec[0] << '"';
1672 O << ";\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001673}
1674
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001675/// EmitCmdLineVecFillCallback - A function object wrapper around
1676/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1677/// argument to EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001678class EmitCmdLineVecFillCallback {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001679 bool IsJoin;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001680 const std::string& ToolName;
1681 public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001682 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1683 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001684
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001685 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001686 raw_ostream& O) const
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001687 {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001688 EmitCmdLineVecFill(Statement, ToolName, IsJoin, IndentLevel, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001689 }
1690};
1691
1692/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1693/// implement EmitActionHandler. Emits code for
1694/// handling the (forward) and (forward_as) option properties.
1695void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001696 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001697 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001698 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001699 const std::string& Name = NewName.empty()
1700 ? ("-" + D.Name)
1701 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001702 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001703
1704 switch (D.Type) {
1705 case OptionType::Switch:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001706 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001707 break;
1708 case OptionType::Parameter:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001709 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
1710 O.indent(IndentLevel) << "vec.push_back(" << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001711 break;
1712 case OptionType::Prefix:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001713 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\" + "
1714 << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001715 break;
1716 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001717 O.indent(IndentLevel)
1718 << "for (" << D.GenTypeDeclaration()
1719 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1720 O.indent(IndentLevel)
1721 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1722 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\" + " << "*B);\n";
1723 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001724
1725 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001726 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1727 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001728 }
1729
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001730 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001731 break;
1732 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001733 O.indent(IndentLevel)
1734 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1735 << D.GenVariableName() << ".begin(),\n";
1736 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1737 << ".end() ; B != E;) {\n";
1738 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001739
1740 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001741 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1742 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001743 }
1744
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001745 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001746 break;
1747 case OptionType::Alias:
1748 default:
1749 throw std::string("Aliases are not allowed in tool option descriptions!");
1750 }
1751}
1752
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001753/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1754/// EmitPreprocessOptionsCallback.
1755struct ActionHandlingCallbackBase {
1756
1757 void onErrorDag(const DagInit& d,
1758 unsigned IndentLevel, raw_ostream& O) const
1759 {
1760 O.indent(IndentLevel)
1761 << "throw std::runtime_error(\"" <<
1762 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1763 : "Unknown error!")
1764 << "\");\n";
1765 }
1766
1767 void onWarningDag(const DagInit& d,
1768 unsigned IndentLevel, raw_ostream& O) const
1769 {
1770 checkNumberOfArguments(&d, 1);
1771 O.indent(IndentLevel) << "llvm::errs() << \""
1772 << InitPtrToString(d.getArg(0)) << "\";\n";
1773 }
1774
1775};
1776
1777/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1778/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001779class EmitActionHandlersCallback;
1780typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1781(const DagInit&, unsigned, raw_ostream&) const;
1782
1783class EmitActionHandlersCallback
1784: public ActionHandlingCallbackBase,
1785 public HandlerTable<EmitActionHandlersCallbackHandler>
1786{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001787 const OptionDescriptions& OptDescs;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001788 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001789
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001790 void onAppendCmd (const DagInit& Dag,
1791 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001792 {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001793 checkNumberOfArguments(&Dag, 1);
1794 const std::string& Cmd = InitPtrToString(Dag.getArg(0));
1795 StrVector Out;
1796 llvm::SplitString(Cmd, Out);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001797
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001798 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1799 B != E; ++B)
1800 O.indent(IndentLevel) << "vec.push_back(\"" << *B << "\");\n";
1801 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001802
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001803 void onForward (const DagInit& Dag,
1804 unsigned IndentLevel, raw_ostream& O) const
1805 {
1806 checkNumberOfArguments(&Dag, 1);
1807 const std::string& Name = InitPtrToString(Dag.getArg(0));
1808 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1809 IndentLevel, "", O);
1810 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001811
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001812 void onForwardAs (const DagInit& Dag,
1813 unsigned IndentLevel, raw_ostream& O) const
1814 {
1815 checkNumberOfArguments(&Dag, 2);
1816 const std::string& Name = InitPtrToString(Dag.getArg(0));
1817 const std::string& NewName = InitPtrToString(Dag.getArg(1));
1818 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1819 IndentLevel, NewName, O);
1820 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001821
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001822 void onForwardValue (const DagInit& Dag,
1823 unsigned IndentLevel, raw_ostream& O) const
1824 {
1825 checkNumberOfArguments(&Dag, 1);
1826 const std::string& Name = InitPtrToString(Dag.getArg(0));
1827 const OptionDescription& D = OptDescs.FindOption(Name);
1828
1829 if (D.isParameter()) {
1830 O.indent(IndentLevel) << "vec.push_back("
1831 << D.GenVariableName() << ");\n";
1832 }
1833 else if (D.isList()) {
1834 O.indent(IndentLevel) << "std::copy(" << D.GenVariableName()
1835 << ".begin(), " << D.GenVariableName()
1836 << ".end(), std::back_inserter(vec));\n";
1837 }
1838 else {
1839 throw "'forward_value' used with a switch or an alias!";
1840 }
1841 }
1842
1843 void onForwardTransformedValue (const DagInit& Dag,
1844 unsigned IndentLevel, raw_ostream& O) const
1845 {
1846 checkNumberOfArguments(&Dag, 2);
1847 const std::string& Name = InitPtrToString(Dag.getArg(0));
1848 const std::string& Hook = InitPtrToString(Dag.getArg(1));
1849 const OptionDescription& D = OptDescs.FindOption(Name);
1850
1851 if (D.isParameter() || D.isList()) {
1852 O.indent(IndentLevel) << "vec.push_back(" << "hooks::"
1853 << Hook << "(" << D.GenVariableName() << "));\n";
1854 }
1855 else {
1856 throw "'forward_transformed_value' used with a switch or an alias!";
1857 }
1858 }
1859
1860
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001861 void onOutputSuffix (const DagInit& Dag,
1862 unsigned IndentLevel, raw_ostream& O) const
1863 {
1864 checkNumberOfArguments(&Dag, 1);
1865 const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
1866 O.indent(IndentLevel) << "output_suffix = \"" << OutSuf << "\";\n";
1867 }
1868
1869 void onStopCompilation (const DagInit& Dag,
1870 unsigned IndentLevel, raw_ostream& O) const
1871 {
1872 O.indent(IndentLevel) << "stop_compilation = true;\n";
1873 }
1874
1875
1876 void onUnpackValues (const DagInit& Dag,
1877 unsigned IndentLevel, raw_ostream& O) const
1878 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001879 throw "'unpack_values' is deprecated. "
1880 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001881 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001882
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001883 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001884
1885 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
1886 : OptDescs(OD)
1887 {
1888 if (!staticMembersInitialized_) {
1889 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
1890 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
1891 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
1892 AddHandler("forward", &EmitActionHandlersCallback::onForward);
1893 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001894 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
1895 AddHandler("forward_transformed_value",
1896 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001897 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
1898 AddHandler("stop_compilation",
1899 &EmitActionHandlersCallback::onStopCompilation);
1900 AddHandler("unpack_values",
1901 &EmitActionHandlersCallback::onUnpackValues);
1902
1903 staticMembersInitialized_ = true;
1904 }
1905 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001906
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001907 void operator()(const Init* Statement,
1908 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001909 {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001910 const DagInit& Dag = InitPtrToDag(Statement);
1911 const std::string& ActionName = GetOperatorName(Dag);
1912 Handler h = GetHandler(ActionName);
1913
1914 ((this)->*(h))(Dag, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001915 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001916};
1917
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001918bool IsOutFileIndexCheckRequiredStr (const Init* CmdLine) {
1919 StrVector StrVec;
1920 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1921
1922 for (StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1923 I != E; ++I) {
1924 if (*I == "$OUTFILE")
1925 return false;
1926 }
1927
1928 return true;
1929}
1930
1931class IsOutFileIndexCheckRequiredStrCallback {
1932 bool* ret_;
1933
1934public:
1935 IsOutFileIndexCheckRequiredStrCallback(bool* ret) : ret_(ret)
1936 {}
1937
1938 void operator()(const Init* CmdLine) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001939 // Ignore nested 'case' DAG.
1940 if (typeid(*CmdLine) == typeid(DagInit))
1941 return;
1942
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001943 if (IsOutFileIndexCheckRequiredStr(CmdLine))
1944 *ret_ = true;
1945 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001946
1947 void operator()(const DagInit* Test, unsigned, bool) {
1948 this->operator()(Test);
1949 }
1950 void operator()(const Init* Statement, unsigned) {
1951 this->operator()(Statement);
1952 }
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001953};
1954
1955bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
1956 bool ret = false;
1957 WalkCase(CmdLine, Id(), IsOutFileIndexCheckRequiredStrCallback(&ret));
1958 return ret;
1959}
1960
1961/// IsOutFileIndexCheckRequired - Should we emit an "out_file_index != -1" check
1962/// in EmitGenerateActionMethod() ?
1963bool IsOutFileIndexCheckRequired (Init* CmdLine) {
1964 if (typeid(*CmdLine) == typeid(StringInit))
1965 return IsOutFileIndexCheckRequiredStr(CmdLine);
1966 else
1967 return IsOutFileIndexCheckRequiredCase(CmdLine);
1968}
1969
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001970void EmitGenerateActionMethodHeader(const ToolDescription& D,
1971 bool IsJoin, raw_ostream& O)
1972{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001973 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001974 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001975 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001976 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001977
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001978 O.indent(Indent2) << "bool HasChildren,\n";
1979 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1980 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1981 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1982 O.indent(Indent1) << "{\n";
1983 O.indent(Indent2) << "std::string cmd;\n";
1984 O.indent(Indent2) << "std::vector<std::string> vec;\n";
1985 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
1986 O.indent(Indent2) << "const char* output_suffix = \""
1987 << D.OutputSuffix << "\";\n";
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001988}
1989
1990// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
1991// Tool::GenerateAction() method.
1992void EmitGenerateActionMethod (const ToolDescription& D,
1993 const OptionDescriptions& OptDescs,
1994 bool IsJoin, raw_ostream& O) {
1995
1996 EmitGenerateActionMethodHeader(D, IsJoin, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001997
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001998 if (!D.CmdLine)
1999 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002000
2001 bool IndexCheckRequired = IsOutFileIndexCheckRequired(D.CmdLine);
2002 O.indent(Indent2) << "int out_file_index"
2003 << (IndexCheckRequired ? " = -1" : "")
2004 << ";\n\n";
2005
2006 // Process the cmd_line property.
2007 if (typeid(*D.CmdLine) == typeid(StringInit))
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002008 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2009 else
2010 EmitCaseConstructHandler(D.CmdLine, Indent2,
2011 EmitCmdLineVecFillCallback(IsJoin, D.Name),
2012 true, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002013
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002014 // For every understood option, emit handling code.
2015 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002016 EmitCaseConstructHandler(D.Actions, Indent2,
2017 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002018 false, OptDescs, O);
2019
2020 O << '\n';
2021 O.indent(Indent2)
2022 << "std::string out_file = OutFilename("
2023 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
2024 O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002025
2026 if (IndexCheckRequired)
2027 O.indent(Indent2) << "if (out_file_index != -1)\n";
2028 O.indent(IndexCheckRequired ? Indent3 : Indent2)
2029 << "vec[out_file_index] = out_file;\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002030
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002031 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002032 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002033 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
2034 O.indent(Indent3) << "vec.insert(vec.end(), "
2035 << SinkOptionName << ".begin(), " << SinkOptionName
2036 << ".end());\n";
2037 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002038 }
2039
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002040 O.indent(Indent2) << "return Action(cmd, vec, stop_compilation, out_file);\n";
2041 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002042}
2043
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002044/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2045/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002046void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2047 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002048 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002049 if (!ToolDesc.isJoin()) {
2050 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
2051 O.indent(Indent2) << "bool HasChildren,\n";
2052 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2053 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2054 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2055 O.indent(Indent1) << "{\n";
2056 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
2057 << " is not a Join tool!\");\n";
2058 O.indent(Indent1) << "}\n\n";
2059 }
2060 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002061 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002062 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002063
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002064 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002065}
2066
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002067/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2068/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002069void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002070 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2071 O.indent(Indent2) << "return InputLanguages_;\n";
2072 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002073
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002074 if (D.OutLanguage.empty())
2075 throw "Tool " + D.Name + " has no 'out_language' property!";
2076
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002077 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2078 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2079 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002080}
2081
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002082/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002083void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002084 O.indent(Indent1) << "const char* Name() const {\n";
2085 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2086 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002087}
2088
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002089/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2090/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002091void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002092 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002093 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002094 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002095 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002096 O.indent(Indent2) << "return false;\n";
2097 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002098}
2099
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002100/// EmitStaticMemberDefinitions - Emit static member definitions for a
2101/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002102void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002103 if (D.InLanguage.empty())
2104 throw "Tool " + D.Name + " has no 'in_language' property!";
2105
2106 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2107 for (StrVector::const_iterator B = D.InLanguage.begin(),
2108 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002109 O << '\"' << *B << "\", ";
2110 O << "0};\n\n";
2111}
2112
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002113/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002114void EmitToolClassDefinition (const ToolDescription& D,
2115 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002116 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002117 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002118 return;
2119
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002120 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002121 O << "class " << D.Name << " : public ";
2122 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002123 O << "JoinTool";
2124 else
2125 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002126
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002127 O << "{\nprivate:\n";
2128 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002129
2130 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002131 EmitNameMethod(D, O);
2132 EmitInOutLanguageMethods(D, O);
2133 EmitIsJoinMethod(D, O);
2134 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002135
2136 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002137 O << "};\n";
2138
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002139 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002140
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002141}
2142
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002143/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002144/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002145void EmitOptionDefinitions (const OptionDescriptions& descs,
2146 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002147 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002148{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002149 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002150
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002151 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002152 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002153 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002154 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002155
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002156 if (val.Type == OptionType::Alias) {
2157 Aliases.push_back(val);
2158 continue;
2159 }
2160
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002161 if (val.isExtern())
2162 O << "extern ";
2163
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002164 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002165 << val.GenVariableName();
2166
2167 if (val.isExtern()) {
2168 O << ";\n";
2169 continue;
2170 }
2171
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002172 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002173
2174 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2175 O << ", cl::Prefix";
2176
2177 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002178 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002179 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002180 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002181 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002182 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002183 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002184 O << ", cl::OneOrMore";
2185 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002186 else if (val.isZeroOrOne() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002187 O << ", cl::ZeroOrOne";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002188 }
2189
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002190 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002191 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002192 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002193 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002194
2195 if (val.isCommaSeparated())
2196 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002197
2198 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002199 O << ", cl::multi_val(" << val.MultiVal << ')';
2200
2201 if (val.InitVal) {
2202 const std::string& str = val.InitVal->getAsString();
2203 O << ", cl::init(" << str << ')';
2204 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002205
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002206 if (!val.Help.empty())
2207 O << ", cl::desc(\"" << val.Help << "\")";
2208
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002209 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002210 }
2211
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002212 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002213 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002214 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002215 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002216
2217 O << val.GenTypeDeclaration() << ' '
2218 << val.GenVariableName()
2219 << "(\"" << val.Name << '\"';
2220
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002221 const OptionDescription& D = descs.FindOption(val.Help);
2222 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002223
2224 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2225 }
2226
2227 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002228 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002229 O << (HasExterns ? "extern cl" : "cl")
2230 << "::list<std::string> " << SinkOptionName
2231 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002232
2233 O << '\n';
2234}
2235
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002236/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002237/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002238class EmitPreprocessOptionsCallback : ActionHandlingCallbackBase {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002239 const OptionDescriptions& OptDescs_;
2240
2241 void onUnsetOption(Init* i, unsigned IndentLevel, raw_ostream& O) {
2242 const std::string& OptName = InitPtrToString(i);
2243 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002244
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002245 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002246 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2247 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002248 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002249 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2250 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002251 else if (OptDesc.isList()) {
2252 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2253 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002254 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002255 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002256 }
2257 }
2258
2259 void processDag(const Init* I, unsigned IndentLevel, raw_ostream& O)
2260 {
2261 const DagInit& d = InitPtrToDag(I);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002262 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002263
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002264 if (OpName == "warning") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002265 this->onWarningDag(d, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002266 }
2267 else if (OpName == "error") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002268 this->onWarningDag(d, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002269 }
2270 else if (OpName == "unset_option") {
2271 checkNumberOfArguments(&d, 1);
2272 Init* I = d.getArg(0);
2273 if (typeid(*I) == typeid(ListInit)) {
2274 const ListInit& DagList = *static_cast<const ListInit*>(I);
2275 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
2276 B != E; ++B)
2277 this->onUnsetOption(*B, IndentLevel, O);
2278 }
2279 else {
2280 this->onUnsetOption(I, IndentLevel, O);
2281 }
2282 }
2283 else {
2284 throw "Unknown operator in the option preprocessor: '" + OpName + "'!"
2285 "\nOnly 'warning', 'error' and 'unset_option' are allowed.";
2286 }
2287 }
2288
2289public:
2290
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002291 void operator()(const Init* I, unsigned IndentLevel, raw_ostream& O) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002292 this->processDag(I, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002293 }
2294
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002295 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002296 : OptDescs_(OptDescs)
2297 {}
2298};
2299
2300/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2301void EmitPreprocessOptions (const RecordKeeper& Records,
2302 const OptionDescriptions& OptDecs, raw_ostream& O)
2303{
2304 O << "void PreprocessOptionsLocal() {\n";
2305
2306 const RecordVector& OptionPreprocessors =
2307 Records.getAllDerivedDefinitions("OptionPreprocessor");
2308
2309 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2310 E = OptionPreprocessors.end(); B!=E; ++B) {
2311 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002312 EmitCaseConstructHandler(Case, Indent1,
2313 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002314 false, OptDecs, O);
2315 }
2316
2317 O << "}\n\n";
2318}
2319
2320/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002321void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002322{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002323 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002324
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002325 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002326 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002327
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002328 // It is allowed for a plugin to have no language map.
2329 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002330
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002331 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2332 if (!LangsToSuffixesList)
2333 throw std::string("Error in the language map definition!");
2334
2335 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002336 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002337
2338 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2339 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2340
2341 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002342 O.indent(Indent1) << "langMap[\""
2343 << InitPtrToString(Suffixes->getElement(i))
2344 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002345 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002346 }
2347
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002348 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002349}
2350
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002351/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2352/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002353void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002354 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002355 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002356 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002357
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002358 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002359 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002360 }
2361 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002362 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002363 }
2364 else if (OpName == "error") {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002365 checkNumberOfArguments(&d, 1);
2366 O.indent(IndentLevel) << "throw std::runtime_error(\""
2367 << InitPtrToString(d.getArg(0))
2368 << "\");\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002369 return;
2370 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002371 else {
2372 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002373 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002374 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002375
2376 if (d.getNumArgs() > 0)
2377 O << InitPtrToInt(d.getArg(0)) << ";\n";
2378 else
2379 O << "2;\n";
2380
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002381}
2382
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002383/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002384void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002385 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002386 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002387
2388 // Class constructor.
2389 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002390 << "public:\n";
2391 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2392 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002393
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002394 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002395 O.indent(Indent1)
2396 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2397 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002398
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002399 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002400 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002401
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002402 O.indent(Indent2) << "return ret;\n";
2403 O.indent(Indent1) << "};\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002404}
2405
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002406/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002407void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002408 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002409 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002410 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002411 for (RecordVector::const_iterator B = EdgeVector.begin(),
2412 E = EdgeVector.end(); B != E; ++B) {
2413 const Record* Edge = *B;
2414 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002415 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002416
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002417 if (!isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002418 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002419 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002420 }
2421}
2422
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002423/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002424/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002425void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002426 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002427 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002428{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002429 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002430
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002431 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2432 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002433 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002434
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002435 O << '\n';
2436
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002437 // Insert edges.
2438
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002439 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002440 for (RecordVector::const_iterator B = EdgeVector.begin(),
2441 E = EdgeVector.end(); B != E; ++B) {
2442 const Record* Edge = *B;
2443 const std::string& NodeA = Edge->getValueAsString("a");
2444 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002445 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002446
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002447 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002448
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002449 if (isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002450 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002451 else
2452 O << "new Edge" << i << "()";
2453
2454 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002455 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002456 }
2457
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002458 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002459}
2460
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002461/// HookInfo - Information about the hook type and number of arguments.
2462struct HookInfo {
2463
2464 // A hook can either have a single parameter of type std::vector<std::string>,
2465 // or NumArgs parameters of type const char*.
2466 enum HookType { ListHook, ArgHook };
2467
2468 HookType Type;
2469 unsigned NumArgs;
2470
2471 HookInfo() : Type(ArgHook), NumArgs(1)
2472 {}
2473
2474 HookInfo(HookType T) : Type(T), NumArgs(1)
2475 {}
2476
2477 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2478 {}
2479};
2480
2481typedef llvm::StringMap<HookInfo> HookInfoMap;
2482
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002483/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002484/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002485/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002486class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002487 HookInfoMap& HookNames_;
2488 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002489public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002490 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2491 : HookNames_(HookNames), OptDescs_(OptDescs)
2492 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002493
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002494 void onAction (const DagInit& Dag) {
2495 if (GetOperatorName(Dag) == "forward_transformed_value") {
2496 checkNumberOfArguments(Dag, 2);
2497 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2498 const std::string& HookName = InitPtrToString(Dag.getArg(1));
2499 const OptionDescription& D = OptDescs_.FindOption(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002500
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002501 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2502 : HookInfo::ArgHook);
2503 }
2504 }
2505
2506 void operator()(const Init* Arg) {
2507
2508 // We're invoked on an action (either a dag or a dag list).
2509 if (typeid(*Arg) == typeid(DagInit)) {
2510 const DagInit& Dag = InitPtrToDag(Arg);
2511 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002512 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002513 }
2514 else if (typeid(*Arg) == typeid(ListInit)) {
2515 const ListInit& List = InitPtrToList(Arg);
2516 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2517 ++B) {
2518 const DagInit& Dag = InitPtrToDag(*B);
2519 this->onAction(Dag);
2520 }
2521 return;
2522 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002523
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002524 // We're invoked on a command line.
2525 StrVector cmds;
2526 TokenizeCmdline(InitPtrToString(Arg), cmds);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002527 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2528 B != E; ++B) {
2529 const std::string& cmd = *B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002530
2531 if (cmd == "$CALL") {
2532 unsigned NumArgs = 0;
2533 checkedIncrement(B, E, "Syntax error in $CALL invocation!");
2534 const std::string& HookName = *B;
2535
2536
2537 if (HookName.at(0) == ')')
2538 throw "$CALL invoked with no arguments!";
2539
2540 while (++B != E && B->at(0) != ')') {
2541 ++NumArgs;
2542 }
2543
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002544 HookInfoMap::const_iterator H = HookNames_.find(HookName);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002545
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002546 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2547 H->second.Type != HookInfo::ArgHook)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002548 throw "Overloading of hooks is not allowed. Overloaded hook: "
2549 + HookName;
2550 else
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002551 HookNames_[HookName] = HookInfo(NumArgs);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002552
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002553 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002554 }
2555 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002556
2557 void operator()(const DagInit* Test, unsigned, bool) {
2558 this->operator()(Test);
2559 }
2560 void operator()(const Init* Statement, unsigned) {
2561 this->operator()(Statement);
2562 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002563};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002564
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002565/// FillInHookNames - Actually extract the hook names from all command
2566/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002567void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002568 const OptionDescriptions& OptDescs,
2569 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002570{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002571 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002572 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2573 E = ToolDescs.end(); B != E; ++B) {
2574 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002575
2576 // Look for 'forward_transformed_value' in 'actions'.
2577 if (D.Actions)
2578 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2579
2580 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002581 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002582 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002583 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002584 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002585 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002586 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002587 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002588 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002589 }
2590}
2591
2592/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2593/// property records and emit hook function declaration for each
2594/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002595void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2596 const OptionDescriptions& OptDescs, raw_ostream& O) {
2597 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002598
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002599 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002600 if (HookNames.empty())
2601 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002602
2603 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002604 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002605 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002606 const char* HookName = B->first();
2607 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002608
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002609 O.indent(Indent1) << "std::string " << HookName << "(";
2610
2611 if (Info.Type == HookInfo::ArgHook) {
2612 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2613 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2614 }
2615 }
2616 else {
2617 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002618 }
2619
2620 O <<");\n";
2621 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002622 O << "}\n\n";
2623}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002624
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002625/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002626void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002627 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2628 O.indent(Indent1) << "int Priority() const { return "
2629 << Priority << "; }\n\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002630 O.indent(Indent1) << "void PreprocessOptions() const\n";
2631 O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002632 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2633 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2634 O.indent(Indent1)
2635 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2636 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2637 << "};\n\n"
2638 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002639}
2640
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002641/// EmitIncludes - Emit necessary #include directives and some
2642/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002643void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002644 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2645 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002646 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002647 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2648 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002649
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002650 << "#include \"llvm/Support/CommandLine.h\"\n"
2651 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002652
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002653 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002654 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002655 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002656 << "#include <stdexcept>\n\n"
2657
2658 << "using namespace llvm;\n"
2659 << "using namespace llvmc;\n\n"
2660
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002661 << "extern cl::opt<std::string> OutputFilename;\n\n"
2662
2663 << "inline const char* checkCString(const char* s)\n"
2664 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002665}
2666
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002667
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002668/// PluginData - Holds all information about a plugin.
2669struct PluginData {
2670 OptionDescriptions OptDescs;
2671 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002672 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002673 ToolDescriptions ToolDescs;
2674 RecordVector Edges;
2675 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002676};
2677
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002678/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002679/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002680bool HasSink(const ToolDescriptions& ToolDescs) {
2681 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2682 E = ToolDescs.end(); B != E; ++B)
2683 if ((*B)->isSink())
2684 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002685
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002686 return false;
2687}
2688
2689/// HasExterns - Go through the list of option descriptions and check
2690/// if there are any external options.
2691bool HasExterns(const OptionDescriptions& OptDescs) {
2692 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2693 E = OptDescs.end(); B != E; ++B)
2694 if (B->second.isExtern())
2695 return true;
2696
2697 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002698}
2699
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002700/// CollectPluginData - Collect tool and option properties,
2701/// compilation graph edges and plugin priority from the parse tree.
2702void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2703 // Collect option properties.
2704 const RecordVector& OptionLists =
2705 Records.getAllDerivedDefinitions("OptionList");
2706 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2707 Data.OptDescs);
2708
2709 // Collect tool properties.
2710 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2711 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2712 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002713 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002714
2715 // Collect compilation graph edges.
2716 const RecordVector& CompilationGraphs =
2717 Records.getAllDerivedDefinitions("CompilationGraph");
2718 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2719 Data.Edges);
2720
2721 // Calculate the priority of this plugin.
2722 const RecordVector& Priorities =
2723 Records.getAllDerivedDefinitions("PluginPriority");
2724 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002725}
2726
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002727/// CheckPluginData - Perform some sanity checks on the collected data.
2728void CheckPluginData(PluginData& Data) {
2729 // Filter out all tools not mentioned in the compilation graph.
2730 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002731
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002732 // Typecheck the compilation graph.
2733 TypecheckGraph(Data.Edges, Data.ToolDescs);
2734
2735 // Check that there are no options without side effects (specified
2736 // only in the OptionList).
2737 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2738
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002739}
2740
Daniel Dunbar1a551802009-07-03 00:10:29 +00002741void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002742 // Emit file header.
2743 EmitIncludes(O);
2744
2745 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002746 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002747
2748 // Emit hook declarations.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002749 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002750
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002751 O << "namespace {\n\n";
2752
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002753 // Emit PreprocessOptionsLocal() function.
2754 EmitPreprocessOptions(Records, Data.OptDescs, O);
2755
2756 // Emit PopulateLanguageMapLocal() function
2757 // (language map maps from file extensions to language names).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002758 EmitPopulateLanguageMap(Records, O);
2759
2760 // Emit Tool classes.
2761 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2762 E = Data.ToolDescs.end(); B!=E; ++B)
2763 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2764
2765 // Emit Edge# classes.
2766 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2767
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002768 // Emit PopulateCompilationGraphLocal() function.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002769 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2770
2771 // Emit code for plugin registration.
2772 EmitRegisterPlugin(Data.Priority, O);
2773
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002774 O << "} // End anonymous namespace.\n\n";
2775
2776 // Force linkage magic.
2777 O << "namespace llvmc {\n";
2778 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2779 O << "}\n";
2780
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002781 // EOF
2782}
2783
2784
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002785// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00002786}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002787
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002788/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002789void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002790 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002791 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002792
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002793 CollectPluginData(Records, Data);
2794 CheckPluginData(Data);
2795
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +00002796 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002797 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002798
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002799 } catch (std::exception& Error) {
2800 throw Error.what() + std::string(" - usually this means a syntax error.");
2801 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002802}