blob: 42cedf2c69069cb2d923dab3025f8ba76afbb8e6 [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"
Mikhail Glushenkove0b65702009-12-23 12:49:30 +000020
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000021#include <algorithm>
22#include <cassert>
23#include <functional>
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +000024#include <stdexcept>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000025#include <string>
Chris Lattner32a9e7a2008-06-04 04:46:14 +000026#include <typeinfo>
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +000027
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000028using namespace llvm;
29
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000030namespace {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
32//===----------------------------------------------------------------------===//
33/// Typedefs
34
35typedef std::vector<Record*> RecordVector;
36typedef std::vector<std::string> StrVector;
37
38//===----------------------------------------------------------------------===//
39/// Constants
40
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +000041// Indentation.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000042const unsigned TabWidth = 4;
43const unsigned Indent1 = TabWidth*1;
44const unsigned Indent2 = TabWidth*2;
45const unsigned Indent3 = TabWidth*3;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000046
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000047// Default help string.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000048const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000049
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000050// Name for the "sink" option.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000051const char * const SinkOptionName = "AutoGeneratedSinkOption";
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) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +000090 return D.getOperator()->getAsString();
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000091}
92
Mikhail Glushenkove0b65702009-12-23 12:49:30 +000093/// CheckBooleanConstant - Check that the provided value is a boolean constant.
94void CheckBooleanConstant(const Init* I) {
95 const DefInit& val = dynamic_cast<const DefInit&>(*I);
96 const std::string& str = val.getAsString();
97
98 if (str != "true" && str != "false") {
99 throw "Incorrect boolean value: '" + str +
100 "': must be either 'true' or 'false'";
101 }
102}
103
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000104// CheckNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +0000105// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000106void CheckNumberOfArguments (const DagInit& d, unsigned minArgs) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000107 if (d.getNumArgs() < minArgs)
108 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +0000109}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000110
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000111// IsDagEmpty - is this DAG marked with an empty marker?
112bool IsDagEmpty (const DagInit& d) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000113 return GetOperatorName(d) == "empty_dag_marker";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000114}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000115
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000116// EscapeVariableName - Escape commas and other symbols not allowed
117// in the C++ variable names. Makes it possible to use options named
118// like "Wa," (useful for prefix options).
119std::string EscapeVariableName(const std::string& Var) {
120 std::string ret;
121 for (unsigned i = 0; i != Var.size(); ++i) {
122 char cur_char = Var[i];
123 if (cur_char == ',') {
124 ret += "_comma_";
125 }
126 else if (cur_char == '+') {
127 ret += "_plus_";
128 }
129 else if (cur_char == '-') {
130 ret += "_dash_";
131 }
132 else {
133 ret.push_back(cur_char);
134 }
135 }
136 return ret;
137}
138
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000139/// OneOf - Does the input string contain this character?
140bool OneOf(const char* lst, char c) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000141 while (*lst) {
142 if (*lst++ == c)
143 return true;
144 }
145 return false;
146}
147
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000148template <class I, class S>
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000149void CheckedIncrement(I& P, I E, S ErrorString) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000150 ++P;
151 if (P == E)
152 throw ErrorString;
153}
154
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000155// apply is needed because C++'s syntax doesn't let us construct a function
156// object and call it in the same statement.
157template<typename F, typename T0>
158void apply(F Fun, T0& Arg0) {
159 return Fun(Arg0);
160}
161
162template<typename F, typename T0, typename T1>
163void apply(F Fun, T0& Arg0, T1& Arg1) {
164 return Fun(Arg0, Arg1);
165}
166
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000167//===----------------------------------------------------------------------===//
168/// Back-end specific code
169
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000170
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000171/// OptionType - One of six different option types. See the
172/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000173namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000174
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000175 enum OptionType { Alias, Switch, Parameter, ParameterList,
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000176 Prefix, PrefixList};
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000177
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000178 bool IsAlias(OptionType t) {
179 return (t == Alias);
180 }
181
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000182 bool IsList (OptionType t) {
183 return (t == ParameterList || t == PrefixList);
184 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000185
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000186 bool IsSwitch (OptionType t) {
187 return (t == Switch);
188 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000189
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000190 bool IsParameter (OptionType t) {
191 return (t == Parameter || t == Prefix);
192 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000193
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000194}
195
196OptionType::OptionType stringToOptionType(const std::string& T) {
197 if (T == "alias_option")
198 return OptionType::Alias;
199 else if (T == "switch_option")
200 return OptionType::Switch;
201 else if (T == "parameter_option")
202 return OptionType::Parameter;
203 else if (T == "parameter_list_option")
204 return OptionType::ParameterList;
205 else if (T == "prefix_option")
206 return OptionType::Prefix;
207 else if (T == "prefix_list_option")
208 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000209 else
210 throw "Unknown option type: " + T + '!';
211}
212
213namespace OptionDescriptionFlags {
214 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000215 ReallyHidden = 0x4, Extern = 0x8,
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000216 OneOrMore = 0x10, Optional = 0x20,
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000217 CommaSeparated = 0x40 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000218}
219
220/// OptionDescription - Represents data contained in a single
221/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000222struct OptionDescription {
223 OptionType::OptionType Type;
224 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000225 unsigned Flags;
226 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000227 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000228 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000229
230 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000231 const std::string& n = "",
232 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000233 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000234 {}
235
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000236 /// GenTypeDeclaration - Returns the C++ variable type of this
237 /// option.
238 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000239
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000240 /// GenVariableName - Returns the variable name used in the
241 /// generated C++ code.
242 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000243
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000244 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000245 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000246
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000247 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000248
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000249 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000250
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000251 bool isMultiVal() const;
252
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000253 bool isCommaSeparated() const;
254 void setCommaSeparated();
255
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000256 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000257 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000258
259 bool isRequired() const;
260 void setRequired();
261
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000262 bool isOneOrMore() const;
263 void setOneOrMore();
264
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000265 bool isOptional() const;
266 void setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000267
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000268 bool isHidden() const;
269 void setHidden();
270
271 bool isReallyHidden() const;
272 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000273
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000274 bool isSwitch() const
275 { return OptionType::IsSwitch(this->Type); }
276
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000277 bool isParameter() const
278 { return OptionType::IsParameter(this->Type); }
279
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000280 bool isList() const
281 { return OptionType::IsList(this->Type); }
282
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000283};
284
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000285void OptionDescription::Merge (const OptionDescription& other)
286{
287 if (other.Type != Type)
288 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000289
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000290 if (Help == other.Help || Help == DefaultHelpString)
291 Help = other.Help;
292 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000293 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000294 " defined for option " + Name + "\n";
295 }
296
297 Flags |= other.Flags;
298}
299
300bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000301 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000302}
303
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000304bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000305 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000306}
307
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000308bool OptionDescription::isCommaSeparated() const {
309 return Flags & OptionDescriptionFlags::CommaSeparated;
310}
311void OptionDescription::setCommaSeparated() {
312 Flags |= OptionDescriptionFlags::CommaSeparated;
313}
314
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000315bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000316 return Flags & OptionDescriptionFlags::Extern;
317}
318void OptionDescription::setExtern() {
319 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000320}
321
322bool OptionDescription::isRequired() const {
323 return Flags & OptionDescriptionFlags::Required;
324}
325void OptionDescription::setRequired() {
326 Flags |= OptionDescriptionFlags::Required;
327}
328
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000329bool OptionDescription::isOneOrMore() const {
330 return Flags & OptionDescriptionFlags::OneOrMore;
331}
332void OptionDescription::setOneOrMore() {
333 Flags |= OptionDescriptionFlags::OneOrMore;
334}
335
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000336bool OptionDescription::isOptional() const {
337 return Flags & OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000338}
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000339void OptionDescription::setOptional() {
340 Flags |= OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000341}
342
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000343bool OptionDescription::isHidden() const {
344 return Flags & OptionDescriptionFlags::Hidden;
345}
346void OptionDescription::setHidden() {
347 Flags |= OptionDescriptionFlags::Hidden;
348}
349
350bool OptionDescription::isReallyHidden() const {
351 return Flags & OptionDescriptionFlags::ReallyHidden;
352}
353void OptionDescription::setReallyHidden() {
354 Flags |= OptionDescriptionFlags::ReallyHidden;
355}
356
357const char* OptionDescription::GenTypeDeclaration() const {
358 switch (Type) {
359 case OptionType::Alias:
360 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000361 case OptionType::PrefixList:
362 case OptionType::ParameterList:
363 return "cl::list<std::string>";
364 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000365 return "cl::opt<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000366 case OptionType::Parameter:
367 case OptionType::Prefix:
368 default:
369 return "cl::opt<std::string>";
370 }
371}
372
373std::string OptionDescription::GenVariableName() const {
374 const std::string& EscapedName = EscapeVariableName(Name);
375 switch (Type) {
376 case OptionType::Alias:
377 return "AutoGeneratedAlias_" + EscapedName;
378 case OptionType::PrefixList:
379 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000380 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000381 case OptionType::Switch:
382 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000383 case OptionType::Prefix:
384 case OptionType::Parameter:
385 default:
386 return "AutoGeneratedParameter_" + EscapedName;
387 }
388}
389
390/// OptionDescriptions - An OptionDescription array plus some helper
391/// functions.
392class OptionDescriptions {
393 typedef StringMap<OptionDescription> container_type;
394
395 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000396 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000397
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000398public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000399 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000400 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000401
402 // Wrappers for FindOption that throw an exception in case the option has a
403 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000404 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000405 const OptionDescription& FindParameter(const std::string& OptName) const;
406 const OptionDescription& FindList(const std::string& OptName) const;
407 const OptionDescription&
408 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000409
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000410 /// insertDescription - Insert new OptionDescription into
411 /// OptionDescriptions list
412 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000413
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000414 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000415 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000416 const_iterator begin() const { return Descriptions.begin(); }
417 const_iterator end() const { return Descriptions.end(); }
418};
419
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000420const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000421OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000422 const_iterator I = Descriptions.find(OptName);
423 if (I != Descriptions.end())
424 return I->second;
425 else
426 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000427}
428
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000429const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000430OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000431 const OptionDescription& OptDesc = this->FindOption(OptName);
432 if (!OptDesc.isSwitch())
433 throw OptName + ": incorrect option type - should be a switch!";
434 return OptDesc;
435}
436
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000437const OptionDescription&
438OptionDescriptions::FindList(const std::string& OptName) const {
439 const OptionDescription& OptDesc = this->FindOption(OptName);
440 if (!OptDesc.isList())
441 throw OptName + ": incorrect option type - should be a list!";
442 return OptDesc;
443}
444
445const OptionDescription&
446OptionDescriptions::FindParameter(const std::string& OptName) const {
447 const OptionDescription& OptDesc = this->FindOption(OptName);
448 if (!OptDesc.isParameter())
449 throw OptName + ": incorrect option type - should be a parameter!";
450 return OptDesc;
451}
452
453const OptionDescription&
454OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
455 const OptionDescription& OptDesc = this->FindOption(OptName);
456 if (!OptDesc.isList() && !OptDesc.isParameter())
457 throw OptName
458 + ": incorrect option type - should be a list or parameter!";
459 return OptDesc;
460}
461
462void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000463 container_type::iterator I = Descriptions.find(o.Name);
464 if (I != Descriptions.end()) {
465 OptionDescription& D = I->second;
466 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000467 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000468 else {
469 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000470 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000471}
472
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000473/// HandlerTable - A base class for function objects implemented as
474/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000475template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000476class HandlerTable {
477protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000478 // Implementation details.
479
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000480 /// HandlerMap - A map from property names to property handlers
481 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000482
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000483 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000484 static bool staticMembersInitialized_;
485
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000486public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000487
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000488 Handler GetHandler (const std::string& HandlerName) const {
489 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000490
491 if (method != Handlers_.end()) {
492 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000493 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000494 }
495 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000496 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000497 }
498 }
499
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000500 void AddHandler(const char* Property, Handler H) {
501 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000502 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000503
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000504};
505
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000506template <class Handler, class FunctionObject>
507Handler GetHandler(FunctionObject* Obj, const DagInit& Dag) {
508 const std::string& HandlerName = GetOperatorName(Dag);
509 return Obj->GetHandler(HandlerName);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000510}
511
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000512template <class FunctionObject>
513void InvokeDagInitHandler(FunctionObject* Obj, Init* I) {
514 typedef void (FunctionObject::*Handler) (const DagInit&);
515
516 const DagInit& Dag = InitPtrToDag(I);
517 Handler h = GetHandler<Handler>(Obj, Dag);
518
519 ((Obj)->*(h))(Dag);
520}
521
522template <class FunctionObject>
523void InvokeDagInitHandler(const FunctionObject* const Obj,
524 const Init* I, unsigned IndentLevel, raw_ostream& O)
525{
526 typedef void (FunctionObject::*Handler)
527 (const DagInit&, unsigned IndentLevel, raw_ostream& O) const;
528
529 const DagInit& Dag = InitPtrToDag(I);
530 Handler h = GetHandler<Handler>(Obj, Dag);
531
532 ((Obj)->*(h))(Dag, IndentLevel, O);
533}
534
535
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000536template <typename H>
537typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
538
539template <typename H>
540bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000541
542
543/// CollectOptionProperties - Function object for iterating over an
544/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000545class CollectOptionProperties;
546typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000547(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000548
549class CollectOptionProperties
550: public HandlerTable<CollectOptionPropertiesHandler>
551{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000552private:
553
554 /// optDescs_ - OptionDescriptions table. This is where the
555 /// information is stored.
556 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000557
558public:
559
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000560 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000561 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000562 {
563 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000564 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000565 AddHandler("help", &CollectOptionProperties::onHelp);
566 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000567 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000568 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
569 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000570 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
571 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000572 AddHandler("optional", &CollectOptionProperties::onOptional);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000573 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000574
575 staticMembersInitialized_ = true;
576 }
577 }
578
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000579 /// operator() - Just forwards to the corresponding property
580 /// handler.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000581 void operator() (Init* I) {
582 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000583 }
584
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000585private:
586
587 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000588 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000589
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000590 void onExtern (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000591 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000592 optDesc_.setExtern();
593 }
594
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000595 void onHelp (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000596 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000597 optDesc_.Help = InitPtrToString(d.getArg(0));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000598 }
599
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000600 void onHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000601 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000602 optDesc_.setHidden();
603 }
604
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000605 void onReallyHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000606 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000607 optDesc_.setReallyHidden();
608 }
609
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000610 void onCommaSeparated (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000611 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000612 if (!optDesc_.isList())
613 throw "'comma_separated' is valid only on list options!";
614 optDesc_.setCommaSeparated();
615 }
616
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000617 void onRequired (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000618 CheckNumberOfArguments(d, 0);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000619 if (optDesc_.isOneOrMore() || optDesc_.isOptional())
620 throw "Only one of (required), (optional) or "
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000621 "(one_or_more) properties is allowed!";
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000622 optDesc_.setRequired();
623 }
624
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000625 void onInit (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000626 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000627 Init* i = d.getArg(0);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000628 const std::string& str = i->getAsString();
629
630 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
631 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
632
633 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000634 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000635
636 optDesc_.InitVal = i;
637 }
638
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000639 void onOneOrMore (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000640 CheckNumberOfArguments(d, 0);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000641 if (optDesc_.isRequired() || optDesc_.isOptional())
642 throw "Only one of (required), (optional) or "
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000643 "(one_or_more) properties is allowed!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000644 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000645 llvm::errs() << "Warning: specifying the 'one_or_more' property "
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000646 "on a non-list option will have no effect.\n";
647 optDesc_.setOneOrMore();
648 }
649
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000650 void onOptional (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000651 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000652 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000653 throw "Only one of (required), (optional) or "
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000654 "(one_or_more) properties is allowed!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000655 if (!OptionType::IsList(optDesc_.Type))
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000656 llvm::errs() << "Warning: specifying the 'optional' property"
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000657 "on a non-list option will have no effect.\n";
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000658 optDesc_.setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000659 }
660
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000661 void onMultiVal (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000662 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000663 int val = InitPtrToInt(d.getArg(0));
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000664 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000665 throw "Error in the 'multi_val' property: "
666 "the value must be greater than 1!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000667 if (!OptionType::IsList(optDesc_.Type))
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000668 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000669 optDesc_.MultiVal = val;
670 }
671
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000672};
673
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000674/// AddOption - A function object that is applied to every option
675/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000676class AddOption {
677private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000678 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000679
680public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000681 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000682 {}
683
684 void operator()(const Init* i) {
685 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000686 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000687
688 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000689 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000690 const std::string& Name = InitPtrToString(d.getArg(0));
691
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000692 OptionDescription OD(Type, Name);
693
694 if (!OD.isExtern())
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000695 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000696
697 if (OD.isAlias()) {
698 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000699 OD.Help = InitPtrToString(d.getArg(1));
700 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000701 else if (!OD.isExtern()) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000702 processOptionProperties(d, OD);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000703 }
704 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000705 }
706
707private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000708 /// processOptionProperties - Go through the list of option
709 /// properties and call a corresponding handler for each.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000710 static void processOptionProperties (const DagInit& d, OptionDescription& o) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000711 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000712 DagInit::const_arg_iterator B = d.arg_begin();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000713 // Skip the first argument: it's always the option name.
714 ++B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000715 std::for_each(B, d.arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000716 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000717
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000718};
719
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000720/// CollectOptionDescriptions - Collects option properties from all
721/// OptionLists.
722void CollectOptionDescriptions (RecordVector::const_iterator B,
723 RecordVector::const_iterator E,
724 OptionDescriptions& OptDescs)
725{
726 // For every OptionList:
727 for (; B!=E; ++B) {
728 RecordVector::value_type T = *B;
729 // Throws an exception if the value does not exist.
730 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000731
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000732 // For every option description in this list:
733 // collect the information and
734 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
735 }
736}
737
738// Tool information record
739
740namespace ToolFlags {
741 enum ToolFlags { Join = 0x1, Sink = 0x2 };
742}
743
744struct ToolDescription : public RefCountedBase<ToolDescription> {
745 std::string Name;
746 Init* CmdLine;
747 Init* Actions;
748 StrVector InLanguage;
749 std::string OutLanguage;
750 std::string OutputSuffix;
751 unsigned Flags;
752
753 // Various boolean properties
754 void setSink() { Flags |= ToolFlags::Sink; }
755 bool isSink() const { return Flags & ToolFlags::Sink; }
756 void setJoin() { Flags |= ToolFlags::Join; }
757 bool isJoin() const { return Flags & ToolFlags::Join; }
758
759 // Default ctor here is needed because StringMap can only store
760 // DefaultConstructible objects
761 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
762 ToolDescription (const std::string& n)
763 : Name(n), CmdLine(0), Actions(0), Flags(0)
764 {}
765};
766
767/// ToolDescriptions - A list of Tool information records.
768typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
769
770
771/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000772/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000773
774class CollectToolProperties;
775typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000776(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000777
778class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
779{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000780private:
781
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000782 /// toolDesc_ - Properties of the current Tool. This is where the
783 /// information is stored.
784 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000785
786public:
787
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000788 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000789 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000790 {
791 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000792
793 AddHandler("actions", &CollectToolProperties::onActions);
794 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
795 AddHandler("in_language", &CollectToolProperties::onInLanguage);
796 AddHandler("join", &CollectToolProperties::onJoin);
797 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
798 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
799 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000800
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000801 staticMembersInitialized_ = true;
802 }
803 }
804
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000805 void operator() (Init* I) {
806 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000807 }
808
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000809private:
810
811 /// Property handlers --
812 /// Functions that extract information about tool properties from
813 /// DAG representation.
814
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000815 void onActions (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000816 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000817 Init* Case = d.getArg(0);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000818 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000819 GetOperatorName(static_cast<DagInit&>(*Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000820 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000821 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000822 }
823
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000824 void onCmdLine (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000825 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000826 toolDesc_.CmdLine = d.getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000827 }
828
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000829 void onInLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000830 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000831 Init* arg = d.getArg(0);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000832
833 // Find out the argument's type.
834 if (typeid(*arg) == typeid(StringInit)) {
835 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000836 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000837 }
838 else {
839 // It's a list.
840 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000841 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000842
843 // Copy strings to the output vector.
844 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
845 B != E; ++B) {
846 out.push_back(InitPtrToString(*B));
847 }
848
849 // Remove duplicates.
850 std::sort(out.begin(), out.end());
851 StrVector::iterator newE = std::unique(out.begin(), out.end());
852 out.erase(newE, out.end());
853 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000854 }
855
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000856 void onJoin (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000857 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000858 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000859 }
860
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000861 void onOutLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000862 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000863 toolDesc_.OutLanguage = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000864 }
865
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000866 void onOutputSuffix (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000867 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000868 toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000869 }
870
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000871 void onSink (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000872 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000873 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000874 }
875
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000876};
877
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000878/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000879/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000880/// CollectToolProperties function object).
881void CollectToolDescriptions (RecordVector::const_iterator B,
882 RecordVector::const_iterator E,
883 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000884{
885 // Iterate over a properties list of every Tool definition
886 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000887 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000888 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000889 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000890
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000891 IntrusiveRefCntPtr<ToolDescription>
892 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000893
894 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000895 CollectToolProperties(*ToolDesc));
896 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000897 }
898}
899
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000900/// FillInEdgeVector - Merge all compilation graph definitions into
901/// one single edge list.
902void FillInEdgeVector(RecordVector::const_iterator B,
903 RecordVector::const_iterator E, RecordVector& Out) {
904 for (; B != E; ++B) {
905 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000906
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000907 for (unsigned i = 0; i < edges->size(); ++i)
908 Out.push_back(edges->getElementAsRecord(i));
909 }
910}
911
912/// CalculatePriority - Calculate the priority of this plugin.
913int CalculatePriority(RecordVector::const_iterator B,
914 RecordVector::const_iterator E) {
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000915 int priority = 0;
916
917 if (B != E) {
918 priority = static_cast<int>((*B)->getValueAsInt("priority"));
919
920 if (++B != E)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000921 throw "More than one 'PluginPriority' instance found: "
922 "most probably an error!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000923 }
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000924
925 return priority;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000926}
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000927
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000928/// NotInGraph - Helper function object for FilterNotInGraph.
929struct NotInGraph {
930private:
931 const llvm::StringSet<>& ToolsInGraph_;
932
933public:
934 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
935 : ToolsInGraph_(ToolsInGraph)
936 {}
937
938 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
939 return (ToolsInGraph_.count(x->Name) == 0);
940 }
941};
942
943/// FilterNotInGraph - Filter out from ToolDescs all Tools not
944/// mentioned in the compilation graph definition.
945void FilterNotInGraph (const RecordVector& EdgeVector,
946 ToolDescriptions& ToolDescs) {
947
948 // List all tools mentioned in the graph.
949 llvm::StringSet<> ToolsInGraph;
950
951 for (RecordVector::const_iterator B = EdgeVector.begin(),
952 E = EdgeVector.end(); B != E; ++B) {
953
954 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000955 const std::string& NodeA = Edge->getValueAsString("a");
956 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000957
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000958 if (NodeA != "root")
959 ToolsInGraph.insert(NodeA);
960 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000961 }
962
963 // Filter ToolPropertiesList.
964 ToolDescriptions::iterator new_end =
965 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
966 NotInGraph(ToolsInGraph));
967 ToolDescs.erase(new_end, ToolDescs.end());
968}
969
970/// FillInToolToLang - Fills in two tables that map tool names to
971/// (input, output) languages. Helper function used by TypecheckGraph().
972void FillInToolToLang (const ToolDescriptions& ToolDescs,
973 StringMap<StringSet<> >& ToolToInLang,
974 StringMap<std::string>& ToolToOutLang) {
975 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
976 E = ToolDescs.end(); B != E; ++B) {
977 const ToolDescription& D = *(*B);
978 for (StrVector::const_iterator B = D.InLanguage.begin(),
979 E = D.InLanguage.end(); B != E; ++B)
980 ToolToInLang[D.Name].insert(*B);
981 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000982 }
983}
984
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000985/// TypecheckGraph - Check that names for output and input languages
986/// on all edges do match. This doesn't do much when the information
987/// about the whole graph is not available (i.e. when compiling most
988/// plugins).
989void TypecheckGraph (const RecordVector& EdgeVector,
990 const ToolDescriptions& ToolDescs) {
991 StringMap<StringSet<> > ToolToInLang;
992 StringMap<std::string> ToolToOutLang;
993
994 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
995 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
996 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
997
998 for (RecordVector::const_iterator B = EdgeVector.begin(),
999 E = EdgeVector.end(); B != E; ++B) {
1000 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001001 const std::string& NodeA = Edge->getValueAsString("a");
1002 const std::string& NodeB = Edge->getValueAsString("b");
1003 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
1004 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001005
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001006 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001007 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001008 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001009 + ": output->input language mismatch";
1010 }
1011
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001012 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001013 throw "Edges back to the root are not allowed!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001014 }
1015}
1016
1017/// WalkCase - Walks the 'case' expression DAG and invokes
1018/// TestCallback on every test, and StatementCallback on every
1019/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001020/// combinators (that is, they are passed directly to TestCallback).
1021/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1022/// IndentLevel, bool FirstTest)'.
1023/// StatementCallback must have type 'void StatementCallback(const Init*,
1024/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001025template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001026void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1027 unsigned IndentLevel = 0)
1028{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001029 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001030
1031 // Error checks.
1032 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001033 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001034
1035 if (d.getNumArgs() < 2)
1036 throw "There should be at least one clause in the 'case' expression:\n"
1037 + d.getAsString();
1038
1039 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001040 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001041 const unsigned numArgs = d.getNumArgs();
1042 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001043 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1044 B != E; ++B) {
1045 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001046
1047 if (!even)
1048 {
1049 // Handle test.
1050 const DagInit& Test = InitPtrToDag(arg);
1051
1052 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001053 throw "The 'default' clause should be the last in the "
1054 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001055 if (i == numArgs)
1056 throw "Case construct handler: no corresponding action "
1057 "found for the test " + Test.getAsString() + '!';
1058
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001059 TestCallback(Test, IndentLevel, (i == 1));
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001060 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001061 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001062 {
1063 if (dynamic_cast<DagInit*>(arg)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001064 && GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001065 // Nested 'case'.
1066 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1067 }
1068
1069 // Handle statement.
1070 StatementCallback(arg, IndentLevel);
1071 }
1072
1073 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001074 even = !even;
1075 }
1076}
1077
1078/// ExtractOptionNames - A helper function object used by
1079/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1080class ExtractOptionNames {
1081 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001082
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001083 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001084 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001085 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001086 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001087 ActionName == "forward_value" ||
1088 ActionName == "forward_transformed_value" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001089 ActionName == "switch_on" || ActionName == "parameter_equals" ||
1090 ActionName == "element_in_list" || ActionName == "not_empty" ||
1091 ActionName == "empty") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001092 CheckNumberOfArguments(Stmt, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001093 const std::string& Name = InitPtrToString(Stmt.getArg(0));
1094 OptionNames_.insert(Name);
1095 }
1096 else if (ActionName == "and" || ActionName == "or") {
1097 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001098 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001099 }
1100 }
1101 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001102
1103public:
1104 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1105 {}
1106
1107 void operator()(const Init* Statement) {
1108 if (typeid(*Statement) == typeid(ListInit)) {
1109 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1110 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1111 B != E; ++B)
1112 this->processDag(*B);
1113 }
1114 else {
1115 this->processDag(Statement);
1116 }
1117 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001118
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001119 void operator()(const DagInit& Test, unsigned, bool) {
1120 this->operator()(&Test);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001121 }
1122 void operator()(const Init* Statement, unsigned) {
1123 this->operator()(Statement);
1124 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001125};
1126
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001127/// CheckForSuperfluousOptions - Check that there are no side
1128/// effect-free options (specified only in the OptionList). Otherwise,
1129/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001130void CheckForSuperfluousOptions (const RecordVector& Edges,
1131 const ToolDescriptions& ToolDescs,
1132 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001133 llvm::StringSet<> nonSuperfluousOptions;
1134
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001135 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001136 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001137 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1138 E = ToolDescs.end(); B != E; ++B) {
1139 const ToolDescription& TD = *(*B);
1140 ExtractOptionNames Callback(nonSuperfluousOptions);
1141 if (TD.Actions)
1142 WalkCase(TD.Actions, Callback, Callback);
1143 }
1144
1145 // Add all options mentioned in the 'case' clauses of the
1146 // OptionalEdges of the compilation graph to the set of
1147 // non-superfluous options.
1148 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1149 B != E; ++B) {
1150 const Record* Edge = *B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001151 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001152
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001153 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001154 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001155 }
1156
1157 // Check that all options in OptDescs belong to the set of
1158 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001159 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001160 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001161 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001162 if (!nonSuperfluousOptions.count(Val.Name)
1163 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001164 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001165 "Probable cause: this option is specified only in the OptionList.\n";
1166 }
1167}
1168
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001169/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1170bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1171 if (TestName == "single_input_file") {
1172 O << "InputFilenames.size() == 1";
1173 return true;
1174 }
1175 else if (TestName == "multiple_input_files") {
1176 O << "InputFilenames.size() > 1";
1177 return true;
1178 }
1179
1180 return false;
1181}
1182
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001183/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1184template <typename F>
1185void EmitListTest(const ListInit& L, const char* LogicOp,
1186 F Callback, raw_ostream& O)
1187{
1188 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1189 // of Dags...
1190 bool isFirst = true;
1191 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1192 if (isFirst)
1193 isFirst = false;
1194 else
1195 O << " || ";
1196 Callback(InitPtrToString(*B), O);
1197 }
1198}
1199
1200// Callbacks for use with EmitListTest.
1201
1202class EmitSwitchOn {
1203 const OptionDescriptions& OptDescs_;
1204public:
1205 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1206 {}
1207
1208 void operator()(const std::string& OptName, raw_ostream& O) const {
1209 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1210 O << OptDesc.GenVariableName();
1211 }
1212};
1213
1214class EmitEmptyTest {
1215 bool EmitNegate_;
1216 const OptionDescriptions& OptDescs_;
1217public:
1218 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1219 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1220 {}
1221
1222 void operator()(const std::string& OptName, raw_ostream& O) const {
1223 const char* Neg = (EmitNegate_ ? "!" : "");
1224 if (OptName == "o") {
1225 O << Neg << "OutputFilename.empty()";
1226 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001227 else if (OptName == "save-temps") {
1228 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1229 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001230 else {
1231 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1232 O << Neg << OptDesc.GenVariableName() << ".empty()";
1233 }
1234 }
1235};
1236
1237
1238/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1239bool EmitCaseTest1ArgList(const std::string& TestName,
1240 const DagInit& d,
1241 const OptionDescriptions& OptDescs,
1242 raw_ostream& O) {
Mikhail Glushenkov3a481e32010-01-01 03:50:51 +00001243 const ListInit& L = InitPtrToList(d.getArg(0));
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001244
1245 if (TestName == "any_switch_on") {
1246 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1247 return true;
1248 }
1249 else if (TestName == "switch_on") {
1250 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1251 return true;
1252 }
1253 else if (TestName == "any_not_empty") {
1254 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1255 return true;
1256 }
1257 else if (TestName == "any_empty") {
1258 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1259 return true;
1260 }
1261 else if (TestName == "not_empty") {
1262 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1263 return true;
1264 }
1265 else if (TestName == "empty") {
1266 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1267 return true;
1268 }
1269
1270 return false;
1271}
1272
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001273/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1274bool EmitCaseTest1ArgStr(const std::string& TestName,
1275 const DagInit& d,
1276 const OptionDescriptions& OptDescs,
1277 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001278 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001279
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001280 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001281 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001282 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001283 }
1284 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001285 O << "InLangs.count(\"" << OptName << "\") != 0";
1286 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001287 }
1288 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001289 // This works only for single-argument Tool::GenerateAction. Join
1290 // tools can process several files in different languages simultaneously.
1291
1292 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001293 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001294 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001295 }
1296 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001297 bool EmitNegate = (TestName == "not_empty");
1298 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001299 return true;
1300 }
1301
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001302 return false;
1303}
1304
1305/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1306bool EmitCaseTest1Arg(const std::string& TestName,
1307 const DagInit& d,
1308 const OptionDescriptions& OptDescs,
1309 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001310 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001311 if (typeid(*d.getArg(0)) == typeid(ListInit))
1312 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1313 else
1314 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1315}
1316
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001317/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001318bool EmitCaseTest2Args(const std::string& TestName,
1319 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001320 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001321 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001322 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001323 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001324 const std::string& OptName = InitPtrToString(d.getArg(0));
1325 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001326
1327 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001328 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001329 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1330 return true;
1331 }
1332 else if (TestName == "element_in_list") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001333 const OptionDescription& OptDesc = OptDescs.FindList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001334 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001335 O << "std::find(" << VarName << ".begin(),\n";
1336 O.indent(IndentLevel + Indent1)
1337 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001338 << OptArg << "\") != " << VarName << ".end()";
1339 return true;
1340 }
1341
1342 return false;
1343}
1344
1345// Forward declaration.
1346// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001347void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001348 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001349 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001350
1351/// EmitLogicalOperationTest - Helper function used by
1352/// EmitCaseConstructHandler.
1353void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001354 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001355 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001356 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001357 O << '(';
1358 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001359 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001360 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001361 if (j != NumArgs - 1) {
1362 O << ")\n";
1363 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1364 }
1365 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001366 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001367 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001368 }
1369}
1370
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001371void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001372 const OptionDescriptions& OptDescs, raw_ostream& O)
1373{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001374 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001375 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1376 O << "! (";
1377 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1378 O << ")";
1379}
1380
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001381/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001382void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001383 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001384 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001385 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001386
1387 if (TestName == "and")
1388 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1389 else if (TestName == "or")
1390 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001391 else if (TestName == "not")
1392 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001393 else if (EmitCaseTest0Args(TestName, O))
1394 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001395 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1396 return;
1397 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1398 return;
1399 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001400 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001401}
1402
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001403
1404/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1405class EmitCaseTestCallback {
1406 bool EmitElseIf_;
1407 const OptionDescriptions& OptDescs_;
1408 raw_ostream& O_;
1409public:
1410
1411 EmitCaseTestCallback(bool EmitElseIf,
1412 const OptionDescriptions& OptDescs, raw_ostream& O)
1413 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1414 {}
1415
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001416 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001417 {
1418 if (GetOperatorName(Test) == "default") {
1419 O_.indent(IndentLevel) << "else {\n";
1420 }
1421 else {
1422 O_.indent(IndentLevel)
1423 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001424 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001425 O_ << ") {\n";
1426 }
1427 }
1428};
1429
1430/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1431template <typename F>
1432class EmitCaseStatementCallback {
1433 F Callback_;
1434 raw_ostream& O_;
1435public:
1436
1437 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1438 : Callback_(Callback), O_(O)
1439 {}
1440
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001441 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001442
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001443 // Ignore nested 'case' DAG.
1444 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001445 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case")) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001446 if (typeid(*Statement) == typeid(ListInit)) {
1447 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1448 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1449 B != E; ++B)
1450 Callback_(*B, (IndentLevel + Indent1), O_);
1451 }
1452 else {
1453 Callback_(Statement, (IndentLevel + Indent1), O_);
1454 }
1455 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001456 O_.indent(IndentLevel) << "}\n";
1457 }
1458
1459};
1460
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001461/// EmitCaseConstructHandler - Emit code that handles the 'case'
1462/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001463/// clause. Implemented on top of WalkCase.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001464/// Callback's type is void F(Init* Statement, unsigned IndentLevel,
1465/// raw_ostream& O).
1466/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001467/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1468/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001469template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001470void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001471 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001472 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001473 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001474 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1475 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001476}
1477
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001478/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001479/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1480/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001481void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001482 const char* Delimiters = " \t\n\v\f\r";
1483 enum TokenizerState
1484 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1485 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001486
1487 if (CmdLine.empty())
1488 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001489 Out.push_back("");
1490
1491 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1492 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001493
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001494 for (; B != E; ++B) {
1495 char cur_ch = CmdLine[B];
1496
1497 switch (cur_st) {
1498 case Normal:
1499 if (cur_ch == '$') {
1500 cur_st = SpecialCommand;
1501 break;
1502 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001503 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001504 // Skip whitespace
1505 B = CmdLine.find_first_not_of(Delimiters, B);
1506 if (B == std::string::npos) {
1507 B = E-1;
1508 continue;
1509 }
1510 --B;
1511 Out.push_back("");
1512 continue;
1513 }
1514 break;
1515
1516
1517 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001518 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001519 cur_st = Normal;
1520 Out.push_back("");
1521 continue;
1522 }
1523 if (cur_ch == '(') {
1524 Out.push_back("");
1525 cur_st = InsideSpecialCommand;
1526 continue;
1527 }
1528 break;
1529
1530 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001531 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001532 continue;
1533 }
1534 if (cur_ch == '\'') {
1535 cur_st = InsideQuotationMarks;
1536 Out.push_back("");
1537 continue;
1538 }
1539 if (cur_ch == ')') {
1540 cur_st = Normal;
1541 Out.push_back("");
1542 }
1543 if (cur_ch == ',') {
1544 continue;
1545 }
1546
1547 break;
1548
1549 case InsideQuotationMarks:
1550 if (cur_ch == '\'') {
1551 cur_st = InsideSpecialCommand;
1552 continue;
1553 }
1554 break;
1555 }
1556
1557 Out.back().push_back(cur_ch);
1558 }
1559}
1560
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001561/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1562/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1563/// SubstituteSpecialCommands().
1564StrVector::const_iterator
1565SubstituteCall (StrVector::const_iterator Pos,
1566 StrVector::const_iterator End,
1567 bool IsJoin, raw_ostream& O)
1568{
1569 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001570 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001571 const std::string& CmdName = *Pos;
1572
1573 if (CmdName == ")")
1574 throw "$CALL invocation: empty argument list!";
1575
1576 O << "hooks::";
1577 O << CmdName << "(";
1578
1579
1580 bool firstIteration = true;
1581 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001582 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001583 const std::string& Arg = *Pos;
1584 assert(Arg.size() != 0);
1585
1586 if (Arg[0] == ')')
1587 break;
1588
1589 if (firstIteration)
1590 firstIteration = false;
1591 else
1592 O << ", ";
1593
1594 if (Arg == "$INFILE") {
1595 if (IsJoin)
1596 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1597 else
1598 O << "inFile.c_str()";
1599 }
1600 else {
1601 O << '"' << Arg << '"';
1602 }
1603 }
1604
1605 O << ')';
1606
1607 return Pos;
1608}
1609
1610/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1611/// function used by SubstituteSpecialCommands().
1612StrVector::const_iterator
1613SubstituteEnv (StrVector::const_iterator Pos,
1614 StrVector::const_iterator End, raw_ostream& O)
1615{
1616 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001617 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001618 const std::string& EnvName = *Pos;
1619
1620 if (EnvName == ")")
1621 throw "$ENV invocation: empty argument list!";
1622
1623 O << "checkCString(std::getenv(\"";
1624 O << EnvName;
1625 O << "\"))";
1626
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001627 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001628
1629 return Pos;
1630}
1631
1632/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1633/// handler code. Helper function used by EmitCmdLineVecFill().
1634StrVector::const_iterator
1635SubstituteSpecialCommands (StrVector::const_iterator Pos,
1636 StrVector::const_iterator End,
1637 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001638{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001639
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001640 const std::string& cmd = *Pos;
1641
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001642 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001643 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001644 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001645 }
1646 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001647 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001648 }
1649 else {
1650 throw "Unknown special command: " + cmd;
1651 }
1652
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001653 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001654 const std::string& Leftover = *Pos;
1655 assert(Leftover.at(0) == ')');
1656 if (Leftover.size() != 1)
1657 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001658
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001659 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001660}
1661
1662/// EmitCmdLineVecFill - Emit code that fills in the command line
1663/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001664void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001665 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001666 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001667 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001668 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001669
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001670 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001671 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001672
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001673 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1674
1675 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001676 assert(!StrVec[0].empty());
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001677 if (StrVec[0][0] == '$') {
1678 while (I != E && (*I)[0] != ')' )
1679 ++I;
1680
1681 // Skip the ')' symbol.
1682 ++I;
1683 }
1684 else {
1685 ++I;
1686 }
1687
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001688 bool hasINFILE = false;
1689
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001690 for (; I != E; ++I) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001691 const std::string& cmd = *I;
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001692 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001693 O.indent(IndentLevel);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001694 if (cmd.at(0) == '$') {
1695 if (cmd == "$INFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001696 hasINFILE = true;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001697 if (IsJoin) {
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001698 O << "for (PathVector::const_iterator B = inFiles.begin()"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001699 << ", E = inFiles.end();\n";
1700 O.indent(IndentLevel) << "B != E; ++B)\n";
1701 O.indent(IndentLevel + Indent1) << "vec.push_back(B->str());\n";
1702 }
1703 else {
Chris Lattner74382b72009-08-23 22:45:37 +00001704 O << "vec.push_back(inFile.str());\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001705 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001706 }
1707 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001708 O << "vec.push_back(\"\");\n";
1709 O.indent(IndentLevel) << "out_file_index = vec.size()-1;\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001710 }
1711 else {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001712 O << "vec.push_back(";
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001713 I = SubstituteSpecialCommands(I, E, IsJoin, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001714 O << ");\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001715 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001716 }
1717 else {
1718 O << "vec.push_back(\"" << cmd << "\");\n";
1719 }
1720 }
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001721 if (!hasINFILE)
1722 throw "Tool '" + ToolName + "' doesn't take any input!";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001723
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001724 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001725 if (StrVec[0][0] == '$')
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001726 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001727 else
1728 O << '"' << StrVec[0] << '"';
1729 O << ";\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001730}
1731
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001732/// EmitCmdLineVecFillCallback - A function object wrapper around
1733/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1734/// argument to EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001735class EmitCmdLineVecFillCallback {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001736 bool IsJoin;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001737 const std::string& ToolName;
1738 public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001739 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1740 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001741
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001742 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001743 raw_ostream& O) const
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001744 {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001745 EmitCmdLineVecFill(Statement, ToolName, IsJoin, IndentLevel, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001746 }
1747};
1748
1749/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1750/// implement EmitActionHandler. Emits code for
1751/// handling the (forward) and (forward_as) option properties.
1752void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001753 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001754 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001755 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001756 const std::string& Name = NewName.empty()
1757 ? ("-" + D.Name)
1758 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001759 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001760
1761 switch (D.Type) {
1762 case OptionType::Switch:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001763 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001764 break;
1765 case OptionType::Parameter:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001766 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
1767 O.indent(IndentLevel) << "vec.push_back(" << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001768 break;
1769 case OptionType::Prefix:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001770 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\" + "
1771 << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001772 break;
1773 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001774 O.indent(IndentLevel)
1775 << "for (" << D.GenTypeDeclaration()
1776 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1777 O.indent(IndentLevel)
1778 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1779 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\" + " << "*B);\n";
1780 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001781
1782 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001783 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1784 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001785 }
1786
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001787 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001788 break;
1789 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001790 O.indent(IndentLevel)
1791 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1792 << D.GenVariableName() << ".begin(),\n";
1793 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1794 << ".end() ; B != E;) {\n";
1795 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001796
1797 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001798 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1799 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001800 }
1801
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001802 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001803 break;
1804 case OptionType::Alias:
1805 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001806 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001807 }
1808}
1809
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001810/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1811/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001812struct ActionHandlingCallbackBase
1813{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001814
1815 void onErrorDag(const DagInit& d,
1816 unsigned IndentLevel, raw_ostream& O) const
1817 {
1818 O.indent(IndentLevel)
1819 << "throw std::runtime_error(\"" <<
1820 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1821 : "Unknown error!")
1822 << "\");\n";
1823 }
1824
1825 void onWarningDag(const DagInit& d,
1826 unsigned IndentLevel, raw_ostream& O) const
1827 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001828 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001829 O.indent(IndentLevel) << "llvm::errs() << \""
1830 << InitPtrToString(d.getArg(0)) << "\";\n";
1831 }
1832
1833};
1834
1835/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1836/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001837
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001838class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001839
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001840typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1841(const DagInit&, unsigned, raw_ostream&) const;
1842
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001843class EmitActionHandlersCallback :
1844 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001845 public HandlerTable<EmitActionHandlersCallbackHandler>
1846{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001847 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001848
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001849 const OptionDescriptions& OptDescs;
1850
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001851 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1852 /// onAppendCmd and onOutputSuffix.
1853 void EmitHookInvocation(const std::string& Str,
1854 const char* BlockOpen, const char* BlockClose,
1855 unsigned IndentLevel, raw_ostream& O) const
1856 {
1857 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001858 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001859
1860 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1861 B != E; ++B) {
1862 const std::string& cmd = *B;
1863
1864 O.indent(IndentLevel) << BlockOpen;
1865
1866 if (cmd.at(0) == '$')
1867 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1868 else
1869 O << '"' << cmd << '"';
1870
1871 O << BlockClose;
1872 }
1873 }
1874
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001875 void onAppendCmd (const DagInit& Dag,
1876 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001877 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001878 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001879 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
1880 "vec.push_back(", ");\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001881 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001882
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001883 void onForward (const DagInit& Dag,
1884 unsigned IndentLevel, raw_ostream& O) const
1885 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001886 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001887 const std::string& Name = InitPtrToString(Dag.getArg(0));
1888 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1889 IndentLevel, "", O);
1890 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001891
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001892 void onForwardAs (const DagInit& Dag,
1893 unsigned IndentLevel, raw_ostream& O) const
1894 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001895 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001896 const std::string& Name = InitPtrToString(Dag.getArg(0));
1897 const std::string& NewName = InitPtrToString(Dag.getArg(1));
1898 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1899 IndentLevel, NewName, O);
1900 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001901
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001902 void onForwardValue (const DagInit& Dag,
1903 unsigned IndentLevel, raw_ostream& O) const
1904 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001905 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001906 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001907 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001908
1909 if (D.isParameter()) {
1910 O.indent(IndentLevel) << "vec.push_back("
1911 << D.GenVariableName() << ");\n";
1912 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001913 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001914 O.indent(IndentLevel) << "std::copy(" << D.GenVariableName()
1915 << ".begin(), " << D.GenVariableName()
1916 << ".end(), std::back_inserter(vec));\n";
1917 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001918 }
1919
1920 void onForwardTransformedValue (const DagInit& Dag,
1921 unsigned IndentLevel, raw_ostream& O) const
1922 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001923 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001924 const std::string& Name = InitPtrToString(Dag.getArg(0));
1925 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001926 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001927
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001928 O.indent(IndentLevel) << "vec.push_back(" << "hooks::"
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00001929 << Hook << "(" << D.GenVariableName()
1930 << (D.isParameter() ? ".c_str()" : "") << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001931 }
1932
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001933 void onOutputSuffix (const DagInit& Dag,
1934 unsigned IndentLevel, raw_ostream& O) const
1935 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001936 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001937 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
1938 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001939 }
1940
1941 void onStopCompilation (const DagInit& Dag,
1942 unsigned IndentLevel, raw_ostream& O) const
1943 {
1944 O.indent(IndentLevel) << "stop_compilation = true;\n";
1945 }
1946
1947
1948 void onUnpackValues (const DagInit& Dag,
1949 unsigned IndentLevel, raw_ostream& O) const
1950 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001951 throw "'unpack_values' is deprecated. "
1952 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001953 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001954
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001955 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001956
1957 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
1958 : OptDescs(OD)
1959 {
1960 if (!staticMembersInitialized_) {
1961 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
1962 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
1963 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
1964 AddHandler("forward", &EmitActionHandlersCallback::onForward);
1965 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001966 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
1967 AddHandler("forward_transformed_value",
1968 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001969 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
1970 AddHandler("stop_compilation",
1971 &EmitActionHandlersCallback::onStopCompilation);
1972 AddHandler("unpack_values",
1973 &EmitActionHandlersCallback::onUnpackValues);
1974
1975 staticMembersInitialized_ = true;
1976 }
1977 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001978
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001979 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001980 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001981 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001982 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001983 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001984};
1985
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001986bool IsOutFileIndexCheckRequiredStr (const Init* CmdLine) {
1987 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001988 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001989
1990 for (StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1991 I != E; ++I) {
1992 if (*I == "$OUTFILE")
1993 return false;
1994 }
1995
1996 return true;
1997}
1998
1999class IsOutFileIndexCheckRequiredStrCallback {
2000 bool* ret_;
2001
2002public:
2003 IsOutFileIndexCheckRequiredStrCallback(bool* ret) : ret_(ret)
2004 {}
2005
2006 void operator()(const Init* CmdLine) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002007 // Ignore nested 'case' DAG.
2008 if (typeid(*CmdLine) == typeid(DagInit))
2009 return;
2010
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002011 if (IsOutFileIndexCheckRequiredStr(CmdLine))
2012 *ret_ = true;
2013 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002014
2015 void operator()(const DagInit* Test, unsigned, bool) {
2016 this->operator()(Test);
2017 }
2018 void operator()(const Init* Statement, unsigned) {
2019 this->operator()(Statement);
2020 }
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002021};
2022
2023bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
2024 bool ret = false;
2025 WalkCase(CmdLine, Id(), IsOutFileIndexCheckRequiredStrCallback(&ret));
2026 return ret;
2027}
2028
2029/// IsOutFileIndexCheckRequired - Should we emit an "out_file_index != -1" check
2030/// in EmitGenerateActionMethod() ?
2031bool IsOutFileIndexCheckRequired (Init* CmdLine) {
2032 if (typeid(*CmdLine) == typeid(StringInit))
2033 return IsOutFileIndexCheckRequiredStr(CmdLine);
2034 else
2035 return IsOutFileIndexCheckRequiredCase(CmdLine);
2036}
2037
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002038void EmitGenerateActionMethodHeader(const ToolDescription& D,
2039 bool IsJoin, raw_ostream& O)
2040{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002041 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002042 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002043 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002044 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002045
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002046 O.indent(Indent2) << "bool HasChildren,\n";
2047 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2048 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2049 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2050 O.indent(Indent1) << "{\n";
2051 O.indent(Indent2) << "std::string cmd;\n";
2052 O.indent(Indent2) << "std::vector<std::string> vec;\n";
2053 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2054 O.indent(Indent2) << "const char* output_suffix = \""
2055 << D.OutputSuffix << "\";\n";
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002056}
2057
2058// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2059// Tool::GenerateAction() method.
2060void EmitGenerateActionMethod (const ToolDescription& D,
2061 const OptionDescriptions& OptDescs,
2062 bool IsJoin, raw_ostream& O) {
2063
2064 EmitGenerateActionMethodHeader(D, IsJoin, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002065
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002066 if (!D.CmdLine)
2067 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002068
2069 bool IndexCheckRequired = IsOutFileIndexCheckRequired(D.CmdLine);
2070 O.indent(Indent2) << "int out_file_index"
2071 << (IndexCheckRequired ? " = -1" : "")
2072 << ";\n\n";
2073
2074 // Process the cmd_line property.
2075 if (typeid(*D.CmdLine) == typeid(StringInit))
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002076 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2077 else
2078 EmitCaseConstructHandler(D.CmdLine, Indent2,
2079 EmitCmdLineVecFillCallback(IsJoin, D.Name),
2080 true, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002081
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002082 // For every understood option, emit handling code.
2083 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002084 EmitCaseConstructHandler(D.Actions, Indent2,
2085 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002086 false, OptDescs, O);
2087
2088 O << '\n';
2089 O.indent(Indent2)
2090 << "std::string out_file = OutFilename("
2091 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
2092 O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002093
2094 if (IndexCheckRequired)
2095 O.indent(Indent2) << "if (out_file_index != -1)\n";
2096 O.indent(IndexCheckRequired ? Indent3 : Indent2)
2097 << "vec[out_file_index] = out_file;\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002098
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002099 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002100 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002101 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
2102 O.indent(Indent3) << "vec.insert(vec.end(), "
2103 << SinkOptionName << ".begin(), " << SinkOptionName
2104 << ".end());\n";
2105 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002106 }
2107
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002108 O.indent(Indent2) << "return Action(cmd, vec, stop_compilation, out_file);\n";
2109 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002110}
2111
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002112/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2113/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002114void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2115 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002116 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002117 if (!ToolDesc.isJoin()) {
2118 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
2119 O.indent(Indent2) << "bool HasChildren,\n";
2120 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2121 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2122 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2123 O.indent(Indent1) << "{\n";
2124 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
2125 << " is not a Join tool!\");\n";
2126 O.indent(Indent1) << "}\n\n";
2127 }
2128 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002129 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002130 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002131
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002132 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002133}
2134
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002135/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2136/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002137void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002138 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2139 O.indent(Indent2) << "return InputLanguages_;\n";
2140 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002141
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002142 if (D.OutLanguage.empty())
2143 throw "Tool " + D.Name + " has no 'out_language' property!";
2144
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002145 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2146 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2147 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002148}
2149
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002150/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002151void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002152 O.indent(Indent1) << "const char* Name() const {\n";
2153 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2154 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002155}
2156
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002157/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2158/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002159void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002160 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002161 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002162 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002163 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002164 O.indent(Indent2) << "return false;\n";
2165 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002166}
2167
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002168/// EmitStaticMemberDefinitions - Emit static member definitions for a
2169/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002170void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002171 if (D.InLanguage.empty())
2172 throw "Tool " + D.Name + " has no 'in_language' property!";
2173
2174 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2175 for (StrVector::const_iterator B = D.InLanguage.begin(),
2176 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002177 O << '\"' << *B << "\", ";
2178 O << "0};\n\n";
2179}
2180
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002181/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002182void EmitToolClassDefinition (const ToolDescription& D,
2183 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002184 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002185 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002186 return;
2187
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002188 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002189 O << "class " << D.Name << " : public ";
2190 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002191 O << "JoinTool";
2192 else
2193 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002194
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002195 O << " {\nprivate:\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002196 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002197
2198 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002199 EmitNameMethod(D, O);
2200 EmitInOutLanguageMethods(D, O);
2201 EmitIsJoinMethod(D, O);
2202 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002203
2204 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002205 O << "};\n";
2206
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002207 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002208
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002209}
2210
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002211/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002212/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002213void EmitOptionDefinitions (const OptionDescriptions& descs,
2214 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002215 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002216{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002217 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002218
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002219 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002220 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002221 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002222 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002223
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002224 if (val.Type == OptionType::Alias) {
2225 Aliases.push_back(val);
2226 continue;
2227 }
2228
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002229 if (val.isExtern())
2230 O << "extern ";
2231
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002232 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002233 << val.GenVariableName();
2234
2235 if (val.isExtern()) {
2236 O << ";\n";
2237 continue;
2238 }
2239
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002240 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002241
2242 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2243 O << ", cl::Prefix";
2244
2245 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002246 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002247 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002248 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002249 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002250 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002251 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002252 O << ", cl::OneOrMore";
2253 }
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002254 else if (val.isOptional() && val.isList()) {
2255 O << ", cl::Optional";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002256 }
2257
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002258 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002259 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002260 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002261 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002262
2263 if (val.isCommaSeparated())
2264 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002265
2266 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002267 O << ", cl::multi_val(" << val.MultiVal << ')';
2268
2269 if (val.InitVal) {
2270 const std::string& str = val.InitVal->getAsString();
2271 O << ", cl::init(" << str << ')';
2272 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002273
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002274 if (!val.Help.empty())
2275 O << ", cl::desc(\"" << val.Help << "\")";
2276
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002277 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002278 }
2279
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002280 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002281 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002282 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002283 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002284
2285 O << val.GenTypeDeclaration() << ' '
2286 << val.GenVariableName()
2287 << "(\"" << val.Name << '\"';
2288
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002289 const OptionDescription& D = descs.FindOption(val.Help);
2290 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002291
2292 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2293 }
2294
2295 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002296 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002297 O << (HasExterns ? "extern cl" : "cl")
2298 << "::list<std::string> " << SinkOptionName
2299 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002300
2301 O << '\n';
2302}
2303
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002304/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002305/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002306
2307class EmitPreprocessOptionsCallback;
2308
2309typedef void
2310(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2311(const DagInit&, unsigned, raw_ostream&) const;
2312
2313class EmitPreprocessOptionsCallback :
2314 public ActionHandlingCallbackBase,
2315 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2316{
2317 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002318 typedef void
2319 (EmitPreprocessOptionsCallback::* HandlerImpl)
2320 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002321
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002322 const OptionDescriptions& OptDescs_;
2323
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002324 void onListOrDag(const DagInit& d, HandlerImpl h,
2325 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002326 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002327 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002328 const Init* I = d.getArg(0);
2329
2330 // If I is a list, apply h to each element.
2331 if (typeid(*I) == typeid(ListInit)) {
2332 const ListInit& L = *static_cast<const ListInit*>(I);
2333 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B)
2334 ((this)->*(h))(*B, IndentLevel, O);
2335 }
2336 // Otherwise, apply h to I.
2337 else {
2338 ((this)->*(h))(I, IndentLevel, O);
2339 }
2340 }
2341
2342 void onUnsetOptionImpl(const Init* I,
2343 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002344 {
2345 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002346 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002347
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002348 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002349 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2350 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002351 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002352 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2353 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002354 else if (OptDesc.isList()) {
2355 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2356 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002357 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002358 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002359 }
2360 }
2361
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002362 void onUnsetOption(const DagInit& d,
2363 unsigned IndentLevel, raw_ostream& O) const
2364 {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002365 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2366 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002367 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002368
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002369 void onSetOptionImpl(const DagInit& d,
2370 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002371 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002372 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002373 const Init* Value = d.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002374 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2375
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002376 if (OptDesc.isList()) {
2377 const ListInit& List = InitPtrToList(Value);
2378
2379 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2380 for (ListInit::const_iterator B = List.begin(), E = List.end();
2381 B != E; ++B) {
2382 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".push_back(\""
2383 << InitPtrToString(*B) << "\");\n";
2384 }
2385 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002386 else if (OptDesc.isSwitch()) {
2387 CheckBooleanConstant(Value);
2388 O.indent(IndentLevel) << OptDesc.GenVariableName()
2389 << " = " << Value->getAsString() << ";\n";
2390 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002391 else if (OptDesc.isParameter()) {
2392 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002393 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002394 << " = \"" << Str << "\";\n";
2395 }
2396 else {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002397 throw "Can't apply 'set_option' to alias option -" + OptName + " !";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002398 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002399 }
2400
2401 void onSetSwitch(const Init* I,
2402 unsigned IndentLevel, raw_ostream& O) const {
2403 const std::string& OptName = InitPtrToString(I);
2404 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2405
2406 if (OptDesc.isSwitch())
2407 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2408 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002409 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002410 }
2411
2412 void onSetOption(const DagInit& d,
2413 unsigned IndentLevel, raw_ostream& O) const
2414 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002415 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002416
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002417 // Two arguments: (set_option "parameter", VALUE), where VALUE can be a
2418 // boolean, a string or a string list.
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002419 if (d.getNumArgs() > 1)
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002420 this->onSetOptionImpl(d, IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002421 // One argument: (set_option "switch")
2422 // or (set_option ["switch1", "switch2", ...])
2423 else
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002424 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2425 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002426 }
2427
2428public:
2429
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002430 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002431 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002432 {
2433 if (!staticMembersInitialized_) {
2434 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2435 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2436 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002437 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002438
2439 staticMembersInitialized_ = true;
2440 }
2441 }
2442
2443 void operator()(const Init* I,
2444 unsigned IndentLevel, raw_ostream& O) const
2445 {
2446 InvokeDagInitHandler(this, I, IndentLevel, O);
2447 }
2448
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002449};
2450
2451/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2452void EmitPreprocessOptions (const RecordKeeper& Records,
2453 const OptionDescriptions& OptDecs, raw_ostream& O)
2454{
2455 O << "void PreprocessOptionsLocal() {\n";
2456
2457 const RecordVector& OptionPreprocessors =
2458 Records.getAllDerivedDefinitions("OptionPreprocessor");
2459
2460 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2461 E = OptionPreprocessors.end(); B!=E; ++B) {
2462 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002463 EmitCaseConstructHandler(Case, Indent1,
2464 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002465 false, OptDecs, O);
2466 }
2467
2468 O << "}\n\n";
2469}
2470
2471/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002472void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002473{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002474 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002475
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002476 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002477 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002478
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002479 // It is allowed for a plugin to have no language map.
2480 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002481
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002482 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2483 if (!LangsToSuffixesList)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00002484 throw "Error in the language map definition!";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002485
2486 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002487 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002488
2489 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2490 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2491
2492 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002493 O.indent(Indent1) << "langMap[\""
2494 << InitPtrToString(Suffixes->getElement(i))
2495 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002496 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002497 }
2498
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002499 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002500}
2501
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002502/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2503/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002504void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002505 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002506 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002507 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002508
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002509 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002510 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002511 }
2512 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002513 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002514 }
2515 else if (OpName == "error") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002516 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002517 O.indent(IndentLevel) << "throw std::runtime_error(\""
2518 << InitPtrToString(d.getArg(0))
2519 << "\");\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002520 return;
2521 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002522 else {
2523 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002524 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002525 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002526
2527 if (d.getNumArgs() > 0)
2528 O << InitPtrToInt(d.getArg(0)) << ";\n";
2529 else
2530 O << "2;\n";
2531
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002532}
2533
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002534/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002535void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002536 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002537 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002538
2539 // Class constructor.
2540 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002541 << "public:\n";
2542 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2543 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002544
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002545 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002546 O.indent(Indent1)
2547 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2548 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002549
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002550 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002551 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002552
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002553 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002554 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002555}
2556
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002557/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002558void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002559 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002560 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002561 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002562 for (RecordVector::const_iterator B = EdgeVector.begin(),
2563 E = EdgeVector.end(); B != E; ++B) {
2564 const Record* Edge = *B;
2565 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002566 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002567
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002568 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002569 EmitEdgeClass(i, NodeB, &Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002570 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002571 }
2572}
2573
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002574/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002575/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002576void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002577 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002578 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002579{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002580 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002581
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002582 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2583 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002584 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002585
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002586 O << '\n';
2587
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002588 // Insert edges.
2589
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002590 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002591 for (RecordVector::const_iterator B = EdgeVector.begin(),
2592 E = EdgeVector.end(); B != E; ++B) {
2593 const Record* Edge = *B;
2594 const std::string& NodeA = Edge->getValueAsString("a");
2595 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002596 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002597
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002598 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002599
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002600 if (IsDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002601 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002602 else
2603 O << "new Edge" << i << "()";
2604
2605 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002606 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002607 }
2608
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002609 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002610}
2611
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002612/// HookInfo - Information about the hook type and number of arguments.
2613struct HookInfo {
2614
2615 // A hook can either have a single parameter of type std::vector<std::string>,
2616 // or NumArgs parameters of type const char*.
2617 enum HookType { ListHook, ArgHook };
2618
2619 HookType Type;
2620 unsigned NumArgs;
2621
2622 HookInfo() : Type(ArgHook), NumArgs(1)
2623 {}
2624
2625 HookInfo(HookType T) : Type(T), NumArgs(1)
2626 {}
2627
2628 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2629 {}
2630};
2631
2632typedef llvm::StringMap<HookInfo> HookInfoMap;
2633
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002634/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002635/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002636/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002637class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002638 HookInfoMap& HookNames_;
2639 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002640public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002641 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2642 : HookNames_(HookNames), OptDescs_(OptDescs)
2643 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002644
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002645 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002646 const std::string& Name = GetOperatorName(Dag);
2647
2648 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002649 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002650 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2651 const std::string& HookName = InitPtrToString(Dag.getArg(1));
2652 const OptionDescription& D = OptDescs_.FindOption(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002653
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002654 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2655 : HookInfo::ArgHook);
2656 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002657 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002658 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002659 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2660 }
2661 }
2662
2663 void onCmdLine(const std::string& Cmd) {
2664 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002665 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002666
2667 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2668 B != E; ++B) {
2669 const std::string& cmd = *B;
2670
2671 if (cmd == "$CALL") {
2672 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002673 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002674 const std::string& HookName = *B;
2675
2676 if (HookName.at(0) == ')')
2677 throw "$CALL invoked with no arguments!";
2678
2679 while (++B != E && B->at(0) != ')') {
2680 ++NumArgs;
2681 }
2682
2683 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2684
2685 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2686 H->second.Type != HookInfo::ArgHook)
2687 throw "Overloading of hooks is not allowed. Overloaded hook: "
2688 + HookName;
2689 else
2690 HookNames_[HookName] = HookInfo(NumArgs);
2691 }
2692 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002693 }
2694
2695 void operator()(const Init* Arg) {
2696
2697 // We're invoked on an action (either a dag or a dag list).
2698 if (typeid(*Arg) == typeid(DagInit)) {
2699 const DagInit& Dag = InitPtrToDag(Arg);
2700 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002701 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002702 }
2703 else if (typeid(*Arg) == typeid(ListInit)) {
2704 const ListInit& List = InitPtrToList(Arg);
2705 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2706 ++B) {
2707 const DagInit& Dag = InitPtrToDag(*B);
2708 this->onAction(Dag);
2709 }
2710 return;
2711 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002712
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002713 // We're invoked on a command line.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002714 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002715 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002716
2717 void operator()(const DagInit* Test, unsigned, bool) {
2718 this->operator()(Test);
2719 }
2720 void operator()(const Init* Statement, unsigned) {
2721 this->operator()(Statement);
2722 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002723};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002724
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002725/// FillInHookNames - Actually extract the hook names from all command
2726/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002727void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002728 const OptionDescriptions& OptDescs,
2729 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002730{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002731 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002732 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2733 E = ToolDescs.end(); B != E; ++B) {
2734 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002735
2736 // Look for 'forward_transformed_value' in 'actions'.
2737 if (D.Actions)
2738 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2739
2740 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002741 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002742 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002743 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002744 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002745 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002746 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002747 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002748 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002749 }
2750}
2751
2752/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2753/// property records and emit hook function declaration for each
2754/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002755void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2756 const OptionDescriptions& OptDescs, raw_ostream& O) {
2757 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002758
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002759 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002760 if (HookNames.empty())
2761 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002762
2763 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002764 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002765 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002766 const char* HookName = B->first();
2767 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002768
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002769 O.indent(Indent1) << "std::string " << HookName << "(";
2770
2771 if (Info.Type == HookInfo::ArgHook) {
2772 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2773 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2774 }
2775 }
2776 else {
2777 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002778 }
2779
2780 O <<");\n";
2781 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002782 O << "}\n\n";
2783}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002784
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002785/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002786void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002787 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2788 O.indent(Indent1) << "int Priority() const { return "
2789 << Priority << "; }\n\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002790 O.indent(Indent1) << "void PreprocessOptions() const\n";
2791 O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002792 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2793 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2794 O.indent(Indent1)
2795 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2796 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2797 << "};\n\n"
2798 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002799}
2800
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002801/// EmitIncludes - Emit necessary #include directives and some
2802/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002803void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002804 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2805 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002806 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002807 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2808 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002809
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002810 << "#include \"llvm/Support/CommandLine.h\"\n"
2811 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002812
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002813 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002814 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002815 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002816 << "#include <stdexcept>\n\n"
2817
2818 << "using namespace llvm;\n"
2819 << "using namespace llvmc;\n\n"
2820
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002821 << "extern cl::opt<std::string> OutputFilename;\n\n"
2822
2823 << "inline const char* checkCString(const char* s)\n"
2824 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002825}
2826
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002827
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002828/// PluginData - Holds all information about a plugin.
2829struct PluginData {
2830 OptionDescriptions OptDescs;
2831 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002832 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002833 ToolDescriptions ToolDescs;
2834 RecordVector Edges;
2835 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002836};
2837
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002838/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002839/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002840bool HasSink(const ToolDescriptions& ToolDescs) {
2841 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2842 E = ToolDescs.end(); B != E; ++B)
2843 if ((*B)->isSink())
2844 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002845
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002846 return false;
2847}
2848
2849/// HasExterns - Go through the list of option descriptions and check
2850/// if there are any external options.
2851bool HasExterns(const OptionDescriptions& OptDescs) {
2852 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2853 E = OptDescs.end(); B != E; ++B)
2854 if (B->second.isExtern())
2855 return true;
2856
2857 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002858}
2859
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002860/// CollectPluginData - Collect tool and option properties,
2861/// compilation graph edges and plugin priority from the parse tree.
2862void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2863 // Collect option properties.
2864 const RecordVector& OptionLists =
2865 Records.getAllDerivedDefinitions("OptionList");
2866 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2867 Data.OptDescs);
2868
2869 // Collect tool properties.
2870 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2871 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2872 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002873 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002874
2875 // Collect compilation graph edges.
2876 const RecordVector& CompilationGraphs =
2877 Records.getAllDerivedDefinitions("CompilationGraph");
2878 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2879 Data.Edges);
2880
2881 // Calculate the priority of this plugin.
2882 const RecordVector& Priorities =
2883 Records.getAllDerivedDefinitions("PluginPriority");
2884 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002885}
2886
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002887/// CheckPluginData - Perform some sanity checks on the collected data.
2888void CheckPluginData(PluginData& Data) {
2889 // Filter out all tools not mentioned in the compilation graph.
2890 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002891
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002892 // Typecheck the compilation graph.
2893 TypecheckGraph(Data.Edges, Data.ToolDescs);
2894
2895 // Check that there are no options without side effects (specified
2896 // only in the OptionList).
2897 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2898
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002899}
2900
Daniel Dunbar1a551802009-07-03 00:10:29 +00002901void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002902 // Emit file header.
2903 EmitIncludes(O);
2904
2905 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002906 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002907
2908 // Emit hook declarations.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002909 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002910
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002911 O << "namespace {\n\n";
2912
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002913 // Emit PreprocessOptionsLocal() function.
2914 EmitPreprocessOptions(Records, Data.OptDescs, O);
2915
2916 // Emit PopulateLanguageMapLocal() function
2917 // (language map maps from file extensions to language names).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002918 EmitPopulateLanguageMap(Records, O);
2919
2920 // Emit Tool classes.
2921 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2922 E = Data.ToolDescs.end(); B!=E; ++B)
2923 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2924
2925 // Emit Edge# classes.
2926 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2927
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002928 // Emit PopulateCompilationGraphLocal() function.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002929 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2930
2931 // Emit code for plugin registration.
2932 EmitRegisterPlugin(Data.Priority, O);
2933
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002934 O << "} // End anonymous namespace.\n\n";
2935
2936 // Force linkage magic.
2937 O << "namespace llvmc {\n";
2938 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2939 O << "}\n";
2940
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002941 // EOF
2942}
2943
2944
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002945// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00002946}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002947
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002948/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002949void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002950 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002951 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002952
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002953 CollectPluginData(Records, Data);
2954 CheckPluginData(Data);
2955
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +00002956 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002957 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002958
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002959 } catch (std::exception& Error) {
2960 throw Error.what() + std::string(" - usually this means a syntax error.");
2961 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002962}