blob: 88fb6c3b2c7a40014f97bae0b0322b57fe31ff3e [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 Glushenkovd64c9072010-01-01 03:51:02 +00001089 ActionName == "switch_on" || ActionName == "any_switch_on" ||
1090 ActionName == "parameter_equals" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001091 ActionName == "element_in_list" || ActionName == "not_empty" ||
1092 ActionName == "empty") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001093 CheckNumberOfArguments(Stmt, 1);
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001094
1095 Init* Arg = Stmt.getArg(0);
1096 if (typeid(*Arg) == typeid(StringInit)) {
1097 const std::string& Name = InitPtrToString(Arg);
1098 OptionNames_.insert(Name);
1099 }
1100 else {
1101 // It's a list.
1102 const ListInit& List = InitPtrToList(Arg);
1103 for (ListInit::const_iterator B = List.begin(), E = List.end();
1104 B != E; ++B) {
1105 const std::string& Name = InitPtrToString(*B);
1106 OptionNames_.insert(Name);
1107 }
1108 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001109 }
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001110 else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001111 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001112 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001113 }
1114 }
1115 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001116
1117public:
1118 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1119 {}
1120
1121 void operator()(const Init* Statement) {
1122 if (typeid(*Statement) == typeid(ListInit)) {
1123 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1124 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1125 B != E; ++B)
1126 this->processDag(*B);
1127 }
1128 else {
1129 this->processDag(Statement);
1130 }
1131 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001132
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001133 void operator()(const DagInit& Test, unsigned, bool) {
1134 this->operator()(&Test);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001135 }
1136 void operator()(const Init* Statement, unsigned) {
1137 this->operator()(Statement);
1138 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001139};
1140
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001141/// CheckForSuperfluousOptions - Check that there are no side
1142/// effect-free options (specified only in the OptionList). Otherwise,
1143/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001144void CheckForSuperfluousOptions (const RecordVector& Edges,
1145 const ToolDescriptions& ToolDescs,
1146 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001147 llvm::StringSet<> nonSuperfluousOptions;
1148
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001149 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001150 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001151 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1152 E = ToolDescs.end(); B != E; ++B) {
1153 const ToolDescription& TD = *(*B);
1154 ExtractOptionNames Callback(nonSuperfluousOptions);
1155 if (TD.Actions)
1156 WalkCase(TD.Actions, Callback, Callback);
1157 }
1158
1159 // Add all options mentioned in the 'case' clauses of the
1160 // OptionalEdges of the compilation graph to the set of
1161 // non-superfluous options.
1162 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1163 B != E; ++B) {
1164 const Record* Edge = *B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001165 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001166
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001167 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001168 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001169 }
1170
1171 // Check that all options in OptDescs belong to the set of
1172 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001173 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001174 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001175 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001176 if (!nonSuperfluousOptions.count(Val.Name)
1177 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001178 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001179 "Probable cause: this option is specified only in the OptionList.\n";
1180 }
1181}
1182
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001183/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1184bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1185 if (TestName == "single_input_file") {
1186 O << "InputFilenames.size() == 1";
1187 return true;
1188 }
1189 else if (TestName == "multiple_input_files") {
1190 O << "InputFilenames.size() > 1";
1191 return true;
1192 }
1193
1194 return false;
1195}
1196
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001197/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1198template <typename F>
1199void EmitListTest(const ListInit& L, const char* LogicOp,
1200 F Callback, raw_ostream& O)
1201{
1202 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1203 // of Dags...
1204 bool isFirst = true;
1205 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1206 if (isFirst)
1207 isFirst = false;
1208 else
Mikhail Glushenkovb7935e02010-01-01 04:40:54 +00001209 O << ' ' << LogicOp << ' ';
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001210 Callback(InitPtrToString(*B), O);
1211 }
1212}
1213
1214// Callbacks for use with EmitListTest.
1215
1216class EmitSwitchOn {
1217 const OptionDescriptions& OptDescs_;
1218public:
1219 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1220 {}
1221
1222 void operator()(const std::string& OptName, raw_ostream& O) const {
1223 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1224 O << OptDesc.GenVariableName();
1225 }
1226};
1227
1228class EmitEmptyTest {
1229 bool EmitNegate_;
1230 const OptionDescriptions& OptDescs_;
1231public:
1232 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1233 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1234 {}
1235
1236 void operator()(const std::string& OptName, raw_ostream& O) const {
1237 const char* Neg = (EmitNegate_ ? "!" : "");
1238 if (OptName == "o") {
1239 O << Neg << "OutputFilename.empty()";
1240 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001241 else if (OptName == "save-temps") {
1242 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1243 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001244 else {
1245 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1246 O << Neg << OptDesc.GenVariableName() << ".empty()";
1247 }
1248 }
1249};
1250
1251
1252/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1253bool EmitCaseTest1ArgList(const std::string& TestName,
1254 const DagInit& d,
1255 const OptionDescriptions& OptDescs,
1256 raw_ostream& O) {
Mikhail Glushenkov3a481e32010-01-01 03:50:51 +00001257 const ListInit& L = InitPtrToList(d.getArg(0));
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001258
1259 if (TestName == "any_switch_on") {
1260 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1261 return true;
1262 }
1263 else if (TestName == "switch_on") {
1264 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1265 return true;
1266 }
1267 else if (TestName == "any_not_empty") {
1268 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1269 return true;
1270 }
1271 else if (TestName == "any_empty") {
1272 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1273 return true;
1274 }
1275 else if (TestName == "not_empty") {
1276 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1277 return true;
1278 }
1279 else if (TestName == "empty") {
1280 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1281 return true;
1282 }
1283
1284 return false;
1285}
1286
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001287/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1288bool EmitCaseTest1ArgStr(const std::string& TestName,
1289 const DagInit& d,
1290 const OptionDescriptions& OptDescs,
1291 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001292 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001293
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001294 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001295 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001296 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001297 }
1298 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001299 O << "InLangs.count(\"" << OptName << "\") != 0";
1300 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001301 }
1302 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001303 // This works only for single-argument Tool::GenerateAction. Join
1304 // tools can process several files in different languages simultaneously.
1305
1306 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001307 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001308 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001309 }
1310 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001311 bool EmitNegate = (TestName == "not_empty");
1312 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001313 return true;
1314 }
1315
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001316 return false;
1317}
1318
1319/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1320bool EmitCaseTest1Arg(const std::string& TestName,
1321 const DagInit& d,
1322 const OptionDescriptions& OptDescs,
1323 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001324 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001325 if (typeid(*d.getArg(0)) == typeid(ListInit))
1326 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1327 else
1328 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1329}
1330
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001331/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001332bool EmitCaseTest2Args(const std::string& TestName,
1333 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001334 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001335 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001336 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001337 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001338 const std::string& OptName = InitPtrToString(d.getArg(0));
1339 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001340
1341 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001342 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001343 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1344 return true;
1345 }
1346 else if (TestName == "element_in_list") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001347 const OptionDescription& OptDesc = OptDescs.FindList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001348 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001349 O << "std::find(" << VarName << ".begin(),\n";
1350 O.indent(IndentLevel + Indent1)
1351 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001352 << OptArg << "\") != " << VarName << ".end()";
1353 return true;
1354 }
1355
1356 return false;
1357}
1358
1359// Forward declaration.
1360// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001361void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001362 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001363 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001364
1365/// EmitLogicalOperationTest - Helper function used by
1366/// EmitCaseConstructHandler.
1367void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001368 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001369 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001370 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001371 O << '(';
1372 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001373 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001374 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001375 if (j != NumArgs - 1) {
1376 O << ")\n";
1377 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1378 }
1379 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001380 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001381 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001382 }
1383}
1384
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001385void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001386 const OptionDescriptions& OptDescs, raw_ostream& O)
1387{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001388 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001389 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1390 O << "! (";
1391 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1392 O << ")";
1393}
1394
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001395/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001396void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001397 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001398 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001399 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001400
1401 if (TestName == "and")
1402 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1403 else if (TestName == "or")
1404 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001405 else if (TestName == "not")
1406 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001407 else if (EmitCaseTest0Args(TestName, O))
1408 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001409 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1410 return;
1411 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1412 return;
1413 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001414 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001415}
1416
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001417
1418/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1419class EmitCaseTestCallback {
1420 bool EmitElseIf_;
1421 const OptionDescriptions& OptDescs_;
1422 raw_ostream& O_;
1423public:
1424
1425 EmitCaseTestCallback(bool EmitElseIf,
1426 const OptionDescriptions& OptDescs, raw_ostream& O)
1427 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1428 {}
1429
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001430 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001431 {
1432 if (GetOperatorName(Test) == "default") {
1433 O_.indent(IndentLevel) << "else {\n";
1434 }
1435 else {
1436 O_.indent(IndentLevel)
1437 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001438 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001439 O_ << ") {\n";
1440 }
1441 }
1442};
1443
1444/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1445template <typename F>
1446class EmitCaseStatementCallback {
1447 F Callback_;
1448 raw_ostream& O_;
1449public:
1450
1451 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1452 : Callback_(Callback), O_(O)
1453 {}
1454
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001455 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001456
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001457 // Ignore nested 'case' DAG.
1458 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001459 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case")) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001460 if (typeid(*Statement) == typeid(ListInit)) {
1461 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1462 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1463 B != E; ++B)
1464 Callback_(*B, (IndentLevel + Indent1), O_);
1465 }
1466 else {
1467 Callback_(Statement, (IndentLevel + Indent1), O_);
1468 }
1469 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001470 O_.indent(IndentLevel) << "}\n";
1471 }
1472
1473};
1474
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001475/// EmitCaseConstructHandler - Emit code that handles the 'case'
1476/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001477/// clause. Implemented on top of WalkCase.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001478/// Callback's type is void F(Init* Statement, unsigned IndentLevel,
1479/// raw_ostream& O).
1480/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001481/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1482/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001483template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001484void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001485 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001486 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001487 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001488 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1489 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001490}
1491
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001492/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001493/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1494/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001495void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001496 const char* Delimiters = " \t\n\v\f\r";
1497 enum TokenizerState
1498 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1499 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001500
1501 if (CmdLine.empty())
1502 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001503 Out.push_back("");
1504
1505 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1506 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001507
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001508 for (; B != E; ++B) {
1509 char cur_ch = CmdLine[B];
1510
1511 switch (cur_st) {
1512 case Normal:
1513 if (cur_ch == '$') {
1514 cur_st = SpecialCommand;
1515 break;
1516 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001517 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001518 // Skip whitespace
1519 B = CmdLine.find_first_not_of(Delimiters, B);
1520 if (B == std::string::npos) {
1521 B = E-1;
1522 continue;
1523 }
1524 --B;
1525 Out.push_back("");
1526 continue;
1527 }
1528 break;
1529
1530
1531 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001532 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001533 cur_st = Normal;
1534 Out.push_back("");
1535 continue;
1536 }
1537 if (cur_ch == '(') {
1538 Out.push_back("");
1539 cur_st = InsideSpecialCommand;
1540 continue;
1541 }
1542 break;
1543
1544 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001545 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001546 continue;
1547 }
1548 if (cur_ch == '\'') {
1549 cur_st = InsideQuotationMarks;
1550 Out.push_back("");
1551 continue;
1552 }
1553 if (cur_ch == ')') {
1554 cur_st = Normal;
1555 Out.push_back("");
1556 }
1557 if (cur_ch == ',') {
1558 continue;
1559 }
1560
1561 break;
1562
1563 case InsideQuotationMarks:
1564 if (cur_ch == '\'') {
1565 cur_st = InsideSpecialCommand;
1566 continue;
1567 }
1568 break;
1569 }
1570
1571 Out.back().push_back(cur_ch);
1572 }
1573}
1574
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001575/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1576/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1577/// SubstituteSpecialCommands().
1578StrVector::const_iterator
1579SubstituteCall (StrVector::const_iterator Pos,
1580 StrVector::const_iterator End,
1581 bool IsJoin, raw_ostream& O)
1582{
1583 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001584 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001585 const std::string& CmdName = *Pos;
1586
1587 if (CmdName == ")")
1588 throw "$CALL invocation: empty argument list!";
1589
1590 O << "hooks::";
1591 O << CmdName << "(";
1592
1593
1594 bool firstIteration = true;
1595 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001596 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001597 const std::string& Arg = *Pos;
1598 assert(Arg.size() != 0);
1599
1600 if (Arg[0] == ')')
1601 break;
1602
1603 if (firstIteration)
1604 firstIteration = false;
1605 else
1606 O << ", ";
1607
1608 if (Arg == "$INFILE") {
1609 if (IsJoin)
1610 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1611 else
1612 O << "inFile.c_str()";
1613 }
1614 else {
1615 O << '"' << Arg << '"';
1616 }
1617 }
1618
1619 O << ')';
1620
1621 return Pos;
1622}
1623
1624/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1625/// function used by SubstituteSpecialCommands().
1626StrVector::const_iterator
1627SubstituteEnv (StrVector::const_iterator Pos,
1628 StrVector::const_iterator End, raw_ostream& O)
1629{
1630 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001631 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001632 const std::string& EnvName = *Pos;
1633
1634 if (EnvName == ")")
1635 throw "$ENV invocation: empty argument list!";
1636
1637 O << "checkCString(std::getenv(\"";
1638 O << EnvName;
1639 O << "\"))";
1640
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001641 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001642
1643 return Pos;
1644}
1645
1646/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1647/// handler code. Helper function used by EmitCmdLineVecFill().
1648StrVector::const_iterator
1649SubstituteSpecialCommands (StrVector::const_iterator Pos,
1650 StrVector::const_iterator End,
1651 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001652{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001653
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001654 const std::string& cmd = *Pos;
1655
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001656 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001657 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001658 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001659 }
1660 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001661 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001662 }
1663 else {
1664 throw "Unknown special command: " + cmd;
1665 }
1666
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001667 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001668 const std::string& Leftover = *Pos;
1669 assert(Leftover.at(0) == ')');
1670 if (Leftover.size() != 1)
1671 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001672
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001673 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001674}
1675
1676/// EmitCmdLineVecFill - Emit code that fills in the command line
1677/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001678void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001679 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001680 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001681 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001682 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001683
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001684 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001685 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001686
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001687 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1688
1689 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001690 assert(!StrVec[0].empty());
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001691 if (StrVec[0][0] == '$') {
1692 while (I != E && (*I)[0] != ')' )
1693 ++I;
1694
1695 // Skip the ')' symbol.
1696 ++I;
1697 }
1698 else {
1699 ++I;
1700 }
1701
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001702 bool hasINFILE = false;
1703
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001704 for (; I != E; ++I) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001705 const std::string& cmd = *I;
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001706 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001707 O.indent(IndentLevel);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001708 if (cmd.at(0) == '$') {
1709 if (cmd == "$INFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001710 hasINFILE = true;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001711 if (IsJoin) {
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001712 O << "for (PathVector::const_iterator B = inFiles.begin()"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001713 << ", E = inFiles.end();\n";
1714 O.indent(IndentLevel) << "B != E; ++B)\n";
1715 O.indent(IndentLevel + Indent1) << "vec.push_back(B->str());\n";
1716 }
1717 else {
Chris Lattner74382b72009-08-23 22:45:37 +00001718 O << "vec.push_back(inFile.str());\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001719 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001720 }
1721 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001722 O << "vec.push_back(\"\");\n";
1723 O.indent(IndentLevel) << "out_file_index = vec.size()-1;\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001724 }
1725 else {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001726 O << "vec.push_back(";
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001727 I = SubstituteSpecialCommands(I, E, IsJoin, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001728 O << ");\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001729 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001730 }
1731 else {
1732 O << "vec.push_back(\"" << cmd << "\");\n";
1733 }
1734 }
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001735 if (!hasINFILE)
1736 throw "Tool '" + ToolName + "' doesn't take any input!";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001737
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001738 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001739 if (StrVec[0][0] == '$')
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001740 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001741 else
1742 O << '"' << StrVec[0] << '"';
1743 O << ";\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001744}
1745
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001746/// EmitCmdLineVecFillCallback - A function object wrapper around
1747/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1748/// argument to EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001749class EmitCmdLineVecFillCallback {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001750 bool IsJoin;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001751 const std::string& ToolName;
1752 public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001753 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1754 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001755
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001756 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001757 raw_ostream& O) const
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001758 {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001759 EmitCmdLineVecFill(Statement, ToolName, IsJoin, IndentLevel, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001760 }
1761};
1762
1763/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1764/// implement EmitActionHandler. Emits code for
1765/// handling the (forward) and (forward_as) option properties.
1766void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001767 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001768 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001769 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001770 const std::string& Name = NewName.empty()
1771 ? ("-" + D.Name)
1772 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001773 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001774
1775 switch (D.Type) {
1776 case OptionType::Switch:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001777 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001778 break;
1779 case OptionType::Parameter:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001780 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
1781 O.indent(IndentLevel) << "vec.push_back(" << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001782 break;
1783 case OptionType::Prefix:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001784 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\" + "
1785 << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001786 break;
1787 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001788 O.indent(IndentLevel)
1789 << "for (" << D.GenTypeDeclaration()
1790 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1791 O.indent(IndentLevel)
1792 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1793 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\" + " << "*B);\n";
1794 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001795
1796 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001797 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1798 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001799 }
1800
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001801 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001802 break;
1803 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001804 O.indent(IndentLevel)
1805 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1806 << D.GenVariableName() << ".begin(),\n";
1807 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1808 << ".end() ; B != E;) {\n";
1809 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001810
1811 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001812 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1813 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001814 }
1815
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001816 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001817 break;
1818 case OptionType::Alias:
1819 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001820 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001821 }
1822}
1823
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001824/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1825/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001826struct ActionHandlingCallbackBase
1827{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001828
1829 void onErrorDag(const DagInit& d,
1830 unsigned IndentLevel, raw_ostream& O) const
1831 {
1832 O.indent(IndentLevel)
1833 << "throw std::runtime_error(\"" <<
1834 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1835 : "Unknown error!")
1836 << "\");\n";
1837 }
1838
1839 void onWarningDag(const DagInit& d,
1840 unsigned IndentLevel, raw_ostream& O) const
1841 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001842 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001843 O.indent(IndentLevel) << "llvm::errs() << \""
1844 << InitPtrToString(d.getArg(0)) << "\";\n";
1845 }
1846
1847};
1848
1849/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1850/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001851
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001852class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001853
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001854typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1855(const DagInit&, unsigned, raw_ostream&) const;
1856
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001857class EmitActionHandlersCallback :
1858 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001859 public HandlerTable<EmitActionHandlersCallbackHandler>
1860{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001861 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001862
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001863 const OptionDescriptions& OptDescs;
1864
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001865 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1866 /// onAppendCmd and onOutputSuffix.
1867 void EmitHookInvocation(const std::string& Str,
1868 const char* BlockOpen, const char* BlockClose,
1869 unsigned IndentLevel, raw_ostream& O) const
1870 {
1871 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001872 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001873
1874 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1875 B != E; ++B) {
1876 const std::string& cmd = *B;
1877
1878 O.indent(IndentLevel) << BlockOpen;
1879
1880 if (cmd.at(0) == '$')
1881 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1882 else
1883 O << '"' << cmd << '"';
1884
1885 O << BlockClose;
1886 }
1887 }
1888
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001889 void onAppendCmd (const DagInit& Dag,
1890 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001891 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001892 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001893 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
1894 "vec.push_back(", ");\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001895 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001896
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001897 void onForward (const DagInit& Dag,
1898 unsigned IndentLevel, raw_ostream& O) const
1899 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001900 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001901 const std::string& Name = InitPtrToString(Dag.getArg(0));
1902 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1903 IndentLevel, "", O);
1904 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001905
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001906 void onForwardAs (const DagInit& Dag,
1907 unsigned IndentLevel, raw_ostream& O) const
1908 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001909 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001910 const std::string& Name = InitPtrToString(Dag.getArg(0));
1911 const std::string& NewName = InitPtrToString(Dag.getArg(1));
1912 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1913 IndentLevel, NewName, O);
1914 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001915
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001916 void onForwardValue (const DagInit& Dag,
1917 unsigned IndentLevel, raw_ostream& O) const
1918 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001919 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001920 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001921 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001922
1923 if (D.isParameter()) {
1924 O.indent(IndentLevel) << "vec.push_back("
1925 << D.GenVariableName() << ");\n";
1926 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001927 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001928 O.indent(IndentLevel) << "std::copy(" << D.GenVariableName()
1929 << ".begin(), " << D.GenVariableName()
1930 << ".end(), std::back_inserter(vec));\n";
1931 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001932 }
1933
1934 void onForwardTransformedValue (const DagInit& Dag,
1935 unsigned IndentLevel, raw_ostream& O) const
1936 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001937 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001938 const std::string& Name = InitPtrToString(Dag.getArg(0));
1939 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001940 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001941
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001942 O.indent(IndentLevel) << "vec.push_back(" << "hooks::"
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00001943 << Hook << "(" << D.GenVariableName()
1944 << (D.isParameter() ? ".c_str()" : "") << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001945 }
1946
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001947 void onOutputSuffix (const DagInit& Dag,
1948 unsigned IndentLevel, raw_ostream& O) const
1949 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001950 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001951 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
1952 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001953 }
1954
1955 void onStopCompilation (const DagInit& Dag,
1956 unsigned IndentLevel, raw_ostream& O) const
1957 {
1958 O.indent(IndentLevel) << "stop_compilation = true;\n";
1959 }
1960
1961
1962 void onUnpackValues (const DagInit& Dag,
1963 unsigned IndentLevel, raw_ostream& O) const
1964 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001965 throw "'unpack_values' is deprecated. "
1966 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001967 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001968
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001969 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001970
1971 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
1972 : OptDescs(OD)
1973 {
1974 if (!staticMembersInitialized_) {
1975 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
1976 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
1977 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
1978 AddHandler("forward", &EmitActionHandlersCallback::onForward);
1979 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001980 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
1981 AddHandler("forward_transformed_value",
1982 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001983 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
1984 AddHandler("stop_compilation",
1985 &EmitActionHandlersCallback::onStopCompilation);
1986 AddHandler("unpack_values",
1987 &EmitActionHandlersCallback::onUnpackValues);
1988
1989 staticMembersInitialized_ = true;
1990 }
1991 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001992
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001993 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001994 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001995 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001996 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001997 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001998};
1999
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002000bool IsOutFileIndexCheckRequiredStr (const Init* CmdLine) {
2001 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002002 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002003
2004 for (StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
2005 I != E; ++I) {
2006 if (*I == "$OUTFILE")
2007 return false;
2008 }
2009
2010 return true;
2011}
2012
2013class IsOutFileIndexCheckRequiredStrCallback {
2014 bool* ret_;
2015
2016public:
2017 IsOutFileIndexCheckRequiredStrCallback(bool* ret) : ret_(ret)
2018 {}
2019
2020 void operator()(const Init* CmdLine) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002021 // Ignore nested 'case' DAG.
2022 if (typeid(*CmdLine) == typeid(DagInit))
2023 return;
2024
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002025 if (IsOutFileIndexCheckRequiredStr(CmdLine))
2026 *ret_ = true;
2027 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002028
2029 void operator()(const DagInit* Test, unsigned, bool) {
2030 this->operator()(Test);
2031 }
2032 void operator()(const Init* Statement, unsigned) {
2033 this->operator()(Statement);
2034 }
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002035};
2036
2037bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
2038 bool ret = false;
2039 WalkCase(CmdLine, Id(), IsOutFileIndexCheckRequiredStrCallback(&ret));
2040 return ret;
2041}
2042
2043/// IsOutFileIndexCheckRequired - Should we emit an "out_file_index != -1" check
2044/// in EmitGenerateActionMethod() ?
2045bool IsOutFileIndexCheckRequired (Init* CmdLine) {
2046 if (typeid(*CmdLine) == typeid(StringInit))
2047 return IsOutFileIndexCheckRequiredStr(CmdLine);
2048 else
2049 return IsOutFileIndexCheckRequiredCase(CmdLine);
2050}
2051
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002052void EmitGenerateActionMethodHeader(const ToolDescription& D,
2053 bool IsJoin, raw_ostream& O)
2054{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002055 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002056 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002057 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002058 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002059
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002060 O.indent(Indent2) << "bool HasChildren,\n";
2061 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2062 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2063 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2064 O.indent(Indent1) << "{\n";
2065 O.indent(Indent2) << "std::string cmd;\n";
2066 O.indent(Indent2) << "std::vector<std::string> vec;\n";
2067 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2068 O.indent(Indent2) << "const char* output_suffix = \""
2069 << D.OutputSuffix << "\";\n";
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002070}
2071
2072// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2073// Tool::GenerateAction() method.
2074void EmitGenerateActionMethod (const ToolDescription& D,
2075 const OptionDescriptions& OptDescs,
2076 bool IsJoin, raw_ostream& O) {
2077
2078 EmitGenerateActionMethodHeader(D, IsJoin, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002079
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002080 if (!D.CmdLine)
2081 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002082
2083 bool IndexCheckRequired = IsOutFileIndexCheckRequired(D.CmdLine);
2084 O.indent(Indent2) << "int out_file_index"
2085 << (IndexCheckRequired ? " = -1" : "")
2086 << ";\n\n";
2087
2088 // Process the cmd_line property.
2089 if (typeid(*D.CmdLine) == typeid(StringInit))
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002090 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2091 else
2092 EmitCaseConstructHandler(D.CmdLine, Indent2,
2093 EmitCmdLineVecFillCallback(IsJoin, D.Name),
2094 true, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002095
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002096 // For every understood option, emit handling code.
2097 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002098 EmitCaseConstructHandler(D.Actions, Indent2,
2099 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002100 false, OptDescs, O);
2101
2102 O << '\n';
2103 O.indent(Indent2)
2104 << "std::string out_file = OutFilename("
2105 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
2106 O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002107
2108 if (IndexCheckRequired)
2109 O.indent(Indent2) << "if (out_file_index != -1)\n";
2110 O.indent(IndexCheckRequired ? Indent3 : Indent2)
2111 << "vec[out_file_index] = out_file;\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002112
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002113 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002114 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002115 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
2116 O.indent(Indent3) << "vec.insert(vec.end(), "
2117 << SinkOptionName << ".begin(), " << SinkOptionName
2118 << ".end());\n";
2119 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002120 }
2121
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002122 O.indent(Indent2) << "return Action(cmd, vec, stop_compilation, out_file);\n";
2123 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002124}
2125
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002126/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2127/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002128void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2129 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002130 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002131 if (!ToolDesc.isJoin()) {
2132 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
2133 O.indent(Indent2) << "bool HasChildren,\n";
2134 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2135 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2136 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2137 O.indent(Indent1) << "{\n";
2138 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
2139 << " is not a Join tool!\");\n";
2140 O.indent(Indent1) << "}\n\n";
2141 }
2142 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002143 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002144 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002145
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002146 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002147}
2148
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002149/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2150/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002151void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002152 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2153 O.indent(Indent2) << "return InputLanguages_;\n";
2154 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002155
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002156 if (D.OutLanguage.empty())
2157 throw "Tool " + D.Name + " has no 'out_language' property!";
2158
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002159 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2160 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2161 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002162}
2163
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002164/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002165void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002166 O.indent(Indent1) << "const char* Name() const {\n";
2167 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2168 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002169}
2170
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002171/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2172/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002173void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002174 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002175 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002176 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002177 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002178 O.indent(Indent2) << "return false;\n";
2179 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002180}
2181
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002182/// EmitStaticMemberDefinitions - Emit static member definitions for a
2183/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002184void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002185 if (D.InLanguage.empty())
2186 throw "Tool " + D.Name + " has no 'in_language' property!";
2187
2188 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2189 for (StrVector::const_iterator B = D.InLanguage.begin(),
2190 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002191 O << '\"' << *B << "\", ";
2192 O << "0};\n\n";
2193}
2194
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002195/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002196void EmitToolClassDefinition (const ToolDescription& D,
2197 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002198 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002199 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002200 return;
2201
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002202 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002203 O << "class " << D.Name << " : public ";
2204 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002205 O << "JoinTool";
2206 else
2207 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002208
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002209 O << " {\nprivate:\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002210 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002211
2212 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002213 EmitNameMethod(D, O);
2214 EmitInOutLanguageMethods(D, O);
2215 EmitIsJoinMethod(D, O);
2216 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002217
2218 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002219 O << "};\n";
2220
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002221 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002222
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002223}
2224
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002225/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002226/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002227void EmitOptionDefinitions (const OptionDescriptions& descs,
2228 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002229 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002230{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002231 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002232
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002233 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002234 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002235 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002236 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002237
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002238 if (val.Type == OptionType::Alias) {
2239 Aliases.push_back(val);
2240 continue;
2241 }
2242
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002243 if (val.isExtern())
2244 O << "extern ";
2245
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002246 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002247 << val.GenVariableName();
2248
2249 if (val.isExtern()) {
2250 O << ";\n";
2251 continue;
2252 }
2253
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002254 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002255
2256 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2257 O << ", cl::Prefix";
2258
2259 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002260 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002261 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002262 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002263 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002264 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002265 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002266 O << ", cl::OneOrMore";
2267 }
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002268 else if (val.isOptional() && val.isList()) {
2269 O << ", cl::Optional";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002270 }
2271
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002272 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002273 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002274 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002275 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002276
2277 if (val.isCommaSeparated())
2278 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002279
2280 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002281 O << ", cl::multi_val(" << val.MultiVal << ')';
2282
2283 if (val.InitVal) {
2284 const std::string& str = val.InitVal->getAsString();
2285 O << ", cl::init(" << str << ')';
2286 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002287
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002288 if (!val.Help.empty())
2289 O << ", cl::desc(\"" << val.Help << "\")";
2290
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002291 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002292 }
2293
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002294 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002295 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002296 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002297 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002298
2299 O << val.GenTypeDeclaration() << ' '
2300 << val.GenVariableName()
2301 << "(\"" << val.Name << '\"';
2302
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002303 const OptionDescription& D = descs.FindOption(val.Help);
2304 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002305
2306 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2307 }
2308
2309 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002310 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002311 O << (HasExterns ? "extern cl" : "cl")
2312 << "::list<std::string> " << SinkOptionName
2313 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002314
2315 O << '\n';
2316}
2317
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002318/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002319/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002320
2321class EmitPreprocessOptionsCallback;
2322
2323typedef void
2324(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2325(const DagInit&, unsigned, raw_ostream&) const;
2326
2327class EmitPreprocessOptionsCallback :
2328 public ActionHandlingCallbackBase,
2329 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2330{
2331 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002332 typedef void
2333 (EmitPreprocessOptionsCallback::* HandlerImpl)
2334 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002335
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002336 const OptionDescriptions& OptDescs_;
2337
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002338 void onListOrDag(const DagInit& d, HandlerImpl h,
2339 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002340 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002341 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002342 const Init* I = d.getArg(0);
2343
2344 // If I is a list, apply h to each element.
2345 if (typeid(*I) == typeid(ListInit)) {
2346 const ListInit& L = *static_cast<const ListInit*>(I);
2347 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B)
2348 ((this)->*(h))(*B, IndentLevel, O);
2349 }
2350 // Otherwise, apply h to I.
2351 else {
2352 ((this)->*(h))(I, IndentLevel, O);
2353 }
2354 }
2355
2356 void onUnsetOptionImpl(const Init* I,
2357 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002358 {
2359 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002360 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002361
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002362 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002363 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2364 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002365 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002366 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2367 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002368 else if (OptDesc.isList()) {
2369 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2370 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002371 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002372 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002373 }
2374 }
2375
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002376 void onUnsetOption(const DagInit& d,
2377 unsigned IndentLevel, raw_ostream& O) const
2378 {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002379 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2380 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002381 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002382
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002383 void onSetOptionImpl(const DagInit& d,
2384 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002385 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002386 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002387 const Init* Value = d.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002388 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2389
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002390 if (OptDesc.isList()) {
2391 const ListInit& List = InitPtrToList(Value);
2392
2393 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2394 for (ListInit::const_iterator B = List.begin(), E = List.end();
2395 B != E; ++B) {
2396 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".push_back(\""
2397 << InitPtrToString(*B) << "\");\n";
2398 }
2399 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002400 else if (OptDesc.isSwitch()) {
2401 CheckBooleanConstant(Value);
2402 O.indent(IndentLevel) << OptDesc.GenVariableName()
2403 << " = " << Value->getAsString() << ";\n";
2404 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002405 else if (OptDesc.isParameter()) {
2406 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002407 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002408 << " = \"" << Str << "\";\n";
2409 }
2410 else {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002411 throw "Can't apply 'set_option' to alias option -" + OptName + " !";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002412 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002413 }
2414
2415 void onSetSwitch(const Init* I,
2416 unsigned IndentLevel, raw_ostream& O) const {
2417 const std::string& OptName = InitPtrToString(I);
2418 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2419
2420 if (OptDesc.isSwitch())
2421 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2422 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002423 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002424 }
2425
2426 void onSetOption(const DagInit& d,
2427 unsigned IndentLevel, raw_ostream& O) const
2428 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002429 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002430
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002431 // Two arguments: (set_option "parameter", VALUE), where VALUE can be a
2432 // boolean, a string or a string list.
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002433 if (d.getNumArgs() > 1)
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002434 this->onSetOptionImpl(d, IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002435 // One argument: (set_option "switch")
2436 // or (set_option ["switch1", "switch2", ...])
2437 else
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002438 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2439 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002440 }
2441
2442public:
2443
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002444 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002445 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002446 {
2447 if (!staticMembersInitialized_) {
2448 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2449 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2450 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002451 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002452
2453 staticMembersInitialized_ = true;
2454 }
2455 }
2456
2457 void operator()(const Init* I,
2458 unsigned IndentLevel, raw_ostream& O) const
2459 {
2460 InvokeDagInitHandler(this, I, IndentLevel, O);
2461 }
2462
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002463};
2464
2465/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2466void EmitPreprocessOptions (const RecordKeeper& Records,
2467 const OptionDescriptions& OptDecs, raw_ostream& O)
2468{
2469 O << "void PreprocessOptionsLocal() {\n";
2470
2471 const RecordVector& OptionPreprocessors =
2472 Records.getAllDerivedDefinitions("OptionPreprocessor");
2473
2474 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2475 E = OptionPreprocessors.end(); B!=E; ++B) {
2476 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002477 EmitCaseConstructHandler(Case, Indent1,
2478 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002479 false, OptDecs, O);
2480 }
2481
2482 O << "}\n\n";
2483}
2484
2485/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002486void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002487{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002488 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002489
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002490 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002491 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002492
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002493 // It is allowed for a plugin to have no language map.
2494 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002495
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002496 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2497 if (!LangsToSuffixesList)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00002498 throw "Error in the language map definition!";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002499
2500 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002501 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002502
2503 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2504 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2505
2506 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002507 O.indent(Indent1) << "langMap[\""
2508 << InitPtrToString(Suffixes->getElement(i))
2509 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002510 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002511 }
2512
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002513 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002514}
2515
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002516/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2517/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002518void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002519 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002520 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002521 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002522
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002523 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002524 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002525 }
2526 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002527 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002528 }
2529 else if (OpName == "error") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002530 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002531 O.indent(IndentLevel) << "throw std::runtime_error(\""
2532 << InitPtrToString(d.getArg(0))
2533 << "\");\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002534 return;
2535 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002536 else {
2537 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002538 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002539 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002540
2541 if (d.getNumArgs() > 0)
2542 O << InitPtrToInt(d.getArg(0)) << ";\n";
2543 else
2544 O << "2;\n";
2545
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002546}
2547
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002548/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002549void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002550 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002551 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002552
2553 // Class constructor.
2554 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002555 << "public:\n";
2556 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2557 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002558
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002559 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002560 O.indent(Indent1)
2561 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2562 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002563
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002564 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002565 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002566
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002567 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002568 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002569}
2570
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002571/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002572void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002573 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002574 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002575 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002576 for (RecordVector::const_iterator B = EdgeVector.begin(),
2577 E = EdgeVector.end(); B != E; ++B) {
2578 const Record* Edge = *B;
2579 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002580 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002581
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002582 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002583 EmitEdgeClass(i, NodeB, &Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002584 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002585 }
2586}
2587
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002588/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002589/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002590void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002591 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002592 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002593{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002594 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002595
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002596 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2597 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002598 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002599
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002600 O << '\n';
2601
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002602 // Insert edges.
2603
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002604 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002605 for (RecordVector::const_iterator B = EdgeVector.begin(),
2606 E = EdgeVector.end(); B != E; ++B) {
2607 const Record* Edge = *B;
2608 const std::string& NodeA = Edge->getValueAsString("a");
2609 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002610 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002611
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002612 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002613
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002614 if (IsDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002615 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002616 else
2617 O << "new Edge" << i << "()";
2618
2619 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002620 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002621 }
2622
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002623 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002624}
2625
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002626/// HookInfo - Information about the hook type and number of arguments.
2627struct HookInfo {
2628
2629 // A hook can either have a single parameter of type std::vector<std::string>,
2630 // or NumArgs parameters of type const char*.
2631 enum HookType { ListHook, ArgHook };
2632
2633 HookType Type;
2634 unsigned NumArgs;
2635
2636 HookInfo() : Type(ArgHook), NumArgs(1)
2637 {}
2638
2639 HookInfo(HookType T) : Type(T), NumArgs(1)
2640 {}
2641
2642 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2643 {}
2644};
2645
2646typedef llvm::StringMap<HookInfo> HookInfoMap;
2647
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002648/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002649/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002650/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002651class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002652 HookInfoMap& HookNames_;
2653 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002654public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002655 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2656 : HookNames_(HookNames), OptDescs_(OptDescs)
2657 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002658
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002659 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002660 const std::string& Name = GetOperatorName(Dag);
2661
2662 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002663 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002664 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2665 const std::string& HookName = InitPtrToString(Dag.getArg(1));
2666 const OptionDescription& D = OptDescs_.FindOption(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002667
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002668 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2669 : HookInfo::ArgHook);
2670 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002671 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002672 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002673 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2674 }
2675 }
2676
2677 void onCmdLine(const std::string& Cmd) {
2678 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002679 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002680
2681 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2682 B != E; ++B) {
2683 const std::string& cmd = *B;
2684
2685 if (cmd == "$CALL") {
2686 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002687 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002688 const std::string& HookName = *B;
2689
2690 if (HookName.at(0) == ')')
2691 throw "$CALL invoked with no arguments!";
2692
2693 while (++B != E && B->at(0) != ')') {
2694 ++NumArgs;
2695 }
2696
2697 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2698
2699 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2700 H->second.Type != HookInfo::ArgHook)
2701 throw "Overloading of hooks is not allowed. Overloaded hook: "
2702 + HookName;
2703 else
2704 HookNames_[HookName] = HookInfo(NumArgs);
2705 }
2706 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002707 }
2708
2709 void operator()(const Init* Arg) {
2710
2711 // We're invoked on an action (either a dag or a dag list).
2712 if (typeid(*Arg) == typeid(DagInit)) {
2713 const DagInit& Dag = InitPtrToDag(Arg);
2714 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002715 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002716 }
2717 else if (typeid(*Arg) == typeid(ListInit)) {
2718 const ListInit& List = InitPtrToList(Arg);
2719 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2720 ++B) {
2721 const DagInit& Dag = InitPtrToDag(*B);
2722 this->onAction(Dag);
2723 }
2724 return;
2725 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002726
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002727 // We're invoked on a command line.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002728 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002729 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002730
2731 void operator()(const DagInit* Test, unsigned, bool) {
2732 this->operator()(Test);
2733 }
2734 void operator()(const Init* Statement, unsigned) {
2735 this->operator()(Statement);
2736 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002737};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002738
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002739/// FillInHookNames - Actually extract the hook names from all command
2740/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002741void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002742 const OptionDescriptions& OptDescs,
2743 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002744{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002745 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002746 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2747 E = ToolDescs.end(); B != E; ++B) {
2748 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002749
2750 // Look for 'forward_transformed_value' in 'actions'.
2751 if (D.Actions)
2752 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2753
2754 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002755 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002756 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002757 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002758 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002759 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002760 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002761 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002762 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002763 }
2764}
2765
2766/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2767/// property records and emit hook function declaration for each
2768/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002769void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2770 const OptionDescriptions& OptDescs, raw_ostream& O) {
2771 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002772
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002773 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002774 if (HookNames.empty())
2775 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002776
2777 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002778 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002779 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002780 const char* HookName = B->first();
2781 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002782
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002783 O.indent(Indent1) << "std::string " << HookName << "(";
2784
2785 if (Info.Type == HookInfo::ArgHook) {
2786 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2787 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2788 }
2789 }
2790 else {
2791 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002792 }
2793
2794 O <<");\n";
2795 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002796 O << "}\n\n";
2797}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002798
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002799/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002800void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002801 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2802 O.indent(Indent1) << "int Priority() const { return "
2803 << Priority << "; }\n\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002804 O.indent(Indent1) << "void PreprocessOptions() const\n";
2805 O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002806 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2807 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2808 O.indent(Indent1)
2809 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2810 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2811 << "};\n\n"
2812 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002813}
2814
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002815/// EmitIncludes - Emit necessary #include directives and some
2816/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002817void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002818 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2819 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002820 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002821 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2822 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002823
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002824 << "#include \"llvm/Support/CommandLine.h\"\n"
2825 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002826
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002827 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002828 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002829 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002830 << "#include <stdexcept>\n\n"
2831
2832 << "using namespace llvm;\n"
2833 << "using namespace llvmc;\n\n"
2834
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002835 << "extern cl::opt<std::string> OutputFilename;\n\n"
2836
2837 << "inline const char* checkCString(const char* s)\n"
2838 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002839}
2840
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002841
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002842/// PluginData - Holds all information about a plugin.
2843struct PluginData {
2844 OptionDescriptions OptDescs;
2845 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002846 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002847 ToolDescriptions ToolDescs;
2848 RecordVector Edges;
2849 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002850};
2851
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002852/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002853/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002854bool HasSink(const ToolDescriptions& ToolDescs) {
2855 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2856 E = ToolDescs.end(); B != E; ++B)
2857 if ((*B)->isSink())
2858 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002859
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002860 return false;
2861}
2862
2863/// HasExterns - Go through the list of option descriptions and check
2864/// if there are any external options.
2865bool HasExterns(const OptionDescriptions& OptDescs) {
2866 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2867 E = OptDescs.end(); B != E; ++B)
2868 if (B->second.isExtern())
2869 return true;
2870
2871 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002872}
2873
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002874/// CollectPluginData - Collect tool and option properties,
2875/// compilation graph edges and plugin priority from the parse tree.
2876void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2877 // Collect option properties.
2878 const RecordVector& OptionLists =
2879 Records.getAllDerivedDefinitions("OptionList");
2880 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2881 Data.OptDescs);
2882
2883 // Collect tool properties.
2884 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2885 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2886 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002887 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002888
2889 // Collect compilation graph edges.
2890 const RecordVector& CompilationGraphs =
2891 Records.getAllDerivedDefinitions("CompilationGraph");
2892 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2893 Data.Edges);
2894
2895 // Calculate the priority of this plugin.
2896 const RecordVector& Priorities =
2897 Records.getAllDerivedDefinitions("PluginPriority");
2898 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002899}
2900
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002901/// CheckPluginData - Perform some sanity checks on the collected data.
2902void CheckPluginData(PluginData& Data) {
2903 // Filter out all tools not mentioned in the compilation graph.
2904 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002905
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002906 // Typecheck the compilation graph.
2907 TypecheckGraph(Data.Edges, Data.ToolDescs);
2908
2909 // Check that there are no options without side effects (specified
2910 // only in the OptionList).
2911 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002912}
2913
Daniel Dunbar1a551802009-07-03 00:10:29 +00002914void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002915 // Emit file header.
2916 EmitIncludes(O);
2917
2918 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002919 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002920
2921 // Emit hook declarations.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002922 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002923
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002924 O << "namespace {\n\n";
2925
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002926 // Emit PreprocessOptionsLocal() function.
2927 EmitPreprocessOptions(Records, Data.OptDescs, O);
2928
2929 // Emit PopulateLanguageMapLocal() function
2930 // (language map maps from file extensions to language names).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002931 EmitPopulateLanguageMap(Records, O);
2932
2933 // Emit Tool classes.
2934 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2935 E = Data.ToolDescs.end(); B!=E; ++B)
2936 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2937
2938 // Emit Edge# classes.
2939 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2940
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002941 // Emit PopulateCompilationGraphLocal() function.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002942 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2943
2944 // Emit code for plugin registration.
2945 EmitRegisterPlugin(Data.Priority, O);
2946
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002947 O << "} // End anonymous namespace.\n\n";
2948
2949 // Force linkage magic.
2950 O << "namespace llvmc {\n";
2951 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2952 O << "}\n";
2953
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002954 // EOF
2955}
2956
2957
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002958// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00002959}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002960
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002961/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002962void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002963 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002964 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002965
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002966 CollectPluginData(Records, Data);
2967 CheckPluginData(Data);
2968
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +00002969 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002970 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002971
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002972 } catch (std::exception& Error) {
2973 throw Error.what() + std::string(" - usually this means a syntax error.");
2974 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002975}