blob: 6217f5c1d34a8e85f00186af390a5268c4da4207 [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"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000018#include "llvm/ADT/StringMap.h"
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000019#include "llvm/ADT/StringSet.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000020#include <algorithm>
21#include <cassert>
22#include <functional>
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +000023#include <stdexcept>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000024#include <string>
Chris Lattner32a9e7a2008-06-04 04:46:14 +000025#include <typeinfo>
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +000026
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000027using namespace llvm;
28
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000029
30//===----------------------------------------------------------------------===//
31/// Typedefs
32
33typedef std::vector<Record*> RecordVector;
34typedef std::vector<std::string> StrVector;
35
36//===----------------------------------------------------------------------===//
37/// Constants
38
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +000039// Indentation.
Chris Lattnerec8e1b72009-11-03 18:30:31 +000040static const unsigned TabWidth = 4;
41static const unsigned Indent1 = TabWidth*1;
42static const unsigned Indent2 = TabWidth*2;
43static const unsigned Indent3 = TabWidth*3;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000044
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000045// Default help string.
Chris Lattnerec8e1b72009-11-03 18:30:31 +000046static const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000047
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000048// Name for the "sink" option.
Chris Lattnerec8e1b72009-11-03 18:30:31 +000049static const char * const SinkOptionName = "AutoGeneratedSinkOption";
50
51namespace {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000052
53//===----------------------------------------------------------------------===//
54/// Helper functions
55
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000056/// Id - An 'identity' function object.
57struct Id {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000058 template<typename T0>
59 void operator()(const T0&) const {
60 }
61 template<typename T0, typename T1>
62 void operator()(const T0&, const T1&) const {
63 }
64 template<typename T0, typename T1, typename T2>
65 void operator()(const T0&, const T1&, const T2&) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000066 }
67};
68
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000069int InitPtrToInt(const Init* ptr) {
70 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000071 return val.getValue();
72}
73
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000074const std::string& InitPtrToString(const Init* ptr) {
75 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
76 return val.getValue();
77}
78
79const ListInit& InitPtrToList(const Init* ptr) {
80 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
81 return val;
82}
83
84const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000085 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000086 return val;
87}
88
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000089const std::string GetOperatorName(const DagInit* D) {
90 return D->getOperator()->getAsString();
91}
92
93const std::string GetOperatorName(const DagInit& D) {
94 return GetOperatorName(&D);
95}
96
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000097// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +000098// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +000099void checkNumberOfArguments (const DagInit* d, unsigned minArgs) {
100 if (!d || d->getNumArgs() < minArgs)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000101 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000102}
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +0000103void checkNumberOfArguments (const DagInit& d, unsigned minArgs) {
104 checkNumberOfArguments(&d, minArgs);
105}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000106
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000107// isDagEmpty - is this DAG marked with an empty marker?
108bool isDagEmpty (const DagInit* d) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000109 return GetOperatorName(d) == "empty_dag_marker";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000110}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000111
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000112// EscapeVariableName - Escape commas and other symbols not allowed
113// in the C++ variable names. Makes it possible to use options named
114// like "Wa," (useful for prefix options).
115std::string EscapeVariableName(const std::string& Var) {
116 std::string ret;
117 for (unsigned i = 0; i != Var.size(); ++i) {
118 char cur_char = Var[i];
119 if (cur_char == ',') {
120 ret += "_comma_";
121 }
122 else if (cur_char == '+') {
123 ret += "_plus_";
124 }
125 else if (cur_char == '-') {
126 ret += "_dash_";
127 }
128 else {
129 ret.push_back(cur_char);
130 }
131 }
132 return ret;
133}
134
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000135/// oneOf - Does the input string contain this character?
136bool oneOf(const char* lst, char c) {
137 while (*lst) {
138 if (*lst++ == c)
139 return true;
140 }
141 return false;
142}
143
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000144template <class I, class S>
145void checkedIncrement(I& P, I E, S ErrorString) {
146 ++P;
147 if (P == E)
148 throw ErrorString;
149}
150
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000151// apply is needed because C++'s syntax doesn't let us construct a function
152// object and call it in the same statement.
153template<typename F, typename T0>
154void apply(F Fun, T0& Arg0) {
155 return Fun(Arg0);
156}
157
158template<typename F, typename T0, typename T1>
159void apply(F Fun, T0& Arg0, T1& Arg1) {
160 return Fun(Arg0, Arg1);
161}
162
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000163//===----------------------------------------------------------------------===//
164/// Back-end specific code
165
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000166
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000167/// OptionType - One of six different option types. See the
168/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000169namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000170
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000171 enum OptionType { Alias, Switch, Parameter, ParameterList,
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000172 Prefix, PrefixList};
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000173
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000174 bool IsAlias(OptionType t) {
175 return (t == Alias);
176 }
177
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000178 bool IsList (OptionType t) {
179 return (t == ParameterList || t == PrefixList);
180 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000181
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000182 bool IsSwitch (OptionType t) {
183 return (t == Switch);
184 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000185
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000186 bool IsParameter (OptionType t) {
187 return (t == Parameter || t == Prefix);
188 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000189
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000190}
191
192OptionType::OptionType stringToOptionType(const std::string& T) {
193 if (T == "alias_option")
194 return OptionType::Alias;
195 else if (T == "switch_option")
196 return OptionType::Switch;
197 else if (T == "parameter_option")
198 return OptionType::Parameter;
199 else if (T == "parameter_list_option")
200 return OptionType::ParameterList;
201 else if (T == "prefix_option")
202 return OptionType::Prefix;
203 else if (T == "prefix_list_option")
204 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000205 else
206 throw "Unknown option type: " + T + '!';
207}
208
209namespace OptionDescriptionFlags {
210 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000211 ReallyHidden = 0x4, Extern = 0x8,
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000212 OneOrMore = 0x10, Optional = 0x20,
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000213 CommaSeparated = 0x40 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000214}
215
216/// OptionDescription - Represents data contained in a single
217/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000218struct OptionDescription {
219 OptionType::OptionType Type;
220 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000221 unsigned Flags;
222 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000223 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000224 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000225
226 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000227 const std::string& n = "",
228 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000229 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000230 {}
231
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000232 /// GenTypeDeclaration - Returns the C++ variable type of this
233 /// option.
234 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000235
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000236 /// GenVariableName - Returns the variable name used in the
237 /// generated C++ code.
238 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000239
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000240 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000241 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000242
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000243 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000244
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000245 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000246
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000247 bool isMultiVal() const;
248
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000249 bool isCommaSeparated() const;
250 void setCommaSeparated();
251
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000252 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000253 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000254
255 bool isRequired() const;
256 void setRequired();
257
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000258 bool isOneOrMore() const;
259 void setOneOrMore();
260
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000261 bool isOptional() const;
262 void setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000263
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000264 bool isHidden() const;
265 void setHidden();
266
267 bool isReallyHidden() const;
268 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000269
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000270 bool isSwitch() const
271 { return OptionType::IsSwitch(this->Type); }
272
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000273 bool isParameter() const
274 { return OptionType::IsParameter(this->Type); }
275
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000276 bool isList() const
277 { return OptionType::IsList(this->Type); }
278
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000279};
280
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000281void OptionDescription::Merge (const OptionDescription& other)
282{
283 if (other.Type != Type)
284 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000285
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000286 if (Help == other.Help || Help == DefaultHelpString)
287 Help = other.Help;
288 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000289 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000290 " defined for option " + Name + "\n";
291 }
292
293 Flags |= other.Flags;
294}
295
296bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000297 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000298}
299
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000300bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000301 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000302}
303
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000304bool OptionDescription::isCommaSeparated() const {
305 return Flags & OptionDescriptionFlags::CommaSeparated;
306}
307void OptionDescription::setCommaSeparated() {
308 Flags |= OptionDescriptionFlags::CommaSeparated;
309}
310
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000311bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000312 return Flags & OptionDescriptionFlags::Extern;
313}
314void OptionDescription::setExtern() {
315 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000316}
317
318bool OptionDescription::isRequired() const {
319 return Flags & OptionDescriptionFlags::Required;
320}
321void OptionDescription::setRequired() {
322 Flags |= OptionDescriptionFlags::Required;
323}
324
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000325bool OptionDescription::isOneOrMore() const {
326 return Flags & OptionDescriptionFlags::OneOrMore;
327}
328void OptionDescription::setOneOrMore() {
329 Flags |= OptionDescriptionFlags::OneOrMore;
330}
331
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000332bool OptionDescription::isOptional() const {
333 return Flags & OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000334}
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000335void OptionDescription::setOptional() {
336 Flags |= OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000337}
338
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000339bool OptionDescription::isHidden() const {
340 return Flags & OptionDescriptionFlags::Hidden;
341}
342void OptionDescription::setHidden() {
343 Flags |= OptionDescriptionFlags::Hidden;
344}
345
346bool OptionDescription::isReallyHidden() const {
347 return Flags & OptionDescriptionFlags::ReallyHidden;
348}
349void OptionDescription::setReallyHidden() {
350 Flags |= OptionDescriptionFlags::ReallyHidden;
351}
352
353const char* OptionDescription::GenTypeDeclaration() const {
354 switch (Type) {
355 case OptionType::Alias:
356 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000357 case OptionType::PrefixList:
358 case OptionType::ParameterList:
359 return "cl::list<std::string>";
360 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000361 return "cl::opt<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000362 case OptionType::Parameter:
363 case OptionType::Prefix:
364 default:
365 return "cl::opt<std::string>";
366 }
367}
368
369std::string OptionDescription::GenVariableName() const {
370 const std::string& EscapedName = EscapeVariableName(Name);
371 switch (Type) {
372 case OptionType::Alias:
373 return "AutoGeneratedAlias_" + EscapedName;
374 case OptionType::PrefixList:
375 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000376 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000377 case OptionType::Switch:
378 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000379 case OptionType::Prefix:
380 case OptionType::Parameter:
381 default:
382 return "AutoGeneratedParameter_" + EscapedName;
383 }
384}
385
386/// OptionDescriptions - An OptionDescription array plus some helper
387/// functions.
388class OptionDescriptions {
389 typedef StringMap<OptionDescription> container_type;
390
391 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000392 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000393
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000394public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000395 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000396 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000397
398 // Wrappers for FindOption that throw an exception in case the option has a
399 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000400 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000401 const OptionDescription& FindParameter(const std::string& OptName) const;
402 const OptionDescription& FindList(const std::string& OptName) const;
403 const OptionDescription&
404 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000405
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000406 /// insertDescription - Insert new OptionDescription into
407 /// OptionDescriptions list
408 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000409
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000410 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000411 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000412 const_iterator begin() const { return Descriptions.begin(); }
413 const_iterator end() const { return Descriptions.end(); }
414};
415
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000416const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000417OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000418 const_iterator I = Descriptions.find(OptName);
419 if (I != Descriptions.end())
420 return I->second;
421 else
422 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000423}
424
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000425const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000426OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000427 const OptionDescription& OptDesc = this->FindOption(OptName);
428 if (!OptDesc.isSwitch())
429 throw OptName + ": incorrect option type - should be a switch!";
430 return OptDesc;
431}
432
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000433const OptionDescription&
434OptionDescriptions::FindList(const std::string& OptName) const {
435 const OptionDescription& OptDesc = this->FindOption(OptName);
436 if (!OptDesc.isList())
437 throw OptName + ": incorrect option type - should be a list!";
438 return OptDesc;
439}
440
441const OptionDescription&
442OptionDescriptions::FindParameter(const std::string& OptName) const {
443 const OptionDescription& OptDesc = this->FindOption(OptName);
444 if (!OptDesc.isParameter())
445 throw OptName + ": incorrect option type - should be a parameter!";
446 return OptDesc;
447}
448
449const OptionDescription&
450OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
451 const OptionDescription& OptDesc = this->FindOption(OptName);
452 if (!OptDesc.isList() && !OptDesc.isParameter())
453 throw OptName
454 + ": incorrect option type - should be a list or parameter!";
455 return OptDesc;
456}
457
458void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000459 container_type::iterator I = Descriptions.find(o.Name);
460 if (I != Descriptions.end()) {
461 OptionDescription& D = I->second;
462 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000463 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000464 else {
465 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000466 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000467}
468
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000469/// HandlerTable - A base class for function objects implemented as
470/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000471template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000472class HandlerTable {
473protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000474 // Implementation details.
475
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000476 /// HandlerMap - A map from property names to property handlers
477 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000478
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000479 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000480 static bool staticMembersInitialized_;
481
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000482public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000483
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000484 Handler GetHandler (const std::string& HandlerName) const {
485 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000486
487 if (method != Handlers_.end()) {
488 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000489 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000490 }
491 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000492 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000493 }
494 }
495
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000496 void AddHandler(const char* Property, Handler H) {
497 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000498 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000499
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000500};
501
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000502template <class FunctionObject>
503void InvokeDagInitHandler(FunctionObject* Obj, Init* i) {
504 typedef void (FunctionObject::*Handler) (const DagInit*);
505
506 const DagInit& property = InitPtrToDag(i);
507 const std::string& property_name = GetOperatorName(property);
508 Handler h = Obj->GetHandler(property_name);
509
510 ((Obj)->*(h))(&property);
511}
512
513template <typename H>
514typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
515
516template <typename H>
517bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000518
519
520/// CollectOptionProperties - Function object for iterating over an
521/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000522class CollectOptionProperties;
523typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
524(const DagInit*);
525
526class CollectOptionProperties
527: public HandlerTable<CollectOptionPropertiesHandler>
528{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000529private:
530
531 /// optDescs_ - OptionDescriptions table. This is where the
532 /// information is stored.
533 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000534
535public:
536
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000537 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000538 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000539 {
540 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000541 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000542 AddHandler("help", &CollectOptionProperties::onHelp);
543 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000544 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000545 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
546 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000547 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
548 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000549 AddHandler("optional", &CollectOptionProperties::onOptional);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000550 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000551
552 staticMembersInitialized_ = true;
553 }
554 }
555
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000556 /// operator() - Just forwards to the corresponding property
557 /// handler.
558 void operator() (Init* i) {
559 InvokeDagInitHandler(this, i);
560 }
561
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000562private:
563
564 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000565 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000566
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000567 void onExtern (const DagInit* d) {
568 checkNumberOfArguments(d, 0);
569 optDesc_.setExtern();
570 }
571
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000572 void onHelp (const DagInit* d) {
573 checkNumberOfArguments(d, 1);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000574 optDesc_.Help = InitPtrToString(d->getArg(0));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000575 }
576
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000577 void onHidden (const DagInit* d) {
578 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000579 optDesc_.setHidden();
580 }
581
582 void onReallyHidden (const DagInit* d) {
583 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000584 optDesc_.setReallyHidden();
585 }
586
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000587 void onCommaSeparated (const DagInit* d) {
588 checkNumberOfArguments(d, 0);
589 if (!optDesc_.isList())
590 throw "'comma_separated' is valid only on list options!";
591 optDesc_.setCommaSeparated();
592 }
593
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000594 void onRequired (const DagInit* d) {
595 checkNumberOfArguments(d, 0);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000596 if (optDesc_.isOneOrMore() || optDesc_.isOptional())
597 throw "Only one of (required), (optional) or "
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000598 "(one_or_more) properties is allowed!";
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000599 optDesc_.setRequired();
600 }
601
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000602 void onInit (const DagInit* d) {
603 checkNumberOfArguments(d, 1);
604 Init* i = d->getArg(0);
605 const std::string& str = i->getAsString();
606
607 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
608 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
609
610 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000611 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000612
613 optDesc_.InitVal = i;
614 }
615
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000616 void onOneOrMore (const DagInit* d) {
617 checkNumberOfArguments(d, 0);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000618 if (optDesc_.isRequired() || optDesc_.isOptional())
619 throw "Only one of (required), (optional) or "
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000620 "(one_or_more) properties is allowed!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000621 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000622 llvm::errs() << "Warning: specifying the 'one_or_more' property "
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000623 "on a non-list option will have no effect.\n";
624 optDesc_.setOneOrMore();
625 }
626
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000627 void onOptional (const DagInit* d) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000628 checkNumberOfArguments(d, 0);
629 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000630 throw "Only one of (required), (optional) or "
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000631 "(one_or_more) properties is allowed!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000632 if (!OptionType::IsList(optDesc_.Type))
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000633 llvm::errs() << "Warning: specifying the 'optional' property"
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000634 "on a non-list option will have no effect.\n";
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000635 optDesc_.setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000636 }
637
638 void onMultiVal (const DagInit* d) {
639 checkNumberOfArguments(d, 1);
640 int val = InitPtrToInt(d->getArg(0));
641 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000642 throw "Error in the 'multi_val' property: "
643 "the value must be greater than 1!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000644 if (!OptionType::IsList(optDesc_.Type))
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000645 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000646 optDesc_.MultiVal = val;
647 }
648
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000649};
650
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000651/// AddOption - A function object that is applied to every option
652/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000653class AddOption {
654private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000655 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000656
657public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000658 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000659 {}
660
661 void operator()(const Init* i) {
662 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000663 checkNumberOfArguments(&d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000664
665 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000666 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000667 const std::string& Name = InitPtrToString(d.getArg(0));
668
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000669 OptionDescription OD(Type, Name);
670
671 if (!OD.isExtern())
672 checkNumberOfArguments(&d, 2);
673
674 if (OD.isAlias()) {
675 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000676 OD.Help = InitPtrToString(d.getArg(1));
677 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000678 else if (!OD.isExtern()) {
679 processOptionProperties(&d, OD);
680 }
681 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000682 }
683
684private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000685 /// processOptionProperties - Go through the list of option
686 /// properties and call a corresponding handler for each.
687 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
688 checkNumberOfArguments(d, 2);
689 DagInit::const_arg_iterator B = d->arg_begin();
690 // Skip the first argument: it's always the option name.
691 ++B;
692 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000693 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000694
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000695};
696
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000697/// CollectOptionDescriptions - Collects option properties from all
698/// OptionLists.
699void CollectOptionDescriptions (RecordVector::const_iterator B,
700 RecordVector::const_iterator E,
701 OptionDescriptions& OptDescs)
702{
703 // For every OptionList:
704 for (; B!=E; ++B) {
705 RecordVector::value_type T = *B;
706 // Throws an exception if the value does not exist.
707 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000708
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000709 // For every option description in this list:
710 // collect the information and
711 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
712 }
713}
714
715// Tool information record
716
717namespace ToolFlags {
718 enum ToolFlags { Join = 0x1, Sink = 0x2 };
719}
720
721struct ToolDescription : public RefCountedBase<ToolDescription> {
722 std::string Name;
723 Init* CmdLine;
724 Init* Actions;
725 StrVector InLanguage;
726 std::string OutLanguage;
727 std::string OutputSuffix;
728 unsigned Flags;
729
730 // Various boolean properties
731 void setSink() { Flags |= ToolFlags::Sink; }
732 bool isSink() const { return Flags & ToolFlags::Sink; }
733 void setJoin() { Flags |= ToolFlags::Join; }
734 bool isJoin() const { return Flags & ToolFlags::Join; }
735
736 // Default ctor here is needed because StringMap can only store
737 // DefaultConstructible objects
738 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
739 ToolDescription (const std::string& n)
740 : Name(n), CmdLine(0), Actions(0), Flags(0)
741 {}
742};
743
744/// ToolDescriptions - A list of Tool information records.
745typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
746
747
748/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000749/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000750
751class CollectToolProperties;
752typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
753(const DagInit*);
754
755class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
756{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000757private:
758
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000759 /// toolDesc_ - Properties of the current Tool. This is where the
760 /// information is stored.
761 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000762
763public:
764
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000765 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000766 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000767 {
768 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000769
770 AddHandler("actions", &CollectToolProperties::onActions);
771 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
772 AddHandler("in_language", &CollectToolProperties::onInLanguage);
773 AddHandler("join", &CollectToolProperties::onJoin);
774 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
775 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
776 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000777
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000778 staticMembersInitialized_ = true;
779 }
780 }
781
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000782 void operator() (Init* i) {
783 InvokeDagInitHandler(this, i);
784 }
785
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000786private:
787
788 /// Property handlers --
789 /// Functions that extract information about tool properties from
790 /// DAG representation.
791
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000792 void onActions (const DagInit* d) {
793 checkNumberOfArguments(d, 1);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000794 Init* Case = d->getArg(0);
795 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000796 GetOperatorName(static_cast<DagInit*>(Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000797 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000798 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000799 }
800
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000801 void onCmdLine (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000802 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000803 toolDesc_.CmdLine = d->getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000804 }
805
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000806 void onInLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000807 checkNumberOfArguments(d, 1);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000808 Init* arg = d->getArg(0);
809
810 // Find out the argument's type.
811 if (typeid(*arg) == typeid(StringInit)) {
812 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000813 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000814 }
815 else {
816 // It's a list.
817 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000818 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000819
820 // Copy strings to the output vector.
821 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
822 B != E; ++B) {
823 out.push_back(InitPtrToString(*B));
824 }
825
826 // Remove duplicates.
827 std::sort(out.begin(), out.end());
828 StrVector::iterator newE = std::unique(out.begin(), out.end());
829 out.erase(newE, out.end());
830 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000831 }
832
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000833 void onJoin (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000834 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000835 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000836 }
837
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000838 void onOutLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000839 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000840 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000841 }
842
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000843 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000844 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000845 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000846 }
847
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000848 void onSink (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000849 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000850 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000851 }
852
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000853};
854
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000855/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000856/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000857/// CollectToolProperties function object).
858void CollectToolDescriptions (RecordVector::const_iterator B,
859 RecordVector::const_iterator E,
860 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000861{
862 // Iterate over a properties list of every Tool definition
863 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000864 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000865 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000866 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000867
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000868 IntrusiveRefCntPtr<ToolDescription>
869 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000870
871 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000872 CollectToolProperties(*ToolDesc));
873 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000874 }
875}
876
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000877/// FillInEdgeVector - Merge all compilation graph definitions into
878/// one single edge list.
879void FillInEdgeVector(RecordVector::const_iterator B,
880 RecordVector::const_iterator E, RecordVector& Out) {
881 for (; B != E; ++B) {
882 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000883
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000884 for (unsigned i = 0; i < edges->size(); ++i)
885 Out.push_back(edges->getElementAsRecord(i));
886 }
887}
888
889/// CalculatePriority - Calculate the priority of this plugin.
890int CalculatePriority(RecordVector::const_iterator B,
891 RecordVector::const_iterator E) {
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000892 int priority = 0;
893
894 if (B != E) {
895 priority = static_cast<int>((*B)->getValueAsInt("priority"));
896
897 if (++B != E)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000898 throw "More than one 'PluginPriority' instance found: "
899 "most probably an error!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000900 }
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000901
902 return priority;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000903}
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000904
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000905/// NotInGraph - Helper function object for FilterNotInGraph.
906struct NotInGraph {
907private:
908 const llvm::StringSet<>& ToolsInGraph_;
909
910public:
911 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
912 : ToolsInGraph_(ToolsInGraph)
913 {}
914
915 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
916 return (ToolsInGraph_.count(x->Name) == 0);
917 }
918};
919
920/// FilterNotInGraph - Filter out from ToolDescs all Tools not
921/// mentioned in the compilation graph definition.
922void FilterNotInGraph (const RecordVector& EdgeVector,
923 ToolDescriptions& ToolDescs) {
924
925 // List all tools mentioned in the graph.
926 llvm::StringSet<> ToolsInGraph;
927
928 for (RecordVector::const_iterator B = EdgeVector.begin(),
929 E = EdgeVector.end(); B != E; ++B) {
930
931 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000932 const std::string& NodeA = Edge->getValueAsString("a");
933 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000934
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000935 if (NodeA != "root")
936 ToolsInGraph.insert(NodeA);
937 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000938 }
939
940 // Filter ToolPropertiesList.
941 ToolDescriptions::iterator new_end =
942 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
943 NotInGraph(ToolsInGraph));
944 ToolDescs.erase(new_end, ToolDescs.end());
945}
946
947/// FillInToolToLang - Fills in two tables that map tool names to
948/// (input, output) languages. Helper function used by TypecheckGraph().
949void FillInToolToLang (const ToolDescriptions& ToolDescs,
950 StringMap<StringSet<> >& ToolToInLang,
951 StringMap<std::string>& ToolToOutLang) {
952 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
953 E = ToolDescs.end(); B != E; ++B) {
954 const ToolDescription& D = *(*B);
955 for (StrVector::const_iterator B = D.InLanguage.begin(),
956 E = D.InLanguage.end(); B != E; ++B)
957 ToolToInLang[D.Name].insert(*B);
958 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000959 }
960}
961
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000962/// TypecheckGraph - Check that names for output and input languages
963/// on all edges do match. This doesn't do much when the information
964/// about the whole graph is not available (i.e. when compiling most
965/// plugins).
966void TypecheckGraph (const RecordVector& EdgeVector,
967 const ToolDescriptions& ToolDescs) {
968 StringMap<StringSet<> > ToolToInLang;
969 StringMap<std::string> ToolToOutLang;
970
971 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
972 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
973 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
974
975 for (RecordVector::const_iterator B = EdgeVector.begin(),
976 E = EdgeVector.end(); B != E; ++B) {
977 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000978 const std::string& NodeA = Edge->getValueAsString("a");
979 const std::string& NodeB = Edge->getValueAsString("b");
980 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
981 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000982
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000983 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000984 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000985 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000986 + ": output->input language mismatch";
987 }
988
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000989 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000990 throw "Edges back to the root are not allowed!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000991 }
992}
993
994/// WalkCase - Walks the 'case' expression DAG and invokes
995/// TestCallback on every test, and StatementCallback on every
996/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000997/// combinators (that is, they are passed directly to TestCallback).
998/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
999/// IndentLevel, bool FirstTest)'.
1000/// StatementCallback must have type 'void StatementCallback(const Init*,
1001/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001002template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001003void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1004 unsigned IndentLevel = 0)
1005{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001006 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001007
1008 // Error checks.
1009 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001010 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001011
1012 if (d.getNumArgs() < 2)
1013 throw "There should be at least one clause in the 'case' expression:\n"
1014 + d.getAsString();
1015
1016 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001017 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001018 const unsigned numArgs = d.getNumArgs();
1019 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001020 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1021 B != E; ++B) {
1022 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001023
1024 if (!even)
1025 {
1026 // Handle test.
1027 const DagInit& Test = InitPtrToDag(arg);
1028
1029 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001030 throw "The 'default' clause should be the last in the "
1031 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001032 if (i == numArgs)
1033 throw "Case construct handler: no corresponding action "
1034 "found for the test " + Test.getAsString() + '!';
1035
1036 TestCallback(&Test, IndentLevel, (i == 1));
1037 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001038 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001039 {
1040 if (dynamic_cast<DagInit*>(arg)
1041 && GetOperatorName(static_cast<DagInit*>(arg)) == "case") {
1042 // Nested 'case'.
1043 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1044 }
1045
1046 // Handle statement.
1047 StatementCallback(arg, IndentLevel);
1048 }
1049
1050 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001051 even = !even;
1052 }
1053}
1054
1055/// ExtractOptionNames - A helper function object used by
1056/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1057class ExtractOptionNames {
1058 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001059
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001060 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001061 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001062 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001063 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001064 ActionName == "forward_value" ||
1065 ActionName == "forward_transformed_value" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001066 ActionName == "switch_on" || ActionName == "parameter_equals" ||
1067 ActionName == "element_in_list" || ActionName == "not_empty" ||
1068 ActionName == "empty") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001069 checkNumberOfArguments(&Stmt, 1);
1070 const std::string& Name = InitPtrToString(Stmt.getArg(0));
1071 OptionNames_.insert(Name);
1072 }
1073 else if (ActionName == "and" || ActionName == "or") {
1074 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001075 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001076 }
1077 }
1078 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001079
1080public:
1081 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1082 {}
1083
1084 void operator()(const Init* Statement) {
1085 if (typeid(*Statement) == typeid(ListInit)) {
1086 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1087 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1088 B != E; ++B)
1089 this->processDag(*B);
1090 }
1091 else {
1092 this->processDag(Statement);
1093 }
1094 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001095
1096 void operator()(const DagInit* Test, unsigned, bool) {
1097 this->operator()(Test);
1098 }
1099 void operator()(const Init* Statement, unsigned) {
1100 this->operator()(Statement);
1101 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001102};
1103
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001104/// CheckForSuperfluousOptions - Check that there are no side
1105/// effect-free options (specified only in the OptionList). Otherwise,
1106/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001107void CheckForSuperfluousOptions (const RecordVector& Edges,
1108 const ToolDescriptions& ToolDescs,
1109 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001110 llvm::StringSet<> nonSuperfluousOptions;
1111
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001112 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001113 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001114 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1115 E = ToolDescs.end(); B != E; ++B) {
1116 const ToolDescription& TD = *(*B);
1117 ExtractOptionNames Callback(nonSuperfluousOptions);
1118 if (TD.Actions)
1119 WalkCase(TD.Actions, Callback, Callback);
1120 }
1121
1122 // Add all options mentioned in the 'case' clauses of the
1123 // OptionalEdges of the compilation graph to the set of
1124 // non-superfluous options.
1125 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1126 B != E; ++B) {
1127 const Record* Edge = *B;
1128 DagInit* Weight = Edge->getValueAsDag("weight");
1129
1130 if (!isDagEmpty(Weight))
1131 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001132 }
1133
1134 // Check that all options in OptDescs belong to the set of
1135 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001136 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001137 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001138 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001139 if (!nonSuperfluousOptions.count(Val.Name)
1140 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001141 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001142 "Probable cause: this option is specified only in the OptionList.\n";
1143 }
1144}
1145
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001146/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1147bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1148 if (TestName == "single_input_file") {
1149 O << "InputFilenames.size() == 1";
1150 return true;
1151 }
1152 else if (TestName == "multiple_input_files") {
1153 O << "InputFilenames.size() > 1";
1154 return true;
1155 }
1156
1157 return false;
1158}
1159
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001160/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1161template <typename F>
1162void EmitListTest(const ListInit& L, const char* LogicOp,
1163 F Callback, raw_ostream& O)
1164{
1165 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1166 // of Dags...
1167 bool isFirst = true;
1168 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1169 if (isFirst)
1170 isFirst = false;
1171 else
1172 O << " || ";
1173 Callback(InitPtrToString(*B), O);
1174 }
1175}
1176
1177// Callbacks for use with EmitListTest.
1178
1179class EmitSwitchOn {
1180 const OptionDescriptions& OptDescs_;
1181public:
1182 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1183 {}
1184
1185 void operator()(const std::string& OptName, raw_ostream& O) const {
1186 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1187 O << OptDesc.GenVariableName();
1188 }
1189};
1190
1191class EmitEmptyTest {
1192 bool EmitNegate_;
1193 const OptionDescriptions& OptDescs_;
1194public:
1195 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1196 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1197 {}
1198
1199 void operator()(const std::string& OptName, raw_ostream& O) const {
1200 const char* Neg = (EmitNegate_ ? "!" : "");
1201 if (OptName == "o") {
1202 O << Neg << "OutputFilename.empty()";
1203 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001204 else if (OptName == "save-temps") {
1205 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1206 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001207 else {
1208 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1209 O << Neg << OptDesc.GenVariableName() << ".empty()";
1210 }
1211 }
1212};
1213
1214
1215/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1216bool EmitCaseTest1ArgList(const std::string& TestName,
1217 const DagInit& d,
1218 const OptionDescriptions& OptDescs,
1219 raw_ostream& O) {
1220 const ListInit& L = *static_cast<ListInit*>(d.getArg(0));
1221
1222 if (TestName == "any_switch_on") {
1223 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1224 return true;
1225 }
1226 else if (TestName == "switch_on") {
1227 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1228 return true;
1229 }
1230 else if (TestName == "any_not_empty") {
1231 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1232 return true;
1233 }
1234 else if (TestName == "any_empty") {
1235 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1236 return true;
1237 }
1238 else if (TestName == "not_empty") {
1239 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1240 return true;
1241 }
1242 else if (TestName == "empty") {
1243 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1244 return true;
1245 }
1246
1247 return false;
1248}
1249
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001250/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1251bool EmitCaseTest1ArgStr(const std::string& TestName,
1252 const DagInit& d,
1253 const OptionDescriptions& OptDescs,
1254 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001255 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001256
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001257 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001258 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001259 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001260 }
1261 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001262 O << "InLangs.count(\"" << OptName << "\") != 0";
1263 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001264 }
1265 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001266 // This works only for single-argument Tool::GenerateAction. Join
1267 // tools can process several files in different languages simultaneously.
1268
1269 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001270 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001271 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001272 }
1273 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001274 bool EmitNegate = (TestName == "not_empty");
1275 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001276 return true;
1277 }
1278
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001279 return false;
1280}
1281
1282/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1283bool EmitCaseTest1Arg(const std::string& TestName,
1284 const DagInit& d,
1285 const OptionDescriptions& OptDescs,
1286 raw_ostream& O) {
1287 checkNumberOfArguments(&d, 1);
1288 if (typeid(*d.getArg(0)) == typeid(ListInit))
1289 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1290 else
1291 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1292}
1293
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001294/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001295bool EmitCaseTest2Args(const std::string& TestName,
1296 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001297 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001298 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001299 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001300 checkNumberOfArguments(&d, 2);
1301 const std::string& OptName = InitPtrToString(d.getArg(0));
1302 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001303
1304 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001305 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001306 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1307 return true;
1308 }
1309 else if (TestName == "element_in_list") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001310 const OptionDescription& OptDesc = OptDescs.FindList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001311 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001312 O << "std::find(" << VarName << ".begin(),\n";
1313 O.indent(IndentLevel + Indent1)
1314 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001315 << OptArg << "\") != " << VarName << ".end()";
1316 return true;
1317 }
1318
1319 return false;
1320}
1321
1322// Forward declaration.
1323// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001324void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001325 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001326 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001327
1328/// EmitLogicalOperationTest - Helper function used by
1329/// EmitCaseConstructHandler.
1330void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001331 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001332 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001333 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001334 O << '(';
1335 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001336 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001337 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001338 if (j != NumArgs - 1) {
1339 O << ")\n";
1340 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1341 }
1342 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001343 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001344 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001345 }
1346}
1347
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001348void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001349 const OptionDescriptions& OptDescs, raw_ostream& O)
1350{
1351 checkNumberOfArguments(&d, 1);
1352 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1353 O << "! (";
1354 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1355 O << ")";
1356}
1357
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001358/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001359void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001360 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001361 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001362 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001363
1364 if (TestName == "and")
1365 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1366 else if (TestName == "or")
1367 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001368 else if (TestName == "not")
1369 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001370 else if (EmitCaseTest0Args(TestName, O))
1371 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001372 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1373 return;
1374 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1375 return;
1376 else
1377 throw TestName + ": unknown edge property!";
1378}
1379
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001380
1381/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1382class EmitCaseTestCallback {
1383 bool EmitElseIf_;
1384 const OptionDescriptions& OptDescs_;
1385 raw_ostream& O_;
1386public:
1387
1388 EmitCaseTestCallback(bool EmitElseIf,
1389 const OptionDescriptions& OptDescs, raw_ostream& O)
1390 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1391 {}
1392
1393 void operator()(const DagInit* Test, unsigned IndentLevel, bool FirstTest)
1394 {
1395 if (GetOperatorName(Test) == "default") {
1396 O_.indent(IndentLevel) << "else {\n";
1397 }
1398 else {
1399 O_.indent(IndentLevel)
1400 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
1401 EmitCaseTest(*Test, IndentLevel, OptDescs_, O_);
1402 O_ << ") {\n";
1403 }
1404 }
1405};
1406
1407/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1408template <typename F>
1409class EmitCaseStatementCallback {
1410 F Callback_;
1411 raw_ostream& O_;
1412public:
1413
1414 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1415 : Callback_(Callback), O_(O)
1416 {}
1417
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001418 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001419
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001420 // Ignore nested 'case' DAG.
1421 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001422 GetOperatorName(static_cast<const DagInit*>(Statement)) == "case")) {
1423 if (typeid(*Statement) == typeid(ListInit)) {
1424 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1425 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1426 B != E; ++B)
1427 Callback_(*B, (IndentLevel + Indent1), O_);
1428 }
1429 else {
1430 Callback_(Statement, (IndentLevel + Indent1), O_);
1431 }
1432 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001433 O_.indent(IndentLevel) << "}\n";
1434 }
1435
1436};
1437
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001438/// EmitCaseConstructHandler - Emit code that handles the 'case'
1439/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001440/// clause. Implemented on top of WalkCase.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001441/// Callback's type is void F(Init* Statement, unsigned IndentLevel,
1442/// raw_ostream& O).
1443/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001444/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1445/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001446template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001447void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001448 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001449 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001450 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001451 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1452 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001453}
1454
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001455/// TokenizeCmdline - converts from
1456/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1457/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001458void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
1459 const char* Delimiters = " \t\n\v\f\r";
1460 enum TokenizerState
1461 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1462 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001463
1464 if (CmdLine.empty())
1465 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001466 Out.push_back("");
1467
1468 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1469 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001470
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001471 for (; B != E; ++B) {
1472 char cur_ch = CmdLine[B];
1473
1474 switch (cur_st) {
1475 case Normal:
1476 if (cur_ch == '$') {
1477 cur_st = SpecialCommand;
1478 break;
1479 }
1480 if (oneOf(Delimiters, cur_ch)) {
1481 // Skip whitespace
1482 B = CmdLine.find_first_not_of(Delimiters, B);
1483 if (B == std::string::npos) {
1484 B = E-1;
1485 continue;
1486 }
1487 --B;
1488 Out.push_back("");
1489 continue;
1490 }
1491 break;
1492
1493
1494 case SpecialCommand:
1495 if (oneOf(Delimiters, cur_ch)) {
1496 cur_st = Normal;
1497 Out.push_back("");
1498 continue;
1499 }
1500 if (cur_ch == '(') {
1501 Out.push_back("");
1502 cur_st = InsideSpecialCommand;
1503 continue;
1504 }
1505 break;
1506
1507 case InsideSpecialCommand:
1508 if (oneOf(Delimiters, cur_ch)) {
1509 continue;
1510 }
1511 if (cur_ch == '\'') {
1512 cur_st = InsideQuotationMarks;
1513 Out.push_back("");
1514 continue;
1515 }
1516 if (cur_ch == ')') {
1517 cur_st = Normal;
1518 Out.push_back("");
1519 }
1520 if (cur_ch == ',') {
1521 continue;
1522 }
1523
1524 break;
1525
1526 case InsideQuotationMarks:
1527 if (cur_ch == '\'') {
1528 cur_st = InsideSpecialCommand;
1529 continue;
1530 }
1531 break;
1532 }
1533
1534 Out.back().push_back(cur_ch);
1535 }
1536}
1537
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001538/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1539/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1540/// SubstituteSpecialCommands().
1541StrVector::const_iterator
1542SubstituteCall (StrVector::const_iterator Pos,
1543 StrVector::const_iterator End,
1544 bool IsJoin, raw_ostream& O)
1545{
1546 const char* errorMessage = "Syntax error in $CALL invocation!";
1547 checkedIncrement(Pos, End, errorMessage);
1548 const std::string& CmdName = *Pos;
1549
1550 if (CmdName == ")")
1551 throw "$CALL invocation: empty argument list!";
1552
1553 O << "hooks::";
1554 O << CmdName << "(";
1555
1556
1557 bool firstIteration = true;
1558 while (true) {
1559 checkedIncrement(Pos, End, errorMessage);
1560 const std::string& Arg = *Pos;
1561 assert(Arg.size() != 0);
1562
1563 if (Arg[0] == ')')
1564 break;
1565
1566 if (firstIteration)
1567 firstIteration = false;
1568 else
1569 O << ", ";
1570
1571 if (Arg == "$INFILE") {
1572 if (IsJoin)
1573 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1574 else
1575 O << "inFile.c_str()";
1576 }
1577 else {
1578 O << '"' << Arg << '"';
1579 }
1580 }
1581
1582 O << ')';
1583
1584 return Pos;
1585}
1586
1587/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1588/// function used by SubstituteSpecialCommands().
1589StrVector::const_iterator
1590SubstituteEnv (StrVector::const_iterator Pos,
1591 StrVector::const_iterator End, raw_ostream& O)
1592{
1593 const char* errorMessage = "Syntax error in $ENV invocation!";
1594 checkedIncrement(Pos, End, errorMessage);
1595 const std::string& EnvName = *Pos;
1596
1597 if (EnvName == ")")
1598 throw "$ENV invocation: empty argument list!";
1599
1600 O << "checkCString(std::getenv(\"";
1601 O << EnvName;
1602 O << "\"))";
1603
1604 checkedIncrement(Pos, End, errorMessage);
1605
1606 return Pos;
1607}
1608
1609/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1610/// handler code. Helper function used by EmitCmdLineVecFill().
1611StrVector::const_iterator
1612SubstituteSpecialCommands (StrVector::const_iterator Pos,
1613 StrVector::const_iterator End,
1614 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001615{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001616
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001617 const std::string& cmd = *Pos;
1618
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001619 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001620 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001621 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001622 }
1623 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001624 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001625 }
1626 else {
1627 throw "Unknown special command: " + cmd;
1628 }
1629
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001630 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001631 const std::string& Leftover = *Pos;
1632 assert(Leftover.at(0) == ')');
1633 if (Leftover.size() != 1)
1634 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001635
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001636 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001637}
1638
1639/// EmitCmdLineVecFill - Emit code that fills in the command line
1640/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001641void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001642 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001643 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001644 StrVector StrVec;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001645 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1646
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001647 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001648 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001649
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001650 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1651
1652 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001653 assert(!StrVec[0].empty());
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001654 if (StrVec[0][0] == '$') {
1655 while (I != E && (*I)[0] != ')' )
1656 ++I;
1657
1658 // Skip the ')' symbol.
1659 ++I;
1660 }
1661 else {
1662 ++I;
1663 }
1664
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001665 bool hasINFILE = false;
1666
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001667 for (; I != E; ++I) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001668 const std::string& cmd = *I;
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001669 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001670 O.indent(IndentLevel);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001671 if (cmd.at(0) == '$') {
1672 if (cmd == "$INFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001673 hasINFILE = true;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001674 if (IsJoin) {
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001675 O << "for (PathVector::const_iterator B = inFiles.begin()"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001676 << ", E = inFiles.end();\n";
1677 O.indent(IndentLevel) << "B != E; ++B)\n";
1678 O.indent(IndentLevel + Indent1) << "vec.push_back(B->str());\n";
1679 }
1680 else {
Chris Lattner74382b72009-08-23 22:45:37 +00001681 O << "vec.push_back(inFile.str());\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001682 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001683 }
1684 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001685 O << "vec.push_back(\"\");\n";
1686 O.indent(IndentLevel) << "out_file_index = vec.size()-1;\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001687 }
1688 else {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001689 O << "vec.push_back(";
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001690 I = SubstituteSpecialCommands(I, E, IsJoin, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001691 O << ");\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001692 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001693 }
1694 else {
1695 O << "vec.push_back(\"" << cmd << "\");\n";
1696 }
1697 }
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001698 if (!hasINFILE)
1699 throw "Tool '" + ToolName + "' doesn't take any input!";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001700
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001701 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001702 if (StrVec[0][0] == '$')
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001703 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001704 else
1705 O << '"' << StrVec[0] << '"';
1706 O << ";\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001707}
1708
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001709/// EmitCmdLineVecFillCallback - A function object wrapper around
1710/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1711/// argument to EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001712class EmitCmdLineVecFillCallback {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001713 bool IsJoin;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001714 const std::string& ToolName;
1715 public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001716 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1717 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001718
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001719 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001720 raw_ostream& O) const
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001721 {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001722 EmitCmdLineVecFill(Statement, ToolName, IsJoin, IndentLevel, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001723 }
1724};
1725
1726/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1727/// implement EmitActionHandler. Emits code for
1728/// handling the (forward) and (forward_as) option properties.
1729void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001730 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001731 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001732 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001733 const std::string& Name = NewName.empty()
1734 ? ("-" + D.Name)
1735 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001736 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001737
1738 switch (D.Type) {
1739 case OptionType::Switch:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001740 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001741 break;
1742 case OptionType::Parameter:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001743 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
1744 O.indent(IndentLevel) << "vec.push_back(" << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001745 break;
1746 case OptionType::Prefix:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001747 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\" + "
1748 << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001749 break;
1750 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001751 O.indent(IndentLevel)
1752 << "for (" << D.GenTypeDeclaration()
1753 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1754 O.indent(IndentLevel)
1755 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1756 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\" + " << "*B);\n";
1757 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001758
1759 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001760 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1761 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001762 }
1763
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001764 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001765 break;
1766 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001767 O.indent(IndentLevel)
1768 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1769 << D.GenVariableName() << ".begin(),\n";
1770 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1771 << ".end() ; B != E;) {\n";
1772 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001773
1774 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001775 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1776 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001777 }
1778
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001779 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001780 break;
1781 case OptionType::Alias:
1782 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001783 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001784 }
1785}
1786
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001787/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1788/// EmitPreprocessOptionsCallback.
1789struct ActionHandlingCallbackBase {
1790
1791 void onErrorDag(const DagInit& d,
1792 unsigned IndentLevel, raw_ostream& O) const
1793 {
1794 O.indent(IndentLevel)
1795 << "throw std::runtime_error(\"" <<
1796 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1797 : "Unknown error!")
1798 << "\");\n";
1799 }
1800
1801 void onWarningDag(const DagInit& d,
1802 unsigned IndentLevel, raw_ostream& O) const
1803 {
1804 checkNumberOfArguments(&d, 1);
1805 O.indent(IndentLevel) << "llvm::errs() << \""
1806 << InitPtrToString(d.getArg(0)) << "\";\n";
1807 }
1808
1809};
1810
1811/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1812/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001813class EmitActionHandlersCallback;
1814typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1815(const DagInit&, unsigned, raw_ostream&) const;
1816
1817class EmitActionHandlersCallback
1818: public ActionHandlingCallbackBase,
1819 public HandlerTable<EmitActionHandlersCallbackHandler>
1820{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001821 const OptionDescriptions& OptDescs;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001822 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001823
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001824 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1825 /// onAppendCmd and onOutputSuffix.
1826 void EmitHookInvocation(const std::string& Str,
1827 const char* BlockOpen, const char* BlockClose,
1828 unsigned IndentLevel, raw_ostream& O) const
1829 {
1830 StrVector Out;
1831 TokenizeCmdline(Str, Out);
1832
1833 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1834 B != E; ++B) {
1835 const std::string& cmd = *B;
1836
1837 O.indent(IndentLevel) << BlockOpen;
1838
1839 if (cmd.at(0) == '$')
1840 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1841 else
1842 O << '"' << cmd << '"';
1843
1844 O << BlockClose;
1845 }
1846 }
1847
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001848 void onAppendCmd (const DagInit& Dag,
1849 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001850 {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001851 checkNumberOfArguments(&Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001852 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
1853 "vec.push_back(", ");\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001854 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001855
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001856 void onForward (const DagInit& Dag,
1857 unsigned IndentLevel, raw_ostream& O) const
1858 {
1859 checkNumberOfArguments(&Dag, 1);
1860 const std::string& Name = InitPtrToString(Dag.getArg(0));
1861 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1862 IndentLevel, "", O);
1863 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001864
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001865 void onForwardAs (const DagInit& Dag,
1866 unsigned IndentLevel, raw_ostream& O) const
1867 {
1868 checkNumberOfArguments(&Dag, 2);
1869 const std::string& Name = InitPtrToString(Dag.getArg(0));
1870 const std::string& NewName = InitPtrToString(Dag.getArg(1));
1871 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1872 IndentLevel, NewName, O);
1873 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001874
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001875 void onForwardValue (const DagInit& Dag,
1876 unsigned IndentLevel, raw_ostream& O) const
1877 {
1878 checkNumberOfArguments(&Dag, 1);
1879 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001880 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001881
1882 if (D.isParameter()) {
1883 O.indent(IndentLevel) << "vec.push_back("
1884 << D.GenVariableName() << ");\n";
1885 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001886 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001887 O.indent(IndentLevel) << "std::copy(" << D.GenVariableName()
1888 << ".begin(), " << D.GenVariableName()
1889 << ".end(), std::back_inserter(vec));\n";
1890 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001891 }
1892
1893 void onForwardTransformedValue (const DagInit& Dag,
1894 unsigned IndentLevel, raw_ostream& O) const
1895 {
1896 checkNumberOfArguments(&Dag, 2);
1897 const std::string& Name = InitPtrToString(Dag.getArg(0));
1898 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001899 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001900
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001901 O.indent(IndentLevel) << "vec.push_back(" << "hooks::"
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00001902 << Hook << "(" << D.GenVariableName()
1903 << (D.isParameter() ? ".c_str()" : "") << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001904 }
1905
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001906 void onOutputSuffix (const DagInit& Dag,
1907 unsigned IndentLevel, raw_ostream& O) const
1908 {
1909 checkNumberOfArguments(&Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001910 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
1911 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001912 }
1913
1914 void onStopCompilation (const DagInit& Dag,
1915 unsigned IndentLevel, raw_ostream& O) const
1916 {
1917 O.indent(IndentLevel) << "stop_compilation = true;\n";
1918 }
1919
1920
1921 void onUnpackValues (const DagInit& Dag,
1922 unsigned IndentLevel, raw_ostream& O) const
1923 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001924 throw "'unpack_values' is deprecated. "
1925 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001926 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001927
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001928 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001929
1930 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
1931 : OptDescs(OD)
1932 {
1933 if (!staticMembersInitialized_) {
1934 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
1935 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
1936 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
1937 AddHandler("forward", &EmitActionHandlersCallback::onForward);
1938 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001939 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
1940 AddHandler("forward_transformed_value",
1941 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001942 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
1943 AddHandler("stop_compilation",
1944 &EmitActionHandlersCallback::onStopCompilation);
1945 AddHandler("unpack_values",
1946 &EmitActionHandlersCallback::onUnpackValues);
1947
1948 staticMembersInitialized_ = true;
1949 }
1950 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001951
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001952 void operator()(const Init* Statement,
1953 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001954 {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001955 const DagInit& Dag = InitPtrToDag(Statement);
1956 const std::string& ActionName = GetOperatorName(Dag);
1957 Handler h = GetHandler(ActionName);
1958
1959 ((this)->*(h))(Dag, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001960 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001961};
1962
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001963bool IsOutFileIndexCheckRequiredStr (const Init* CmdLine) {
1964 StrVector StrVec;
1965 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1966
1967 for (StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1968 I != E; ++I) {
1969 if (*I == "$OUTFILE")
1970 return false;
1971 }
1972
1973 return true;
1974}
1975
1976class IsOutFileIndexCheckRequiredStrCallback {
1977 bool* ret_;
1978
1979public:
1980 IsOutFileIndexCheckRequiredStrCallback(bool* ret) : ret_(ret)
1981 {}
1982
1983 void operator()(const Init* CmdLine) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001984 // Ignore nested 'case' DAG.
1985 if (typeid(*CmdLine) == typeid(DagInit))
1986 return;
1987
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001988 if (IsOutFileIndexCheckRequiredStr(CmdLine))
1989 *ret_ = true;
1990 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001991
1992 void operator()(const DagInit* Test, unsigned, bool) {
1993 this->operator()(Test);
1994 }
1995 void operator()(const Init* Statement, unsigned) {
1996 this->operator()(Statement);
1997 }
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001998};
1999
2000bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
2001 bool ret = false;
2002 WalkCase(CmdLine, Id(), IsOutFileIndexCheckRequiredStrCallback(&ret));
2003 return ret;
2004}
2005
2006/// IsOutFileIndexCheckRequired - Should we emit an "out_file_index != -1" check
2007/// in EmitGenerateActionMethod() ?
2008bool IsOutFileIndexCheckRequired (Init* CmdLine) {
2009 if (typeid(*CmdLine) == typeid(StringInit))
2010 return IsOutFileIndexCheckRequiredStr(CmdLine);
2011 else
2012 return IsOutFileIndexCheckRequiredCase(CmdLine);
2013}
2014
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002015void EmitGenerateActionMethodHeader(const ToolDescription& D,
2016 bool IsJoin, raw_ostream& O)
2017{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002018 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002019 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002020 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002021 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002022
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002023 O.indent(Indent2) << "bool HasChildren,\n";
2024 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2025 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2026 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2027 O.indent(Indent1) << "{\n";
2028 O.indent(Indent2) << "std::string cmd;\n";
2029 O.indent(Indent2) << "std::vector<std::string> vec;\n";
2030 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2031 O.indent(Indent2) << "const char* output_suffix = \""
2032 << D.OutputSuffix << "\";\n";
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002033}
2034
2035// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2036// Tool::GenerateAction() method.
2037void EmitGenerateActionMethod (const ToolDescription& D,
2038 const OptionDescriptions& OptDescs,
2039 bool IsJoin, raw_ostream& O) {
2040
2041 EmitGenerateActionMethodHeader(D, IsJoin, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002042
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002043 if (!D.CmdLine)
2044 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002045
2046 bool IndexCheckRequired = IsOutFileIndexCheckRequired(D.CmdLine);
2047 O.indent(Indent2) << "int out_file_index"
2048 << (IndexCheckRequired ? " = -1" : "")
2049 << ";\n\n";
2050
2051 // Process the cmd_line property.
2052 if (typeid(*D.CmdLine) == typeid(StringInit))
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002053 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2054 else
2055 EmitCaseConstructHandler(D.CmdLine, Indent2,
2056 EmitCmdLineVecFillCallback(IsJoin, D.Name),
2057 true, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002058
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002059 // For every understood option, emit handling code.
2060 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002061 EmitCaseConstructHandler(D.Actions, Indent2,
2062 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002063 false, OptDescs, O);
2064
2065 O << '\n';
2066 O.indent(Indent2)
2067 << "std::string out_file = OutFilename("
2068 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
2069 O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002070
2071 if (IndexCheckRequired)
2072 O.indent(Indent2) << "if (out_file_index != -1)\n";
2073 O.indent(IndexCheckRequired ? Indent3 : Indent2)
2074 << "vec[out_file_index] = out_file;\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002075
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002076 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002077 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002078 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
2079 O.indent(Indent3) << "vec.insert(vec.end(), "
2080 << SinkOptionName << ".begin(), " << SinkOptionName
2081 << ".end());\n";
2082 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002083 }
2084
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002085 O.indent(Indent2) << "return Action(cmd, vec, stop_compilation, out_file);\n";
2086 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002087}
2088
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002089/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2090/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002091void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2092 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002093 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002094 if (!ToolDesc.isJoin()) {
2095 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
2096 O.indent(Indent2) << "bool HasChildren,\n";
2097 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2098 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2099 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2100 O.indent(Indent1) << "{\n";
2101 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
2102 << " is not a Join tool!\");\n";
2103 O.indent(Indent1) << "}\n\n";
2104 }
2105 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002106 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002107 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002108
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002109 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002110}
2111
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002112/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2113/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002114void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002115 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2116 O.indent(Indent2) << "return InputLanguages_;\n";
2117 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002118
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002119 if (D.OutLanguage.empty())
2120 throw "Tool " + D.Name + " has no 'out_language' property!";
2121
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002122 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2123 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2124 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002125}
2126
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002127/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002128void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002129 O.indent(Indent1) << "const char* Name() const {\n";
2130 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2131 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002132}
2133
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002134/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2135/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002136void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002137 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002138 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002139 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002140 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002141 O.indent(Indent2) << "return false;\n";
2142 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002143}
2144
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002145/// EmitStaticMemberDefinitions - Emit static member definitions for a
2146/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002147void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002148 if (D.InLanguage.empty())
2149 throw "Tool " + D.Name + " has no 'in_language' property!";
2150
2151 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2152 for (StrVector::const_iterator B = D.InLanguage.begin(),
2153 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002154 O << '\"' << *B << "\", ";
2155 O << "0};\n\n";
2156}
2157
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002158/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002159void EmitToolClassDefinition (const ToolDescription& D,
2160 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002161 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002162 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002163 return;
2164
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002165 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002166 O << "class " << D.Name << " : public ";
2167 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002168 O << "JoinTool";
2169 else
2170 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002171
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002172 O << "{\nprivate:\n";
2173 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002174
2175 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002176 EmitNameMethod(D, O);
2177 EmitInOutLanguageMethods(D, O);
2178 EmitIsJoinMethod(D, O);
2179 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002180
2181 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002182 O << "};\n";
2183
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002184 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002185
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002186}
2187
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002188/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002189/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002190void EmitOptionDefinitions (const OptionDescriptions& descs,
2191 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002192 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002193{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002194 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002195
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002196 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002197 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002198 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002199 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002200
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002201 if (val.Type == OptionType::Alias) {
2202 Aliases.push_back(val);
2203 continue;
2204 }
2205
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002206 if (val.isExtern())
2207 O << "extern ";
2208
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002209 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002210 << val.GenVariableName();
2211
2212 if (val.isExtern()) {
2213 O << ";\n";
2214 continue;
2215 }
2216
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002217 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002218
2219 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2220 O << ", cl::Prefix";
2221
2222 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002223 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002224 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002225 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002226 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002227 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002228 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002229 O << ", cl::OneOrMore";
2230 }
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002231 else if (val.isOptional() && val.isList()) {
2232 O << ", cl::Optional";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002233 }
2234
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002235 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002236 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002237 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002238 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002239
2240 if (val.isCommaSeparated())
2241 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002242
2243 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002244 O << ", cl::multi_val(" << val.MultiVal << ')';
2245
2246 if (val.InitVal) {
2247 const std::string& str = val.InitVal->getAsString();
2248 O << ", cl::init(" << str << ')';
2249 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002250
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002251 if (!val.Help.empty())
2252 O << ", cl::desc(\"" << val.Help << "\")";
2253
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002254 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002255 }
2256
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002257 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002258 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002259 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002260 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002261
2262 O << val.GenTypeDeclaration() << ' '
2263 << val.GenVariableName()
2264 << "(\"" << val.Name << '\"';
2265
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002266 const OptionDescription& D = descs.FindOption(val.Help);
2267 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002268
2269 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2270 }
2271
2272 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002273 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002274 O << (HasExterns ? "extern cl" : "cl")
2275 << "::list<std::string> " << SinkOptionName
2276 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002277
2278 O << '\n';
2279}
2280
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002281/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002282/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002283class EmitPreprocessOptionsCallback : ActionHandlingCallbackBase {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002284 const OptionDescriptions& OptDescs_;
2285
2286 void onUnsetOption(Init* i, unsigned IndentLevel, raw_ostream& O) {
2287 const std::string& OptName = InitPtrToString(i);
2288 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002289
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002290 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002291 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2292 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002293 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002294 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2295 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002296 else if (OptDesc.isList()) {
2297 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2298 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002299 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002300 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002301 }
2302 }
2303
2304 void processDag(const Init* I, unsigned IndentLevel, raw_ostream& O)
2305 {
2306 const DagInit& d = InitPtrToDag(I);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002307 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002308
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002309 if (OpName == "warning") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002310 this->onWarningDag(d, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002311 }
2312 else if (OpName == "error") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002313 this->onWarningDag(d, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002314 }
2315 else if (OpName == "unset_option") {
2316 checkNumberOfArguments(&d, 1);
2317 Init* I = d.getArg(0);
2318 if (typeid(*I) == typeid(ListInit)) {
2319 const ListInit& DagList = *static_cast<const ListInit*>(I);
2320 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
2321 B != E; ++B)
2322 this->onUnsetOption(*B, IndentLevel, O);
2323 }
2324 else {
2325 this->onUnsetOption(I, IndentLevel, O);
2326 }
2327 }
2328 else {
2329 throw "Unknown operator in the option preprocessor: '" + OpName + "'!"
2330 "\nOnly 'warning', 'error' and 'unset_option' are allowed.";
2331 }
2332 }
2333
2334public:
2335
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002336 void operator()(const Init* I, unsigned IndentLevel, raw_ostream& O) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002337 this->processDag(I, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002338 }
2339
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002340 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002341 : OptDescs_(OptDescs)
2342 {}
2343};
2344
2345/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2346void EmitPreprocessOptions (const RecordKeeper& Records,
2347 const OptionDescriptions& OptDecs, raw_ostream& O)
2348{
2349 O << "void PreprocessOptionsLocal() {\n";
2350
2351 const RecordVector& OptionPreprocessors =
2352 Records.getAllDerivedDefinitions("OptionPreprocessor");
2353
2354 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2355 E = OptionPreprocessors.end(); B!=E; ++B) {
2356 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002357 EmitCaseConstructHandler(Case, Indent1,
2358 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002359 false, OptDecs, O);
2360 }
2361
2362 O << "}\n\n";
2363}
2364
2365/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002366void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002367{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002368 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002369
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002370 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002371 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002372
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002373 // It is allowed for a plugin to have no language map.
2374 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002375
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002376 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2377 if (!LangsToSuffixesList)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00002378 throw "Error in the language map definition!";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002379
2380 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002381 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002382
2383 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2384 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2385
2386 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002387 O.indent(Indent1) << "langMap[\""
2388 << InitPtrToString(Suffixes->getElement(i))
2389 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002390 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002391 }
2392
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002393 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002394}
2395
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002396/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2397/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002398void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002399 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002400 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002401 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002402
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002403 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002404 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002405 }
2406 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002407 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002408 }
2409 else if (OpName == "error") {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002410 checkNumberOfArguments(&d, 1);
2411 O.indent(IndentLevel) << "throw std::runtime_error(\""
2412 << InitPtrToString(d.getArg(0))
2413 << "\");\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002414 return;
2415 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002416 else {
2417 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002418 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002419 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002420
2421 if (d.getNumArgs() > 0)
2422 O << InitPtrToInt(d.getArg(0)) << ";\n";
2423 else
2424 O << "2;\n";
2425
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002426}
2427
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002428/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002429void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002430 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002431 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002432
2433 // Class constructor.
2434 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002435 << "public:\n";
2436 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2437 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002438
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002439 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002440 O.indent(Indent1)
2441 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2442 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002443
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002444 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002445 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002446
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002447 O.indent(Indent2) << "return ret;\n";
2448 O.indent(Indent1) << "};\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002449}
2450
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002451/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002452void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002453 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002454 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002455 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002456 for (RecordVector::const_iterator B = EdgeVector.begin(),
2457 E = EdgeVector.end(); B != E; ++B) {
2458 const Record* Edge = *B;
2459 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002460 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002461
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002462 if (!isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002463 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002464 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002465 }
2466}
2467
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002468/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002469/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002470void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002471 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002472 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002473{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002474 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002475
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002476 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2477 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002478 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002479
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002480 O << '\n';
2481
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002482 // Insert edges.
2483
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002484 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002485 for (RecordVector::const_iterator B = EdgeVector.begin(),
2486 E = EdgeVector.end(); B != E; ++B) {
2487 const Record* Edge = *B;
2488 const std::string& NodeA = Edge->getValueAsString("a");
2489 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002490 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002491
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002492 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002493
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002494 if (isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002495 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002496 else
2497 O << "new Edge" << i << "()";
2498
2499 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002500 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002501 }
2502
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002503 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002504}
2505
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002506/// HookInfo - Information about the hook type and number of arguments.
2507struct HookInfo {
2508
2509 // A hook can either have a single parameter of type std::vector<std::string>,
2510 // or NumArgs parameters of type const char*.
2511 enum HookType { ListHook, ArgHook };
2512
2513 HookType Type;
2514 unsigned NumArgs;
2515
2516 HookInfo() : Type(ArgHook), NumArgs(1)
2517 {}
2518
2519 HookInfo(HookType T) : Type(T), NumArgs(1)
2520 {}
2521
2522 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2523 {}
2524};
2525
2526typedef llvm::StringMap<HookInfo> HookInfoMap;
2527
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002528/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002529/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002530/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002531class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002532 HookInfoMap& HookNames_;
2533 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002534public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002535 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2536 : HookNames_(HookNames), OptDescs_(OptDescs)
2537 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002538
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002539 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002540 const std::string& Name = GetOperatorName(Dag);
2541
2542 if (Name == "forward_transformed_value") {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002543 checkNumberOfArguments(Dag, 2);
2544 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2545 const std::string& HookName = InitPtrToString(Dag.getArg(1));
2546 const OptionDescription& D = OptDescs_.FindOption(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002547
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002548 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2549 : HookInfo::ArgHook);
2550 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002551 else if (Name == "append_cmd" || Name == "output_suffix") {
2552 checkNumberOfArguments(Dag, 1);
2553 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2554 }
2555 }
2556
2557 void onCmdLine(const std::string& Cmd) {
2558 StrVector cmds;
2559 TokenizeCmdline(Cmd, cmds);
2560
2561 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2562 B != E; ++B) {
2563 const std::string& cmd = *B;
2564
2565 if (cmd == "$CALL") {
2566 unsigned NumArgs = 0;
2567 checkedIncrement(B, E, "Syntax error in $CALL invocation!");
2568 const std::string& HookName = *B;
2569
2570 if (HookName.at(0) == ')')
2571 throw "$CALL invoked with no arguments!";
2572
2573 while (++B != E && B->at(0) != ')') {
2574 ++NumArgs;
2575 }
2576
2577 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2578
2579 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2580 H->second.Type != HookInfo::ArgHook)
2581 throw "Overloading of hooks is not allowed. Overloaded hook: "
2582 + HookName;
2583 else
2584 HookNames_[HookName] = HookInfo(NumArgs);
2585 }
2586 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002587 }
2588
2589 void operator()(const Init* Arg) {
2590
2591 // We're invoked on an action (either a dag or a dag list).
2592 if (typeid(*Arg) == typeid(DagInit)) {
2593 const DagInit& Dag = InitPtrToDag(Arg);
2594 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002595 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002596 }
2597 else if (typeid(*Arg) == typeid(ListInit)) {
2598 const ListInit& List = InitPtrToList(Arg);
2599 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2600 ++B) {
2601 const DagInit& Dag = InitPtrToDag(*B);
2602 this->onAction(Dag);
2603 }
2604 return;
2605 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002606
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002607 // We're invoked on a command line.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002608 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002609 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002610
2611 void operator()(const DagInit* Test, unsigned, bool) {
2612 this->operator()(Test);
2613 }
2614 void operator()(const Init* Statement, unsigned) {
2615 this->operator()(Statement);
2616 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002617};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002618
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002619/// FillInHookNames - Actually extract the hook names from all command
2620/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002621void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002622 const OptionDescriptions& OptDescs,
2623 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002624{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002625 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002626 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2627 E = ToolDescs.end(); B != E; ++B) {
2628 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002629
2630 // Look for 'forward_transformed_value' in 'actions'.
2631 if (D.Actions)
2632 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2633
2634 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002635 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002636 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002637 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002638 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002639 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002640 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002641 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002642 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002643 }
2644}
2645
2646/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2647/// property records and emit hook function declaration for each
2648/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002649void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2650 const OptionDescriptions& OptDescs, raw_ostream& O) {
2651 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002652
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002653 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002654 if (HookNames.empty())
2655 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002656
2657 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002658 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002659 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002660 const char* HookName = B->first();
2661 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002662
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002663 O.indent(Indent1) << "std::string " << HookName << "(";
2664
2665 if (Info.Type == HookInfo::ArgHook) {
2666 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2667 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2668 }
2669 }
2670 else {
2671 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002672 }
2673
2674 O <<");\n";
2675 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002676 O << "}\n\n";
2677}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002678
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002679/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002680void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002681 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2682 O.indent(Indent1) << "int Priority() const { return "
2683 << Priority << "; }\n\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002684 O.indent(Indent1) << "void PreprocessOptions() const\n";
2685 O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002686 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2687 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2688 O.indent(Indent1)
2689 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2690 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2691 << "};\n\n"
2692 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002693}
2694
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002695/// EmitIncludes - Emit necessary #include directives and some
2696/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002697void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002698 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2699 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002700 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002701 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2702 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002703
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002704 << "#include \"llvm/Support/CommandLine.h\"\n"
2705 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002706
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002707 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002708 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002709 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002710 << "#include <stdexcept>\n\n"
2711
2712 << "using namespace llvm;\n"
2713 << "using namespace llvmc;\n\n"
2714
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002715 << "extern cl::opt<std::string> OutputFilename;\n\n"
2716
2717 << "inline const char* checkCString(const char* s)\n"
2718 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002719}
2720
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002721
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002722/// PluginData - Holds all information about a plugin.
2723struct PluginData {
2724 OptionDescriptions OptDescs;
2725 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002726 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002727 ToolDescriptions ToolDescs;
2728 RecordVector Edges;
2729 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002730};
2731
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002732/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002733/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002734bool HasSink(const ToolDescriptions& ToolDescs) {
2735 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2736 E = ToolDescs.end(); B != E; ++B)
2737 if ((*B)->isSink())
2738 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002739
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002740 return false;
2741}
2742
2743/// HasExterns - Go through the list of option descriptions and check
2744/// if there are any external options.
2745bool HasExterns(const OptionDescriptions& OptDescs) {
2746 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2747 E = OptDescs.end(); B != E; ++B)
2748 if (B->second.isExtern())
2749 return true;
2750
2751 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002752}
2753
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002754/// CollectPluginData - Collect tool and option properties,
2755/// compilation graph edges and plugin priority from the parse tree.
2756void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2757 // Collect option properties.
2758 const RecordVector& OptionLists =
2759 Records.getAllDerivedDefinitions("OptionList");
2760 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2761 Data.OptDescs);
2762
2763 // Collect tool properties.
2764 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2765 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2766 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002767 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002768
2769 // Collect compilation graph edges.
2770 const RecordVector& CompilationGraphs =
2771 Records.getAllDerivedDefinitions("CompilationGraph");
2772 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2773 Data.Edges);
2774
2775 // Calculate the priority of this plugin.
2776 const RecordVector& Priorities =
2777 Records.getAllDerivedDefinitions("PluginPriority");
2778 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002779}
2780
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002781/// CheckPluginData - Perform some sanity checks on the collected data.
2782void CheckPluginData(PluginData& Data) {
2783 // Filter out all tools not mentioned in the compilation graph.
2784 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002785
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002786 // Typecheck the compilation graph.
2787 TypecheckGraph(Data.Edges, Data.ToolDescs);
2788
2789 // Check that there are no options without side effects (specified
2790 // only in the OptionList).
2791 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2792
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002793}
2794
Daniel Dunbar1a551802009-07-03 00:10:29 +00002795void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002796 // Emit file header.
2797 EmitIncludes(O);
2798
2799 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002800 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002801
2802 // Emit hook declarations.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002803 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002804
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002805 O << "namespace {\n\n";
2806
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002807 // Emit PreprocessOptionsLocal() function.
2808 EmitPreprocessOptions(Records, Data.OptDescs, O);
2809
2810 // Emit PopulateLanguageMapLocal() function
2811 // (language map maps from file extensions to language names).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002812 EmitPopulateLanguageMap(Records, O);
2813
2814 // Emit Tool classes.
2815 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2816 E = Data.ToolDescs.end(); B!=E; ++B)
2817 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2818
2819 // Emit Edge# classes.
2820 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2821
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002822 // Emit PopulateCompilationGraphLocal() function.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002823 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2824
2825 // Emit code for plugin registration.
2826 EmitRegisterPlugin(Data.Priority, O);
2827
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002828 O << "} // End anonymous namespace.\n\n";
2829
2830 // Force linkage magic.
2831 O << "namespace llvmc {\n";
2832 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2833 O << "}\n";
2834
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002835 // EOF
2836}
2837
2838
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002839// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00002840}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002841
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002842/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002843void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002844 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002845 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002846
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002847 CollectPluginData(Records, Data);
2848 CheckPluginData(Data);
2849
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +00002850 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002851 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002852
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002853 } catch (std::exception& Error) {
2854 throw Error.what() + std::string(" - usually this means a syntax error.");
2855 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002856}