blob: b7ac85c5ed54489488d9516bcfb143fc083dcb73 [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
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +000028
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000029using namespace llvm;
30
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000031namespace {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000032
33//===----------------------------------------------------------------------===//
34/// Typedefs
35
36typedef std::vector<Record*> RecordVector;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +000037typedef std::vector<const DagInit*> DagVector;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000038typedef std::vector<std::string> StrVector;
39
40//===----------------------------------------------------------------------===//
41/// Constants
42
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +000043// Indentation.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000044const unsigned TabWidth = 4;
45const unsigned Indent1 = TabWidth*1;
46const unsigned Indent2 = TabWidth*2;
47const unsigned Indent3 = TabWidth*3;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +000048const unsigned Indent4 = TabWidth*4;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000049
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000050// Default help string.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000051const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000052
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000053// Name for the "sink" option.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +000054const char * const SinkOptionName = "SinkOption";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000055
56//===----------------------------------------------------------------------===//
57/// Helper functions
58
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000059/// Id - An 'identity' function object.
60struct Id {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000061 template<typename T0>
62 void operator()(const T0&) const {
63 }
64 template<typename T0, typename T1>
65 void operator()(const T0&, const T1&) const {
66 }
67 template<typename T0, typename T1, typename T2>
68 void operator()(const T0&, const T1&, const T2&) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000069 }
70};
71
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000072int InitPtrToInt(const Init* ptr) {
73 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000074 return val.getValue();
75}
76
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000077const std::string& InitPtrToString(const Init* ptr) {
78 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
79 return val.getValue();
80}
81
82const ListInit& InitPtrToList(const Init* ptr) {
83 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
84 return val;
85}
86
87const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000088 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000089 return val;
90}
91
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000092const std::string GetOperatorName(const DagInit& D) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +000093 return D.getOperator()->getAsString();
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000094}
95
Mikhail Glushenkove0b65702009-12-23 12:49:30 +000096/// CheckBooleanConstant - Check that the provided value is a boolean constant.
97void CheckBooleanConstant(const Init* I) {
98 const DefInit& val = dynamic_cast<const DefInit&>(*I);
99 const std::string& str = val.getAsString();
100
101 if (str != "true" && str != "false") {
102 throw "Incorrect boolean value: '" + str +
103 "': must be either 'true' or 'false'";
104 }
105}
106
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000107// CheckNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +0000108// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000109void CheckNumberOfArguments (const DagInit& d, unsigned minArgs) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000110 if (d.getNumArgs() < minArgs)
111 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +0000112}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000113
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000114// EscapeVariableName - Escape commas and other symbols not allowed
115// in the C++ variable names. Makes it possible to use options named
116// like "Wa," (useful for prefix options).
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000117std::string EscapeVariableName (const std::string& Var) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000118 std::string ret;
119 for (unsigned i = 0; i != Var.size(); ++i) {
120 char cur_char = Var[i];
121 if (cur_char == ',') {
122 ret += "_comma_";
123 }
124 else if (cur_char == '+') {
125 ret += "_plus_";
126 }
127 else if (cur_char == '-') {
128 ret += "_dash_";
129 }
130 else {
131 ret.push_back(cur_char);
132 }
133 }
134 return ret;
135}
136
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000137/// EscapeQuotes - Replace '"' with '\"'.
138std::string EscapeQuotes (const std::string& Var) {
139 std::string ret;
140 for (unsigned i = 0; i != Var.size(); ++i) {
141 char cur_char = Var[i];
142 if (cur_char == '"') {
143 ret += "\\\"";
144 }
145 else {
146 ret.push_back(cur_char);
147 }
148 }
149 return ret;
150}
151
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000152/// OneOf - Does the input string contain this character?
153bool OneOf(const char* lst, char c) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000154 while (*lst) {
155 if (*lst++ == c)
156 return true;
157 }
158 return false;
159}
160
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000161template <class I, class S>
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000162void CheckedIncrement(I& P, I E, S ErrorString) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000163 ++P;
164 if (P == E)
165 throw ErrorString;
166}
167
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000168//===----------------------------------------------------------------------===//
169/// Back-end specific code
170
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000171
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000172/// OptionType - One of six different option types. See the
173/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000174namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000175
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000176 enum OptionType { Alias, Switch, SwitchList,
177 Parameter, ParameterList, Prefix, PrefixList };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000178
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000179 bool IsAlias(OptionType t) {
180 return (t == Alias);
181 }
182
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000183 bool IsList (OptionType t) {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000184 return (t == SwitchList || t == ParameterList || t == PrefixList);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000185 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000186
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000187 bool IsSwitch (OptionType t) {
188 return (t == Switch);
189 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000190
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000191 bool IsSwitchList (OptionType t) {
192 return (t == SwitchList);
193 }
194
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000195 bool IsParameter (OptionType t) {
196 return (t == Parameter || t == Prefix);
197 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000198
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000199}
200
201OptionType::OptionType stringToOptionType(const std::string& T) {
202 if (T == "alias_option")
203 return OptionType::Alias;
204 else if (T == "switch_option")
205 return OptionType::Switch;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000206 else if (T == "switch_list_option")
207 return OptionType::SwitchList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000208 else if (T == "parameter_option")
209 return OptionType::Parameter;
210 else if (T == "parameter_list_option")
211 return OptionType::ParameterList;
212 else if (T == "prefix_option")
213 return OptionType::Prefix;
214 else if (T == "prefix_list_option")
215 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000216 else
217 throw "Unknown option type: " + T + '!';
218}
219
220namespace OptionDescriptionFlags {
221 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000222 ReallyHidden = 0x4, OneOrMore = 0x8,
223 Optional = 0x10, CommaSeparated = 0x20,
224 ForwardNotSplit = 0x40, ZeroOrMore = 0x80 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000225}
226
227/// OptionDescription - Represents data contained in a single
228/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000229struct OptionDescription {
230 OptionType::OptionType Type;
231 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000232 unsigned Flags;
233 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000234 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000235 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000236
237 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000238 const std::string& n = "",
239 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000240 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000241 {}
242
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000243 /// GenTypeDeclaration - Returns the C++ variable type of this
244 /// option.
245 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000246
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000247 /// GenVariableName - Returns the variable name used in the
248 /// generated C++ code.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000249 std::string GenVariableName() const
250 { return "autogenerated::" + GenOptionType() + EscapeVariableName(Name); }
251
252 /// GenPlainVariableName - Returns the variable name without the namespace
253 /// prefix.
254 std::string GenPlainVariableName() const
255 { return GenOptionType() + EscapeVariableName(Name); }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000256
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000257 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000258 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000259
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000260 /// CheckConsistency - Check that the flags are consistent.
261 void CheckConsistency() const;
262
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000263 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000264
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000265 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000266
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000267 bool isMultiVal() const;
268
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000269 bool isCommaSeparated() const;
270 void setCommaSeparated();
271
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000272 bool isForwardNotSplit() const;
273 void setForwardNotSplit();
274
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000275 bool isRequired() const;
276 void setRequired();
277
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000278 bool isOneOrMore() const;
279 void setOneOrMore();
280
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000281 bool isZeroOrMore() const;
282 void setZeroOrMore();
283
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000284 bool isOptional() const;
285 void setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000286
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000287 bool isHidden() const;
288 void setHidden();
289
290 bool isReallyHidden() const;
291 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000292
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000293 bool isSwitch() const
294 { return OptionType::IsSwitch(this->Type); }
295
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000296 bool isSwitchList() const
297 { return OptionType::IsSwitchList(this->Type); }
298
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000299 bool isParameter() const
300 { return OptionType::IsParameter(this->Type); }
301
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000302 bool isList() const
303 { return OptionType::IsList(this->Type); }
304
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000305 bool isParameterList() const
306 { return (OptionType::IsList(this->Type)
307 && !OptionType::IsSwitchList(this->Type)); }
308
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000309private:
310
311 // GenOptionType - Helper function used by GenVariableName().
312 std::string GenOptionType() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000313};
314
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000315void OptionDescription::CheckConsistency() const {
316 unsigned i = 0;
317
318 i += this->isRequired();
319 i += this->isOptional();
320 i += this->isOneOrMore();
321 i += this->isZeroOrMore();
322
323 if (i > 1) {
324 throw "Only one of (required), (optional), (one_or_more) or "
325 "(zero_or_more) properties is allowed!";
326 }
327}
328
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000329void OptionDescription::Merge (const OptionDescription& other)
330{
331 if (other.Type != Type)
332 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000333
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000334 if (Help == other.Help || Help == DefaultHelpString)
335 Help = other.Help;
336 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000337 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000338 " defined for option " + Name + "\n";
339 }
340
341 Flags |= other.Flags;
342}
343
344bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000345 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000346}
347
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000348bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000349 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000350}
351
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000352bool OptionDescription::isCommaSeparated() const {
353 return Flags & OptionDescriptionFlags::CommaSeparated;
354}
355void OptionDescription::setCommaSeparated() {
356 Flags |= OptionDescriptionFlags::CommaSeparated;
357}
358
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000359bool OptionDescription::isForwardNotSplit() const {
360 return Flags & OptionDescriptionFlags::ForwardNotSplit;
361}
362void OptionDescription::setForwardNotSplit() {
363 Flags |= OptionDescriptionFlags::ForwardNotSplit;
364}
365
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000366bool OptionDescription::isRequired() const {
367 return Flags & OptionDescriptionFlags::Required;
368}
369void OptionDescription::setRequired() {
370 Flags |= OptionDescriptionFlags::Required;
371}
372
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000373bool OptionDescription::isOneOrMore() const {
374 return Flags & OptionDescriptionFlags::OneOrMore;
375}
376void OptionDescription::setOneOrMore() {
377 Flags |= OptionDescriptionFlags::OneOrMore;
378}
379
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000380bool OptionDescription::isZeroOrMore() const {
381 return Flags & OptionDescriptionFlags::ZeroOrMore;
382}
383void OptionDescription::setZeroOrMore() {
384 Flags |= OptionDescriptionFlags::ZeroOrMore;
385}
386
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000387bool OptionDescription::isOptional() const {
388 return Flags & OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000389}
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000390void OptionDescription::setOptional() {
391 Flags |= OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000392}
393
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000394bool OptionDescription::isHidden() const {
395 return Flags & OptionDescriptionFlags::Hidden;
396}
397void OptionDescription::setHidden() {
398 Flags |= OptionDescriptionFlags::Hidden;
399}
400
401bool OptionDescription::isReallyHidden() const {
402 return Flags & OptionDescriptionFlags::ReallyHidden;
403}
404void OptionDescription::setReallyHidden() {
405 Flags |= OptionDescriptionFlags::ReallyHidden;
406}
407
408const char* OptionDescription::GenTypeDeclaration() const {
409 switch (Type) {
410 case OptionType::Alias:
411 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000412 case OptionType::PrefixList:
413 case OptionType::ParameterList:
414 return "cl::list<std::string>";
415 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000416 return "cl::opt<bool>";
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000417 case OptionType::SwitchList:
418 return "cl::list<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000419 case OptionType::Parameter:
420 case OptionType::Prefix:
421 default:
422 return "cl::opt<std::string>";
423 }
424}
425
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000426std::string OptionDescription::GenOptionType() const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000427 switch (Type) {
428 case OptionType::Alias:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000429 return "Alias_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000430 case OptionType::PrefixList:
431 case OptionType::ParameterList:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000432 return "List_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000433 case OptionType::Switch:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000434 return "Switch_";
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000435 case OptionType::SwitchList:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000436 return "SwitchList_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000437 case OptionType::Prefix:
438 case OptionType::Parameter:
439 default:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000440 return "Parameter_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000441 }
442}
443
444/// OptionDescriptions - An OptionDescription array plus some helper
445/// functions.
446class OptionDescriptions {
447 typedef StringMap<OptionDescription> container_type;
448
449 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000450 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000451
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000452public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000453 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000454 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000455
456 // Wrappers for FindOption that throw an exception in case the option has a
457 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000458 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000459 const OptionDescription& FindParameter(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000460 const OptionDescription& FindParameterList(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000461 const OptionDescription&
462 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000463 const OptionDescription&
464 FindParameterListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000465
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000466 /// insertDescription - Insert new OptionDescription into
467 /// OptionDescriptions list
468 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000469
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000470 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000471 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000472 const_iterator begin() const { return Descriptions.begin(); }
473 const_iterator end() const { return Descriptions.end(); }
474};
475
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000476const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000477OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000478 const_iterator I = Descriptions.find(OptName);
479 if (I != Descriptions.end())
480 return I->second;
481 else
482 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000483}
484
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000485const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000486OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000487 const OptionDescription& OptDesc = this->FindOption(OptName);
488 if (!OptDesc.isSwitch())
489 throw OptName + ": incorrect option type - should be a switch!";
490 return OptDesc;
491}
492
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000493const OptionDescription&
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000494OptionDescriptions::FindParameterList(const std::string& OptName) const {
495 const OptionDescription& OptDesc = this->FindOption(OptName);
496 if (!OptDesc.isList() || OptDesc.isSwitchList())
497 throw OptName + ": incorrect option type - should be a parameter list!";
498 return OptDesc;
499}
500
501const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000502OptionDescriptions::FindParameter(const std::string& OptName) const {
503 const OptionDescription& OptDesc = this->FindOption(OptName);
504 if (!OptDesc.isParameter())
505 throw OptName + ": incorrect option type - should be a parameter!";
506 return OptDesc;
507}
508
509const OptionDescription&
510OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
511 const OptionDescription& OptDesc = this->FindOption(OptName);
512 if (!OptDesc.isList() && !OptDesc.isParameter())
513 throw OptName
514 + ": incorrect option type - should be a list or parameter!";
515 return OptDesc;
516}
517
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000518const OptionDescription&
519OptionDescriptions::FindParameterListOrParameter
520(const std::string& OptName) const {
521 const OptionDescription& OptDesc = this->FindOption(OptName);
522 if ((!OptDesc.isList() && !OptDesc.isParameter()) || OptDesc.isSwitchList())
523 throw OptName
524 + ": incorrect option type - should be a parameter list or parameter!";
525 return OptDesc;
526}
527
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000528void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000529 container_type::iterator I = Descriptions.find(o.Name);
530 if (I != Descriptions.end()) {
531 OptionDescription& D = I->second;
532 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000533 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000534 else {
535 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000536 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000537}
538
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000539/// HandlerTable - A base class for function objects implemented as
540/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000541template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000542class HandlerTable {
543protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000544 // Implementation details.
545
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000546 /// HandlerMap - A map from property names to property handlers
547 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000548
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000549 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000550 static bool staticMembersInitialized_;
551
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000552public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000553
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000554 Handler GetHandler (const std::string& HandlerName) const {
555 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000556
557 if (method != Handlers_.end()) {
558 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000559 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000560 }
561 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000562 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000563 }
564 }
565
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000566 void AddHandler(const char* Property, Handler H) {
567 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000568 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000569
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000570};
571
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000572template <class Handler, class FunctionObject>
573Handler GetHandler(FunctionObject* Obj, const DagInit& Dag) {
574 const std::string& HandlerName = GetOperatorName(Dag);
575 return Obj->GetHandler(HandlerName);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000576}
577
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000578template <class FunctionObject>
579void InvokeDagInitHandler(FunctionObject* Obj, Init* I) {
580 typedef void (FunctionObject::*Handler) (const DagInit&);
581
582 const DagInit& Dag = InitPtrToDag(I);
583 Handler h = GetHandler<Handler>(Obj, Dag);
584
585 ((Obj)->*(h))(Dag);
586}
587
588template <class FunctionObject>
589void InvokeDagInitHandler(const FunctionObject* const Obj,
590 const Init* I, unsigned IndentLevel, raw_ostream& O)
591{
592 typedef void (FunctionObject::*Handler)
593 (const DagInit&, unsigned IndentLevel, raw_ostream& O) const;
594
595 const DagInit& Dag = InitPtrToDag(I);
596 Handler h = GetHandler<Handler>(Obj, Dag);
597
598 ((Obj)->*(h))(Dag, IndentLevel, O);
599}
600
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000601template <typename H>
602typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
603
604template <typename H>
605bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000606
607
608/// CollectOptionProperties - Function object for iterating over an
609/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000610class CollectOptionProperties;
611typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000612(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000613
614class CollectOptionProperties
615: public HandlerTable<CollectOptionPropertiesHandler>
616{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000617private:
618
619 /// optDescs_ - OptionDescriptions table. This is where the
620 /// information is stored.
621 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000622
623public:
624
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000625 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000626 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000627 {
628 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000629 AddHandler("help", &CollectOptionProperties::onHelp);
630 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000631 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000632 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
633 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000634 AddHandler("zero_or_more", &CollectOptionProperties::onZeroOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000635 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
636 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000637 AddHandler("optional", &CollectOptionProperties::onOptional);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000638 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000639 AddHandler("forward_not_split",
640 &CollectOptionProperties::onForwardNotSplit);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000641
642 staticMembersInitialized_ = true;
643 }
644 }
645
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000646 /// operator() - Just forwards to the corresponding property
647 /// handler.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000648 void operator() (Init* I) {
649 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000650 }
651
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000652private:
653
654 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000655 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000656
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000657 void onHelp (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000658 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000659 optDesc_.Help = EscapeQuotes(InitPtrToString(d.getArg(0)));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000660 }
661
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000662 void onHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000663 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000664 optDesc_.setHidden();
665 }
666
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000667 void onReallyHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000668 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000669 optDesc_.setReallyHidden();
670 }
671
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000672 void onCommaSeparated (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000673 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000674 if (!optDesc_.isParameterList())
675 throw "'comma_separated' is valid only on parameter list options!";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000676 optDesc_.setCommaSeparated();
677 }
678
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000679 void onForwardNotSplit (const DagInit& d) {
680 CheckNumberOfArguments(d, 0);
681 if (!optDesc_.isParameter())
682 throw "'forward_not_split' is valid only for parameter options!";
683 optDesc_.setForwardNotSplit();
684 }
685
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000686 void onRequired (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000687 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000688
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000689 optDesc_.setRequired();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000690 optDesc_.CheckConsistency();
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000691 }
692
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000693 void onInit (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000694 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000695 Init* i = d.getArg(0);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000696 const std::string& str = i->getAsString();
697
698 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
699 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
700
701 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000702 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000703
704 optDesc_.InitVal = i;
705 }
706
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000707 void onOneOrMore (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000708 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000709
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000710 optDesc_.setOneOrMore();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000711 optDesc_.CheckConsistency();
712 }
713
714 void onZeroOrMore (const DagInit& d) {
715 CheckNumberOfArguments(d, 0);
716
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000717 if (optDesc_.isList())
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000718 llvm::errs() << "Warning: specifying the 'zero_or_more' property "
719 "on a list option has no effect.\n";
720
721 optDesc_.setZeroOrMore();
722 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000723 }
724
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000725 void onOptional (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000726 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000727
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000728 if (!optDesc_.isList())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000729 llvm::errs() << "Warning: specifying the 'optional' property"
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000730 "on a non-list option has no effect.\n";
731
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000732 optDesc_.setOptional();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000733 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000734 }
735
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000736 void onMultiVal (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000737 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000738 int val = InitPtrToInt(d.getArg(0));
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000739 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000740 throw "Error in the 'multi_val' property: "
741 "the value must be greater than 1!";
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000742 if (!optDesc_.isParameterList())
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000743 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000744 optDesc_.MultiVal = val;
745 }
746
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000747};
748
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000749/// AddOption - A function object that is applied to every option
750/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000751class AddOption {
752private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000753 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000754
755public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000756 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000757 {}
758
759 void operator()(const Init* i) {
760 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000761 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000762
763 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000764 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000765 const std::string& Name = InitPtrToString(d.getArg(0));
766
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000767 OptionDescription OD(Type, Name);
768
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000769 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000770
771 if (OD.isAlias()) {
772 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000773 OD.Help = InitPtrToString(d.getArg(1));
774 }
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000775 else {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000776 processOptionProperties(d, OD);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000777 }
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000778
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000779 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000780 }
781
782private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000783 /// processOptionProperties - Go through the list of option
784 /// properties and call a corresponding handler for each.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000785 static void processOptionProperties (const DagInit& d, OptionDescription& o) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000786 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000787 DagInit::const_arg_iterator B = d.arg_begin();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000788 // Skip the first argument: it's always the option name.
789 ++B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000790 std::for_each(B, d.arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000791 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000792
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000793};
794
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000795/// CollectOptionDescriptions - Collects option properties from all
796/// OptionLists.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000797void CollectOptionDescriptions (const RecordVector& V,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000798 OptionDescriptions& OptDescs)
799{
800 // For every OptionList:
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000801 for (RecordVector::const_iterator B = V.begin(), E = V.end(); B!=E; ++B)
802 {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000803 // Throws an exception if the value does not exist.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000804 ListInit* PropList = (*B)->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000805
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000806 // For every option description in this list: invoke AddOption.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000807 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
808 }
809}
810
811// Tool information record
812
813namespace ToolFlags {
814 enum ToolFlags { Join = 0x1, Sink = 0x2 };
815}
816
817struct ToolDescription : public RefCountedBase<ToolDescription> {
818 std::string Name;
819 Init* CmdLine;
820 Init* Actions;
821 StrVector InLanguage;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000822 std::string InFileOption;
823 std::string OutFileOption;
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000824 StrVector OutLanguage;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000825 std::string OutputSuffix;
826 unsigned Flags;
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000827 const Init* OnEmpty;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000828
829 // Various boolean properties
830 void setSink() { Flags |= ToolFlags::Sink; }
831 bool isSink() const { return Flags & ToolFlags::Sink; }
832 void setJoin() { Flags |= ToolFlags::Join; }
833 bool isJoin() const { return Flags & ToolFlags::Join; }
834
835 // Default ctor here is needed because StringMap can only store
836 // DefaultConstructible objects
Chris Lattner2d900582010-08-28 03:43:50 +0000837 ToolDescription (const std::string &n = "")
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000838 : Name(n), CmdLine(0), Actions(0), OutFileOption("-o"),
839 Flags(0), OnEmpty(0)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000840 {}
841};
842
843/// ToolDescriptions - A list of Tool information records.
844typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
845
846
847/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000848/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000849
850class CollectToolProperties;
851typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000852(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000853
854class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
855{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000856private:
857
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000858 /// toolDesc_ - Properties of the current Tool. This is where the
859 /// information is stored.
860 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000861
862public:
863
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000864 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000865 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000866 {
867 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000868
869 AddHandler("actions", &CollectToolProperties::onActions);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000870 AddHandler("command", &CollectToolProperties::onCommand);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000871 AddHandler("in_language", &CollectToolProperties::onInLanguage);
872 AddHandler("join", &CollectToolProperties::onJoin);
873 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000874
875 AddHandler("out_file_option", &CollectToolProperties::onOutFileOption);
876 AddHandler("in_file_option", &CollectToolProperties::onInFileOption);
877
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000878 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
879 AddHandler("sink", &CollectToolProperties::onSink);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000880 AddHandler("works_on_empty", &CollectToolProperties::onWorksOnEmpty);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000881
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000882 staticMembersInitialized_ = true;
883 }
884 }
885
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000886 void operator() (Init* I) {
887 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000888 }
889
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000890private:
891
892 /// Property handlers --
893 /// Functions that extract information about tool properties from
894 /// DAG representation.
895
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000896 void onActions (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000897 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000898 Init* Case = d.getArg(0);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000899 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000900 GetOperatorName(static_cast<DagInit&>(*Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000901 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000902 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000903 }
904
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000905 void onCommand (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000906 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000907 toolDesc_.CmdLine = d.getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000908 }
909
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000910 /// onInOutLanguage - Common implementation of on{In,Out}Language().
911 void onInOutLanguage (const DagInit& d, StrVector& OutVec) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000912 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000913
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000914 // Copy strings to the output vector.
915 for (unsigned i = 0, NumArgs = d.getNumArgs(); i < NumArgs; ++i) {
916 OutVec.push_back(InitPtrToString(d.getArg(i)));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000917 }
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000918
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000919 // Remove duplicates.
920 std::sort(OutVec.begin(), OutVec.end());
921 StrVector::iterator newE = std::unique(OutVec.begin(), OutVec.end());
922 OutVec.erase(newE, OutVec.end());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000923 }
924
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000925
926 void onInLanguage (const DagInit& d) {
927 this->onInOutLanguage(d, toolDesc_.InLanguage);
928 }
929
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000930 void onJoin (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000931 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000932 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000933 }
934
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000935 void onOutLanguage (const DagInit& d) {
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000936 this->onInOutLanguage(d, toolDesc_.OutLanguage);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000937 }
938
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000939 void onOutFileOption (const DagInit& d) {
940 CheckNumberOfArguments(d, 1);
941 toolDesc_.OutFileOption = InitPtrToString(d.getArg(0));
942 }
943
944 void onInFileOption (const DagInit& d) {
945 CheckNumberOfArguments(d, 1);
946 toolDesc_.InFileOption = InitPtrToString(d.getArg(0));
947 }
948
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000949 void onOutputSuffix (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000950 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000951 toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000952 }
953
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000954 void onSink (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000955 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000956 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000957 }
958
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000959 void onWorksOnEmpty (const DagInit& d) {
960 toolDesc_.OnEmpty = d.getArg(0);
961 }
962
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000963};
964
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000965/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000966/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000967/// CollectToolProperties function object).
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000968void CollectToolDescriptions (const RecordVector& Tools,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000969 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000970{
971 // Iterate over a properties list of every Tool definition
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000972 for (RecordVector::const_iterator B = Tools.begin(),
973 E = Tools.end(); B!=E; ++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000974 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000975 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000976 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000977
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000978 IntrusiveRefCntPtr<ToolDescription>
979 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000980
981 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000982 CollectToolProperties(*ToolDesc));
983 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000984 }
985}
986
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000987/// FillInEdgeVector - Merge all compilation graph definitions into
988/// one single edge list.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000989void FillInEdgeVector(const RecordVector& CompilationGraphs,
990 DagVector& Out) {
991 for (RecordVector::const_iterator B = CompilationGraphs.begin(),
992 E = CompilationGraphs.end(); B != E; ++B) {
993 const ListInit* Edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000994
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000995 for (ListInit::const_iterator B = Edges->begin(),
996 E = Edges->end(); B != E; ++B) {
997 Out.push_back(&InitPtrToDag(*B));
998 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000999 }
1000}
1001
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001002/// NotInGraph - Helper function object for FilterNotInGraph.
1003struct NotInGraph {
1004private:
1005 const llvm::StringSet<>& ToolsInGraph_;
1006
1007public:
1008 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
1009 : ToolsInGraph_(ToolsInGraph)
1010 {}
1011
1012 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
1013 return (ToolsInGraph_.count(x->Name) == 0);
1014 }
1015};
1016
1017/// FilterNotInGraph - Filter out from ToolDescs all Tools not
1018/// mentioned in the compilation graph definition.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001019void FilterNotInGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001020 ToolDescriptions& ToolDescs) {
1021
1022 // List all tools mentioned in the graph.
1023 llvm::StringSet<> ToolsInGraph;
1024
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001025 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001026 E = EdgeVector.end(); B != E; ++B) {
1027
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001028 const DagInit* Edge = *B;
1029 const std::string& NodeA = InitPtrToString(Edge->getArg(0));
1030 const std::string& NodeB = InitPtrToString(Edge->getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001031
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001032 if (NodeA != "root")
1033 ToolsInGraph.insert(NodeA);
1034 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001035 }
1036
1037 // Filter ToolPropertiesList.
1038 ToolDescriptions::iterator new_end =
1039 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
1040 NotInGraph(ToolsInGraph));
1041 ToolDescs.erase(new_end, ToolDescs.end());
1042}
1043
1044/// FillInToolToLang - Fills in two tables that map tool names to
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001045/// input & output language names. Helper function used by TypecheckGraph().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001046void FillInToolToLang (const ToolDescriptions& ToolDescs,
1047 StringMap<StringSet<> >& ToolToInLang,
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001048 StringMap<StringSet<> >& ToolToOutLang) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001049 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1050 E = ToolDescs.end(); B != E; ++B) {
1051 const ToolDescription& D = *(*B);
1052 for (StrVector::const_iterator B = D.InLanguage.begin(),
1053 E = D.InLanguage.end(); B != E; ++B)
1054 ToolToInLang[D.Name].insert(*B);
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001055 for (StrVector::const_iterator B = D.OutLanguage.begin(),
1056 E = D.OutLanguage.end(); B != E; ++B)
1057 ToolToOutLang[D.Name].insert(*B);
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001058 }
1059}
1060
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001061/// Intersect - Is set intersection non-empty?
1062bool Intersect (const StringSet<>& S1, const StringSet<>& S2) {
1063 for (StringSet<>::const_iterator B = S1.begin(), E = S1.end(); B != E; ++B) {
1064 if (S2.count(B->first()) != 0)
1065 return true;
1066 }
1067 return false;
1068}
1069
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001070/// TypecheckGraph - Check that names for output and input languages
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00001071/// on all edges do match.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001072void TypecheckGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001073 const ToolDescriptions& ToolDescs) {
1074 StringMap<StringSet<> > ToolToInLang;
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001075 StringMap<StringSet<> > ToolToOutLang;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001076
1077 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001078
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001079 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001080 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001081 const DagInit* Edge = *B;
1082 const std::string& NodeA = InitPtrToString(Edge->getArg(0));
1083 const std::string& NodeB = InitPtrToString(Edge->getArg(1));
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001084 StringMap<StringSet<> >::iterator IA = ToolToOutLang.find(NodeA);
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001085 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001086
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001087 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001088 throw "Edges back to the root are not allowed!";
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001089
1090 if (NodeA != "root") {
1091 if (IA == ToolToOutLang.end())
1092 throw NodeA + ": no output language defined!";
1093 if (IB == ToolToInLang.end())
1094 throw NodeB + ": no input language defined!";
1095
1096 if (!Intersect(IA->second, IB->second)) {
1097 throw "Edge " + NodeA + "->" + NodeB
1098 + ": output->input language mismatch";
1099 }
1100 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001101 }
1102}
1103
1104/// WalkCase - Walks the 'case' expression DAG and invokes
1105/// TestCallback on every test, and StatementCallback on every
1106/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001107/// combinators (that is, they are passed directly to TestCallback).
1108/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1109/// IndentLevel, bool FirstTest)'.
1110/// StatementCallback must have type 'void StatementCallback(const Init*,
1111/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001112template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001113void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1114 unsigned IndentLevel = 0)
1115{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001116 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001117
1118 // Error checks.
1119 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001120 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001121
1122 if (d.getNumArgs() < 2)
1123 throw "There should be at least one clause in the 'case' expression:\n"
1124 + d.getAsString();
1125
1126 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001127 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001128 const unsigned numArgs = d.getNumArgs();
1129 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001130 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1131 B != E; ++B) {
1132 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001133
1134 if (!even)
1135 {
1136 // Handle test.
1137 const DagInit& Test = InitPtrToDag(arg);
1138
1139 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001140 throw "The 'default' clause should be the last in the "
1141 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001142 if (i == numArgs)
1143 throw "Case construct handler: no corresponding action "
1144 "found for the test " + Test.getAsString() + '!';
1145
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001146 TestCallback(Test, IndentLevel, (i == 1));
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001147 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001148 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001149 {
1150 if (dynamic_cast<DagInit*>(arg)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001151 && GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001152 // Nested 'case'.
1153 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1154 }
1155
1156 // Handle statement.
1157 StatementCallback(arg, IndentLevel);
1158 }
1159
1160 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001161 even = !even;
1162 }
1163}
1164
1165/// ExtractOptionNames - A helper function object used by
1166/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1167class ExtractOptionNames {
1168 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001169
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001170 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001171 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001172 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001173 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001174 ActionName == "forward_value" ||
1175 ActionName == "forward_transformed_value" ||
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001176 ActionName == "parameter_equals" || ActionName == "element_in_list") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001177 CheckNumberOfArguments(Stmt, 1);
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001178
1179 Init* Arg = Stmt.getArg(0);
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001180 if (typeid(*Arg) == typeid(StringInit))
1181 OptionNames_.insert(InitPtrToString(Arg));
1182 }
1183 else if (ActionName == "any_switch_on" || ActionName == "switch_on" ||
1184 ActionName == "any_not_empty" || ActionName == "any_empty" ||
1185 ActionName == "not_empty" || ActionName == "empty") {
1186 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
1187 Init* Arg = Stmt.getArg(i);
1188 if (typeid(*Arg) == typeid(StringInit))
1189 OptionNames_.insert(InitPtrToString(Arg));
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001190 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001191 }
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001192 else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001193 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001194 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001195 }
1196 }
1197 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001198
1199public:
1200 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1201 {}
1202
1203 void operator()(const Init* Statement) {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001204 // Statement is either a dag, or a list of dags.
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001205 if (typeid(*Statement) == typeid(ListInit)) {
1206 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1207 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1208 B != E; ++B)
1209 this->processDag(*B);
1210 }
1211 else {
1212 this->processDag(Statement);
1213 }
1214 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001215
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001216 void operator()(const DagInit& Test, unsigned, bool) {
1217 this->operator()(&Test);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001218 }
1219 void operator()(const Init* Statement, unsigned) {
1220 this->operator()(Statement);
1221 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001222};
1223
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00001224/// IsOptionalEdge - Validate that the 'optional_edge' has proper structure.
1225bool IsOptionalEdge (const DagInit& Edg) {
1226 return (GetOperatorName(Edg) == "optional_edge") && (Edg.getNumArgs() > 2);
1227}
1228
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001229/// CheckForSuperfluousOptions - Check that there are no side
1230/// effect-free options (specified only in the OptionList). Otherwise,
1231/// output a warning.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001232void CheckForSuperfluousOptions (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001233 const ToolDescriptions& ToolDescs,
1234 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001235 llvm::StringSet<> nonSuperfluousOptions;
1236
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001237 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001238 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001239 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1240 E = ToolDescs.end(); B != E; ++B) {
1241 const ToolDescription& TD = *(*B);
1242 ExtractOptionNames Callback(nonSuperfluousOptions);
1243 if (TD.Actions)
1244 WalkCase(TD.Actions, Callback, Callback);
1245 }
1246
1247 // Add all options mentioned in the 'case' clauses of the
1248 // OptionalEdges of the compilation graph to the set of
1249 // non-superfluous options.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001250 for (DagVector::const_iterator B = EdgeVector.begin(),
1251 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00001252 const DagInit& Edge = **B;
1253 if (IsOptionalEdge(Edge)) {
1254 const DagInit& Weight = InitPtrToDag(Edge.getArg(2));
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001255 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001256 }
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001257 }
1258
1259 // Check that all options in OptDescs belong to the set of
1260 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001261 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001262 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001263 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001264 if (!nonSuperfluousOptions.count(Val.Name)
1265 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001266 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001267 "Probable cause: this option is specified only in the OptionList.\n";
1268 }
1269}
1270
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001271/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1272bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1273 if (TestName == "single_input_file") {
1274 O << "InputFilenames.size() == 1";
1275 return true;
1276 }
1277 else if (TestName == "multiple_input_files") {
1278 O << "InputFilenames.size() > 1";
1279 return true;
1280 }
1281
1282 return false;
1283}
1284
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001285/// EmitMultipleArgumentTest - Helper function used by
1286/// EmitCaseTestMultipleArgs()
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001287template <typename F>
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001288void EmitMultipleArgumentTest(const DagInit& D, const char* LogicOp,
1289 F Callback, raw_ostream& O)
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001290{
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001291 for (unsigned i = 0, NumArgs = D.getNumArgs(); i < NumArgs; ++i) {
1292 if (i != 0)
1293 O << ' ' << LogicOp << ' ';
1294 Callback(InitPtrToString(D.getArg(i)), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001295 }
1296}
1297
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001298// Callbacks for use with EmitMultipleArgumentTest
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001299
1300class EmitSwitchOn {
1301 const OptionDescriptions& OptDescs_;
1302public:
1303 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1304 {}
1305
1306 void operator()(const std::string& OptName, raw_ostream& O) const {
1307 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1308 O << OptDesc.GenVariableName();
1309 }
1310};
1311
1312class EmitEmptyTest {
1313 bool EmitNegate_;
1314 const OptionDescriptions& OptDescs_;
1315public:
1316 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1317 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1318 {}
1319
1320 void operator()(const std::string& OptName, raw_ostream& O) const {
1321 const char* Neg = (EmitNegate_ ? "!" : "");
1322 if (OptName == "o") {
1323 O << Neg << "OutputFilename.empty()";
1324 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001325 else if (OptName == "save-temps") {
1326 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1327 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001328 else {
1329 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1330 O << Neg << OptDesc.GenVariableName() << ".empty()";
1331 }
1332 }
1333};
1334
1335
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001336/// EmitCaseTestMultipleArgs - Helper function used by EmitCaseTest1Arg()
1337bool EmitCaseTestMultipleArgs (const std::string& TestName,
1338 const DagInit& d,
1339 const OptionDescriptions& OptDescs,
1340 raw_ostream& O) {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001341 if (TestName == "any_switch_on") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001342 EmitMultipleArgumentTest(d, "||", EmitSwitchOn(OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001343 return true;
1344 }
1345 else if (TestName == "switch_on") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001346 EmitMultipleArgumentTest(d, "&&", EmitSwitchOn(OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001347 return true;
1348 }
1349 else if (TestName == "any_not_empty") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001350 EmitMultipleArgumentTest(d, "||", EmitEmptyTest(true, OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001351 return true;
1352 }
1353 else if (TestName == "any_empty") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001354 EmitMultipleArgumentTest(d, "||", EmitEmptyTest(false, OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001355 return true;
1356 }
1357 else if (TestName == "not_empty") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001358 EmitMultipleArgumentTest(d, "&&", EmitEmptyTest(true, OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001359 return true;
1360 }
1361 else if (TestName == "empty") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001362 EmitMultipleArgumentTest(d, "&&", EmitEmptyTest(false, OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001363 return true;
1364 }
1365
1366 return false;
1367}
1368
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001369/// EmitCaseTest1Arg - Helper function used by EmitCaseTest1OrMoreArgs()
1370bool EmitCaseTest1Arg (const std::string& TestName,
1371 const DagInit& d,
1372 const OptionDescriptions& OptDescs,
1373 raw_ostream& O) {
1374 const std::string& Arg = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001375
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001376 if (TestName == "input_languages_contain") {
1377 O << "InLangs.count(\"" << Arg << "\") != 0";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001378 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001379 }
1380 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001381 // This works only for single-argument Tool::GenerateAction. Join
1382 // tools can process several files in different languages simultaneously.
1383
1384 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001385 O << "LangMap.GetLanguage(inFile) == \"" << Arg << '\"';
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001386 return true;
1387 }
1388
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001389 return false;
1390}
1391
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001392/// EmitCaseTest1OrMoreArgs - Helper function used by
1393/// EmitCaseConstructHandler()
1394bool EmitCaseTest1OrMoreArgs(const std::string& TestName,
1395 const DagInit& d,
1396 const OptionDescriptions& OptDescs,
1397 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001398 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001399 return EmitCaseTest1Arg(TestName, d, OptDescs, O) ||
1400 EmitCaseTestMultipleArgs(TestName, d, OptDescs, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001401}
1402
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001403/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001404bool EmitCaseTest2Args(const std::string& TestName,
1405 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001406 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001407 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001408 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001409 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001410 const std::string& OptName = InitPtrToString(d.getArg(0));
1411 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001412
1413 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001414 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001415 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1416 return true;
1417 }
1418 else if (TestName == "element_in_list") {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00001419 const OptionDescription& OptDesc = OptDescs.FindParameterList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001420 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001421 O << "std::find(" << VarName << ".begin(),\n";
1422 O.indent(IndentLevel + Indent1)
1423 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001424 << OptArg << "\") != " << VarName << ".end()";
1425 return true;
1426 }
1427
1428 return false;
1429}
1430
1431// Forward declaration.
1432// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001433void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001434 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001435 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001436
1437/// EmitLogicalOperationTest - Helper function used by
1438/// EmitCaseConstructHandler.
1439void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001440 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001441 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001442 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001443 O << '(';
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001444 for (unsigned i = 0, NumArgs = d.getNumArgs(); i < NumArgs; ++i) {
1445 const DagInit& InnerTest = InitPtrToDag(d.getArg(i));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001446 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001447 if (i != NumArgs - 1) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001448 O << ")\n";
1449 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1450 }
1451 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001452 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001453 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001454 }
1455}
1456
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001457void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001458 const OptionDescriptions& OptDescs, raw_ostream& O)
1459{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001460 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001461 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1462 O << "! (";
1463 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1464 O << ")";
1465}
1466
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001467/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001468void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001469 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001470 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001471 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001472
1473 if (TestName == "and")
1474 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1475 else if (TestName == "or")
1476 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001477 else if (TestName == "not")
1478 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001479 else if (EmitCaseTest0Args(TestName, O))
1480 return;
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001481 else if (EmitCaseTest1OrMoreArgs(TestName, d, OptDescs, O))
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001482 return;
1483 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1484 return;
1485 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001486 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001487}
1488
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001489
1490/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1491class EmitCaseTestCallback {
1492 bool EmitElseIf_;
1493 const OptionDescriptions& OptDescs_;
1494 raw_ostream& O_;
1495public:
1496
1497 EmitCaseTestCallback(bool EmitElseIf,
1498 const OptionDescriptions& OptDescs, raw_ostream& O)
1499 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1500 {}
1501
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001502 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001503 {
1504 if (GetOperatorName(Test) == "default") {
1505 O_.indent(IndentLevel) << "else {\n";
1506 }
1507 else {
1508 O_.indent(IndentLevel)
1509 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001510 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001511 O_ << ") {\n";
1512 }
1513 }
1514};
1515
1516/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1517template <typename F>
1518class EmitCaseStatementCallback {
1519 F Callback_;
1520 raw_ostream& O_;
1521public:
1522
1523 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1524 : Callback_(Callback), O_(O)
1525 {}
1526
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001527 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001528 // Is this a nested 'case'?
1529 bool IsCase = dynamic_cast<const DagInit*>(Statement) &&
1530 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case";
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001531
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001532 // If so, ignore it, it is handled by our caller, WalkCase.
1533 if (!IsCase) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001534 if (typeid(*Statement) == typeid(ListInit)) {
1535 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1536 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1537 B != E; ++B)
1538 Callback_(*B, (IndentLevel + Indent1), O_);
1539 }
1540 else {
1541 Callback_(Statement, (IndentLevel + Indent1), O_);
1542 }
1543 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001544 O_.indent(IndentLevel) << "}\n";
1545 }
1546
1547};
1548
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001549/// EmitCaseConstructHandler - Emit code that handles the 'case'
1550/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001551/// clause. Implemented on top of WalkCase.
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00001552/// Callback's type is void F(const Init* Statement, unsigned IndentLevel,
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001553/// raw_ostream& O).
1554/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001555/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1556/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001557template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001558void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001559 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001560 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001561 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001562 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1563 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001564}
1565
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001566/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001567/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1568/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001569void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001570 const char* Delimiters = " \t\n\v\f\r";
1571 enum TokenizerState
1572 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1573 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001574
1575 if (CmdLine.empty())
1576 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001577 Out.push_back("");
1578
1579 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1580 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001581
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001582 for (; B != E; ++B) {
1583 char cur_ch = CmdLine[B];
1584
1585 switch (cur_st) {
1586 case Normal:
1587 if (cur_ch == '$') {
1588 cur_st = SpecialCommand;
1589 break;
1590 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001591 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001592 // Skip whitespace
1593 B = CmdLine.find_first_not_of(Delimiters, B);
1594 if (B == std::string::npos) {
1595 B = E-1;
1596 continue;
1597 }
1598 --B;
1599 Out.push_back("");
1600 continue;
1601 }
1602 break;
1603
1604
1605 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001606 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001607 cur_st = Normal;
1608 Out.push_back("");
1609 continue;
1610 }
1611 if (cur_ch == '(') {
1612 Out.push_back("");
1613 cur_st = InsideSpecialCommand;
1614 continue;
1615 }
1616 break;
1617
1618 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001619 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001620 continue;
1621 }
1622 if (cur_ch == '\'') {
1623 cur_st = InsideQuotationMarks;
1624 Out.push_back("");
1625 continue;
1626 }
1627 if (cur_ch == ')') {
1628 cur_st = Normal;
1629 Out.push_back("");
1630 }
1631 if (cur_ch == ',') {
1632 continue;
1633 }
1634
1635 break;
1636
1637 case InsideQuotationMarks:
1638 if (cur_ch == '\'') {
1639 cur_st = InsideSpecialCommand;
1640 continue;
1641 }
1642 break;
1643 }
1644
1645 Out.back().push_back(cur_ch);
1646 }
1647}
1648
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001649/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1650/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1651/// SubstituteSpecialCommands().
1652StrVector::const_iterator
1653SubstituteCall (StrVector::const_iterator Pos,
1654 StrVector::const_iterator End,
1655 bool IsJoin, raw_ostream& O)
1656{
1657 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001658 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001659 const std::string& CmdName = *Pos;
1660
1661 if (CmdName == ")")
1662 throw "$CALL invocation: empty argument list!";
1663
1664 O << "hooks::";
1665 O << CmdName << "(";
1666
1667
1668 bool firstIteration = true;
1669 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001670 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001671 const std::string& Arg = *Pos;
1672 assert(Arg.size() != 0);
1673
1674 if (Arg[0] == ')')
1675 break;
1676
1677 if (firstIteration)
1678 firstIteration = false;
1679 else
1680 O << ", ";
1681
1682 if (Arg == "$INFILE") {
1683 if (IsJoin)
1684 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1685 else
1686 O << "inFile.c_str()";
1687 }
1688 else {
1689 O << '"' << Arg << '"';
1690 }
1691 }
1692
1693 O << ')';
1694
1695 return Pos;
1696}
1697
1698/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1699/// function used by SubstituteSpecialCommands().
1700StrVector::const_iterator
1701SubstituteEnv (StrVector::const_iterator Pos,
1702 StrVector::const_iterator End, raw_ostream& O)
1703{
1704 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001705 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001706 const std::string& EnvName = *Pos;
1707
1708 if (EnvName == ")")
1709 throw "$ENV invocation: empty argument list!";
1710
1711 O << "checkCString(std::getenv(\"";
1712 O << EnvName;
1713 O << "\"))";
1714
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001715 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001716
1717 return Pos;
1718}
1719
1720/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1721/// handler code. Helper function used by EmitCmdLineVecFill().
1722StrVector::const_iterator
1723SubstituteSpecialCommands (StrVector::const_iterator Pos,
1724 StrVector::const_iterator End,
1725 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001726{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001727
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001728 const std::string& cmd = *Pos;
1729
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001730 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001731 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001732 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001733 }
1734 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001735 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001736 }
1737 else {
1738 throw "Unknown special command: " + cmd;
1739 }
1740
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001741 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001742 const std::string& Leftover = *Pos;
1743 assert(Leftover.at(0) == ')');
1744 if (Leftover.size() != 1)
1745 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001746
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001747 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001748}
1749
1750/// EmitCmdLineVecFill - Emit code that fills in the command line
1751/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001752void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001753 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001754 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001755 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001756 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001757
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001758 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001759 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001760
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001761 StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001762
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001763 // Emit the command itself.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001764 assert(!StrVec[0].empty());
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001765 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001766 if (StrVec[0][0] == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001767 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1768 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001769 }
1770 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001771 O << '"' << StrVec[0] << '"';
1772 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001773 }
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001774 O << ";\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001775
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001776 // Go through the command arguments.
1777 assert(B <= E);
1778 for (; B != E; ++B) {
1779 const std::string& cmd = *B;
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001780
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001781 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001782 O.indent(IndentLevel);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001783
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001784 if (cmd.at(0) == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001785 O << "vec.push_back(std::make_pair(0, ";
1786 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1787 O << "));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001788 }
1789 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001790 O << "vec.push_back(std::make_pair(0, \"" << cmd << "\"));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001791 }
1792 }
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001793
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001794}
1795
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001796/// EmitForEachListElementCycleHeader - Emit common code for iterating through
1797/// all elements of a list. Helper function used by
1798/// EmitForwardOptionPropertyHandlingCode.
1799void EmitForEachListElementCycleHeader (const OptionDescription& D,
1800 unsigned IndentLevel,
1801 raw_ostream& O) {
1802 unsigned IndentLevel1 = IndentLevel + Indent1;
1803
1804 O.indent(IndentLevel)
1805 << "for (" << D.GenTypeDeclaration()
1806 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1807 O.indent(IndentLevel)
1808 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1809 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1810 << ".getPosition(B - " << D.GenVariableName()
1811 << ".begin());\n";
1812}
1813
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001814/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1815/// implement EmitActionHandler. Emits code for
1816/// handling the (forward) and (forward_as) option properties.
1817void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001818 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001819 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001820 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001821 const std::string& Name = NewName.empty()
1822 ? ("-" + D.Name)
1823 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001824 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001825
1826 switch (D.Type) {
1827 case OptionType::Switch:
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001828 O.indent(IndentLevel)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001829 << "vec.push_back(std::make_pair(" << D.GenVariableName()
1830 << ".getPosition(), \"" << Name << "\"));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001831 break;
1832 case OptionType::Parameter:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001833 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1834 << D.GenVariableName()
1835 <<".getPosition(), \"" << Name;
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001836
1837 if (!D.isForwardNotSplit()) {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001838 O << "\"));\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001839 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1840 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001841 << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001842 }
1843 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001844 O << "=\" + " << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001845 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001846 break;
1847 case OptionType::Prefix:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001848 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1849 << D.GenVariableName() << ".getPosition(), \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001850 << Name << "\" + "
1851 << D.GenVariableName() << "));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001852 break;
1853 case OptionType::PrefixList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001854 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001855 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001856 << Name << "\" + " << "*B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001857 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001858
1859 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001860 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001861 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001862 }
1863
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001864 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001865 break;
1866 case OptionType::ParameterList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001867 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001868 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001869 << Name << "\"));\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001870
1871 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001872 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001873 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001874 }
1875
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001876 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001877 break;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001878 case OptionType::SwitchList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001879 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001880 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
1881 << Name << "\"));\n";
1882 O.indent(IndentLevel1) << "++B;\n";
1883 O.indent(IndentLevel) << "}\n";
1884 break;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001885 case OptionType::Alias:
1886 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001887 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001888 }
1889}
1890
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001891/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1892/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001893struct ActionHandlingCallbackBase
1894{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001895
1896 void onErrorDag(const DagInit& d,
1897 unsigned IndentLevel, raw_ostream& O) const
1898 {
1899 O.indent(IndentLevel)
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00001900 << "PrintError(\""
1901 << (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0)) : "Unknown error!")
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001902 << "\");\n";
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +00001903 O.indent(IndentLevel) << "return 1;\n";
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001904 }
1905
1906 void onWarningDag(const DagInit& d,
1907 unsigned IndentLevel, raw_ostream& O) const
1908 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001909 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001910 O.indent(IndentLevel) << "llvm::errs() << \""
1911 << InitPtrToString(d.getArg(0)) << "\";\n";
1912 }
1913
1914};
1915
1916/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1917/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001918class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001919
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001920typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1921(const DagInit&, unsigned, raw_ostream&) const;
1922
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001923class EmitActionHandlersCallback :
1924 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001925 public HandlerTable<EmitActionHandlersCallbackHandler>
1926{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001927 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001928
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001929 const OptionDescriptions& OptDescs;
1930
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001931 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1932 /// onAppendCmd and onOutputSuffix.
1933 void EmitHookInvocation(const std::string& Str,
1934 const char* BlockOpen, const char* BlockClose,
1935 unsigned IndentLevel, raw_ostream& O) const
1936 {
1937 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001938 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001939
1940 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1941 B != E; ++B) {
1942 const std::string& cmd = *B;
1943
1944 O.indent(IndentLevel) << BlockOpen;
1945
1946 if (cmd.at(0) == '$')
1947 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1948 else
1949 O << '"' << cmd << '"';
1950
1951 O << BlockClose;
1952 }
1953 }
1954
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001955 void onAppendCmd (const DagInit& Dag,
1956 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001957 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001958 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001959 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001960 "vec.push_back(std::make_pair(65536, ", "));\n",
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001961 IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001962 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001963
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001964 void onForward (const DagInit& Dag,
1965 unsigned IndentLevel, raw_ostream& O) const
1966 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001967 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001968 const std::string& Name = InitPtrToString(Dag.getArg(0));
1969 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1970 IndentLevel, "", O);
1971 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001972
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001973 void onForwardAs (const DagInit& Dag,
1974 unsigned IndentLevel, raw_ostream& O) const
1975 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001976 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001977 const std::string& Name = InitPtrToString(Dag.getArg(0));
1978 const std::string& NewName = InitPtrToString(Dag.getArg(1));
1979 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1980 IndentLevel, NewName, O);
1981 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001982
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001983 void onForwardValue (const DagInit& Dag,
1984 unsigned IndentLevel, raw_ostream& O) const
1985 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001986 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001987 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00001988 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001989
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001990 if (D.isSwitchList()) {
1991 throw std::runtime_error
1992 ("forward_value is not allowed with switch_list");
1993 }
1994
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001995 if (D.isParameter()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001996 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1997 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001998 << D.GenVariableName() << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001999 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00002000 else {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002001 O.indent(IndentLevel) << "for (" << D.GenTypeDeclaration()
2002 << "::iterator B = " << D.GenVariableName()
2003 << ".begin(), \n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002004 O.indent(IndentLevel + Indent1) << " E = " << D.GenVariableName()
2005 << ".end(); B != E; ++B)\n";
2006 O.indent(IndentLevel) << "{\n";
2007 O.indent(IndentLevel + Indent1)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002008 << "unsigned pos = " << D.GenVariableName()
2009 << ".getPosition(B - " << D.GenVariableName()
2010 << ".begin());\n";
2011 O.indent(IndentLevel + Indent1)
2012 << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002013 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002014 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002015 }
2016
2017 void onForwardTransformedValue (const DagInit& Dag,
2018 unsigned IndentLevel, raw_ostream& O) const
2019 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002020 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002021 const std::string& Name = InitPtrToString(Dag.getArg(0));
2022 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002023 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002024
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002025 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2026 << D.GenVariableName() << ".getPosition("
2027 << (D.isList() ? "0" : "") << "), "
2028 << "hooks::" << Hook << "(" << D.GenVariableName()
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002029 << (D.isParameter() ? ".c_str()" : "") << ")));\n";
2030 }
2031
2032 void onNoOutFile (const DagInit& Dag,
2033 unsigned IndentLevel, raw_ostream& O) const
2034 {
2035 CheckNumberOfArguments(Dag, 0);
2036 O.indent(IndentLevel) << "no_out_file = true;\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002037 }
2038
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002039 void onOutputSuffix (const DagInit& Dag,
2040 unsigned IndentLevel, raw_ostream& O) const
2041 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002042 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002043 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
2044 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002045 }
2046
2047 void onStopCompilation (const DagInit& Dag,
2048 unsigned IndentLevel, raw_ostream& O) const
2049 {
2050 O.indent(IndentLevel) << "stop_compilation = true;\n";
2051 }
2052
2053
2054 void onUnpackValues (const DagInit& Dag,
2055 unsigned IndentLevel, raw_ostream& O) const
2056 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002057 throw "'unpack_values' is deprecated. "
2058 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002059 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002060
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002061 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002062
2063 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
2064 : OptDescs(OD)
2065 {
2066 if (!staticMembersInitialized_) {
2067 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
2068 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
2069 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
2070 AddHandler("forward", &EmitActionHandlersCallback::onForward);
2071 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002072 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
2073 AddHandler("forward_transformed_value",
2074 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002075 AddHandler("no_out_file",
2076 &EmitActionHandlersCallback::onNoOutFile);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002077 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
2078 AddHandler("stop_compilation",
2079 &EmitActionHandlersCallback::onStopCompilation);
2080 AddHandler("unpack_values",
2081 &EmitActionHandlersCallback::onUnpackValues);
2082
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002083
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002084 staticMembersInitialized_ = true;
2085 }
2086 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002087
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002088 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002089 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002090 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002091 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002092 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002093};
2094
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002095void EmitGenerateActionMethodHeader(const ToolDescription& D,
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002096 bool IsJoin, bool Naked,
2097 raw_ostream& O)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002098{
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002099 O.indent(Indent1) << "int GenerateAction(Action& Out,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002100
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002101 if (IsJoin)
2102 O.indent(Indent2) << "const PathVector& inFiles,\n";
2103 else
2104 O.indent(Indent2) << "const sys::Path& inFile,\n";
2105
2106 O.indent(Indent2) << "const bool HasChildren,\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002107 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2108 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2109 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2110 O.indent(Indent1) << "{\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002111
2112 if (!Naked) {
2113 O.indent(Indent2) << "std::string cmd;\n";
2114 O.indent(Indent2) << "std::string out_file;\n";
2115 O.indent(Indent2)
2116 << "std::vector<std::pair<unsigned, std::string> > vec;\n";
2117 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2118 O.indent(Indent2) << "bool no_out_file = false;\n";
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002119 O.indent(Indent2) << "std::string output_suffix(\""
2120 << D.OutputSuffix << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002121 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002122}
2123
2124// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2125// Tool::GenerateAction() method.
2126void EmitGenerateActionMethod (const ToolDescription& D,
2127 const OptionDescriptions& OptDescs,
2128 bool IsJoin, raw_ostream& O) {
2129
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002130 EmitGenerateActionMethodHeader(D, IsJoin, /* Naked = */ false, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002131
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002132 if (!D.CmdLine)
2133 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002134
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002135 // Process the 'command' property.
2136 O << '\n';
2137 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2138 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002139
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002140 // Process the 'actions' list of this tool.
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002141 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002142 EmitCaseConstructHandler(D.Actions, Indent2,
2143 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002144 false, OptDescs, O);
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002145 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002146
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002147 // Input file (s)
2148 if (!D.InFileOption.empty()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002149 O.indent(Indent2)
2150 << "vec.push_back(std::make_pair(InputFilenames.getPosition(0), \""
2151 << D.InFileOption << "\");\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002152 }
2153
2154 if (IsJoin) {
2155 O.indent(Indent2)
2156 << "for (PathVector::const_iterator B = inFiles.begin(),\n";
2157 O.indent(Indent3) << "E = inFiles.end(); B != E; ++B)\n";
2158 O.indent(Indent2) << "{\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002159 O.indent(Indent3) << "vec.push_back(std::make_pair("
2160 << "InputFilenames.getPosition(B - inFiles.begin()), "
2161 << "B->str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002162 O.indent(Indent2) << "}\n";
2163 }
2164 else {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002165 O.indent(Indent2) << "vec.push_back(std::make_pair("
2166 << "InputFilenames.getPosition(0), inFile.str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002167 }
2168
2169 // Output file
2170 O.indent(Indent2) << "if (!no_out_file) {\n";
2171 if (!D.OutFileOption.empty())
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002172 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002173 << D.OutFileOption << "\"));\n";
2174
2175 O.indent(Indent3) << "out_file = this->OutFilename("
2176 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002177 O.indent(Indent4) <<
2178 "TempDir, stop_compilation, output_suffix.c_str()).str();\n\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002179 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, out_file));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002180
2181 O.indent(Indent2) << "}\n\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002182
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002183 // Handle the Sink property.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002184 std::string SinkOption("autogenerated::");
2185 SinkOption += SinkOptionName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002186 if (D.isSink()) {
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002187 O.indent(Indent2) << "if (!" << SinkOption << ".empty()) {\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002188 O.indent(Indent3) << "for (cl::list<std::string>::iterator B = "
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002189 << SinkOption << ".begin(), E = " << SinkOption
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002190 << ".end(); B != E; ++B)\n";
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002191 O.indent(Indent4) << "vec.push_back(std::make_pair(" << SinkOption
2192 << ".getPosition(B - " << SinkOption
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002193 << ".begin()), *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002194 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002195 }
2196
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002197 O.indent(Indent2) << "Out.Construct(cmd, this->SortArgs(vec), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002198 << "stop_compilation, out_file);\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002199 O.indent(Indent2) << "return 0;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002200 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002201}
2202
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002203/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2204/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002205void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2206 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002207 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002208 if (!ToolDesc.isJoin()) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002209 EmitGenerateActionMethodHeader(ToolDesc, /* IsJoin = */ true,
2210 /* Naked = */ true, O);
2211 O.indent(Indent2) << "PrintError(\"" << ToolDesc.Name
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002212 << " is not a Join tool!\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002213 O.indent(Indent2) << "return -1;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002214 O.indent(Indent1) << "}\n\n";
2215 }
2216 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002217 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002218 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002219
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002220 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002221}
2222
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002223/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2224/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002225void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002226 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2227 O.indent(Indent2) << "return InputLanguages_;\n";
2228 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002229
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002230 O.indent(Indent1) << "const char** OutputLanguages() const {\n";
2231 O.indent(Indent2) << "return OutputLanguages_;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002232 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002233}
2234
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002235/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002236void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002237 O.indent(Indent1) << "const char* Name() const {\n";
2238 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2239 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002240}
2241
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002242/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2243/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002244void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002245 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002246 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002247 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002248 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002249 O.indent(Indent2) << "return false;\n";
2250 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002251}
2252
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002253/// EmitWorksOnEmptyCallback - Callback used by EmitWorksOnEmptyMethod in
2254/// conjunction with EmitCaseConstructHandler.
2255void EmitWorksOnEmptyCallback (const Init* Value,
2256 unsigned IndentLevel, raw_ostream& O) {
2257 CheckBooleanConstant(Value);
2258 O.indent(IndentLevel) << "return " << Value->getAsString() << ";\n";
2259}
2260
2261/// EmitWorksOnEmptyMethod - Emit the WorksOnEmpty() method for a given Tool
2262/// class.
2263void EmitWorksOnEmptyMethod (const ToolDescription& D,
2264 const OptionDescriptions& OptDescs,
2265 raw_ostream& O)
2266{
2267 O.indent(Indent1) << "bool WorksOnEmpty() const {\n";
2268 if (D.OnEmpty == 0)
2269 O.indent(Indent2) << "return false;\n";
2270 else
2271 EmitCaseConstructHandler(D.OnEmpty, Indent2, EmitWorksOnEmptyCallback,
2272 /*EmitElseIf = */ true, OptDescs, O);
2273 O.indent(Indent1) << "}\n\n";
2274}
2275
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002276/// EmitStrArray - Emit definition of a 'const char**' static member
2277/// variable. Helper used by EmitStaticMemberDefinitions();
2278void EmitStrArray(const std::string& Name, const std::string& VarName,
2279 const StrVector& StrVec, raw_ostream& O) {
2280 O << "const char* " << Name << "::" << VarName << "[] = {";
2281 for (StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
2282 B != E; ++B)
2283 O << '\"' << *B << "\", ";
2284 O << "0};\n";
2285}
2286
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002287/// EmitStaticMemberDefinitions - Emit static member definitions for a
2288/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002289void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002290 if (D.InLanguage.empty())
2291 throw "Tool " + D.Name + " has no 'in_language' property!";
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002292 if (D.OutLanguage.empty())
2293 throw "Tool " + D.Name + " has no 'out_language' property!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002294
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002295 EmitStrArray(D.Name, "InputLanguages_", D.InLanguage, O);
2296 EmitStrArray(D.Name, "OutputLanguages_", D.OutLanguage, O);
2297 O << '\n';
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002298}
2299
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002300/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002301void EmitToolClassDefinition (const ToolDescription& D,
2302 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002303 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002304 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002305 return;
2306
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002307 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002308 O << "class " << D.Name << " : public ";
2309 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002310 O << "JoinTool";
2311 else
2312 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002313
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002314 O << " {\nprivate:\n";
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002315 O.indent(Indent1) << "static const char* InputLanguages_[];\n";
2316 O.indent(Indent1) << "static const char* OutputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002317
2318 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002319 EmitNameMethod(D, O);
2320 EmitInOutLanguageMethods(D, O);
2321 EmitIsJoinMethod(D, O);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002322 EmitWorksOnEmptyMethod(D, OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002323 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002324
2325 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002326 O << "};\n";
2327
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002328 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002329
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002330}
2331
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002332/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002333/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002334void EmitOptionDefinitions (const OptionDescriptions& descs,
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002335 bool HasSink, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002336{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002337 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002338
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002339 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002340 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002341 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002342 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002343
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002344 if (val.Type == OptionType::Alias) {
2345 Aliases.push_back(val);
2346 continue;
2347 }
2348
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002349 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002350 << val.GenPlainVariableName();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002351
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002352 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002353
2354 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2355 O << ", cl::Prefix";
2356
2357 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002358 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002359 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002360 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002361 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002362 }
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002363
2364 if (val.isOptional())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002365 O << ", cl::Optional";
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002366
2367 if (val.isOneOrMore())
2368 O << ", cl::OneOrMore";
2369
2370 if (val.isZeroOrMore())
2371 O << ", cl::ZeroOrMore";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002372
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002373 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002374 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002375 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002376 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002377
2378 if (val.isCommaSeparated())
2379 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002380
2381 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002382 O << ", cl::multi_val(" << val.MultiVal << ')';
2383
2384 if (val.InitVal) {
2385 const std::string& str = val.InitVal->getAsString();
2386 O << ", cl::init(" << str << ')';
2387 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002388
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002389 if (!val.Help.empty())
2390 O << ", cl::desc(\"" << val.Help << "\")";
2391
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002392 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002393 }
2394
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002395 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002396 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002397 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002398 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002399
2400 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov297514d2010-08-20 18:16:26 +00002401 << val.GenPlainVariableName()
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002402 << "(\"" << val.Name << '\"';
2403
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002404 const OptionDescription& D = descs.FindOption(val.Help);
2405 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002406
2407 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2408 }
2409
2410 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002411 if (HasSink)
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002412 O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002413
2414 O << '\n';
2415}
2416
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002417/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002418/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002419
2420class EmitPreprocessOptionsCallback;
2421
2422typedef void
2423(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2424(const DagInit&, unsigned, raw_ostream&) const;
2425
2426class EmitPreprocessOptionsCallback :
2427 public ActionHandlingCallbackBase,
2428 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2429{
2430 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002431 typedef void
2432 (EmitPreprocessOptionsCallback::* HandlerImpl)
2433 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002434
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002435 const OptionDescriptions& OptDescs_;
2436
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002437 void onEachArgument(const DagInit& d, HandlerImpl h,
2438 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002439 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002440 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002441
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002442 for (unsigned i = 0, NumArgs = d.getNumArgs(); i < NumArgs; ++i) {
2443 ((this)->*(h))(d.getArg(i), IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002444 }
2445 }
2446
2447 void onUnsetOptionImpl(const Init* I,
2448 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002449 {
2450 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002451 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002452
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002453 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002454 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2455 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002456 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002457 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2458 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002459 else if (OptDesc.isList()) {
2460 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2461 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002462 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002463 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002464 }
2465 }
2466
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002467 void onUnsetOption(const DagInit& d,
2468 unsigned IndentLevel, raw_ostream& O) const
2469 {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002470 this->onEachArgument(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2471 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002472 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002473
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002474 void onSetOptionImpl(const DagInit& D,
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002475 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002476 CheckNumberOfArguments(D, 2);
2477
2478 const std::string& OptName = InitPtrToString(D.getArg(0));
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002479 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002480 const Init* Value = D.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002481
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002482 if (OptDesc.isList()) {
2483 const ListInit& List = InitPtrToList(Value);
2484
2485 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2486 for (ListInit::const_iterator B = List.begin(), E = List.end();
2487 B != E; ++B) {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002488 const Init* CurElem = *B;
2489 if (OptDesc.isSwitchList())
2490 CheckBooleanConstant(CurElem);
2491
2492 O.indent(IndentLevel)
2493 << OptDesc.GenVariableName() << ".push_back(\""
2494 << (OptDesc.isSwitchList() ? CurElem->getAsString()
2495 : InitPtrToString(CurElem))
2496 << "\");\n";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002497 }
2498 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002499 else if (OptDesc.isSwitch()) {
2500 CheckBooleanConstant(Value);
2501 O.indent(IndentLevel) << OptDesc.GenVariableName()
2502 << " = " << Value->getAsString() << ";\n";
2503 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002504 else if (OptDesc.isParameter()) {
2505 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002506 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002507 << " = \"" << Str << "\";\n";
2508 }
2509 else {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002510 throw "Can't apply 'set_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002511 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002512 }
2513
2514 void onSetSwitch(const Init* I,
2515 unsigned IndentLevel, raw_ostream& O) const {
2516 const std::string& OptName = InitPtrToString(I);
2517 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2518
2519 if (OptDesc.isSwitch())
2520 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2521 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002522 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002523 }
2524
2525 void onSetOption(const DagInit& d,
2526 unsigned IndentLevel, raw_ostream& O) const
2527 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002528 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002529
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002530 // 2-argument form: (set_option "A", true), (set_option "B", "C"),
2531 // (set_option "D", ["E", "F"])
2532 if (d.getNumArgs() == 2) {
2533 const OptionDescription& OptDesc =
2534 OptDescs_.FindOption(InitPtrToString(d.getArg(0)));
2535 const Init* Opt2 = d.getArg(1);
2536
2537 if (!OptDesc.isSwitch() || typeid(*Opt2) != typeid(StringInit)) {
2538 this->onSetOptionImpl(d, IndentLevel, O);
2539 return;
2540 }
2541 }
2542
2543 // Multiple argument form: (set_option "A"), (set_option "B", "C", "D")
2544 this->onEachArgument(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2545 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002546 }
2547
2548public:
2549
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002550 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002551 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002552 {
2553 if (!staticMembersInitialized_) {
2554 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2555 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2556 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002557 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002558
2559 staticMembersInitialized_ = true;
2560 }
2561 }
2562
2563 void operator()(const Init* I,
2564 unsigned IndentLevel, raw_ostream& O) const
2565 {
2566 InvokeDagInitHandler(this, I, IndentLevel, O);
2567 }
2568
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002569};
2570
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002571/// EmitPreprocessOptions - Emit the PreprocessOptions() function.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002572void EmitPreprocessOptions (const RecordKeeper& Records,
2573 const OptionDescriptions& OptDecs, raw_ostream& O)
2574{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002575 O << "int PreprocessOptions () {\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002576
2577 const RecordVector& OptionPreprocessors =
2578 Records.getAllDerivedDefinitions("OptionPreprocessor");
2579
2580 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2581 E = OptionPreprocessors.end(); B!=E; ++B) {
2582 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002583 EmitCaseConstructHandler(Case, Indent1,
2584 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002585 false, OptDecs, O);
2586 }
2587
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002588 O << '\n';
2589 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002590 O << "}\n\n";
2591}
2592
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002593class DoEmitPopulateLanguageMap;
2594typedef void (DoEmitPopulateLanguageMap::* DoEmitPopulateLanguageMapHandler)
2595(const DagInit& D);
2596
2597class DoEmitPopulateLanguageMap
2598: public HandlerTable<DoEmitPopulateLanguageMapHandler>
2599{
2600private:
2601 raw_ostream& O_;
2602
2603public:
2604
2605 explicit DoEmitPopulateLanguageMap (raw_ostream& O) : O_(O) {
2606 if (!staticMembersInitialized_) {
2607 AddHandler("lang_to_suffixes",
2608 &DoEmitPopulateLanguageMap::onLangToSuffixes);
2609
2610 staticMembersInitialized_ = true;
2611 }
2612 }
2613
2614 void operator() (Init* I) {
2615 InvokeDagInitHandler(this, I);
2616 }
2617
2618private:
2619
2620 void onLangToSuffixes (const DagInit& d) {
2621 CheckNumberOfArguments(d, 2);
2622
2623 const std::string& Lang = InitPtrToString(d.getArg(0));
2624 Init* Suffixes = d.getArg(1);
2625
2626 // Second argument to lang_to_suffixes is either a single string...
2627 if (typeid(*Suffixes) == typeid(StringInit)) {
2628 O_.indent(Indent1) << "langMap[\"" << InitPtrToString(Suffixes)
2629 << "\"] = \"" << Lang << "\";\n";
2630 }
2631 // ...or a list of strings.
2632 else {
2633 const ListInit& Lst = InitPtrToList(Suffixes);
2634 assert(Lst.size() != 0);
2635 for (ListInit::const_iterator B = Lst.begin(), E = Lst.end();
2636 B != E; ++B) {
2637 O_.indent(Indent1) << "langMap[\"" << InitPtrToString(*B)
2638 << "\"] = \"" << Lang << "\";\n";
2639 }
2640 }
2641 }
2642
2643};
2644
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002645/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002646void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002647{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002648 O << "int PopulateLanguageMap (LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002649
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002650 // For each LanguageMap:
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002651 const RecordVector& LangMaps =
Mikhail Glushenkov00a5b5b2010-08-23 19:24:16 +00002652 Records.getAllDerivedDefinitions("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002653
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002654 // Call DoEmitPopulateLanguageMap.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002655 for (RecordVector::const_iterator B = LangMaps.begin(),
2656 E = LangMaps.end(); B!=E; ++B) {
2657 ListInit* LangMap = (*B)->getValueAsListInit("map");
2658 std::for_each(LangMap->begin(), LangMap->end(),
2659 DoEmitPopulateLanguageMap(O));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002660 }
2661
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002662 O << '\n';
2663 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002664 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002665}
2666
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002667/// EmitEdgePropertyHandlerCallback - Emits code that handles edge
2668/// properties. Helper function passed to EmitCaseConstructHandler() by
2669/// EmitEdgeClass().
2670void EmitEdgePropertyHandlerCallback (const Init* i, unsigned IndentLevel,
2671 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002672 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002673 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002674
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002675 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002676 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002677 }
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002678 else if (OpName == "error") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002679 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002680 O.indent(IndentLevel) << "PrintError(\""
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002681 << InitPtrToString(d.getArg(0))
2682 << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002683 O.indent(IndentLevel) << "return -1;\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002684 return;
2685 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002686 else {
2687 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002688 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002689 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002690
2691 if (d.getNumArgs() > 0)
2692 O << InitPtrToInt(d.getArg(0)) << ";\n";
2693 else
2694 O << "2;\n";
2695
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002696}
2697
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002698/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002699void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002700 const DagInit& Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002701 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002702
2703 // Class constructor.
2704 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002705 << "public:\n";
2706 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2707 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002708
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002709 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002710 O.indent(Indent1)
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002711 << "int Weight(const InputLanguagesSet& InLangs) const {\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002712 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002713
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002714 // Handle the 'case' construct.
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002715 EmitCaseConstructHandler(&Case, Indent2, EmitEdgePropertyHandlerCallback,
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002716 false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002717
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002718 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002719 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002720}
2721
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002722/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002723void EmitEdgeClasses (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002724 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002725 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002726 int i = 0;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002727 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002728 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002729 const DagInit& Edge = **B;
2730 const std::string& Name = GetOperatorName(Edge);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002731
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002732 if (Name == "optional_edge") {
2733 assert(IsOptionalEdge(Edge));
2734 const std::string& NodeB = InitPtrToString(Edge.getArg(1));
2735
2736 const DagInit& Weight = InitPtrToDag(Edge.getArg(2));
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002737 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
2738 }
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002739 else if (Name != "edge") {
2740 throw "Unknown edge class: '" + Name + "'!";
2741 }
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002742
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002743 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002744 }
2745}
2746
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002747/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph() function.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002748void EmitPopulateCompilationGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002749 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002750 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002751{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002752 O << "int PopulateCompilationGraph (CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002753
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002754 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2755 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002756 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002757
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002758 O << '\n';
2759
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002760 // Insert edges.
2761
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002762 int i = 0;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002763 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002764 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002765 const DagInit& Edge = **B;
2766 const std::string& NodeA = InitPtrToString(Edge.getArg(0));
2767 const std::string& NodeB = InitPtrToString(Edge.getArg(1));
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002768
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002769 O.indent(Indent1) << "if (int ret = G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002770
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002771 if (IsOptionalEdge(Edge))
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002772 O << "new Edge" << i << "()";
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002773 else
2774 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002775
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002776 O << "))\n";
2777 O.indent(Indent2) << "return ret;\n";
2778
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002779 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002780 }
2781
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002782 O << '\n';
2783 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002784 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002785}
2786
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002787/// HookInfo - Information about the hook type and number of arguments.
2788struct HookInfo {
2789
2790 // A hook can either have a single parameter of type std::vector<std::string>,
2791 // or NumArgs parameters of type const char*.
2792 enum HookType { ListHook, ArgHook };
2793
2794 HookType Type;
2795 unsigned NumArgs;
2796
2797 HookInfo() : Type(ArgHook), NumArgs(1)
2798 {}
2799
2800 HookInfo(HookType T) : Type(T), NumArgs(1)
2801 {}
2802
2803 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2804 {}
2805};
2806
2807typedef llvm::StringMap<HookInfo> HookInfoMap;
2808
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002809/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002810/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002811/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002812class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002813 HookInfoMap& HookNames_;
2814 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002815public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002816 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2817 : HookNames_(HookNames), OptDescs_(OptDescs)
2818 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002819
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002820 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002821 const std::string& Name = GetOperatorName(Dag);
2822
2823 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002824 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002825 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2826 const std::string& HookName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002827 const OptionDescription& D =
2828 OptDescs_.FindParameterListOrParameter(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002829
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002830 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2831 : HookInfo::ArgHook);
2832 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002833 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002834 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002835 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2836 }
2837 }
2838
2839 void onCmdLine(const std::string& Cmd) {
2840 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002841 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002842
2843 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2844 B != E; ++B) {
2845 const std::string& cmd = *B;
2846
2847 if (cmd == "$CALL") {
2848 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002849 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002850 const std::string& HookName = *B;
2851
2852 if (HookName.at(0) == ')')
2853 throw "$CALL invoked with no arguments!";
2854
2855 while (++B != E && B->at(0) != ')') {
2856 ++NumArgs;
2857 }
2858
2859 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2860
2861 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2862 H->second.Type != HookInfo::ArgHook)
2863 throw "Overloading of hooks is not allowed. Overloaded hook: "
2864 + HookName;
2865 else
2866 HookNames_[HookName] = HookInfo(NumArgs);
2867 }
2868 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002869 }
2870
2871 void operator()(const Init* Arg) {
2872
2873 // We're invoked on an action (either a dag or a dag list).
2874 if (typeid(*Arg) == typeid(DagInit)) {
2875 const DagInit& Dag = InitPtrToDag(Arg);
2876 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002877 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002878 }
2879 else if (typeid(*Arg) == typeid(ListInit)) {
2880 const ListInit& List = InitPtrToList(Arg);
2881 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2882 ++B) {
2883 const DagInit& Dag = InitPtrToDag(*B);
2884 this->onAction(Dag);
2885 }
2886 return;
2887 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002888
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002889 // We're invoked on a command line string.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002890 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002891 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002892
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002893 void operator()(const Init* Statement, unsigned) {
2894 this->operator()(Statement);
2895 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002896};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002897
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002898/// FillInHookNames - Actually extract the hook names from all command
2899/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002900void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002901 const OptionDescriptions& OptDescs,
2902 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002903{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002904 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002905 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2906 E = ToolDescs.end(); B != E; ++B) {
2907 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002908
2909 // Look for 'forward_transformed_value' in 'actions'.
2910 if (D.Actions)
2911 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2912
2913 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002914 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002915 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002916 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002917 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002918 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002919 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002920 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002921 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002922 }
2923}
2924
2925/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2926/// property records and emit hook function declaration for each
2927/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002928void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2929 const OptionDescriptions& OptDescs, raw_ostream& O) {
2930 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002931
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002932 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002933 if (HookNames.empty())
2934 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002935
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002936 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002937 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002938 const char* HookName = B->first();
2939 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002940
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002941 O.indent(Indent1) << "std::string " << HookName << "(";
2942
2943 if (Info.Type == HookInfo::ArgHook) {
2944 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2945 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2946 }
2947 }
2948 else {
2949 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002950 }
2951
2952 O <<");\n";
2953 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002954}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002955
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002956/// EmitIncludes - Emit necessary #include directives and some
2957/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002958void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002959 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2960 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002961 << "#include \"llvm/CompilerDriver/Error.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002962 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002963
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002964 << "#include \"llvm/Support/CommandLine.h\"\n"
2965 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002966
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002967 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002968 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002969 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002970 << "#include <stdexcept>\n\n"
2971
2972 << "using namespace llvm;\n"
2973 << "using namespace llvmc;\n\n"
2974
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002975 << "inline const char* checkCString(const char* s)\n"
2976 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002977}
2978
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002979
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00002980/// DriverData - Holds all information about the driver.
2981struct DriverData {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002982 OptionDescriptions OptDescs;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002983 ToolDescriptions ToolDescs;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002984 DagVector Edges;
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00002985 bool HasSink;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002986};
2987
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002988/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002989/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002990bool HasSink(const ToolDescriptions& ToolDescs) {
2991 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2992 E = ToolDescs.end(); B != E; ++B)
2993 if ((*B)->isSink())
2994 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002995
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002996 return false;
2997}
2998
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00002999/// CollectDriverData - Collect compilation graph edges, tool properties and
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003000/// option properties from the parse tree.
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003001void CollectDriverData (const RecordKeeper& Records, DriverData& Data) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003002 // Collect option properties.
3003 const RecordVector& OptionLists =
3004 Records.getAllDerivedDefinitions("OptionList");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003005 CollectOptionDescriptions(OptionLists, Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003006
3007 // Collect tool properties.
3008 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003009 CollectToolDescriptions(Tools, Data.ToolDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003010 Data.HasSink = HasSink(Data.ToolDescs);
3011
3012 // Collect compilation graph edges.
3013 const RecordVector& CompilationGraphs =
3014 Records.getAllDerivedDefinitions("CompilationGraph");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003015 FillInEdgeVector(CompilationGraphs, Data.Edges);
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00003016}
3017
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003018/// CheckDriverData - Perform some sanity checks on the collected data.
3019void CheckDriverData(DriverData& Data) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003020 // Filter out all tools not mentioned in the compilation graph.
3021 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003022
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003023 // Typecheck the compilation graph.
3024 TypecheckGraph(Data.Edges, Data.ToolDescs);
3025
3026 // Check that there are no options without side effects (specified
3027 // only in the OptionList).
3028 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003029}
3030
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003031void EmitDriverCode(const DriverData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003032 // Emit file header.
3033 EmitIncludes(O);
3034
3035 // Emit global option registration code.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003036 O << "namespace llvmc {\n"
3037 << "namespace autogenerated {\n\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003038 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003039 O << "} // End namespace autogenerated.\n"
3040 << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003041
3042 // Emit hook declarations.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003043 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003044 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003045 O << "} // End namespace hooks.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003046
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003047 O << "namespace {\n\n";
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003048 O << "using namespace llvmc::autogenerated;\n\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003049
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003050 // Emit Tool classes.
3051 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
3052 E = Data.ToolDescs.end(); B!=E; ++B)
3053 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
3054
3055 // Emit Edge# classes.
3056 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
3057
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003058 O << "} // End anonymous namespace.\n\n";
3059
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003060 O << "namespace llvmc {\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003061 O << "namespace autogenerated {\n\n";
3062
3063 // Emit PreprocessOptions() function.
3064 EmitPreprocessOptions(Records, Data.OptDescs, O);
3065
3066 // Emit PopulateLanguageMap() function
3067 // (language map maps from file extensions to language names).
3068 EmitPopulateLanguageMap(Records, O);
3069
3070 // Emit PopulateCompilationGraph() function.
3071 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
3072
3073 O << "} // End namespace autogenerated.\n";
3074 O << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003075
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003076 // EOF
3077}
3078
3079
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003080// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00003081}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003082
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003083/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00003084void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003085 try {
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003086 DriverData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003087
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003088 CollectDriverData(Records, Data);
3089 CheckDriverData(Data);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003090
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003091 this->EmitSourceFileHeader("llvmc-based driver: auto-generated code", O);
3092 EmitDriverCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003093
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003094 } catch (std::exception& Error) {
3095 throw Error.what() + std::string(" - usually this means a syntax error.");
3096 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003097}