blob: a5867b4a7fa8bb35b6eff7538b2b557ce9191f4a [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,
214 OneOrMore = 0x10, ZeroOrOne = 0x20 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000215}
216
217/// OptionDescription - Represents data contained in a single
218/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000219struct OptionDescription {
220 OptionType::OptionType Type;
221 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000222 unsigned Flags;
223 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000224 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000225 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000226
227 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000228 const std::string& n = "",
229 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000230 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000231 {}
232
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000233 /// GenTypeDeclaration - Returns the C++ variable type of this
234 /// option.
235 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000236
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000237 /// GenVariableName - Returns the variable name used in the
238 /// generated C++ code.
239 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000240
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000241 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000242 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000243
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000244 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000245
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000246 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000247
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000248 bool isMultiVal() const;
249
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000250 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000251 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000252
253 bool isRequired() const;
254 void setRequired();
255
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000256 bool isOneOrMore() const;
257 void setOneOrMore();
258
259 bool isZeroOrOne() const;
260 void setZeroOrOne();
261
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000262 bool isHidden() const;
263 void setHidden();
264
265 bool isReallyHidden() const;
266 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000267
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000268 bool isSwitch() const
269 { return OptionType::IsSwitch(this->Type); }
270
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000271 bool isParameter() const
272 { return OptionType::IsParameter(this->Type); }
273
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000274 bool isList() const
275 { return OptionType::IsList(this->Type); }
276
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000277};
278
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000279void OptionDescription::Merge (const OptionDescription& other)
280{
281 if (other.Type != Type)
282 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000283
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000284 if (Help == other.Help || Help == DefaultHelpString)
285 Help = other.Help;
286 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000287 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000288 " defined for option " + Name + "\n";
289 }
290
291 Flags |= other.Flags;
292}
293
294bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000295 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000296}
297
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000298bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000299 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000300}
301
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000302bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000303 return Flags & OptionDescriptionFlags::Extern;
304}
305void OptionDescription::setExtern() {
306 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000307}
308
309bool OptionDescription::isRequired() const {
310 return Flags & OptionDescriptionFlags::Required;
311}
312void OptionDescription::setRequired() {
313 Flags |= OptionDescriptionFlags::Required;
314}
315
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000316bool OptionDescription::isOneOrMore() const {
317 return Flags & OptionDescriptionFlags::OneOrMore;
318}
319void OptionDescription::setOneOrMore() {
320 Flags |= OptionDescriptionFlags::OneOrMore;
321}
322
323bool OptionDescription::isZeroOrOne() const {
324 return Flags & OptionDescriptionFlags::ZeroOrOne;
325}
326void OptionDescription::setZeroOrOne() {
327 Flags |= OptionDescriptionFlags::ZeroOrOne;
328}
329
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000330bool OptionDescription::isHidden() const {
331 return Flags & OptionDescriptionFlags::Hidden;
332}
333void OptionDescription::setHidden() {
334 Flags |= OptionDescriptionFlags::Hidden;
335}
336
337bool OptionDescription::isReallyHidden() const {
338 return Flags & OptionDescriptionFlags::ReallyHidden;
339}
340void OptionDescription::setReallyHidden() {
341 Flags |= OptionDescriptionFlags::ReallyHidden;
342}
343
344const char* OptionDescription::GenTypeDeclaration() const {
345 switch (Type) {
346 case OptionType::Alias:
347 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000348 case OptionType::PrefixList:
349 case OptionType::ParameterList:
350 return "cl::list<std::string>";
351 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000352 return "cl::opt<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000353 case OptionType::Parameter:
354 case OptionType::Prefix:
355 default:
356 return "cl::opt<std::string>";
357 }
358}
359
360std::string OptionDescription::GenVariableName() const {
361 const std::string& EscapedName = EscapeVariableName(Name);
362 switch (Type) {
363 case OptionType::Alias:
364 return "AutoGeneratedAlias_" + EscapedName;
365 case OptionType::PrefixList:
366 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000367 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000368 case OptionType::Switch:
369 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000370 case OptionType::Prefix:
371 case OptionType::Parameter:
372 default:
373 return "AutoGeneratedParameter_" + EscapedName;
374 }
375}
376
377/// OptionDescriptions - An OptionDescription array plus some helper
378/// functions.
379class OptionDescriptions {
380 typedef StringMap<OptionDescription> container_type;
381
382 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000383 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000384
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000385public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000386 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000387 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000388
389 // Wrappers for FindOption that throw an exception in case the option has a
390 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000391 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000392 const OptionDescription& FindParameter(const std::string& OptName) const;
393 const OptionDescription& FindList(const std::string& OptName) const;
394 const OptionDescription&
395 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000396
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000397 /// insertDescription - Insert new OptionDescription into
398 /// OptionDescriptions list
399 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000400
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000401 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000402 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000403 const_iterator begin() const { return Descriptions.begin(); }
404 const_iterator end() const { return Descriptions.end(); }
405};
406
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000407const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000408OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000409 const_iterator I = Descriptions.find(OptName);
410 if (I != Descriptions.end())
411 return I->second;
412 else
413 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000414}
415
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000416const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000417OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000418 const OptionDescription& OptDesc = this->FindOption(OptName);
419 if (!OptDesc.isSwitch())
420 throw OptName + ": incorrect option type - should be a switch!";
421 return OptDesc;
422}
423
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000424const OptionDescription&
425OptionDescriptions::FindList(const std::string& OptName) const {
426 const OptionDescription& OptDesc = this->FindOption(OptName);
427 if (!OptDesc.isList())
428 throw OptName + ": incorrect option type - should be a list!";
429 return OptDesc;
430}
431
432const OptionDescription&
433OptionDescriptions::FindParameter(const std::string& OptName) const {
434 const OptionDescription& OptDesc = this->FindOption(OptName);
435 if (!OptDesc.isParameter())
436 throw OptName + ": incorrect option type - should be a parameter!";
437 return OptDesc;
438}
439
440const OptionDescription&
441OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
442 const OptionDescription& OptDesc = this->FindOption(OptName);
443 if (!OptDesc.isList() && !OptDesc.isParameter())
444 throw OptName
445 + ": incorrect option type - should be a list or parameter!";
446 return OptDesc;
447}
448
449void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000450 container_type::iterator I = Descriptions.find(o.Name);
451 if (I != Descriptions.end()) {
452 OptionDescription& D = I->second;
453 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000454 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000455 else {
456 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000457 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000458}
459
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000460/// HandlerTable - A base class for function objects implemented as
461/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000462template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000463class HandlerTable {
464protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000465 // Implementation details.
466
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000467 /// HandlerMap - A map from property names to property handlers
468 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000469
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000470 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000471 static bool staticMembersInitialized_;
472
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000473public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000474
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000475 Handler GetHandler (const std::string& HandlerName) const {
476 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000477
478 if (method != Handlers_.end()) {
479 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000480 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000481 }
482 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000483 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000484 }
485 }
486
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000487 void AddHandler(const char* Property, Handler H) {
488 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000489 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000490
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000491};
492
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000493template <class FunctionObject>
494void InvokeDagInitHandler(FunctionObject* Obj, Init* i) {
495 typedef void (FunctionObject::*Handler) (const DagInit*);
496
497 const DagInit& property = InitPtrToDag(i);
498 const std::string& property_name = GetOperatorName(property);
499 Handler h = Obj->GetHandler(property_name);
500
501 ((Obj)->*(h))(&property);
502}
503
504template <typename H>
505typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
506
507template <typename H>
508bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000509
510
511/// CollectOptionProperties - Function object for iterating over an
512/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000513class CollectOptionProperties;
514typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
515(const DagInit*);
516
517class CollectOptionProperties
518: public HandlerTable<CollectOptionPropertiesHandler>
519{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000520private:
521
522 /// optDescs_ - OptionDescriptions table. This is where the
523 /// information is stored.
524 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000525
526public:
527
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000528 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000529 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000530 {
531 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000532 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000533 AddHandler("help", &CollectOptionProperties::onHelp);
534 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000535 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000536 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
537 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000538 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
539 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000540 AddHandler("zero_or_one", &CollectOptionProperties::onZeroOrOne);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000541
542 staticMembersInitialized_ = true;
543 }
544 }
545
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000546 /// operator() - Just forwards to the corresponding property
547 /// handler.
548 void operator() (Init* i) {
549 InvokeDagInitHandler(this, i);
550 }
551
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000552private:
553
554 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000555 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000556
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000557 void onExtern (const DagInit* d) {
558 checkNumberOfArguments(d, 0);
559 optDesc_.setExtern();
560 }
561
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000562 void onHelp (const DagInit* d) {
563 checkNumberOfArguments(d, 1);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000564 optDesc_.Help = InitPtrToString(d->getArg(0));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000565 }
566
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000567 void onHidden (const DagInit* d) {
568 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000569 optDesc_.setHidden();
570 }
571
572 void onReallyHidden (const DagInit* d) {
573 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000574 optDesc_.setReallyHidden();
575 }
576
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000577 void onRequired (const DagInit* d) {
578 checkNumberOfArguments(d, 0);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000579 if (optDesc_.isOneOrMore())
580 throw std::string("An option can't have both (required) "
581 "and (one_or_more) properties!");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000582 optDesc_.setRequired();
583 }
584
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000585 void onInit (const DagInit* d) {
586 checkNumberOfArguments(d, 1);
587 Init* i = d->getArg(0);
588 const std::string& str = i->getAsString();
589
590 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
591 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
592
593 if (!correct)
594 throw std::string("Incorrect usage of the 'init' option property!");
595
596 optDesc_.InitVal = i;
597 }
598
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000599 void onOneOrMore (const DagInit* d) {
600 checkNumberOfArguments(d, 0);
601 if (optDesc_.isRequired() || optDesc_.isZeroOrOne())
602 throw std::string("Only one of (required), (zero_or_one) or "
603 "(one_or_more) properties is allowed!");
604 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000605 llvm::errs() << "Warning: specifying the 'one_or_more' property "
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000606 "on a non-list option will have no effect.\n";
607 optDesc_.setOneOrMore();
608 }
609
610 void onZeroOrOne (const DagInit* d) {
611 checkNumberOfArguments(d, 0);
612 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
613 throw std::string("Only one of (required), (zero_or_one) or "
614 "(one_or_more) properties is allowed!");
615 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000616 llvm::errs() << "Warning: specifying the 'zero_or_one' property"
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000617 "on a non-list option will have no effect.\n";
618 optDesc_.setZeroOrOne();
619 }
620
621 void onMultiVal (const DagInit* d) {
622 checkNumberOfArguments(d, 1);
623 int val = InitPtrToInt(d->getArg(0));
624 if (val < 2)
625 throw std::string("Error in the 'multi_val' property: "
626 "the value must be greater than 1!");
627 if (!OptionType::IsList(optDesc_.Type))
628 throw std::string("The multi_val property is valid only "
629 "on list options!");
630 optDesc_.MultiVal = val;
631 }
632
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000633};
634
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000635/// AddOption - A function object that is applied to every option
636/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000637class AddOption {
638private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000639 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000640
641public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000642 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000643 {}
644
645 void operator()(const Init* i) {
646 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000647 checkNumberOfArguments(&d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000648
649 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000650 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000651 const std::string& Name = InitPtrToString(d.getArg(0));
652
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000653 OptionDescription OD(Type, Name);
654
655 if (!OD.isExtern())
656 checkNumberOfArguments(&d, 2);
657
658 if (OD.isAlias()) {
659 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000660 OD.Help = InitPtrToString(d.getArg(1));
661 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000662 else if (!OD.isExtern()) {
663 processOptionProperties(&d, OD);
664 }
665 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000666 }
667
668private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000669 /// processOptionProperties - Go through the list of option
670 /// properties and call a corresponding handler for each.
671 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
672 checkNumberOfArguments(d, 2);
673 DagInit::const_arg_iterator B = d->arg_begin();
674 // Skip the first argument: it's always the option name.
675 ++B;
676 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000677 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000678
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000679};
680
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000681/// CollectOptionDescriptions - Collects option properties from all
682/// OptionLists.
683void CollectOptionDescriptions (RecordVector::const_iterator B,
684 RecordVector::const_iterator E,
685 OptionDescriptions& OptDescs)
686{
687 // For every OptionList:
688 for (; B!=E; ++B) {
689 RecordVector::value_type T = *B;
690 // Throws an exception if the value does not exist.
691 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000692
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000693 // For every option description in this list:
694 // collect the information and
695 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
696 }
697}
698
699// Tool information record
700
701namespace ToolFlags {
702 enum ToolFlags { Join = 0x1, Sink = 0x2 };
703}
704
705struct ToolDescription : public RefCountedBase<ToolDescription> {
706 std::string Name;
707 Init* CmdLine;
708 Init* Actions;
709 StrVector InLanguage;
710 std::string OutLanguage;
711 std::string OutputSuffix;
712 unsigned Flags;
713
714 // Various boolean properties
715 void setSink() { Flags |= ToolFlags::Sink; }
716 bool isSink() const { return Flags & ToolFlags::Sink; }
717 void setJoin() { Flags |= ToolFlags::Join; }
718 bool isJoin() const { return Flags & ToolFlags::Join; }
719
720 // Default ctor here is needed because StringMap can only store
721 // DefaultConstructible objects
722 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
723 ToolDescription (const std::string& n)
724 : Name(n), CmdLine(0), Actions(0), Flags(0)
725 {}
726};
727
728/// ToolDescriptions - A list of Tool information records.
729typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
730
731
732/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000733/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000734
735class CollectToolProperties;
736typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
737(const DagInit*);
738
739class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
740{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000741private:
742
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000743 /// toolDesc_ - Properties of the current Tool. This is where the
744 /// information is stored.
745 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000746
747public:
748
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000749 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000750 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000751 {
752 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000753
754 AddHandler("actions", &CollectToolProperties::onActions);
755 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
756 AddHandler("in_language", &CollectToolProperties::onInLanguage);
757 AddHandler("join", &CollectToolProperties::onJoin);
758 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
759 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
760 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000761
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000762 staticMembersInitialized_ = true;
763 }
764 }
765
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000766 void operator() (Init* i) {
767 InvokeDagInitHandler(this, i);
768 }
769
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000770private:
771
772 /// Property handlers --
773 /// Functions that extract information about tool properties from
774 /// DAG representation.
775
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000776 void onActions (const DagInit* d) {
777 checkNumberOfArguments(d, 1);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000778 Init* Case = d->getArg(0);
779 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000780 GetOperatorName(static_cast<DagInit*>(Case)) != "case")
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000781 throw
782 std::string("The argument to (actions) should be a 'case' construct!");
783 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000784 }
785
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000786 void onCmdLine (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000787 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000788 toolDesc_.CmdLine = d->getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000789 }
790
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000791 void onInLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000792 checkNumberOfArguments(d, 1);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000793 Init* arg = d->getArg(0);
794
795 // Find out the argument's type.
796 if (typeid(*arg) == typeid(StringInit)) {
797 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000798 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000799 }
800 else {
801 // It's a list.
802 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000803 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000804
805 // Copy strings to the output vector.
806 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
807 B != E; ++B) {
808 out.push_back(InitPtrToString(*B));
809 }
810
811 // Remove duplicates.
812 std::sort(out.begin(), out.end());
813 StrVector::iterator newE = std::unique(out.begin(), out.end());
814 out.erase(newE, out.end());
815 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000816 }
817
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000818 void onJoin (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000819 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000820 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000821 }
822
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000823 void onOutLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000824 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000825 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000826 }
827
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000828 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000829 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000830 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000831 }
832
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000833 void onSink (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000834 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000835 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000836 }
837
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000838};
839
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000840/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000841/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000842/// CollectToolProperties function object).
843void CollectToolDescriptions (RecordVector::const_iterator B,
844 RecordVector::const_iterator E,
845 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000846{
847 // Iterate over a properties list of every Tool definition
848 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000849 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000850 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000851 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000852
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000853 IntrusiveRefCntPtr<ToolDescription>
854 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000855
856 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000857 CollectToolProperties(*ToolDesc));
858 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000859 }
860}
861
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000862/// FillInEdgeVector - Merge all compilation graph definitions into
863/// one single edge list.
864void FillInEdgeVector(RecordVector::const_iterator B,
865 RecordVector::const_iterator E, RecordVector& Out) {
866 for (; B != E; ++B) {
867 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000868
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000869 for (unsigned i = 0; i < edges->size(); ++i)
870 Out.push_back(edges->getElementAsRecord(i));
871 }
872}
873
874/// CalculatePriority - Calculate the priority of this plugin.
875int CalculatePriority(RecordVector::const_iterator B,
876 RecordVector::const_iterator E) {
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000877 int priority = 0;
878
879 if (B != E) {
880 priority = static_cast<int>((*B)->getValueAsInt("priority"));
881
882 if (++B != E)
883 throw std::string("More than one 'PluginPriority' instance found: "
884 "most probably an error!");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000885 }
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000886
887 return priority;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000888}
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000889
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000890/// NotInGraph - Helper function object for FilterNotInGraph.
891struct NotInGraph {
892private:
893 const llvm::StringSet<>& ToolsInGraph_;
894
895public:
896 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
897 : ToolsInGraph_(ToolsInGraph)
898 {}
899
900 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
901 return (ToolsInGraph_.count(x->Name) == 0);
902 }
903};
904
905/// FilterNotInGraph - Filter out from ToolDescs all Tools not
906/// mentioned in the compilation graph definition.
907void FilterNotInGraph (const RecordVector& EdgeVector,
908 ToolDescriptions& ToolDescs) {
909
910 // List all tools mentioned in the graph.
911 llvm::StringSet<> ToolsInGraph;
912
913 for (RecordVector::const_iterator B = EdgeVector.begin(),
914 E = EdgeVector.end(); B != E; ++B) {
915
916 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000917 const std::string& NodeA = Edge->getValueAsString("a");
918 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000919
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000920 if (NodeA != "root")
921 ToolsInGraph.insert(NodeA);
922 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000923 }
924
925 // Filter ToolPropertiesList.
926 ToolDescriptions::iterator new_end =
927 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
928 NotInGraph(ToolsInGraph));
929 ToolDescs.erase(new_end, ToolDescs.end());
930}
931
932/// FillInToolToLang - Fills in two tables that map tool names to
933/// (input, output) languages. Helper function used by TypecheckGraph().
934void FillInToolToLang (const ToolDescriptions& ToolDescs,
935 StringMap<StringSet<> >& ToolToInLang,
936 StringMap<std::string>& ToolToOutLang) {
937 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
938 E = ToolDescs.end(); B != E; ++B) {
939 const ToolDescription& D = *(*B);
940 for (StrVector::const_iterator B = D.InLanguage.begin(),
941 E = D.InLanguage.end(); B != E; ++B)
942 ToolToInLang[D.Name].insert(*B);
943 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000944 }
945}
946
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000947/// TypecheckGraph - Check that names for output and input languages
948/// on all edges do match. This doesn't do much when the information
949/// about the whole graph is not available (i.e. when compiling most
950/// plugins).
951void TypecheckGraph (const RecordVector& EdgeVector,
952 const ToolDescriptions& ToolDescs) {
953 StringMap<StringSet<> > ToolToInLang;
954 StringMap<std::string> ToolToOutLang;
955
956 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
957 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
958 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
959
960 for (RecordVector::const_iterator B = EdgeVector.begin(),
961 E = EdgeVector.end(); B != E; ++B) {
962 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000963 const std::string& NodeA = Edge->getValueAsString("a");
964 const std::string& NodeB = Edge->getValueAsString("b");
965 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
966 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000967
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000968 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000969 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000970 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000971 + ": output->input language mismatch";
972 }
973
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000974 if (NodeB == "root")
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000975 throw std::string("Edges back to the root are not allowed!");
976 }
977}
978
979/// WalkCase - Walks the 'case' expression DAG and invokes
980/// TestCallback on every test, and StatementCallback on every
981/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000982/// combinators (that is, they are passed directly to TestCallback).
983/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
984/// IndentLevel, bool FirstTest)'.
985/// StatementCallback must have type 'void StatementCallback(const Init*,
986/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000987template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000988void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
989 unsigned IndentLevel = 0)
990{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000991 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000992
993 // Error checks.
994 if (GetOperatorName(d) != "case")
995 throw std::string("WalkCase should be invoked only on 'case' expressions!");
996
997 if (d.getNumArgs() < 2)
998 throw "There should be at least one clause in the 'case' expression:\n"
999 + d.getAsString();
1000
1001 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001002 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001003 const unsigned numArgs = d.getNumArgs();
1004 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001005 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1006 B != E; ++B) {
1007 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001008
1009 if (!even)
1010 {
1011 // Handle test.
1012 const DagInit& Test = InitPtrToDag(arg);
1013
1014 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
1015 throw std::string("The 'default' clause should be the last in the"
1016 "'case' construct!");
1017 if (i == numArgs)
1018 throw "Case construct handler: no corresponding action "
1019 "found for the test " + Test.getAsString() + '!';
1020
1021 TestCallback(&Test, IndentLevel, (i == 1));
1022 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001023 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001024 {
1025 if (dynamic_cast<DagInit*>(arg)
1026 && GetOperatorName(static_cast<DagInit*>(arg)) == "case") {
1027 // Nested 'case'.
1028 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1029 }
1030
1031 // Handle statement.
1032 StatementCallback(arg, IndentLevel);
1033 }
1034
1035 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001036 even = !even;
1037 }
1038}
1039
1040/// ExtractOptionNames - A helper function object used by
1041/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1042class ExtractOptionNames {
1043 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001044
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001045 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001046 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001047 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001048 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001049 ActionName == "forward_value" ||
1050 ActionName == "forward_transformed_value" ||
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001051 ActionName == "unpack_values" || ActionName == "switch_on" ||
1052 ActionName == "parameter_equals" || ActionName == "element_in_list" ||
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001053 ActionName == "not_empty" || ActionName == "empty") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001054 checkNumberOfArguments(&Stmt, 1);
1055 const std::string& Name = InitPtrToString(Stmt.getArg(0));
1056 OptionNames_.insert(Name);
1057 }
1058 else if (ActionName == "and" || ActionName == "or") {
1059 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001060 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001061 }
1062 }
1063 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001064
1065public:
1066 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1067 {}
1068
1069 void operator()(const Init* Statement) {
1070 if (typeid(*Statement) == typeid(ListInit)) {
1071 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1072 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1073 B != E; ++B)
1074 this->processDag(*B);
1075 }
1076 else {
1077 this->processDag(Statement);
1078 }
1079 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001080
1081 void operator()(const DagInit* Test, unsigned, bool) {
1082 this->operator()(Test);
1083 }
1084 void operator()(const Init* Statement, unsigned) {
1085 this->operator()(Statement);
1086 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001087};
1088
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001089/// CheckForSuperfluousOptions - Check that there are no side
1090/// effect-free options (specified only in the OptionList). Otherwise,
1091/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001092void CheckForSuperfluousOptions (const RecordVector& Edges,
1093 const ToolDescriptions& ToolDescs,
1094 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001095 llvm::StringSet<> nonSuperfluousOptions;
1096
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001097 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001098 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001099 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1100 E = ToolDescs.end(); B != E; ++B) {
1101 const ToolDescription& TD = *(*B);
1102 ExtractOptionNames Callback(nonSuperfluousOptions);
1103 if (TD.Actions)
1104 WalkCase(TD.Actions, Callback, Callback);
1105 }
1106
1107 // Add all options mentioned in the 'case' clauses of the
1108 // OptionalEdges of the compilation graph to the set of
1109 // non-superfluous options.
1110 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1111 B != E; ++B) {
1112 const Record* Edge = *B;
1113 DagInit* Weight = Edge->getValueAsDag("weight");
1114
1115 if (!isDagEmpty(Weight))
1116 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001117 }
1118
1119 // Check that all options in OptDescs belong to the set of
1120 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001121 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001122 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001123 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001124 if (!nonSuperfluousOptions.count(Val.Name)
1125 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001126 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001127 "Probable cause: this option is specified only in the OptionList.\n";
1128 }
1129}
1130
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001131/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1132bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1133 if (TestName == "single_input_file") {
1134 O << "InputFilenames.size() == 1";
1135 return true;
1136 }
1137 else if (TestName == "multiple_input_files") {
1138 O << "InputFilenames.size() > 1";
1139 return true;
1140 }
1141
1142 return false;
1143}
1144
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001145/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1146template <typename F>
1147void EmitListTest(const ListInit& L, const char* LogicOp,
1148 F Callback, raw_ostream& O)
1149{
1150 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1151 // of Dags...
1152 bool isFirst = true;
1153 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1154 if (isFirst)
1155 isFirst = false;
1156 else
1157 O << " || ";
1158 Callback(InitPtrToString(*B), O);
1159 }
1160}
1161
1162// Callbacks for use with EmitListTest.
1163
1164class EmitSwitchOn {
1165 const OptionDescriptions& OptDescs_;
1166public:
1167 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1168 {}
1169
1170 void operator()(const std::string& OptName, raw_ostream& O) const {
1171 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1172 O << OptDesc.GenVariableName();
1173 }
1174};
1175
1176class EmitEmptyTest {
1177 bool EmitNegate_;
1178 const OptionDescriptions& OptDescs_;
1179public:
1180 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1181 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1182 {}
1183
1184 void operator()(const std::string& OptName, raw_ostream& O) const {
1185 const char* Neg = (EmitNegate_ ? "!" : "");
1186 if (OptName == "o") {
1187 O << Neg << "OutputFilename.empty()";
1188 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001189 else if (OptName == "save-temps") {
1190 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1191 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001192 else {
1193 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1194 O << Neg << OptDesc.GenVariableName() << ".empty()";
1195 }
1196 }
1197};
1198
1199
1200/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1201bool EmitCaseTest1ArgList(const std::string& TestName,
1202 const DagInit& d,
1203 const OptionDescriptions& OptDescs,
1204 raw_ostream& O) {
1205 const ListInit& L = *static_cast<ListInit*>(d.getArg(0));
1206
1207 if (TestName == "any_switch_on") {
1208 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1209 return true;
1210 }
1211 else if (TestName == "switch_on") {
1212 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1213 return true;
1214 }
1215 else if (TestName == "any_not_empty") {
1216 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1217 return true;
1218 }
1219 else if (TestName == "any_empty") {
1220 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1221 return true;
1222 }
1223 else if (TestName == "not_empty") {
1224 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1225 return true;
1226 }
1227 else if (TestName == "empty") {
1228 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1229 return true;
1230 }
1231
1232 return false;
1233}
1234
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001235/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1236bool EmitCaseTest1ArgStr(const std::string& TestName,
1237 const DagInit& d,
1238 const OptionDescriptions& OptDescs,
1239 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001240 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001241
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001242 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001243 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001244 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001245 }
1246 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001247 O << "InLangs.count(\"" << OptName << "\") != 0";
1248 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001249 }
1250 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001251 // This works only for single-argument Tool::GenerateAction. Join
1252 // tools can process several files in different languages simultaneously.
1253
1254 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001255 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001256 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001257 }
1258 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001259 bool EmitNegate = (TestName == "not_empty");
1260 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001261 return true;
1262 }
1263
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001264 return false;
1265}
1266
1267/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1268bool EmitCaseTest1Arg(const std::string& TestName,
1269 const DagInit& d,
1270 const OptionDescriptions& OptDescs,
1271 raw_ostream& O) {
1272 checkNumberOfArguments(&d, 1);
1273 if (typeid(*d.getArg(0)) == typeid(ListInit))
1274 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1275 else
1276 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1277}
1278
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001279/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001280bool EmitCaseTest2Args(const std::string& TestName,
1281 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001282 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001283 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001284 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001285 checkNumberOfArguments(&d, 2);
1286 const std::string& OptName = InitPtrToString(d.getArg(0));
1287 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001288
1289 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001290 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001291 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1292 return true;
1293 }
1294 else if (TestName == "element_in_list") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001295 const OptionDescription& OptDesc = OptDescs.FindList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001296 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001297 O << "std::find(" << VarName << ".begin(),\n";
1298 O.indent(IndentLevel + Indent1)
1299 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001300 << OptArg << "\") != " << VarName << ".end()";
1301 return true;
1302 }
1303
1304 return false;
1305}
1306
1307// Forward declaration.
1308// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001309void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001310 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001311 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001312
1313/// EmitLogicalOperationTest - Helper function used by
1314/// EmitCaseConstructHandler.
1315void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001316 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001317 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001318 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001319 O << '(';
1320 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001321 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001322 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001323 if (j != NumArgs - 1) {
1324 O << ")\n";
1325 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1326 }
1327 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001328 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001329 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001330 }
1331}
1332
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001333void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001334 const OptionDescriptions& OptDescs, raw_ostream& O)
1335{
1336 checkNumberOfArguments(&d, 1);
1337 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1338 O << "! (";
1339 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1340 O << ")";
1341}
1342
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001343/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001344void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001345 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001346 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001347 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001348
1349 if (TestName == "and")
1350 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1351 else if (TestName == "or")
1352 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001353 else if (TestName == "not")
1354 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001355 else if (EmitCaseTest0Args(TestName, O))
1356 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001357 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1358 return;
1359 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1360 return;
1361 else
1362 throw TestName + ": unknown edge property!";
1363}
1364
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001365
1366/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1367class EmitCaseTestCallback {
1368 bool EmitElseIf_;
1369 const OptionDescriptions& OptDescs_;
1370 raw_ostream& O_;
1371public:
1372
1373 EmitCaseTestCallback(bool EmitElseIf,
1374 const OptionDescriptions& OptDescs, raw_ostream& O)
1375 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1376 {}
1377
1378 void operator()(const DagInit* Test, unsigned IndentLevel, bool FirstTest)
1379 {
1380 if (GetOperatorName(Test) == "default") {
1381 O_.indent(IndentLevel) << "else {\n";
1382 }
1383 else {
1384 O_.indent(IndentLevel)
1385 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
1386 EmitCaseTest(*Test, IndentLevel, OptDescs_, O_);
1387 O_ << ") {\n";
1388 }
1389 }
1390};
1391
1392/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1393template <typename F>
1394class EmitCaseStatementCallback {
1395 F Callback_;
1396 raw_ostream& O_;
1397public:
1398
1399 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1400 : Callback_(Callback), O_(O)
1401 {}
1402
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001403 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001404
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001405 // Ignore nested 'case' DAG.
1406 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001407 GetOperatorName(static_cast<const DagInit*>(Statement)) == "case")) {
1408 if (typeid(*Statement) == typeid(ListInit)) {
1409 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1410 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1411 B != E; ++B)
1412 Callback_(*B, (IndentLevel + Indent1), O_);
1413 }
1414 else {
1415 Callback_(Statement, (IndentLevel + Indent1), O_);
1416 }
1417 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001418 O_.indent(IndentLevel) << "}\n";
1419 }
1420
1421};
1422
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001423/// EmitCaseConstructHandler - Emit code that handles the 'case'
1424/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001425/// clause. Implemented on top of WalkCase.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001426/// Callback's type is void F(Init* Statement, unsigned IndentLevel,
1427/// raw_ostream& O).
1428/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001429/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1430/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001431template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001432void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001433 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001434 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001435 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001436 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1437 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001438}
1439
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001440/// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to
1441/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path"] .
1442/// Helper function used by EmitCmdLineVecFill and.
1443void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
1444 const char* Delimiters = " \t\n\v\f\r";
1445 enum TokenizerState
1446 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1447 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001448
1449 if (CmdLine.empty())
1450 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001451 Out.push_back("");
1452
1453 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1454 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001455
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001456 for (; B != E; ++B) {
1457 char cur_ch = CmdLine[B];
1458
1459 switch (cur_st) {
1460 case Normal:
1461 if (cur_ch == '$') {
1462 cur_st = SpecialCommand;
1463 break;
1464 }
1465 if (oneOf(Delimiters, cur_ch)) {
1466 // Skip whitespace
1467 B = CmdLine.find_first_not_of(Delimiters, B);
1468 if (B == std::string::npos) {
1469 B = E-1;
1470 continue;
1471 }
1472 --B;
1473 Out.push_back("");
1474 continue;
1475 }
1476 break;
1477
1478
1479 case SpecialCommand:
1480 if (oneOf(Delimiters, cur_ch)) {
1481 cur_st = Normal;
1482 Out.push_back("");
1483 continue;
1484 }
1485 if (cur_ch == '(') {
1486 Out.push_back("");
1487 cur_st = InsideSpecialCommand;
1488 continue;
1489 }
1490 break;
1491
1492 case InsideSpecialCommand:
1493 if (oneOf(Delimiters, cur_ch)) {
1494 continue;
1495 }
1496 if (cur_ch == '\'') {
1497 cur_st = InsideQuotationMarks;
1498 Out.push_back("");
1499 continue;
1500 }
1501 if (cur_ch == ')') {
1502 cur_st = Normal;
1503 Out.push_back("");
1504 }
1505 if (cur_ch == ',') {
1506 continue;
1507 }
1508
1509 break;
1510
1511 case InsideQuotationMarks:
1512 if (cur_ch == '\'') {
1513 cur_st = InsideSpecialCommand;
1514 continue;
1515 }
1516 break;
1517 }
1518
1519 Out.back().push_back(cur_ch);
1520 }
1521}
1522
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001523/// SubstituteSpecialCommands - Perform string substitution for $CALL
1524/// and $ENV. Helper function used by EmitCmdLineVecFill().
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001525StrVector::const_iterator SubstituteSpecialCommands
Daniel Dunbar1a551802009-07-03 00:10:29 +00001526(StrVector::const_iterator Pos, StrVector::const_iterator End, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001527{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001528
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001529 const std::string& cmd = *Pos;
1530
1531 if (cmd == "$CALL") {
1532 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1533 const std::string& CmdName = *Pos;
1534
1535 if (CmdName == ")")
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001536 throw std::string("$CALL invocation: empty argument list!");
1537
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001538 O << "hooks::";
1539 O << CmdName << "(";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001540
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001541
1542 bool firstIteration = true;
1543 while (true) {
1544 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1545 const std::string& Arg = *Pos;
1546 assert(Arg.size() != 0);
1547
1548 if (Arg[0] == ')')
1549 break;
1550
1551 if (firstIteration)
1552 firstIteration = false;
1553 else
1554 O << ", ";
1555
1556 O << '"' << Arg << '"';
1557 }
1558
1559 O << ')';
1560
1561 }
1562 else if (cmd == "$ENV") {
1563 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
1564 const std::string& EnvName = *Pos;
1565
1566 if (EnvName == ")")
1567 throw "$ENV invocation: empty argument list!";
1568
1569 O << "checkCString(std::getenv(\"";
1570 O << EnvName;
1571 O << "\"))";
1572
1573 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001574 }
1575 else {
1576 throw "Unknown special command: " + cmd;
1577 }
1578
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001579 const std::string& Leftover = *Pos;
1580 assert(Leftover.at(0) == ')');
1581 if (Leftover.size() != 1)
1582 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001583
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001584 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001585}
1586
1587/// EmitCmdLineVecFill - Emit code that fills in the command line
1588/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001589void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001590 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001591 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001592 StrVector StrVec;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001593 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1594
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001595 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001596 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001597
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001598 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1599
1600 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001601 assert(!StrVec[0].empty());
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001602 if (StrVec[0][0] == '$') {
1603 while (I != E && (*I)[0] != ')' )
1604 ++I;
1605
1606 // Skip the ')' symbol.
1607 ++I;
1608 }
1609 else {
1610 ++I;
1611 }
1612
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001613 bool hasINFILE = false;
1614
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001615 for (; I != E; ++I) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001616 const std::string& cmd = *I;
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001617 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001618 O.indent(IndentLevel);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001619 if (cmd.at(0) == '$') {
1620 if (cmd == "$INFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001621 hasINFILE = true;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001622 if (IsJoin) {
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001623 O << "for (PathVector::const_iterator B = inFiles.begin()"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001624 << ", E = inFiles.end();\n";
1625 O.indent(IndentLevel) << "B != E; ++B)\n";
1626 O.indent(IndentLevel + Indent1) << "vec.push_back(B->str());\n";
1627 }
1628 else {
Chris Lattner74382b72009-08-23 22:45:37 +00001629 O << "vec.push_back(inFile.str());\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001630 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001631 }
1632 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001633 O << "vec.push_back(\"\");\n";
1634 O.indent(IndentLevel) << "out_file_index = vec.size()-1;\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001635 }
1636 else {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001637 O << "vec.push_back(";
1638 I = SubstituteSpecialCommands(I, E, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001639 O << ");\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001640 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001641 }
1642 else {
1643 O << "vec.push_back(\"" << cmd << "\");\n";
1644 }
1645 }
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001646 if (!hasINFILE)
1647 throw "Tool '" + ToolName + "' doesn't take any input!";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001648
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001649 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001650 if (StrVec[0][0] == '$')
1651 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), O);
1652 else
1653 O << '"' << StrVec[0] << '"';
1654 O << ";\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001655}
1656
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001657/// EmitCmdLineVecFillCallback - A function object wrapper around
1658/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1659/// argument to EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001660class EmitCmdLineVecFillCallback {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001661 bool IsJoin;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001662 const std::string& ToolName;
1663 public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001664 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1665 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001666
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001667 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001668 raw_ostream& O) const
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001669 {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001670 EmitCmdLineVecFill(Statement, ToolName, IsJoin, IndentLevel, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001671 }
1672};
1673
1674/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1675/// implement EmitActionHandler. Emits code for
1676/// handling the (forward) and (forward_as) option properties.
1677void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001678 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001679 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001680 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001681 const std::string& Name = NewName.empty()
1682 ? ("-" + D.Name)
1683 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001684 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001685
1686 switch (D.Type) {
1687 case OptionType::Switch:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001688 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001689 break;
1690 case OptionType::Parameter:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001691 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
1692 O.indent(IndentLevel) << "vec.push_back(" << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001693 break;
1694 case OptionType::Prefix:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001695 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\" + "
1696 << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001697 break;
1698 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001699 O.indent(IndentLevel)
1700 << "for (" << D.GenTypeDeclaration()
1701 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1702 O.indent(IndentLevel)
1703 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1704 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\" + " << "*B);\n";
1705 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001706
1707 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001708 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1709 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001710 }
1711
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001712 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001713 break;
1714 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001715 O.indent(IndentLevel)
1716 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1717 << D.GenVariableName() << ".begin(),\n";
1718 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1719 << ".end() ; B != E;) {\n";
1720 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001721
1722 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001723 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1724 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001725 }
1726
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001727 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001728 break;
1729 case OptionType::Alias:
1730 default:
1731 throw std::string("Aliases are not allowed in tool option descriptions!");
1732 }
1733}
1734
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001735/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1736/// EmitPreprocessOptionsCallback.
1737struct ActionHandlingCallbackBase {
1738
1739 void onErrorDag(const DagInit& d,
1740 unsigned IndentLevel, raw_ostream& O) const
1741 {
1742 O.indent(IndentLevel)
1743 << "throw std::runtime_error(\"" <<
1744 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1745 : "Unknown error!")
1746 << "\");\n";
1747 }
1748
1749 void onWarningDag(const DagInit& d,
1750 unsigned IndentLevel, raw_ostream& O) const
1751 {
1752 checkNumberOfArguments(&d, 1);
1753 O.indent(IndentLevel) << "llvm::errs() << \""
1754 << InitPtrToString(d.getArg(0)) << "\";\n";
1755 }
1756
1757};
1758
1759/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1760/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001761class EmitActionHandlersCallback;
1762typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1763(const DagInit&, unsigned, raw_ostream&) const;
1764
1765class EmitActionHandlersCallback
1766: public ActionHandlingCallbackBase,
1767 public HandlerTable<EmitActionHandlersCallbackHandler>
1768{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001769 const OptionDescriptions& OptDescs;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001770 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001771
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001772 void onAppendCmd (const DagInit& Dag,
1773 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001774 {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001775 checkNumberOfArguments(&Dag, 1);
1776 const std::string& Cmd = InitPtrToString(Dag.getArg(0));
1777 StrVector Out;
1778 llvm::SplitString(Cmd, Out);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001779
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001780 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1781 B != E; ++B)
1782 O.indent(IndentLevel) << "vec.push_back(\"" << *B << "\");\n";
1783 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001784
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001785 void onForward (const DagInit& Dag,
1786 unsigned IndentLevel, raw_ostream& O) const
1787 {
1788 checkNumberOfArguments(&Dag, 1);
1789 const std::string& Name = InitPtrToString(Dag.getArg(0));
1790 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1791 IndentLevel, "", O);
1792 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001793
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001794 void onForwardAs (const DagInit& Dag,
1795 unsigned IndentLevel, raw_ostream& O) const
1796 {
1797 checkNumberOfArguments(&Dag, 2);
1798 const std::string& Name = InitPtrToString(Dag.getArg(0));
1799 const std::string& NewName = InitPtrToString(Dag.getArg(1));
1800 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1801 IndentLevel, NewName, O);
1802 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001803
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001804 void onForwardValue (const DagInit& Dag,
1805 unsigned IndentLevel, raw_ostream& O) const
1806 {
1807 checkNumberOfArguments(&Dag, 1);
1808 const std::string& Name = InitPtrToString(Dag.getArg(0));
1809 const OptionDescription& D = OptDescs.FindOption(Name);
1810
1811 if (D.isParameter()) {
1812 O.indent(IndentLevel) << "vec.push_back("
1813 << D.GenVariableName() << ");\n";
1814 }
1815 else if (D.isList()) {
1816 O.indent(IndentLevel) << "std::copy(" << D.GenVariableName()
1817 << ".begin(), " << D.GenVariableName()
1818 << ".end(), std::back_inserter(vec));\n";
1819 }
1820 else {
1821 throw "'forward_value' used with a switch or an alias!";
1822 }
1823 }
1824
1825 void onForwardTransformedValue (const DagInit& Dag,
1826 unsigned IndentLevel, raw_ostream& O) const
1827 {
1828 checkNumberOfArguments(&Dag, 2);
1829 const std::string& Name = InitPtrToString(Dag.getArg(0));
1830 const std::string& Hook = InitPtrToString(Dag.getArg(1));
1831 const OptionDescription& D = OptDescs.FindOption(Name);
1832
1833 if (D.isParameter() || D.isList()) {
1834 O.indent(IndentLevel) << "vec.push_back(" << "hooks::"
1835 << Hook << "(" << D.GenVariableName() << "));\n";
1836 }
1837 else {
1838 throw "'forward_transformed_value' used with a switch or an alias!";
1839 }
1840 }
1841
1842
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001843 void onOutputSuffix (const DagInit& Dag,
1844 unsigned IndentLevel, raw_ostream& O) const
1845 {
1846 checkNumberOfArguments(&Dag, 1);
1847 const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
1848 O.indent(IndentLevel) << "output_suffix = \"" << OutSuf << "\";\n";
1849 }
1850
1851 void onStopCompilation (const DagInit& Dag,
1852 unsigned IndentLevel, raw_ostream& O) const
1853 {
1854 O.indent(IndentLevel) << "stop_compilation = true;\n";
1855 }
1856
1857
1858 void onUnpackValues (const DagInit& Dag,
1859 unsigned IndentLevel, raw_ostream& O) const
1860 {
1861 checkNumberOfArguments(&Dag, 1);
1862 const std::string& Name = InitPtrToString(Dag.getArg(0));
1863 const OptionDescription& D = OptDescs.FindOption(Name);
1864
1865 if (D.isMultiVal())
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001866 throw "Can't use unpack_values with multi-valued options!";
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001867
1868 if (D.isList()) {
1869 O.indent(IndentLevel)
1870 << "for (" << D.GenTypeDeclaration()
1871 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1872 O.indent(IndentLevel)
1873 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n";
1874 O.indent(IndentLevel + Indent1)
1875 << "llvm::SplitString(*B, vec, \",\");\n";
1876 }
1877 else if (D.isParameter()){
1878 O.indent(IndentLevel) << "llvm::SplitString("
1879 << D.GenVariableName() << ", vec, \",\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001880 }
1881 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001882 throw "Option '" + D.Name +
1883 "': switches can't have the 'unpack_values' property!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001884 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001885 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001886
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001887 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001888
1889 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
1890 : OptDescs(OD)
1891 {
1892 if (!staticMembersInitialized_) {
1893 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
1894 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
1895 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
1896 AddHandler("forward", &EmitActionHandlersCallback::onForward);
1897 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001898 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
1899 AddHandler("forward_transformed_value",
1900 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001901 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
1902 AddHandler("stop_compilation",
1903 &EmitActionHandlersCallback::onStopCompilation);
1904 AddHandler("unpack_values",
1905 &EmitActionHandlersCallback::onUnpackValues);
1906
1907 staticMembersInitialized_ = true;
1908 }
1909 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001910
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001911 void operator()(const Init* Statement,
1912 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001913 {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001914 const DagInit& Dag = InitPtrToDag(Statement);
1915 const std::string& ActionName = GetOperatorName(Dag);
1916 Handler h = GetHandler(ActionName);
1917
1918 ((this)->*(h))(Dag, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001919 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001920};
1921
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001922bool IsOutFileIndexCheckRequiredStr (const Init* CmdLine) {
1923 StrVector StrVec;
1924 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1925
1926 for (StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1927 I != E; ++I) {
1928 if (*I == "$OUTFILE")
1929 return false;
1930 }
1931
1932 return true;
1933}
1934
1935class IsOutFileIndexCheckRequiredStrCallback {
1936 bool* ret_;
1937
1938public:
1939 IsOutFileIndexCheckRequiredStrCallback(bool* ret) : ret_(ret)
1940 {}
1941
1942 void operator()(const Init* CmdLine) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001943 // Ignore nested 'case' DAG.
1944 if (typeid(*CmdLine) == typeid(DagInit))
1945 return;
1946
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001947 if (IsOutFileIndexCheckRequiredStr(CmdLine))
1948 *ret_ = true;
1949 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001950
1951 void operator()(const DagInit* Test, unsigned, bool) {
1952 this->operator()(Test);
1953 }
1954 void operator()(const Init* Statement, unsigned) {
1955 this->operator()(Statement);
1956 }
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001957};
1958
1959bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
1960 bool ret = false;
1961 WalkCase(CmdLine, Id(), IsOutFileIndexCheckRequiredStrCallback(&ret));
1962 return ret;
1963}
1964
1965/// IsOutFileIndexCheckRequired - Should we emit an "out_file_index != -1" check
1966/// in EmitGenerateActionMethod() ?
1967bool IsOutFileIndexCheckRequired (Init* CmdLine) {
1968 if (typeid(*CmdLine) == typeid(StringInit))
1969 return IsOutFileIndexCheckRequiredStr(CmdLine);
1970 else
1971 return IsOutFileIndexCheckRequiredCase(CmdLine);
1972}
1973
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001974void EmitGenerateActionMethodHeader(const ToolDescription& D,
1975 bool IsJoin, raw_ostream& O)
1976{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001977 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001978 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001979 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001980 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001981
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001982 O.indent(Indent2) << "bool HasChildren,\n";
1983 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1984 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1985 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1986 O.indent(Indent1) << "{\n";
1987 O.indent(Indent2) << "std::string cmd;\n";
1988 O.indent(Indent2) << "std::vector<std::string> vec;\n";
1989 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
1990 O.indent(Indent2) << "const char* output_suffix = \""
1991 << D.OutputSuffix << "\";\n";
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001992}
1993
1994// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
1995// Tool::GenerateAction() method.
1996void EmitGenerateActionMethod (const ToolDescription& D,
1997 const OptionDescriptions& OptDescs,
1998 bool IsJoin, raw_ostream& O) {
1999
2000 EmitGenerateActionMethodHeader(D, IsJoin, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002001
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002002 if (!D.CmdLine)
2003 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002004
2005 bool IndexCheckRequired = IsOutFileIndexCheckRequired(D.CmdLine);
2006 O.indent(Indent2) << "int out_file_index"
2007 << (IndexCheckRequired ? " = -1" : "")
2008 << ";\n\n";
2009
2010 // Process the cmd_line property.
2011 if (typeid(*D.CmdLine) == typeid(StringInit))
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002012 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2013 else
2014 EmitCaseConstructHandler(D.CmdLine, Indent2,
2015 EmitCmdLineVecFillCallback(IsJoin, D.Name),
2016 true, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002017
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002018 // For every understood option, emit handling code.
2019 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002020 EmitCaseConstructHandler(D.Actions, Indent2,
2021 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002022 false, OptDescs, O);
2023
2024 O << '\n';
2025 O.indent(Indent2)
2026 << "std::string out_file = OutFilename("
2027 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
2028 O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002029
2030 if (IndexCheckRequired)
2031 O.indent(Indent2) << "if (out_file_index != -1)\n";
2032 O.indent(IndexCheckRequired ? Indent3 : Indent2)
2033 << "vec[out_file_index] = out_file;\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002034
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002035 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002036 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002037 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
2038 O.indent(Indent3) << "vec.insert(vec.end(), "
2039 << SinkOptionName << ".begin(), " << SinkOptionName
2040 << ".end());\n";
2041 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002042 }
2043
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002044 O.indent(Indent2) << "return Action(cmd, vec, stop_compilation, out_file);\n";
2045 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002046}
2047
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002048/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2049/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002050void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2051 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002052 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002053 if (!ToolDesc.isJoin()) {
2054 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
2055 O.indent(Indent2) << "bool HasChildren,\n";
2056 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2057 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2058 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2059 O.indent(Indent1) << "{\n";
2060 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
2061 << " is not a Join tool!\");\n";
2062 O.indent(Indent1) << "}\n\n";
2063 }
2064 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002065 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002066 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002067
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002068 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002069}
2070
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002071/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2072/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002073void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002074 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2075 O.indent(Indent2) << "return InputLanguages_;\n";
2076 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002077
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002078 if (D.OutLanguage.empty())
2079 throw "Tool " + D.Name + " has no 'out_language' property!";
2080
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002081 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2082 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2083 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002084}
2085
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002086/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002087void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002088 O.indent(Indent1) << "const char* Name() const {\n";
2089 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2090 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002091}
2092
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002093/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2094/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002095void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002096 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002097 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002098 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002099 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002100 O.indent(Indent2) << "return false;\n";
2101 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002102}
2103
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002104/// EmitStaticMemberDefinitions - Emit static member definitions for a
2105/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002106void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002107 if (D.InLanguage.empty())
2108 throw "Tool " + D.Name + " has no 'in_language' property!";
2109
2110 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2111 for (StrVector::const_iterator B = D.InLanguage.begin(),
2112 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002113 O << '\"' << *B << "\", ";
2114 O << "0};\n\n";
2115}
2116
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002117/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002118void EmitToolClassDefinition (const ToolDescription& D,
2119 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002120 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002121 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002122 return;
2123
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002124 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002125 O << "class " << D.Name << " : public ";
2126 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002127 O << "JoinTool";
2128 else
2129 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002130
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002131 O << "{\nprivate:\n";
2132 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002133
2134 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002135 EmitNameMethod(D, O);
2136 EmitInOutLanguageMethods(D, O);
2137 EmitIsJoinMethod(D, O);
2138 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002139
2140 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002141 O << "};\n";
2142
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002143 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002144
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002145}
2146
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002147/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002148/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002149void EmitOptionDefinitions (const OptionDescriptions& descs,
2150 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002151 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002152{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002153 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002154
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002155 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002156 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002157 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002158 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002159
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002160 if (val.Type == OptionType::Alias) {
2161 Aliases.push_back(val);
2162 continue;
2163 }
2164
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002165 if (val.isExtern())
2166 O << "extern ";
2167
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002168 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002169 << val.GenVariableName();
2170
2171 if (val.isExtern()) {
2172 O << ";\n";
2173 continue;
2174 }
2175
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002176 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002177
2178 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2179 O << ", cl::Prefix";
2180
2181 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002182 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002183 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002184 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002185 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002186 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002187 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002188 O << ", cl::OneOrMore";
2189 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002190 else if (val.isZeroOrOne() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002191 O << ", cl::ZeroOrOne";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002192 }
2193
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002194 if (val.isReallyHidden()) {
2195 O << ", cl::ReallyHidden";
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002196 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002197 else if (val.isHidden()) {
2198 O << ", cl::Hidden";
2199 }
2200
2201 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002202 O << ", cl::multi_val(" << val.MultiVal << ')';
2203
2204 if (val.InitVal) {
2205 const std::string& str = val.InitVal->getAsString();
2206 O << ", cl::init(" << str << ')';
2207 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002208
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002209 if (!val.Help.empty())
2210 O << ", cl::desc(\"" << val.Help << "\")";
2211
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002212 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002213 }
2214
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002215 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002216 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002217 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002218 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002219
2220 O << val.GenTypeDeclaration() << ' '
2221 << val.GenVariableName()
2222 << "(\"" << val.Name << '\"';
2223
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002224 const OptionDescription& D = descs.FindOption(val.Help);
2225 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002226
2227 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2228 }
2229
2230 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002231 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002232 O << (HasExterns ? "extern cl" : "cl")
2233 << "::list<std::string> " << SinkOptionName
2234 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002235
2236 O << '\n';
2237}
2238
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002239/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002240/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002241class EmitPreprocessOptionsCallback : ActionHandlingCallbackBase {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002242 const OptionDescriptions& OptDescs_;
2243
2244 void onUnsetOption(Init* i, unsigned IndentLevel, raw_ostream& O) {
2245 const std::string& OptName = InitPtrToString(i);
2246 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002247
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002248 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002249 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2250 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002251 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002252 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2253 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002254 else if (OptDesc.isList()) {
2255 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2256 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002257 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002258 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002259 }
2260 }
2261
2262 void processDag(const Init* I, unsigned IndentLevel, raw_ostream& O)
2263 {
2264 const DagInit& d = InitPtrToDag(I);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002265 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002266
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002267 if (OpName == "warning") {
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 == "error") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002271 this->onWarningDag(d, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002272 }
2273 else if (OpName == "unset_option") {
2274 checkNumberOfArguments(&d, 1);
2275 Init* I = d.getArg(0);
2276 if (typeid(*I) == typeid(ListInit)) {
2277 const ListInit& DagList = *static_cast<const ListInit*>(I);
2278 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
2279 B != E; ++B)
2280 this->onUnsetOption(*B, IndentLevel, O);
2281 }
2282 else {
2283 this->onUnsetOption(I, IndentLevel, O);
2284 }
2285 }
2286 else {
2287 throw "Unknown operator in the option preprocessor: '" + OpName + "'!"
2288 "\nOnly 'warning', 'error' and 'unset_option' are allowed.";
2289 }
2290 }
2291
2292public:
2293
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002294 void operator()(const Init* I, unsigned IndentLevel, raw_ostream& O) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002295 this->processDag(I, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002296 }
2297
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002298 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002299 : OptDescs_(OptDescs)
2300 {}
2301};
2302
2303/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2304void EmitPreprocessOptions (const RecordKeeper& Records,
2305 const OptionDescriptions& OptDecs, raw_ostream& O)
2306{
2307 O << "void PreprocessOptionsLocal() {\n";
2308
2309 const RecordVector& OptionPreprocessors =
2310 Records.getAllDerivedDefinitions("OptionPreprocessor");
2311
2312 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2313 E = OptionPreprocessors.end(); B!=E; ++B) {
2314 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002315 EmitCaseConstructHandler(Case, Indent1,
2316 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002317 false, OptDecs, O);
2318 }
2319
2320 O << "}\n\n";
2321}
2322
2323/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002324void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002325{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002326 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002327
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002328 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002329 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002330
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002331 // It is allowed for a plugin to have no language map.
2332 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002333
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002334 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2335 if (!LangsToSuffixesList)
2336 throw std::string("Error in the language map definition!");
2337
2338 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002339 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002340
2341 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2342 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2343
2344 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002345 O.indent(Indent1) << "langMap[\""
2346 << InitPtrToString(Suffixes->getElement(i))
2347 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002348 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002349 }
2350
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002351 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002352}
2353
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002354/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2355/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002356void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002357 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002358 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002359 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002360
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002361 if (OpName == "inc_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 == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002365 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002366 }
2367 else if (OpName == "error") {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002368 checkNumberOfArguments(&d, 1);
2369 O.indent(IndentLevel) << "throw std::runtime_error(\""
2370 << InitPtrToString(d.getArg(0))
2371 << "\");\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002372 return;
2373 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002374 else {
2375 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002376 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002377 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002378
2379 if (d.getNumArgs() > 0)
2380 O << InitPtrToInt(d.getArg(0)) << ";\n";
2381 else
2382 O << "2;\n";
2383
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002384}
2385
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002386/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002387void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002388 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002389 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002390
2391 // Class constructor.
2392 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002393 << "public:\n";
2394 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2395 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002396
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002397 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002398 O.indent(Indent1)
2399 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2400 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002401
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002402 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002403 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002404
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002405 O.indent(Indent2) << "return ret;\n";
2406 O.indent(Indent1) << "};\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002407}
2408
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002409/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002410void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002411 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002412 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002413 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002414 for (RecordVector::const_iterator B = EdgeVector.begin(),
2415 E = EdgeVector.end(); B != E; ++B) {
2416 const Record* Edge = *B;
2417 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002418 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002419
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002420 if (!isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002421 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002422 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002423 }
2424}
2425
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002426/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002427/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002428void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002429 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002430 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002431{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002432 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002433
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002434 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2435 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002436 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002437
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002438 O << '\n';
2439
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002440 // Insert edges.
2441
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002442 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002443 for (RecordVector::const_iterator B = EdgeVector.begin(),
2444 E = EdgeVector.end(); B != E; ++B) {
2445 const Record* Edge = *B;
2446 const std::string& NodeA = Edge->getValueAsString("a");
2447 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002448 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002449
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002450 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002451
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002452 if (isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002453 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002454 else
2455 O << "new Edge" << i << "()";
2456
2457 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002458 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002459 }
2460
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002461 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002462}
2463
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002464/// HookInfo - Information about the hook type and number of arguments.
2465struct HookInfo {
2466
2467 // A hook can either have a single parameter of type std::vector<std::string>,
2468 // or NumArgs parameters of type const char*.
2469 enum HookType { ListHook, ArgHook };
2470
2471 HookType Type;
2472 unsigned NumArgs;
2473
2474 HookInfo() : Type(ArgHook), NumArgs(1)
2475 {}
2476
2477 HookInfo(HookType T) : Type(T), NumArgs(1)
2478 {}
2479
2480 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2481 {}
2482};
2483
2484typedef llvm::StringMap<HookInfo> HookInfoMap;
2485
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002486/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002487/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002488/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002489class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002490 HookInfoMap& HookNames_;
2491 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002492public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002493 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2494 : HookNames_(HookNames), OptDescs_(OptDescs)
2495 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002496
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002497 void onAction (const DagInit& Dag) {
2498 if (GetOperatorName(Dag) == "forward_transformed_value") {
2499 checkNumberOfArguments(Dag, 2);
2500 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2501 const std::string& HookName = InitPtrToString(Dag.getArg(1));
2502 const OptionDescription& D = OptDescs_.FindOption(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002503
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002504 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2505 : HookInfo::ArgHook);
2506 }
2507 }
2508
2509 void operator()(const Init* Arg) {
2510
2511 // We're invoked on an action (either a dag or a dag list).
2512 if (typeid(*Arg) == typeid(DagInit)) {
2513 const DagInit& Dag = InitPtrToDag(Arg);
2514 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002515 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002516 }
2517 else if (typeid(*Arg) == typeid(ListInit)) {
2518 const ListInit& List = InitPtrToList(Arg);
2519 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2520 ++B) {
2521 const DagInit& Dag = InitPtrToDag(*B);
2522 this->onAction(Dag);
2523 }
2524 return;
2525 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002526
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002527 // We're invoked on a command line.
2528 StrVector cmds;
2529 TokenizeCmdline(InitPtrToString(Arg), cmds);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002530 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2531 B != E; ++B) {
2532 const std::string& cmd = *B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002533
2534 if (cmd == "$CALL") {
2535 unsigned NumArgs = 0;
2536 checkedIncrement(B, E, "Syntax error in $CALL invocation!");
2537 const std::string& HookName = *B;
2538
2539
2540 if (HookName.at(0) == ')')
2541 throw "$CALL invoked with no arguments!";
2542
2543 while (++B != E && B->at(0) != ')') {
2544 ++NumArgs;
2545 }
2546
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002547 HookInfoMap::const_iterator H = HookNames_.find(HookName);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002548
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002549 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2550 H->second.Type != HookInfo::ArgHook)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002551 throw "Overloading of hooks is not allowed. Overloaded hook: "
2552 + HookName;
2553 else
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002554 HookNames_[HookName] = HookInfo(NumArgs);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002555
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002556 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002557 }
2558 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002559
2560 void operator()(const DagInit* Test, unsigned, bool) {
2561 this->operator()(Test);
2562 }
2563 void operator()(const Init* Statement, unsigned) {
2564 this->operator()(Statement);
2565 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002566};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002567
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002568/// FillInHookNames - Actually extract the hook names from all command
2569/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002570void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002571 const OptionDescriptions& OptDescs,
2572 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002573{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002574 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002575 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2576 E = ToolDescs.end(); B != E; ++B) {
2577 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002578
2579 // Look for 'forward_transformed_value' in 'actions'.
2580 if (D.Actions)
2581 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2582
2583 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002584 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002585 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002586 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002587 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002588 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002589 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002590 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002591 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002592 }
2593}
2594
2595/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2596/// property records and emit hook function declaration for each
2597/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002598void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2599 const OptionDescriptions& OptDescs, raw_ostream& O) {
2600 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002601
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002602 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002603 if (HookNames.empty())
2604 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002605
2606 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002607 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002608 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002609 const char* HookName = B->first();
2610 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002611
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002612 O.indent(Indent1) << "std::string " << HookName << "(";
2613
2614 if (Info.Type == HookInfo::ArgHook) {
2615 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2616 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2617 }
2618 }
2619 else {
2620 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002621 }
2622
2623 O <<");\n";
2624 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002625 O << "}\n\n";
2626}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002627
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002628/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002629void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002630 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2631 O.indent(Indent1) << "int Priority() const { return "
2632 << Priority << "; }\n\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002633 O.indent(Indent1) << "void PreprocessOptions() const\n";
2634 O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002635 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2636 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2637 O.indent(Indent1)
2638 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2639 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2640 << "};\n\n"
2641 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002642}
2643
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002644/// EmitIncludes - Emit necessary #include directives and some
2645/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002646void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002647 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2648 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002649 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002650 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2651 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002652
2653 << "#include \"llvm/ADT/StringExtras.h\"\n"
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002654 << "#include \"llvm/Support/CommandLine.h\"\n"
2655 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002656
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002657 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002658 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002659 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002660 << "#include <stdexcept>\n\n"
2661
2662 << "using namespace llvm;\n"
2663 << "using namespace llvmc;\n\n"
2664
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002665 << "extern cl::opt<std::string> OutputFilename;\n\n"
2666
2667 << "inline const char* checkCString(const char* s)\n"
2668 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002669}
2670
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002671
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002672/// PluginData - Holds all information about a plugin.
2673struct PluginData {
2674 OptionDescriptions OptDescs;
2675 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002676 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002677 ToolDescriptions ToolDescs;
2678 RecordVector Edges;
2679 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002680};
2681
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002682/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002683/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002684bool HasSink(const ToolDescriptions& ToolDescs) {
2685 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2686 E = ToolDescs.end(); B != E; ++B)
2687 if ((*B)->isSink())
2688 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002689
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002690 return false;
2691}
2692
2693/// HasExterns - Go through the list of option descriptions and check
2694/// if there are any external options.
2695bool HasExterns(const OptionDescriptions& OptDescs) {
2696 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2697 E = OptDescs.end(); B != E; ++B)
2698 if (B->second.isExtern())
2699 return true;
2700
2701 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002702}
2703
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002704/// CollectPluginData - Collect tool and option properties,
2705/// compilation graph edges and plugin priority from the parse tree.
2706void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2707 // Collect option properties.
2708 const RecordVector& OptionLists =
2709 Records.getAllDerivedDefinitions("OptionList");
2710 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2711 Data.OptDescs);
2712
2713 // Collect tool properties.
2714 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2715 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2716 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002717 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002718
2719 // Collect compilation graph edges.
2720 const RecordVector& CompilationGraphs =
2721 Records.getAllDerivedDefinitions("CompilationGraph");
2722 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2723 Data.Edges);
2724
2725 // Calculate the priority of this plugin.
2726 const RecordVector& Priorities =
2727 Records.getAllDerivedDefinitions("PluginPriority");
2728 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002729}
2730
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002731/// CheckPluginData - Perform some sanity checks on the collected data.
2732void CheckPluginData(PluginData& Data) {
2733 // Filter out all tools not mentioned in the compilation graph.
2734 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002735
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002736 // Typecheck the compilation graph.
2737 TypecheckGraph(Data.Edges, Data.ToolDescs);
2738
2739 // Check that there are no options without side effects (specified
2740 // only in the OptionList).
2741 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2742
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002743}
2744
Daniel Dunbar1a551802009-07-03 00:10:29 +00002745void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002746 // Emit file header.
2747 EmitIncludes(O);
2748
2749 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002750 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002751
2752 // Emit hook declarations.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002753 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002754
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002755 O << "namespace {\n\n";
2756
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002757 // Emit PreprocessOptionsLocal() function.
2758 EmitPreprocessOptions(Records, Data.OptDescs, O);
2759
2760 // Emit PopulateLanguageMapLocal() function
2761 // (language map maps from file extensions to language names).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002762 EmitPopulateLanguageMap(Records, O);
2763
2764 // Emit Tool classes.
2765 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2766 E = Data.ToolDescs.end(); B!=E; ++B)
2767 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2768
2769 // Emit Edge# classes.
2770 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2771
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002772 // Emit PopulateCompilationGraphLocal() function.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002773 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2774
2775 // Emit code for plugin registration.
2776 EmitRegisterPlugin(Data.Priority, O);
2777
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002778 O << "} // End anonymous namespace.\n\n";
2779
2780 // Force linkage magic.
2781 O << "namespace llvmc {\n";
2782 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2783 O << "}\n";
2784
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002785 // EOF
2786}
2787
2788
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002789// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00002790}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002791
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002792/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002793void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002794 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002795 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002796
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002797 CollectPluginData(Records, Data);
2798 CheckPluginData(Data);
2799
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +00002800 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002801 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002802
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002803 } catch (std::exception& Error) {
2804 throw Error.what() + std::string(" - usually this means a syntax error.");
2805 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002806}