blob: acffc435f1a84cb804ef69dc8c3e19f805cb9a95 [file] [log] [blame]
Mikhail Glushenkovfb37f392008-05-30 06:20:54 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000010// This tablegen backend is responsible for emitting LLVMC configuration code.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkovecbdcf22008-05-06 18:09:29 +000014#include "LLVMCConfigurationEmitter.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000015#include "Record.h"
16
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/StringMap.h"
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000021#include "llvm/ADT/StringSet.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000022#include <algorithm>
23#include <cassert>
24#include <functional>
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +000025#include <stdexcept>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000026#include <string>
Chris Lattner32a9e7a2008-06-04 04:46:14 +000027#include <typeinfo>
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +000028
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000029using namespace llvm;
30
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
32//===----------------------------------------------------------------------===//
33/// Typedefs
34
35typedef std::vector<Record*> RecordVector;
36typedef std::vector<std::string> StrVector;
37
38//===----------------------------------------------------------------------===//
39/// Constants
40
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +000041// Indentation.
Chris Lattnerec8e1b72009-11-03 18:30:31 +000042static const unsigned TabWidth = 4;
43static const unsigned Indent1 = TabWidth*1;
44static const unsigned Indent2 = TabWidth*2;
45static const unsigned Indent3 = TabWidth*3;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000046
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000047// Default help string.
Chris Lattnerec8e1b72009-11-03 18:30:31 +000048static const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000049
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000050// Name for the "sink" option.
Chris Lattnerec8e1b72009-11-03 18:30:31 +000051static const char * const SinkOptionName = "AutoGeneratedSinkOption";
52
53namespace {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000054
55//===----------------------------------------------------------------------===//
56/// Helper functions
57
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000058/// Id - An 'identity' function object.
59struct Id {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000060 template<typename T0>
61 void operator()(const T0&) const {
62 }
63 template<typename T0, typename T1>
64 void operator()(const T0&, const T1&) const {
65 }
66 template<typename T0, typename T1, typename T2>
67 void operator()(const T0&, const T1&, const T2&) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000068 }
69};
70
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000071int InitPtrToInt(const Init* ptr) {
72 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000073 return val.getValue();
74}
75
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000076const std::string& InitPtrToString(const Init* ptr) {
77 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
78 return val.getValue();
79}
80
81const ListInit& InitPtrToList(const Init* ptr) {
82 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
83 return val;
84}
85
86const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000087 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000088 return val;
89}
90
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000091const std::string GetOperatorName(const DagInit* D) {
92 return D->getOperator()->getAsString();
93}
94
95const std::string GetOperatorName(const DagInit& D) {
96 return GetOperatorName(&D);
97}
98
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000099// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +0000100// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +0000101void checkNumberOfArguments (const DagInit* d, unsigned minArgs) {
102 if (!d || d->getNumArgs() < minArgs)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000103 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000104}
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +0000105void checkNumberOfArguments (const DagInit& d, unsigned minArgs) {
106 checkNumberOfArguments(&d, minArgs);
107}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000108
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000109// isDagEmpty - is this DAG marked with an empty marker?
110bool isDagEmpty (const DagInit* d) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000111 return GetOperatorName(d) == "empty_dag_marker";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000112}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000113
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000114// EscapeVariableName - Escape commas and other symbols not allowed
115// in the C++ variable names. Makes it possible to use options named
116// like "Wa," (useful for prefix options).
117std::string EscapeVariableName(const std::string& Var) {
118 std::string ret;
119 for (unsigned i = 0; i != Var.size(); ++i) {
120 char cur_char = Var[i];
121 if (cur_char == ',') {
122 ret += "_comma_";
123 }
124 else if (cur_char == '+') {
125 ret += "_plus_";
126 }
127 else if (cur_char == '-') {
128 ret += "_dash_";
129 }
130 else {
131 ret.push_back(cur_char);
132 }
133 }
134 return ret;
135}
136
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000137/// oneOf - Does the input string contain this character?
138bool oneOf(const char* lst, char c) {
139 while (*lst) {
140 if (*lst++ == c)
141 return true;
142 }
143 return false;
144}
145
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000146template <class I, class S>
147void checkedIncrement(I& P, I E, S ErrorString) {
148 ++P;
149 if (P == E)
150 throw ErrorString;
151}
152
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000153// apply is needed because C++'s syntax doesn't let us construct a function
154// object and call it in the same statement.
155template<typename F, typename T0>
156void apply(F Fun, T0& Arg0) {
157 return Fun(Arg0);
158}
159
160template<typename F, typename T0, typename T1>
161void apply(F Fun, T0& Arg0, T1& Arg1) {
162 return Fun(Arg0, Arg1);
163}
164
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000165//===----------------------------------------------------------------------===//
166/// Back-end specific code
167
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000168
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000169/// OptionType - One of six different option types. See the
170/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000171namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000172
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000173 enum OptionType { Alias, Switch, Parameter, ParameterList,
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000174 Prefix, PrefixList};
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000175
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000176 bool IsAlias(OptionType t) {
177 return (t == Alias);
178 }
179
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000180 bool IsList (OptionType t) {
181 return (t == ParameterList || t == PrefixList);
182 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000183
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000184 bool IsSwitch (OptionType t) {
185 return (t == Switch);
186 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000187
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000188 bool IsParameter (OptionType t) {
189 return (t == Parameter || t == Prefix);
190 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000191
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000192}
193
194OptionType::OptionType stringToOptionType(const std::string& T) {
195 if (T == "alias_option")
196 return OptionType::Alias;
197 else if (T == "switch_option")
198 return OptionType::Switch;
199 else if (T == "parameter_option")
200 return OptionType::Parameter;
201 else if (T == "parameter_list_option")
202 return OptionType::ParameterList;
203 else if (T == "prefix_option")
204 return OptionType::Prefix;
205 else if (T == "prefix_list_option")
206 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000207 else
208 throw "Unknown option type: " + T + '!';
209}
210
211namespace OptionDescriptionFlags {
212 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000213 ReallyHidden = 0x4, Extern = 0x8,
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000214 OneOrMore = 0x10, ZeroOrOne = 0x20,
215 CommaSeparated = 0x40 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000216}
217
218/// OptionDescription - Represents data contained in a single
219/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000220struct OptionDescription {
221 OptionType::OptionType Type;
222 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000223 unsigned Flags;
224 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000225 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000226 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000227
228 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000229 const std::string& n = "",
230 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000231 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000232 {}
233
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000234 /// GenTypeDeclaration - Returns the C++ variable type of this
235 /// option.
236 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000237
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000238 /// GenVariableName - Returns the variable name used in the
239 /// generated C++ code.
240 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000241
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000242 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000243 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000244
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000245 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000246
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000247 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000248
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000249 bool isMultiVal() const;
250
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000251 bool isCommaSeparated() const;
252 void setCommaSeparated();
253
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000254 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000255 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000256
257 bool isRequired() const;
258 void setRequired();
259
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000260 bool isOneOrMore() const;
261 void setOneOrMore();
262
263 bool isZeroOrOne() const;
264 void setZeroOrOne();
265
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000266 bool isHidden() const;
267 void setHidden();
268
269 bool isReallyHidden() const;
270 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000271
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000272 bool isSwitch() const
273 { return OptionType::IsSwitch(this->Type); }
274
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000275 bool isParameter() const
276 { return OptionType::IsParameter(this->Type); }
277
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000278 bool isList() const
279 { return OptionType::IsList(this->Type); }
280
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000281};
282
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000283void OptionDescription::Merge (const OptionDescription& other)
284{
285 if (other.Type != Type)
286 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000287
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000288 if (Help == other.Help || Help == DefaultHelpString)
289 Help = other.Help;
290 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000291 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000292 " defined for option " + Name + "\n";
293 }
294
295 Flags |= other.Flags;
296}
297
298bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000299 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000300}
301
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000302bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000303 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000304}
305
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000306bool OptionDescription::isCommaSeparated() const {
307 return Flags & OptionDescriptionFlags::CommaSeparated;
308}
309void OptionDescription::setCommaSeparated() {
310 Flags |= OptionDescriptionFlags::CommaSeparated;
311}
312
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000313bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000314 return Flags & OptionDescriptionFlags::Extern;
315}
316void OptionDescription::setExtern() {
317 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000318}
319
320bool OptionDescription::isRequired() const {
321 return Flags & OptionDescriptionFlags::Required;
322}
323void OptionDescription::setRequired() {
324 Flags |= OptionDescriptionFlags::Required;
325}
326
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000327bool OptionDescription::isOneOrMore() const {
328 return Flags & OptionDescriptionFlags::OneOrMore;
329}
330void OptionDescription::setOneOrMore() {
331 Flags |= OptionDescriptionFlags::OneOrMore;
332}
333
334bool OptionDescription::isZeroOrOne() const {
335 return Flags & OptionDescriptionFlags::ZeroOrOne;
336}
337void OptionDescription::setZeroOrOne() {
338 Flags |= OptionDescriptionFlags::ZeroOrOne;
339}
340
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000341bool OptionDescription::isHidden() const {
342 return Flags & OptionDescriptionFlags::Hidden;
343}
344void OptionDescription::setHidden() {
345 Flags |= OptionDescriptionFlags::Hidden;
346}
347
348bool OptionDescription::isReallyHidden() const {
349 return Flags & OptionDescriptionFlags::ReallyHidden;
350}
351void OptionDescription::setReallyHidden() {
352 Flags |= OptionDescriptionFlags::ReallyHidden;
353}
354
355const char* OptionDescription::GenTypeDeclaration() const {
356 switch (Type) {
357 case OptionType::Alias:
358 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000359 case OptionType::PrefixList:
360 case OptionType::ParameterList:
361 return "cl::list<std::string>";
362 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000363 return "cl::opt<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000364 case OptionType::Parameter:
365 case OptionType::Prefix:
366 default:
367 return "cl::opt<std::string>";
368 }
369}
370
371std::string OptionDescription::GenVariableName() const {
372 const std::string& EscapedName = EscapeVariableName(Name);
373 switch (Type) {
374 case OptionType::Alias:
375 return "AutoGeneratedAlias_" + EscapedName;
376 case OptionType::PrefixList:
377 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000378 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000379 case OptionType::Switch:
380 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000381 case OptionType::Prefix:
382 case OptionType::Parameter:
383 default:
384 return "AutoGeneratedParameter_" + EscapedName;
385 }
386}
387
388/// OptionDescriptions - An OptionDescription array plus some helper
389/// functions.
390class OptionDescriptions {
391 typedef StringMap<OptionDescription> container_type;
392
393 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000394 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000395
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000396public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000397 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000398 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000399
400 // Wrappers for FindOption that throw an exception in case the option has a
401 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000402 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000403 const OptionDescription& FindParameter(const std::string& OptName) const;
404 const OptionDescription& FindList(const std::string& OptName) const;
405 const OptionDescription&
406 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000407
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000408 /// insertDescription - Insert new OptionDescription into
409 /// OptionDescriptions list
410 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000411
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000412 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000413 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000414 const_iterator begin() const { return Descriptions.begin(); }
415 const_iterator end() const { return Descriptions.end(); }
416};
417
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000418const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000419OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000420 const_iterator I = Descriptions.find(OptName);
421 if (I != Descriptions.end())
422 return I->second;
423 else
424 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000425}
426
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000427const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000428OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000429 const OptionDescription& OptDesc = this->FindOption(OptName);
430 if (!OptDesc.isSwitch())
431 throw OptName + ": incorrect option type - should be a switch!";
432 return OptDesc;
433}
434
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000435const OptionDescription&
436OptionDescriptions::FindList(const std::string& OptName) const {
437 const OptionDescription& OptDesc = this->FindOption(OptName);
438 if (!OptDesc.isList())
439 throw OptName + ": incorrect option type - should be a list!";
440 return OptDesc;
441}
442
443const OptionDescription&
444OptionDescriptions::FindParameter(const std::string& OptName) const {
445 const OptionDescription& OptDesc = this->FindOption(OptName);
446 if (!OptDesc.isParameter())
447 throw OptName + ": incorrect option type - should be a parameter!";
448 return OptDesc;
449}
450
451const OptionDescription&
452OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
453 const OptionDescription& OptDesc = this->FindOption(OptName);
454 if (!OptDesc.isList() && !OptDesc.isParameter())
455 throw OptName
456 + ": incorrect option type - should be a list or parameter!";
457 return OptDesc;
458}
459
460void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000461 container_type::iterator I = Descriptions.find(o.Name);
462 if (I != Descriptions.end()) {
463 OptionDescription& D = I->second;
464 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000465 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000466 else {
467 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000468 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000469}
470
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000471/// HandlerTable - A base class for function objects implemented as
472/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000473template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000474class HandlerTable {
475protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000476 // Implementation details.
477
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000478 /// HandlerMap - A map from property names to property handlers
479 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000480
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000481 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000482 static bool staticMembersInitialized_;
483
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000484public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000485
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000486 Handler GetHandler (const std::string& HandlerName) const {
487 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000488
489 if (method != Handlers_.end()) {
490 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000491 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000492 }
493 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000494 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000495 }
496 }
497
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000498 void AddHandler(const char* Property, Handler H) {
499 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000500 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000501
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000502};
503
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000504template <class FunctionObject>
505void InvokeDagInitHandler(FunctionObject* Obj, Init* i) {
506 typedef void (FunctionObject::*Handler) (const DagInit*);
507
508 const DagInit& property = InitPtrToDag(i);
509 const std::string& property_name = GetOperatorName(property);
510 Handler h = Obj->GetHandler(property_name);
511
512 ((Obj)->*(h))(&property);
513}
514
515template <typename H>
516typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
517
518template <typename H>
519bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000520
521
522/// CollectOptionProperties - Function object for iterating over an
523/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000524class CollectOptionProperties;
525typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
526(const DagInit*);
527
528class CollectOptionProperties
529: public HandlerTable<CollectOptionPropertiesHandler>
530{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000531private:
532
533 /// optDescs_ - OptionDescriptions table. This is where the
534 /// information is stored.
535 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000536
537public:
538
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000539 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000540 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000541 {
542 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000543 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000544 AddHandler("help", &CollectOptionProperties::onHelp);
545 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000546 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000547 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
548 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000549 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
550 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000551 AddHandler("zero_or_one", &CollectOptionProperties::onZeroOrOne);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000552 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000553
554 staticMembersInitialized_ = true;
555 }
556 }
557
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000558 /// operator() - Just forwards to the corresponding property
559 /// handler.
560 void operator() (Init* i) {
561 InvokeDagInitHandler(this, i);
562 }
563
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000564private:
565
566 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000567 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000568
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000569 void onExtern (const DagInit* d) {
570 checkNumberOfArguments(d, 0);
571 optDesc_.setExtern();
572 }
573
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000574 void onHelp (const DagInit* d) {
575 checkNumberOfArguments(d, 1);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000576 optDesc_.Help = InitPtrToString(d->getArg(0));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000577 }
578
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000579 void onHidden (const DagInit* d) {
580 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000581 optDesc_.setHidden();
582 }
583
584 void onReallyHidden (const DagInit* d) {
585 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000586 optDesc_.setReallyHidden();
587 }
588
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000589 void onCommaSeparated (const DagInit* d) {
590 checkNumberOfArguments(d, 0);
591 if (!optDesc_.isList())
592 throw "'comma_separated' is valid only on list options!";
593 optDesc_.setCommaSeparated();
594 }
595
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000596 void onRequired (const DagInit* d) {
597 checkNumberOfArguments(d, 0);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000598 if (optDesc_.isOneOrMore() || optDesc_.isZeroOrOne())
599 throw "Only one of (required), (zero_or_one) or "
600 "(one_or_more) properties is allowed!";
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000601 optDesc_.setRequired();
602 }
603
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000604 void onInit (const DagInit* d) {
605 checkNumberOfArguments(d, 1);
606 Init* i = d->getArg(0);
607 const std::string& str = i->getAsString();
608
609 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
610 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
611
612 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000613 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000614
615 optDesc_.InitVal = i;
616 }
617
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000618 void onOneOrMore (const DagInit* d) {
619 checkNumberOfArguments(d, 0);
620 if (optDesc_.isRequired() || optDesc_.isZeroOrOne())
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000621 throw "Only one of (required), (zero_or_one) or "
622 "(one_or_more) properties is allowed!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000623 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000624 llvm::errs() << "Warning: specifying the 'one_or_more' property "
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000625 "on a non-list option will have no effect.\n";
626 optDesc_.setOneOrMore();
627 }
628
629 void onZeroOrOne (const DagInit* d) {
630 checkNumberOfArguments(d, 0);
631 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000632 throw "Only one of (required), (zero_or_one) or "
633 "(one_or_more) properties is allowed!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000634 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000635 llvm::errs() << "Warning: specifying the 'zero_or_one' property"
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000636 "on a non-list option will have no effect.\n";
637 optDesc_.setZeroOrOne();
638 }
639
640 void onMultiVal (const DagInit* d) {
641 checkNumberOfArguments(d, 1);
642 int val = InitPtrToInt(d->getArg(0));
643 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000644 throw "Error in the 'multi_val' property: "
645 "the value must be greater than 1!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000646 if (!OptionType::IsList(optDesc_.Type))
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000647 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000648 optDesc_.MultiVal = val;
649 }
650
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000651};
652
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000653/// AddOption - A function object that is applied to every option
654/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000655class AddOption {
656private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000657 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000658
659public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000660 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000661 {}
662
663 void operator()(const Init* i) {
664 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000665 checkNumberOfArguments(&d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000666
667 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000668 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000669 const std::string& Name = InitPtrToString(d.getArg(0));
670
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000671 OptionDescription OD(Type, Name);
672
673 if (!OD.isExtern())
674 checkNumberOfArguments(&d, 2);
675
676 if (OD.isAlias()) {
677 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000678 OD.Help = InitPtrToString(d.getArg(1));
679 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000680 else if (!OD.isExtern()) {
681 processOptionProperties(&d, OD);
682 }
683 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000684 }
685
686private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000687 /// processOptionProperties - Go through the list of option
688 /// properties and call a corresponding handler for each.
689 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
690 checkNumberOfArguments(d, 2);
691 DagInit::const_arg_iterator B = d->arg_begin();
692 // Skip the first argument: it's always the option name.
693 ++B;
694 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000695 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000696
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000697};
698
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000699/// CollectOptionDescriptions - Collects option properties from all
700/// OptionLists.
701void CollectOptionDescriptions (RecordVector::const_iterator B,
702 RecordVector::const_iterator E,
703 OptionDescriptions& OptDescs)
704{
705 // For every OptionList:
706 for (; B!=E; ++B) {
707 RecordVector::value_type T = *B;
708 // Throws an exception if the value does not exist.
709 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000710
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000711 // For every option description in this list:
712 // collect the information and
713 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
714 }
715}
716
717// Tool information record
718
719namespace ToolFlags {
720 enum ToolFlags { Join = 0x1, Sink = 0x2 };
721}
722
723struct ToolDescription : public RefCountedBase<ToolDescription> {
724 std::string Name;
725 Init* CmdLine;
726 Init* Actions;
727 StrVector InLanguage;
728 std::string OutLanguage;
729 std::string OutputSuffix;
730 unsigned Flags;
731
732 // Various boolean properties
733 void setSink() { Flags |= ToolFlags::Sink; }
734 bool isSink() const { return Flags & ToolFlags::Sink; }
735 void setJoin() { Flags |= ToolFlags::Join; }
736 bool isJoin() const { return Flags & ToolFlags::Join; }
737
738 // Default ctor here is needed because StringMap can only store
739 // DefaultConstructible objects
740 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
741 ToolDescription (const std::string& n)
742 : Name(n), CmdLine(0), Actions(0), Flags(0)
743 {}
744};
745
746/// ToolDescriptions - A list of Tool information records.
747typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
748
749
750/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000751/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000752
753class CollectToolProperties;
754typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
755(const DagInit*);
756
757class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
758{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000759private:
760
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000761 /// toolDesc_ - Properties of the current Tool. This is where the
762 /// information is stored.
763 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000764
765public:
766
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000767 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000768 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000769 {
770 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000771
772 AddHandler("actions", &CollectToolProperties::onActions);
773 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
774 AddHandler("in_language", &CollectToolProperties::onInLanguage);
775 AddHandler("join", &CollectToolProperties::onJoin);
776 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
777 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
778 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000779
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000780 staticMembersInitialized_ = true;
781 }
782 }
783
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000784 void operator() (Init* i) {
785 InvokeDagInitHandler(this, i);
786 }
787
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000788private:
789
790 /// Property handlers --
791 /// Functions that extract information about tool properties from
792 /// DAG representation.
793
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000794 void onActions (const DagInit* d) {
795 checkNumberOfArguments(d, 1);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000796 Init* Case = d->getArg(0);
797 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000798 GetOperatorName(static_cast<DagInit*>(Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000799 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000800 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000801 }
802
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000803 void onCmdLine (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000804 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000805 toolDesc_.CmdLine = d->getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000806 }
807
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000808 void onInLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000809 checkNumberOfArguments(d, 1);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000810 Init* arg = d->getArg(0);
811
812 // Find out the argument's type.
813 if (typeid(*arg) == typeid(StringInit)) {
814 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000815 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000816 }
817 else {
818 // It's a list.
819 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000820 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000821
822 // Copy strings to the output vector.
823 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
824 B != E; ++B) {
825 out.push_back(InitPtrToString(*B));
826 }
827
828 // Remove duplicates.
829 std::sort(out.begin(), out.end());
830 StrVector::iterator newE = std::unique(out.begin(), out.end());
831 out.erase(newE, out.end());
832 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000833 }
834
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000835 void onJoin (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000836 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000837 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000838 }
839
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000840 void onOutLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000841 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000842 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000843 }
844
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000845 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000846 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000847 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000848 }
849
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000850 void onSink (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000851 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000852 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000853 }
854
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000855};
856
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000857/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000858/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000859/// CollectToolProperties function object).
860void CollectToolDescriptions (RecordVector::const_iterator B,
861 RecordVector::const_iterator E,
862 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000863{
864 // Iterate over a properties list of every Tool definition
865 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000866 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000867 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000868 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000869
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000870 IntrusiveRefCntPtr<ToolDescription>
871 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000872
873 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000874 CollectToolProperties(*ToolDesc));
875 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000876 }
877}
878
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000879/// FillInEdgeVector - Merge all compilation graph definitions into
880/// one single edge list.
881void FillInEdgeVector(RecordVector::const_iterator B,
882 RecordVector::const_iterator E, RecordVector& Out) {
883 for (; B != E; ++B) {
884 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000885
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000886 for (unsigned i = 0; i < edges->size(); ++i)
887 Out.push_back(edges->getElementAsRecord(i));
888 }
889}
890
891/// CalculatePriority - Calculate the priority of this plugin.
892int CalculatePriority(RecordVector::const_iterator B,
893 RecordVector::const_iterator E) {
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000894 int priority = 0;
895
896 if (B != E) {
897 priority = static_cast<int>((*B)->getValueAsInt("priority"));
898
899 if (++B != E)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000900 throw "More than one 'PluginPriority' instance found: "
901 "most probably an error!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000902 }
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000903
904 return priority;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000905}
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000906
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000907/// NotInGraph - Helper function object for FilterNotInGraph.
908struct NotInGraph {
909private:
910 const llvm::StringSet<>& ToolsInGraph_;
911
912public:
913 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
914 : ToolsInGraph_(ToolsInGraph)
915 {}
916
917 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
918 return (ToolsInGraph_.count(x->Name) == 0);
919 }
920};
921
922/// FilterNotInGraph - Filter out from ToolDescs all Tools not
923/// mentioned in the compilation graph definition.
924void FilterNotInGraph (const RecordVector& EdgeVector,
925 ToolDescriptions& ToolDescs) {
926
927 // List all tools mentioned in the graph.
928 llvm::StringSet<> ToolsInGraph;
929
930 for (RecordVector::const_iterator B = EdgeVector.begin(),
931 E = EdgeVector.end(); B != E; ++B) {
932
933 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000934 const std::string& NodeA = Edge->getValueAsString("a");
935 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000936
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000937 if (NodeA != "root")
938 ToolsInGraph.insert(NodeA);
939 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000940 }
941
942 // Filter ToolPropertiesList.
943 ToolDescriptions::iterator new_end =
944 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
945 NotInGraph(ToolsInGraph));
946 ToolDescs.erase(new_end, ToolDescs.end());
947}
948
949/// FillInToolToLang - Fills in two tables that map tool names to
950/// (input, output) languages. Helper function used by TypecheckGraph().
951void FillInToolToLang (const ToolDescriptions& ToolDescs,
952 StringMap<StringSet<> >& ToolToInLang,
953 StringMap<std::string>& ToolToOutLang) {
954 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
955 E = ToolDescs.end(); B != E; ++B) {
956 const ToolDescription& D = *(*B);
957 for (StrVector::const_iterator B = D.InLanguage.begin(),
958 E = D.InLanguage.end(); B != E; ++B)
959 ToolToInLang[D.Name].insert(*B);
960 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000961 }
962}
963
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000964/// TypecheckGraph - Check that names for output and input languages
965/// on all edges do match. This doesn't do much when the information
966/// about the whole graph is not available (i.e. when compiling most
967/// plugins).
968void TypecheckGraph (const RecordVector& EdgeVector,
969 const ToolDescriptions& ToolDescs) {
970 StringMap<StringSet<> > ToolToInLang;
971 StringMap<std::string> ToolToOutLang;
972
973 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
974 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
975 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
976
977 for (RecordVector::const_iterator B = EdgeVector.begin(),
978 E = EdgeVector.end(); B != E; ++B) {
979 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000980 const std::string& NodeA = Edge->getValueAsString("a");
981 const std::string& NodeB = Edge->getValueAsString("b");
982 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
983 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000984
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000985 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000986 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000987 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000988 + ": output->input language mismatch";
989 }
990
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000991 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000992 throw "Edges back to the root are not allowed!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000993 }
994}
995
996/// WalkCase - Walks the 'case' expression DAG and invokes
997/// TestCallback on every test, and StatementCallback on every
998/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000999/// combinators (that is, they are passed directly to TestCallback).
1000/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1001/// IndentLevel, bool FirstTest)'.
1002/// StatementCallback must have type 'void StatementCallback(const Init*,
1003/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001004template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001005void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1006 unsigned IndentLevel = 0)
1007{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001008 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001009
1010 // Error checks.
1011 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001012 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001013
1014 if (d.getNumArgs() < 2)
1015 throw "There should be at least one clause in the 'case' expression:\n"
1016 + d.getAsString();
1017
1018 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001019 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001020 const unsigned numArgs = d.getNumArgs();
1021 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001022 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1023 B != E; ++B) {
1024 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001025
1026 if (!even)
1027 {
1028 // Handle test.
1029 const DagInit& Test = InitPtrToDag(arg);
1030
1031 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001032 throw "The 'default' clause should be the last in the "
1033 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001034 if (i == numArgs)
1035 throw "Case construct handler: no corresponding action "
1036 "found for the test " + Test.getAsString() + '!';
1037
1038 TestCallback(&Test, IndentLevel, (i == 1));
1039 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001040 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001041 {
1042 if (dynamic_cast<DagInit*>(arg)
1043 && GetOperatorName(static_cast<DagInit*>(arg)) == "case") {
1044 // Nested 'case'.
1045 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1046 }
1047
1048 // Handle statement.
1049 StatementCallback(arg, IndentLevel);
1050 }
1051
1052 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001053 even = !even;
1054 }
1055}
1056
1057/// ExtractOptionNames - A helper function object used by
1058/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1059class ExtractOptionNames {
1060 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001061
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001062 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001063 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001064 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001065 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001066 ActionName == "forward_value" ||
1067 ActionName == "forward_transformed_value" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001068 ActionName == "switch_on" || ActionName == "parameter_equals" ||
1069 ActionName == "element_in_list" || ActionName == "not_empty" ||
1070 ActionName == "empty") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001071 checkNumberOfArguments(&Stmt, 1);
1072 const std::string& Name = InitPtrToString(Stmt.getArg(0));
1073 OptionNames_.insert(Name);
1074 }
1075 else if (ActionName == "and" || ActionName == "or") {
1076 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001077 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001078 }
1079 }
1080 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001081
1082public:
1083 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1084 {}
1085
1086 void operator()(const Init* Statement) {
1087 if (typeid(*Statement) == typeid(ListInit)) {
1088 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1089 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1090 B != E; ++B)
1091 this->processDag(*B);
1092 }
1093 else {
1094 this->processDag(Statement);
1095 }
1096 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001097
1098 void operator()(const DagInit* Test, unsigned, bool) {
1099 this->operator()(Test);
1100 }
1101 void operator()(const Init* Statement, unsigned) {
1102 this->operator()(Statement);
1103 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001104};
1105
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001106/// CheckForSuperfluousOptions - Check that there are no side
1107/// effect-free options (specified only in the OptionList). Otherwise,
1108/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001109void CheckForSuperfluousOptions (const RecordVector& Edges,
1110 const ToolDescriptions& ToolDescs,
1111 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001112 llvm::StringSet<> nonSuperfluousOptions;
1113
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001114 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001115 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001116 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1117 E = ToolDescs.end(); B != E; ++B) {
1118 const ToolDescription& TD = *(*B);
1119 ExtractOptionNames Callback(nonSuperfluousOptions);
1120 if (TD.Actions)
1121 WalkCase(TD.Actions, Callback, Callback);
1122 }
1123
1124 // Add all options mentioned in the 'case' clauses of the
1125 // OptionalEdges of the compilation graph to the set of
1126 // non-superfluous options.
1127 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1128 B != E; ++B) {
1129 const Record* Edge = *B;
1130 DagInit* Weight = Edge->getValueAsDag("weight");
1131
1132 if (!isDagEmpty(Weight))
1133 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001134 }
1135
1136 // Check that all options in OptDescs belong to the set of
1137 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001138 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001139 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001140 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001141 if (!nonSuperfluousOptions.count(Val.Name)
1142 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001143 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001144 "Probable cause: this option is specified only in the OptionList.\n";
1145 }
1146}
1147
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001148/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1149bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1150 if (TestName == "single_input_file") {
1151 O << "InputFilenames.size() == 1";
1152 return true;
1153 }
1154 else if (TestName == "multiple_input_files") {
1155 O << "InputFilenames.size() > 1";
1156 return true;
1157 }
1158
1159 return false;
1160}
1161
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001162/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1163template <typename F>
1164void EmitListTest(const ListInit& L, const char* LogicOp,
1165 F Callback, raw_ostream& O)
1166{
1167 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1168 // of Dags...
1169 bool isFirst = true;
1170 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1171 if (isFirst)
1172 isFirst = false;
1173 else
1174 O << " || ";
1175 Callback(InitPtrToString(*B), O);
1176 }
1177}
1178
1179// Callbacks for use with EmitListTest.
1180
1181class EmitSwitchOn {
1182 const OptionDescriptions& OptDescs_;
1183public:
1184 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1185 {}
1186
1187 void operator()(const std::string& OptName, raw_ostream& O) const {
1188 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1189 O << OptDesc.GenVariableName();
1190 }
1191};
1192
1193class EmitEmptyTest {
1194 bool EmitNegate_;
1195 const OptionDescriptions& OptDescs_;
1196public:
1197 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1198 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1199 {}
1200
1201 void operator()(const std::string& OptName, raw_ostream& O) const {
1202 const char* Neg = (EmitNegate_ ? "!" : "");
1203 if (OptName == "o") {
1204 O << Neg << "OutputFilename.empty()";
1205 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001206 else if (OptName == "save-temps") {
1207 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1208 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001209 else {
1210 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1211 O << Neg << OptDesc.GenVariableName() << ".empty()";
1212 }
1213 }
1214};
1215
1216
1217/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1218bool EmitCaseTest1ArgList(const std::string& TestName,
1219 const DagInit& d,
1220 const OptionDescriptions& OptDescs,
1221 raw_ostream& O) {
1222 const ListInit& L = *static_cast<ListInit*>(d.getArg(0));
1223
1224 if (TestName == "any_switch_on") {
1225 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1226 return true;
1227 }
1228 else if (TestName == "switch_on") {
1229 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1230 return true;
1231 }
1232 else if (TestName == "any_not_empty") {
1233 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1234 return true;
1235 }
1236 else if (TestName == "any_empty") {
1237 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1238 return true;
1239 }
1240 else if (TestName == "not_empty") {
1241 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1242 return true;
1243 }
1244 else if (TestName == "empty") {
1245 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1246 return true;
1247 }
1248
1249 return false;
1250}
1251
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001252/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1253bool EmitCaseTest1ArgStr(const std::string& TestName,
1254 const DagInit& d,
1255 const OptionDescriptions& OptDescs,
1256 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001257 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001258
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001259 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001260 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001261 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001262 }
1263 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001264 O << "InLangs.count(\"" << OptName << "\") != 0";
1265 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001266 }
1267 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001268 // This works only for single-argument Tool::GenerateAction. Join
1269 // tools can process several files in different languages simultaneously.
1270
1271 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001272 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001273 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001274 }
1275 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001276 bool EmitNegate = (TestName == "not_empty");
1277 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001278 return true;
1279 }
1280
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001281 return false;
1282}
1283
1284/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1285bool EmitCaseTest1Arg(const std::string& TestName,
1286 const DagInit& d,
1287 const OptionDescriptions& OptDescs,
1288 raw_ostream& O) {
1289 checkNumberOfArguments(&d, 1);
1290 if (typeid(*d.getArg(0)) == typeid(ListInit))
1291 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1292 else
1293 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1294}
1295
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001296/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001297bool EmitCaseTest2Args(const std::string& TestName,
1298 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001299 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001300 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001301 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001302 checkNumberOfArguments(&d, 2);
1303 const std::string& OptName = InitPtrToString(d.getArg(0));
1304 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001305
1306 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001307 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001308 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1309 return true;
1310 }
1311 else if (TestName == "element_in_list") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001312 const OptionDescription& OptDesc = OptDescs.FindList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001313 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001314 O << "std::find(" << VarName << ".begin(),\n";
1315 O.indent(IndentLevel + Indent1)
1316 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001317 << OptArg << "\") != " << VarName << ".end()";
1318 return true;
1319 }
1320
1321 return false;
1322}
1323
1324// Forward declaration.
1325// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001326void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001327 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001328 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001329
1330/// EmitLogicalOperationTest - Helper function used by
1331/// EmitCaseConstructHandler.
1332void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001333 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001334 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001335 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001336 O << '(';
1337 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001338 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001339 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001340 if (j != NumArgs - 1) {
1341 O << ")\n";
1342 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1343 }
1344 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001345 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001346 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001347 }
1348}
1349
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001350void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001351 const OptionDescriptions& OptDescs, raw_ostream& O)
1352{
1353 checkNumberOfArguments(&d, 1);
1354 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1355 O << "! (";
1356 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1357 O << ")";
1358}
1359
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001360/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001361void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001362 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001363 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001364 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001365
1366 if (TestName == "and")
1367 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1368 else if (TestName == "or")
1369 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001370 else if (TestName == "not")
1371 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001372 else if (EmitCaseTest0Args(TestName, O))
1373 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001374 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1375 return;
1376 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1377 return;
1378 else
1379 throw TestName + ": unknown edge property!";
1380}
1381
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001382
1383/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1384class EmitCaseTestCallback {
1385 bool EmitElseIf_;
1386 const OptionDescriptions& OptDescs_;
1387 raw_ostream& O_;
1388public:
1389
1390 EmitCaseTestCallback(bool EmitElseIf,
1391 const OptionDescriptions& OptDescs, raw_ostream& O)
1392 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1393 {}
1394
1395 void operator()(const DagInit* Test, unsigned IndentLevel, bool FirstTest)
1396 {
1397 if (GetOperatorName(Test) == "default") {
1398 O_.indent(IndentLevel) << "else {\n";
1399 }
1400 else {
1401 O_.indent(IndentLevel)
1402 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
1403 EmitCaseTest(*Test, IndentLevel, OptDescs_, O_);
1404 O_ << ") {\n";
1405 }
1406 }
1407};
1408
1409/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1410template <typename F>
1411class EmitCaseStatementCallback {
1412 F Callback_;
1413 raw_ostream& O_;
1414public:
1415
1416 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1417 : Callback_(Callback), O_(O)
1418 {}
1419
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001420 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001421
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001422 // Ignore nested 'case' DAG.
1423 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001424 GetOperatorName(static_cast<const DagInit*>(Statement)) == "case")) {
1425 if (typeid(*Statement) == typeid(ListInit)) {
1426 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1427 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1428 B != E; ++B)
1429 Callback_(*B, (IndentLevel + Indent1), O_);
1430 }
1431 else {
1432 Callback_(Statement, (IndentLevel + Indent1), O_);
1433 }
1434 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001435 O_.indent(IndentLevel) << "}\n";
1436 }
1437
1438};
1439
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001440/// EmitCaseConstructHandler - Emit code that handles the 'case'
1441/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001442/// clause. Implemented on top of WalkCase.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001443/// Callback's type is void F(Init* Statement, unsigned IndentLevel,
1444/// raw_ostream& O).
1445/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001446/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1447/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001448template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001449void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001450 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001451 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001452 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001453 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1454 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001455}
1456
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001457/// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to
1458/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path"] .
1459/// Helper function used by EmitCmdLineVecFill and.
1460void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
1461 const char* Delimiters = " \t\n\v\f\r";
1462 enum TokenizerState
1463 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1464 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001465
1466 if (CmdLine.empty())
1467 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001468 Out.push_back("");
1469
1470 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1471 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001472
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001473 for (; B != E; ++B) {
1474 char cur_ch = CmdLine[B];
1475
1476 switch (cur_st) {
1477 case Normal:
1478 if (cur_ch == '$') {
1479 cur_st = SpecialCommand;
1480 break;
1481 }
1482 if (oneOf(Delimiters, cur_ch)) {
1483 // Skip whitespace
1484 B = CmdLine.find_first_not_of(Delimiters, B);
1485 if (B == std::string::npos) {
1486 B = E-1;
1487 continue;
1488 }
1489 --B;
1490 Out.push_back("");
1491 continue;
1492 }
1493 break;
1494
1495
1496 case SpecialCommand:
1497 if (oneOf(Delimiters, cur_ch)) {
1498 cur_st = Normal;
1499 Out.push_back("");
1500 continue;
1501 }
1502 if (cur_ch == '(') {
1503 Out.push_back("");
1504 cur_st = InsideSpecialCommand;
1505 continue;
1506 }
1507 break;
1508
1509 case InsideSpecialCommand:
1510 if (oneOf(Delimiters, cur_ch)) {
1511 continue;
1512 }
1513 if (cur_ch == '\'') {
1514 cur_st = InsideQuotationMarks;
1515 Out.push_back("");
1516 continue;
1517 }
1518 if (cur_ch == ')') {
1519 cur_st = Normal;
1520 Out.push_back("");
1521 }
1522 if (cur_ch == ',') {
1523 continue;
1524 }
1525
1526 break;
1527
1528 case InsideQuotationMarks:
1529 if (cur_ch == '\'') {
1530 cur_st = InsideSpecialCommand;
1531 continue;
1532 }
1533 break;
1534 }
1535
1536 Out.back().push_back(cur_ch);
1537 }
1538}
1539
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001540/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1541/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1542/// SubstituteSpecialCommands().
1543StrVector::const_iterator
1544SubstituteCall (StrVector::const_iterator Pos,
1545 StrVector::const_iterator End,
1546 bool IsJoin, raw_ostream& O)
1547{
1548 const char* errorMessage = "Syntax error in $CALL invocation!";
1549 checkedIncrement(Pos, End, errorMessage);
1550 const std::string& CmdName = *Pos;
1551
1552 if (CmdName == ")")
1553 throw "$CALL invocation: empty argument list!";
1554
1555 O << "hooks::";
1556 O << CmdName << "(";
1557
1558
1559 bool firstIteration = true;
1560 while (true) {
1561 checkedIncrement(Pos, End, errorMessage);
1562 const std::string& Arg = *Pos;
1563 assert(Arg.size() != 0);
1564
1565 if (Arg[0] == ')')
1566 break;
1567
1568 if (firstIteration)
1569 firstIteration = false;
1570 else
1571 O << ", ";
1572
1573 if (Arg == "$INFILE") {
1574 if (IsJoin)
1575 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1576 else
1577 O << "inFile.c_str()";
1578 }
1579 else {
1580 O << '"' << Arg << '"';
1581 }
1582 }
1583
1584 O << ')';
1585
1586 return Pos;
1587}
1588
1589/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1590/// function used by SubstituteSpecialCommands().
1591StrVector::const_iterator
1592SubstituteEnv (StrVector::const_iterator Pos,
1593 StrVector::const_iterator End, raw_ostream& O)
1594{
1595 const char* errorMessage = "Syntax error in $ENV invocation!";
1596 checkedIncrement(Pos, End, errorMessage);
1597 const std::string& EnvName = *Pos;
1598
1599 if (EnvName == ")")
1600 throw "$ENV invocation: empty argument list!";
1601
1602 O << "checkCString(std::getenv(\"";
1603 O << EnvName;
1604 O << "\"))";
1605
1606 checkedIncrement(Pos, End, errorMessage);
1607
1608 return Pos;
1609}
1610
1611/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1612/// handler code. Helper function used by EmitCmdLineVecFill().
1613StrVector::const_iterator
1614SubstituteSpecialCommands (StrVector::const_iterator Pos,
1615 StrVector::const_iterator End,
1616 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001617{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001618
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001619 const std::string& cmd = *Pos;
1620
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001621 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001622 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001623 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001624 }
1625 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001626 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001627 }
1628 else {
1629 throw "Unknown special command: " + cmd;
1630 }
1631
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001632 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001633 const std::string& Leftover = *Pos;
1634 assert(Leftover.at(0) == ')');
1635 if (Leftover.size() != 1)
1636 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001637
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001638 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001639}
1640
1641/// EmitCmdLineVecFill - Emit code that fills in the command line
1642/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001643void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001644 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001645 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001646 StrVector StrVec;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001647 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1648
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001649 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001650 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001651
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001652 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1653
1654 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001655 assert(!StrVec[0].empty());
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001656 if (StrVec[0][0] == '$') {
1657 while (I != E && (*I)[0] != ')' )
1658 ++I;
1659
1660 // Skip the ')' symbol.
1661 ++I;
1662 }
1663 else {
1664 ++I;
1665 }
1666
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001667 bool hasINFILE = false;
1668
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001669 for (; I != E; ++I) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001670 const std::string& cmd = *I;
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001671 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001672 O.indent(IndentLevel);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001673 if (cmd.at(0) == '$') {
1674 if (cmd == "$INFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001675 hasINFILE = true;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001676 if (IsJoin) {
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001677 O << "for (PathVector::const_iterator B = inFiles.begin()"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001678 << ", E = inFiles.end();\n";
1679 O.indent(IndentLevel) << "B != E; ++B)\n";
1680 O.indent(IndentLevel + Indent1) << "vec.push_back(B->str());\n";
1681 }
1682 else {
Chris Lattner74382b72009-08-23 22:45:37 +00001683 O << "vec.push_back(inFile.str());\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001684 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001685 }
1686 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001687 O << "vec.push_back(\"\");\n";
1688 O.indent(IndentLevel) << "out_file_index = vec.size()-1;\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001689 }
1690 else {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001691 O << "vec.push_back(";
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001692 I = SubstituteSpecialCommands(I, E, IsJoin, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001693 O << ");\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001694 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001695 }
1696 else {
1697 O << "vec.push_back(\"" << cmd << "\");\n";
1698 }
1699 }
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001700 if (!hasINFILE)
1701 throw "Tool '" + ToolName + "' doesn't take any input!";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001702
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001703 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001704 if (StrVec[0][0] == '$')
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001705 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001706 else
1707 O << '"' << StrVec[0] << '"';
1708 O << ";\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001709}
1710
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001711/// EmitCmdLineVecFillCallback - A function object wrapper around
1712/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1713/// argument to EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001714class EmitCmdLineVecFillCallback {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001715 bool IsJoin;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001716 const std::string& ToolName;
1717 public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001718 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1719 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001720
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001721 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001722 raw_ostream& O) const
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001723 {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001724 EmitCmdLineVecFill(Statement, ToolName, IsJoin, IndentLevel, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001725 }
1726};
1727
1728/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1729/// implement EmitActionHandler. Emits code for
1730/// handling the (forward) and (forward_as) option properties.
1731void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001732 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001733 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001734 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001735 const std::string& Name = NewName.empty()
1736 ? ("-" + D.Name)
1737 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001738 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001739
1740 switch (D.Type) {
1741 case OptionType::Switch:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001742 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001743 break;
1744 case OptionType::Parameter:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001745 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
1746 O.indent(IndentLevel) << "vec.push_back(" << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001747 break;
1748 case OptionType::Prefix:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001749 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\" + "
1750 << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001751 break;
1752 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001753 O.indent(IndentLevel)
1754 << "for (" << D.GenTypeDeclaration()
1755 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1756 O.indent(IndentLevel)
1757 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1758 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\" + " << "*B);\n";
1759 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001760
1761 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001762 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1763 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001764 }
1765
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001766 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001767 break;
1768 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001769 O.indent(IndentLevel)
1770 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1771 << D.GenVariableName() << ".begin(),\n";
1772 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1773 << ".end() ; B != E;) {\n";
1774 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001775
1776 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001777 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1778 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001779 }
1780
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001781 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001782 break;
1783 case OptionType::Alias:
1784 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001785 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001786 }
1787}
1788
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001789/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1790/// EmitPreprocessOptionsCallback.
1791struct ActionHandlingCallbackBase {
1792
1793 void onErrorDag(const DagInit& d,
1794 unsigned IndentLevel, raw_ostream& O) const
1795 {
1796 O.indent(IndentLevel)
1797 << "throw std::runtime_error(\"" <<
1798 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1799 : "Unknown error!")
1800 << "\");\n";
1801 }
1802
1803 void onWarningDag(const DagInit& d,
1804 unsigned IndentLevel, raw_ostream& O) const
1805 {
1806 checkNumberOfArguments(&d, 1);
1807 O.indent(IndentLevel) << "llvm::errs() << \""
1808 << InitPtrToString(d.getArg(0)) << "\";\n";
1809 }
1810
1811};
1812
1813/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1814/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001815class EmitActionHandlersCallback;
1816typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1817(const DagInit&, unsigned, raw_ostream&) const;
1818
1819class EmitActionHandlersCallback
1820: public ActionHandlingCallbackBase,
1821 public HandlerTable<EmitActionHandlersCallbackHandler>
1822{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001823 const OptionDescriptions& OptDescs;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001824 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001825
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001826 void onAppendCmd (const DagInit& Dag,
1827 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001828 {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001829 checkNumberOfArguments(&Dag, 1);
1830 const std::string& Cmd = InitPtrToString(Dag.getArg(0));
1831 StrVector Out;
1832 llvm::SplitString(Cmd, Out);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001833
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001834 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1835 B != E; ++B)
1836 O.indent(IndentLevel) << "vec.push_back(\"" << *B << "\");\n";
1837 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001838
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001839 void onForward (const DagInit& Dag,
1840 unsigned IndentLevel, raw_ostream& O) const
1841 {
1842 checkNumberOfArguments(&Dag, 1);
1843 const std::string& Name = InitPtrToString(Dag.getArg(0));
1844 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1845 IndentLevel, "", O);
1846 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001847
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001848 void onForwardAs (const DagInit& Dag,
1849 unsigned IndentLevel, raw_ostream& O) const
1850 {
1851 checkNumberOfArguments(&Dag, 2);
1852 const std::string& Name = InitPtrToString(Dag.getArg(0));
1853 const std::string& NewName = InitPtrToString(Dag.getArg(1));
1854 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1855 IndentLevel, NewName, O);
1856 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001857
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001858 void onForwardValue (const DagInit& Dag,
1859 unsigned IndentLevel, raw_ostream& O) const
1860 {
1861 checkNumberOfArguments(&Dag, 1);
1862 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001863 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001864
1865 if (D.isParameter()) {
1866 O.indent(IndentLevel) << "vec.push_back("
1867 << D.GenVariableName() << ");\n";
1868 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001869 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001870 O.indent(IndentLevel) << "std::copy(" << D.GenVariableName()
1871 << ".begin(), " << D.GenVariableName()
1872 << ".end(), std::back_inserter(vec));\n";
1873 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001874 }
1875
1876 void onForwardTransformedValue (const DagInit& Dag,
1877 unsigned IndentLevel, raw_ostream& O) const
1878 {
1879 checkNumberOfArguments(&Dag, 2);
1880 const std::string& Name = InitPtrToString(Dag.getArg(0));
1881 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001882 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001883
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001884 O.indent(IndentLevel) << "vec.push_back(" << "hooks::"
1885 << Hook << "(" << D.GenVariableName() << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001886 }
1887
1888
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001889 void onOutputSuffix (const DagInit& Dag,
1890 unsigned IndentLevel, raw_ostream& O) const
1891 {
1892 checkNumberOfArguments(&Dag, 1);
1893 const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
1894 O.indent(IndentLevel) << "output_suffix = \"" << OutSuf << "\";\n";
1895 }
1896
1897 void onStopCompilation (const DagInit& Dag,
1898 unsigned IndentLevel, raw_ostream& O) const
1899 {
1900 O.indent(IndentLevel) << "stop_compilation = true;\n";
1901 }
1902
1903
1904 void onUnpackValues (const DagInit& Dag,
1905 unsigned IndentLevel, raw_ostream& O) const
1906 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001907 throw "'unpack_values' is deprecated. "
1908 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001909 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001910
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001911 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001912
1913 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
1914 : OptDescs(OD)
1915 {
1916 if (!staticMembersInitialized_) {
1917 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
1918 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
1919 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
1920 AddHandler("forward", &EmitActionHandlersCallback::onForward);
1921 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001922 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
1923 AddHandler("forward_transformed_value",
1924 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001925 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
1926 AddHandler("stop_compilation",
1927 &EmitActionHandlersCallback::onStopCompilation);
1928 AddHandler("unpack_values",
1929 &EmitActionHandlersCallback::onUnpackValues);
1930
1931 staticMembersInitialized_ = true;
1932 }
1933 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001934
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001935 void operator()(const Init* Statement,
1936 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001937 {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001938 const DagInit& Dag = InitPtrToDag(Statement);
1939 const std::string& ActionName = GetOperatorName(Dag);
1940 Handler h = GetHandler(ActionName);
1941
1942 ((this)->*(h))(Dag, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001943 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001944};
1945
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001946bool IsOutFileIndexCheckRequiredStr (const Init* CmdLine) {
1947 StrVector StrVec;
1948 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1949
1950 for (StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1951 I != E; ++I) {
1952 if (*I == "$OUTFILE")
1953 return false;
1954 }
1955
1956 return true;
1957}
1958
1959class IsOutFileIndexCheckRequiredStrCallback {
1960 bool* ret_;
1961
1962public:
1963 IsOutFileIndexCheckRequiredStrCallback(bool* ret) : ret_(ret)
1964 {}
1965
1966 void operator()(const Init* CmdLine) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001967 // Ignore nested 'case' DAG.
1968 if (typeid(*CmdLine) == typeid(DagInit))
1969 return;
1970
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001971 if (IsOutFileIndexCheckRequiredStr(CmdLine))
1972 *ret_ = true;
1973 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001974
1975 void operator()(const DagInit* Test, unsigned, bool) {
1976 this->operator()(Test);
1977 }
1978 void operator()(const Init* Statement, unsigned) {
1979 this->operator()(Statement);
1980 }
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001981};
1982
1983bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
1984 bool ret = false;
1985 WalkCase(CmdLine, Id(), IsOutFileIndexCheckRequiredStrCallback(&ret));
1986 return ret;
1987}
1988
1989/// IsOutFileIndexCheckRequired - Should we emit an "out_file_index != -1" check
1990/// in EmitGenerateActionMethod() ?
1991bool IsOutFileIndexCheckRequired (Init* CmdLine) {
1992 if (typeid(*CmdLine) == typeid(StringInit))
1993 return IsOutFileIndexCheckRequiredStr(CmdLine);
1994 else
1995 return IsOutFileIndexCheckRequiredCase(CmdLine);
1996}
1997
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001998void EmitGenerateActionMethodHeader(const ToolDescription& D,
1999 bool IsJoin, raw_ostream& O)
2000{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002001 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002002 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002003 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002004 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002005
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002006 O.indent(Indent2) << "bool HasChildren,\n";
2007 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2008 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2009 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2010 O.indent(Indent1) << "{\n";
2011 O.indent(Indent2) << "std::string cmd;\n";
2012 O.indent(Indent2) << "std::vector<std::string> vec;\n";
2013 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2014 O.indent(Indent2) << "const char* output_suffix = \""
2015 << D.OutputSuffix << "\";\n";
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002016}
2017
2018// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2019// Tool::GenerateAction() method.
2020void EmitGenerateActionMethod (const ToolDescription& D,
2021 const OptionDescriptions& OptDescs,
2022 bool IsJoin, raw_ostream& O) {
2023
2024 EmitGenerateActionMethodHeader(D, IsJoin, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002025
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002026 if (!D.CmdLine)
2027 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002028
2029 bool IndexCheckRequired = IsOutFileIndexCheckRequired(D.CmdLine);
2030 O.indent(Indent2) << "int out_file_index"
2031 << (IndexCheckRequired ? " = -1" : "")
2032 << ";\n\n";
2033
2034 // Process the cmd_line property.
2035 if (typeid(*D.CmdLine) == typeid(StringInit))
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002036 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2037 else
2038 EmitCaseConstructHandler(D.CmdLine, Indent2,
2039 EmitCmdLineVecFillCallback(IsJoin, D.Name),
2040 true, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002041
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002042 // For every understood option, emit handling code.
2043 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002044 EmitCaseConstructHandler(D.Actions, Indent2,
2045 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002046 false, OptDescs, O);
2047
2048 O << '\n';
2049 O.indent(Indent2)
2050 << "std::string out_file = OutFilename("
2051 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
2052 O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002053
2054 if (IndexCheckRequired)
2055 O.indent(Indent2) << "if (out_file_index != -1)\n";
2056 O.indent(IndexCheckRequired ? Indent3 : Indent2)
2057 << "vec[out_file_index] = out_file;\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002058
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002059 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002060 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002061 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
2062 O.indent(Indent3) << "vec.insert(vec.end(), "
2063 << SinkOptionName << ".begin(), " << SinkOptionName
2064 << ".end());\n";
2065 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002066 }
2067
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002068 O.indent(Indent2) << "return Action(cmd, vec, stop_compilation, out_file);\n";
2069 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002070}
2071
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002072/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2073/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002074void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2075 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002076 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002077 if (!ToolDesc.isJoin()) {
2078 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
2079 O.indent(Indent2) << "bool HasChildren,\n";
2080 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2081 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2082 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2083 O.indent(Indent1) << "{\n";
2084 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
2085 << " is not a Join tool!\");\n";
2086 O.indent(Indent1) << "}\n\n";
2087 }
2088 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002089 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002090 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002091
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002092 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002093}
2094
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002095/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2096/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002097void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002098 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2099 O.indent(Indent2) << "return InputLanguages_;\n";
2100 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002101
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002102 if (D.OutLanguage.empty())
2103 throw "Tool " + D.Name + " has no 'out_language' property!";
2104
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002105 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2106 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2107 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002108}
2109
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002110/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002111void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002112 O.indent(Indent1) << "const char* Name() const {\n";
2113 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2114 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002115}
2116
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002117/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2118/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002119void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002120 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002121 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002122 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002123 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002124 O.indent(Indent2) << "return false;\n";
2125 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002126}
2127
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002128/// EmitStaticMemberDefinitions - Emit static member definitions for a
2129/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002130void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002131 if (D.InLanguage.empty())
2132 throw "Tool " + D.Name + " has no 'in_language' property!";
2133
2134 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2135 for (StrVector::const_iterator B = D.InLanguage.begin(),
2136 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002137 O << '\"' << *B << "\", ";
2138 O << "0};\n\n";
2139}
2140
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002141/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002142void EmitToolClassDefinition (const ToolDescription& D,
2143 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002144 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002145 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002146 return;
2147
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002148 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002149 O << "class " << D.Name << " : public ";
2150 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002151 O << "JoinTool";
2152 else
2153 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002154
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002155 O << "{\nprivate:\n";
2156 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002157
2158 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002159 EmitNameMethod(D, O);
2160 EmitInOutLanguageMethods(D, O);
2161 EmitIsJoinMethod(D, O);
2162 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002163
2164 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002165 O << "};\n";
2166
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002167 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002168
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002169}
2170
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002171/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002172/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002173void EmitOptionDefinitions (const OptionDescriptions& descs,
2174 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002175 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002176{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002177 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002178
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002179 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002180 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002181 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002182 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002183
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002184 if (val.Type == OptionType::Alias) {
2185 Aliases.push_back(val);
2186 continue;
2187 }
2188
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002189 if (val.isExtern())
2190 O << "extern ";
2191
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002192 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002193 << val.GenVariableName();
2194
2195 if (val.isExtern()) {
2196 O << ";\n";
2197 continue;
2198 }
2199
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002200 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002201
2202 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2203 O << ", cl::Prefix";
2204
2205 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002206 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002207 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002208 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002209 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002210 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002211 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002212 O << ", cl::OneOrMore";
2213 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002214 else if (val.isZeroOrOne() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002215 O << ", cl::ZeroOrOne";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002216 }
2217
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002218 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002219 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002220 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002221 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002222
2223 if (val.isCommaSeparated())
2224 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002225
2226 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002227 O << ", cl::multi_val(" << val.MultiVal << ')';
2228
2229 if (val.InitVal) {
2230 const std::string& str = val.InitVal->getAsString();
2231 O << ", cl::init(" << str << ')';
2232 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002233
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002234 if (!val.Help.empty())
2235 O << ", cl::desc(\"" << val.Help << "\")";
2236
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002237 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002238 }
2239
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002240 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002241 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002242 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002243 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002244
2245 O << val.GenTypeDeclaration() << ' '
2246 << val.GenVariableName()
2247 << "(\"" << val.Name << '\"';
2248
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002249 const OptionDescription& D = descs.FindOption(val.Help);
2250 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002251
2252 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2253 }
2254
2255 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002256 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002257 O << (HasExterns ? "extern cl" : "cl")
2258 << "::list<std::string> " << SinkOptionName
2259 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002260
2261 O << '\n';
2262}
2263
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002264/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002265/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002266class EmitPreprocessOptionsCallback : ActionHandlingCallbackBase {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002267 const OptionDescriptions& OptDescs_;
2268
2269 void onUnsetOption(Init* i, unsigned IndentLevel, raw_ostream& O) {
2270 const std::string& OptName = InitPtrToString(i);
2271 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002272
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002273 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002274 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2275 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002276 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002277 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2278 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002279 else if (OptDesc.isList()) {
2280 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2281 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002282 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002283 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002284 }
2285 }
2286
2287 void processDag(const Init* I, unsigned IndentLevel, raw_ostream& O)
2288 {
2289 const DagInit& d = InitPtrToDag(I);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002290 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002291
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002292 if (OpName == "warning") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002293 this->onWarningDag(d, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002294 }
2295 else if (OpName == "error") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002296 this->onWarningDag(d, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002297 }
2298 else if (OpName == "unset_option") {
2299 checkNumberOfArguments(&d, 1);
2300 Init* I = d.getArg(0);
2301 if (typeid(*I) == typeid(ListInit)) {
2302 const ListInit& DagList = *static_cast<const ListInit*>(I);
2303 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
2304 B != E; ++B)
2305 this->onUnsetOption(*B, IndentLevel, O);
2306 }
2307 else {
2308 this->onUnsetOption(I, IndentLevel, O);
2309 }
2310 }
2311 else {
2312 throw "Unknown operator in the option preprocessor: '" + OpName + "'!"
2313 "\nOnly 'warning', 'error' and 'unset_option' are allowed.";
2314 }
2315 }
2316
2317public:
2318
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002319 void operator()(const Init* I, unsigned IndentLevel, raw_ostream& O) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002320 this->processDag(I, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002321 }
2322
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002323 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002324 : OptDescs_(OptDescs)
2325 {}
2326};
2327
2328/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2329void EmitPreprocessOptions (const RecordKeeper& Records,
2330 const OptionDescriptions& OptDecs, raw_ostream& O)
2331{
2332 O << "void PreprocessOptionsLocal() {\n";
2333
2334 const RecordVector& OptionPreprocessors =
2335 Records.getAllDerivedDefinitions("OptionPreprocessor");
2336
2337 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2338 E = OptionPreprocessors.end(); B!=E; ++B) {
2339 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002340 EmitCaseConstructHandler(Case, Indent1,
2341 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002342 false, OptDecs, O);
2343 }
2344
2345 O << "}\n\n";
2346}
2347
2348/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002349void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002350{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002351 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002352
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002353 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002354 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002355
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002356 // It is allowed for a plugin to have no language map.
2357 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002358
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002359 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2360 if (!LangsToSuffixesList)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00002361 throw "Error in the language map definition!";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002362
2363 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002364 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002365
2366 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2367 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2368
2369 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002370 O.indent(Indent1) << "langMap[\""
2371 << InitPtrToString(Suffixes->getElement(i))
2372 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002373 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002374 }
2375
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002376 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002377}
2378
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002379/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2380/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002381void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002382 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002383 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002384 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002385
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002386 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002387 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002388 }
2389 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002390 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002391 }
2392 else if (OpName == "error") {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002393 checkNumberOfArguments(&d, 1);
2394 O.indent(IndentLevel) << "throw std::runtime_error(\""
2395 << InitPtrToString(d.getArg(0))
2396 << "\");\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002397 return;
2398 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002399 else {
2400 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002401 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002402 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002403
2404 if (d.getNumArgs() > 0)
2405 O << InitPtrToInt(d.getArg(0)) << ";\n";
2406 else
2407 O << "2;\n";
2408
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002409}
2410
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002411/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002412void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002413 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002414 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002415
2416 // Class constructor.
2417 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002418 << "public:\n";
2419 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2420 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002421
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002422 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002423 O.indent(Indent1)
2424 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2425 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002426
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002427 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002428 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002429
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002430 O.indent(Indent2) << "return ret;\n";
2431 O.indent(Indent1) << "};\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002432}
2433
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002434/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002435void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002436 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002437 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002438 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002439 for (RecordVector::const_iterator B = EdgeVector.begin(),
2440 E = EdgeVector.end(); B != E; ++B) {
2441 const Record* Edge = *B;
2442 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002443 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002444
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002445 if (!isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002446 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002447 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002448 }
2449}
2450
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002451/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002452/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002453void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002454 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002455 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002456{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002457 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002458
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002459 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2460 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002461 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002462
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002463 O << '\n';
2464
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002465 // Insert edges.
2466
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002467 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002468 for (RecordVector::const_iterator B = EdgeVector.begin(),
2469 E = EdgeVector.end(); B != E; ++B) {
2470 const Record* Edge = *B;
2471 const std::string& NodeA = Edge->getValueAsString("a");
2472 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002473 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002474
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002475 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002476
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002477 if (isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002478 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002479 else
2480 O << "new Edge" << i << "()";
2481
2482 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002483 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002484 }
2485
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002486 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002487}
2488
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002489/// HookInfo - Information about the hook type and number of arguments.
2490struct HookInfo {
2491
2492 // A hook can either have a single parameter of type std::vector<std::string>,
2493 // or NumArgs parameters of type const char*.
2494 enum HookType { ListHook, ArgHook };
2495
2496 HookType Type;
2497 unsigned NumArgs;
2498
2499 HookInfo() : Type(ArgHook), NumArgs(1)
2500 {}
2501
2502 HookInfo(HookType T) : Type(T), NumArgs(1)
2503 {}
2504
2505 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2506 {}
2507};
2508
2509typedef llvm::StringMap<HookInfo> HookInfoMap;
2510
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002511/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002512/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002513/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002514class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002515 HookInfoMap& HookNames_;
2516 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002517public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002518 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2519 : HookNames_(HookNames), OptDescs_(OptDescs)
2520 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002521
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002522 void onAction (const DagInit& Dag) {
2523 if (GetOperatorName(Dag) == "forward_transformed_value") {
2524 checkNumberOfArguments(Dag, 2);
2525 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2526 const std::string& HookName = InitPtrToString(Dag.getArg(1));
2527 const OptionDescription& D = OptDescs_.FindOption(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002528
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002529 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2530 : HookInfo::ArgHook);
2531 }
2532 }
2533
2534 void operator()(const Init* Arg) {
2535
2536 // We're invoked on an action (either a dag or a dag list).
2537 if (typeid(*Arg) == typeid(DagInit)) {
2538 const DagInit& Dag = InitPtrToDag(Arg);
2539 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002540 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002541 }
2542 else if (typeid(*Arg) == typeid(ListInit)) {
2543 const ListInit& List = InitPtrToList(Arg);
2544 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2545 ++B) {
2546 const DagInit& Dag = InitPtrToDag(*B);
2547 this->onAction(Dag);
2548 }
2549 return;
2550 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002551
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002552 // We're invoked on a command line.
2553 StrVector cmds;
2554 TokenizeCmdline(InitPtrToString(Arg), cmds);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002555 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2556 B != E; ++B) {
2557 const std::string& cmd = *B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002558
2559 if (cmd == "$CALL") {
2560 unsigned NumArgs = 0;
2561 checkedIncrement(B, E, "Syntax error in $CALL invocation!");
2562 const std::string& HookName = *B;
2563
2564
2565 if (HookName.at(0) == ')')
2566 throw "$CALL invoked with no arguments!";
2567
2568 while (++B != E && B->at(0) != ')') {
2569 ++NumArgs;
2570 }
2571
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002572 HookInfoMap::const_iterator H = HookNames_.find(HookName);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002573
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002574 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2575 H->second.Type != HookInfo::ArgHook)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002576 throw "Overloading of hooks is not allowed. Overloaded hook: "
2577 + HookName;
2578 else
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002579 HookNames_[HookName] = HookInfo(NumArgs);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002580
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002581 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002582 }
2583 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002584
2585 void operator()(const DagInit* Test, unsigned, bool) {
2586 this->operator()(Test);
2587 }
2588 void operator()(const Init* Statement, unsigned) {
2589 this->operator()(Statement);
2590 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002591};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002592
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002593/// FillInHookNames - Actually extract the hook names from all command
2594/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002595void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002596 const OptionDescriptions& OptDescs,
2597 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002598{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002599 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002600 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2601 E = ToolDescs.end(); B != E; ++B) {
2602 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002603
2604 // Look for 'forward_transformed_value' in 'actions'.
2605 if (D.Actions)
2606 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2607
2608 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002609 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002610 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002611 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002612 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002613 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002614 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002615 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002616 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002617 }
2618}
2619
2620/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2621/// property records and emit hook function declaration for each
2622/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002623void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2624 const OptionDescriptions& OptDescs, raw_ostream& O) {
2625 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002626
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002627 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002628 if (HookNames.empty())
2629 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002630
2631 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002632 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002633 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002634 const char* HookName = B->first();
2635 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002636
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002637 O.indent(Indent1) << "std::string " << HookName << "(";
2638
2639 if (Info.Type == HookInfo::ArgHook) {
2640 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2641 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2642 }
2643 }
2644 else {
2645 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002646 }
2647
2648 O <<");\n";
2649 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002650 O << "}\n\n";
2651}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002652
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002653/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002654void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002655 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2656 O.indent(Indent1) << "int Priority() const { return "
2657 << Priority << "; }\n\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002658 O.indent(Indent1) << "void PreprocessOptions() const\n";
2659 O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002660 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2661 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2662 O.indent(Indent1)
2663 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2664 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2665 << "};\n\n"
2666 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002667}
2668
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002669/// EmitIncludes - Emit necessary #include directives and some
2670/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002671void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002672 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2673 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002674 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002675 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2676 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002677
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002678 << "#include \"llvm/Support/CommandLine.h\"\n"
2679 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002680
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002681 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002682 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002683 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002684 << "#include <stdexcept>\n\n"
2685
2686 << "using namespace llvm;\n"
2687 << "using namespace llvmc;\n\n"
2688
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002689 << "extern cl::opt<std::string> OutputFilename;\n\n"
2690
2691 << "inline const char* checkCString(const char* s)\n"
2692 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002693}
2694
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002695
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002696/// PluginData - Holds all information about a plugin.
2697struct PluginData {
2698 OptionDescriptions OptDescs;
2699 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002700 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002701 ToolDescriptions ToolDescs;
2702 RecordVector Edges;
2703 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002704};
2705
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002706/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002707/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002708bool HasSink(const ToolDescriptions& ToolDescs) {
2709 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2710 E = ToolDescs.end(); B != E; ++B)
2711 if ((*B)->isSink())
2712 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002713
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002714 return false;
2715}
2716
2717/// HasExterns - Go through the list of option descriptions and check
2718/// if there are any external options.
2719bool HasExterns(const OptionDescriptions& OptDescs) {
2720 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2721 E = OptDescs.end(); B != E; ++B)
2722 if (B->second.isExtern())
2723 return true;
2724
2725 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002726}
2727
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002728/// CollectPluginData - Collect tool and option properties,
2729/// compilation graph edges and plugin priority from the parse tree.
2730void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2731 // Collect option properties.
2732 const RecordVector& OptionLists =
2733 Records.getAllDerivedDefinitions("OptionList");
2734 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2735 Data.OptDescs);
2736
2737 // Collect tool properties.
2738 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2739 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2740 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002741 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002742
2743 // Collect compilation graph edges.
2744 const RecordVector& CompilationGraphs =
2745 Records.getAllDerivedDefinitions("CompilationGraph");
2746 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2747 Data.Edges);
2748
2749 // Calculate the priority of this plugin.
2750 const RecordVector& Priorities =
2751 Records.getAllDerivedDefinitions("PluginPriority");
2752 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002753}
2754
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002755/// CheckPluginData - Perform some sanity checks on the collected data.
2756void CheckPluginData(PluginData& Data) {
2757 // Filter out all tools not mentioned in the compilation graph.
2758 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002759
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002760 // Typecheck the compilation graph.
2761 TypecheckGraph(Data.Edges, Data.ToolDescs);
2762
2763 // Check that there are no options without side effects (specified
2764 // only in the OptionList).
2765 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2766
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002767}
2768
Daniel Dunbar1a551802009-07-03 00:10:29 +00002769void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002770 // Emit file header.
2771 EmitIncludes(O);
2772
2773 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002774 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002775
2776 // Emit hook declarations.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002777 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002778
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002779 O << "namespace {\n\n";
2780
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002781 // Emit PreprocessOptionsLocal() function.
2782 EmitPreprocessOptions(Records, Data.OptDescs, O);
2783
2784 // Emit PopulateLanguageMapLocal() function
2785 // (language map maps from file extensions to language names).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002786 EmitPopulateLanguageMap(Records, O);
2787
2788 // Emit Tool classes.
2789 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2790 E = Data.ToolDescs.end(); B!=E; ++B)
2791 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2792
2793 // Emit Edge# classes.
2794 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2795
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002796 // Emit PopulateCompilationGraphLocal() function.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002797 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2798
2799 // Emit code for plugin registration.
2800 EmitRegisterPlugin(Data.Priority, O);
2801
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002802 O << "} // End anonymous namespace.\n\n";
2803
2804 // Force linkage magic.
2805 O << "namespace llvmc {\n";
2806 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2807 O << "}\n";
2808
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002809 // EOF
2810}
2811
2812
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002813// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00002814}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002815
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002816/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002817void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002818 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002819 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002820
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002821 CollectPluginData(Records, Data);
2822 CheckPluginData(Data);
2823
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +00002824 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002825 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002826
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002827 } catch (std::exception& Error) {
2828 throw Error.what() + std::string(" - usually this means a syntax error.");
2829 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002830}