blob: 7ad981f4e498fbbb2a811e6f2a4600594d102d07 [file] [log] [blame]
Mikhail Glushenkovfb37f392008-05-30 06:20:54 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000010// This tablegen backend is responsible for emitting LLVMC configuration code.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkovecbdcf22008-05-06 18:09:29 +000014#include "LLVMCConfigurationEmitter.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000015#include "Record.h"
16
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000018#include "llvm/ADT/StringMap.h"
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000019#include "llvm/ADT/StringSet.h"
Mikhail Glushenkove0b65702009-12-23 12:49:30 +000020
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000021#include <algorithm>
22#include <cassert>
23#include <functional>
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +000024#include <stdexcept>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000025#include <string>
Chris Lattner32a9e7a2008-06-04 04:46:14 +000026#include <typeinfo>
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +000027
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000028using namespace llvm;
29
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000030namespace {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
32//===----------------------------------------------------------------------===//
33/// Typedefs
34
35typedef std::vector<Record*> RecordVector;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +000036typedef std::vector<const DagInit*> DagVector;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000037typedef std::vector<std::string> StrVector;
38
39//===----------------------------------------------------------------------===//
40/// Constants
41
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +000042// Indentation.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000043const unsigned TabWidth = 4;
44const unsigned Indent1 = TabWidth*1;
45const unsigned Indent2 = TabWidth*2;
46const unsigned Indent3 = TabWidth*3;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +000047const unsigned Indent4 = TabWidth*4;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000048
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000049// Default help string.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000050const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000051
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000052// Name for the "sink" option.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +000053const char * const SinkOptionName = "SinkOption";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000054
55//===----------------------------------------------------------------------===//
56/// Helper functions
57
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000058/// Id - An 'identity' function object.
59struct Id {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000060 template<typename T0>
61 void operator()(const T0&) const {
62 }
63 template<typename T0, typename T1>
64 void operator()(const T0&, const T1&) const {
65 }
66 template<typename T0, typename T1, typename T2>
67 void operator()(const T0&, const T1&, const T2&) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000068 }
69};
70
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000071int InitPtrToInt(const Init* ptr) {
72 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000073 return val.getValue();
74}
75
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000076const std::string& InitPtrToString(const Init* ptr) {
77 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
78 return val.getValue();
79}
80
81const ListInit& InitPtrToList(const Init* ptr) {
82 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
83 return val;
84}
85
86const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000087 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000088 return val;
89}
90
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000091const std::string GetOperatorName(const DagInit& D) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +000092 return D.getOperator()->getAsString();
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000093}
94
Mikhail Glushenkove0b65702009-12-23 12:49:30 +000095/// CheckBooleanConstant - Check that the provided value is a boolean constant.
96void CheckBooleanConstant(const Init* I) {
97 const DefInit& val = dynamic_cast<const DefInit&>(*I);
98 const std::string& str = val.getAsString();
99
100 if (str != "true" && str != "false") {
101 throw "Incorrect boolean value: '" + str +
102 "': must be either 'true' or 'false'";
103 }
104}
105
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000106// CheckNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +0000107// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000108void CheckNumberOfArguments (const DagInit& d, unsigned minArgs) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000109 if (d.getNumArgs() < minArgs)
110 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +0000111}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000112
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000113// EscapeVariableName - Escape commas and other symbols not allowed
114// in the C++ variable names. Makes it possible to use options named
115// like "Wa," (useful for prefix options).
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000116std::string EscapeVariableName (const std::string& Var) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000117 std::string ret;
118 for (unsigned i = 0; i != Var.size(); ++i) {
119 char cur_char = Var[i];
120 if (cur_char == ',') {
121 ret += "_comma_";
122 }
123 else if (cur_char == '+') {
124 ret += "_plus_";
125 }
126 else if (cur_char == '-') {
127 ret += "_dash_";
128 }
129 else {
130 ret.push_back(cur_char);
131 }
132 }
133 return ret;
134}
135
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000136/// EscapeQuotes - Replace '"' with '\"'.
137std::string EscapeQuotes (const std::string& Var) {
138 std::string ret;
139 for (unsigned i = 0; i != Var.size(); ++i) {
140 char cur_char = Var[i];
141 if (cur_char == '"') {
142 ret += "\\\"";
143 }
144 else {
145 ret.push_back(cur_char);
146 }
147 }
148 return ret;
149}
150
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000151/// OneOf - Does the input string contain this character?
152bool OneOf(const char* lst, char c) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000153 while (*lst) {
154 if (*lst++ == c)
155 return true;
156 }
157 return false;
158}
159
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000160template <class I, class S>
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000161void CheckedIncrement(I& P, I E, S ErrorString) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000162 ++P;
163 if (P == E)
164 throw ErrorString;
165}
166
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000167// apply is needed because C++'s syntax doesn't let us construct a function
168// object and call it in the same statement.
169template<typename F, typename T0>
170void apply(F Fun, T0& Arg0) {
171 return Fun(Arg0);
172}
173
174template<typename F, typename T0, typename T1>
175void apply(F Fun, T0& Arg0, T1& Arg1) {
176 return Fun(Arg0, Arg1);
177}
178
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000179//===----------------------------------------------------------------------===//
180/// Back-end specific code
181
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000182
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000183/// OptionType - One of six different option types. See the
184/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000185namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000186
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000187 enum OptionType { Alias, Switch, SwitchList,
188 Parameter, ParameterList, Prefix, PrefixList };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000189
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000190 bool IsAlias(OptionType t) {
191 return (t == Alias);
192 }
193
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000194 bool IsList (OptionType t) {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000195 return (t == SwitchList || t == ParameterList || t == PrefixList);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000196 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000197
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000198 bool IsSwitch (OptionType t) {
199 return (t == Switch);
200 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000201
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000202 bool IsSwitchList (OptionType t) {
203 return (t == SwitchList);
204 }
205
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000206 bool IsParameter (OptionType t) {
207 return (t == Parameter || t == Prefix);
208 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000209
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000210}
211
212OptionType::OptionType stringToOptionType(const std::string& T) {
213 if (T == "alias_option")
214 return OptionType::Alias;
215 else if (T == "switch_option")
216 return OptionType::Switch;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000217 else if (T == "switch_list_option")
218 return OptionType::SwitchList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000219 else if (T == "parameter_option")
220 return OptionType::Parameter;
221 else if (T == "parameter_list_option")
222 return OptionType::ParameterList;
223 else if (T == "prefix_option")
224 return OptionType::Prefix;
225 else if (T == "prefix_list_option")
226 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000227 else
228 throw "Unknown option type: " + T + '!';
229}
230
231namespace OptionDescriptionFlags {
232 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000233 ReallyHidden = 0x4, OneOrMore = 0x8,
234 Optional = 0x10, CommaSeparated = 0x20,
235 ForwardNotSplit = 0x40, ZeroOrMore = 0x80 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000236}
237
238/// OptionDescription - Represents data contained in a single
239/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000240struct OptionDescription {
241 OptionType::OptionType Type;
242 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000243 unsigned Flags;
244 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000245 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000246 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000247
248 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000249 const std::string& n = "",
250 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000251 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000252 {}
253
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000254 /// GenTypeDeclaration - Returns the C++ variable type of this
255 /// option.
256 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000257
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000258 /// GenVariableName - Returns the variable name used in the
259 /// generated C++ code.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000260 std::string GenVariableName() const
261 { return "autogenerated::" + GenOptionType() + EscapeVariableName(Name); }
262
263 /// GenPlainVariableName - Returns the variable name without the namespace
264 /// prefix.
265 std::string GenPlainVariableName() const
266 { return GenOptionType() + EscapeVariableName(Name); }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000267
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000268 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000269 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000270
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000271 /// CheckConsistency - Check that the flags are consistent.
272 void CheckConsistency() const;
273
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000274 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000275
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000276 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000277
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000278 bool isMultiVal() const;
279
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000280 bool isCommaSeparated() const;
281 void setCommaSeparated();
282
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000283 bool isForwardNotSplit() const;
284 void setForwardNotSplit();
285
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000286 bool isRequired() const;
287 void setRequired();
288
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000289 bool isOneOrMore() const;
290 void setOneOrMore();
291
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000292 bool isZeroOrMore() const;
293 void setZeroOrMore();
294
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000295 bool isOptional() const;
296 void setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000297
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000298 bool isHidden() const;
299 void setHidden();
300
301 bool isReallyHidden() const;
302 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000303
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000304 bool isSwitch() const
305 { return OptionType::IsSwitch(this->Type); }
306
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000307 bool isSwitchList() const
308 { return OptionType::IsSwitchList(this->Type); }
309
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000310 bool isParameter() const
311 { return OptionType::IsParameter(this->Type); }
312
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000313 bool isList() const
314 { return OptionType::IsList(this->Type); }
315
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000316 bool isParameterList() const
317 { return (OptionType::IsList(this->Type)
318 && !OptionType::IsSwitchList(this->Type)); }
319
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000320private:
321
322 // GenOptionType - Helper function used by GenVariableName().
323 std::string GenOptionType() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000324};
325
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000326void OptionDescription::CheckConsistency() const {
327 unsigned i = 0;
328
329 i += this->isRequired();
330 i += this->isOptional();
331 i += this->isOneOrMore();
332 i += this->isZeroOrMore();
333
334 if (i > 1) {
335 throw "Only one of (required), (optional), (one_or_more) or "
336 "(zero_or_more) properties is allowed!";
337 }
338}
339
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000340void OptionDescription::Merge (const OptionDescription& other)
341{
342 if (other.Type != Type)
343 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000344
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000345 if (Help == other.Help || Help == DefaultHelpString)
346 Help = other.Help;
347 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000348 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000349 " defined for option " + Name + "\n";
350 }
351
352 Flags |= other.Flags;
353}
354
355bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000356 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000357}
358
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000359bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000360 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000361}
362
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000363bool OptionDescription::isCommaSeparated() const {
364 return Flags & OptionDescriptionFlags::CommaSeparated;
365}
366void OptionDescription::setCommaSeparated() {
367 Flags |= OptionDescriptionFlags::CommaSeparated;
368}
369
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000370bool OptionDescription::isForwardNotSplit() const {
371 return Flags & OptionDescriptionFlags::ForwardNotSplit;
372}
373void OptionDescription::setForwardNotSplit() {
374 Flags |= OptionDescriptionFlags::ForwardNotSplit;
375}
376
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000377bool OptionDescription::isRequired() const {
378 return Flags & OptionDescriptionFlags::Required;
379}
380void OptionDescription::setRequired() {
381 Flags |= OptionDescriptionFlags::Required;
382}
383
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000384bool OptionDescription::isOneOrMore() const {
385 return Flags & OptionDescriptionFlags::OneOrMore;
386}
387void OptionDescription::setOneOrMore() {
388 Flags |= OptionDescriptionFlags::OneOrMore;
389}
390
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000391bool OptionDescription::isZeroOrMore() const {
392 return Flags & OptionDescriptionFlags::ZeroOrMore;
393}
394void OptionDescription::setZeroOrMore() {
395 Flags |= OptionDescriptionFlags::ZeroOrMore;
396}
397
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000398bool OptionDescription::isOptional() const {
399 return Flags & OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000400}
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000401void OptionDescription::setOptional() {
402 Flags |= OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000403}
404
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000405bool OptionDescription::isHidden() const {
406 return Flags & OptionDescriptionFlags::Hidden;
407}
408void OptionDescription::setHidden() {
409 Flags |= OptionDescriptionFlags::Hidden;
410}
411
412bool OptionDescription::isReallyHidden() const {
413 return Flags & OptionDescriptionFlags::ReallyHidden;
414}
415void OptionDescription::setReallyHidden() {
416 Flags |= OptionDescriptionFlags::ReallyHidden;
417}
418
419const char* OptionDescription::GenTypeDeclaration() const {
420 switch (Type) {
421 case OptionType::Alias:
422 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000423 case OptionType::PrefixList:
424 case OptionType::ParameterList:
425 return "cl::list<std::string>";
426 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000427 return "cl::opt<bool>";
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000428 case OptionType::SwitchList:
429 return "cl::list<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000430 case OptionType::Parameter:
431 case OptionType::Prefix:
432 default:
433 return "cl::opt<std::string>";
434 }
435}
436
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000437std::string OptionDescription::GenOptionType() const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000438 switch (Type) {
439 case OptionType::Alias:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000440 return "Alias_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000441 case OptionType::PrefixList:
442 case OptionType::ParameterList:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000443 return "List_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000444 case OptionType::Switch:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000445 return "Switch_";
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000446 case OptionType::SwitchList:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000447 return "SwitchList_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000448 case OptionType::Prefix:
449 case OptionType::Parameter:
450 default:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000451 return "Parameter_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000452 }
453}
454
455/// OptionDescriptions - An OptionDescription array plus some helper
456/// functions.
457class OptionDescriptions {
458 typedef StringMap<OptionDescription> container_type;
459
460 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000461 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000462
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000463public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000464 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000465 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000466
467 // Wrappers for FindOption that throw an exception in case the option has a
468 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000469 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000470 const OptionDescription& FindParameter(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000471 const OptionDescription& FindParameterList(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000472 const OptionDescription&
473 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000474 const OptionDescription&
475 FindParameterListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000476
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000477 /// insertDescription - Insert new OptionDescription into
478 /// OptionDescriptions list
479 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000480
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000481 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000482 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000483 const_iterator begin() const { return Descriptions.begin(); }
484 const_iterator end() const { return Descriptions.end(); }
485};
486
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000487const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000488OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000489 const_iterator I = Descriptions.find(OptName);
490 if (I != Descriptions.end())
491 return I->second;
492 else
493 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000494}
495
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000496const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000497OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000498 const OptionDescription& OptDesc = this->FindOption(OptName);
499 if (!OptDesc.isSwitch())
500 throw OptName + ": incorrect option type - should be a switch!";
501 return OptDesc;
502}
503
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000504const OptionDescription&
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000505OptionDescriptions::FindParameterList(const std::string& OptName) const {
506 const OptionDescription& OptDesc = this->FindOption(OptName);
507 if (!OptDesc.isList() || OptDesc.isSwitchList())
508 throw OptName + ": incorrect option type - should be a parameter list!";
509 return OptDesc;
510}
511
512const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000513OptionDescriptions::FindParameter(const std::string& OptName) const {
514 const OptionDescription& OptDesc = this->FindOption(OptName);
515 if (!OptDesc.isParameter())
516 throw OptName + ": incorrect option type - should be a parameter!";
517 return OptDesc;
518}
519
520const OptionDescription&
521OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
522 const OptionDescription& OptDesc = this->FindOption(OptName);
523 if (!OptDesc.isList() && !OptDesc.isParameter())
524 throw OptName
525 + ": incorrect option type - should be a list or parameter!";
526 return OptDesc;
527}
528
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000529const OptionDescription&
530OptionDescriptions::FindParameterListOrParameter
531(const std::string& OptName) const {
532 const OptionDescription& OptDesc = this->FindOption(OptName);
533 if ((!OptDesc.isList() && !OptDesc.isParameter()) || OptDesc.isSwitchList())
534 throw OptName
535 + ": incorrect option type - should be a parameter list or parameter!";
536 return OptDesc;
537}
538
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000539void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000540 container_type::iterator I = Descriptions.find(o.Name);
541 if (I != Descriptions.end()) {
542 OptionDescription& D = I->second;
543 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000544 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000545 else {
546 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000547 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000548}
549
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000550/// HandlerTable - A base class for function objects implemented as
551/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000552template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000553class HandlerTable {
554protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000555 // Implementation details.
556
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000557 /// HandlerMap - A map from property names to property handlers
558 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000559
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000560 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000561 static bool staticMembersInitialized_;
562
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000563public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000564
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000565 Handler GetHandler (const std::string& HandlerName) const {
566 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000567
568 if (method != Handlers_.end()) {
569 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000570 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000571 }
572 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000573 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000574 }
575 }
576
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000577 void AddHandler(const char* Property, Handler H) {
578 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000579 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000580
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000581};
582
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000583template <class Handler, class FunctionObject>
584Handler GetHandler(FunctionObject* Obj, const DagInit& Dag) {
585 const std::string& HandlerName = GetOperatorName(Dag);
586 return Obj->GetHandler(HandlerName);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000587}
588
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000589template <class FunctionObject>
590void InvokeDagInitHandler(FunctionObject* Obj, Init* I) {
591 typedef void (FunctionObject::*Handler) (const DagInit&);
592
593 const DagInit& Dag = InitPtrToDag(I);
594 Handler h = GetHandler<Handler>(Obj, Dag);
595
596 ((Obj)->*(h))(Dag);
597}
598
599template <class FunctionObject>
600void InvokeDagInitHandler(const FunctionObject* const Obj,
601 const Init* I, unsigned IndentLevel, raw_ostream& O)
602{
603 typedef void (FunctionObject::*Handler)
604 (const DagInit&, unsigned IndentLevel, raw_ostream& O) const;
605
606 const DagInit& Dag = InitPtrToDag(I);
607 Handler h = GetHandler<Handler>(Obj, Dag);
608
609 ((Obj)->*(h))(Dag, IndentLevel, O);
610}
611
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000612template <typename H>
613typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
614
615template <typename H>
616bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000617
618
619/// CollectOptionProperties - Function object for iterating over an
620/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000621class CollectOptionProperties;
622typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000623(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000624
625class CollectOptionProperties
626: public HandlerTable<CollectOptionPropertiesHandler>
627{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000628private:
629
630 /// optDescs_ - OptionDescriptions table. This is where the
631 /// information is stored.
632 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000633
634public:
635
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000636 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000637 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000638 {
639 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000640 AddHandler("help", &CollectOptionProperties::onHelp);
641 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000642 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000643 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
644 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000645 AddHandler("zero_or_more", &CollectOptionProperties::onZeroOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000646 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
647 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000648 AddHandler("optional", &CollectOptionProperties::onOptional);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000649 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000650 AddHandler("forward_not_split",
651 &CollectOptionProperties::onForwardNotSplit);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000652
653 staticMembersInitialized_ = true;
654 }
655 }
656
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000657 /// operator() - Just forwards to the corresponding property
658 /// handler.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000659 void operator() (Init* I) {
660 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000661 }
662
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000663private:
664
665 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000666 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000667
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000668 void onHelp (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000669 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000670 optDesc_.Help = EscapeQuotes(InitPtrToString(d.getArg(0)));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000671 }
672
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000673 void onHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000674 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000675 optDesc_.setHidden();
676 }
677
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000678 void onReallyHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000679 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000680 optDesc_.setReallyHidden();
681 }
682
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000683 void onCommaSeparated (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000684 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000685 if (!optDesc_.isParameterList())
686 throw "'comma_separated' is valid only on parameter list options!";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000687 optDesc_.setCommaSeparated();
688 }
689
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000690 void onForwardNotSplit (const DagInit& d) {
691 CheckNumberOfArguments(d, 0);
692 if (!optDesc_.isParameter())
693 throw "'forward_not_split' is valid only for parameter options!";
694 optDesc_.setForwardNotSplit();
695 }
696
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000697 void onRequired (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000698 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000699
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000700 optDesc_.setRequired();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000701 optDesc_.CheckConsistency();
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000702 }
703
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000704 void onInit (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000705 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000706 Init* i = d.getArg(0);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000707 const std::string& str = i->getAsString();
708
709 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
710 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
711
712 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000713 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000714
715 optDesc_.InitVal = i;
716 }
717
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000718 void onOneOrMore (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000719 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000720
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000721 optDesc_.setOneOrMore();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000722 optDesc_.CheckConsistency();
723 }
724
725 void onZeroOrMore (const DagInit& d) {
726 CheckNumberOfArguments(d, 0);
727
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000728 if (optDesc_.isList())
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000729 llvm::errs() << "Warning: specifying the 'zero_or_more' property "
730 "on a list option has no effect.\n";
731
732 optDesc_.setZeroOrMore();
733 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000734 }
735
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000736 void onOptional (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000737 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000738
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000739 if (!optDesc_.isList())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000740 llvm::errs() << "Warning: specifying the 'optional' property"
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000741 "on a non-list option has no effect.\n";
742
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000743 optDesc_.setOptional();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000744 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000745 }
746
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000747 void onMultiVal (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000748 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000749 int val = InitPtrToInt(d.getArg(0));
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000750 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000751 throw "Error in the 'multi_val' property: "
752 "the value must be greater than 1!";
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000753 if (!optDesc_.isParameterList())
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000754 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000755 optDesc_.MultiVal = val;
756 }
757
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000758};
759
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000760/// AddOption - A function object that is applied to every option
761/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000762class AddOption {
763private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000764 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000765
766public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000767 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000768 {}
769
770 void operator()(const Init* i) {
771 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000772 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000773
774 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000775 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000776 const std::string& Name = InitPtrToString(d.getArg(0));
777
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000778 OptionDescription OD(Type, Name);
779
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000780 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000781
782 if (OD.isAlias()) {
783 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000784 OD.Help = InitPtrToString(d.getArg(1));
785 }
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000786 else {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000787 processOptionProperties(d, OD);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000788 }
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000789
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000790 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000791 }
792
793private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000794 /// processOptionProperties - Go through the list of option
795 /// properties and call a corresponding handler for each.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000796 static void processOptionProperties (const DagInit& d, OptionDescription& o) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000797 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000798 DagInit::const_arg_iterator B = d.arg_begin();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000799 // Skip the first argument: it's always the option name.
800 ++B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000801 std::for_each(B, d.arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000802 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000803
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000804};
805
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000806/// CollectOptionDescriptions - Collects option properties from all
807/// OptionLists.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000808void CollectOptionDescriptions (const RecordVector& V,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000809 OptionDescriptions& OptDescs)
810{
811 // For every OptionList:
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000812 for (RecordVector::const_iterator B = V.begin(),
813 E = V.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000814 // Throws an exception if the value does not exist.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000815 ListInit* PropList = (*B)->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000816
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000817 // For every option description in this list:
818 // collect the information and
819 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
820 }
821}
822
823// Tool information record
824
825namespace ToolFlags {
826 enum ToolFlags { Join = 0x1, Sink = 0x2 };
827}
828
829struct ToolDescription : public RefCountedBase<ToolDescription> {
830 std::string Name;
831 Init* CmdLine;
832 Init* Actions;
833 StrVector InLanguage;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000834 std::string InFileOption;
835 std::string OutFileOption;
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000836 StrVector OutLanguage;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000837 std::string OutputSuffix;
838 unsigned Flags;
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000839 const Init* OnEmpty;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000840
841 // Various boolean properties
842 void setSink() { Flags |= ToolFlags::Sink; }
843 bool isSink() const { return Flags & ToolFlags::Sink; }
844 void setJoin() { Flags |= ToolFlags::Join; }
845 bool isJoin() const { return Flags & ToolFlags::Join; }
846
847 // Default ctor here is needed because StringMap can only store
848 // DefaultConstructible objects
Chris Lattner2d900582010-08-28 03:43:50 +0000849 ToolDescription (const std::string &n = "")
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000850 : Name(n), CmdLine(0), Actions(0), OutFileOption("-o"),
851 Flags(0), OnEmpty(0)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000852 {}
853};
854
855/// ToolDescriptions - A list of Tool information records.
856typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
857
858
859/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000860/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000861
862class CollectToolProperties;
863typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000864(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000865
866class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
867{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000868private:
869
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000870 /// toolDesc_ - Properties of the current Tool. This is where the
871 /// information is stored.
872 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000873
874public:
875
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000876 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000877 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000878 {
879 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000880
881 AddHandler("actions", &CollectToolProperties::onActions);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000882 AddHandler("command", &CollectToolProperties::onCommand);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000883 AddHandler("in_language", &CollectToolProperties::onInLanguage);
884 AddHandler("join", &CollectToolProperties::onJoin);
885 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000886
887 AddHandler("out_file_option", &CollectToolProperties::onOutFileOption);
888 AddHandler("in_file_option", &CollectToolProperties::onInFileOption);
889
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000890 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
891 AddHandler("sink", &CollectToolProperties::onSink);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000892 AddHandler("works_on_empty", &CollectToolProperties::onWorksOnEmpty);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000893
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000894 staticMembersInitialized_ = true;
895 }
896 }
897
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000898 void operator() (Init* I) {
899 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000900 }
901
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000902private:
903
904 /// Property handlers --
905 /// Functions that extract information about tool properties from
906 /// DAG representation.
907
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000908 void onActions (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000909 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000910 Init* Case = d.getArg(0);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000911 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000912 GetOperatorName(static_cast<DagInit&>(*Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000913 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000914 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000915 }
916
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000917 void onCommand (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000918 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000919 toolDesc_.CmdLine = d.getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000920 }
921
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000922 /// onInOutLanguage - Common implementation of on{In,Out}Language().
923 void onInOutLanguage (const DagInit& d, StrVector& OutVec) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000924 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000925 Init* arg = d.getArg(0);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000926
927 // Find out the argument's type.
928 if (typeid(*arg) == typeid(StringInit)) {
929 // It's a string.
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000930 OutVec.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000931 }
932 else {
933 // It's a list.
934 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000935
936 // Copy strings to the output vector.
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000937 for (ListInit::const_iterator B = lst.begin(), E = lst.end(); B != E; ++B)
938 OutVec.push_back(InitPtrToString(*B));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000939
940 // Remove duplicates.
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000941 std::sort(OutVec.begin(), OutVec.end());
942 StrVector::iterator newE = std::unique(OutVec.begin(), OutVec.end());
943 OutVec.erase(newE, OutVec.end());
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000944 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000945 }
946
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000947
948 void onInLanguage (const DagInit& d) {
949 this->onInOutLanguage(d, toolDesc_.InLanguage);
950 }
951
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000952 void onJoin (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000953 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000954 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000955 }
956
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000957 void onOutLanguage (const DagInit& d) {
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000958 this->onInOutLanguage(d, toolDesc_.OutLanguage);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000959 }
960
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000961 void onOutFileOption (const DagInit& d) {
962 CheckNumberOfArguments(d, 1);
963 toolDesc_.OutFileOption = InitPtrToString(d.getArg(0));
964 }
965
966 void onInFileOption (const DagInit& d) {
967 CheckNumberOfArguments(d, 1);
968 toolDesc_.InFileOption = InitPtrToString(d.getArg(0));
969 }
970
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000971 void onOutputSuffix (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000972 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000973 toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000974 }
975
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000976 void onSink (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000977 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000978 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000979 }
980
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000981 void onWorksOnEmpty (const DagInit& d) {
982 toolDesc_.OnEmpty = d.getArg(0);
983 }
984
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000985};
986
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000987/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000988/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000989/// CollectToolProperties function object).
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000990void CollectToolDescriptions (const RecordVector& Tools,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000991 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000992{
993 // Iterate over a properties list of every Tool definition
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000994 for (RecordVector::const_iterator B = Tools.begin(),
995 E = Tools.end(); B!=E; ++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000996 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000997 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000998 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000999
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001000 IntrusiveRefCntPtr<ToolDescription>
1001 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001002
1003 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001004 CollectToolProperties(*ToolDesc));
1005 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001006 }
1007}
1008
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001009/// FillInEdgeVector - Merge all compilation graph definitions into
1010/// one single edge list.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001011void FillInEdgeVector(const RecordVector& CompilationGraphs,
1012 DagVector& Out) {
1013 for (RecordVector::const_iterator B = CompilationGraphs.begin(),
1014 E = CompilationGraphs.end(); B != E; ++B) {
1015 const ListInit* Edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +00001016
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001017 for (ListInit::const_iterator B = Edges->begin(),
1018 E = Edges->end(); B != E; ++B) {
1019 Out.push_back(&InitPtrToDag(*B));
1020 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001021 }
1022}
1023
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001024/// NotInGraph - Helper function object for FilterNotInGraph.
1025struct NotInGraph {
1026private:
1027 const llvm::StringSet<>& ToolsInGraph_;
1028
1029public:
1030 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
1031 : ToolsInGraph_(ToolsInGraph)
1032 {}
1033
1034 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
1035 return (ToolsInGraph_.count(x->Name) == 0);
1036 }
1037};
1038
1039/// FilterNotInGraph - Filter out from ToolDescs all Tools not
1040/// mentioned in the compilation graph definition.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001041void FilterNotInGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001042 ToolDescriptions& ToolDescs) {
1043
1044 // List all tools mentioned in the graph.
1045 llvm::StringSet<> ToolsInGraph;
1046
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001047 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001048 E = EdgeVector.end(); B != E; ++B) {
1049
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001050 const DagInit* Edge = *B;
1051 const std::string& NodeA = InitPtrToString(Edge->getArg(0));
1052 const std::string& NodeB = InitPtrToString(Edge->getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001053
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001054 if (NodeA != "root")
1055 ToolsInGraph.insert(NodeA);
1056 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001057 }
1058
1059 // Filter ToolPropertiesList.
1060 ToolDescriptions::iterator new_end =
1061 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
1062 NotInGraph(ToolsInGraph));
1063 ToolDescs.erase(new_end, ToolDescs.end());
1064}
1065
1066/// FillInToolToLang - Fills in two tables that map tool names to
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001067/// input & output language names. Helper function used by TypecheckGraph().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001068void FillInToolToLang (const ToolDescriptions& ToolDescs,
1069 StringMap<StringSet<> >& ToolToInLang,
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001070 StringMap<StringSet<> >& ToolToOutLang) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001071 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1072 E = ToolDescs.end(); B != E; ++B) {
1073 const ToolDescription& D = *(*B);
1074 for (StrVector::const_iterator B = D.InLanguage.begin(),
1075 E = D.InLanguage.end(); B != E; ++B)
1076 ToolToInLang[D.Name].insert(*B);
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001077 for (StrVector::const_iterator B = D.OutLanguage.begin(),
1078 E = D.OutLanguage.end(); B != E; ++B)
1079 ToolToOutLang[D.Name].insert(*B);
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001080 }
1081}
1082
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001083/// Intersect - Is set intersection non-empty?
1084bool Intersect (const StringSet<>& S1, const StringSet<>& S2) {
1085 for (StringSet<>::const_iterator B = S1.begin(), E = S1.end(); B != E; ++B) {
1086 if (S2.count(B->first()) != 0)
1087 return true;
1088 }
1089 return false;
1090}
1091
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001092/// TypecheckGraph - Check that names for output and input languages
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00001093/// on all edges do match.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001094void TypecheckGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001095 const ToolDescriptions& ToolDescs) {
1096 StringMap<StringSet<> > ToolToInLang;
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001097 StringMap<StringSet<> > ToolToOutLang;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001098
1099 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001100
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001101 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001102 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001103 const DagInit* Edge = *B;
1104 const std::string& NodeA = InitPtrToString(Edge->getArg(0));
1105 const std::string& NodeB = InitPtrToString(Edge->getArg(1));
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001106 StringMap<StringSet<> >::iterator IA = ToolToOutLang.find(NodeA);
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001107 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001108
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001109 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001110 throw "Edges back to the root are not allowed!";
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001111
1112 if (NodeA != "root") {
1113 if (IA == ToolToOutLang.end())
1114 throw NodeA + ": no output language defined!";
1115 if (IB == ToolToInLang.end())
1116 throw NodeB + ": no input language defined!";
1117
1118 if (!Intersect(IA->second, IB->second)) {
1119 throw "Edge " + NodeA + "->" + NodeB
1120 + ": output->input language mismatch";
1121 }
1122 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001123 }
1124}
1125
1126/// WalkCase - Walks the 'case' expression DAG and invokes
1127/// TestCallback on every test, and StatementCallback on every
1128/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001129/// combinators (that is, they are passed directly to TestCallback).
1130/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1131/// IndentLevel, bool FirstTest)'.
1132/// StatementCallback must have type 'void StatementCallback(const Init*,
1133/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001134template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001135void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1136 unsigned IndentLevel = 0)
1137{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001138 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001139
1140 // Error checks.
1141 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001142 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001143
1144 if (d.getNumArgs() < 2)
1145 throw "There should be at least one clause in the 'case' expression:\n"
1146 + d.getAsString();
1147
1148 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001149 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001150 const unsigned numArgs = d.getNumArgs();
1151 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001152 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1153 B != E; ++B) {
1154 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001155
1156 if (!even)
1157 {
1158 // Handle test.
1159 const DagInit& Test = InitPtrToDag(arg);
1160
1161 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001162 throw "The 'default' clause should be the last in the "
1163 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001164 if (i == numArgs)
1165 throw "Case construct handler: no corresponding action "
1166 "found for the test " + Test.getAsString() + '!';
1167
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001168 TestCallback(Test, IndentLevel, (i == 1));
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001169 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001170 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001171 {
1172 if (dynamic_cast<DagInit*>(arg)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001173 && GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001174 // Nested 'case'.
1175 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1176 }
1177
1178 // Handle statement.
1179 StatementCallback(arg, IndentLevel);
1180 }
1181
1182 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001183 even = !even;
1184 }
1185}
1186
1187/// ExtractOptionNames - A helper function object used by
1188/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1189class ExtractOptionNames {
1190 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001191
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001192 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001193 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001194 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001195 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001196 ActionName == "forward_value" ||
1197 ActionName == "forward_transformed_value" ||
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001198 ActionName == "switch_on" || ActionName == "any_switch_on" ||
1199 ActionName == "parameter_equals" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001200 ActionName == "element_in_list" || ActionName == "not_empty" ||
1201 ActionName == "empty") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001202 CheckNumberOfArguments(Stmt, 1);
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001203
1204 Init* Arg = Stmt.getArg(0);
1205 if (typeid(*Arg) == typeid(StringInit)) {
1206 const std::string& Name = InitPtrToString(Arg);
1207 OptionNames_.insert(Name);
1208 }
1209 else {
1210 // It's a list.
1211 const ListInit& List = InitPtrToList(Arg);
1212 for (ListInit::const_iterator B = List.begin(), E = List.end();
1213 B != E; ++B) {
1214 const std::string& Name = InitPtrToString(*B);
1215 OptionNames_.insert(Name);
1216 }
1217 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001218 }
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001219 else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001220 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001221 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001222 }
1223 }
1224 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001225
1226public:
1227 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1228 {}
1229
1230 void operator()(const Init* Statement) {
1231 if (typeid(*Statement) == typeid(ListInit)) {
1232 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1233 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1234 B != E; ++B)
1235 this->processDag(*B);
1236 }
1237 else {
1238 this->processDag(Statement);
1239 }
1240 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001241
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001242 void operator()(const DagInit& Test, unsigned, bool) {
1243 this->operator()(&Test);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001244 }
1245 void operator()(const Init* Statement, unsigned) {
1246 this->operator()(Statement);
1247 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001248};
1249
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00001250/// IsOptionalEdge - Validate that the 'optional_edge' has proper structure.
1251bool IsOptionalEdge (const DagInit& Edg) {
1252 return (GetOperatorName(Edg) == "optional_edge") && (Edg.getNumArgs() > 2);
1253}
1254
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001255/// CheckForSuperfluousOptions - Check that there are no side
1256/// effect-free options (specified only in the OptionList). Otherwise,
1257/// output a warning.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001258void CheckForSuperfluousOptions (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001259 const ToolDescriptions& ToolDescs,
1260 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001261 llvm::StringSet<> nonSuperfluousOptions;
1262
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001263 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001264 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001265 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1266 E = ToolDescs.end(); B != E; ++B) {
1267 const ToolDescription& TD = *(*B);
1268 ExtractOptionNames Callback(nonSuperfluousOptions);
1269 if (TD.Actions)
1270 WalkCase(TD.Actions, Callback, Callback);
1271 }
1272
1273 // Add all options mentioned in the 'case' clauses of the
1274 // OptionalEdges of the compilation graph to the set of
1275 // non-superfluous options.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001276 for (DagVector::const_iterator B = EdgeVector.begin(),
1277 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00001278 const DagInit& Edge = **B;
1279 if (IsOptionalEdge(Edge)) {
1280 const DagInit& Weight = InitPtrToDag(Edge.getArg(2));
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001281 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001282 }
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001283 }
1284
1285 // Check that all options in OptDescs belong to the set of
1286 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001287 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001288 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001289 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001290 if (!nonSuperfluousOptions.count(Val.Name)
1291 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001292 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001293 "Probable cause: this option is specified only in the OptionList.\n";
1294 }
1295}
1296
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001297/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1298bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1299 if (TestName == "single_input_file") {
1300 O << "InputFilenames.size() == 1";
1301 return true;
1302 }
1303 else if (TestName == "multiple_input_files") {
1304 O << "InputFilenames.size() > 1";
1305 return true;
1306 }
1307
1308 return false;
1309}
1310
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001311/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1312template <typename F>
1313void EmitListTest(const ListInit& L, const char* LogicOp,
1314 F Callback, raw_ostream& O)
1315{
1316 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1317 // of Dags...
1318 bool isFirst = true;
1319 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1320 if (isFirst)
1321 isFirst = false;
1322 else
Mikhail Glushenkovb7935e02010-01-01 04:40:54 +00001323 O << ' ' << LogicOp << ' ';
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001324 Callback(InitPtrToString(*B), O);
1325 }
1326}
1327
1328// Callbacks for use with EmitListTest.
1329
1330class EmitSwitchOn {
1331 const OptionDescriptions& OptDescs_;
1332public:
1333 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1334 {}
1335
1336 void operator()(const std::string& OptName, raw_ostream& O) const {
1337 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1338 O << OptDesc.GenVariableName();
1339 }
1340};
1341
1342class EmitEmptyTest {
1343 bool EmitNegate_;
1344 const OptionDescriptions& OptDescs_;
1345public:
1346 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1347 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1348 {}
1349
1350 void operator()(const std::string& OptName, raw_ostream& O) const {
1351 const char* Neg = (EmitNegate_ ? "!" : "");
1352 if (OptName == "o") {
1353 O << Neg << "OutputFilename.empty()";
1354 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001355 else if (OptName == "save-temps") {
1356 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1357 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001358 else {
1359 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1360 O << Neg << OptDesc.GenVariableName() << ".empty()";
1361 }
1362 }
1363};
1364
1365
1366/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1367bool EmitCaseTest1ArgList(const std::string& TestName,
1368 const DagInit& d,
1369 const OptionDescriptions& OptDescs,
1370 raw_ostream& O) {
Mikhail Glushenkov3a481e32010-01-01 03:50:51 +00001371 const ListInit& L = InitPtrToList(d.getArg(0));
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001372
1373 if (TestName == "any_switch_on") {
1374 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1375 return true;
1376 }
1377 else if (TestName == "switch_on") {
1378 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1379 return true;
1380 }
1381 else if (TestName == "any_not_empty") {
1382 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1383 return true;
1384 }
1385 else if (TestName == "any_empty") {
1386 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1387 return true;
1388 }
1389 else if (TestName == "not_empty") {
1390 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1391 return true;
1392 }
1393 else if (TestName == "empty") {
1394 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1395 return true;
1396 }
1397
1398 return false;
1399}
1400
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001401/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1402bool EmitCaseTest1ArgStr(const std::string& TestName,
1403 const DagInit& d,
1404 const OptionDescriptions& OptDescs,
1405 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001406 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001407
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001408 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001409 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001410 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001411 }
1412 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001413 O << "InLangs.count(\"" << OptName << "\") != 0";
1414 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001415 }
1416 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001417 // This works only for single-argument Tool::GenerateAction. Join
1418 // tools can process several files in different languages simultaneously.
1419
1420 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001421 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001422 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001423 }
1424 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001425 bool EmitNegate = (TestName == "not_empty");
1426 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001427 return true;
1428 }
1429
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001430 return false;
1431}
1432
1433/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1434bool EmitCaseTest1Arg(const std::string& TestName,
1435 const DagInit& d,
1436 const OptionDescriptions& OptDescs,
1437 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001438 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001439 if (typeid(*d.getArg(0)) == typeid(ListInit))
1440 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1441 else
1442 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1443}
1444
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001445/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001446bool EmitCaseTest2Args(const std::string& TestName,
1447 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001448 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001449 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001450 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001451 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001452 const std::string& OptName = InitPtrToString(d.getArg(0));
1453 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001454
1455 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001456 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001457 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1458 return true;
1459 }
1460 else if (TestName == "element_in_list") {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00001461 const OptionDescription& OptDesc = OptDescs.FindParameterList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001462 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001463 O << "std::find(" << VarName << ".begin(),\n";
1464 O.indent(IndentLevel + Indent1)
1465 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001466 << OptArg << "\") != " << VarName << ".end()";
1467 return true;
1468 }
1469
1470 return false;
1471}
1472
1473// Forward declaration.
1474// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001475void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001476 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001477 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001478
1479/// EmitLogicalOperationTest - Helper function used by
1480/// EmitCaseConstructHandler.
1481void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001482 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001483 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001484 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001485 O << '(';
1486 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001487 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001488 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001489 if (j != NumArgs - 1) {
1490 O << ")\n";
1491 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1492 }
1493 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001494 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001495 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001496 }
1497}
1498
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001499void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001500 const OptionDescriptions& OptDescs, raw_ostream& O)
1501{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001502 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001503 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1504 O << "! (";
1505 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1506 O << ")";
1507}
1508
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001509/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001510void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001511 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001512 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001513 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001514
1515 if (TestName == "and")
1516 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1517 else if (TestName == "or")
1518 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001519 else if (TestName == "not")
1520 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001521 else if (EmitCaseTest0Args(TestName, O))
1522 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001523 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1524 return;
1525 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1526 return;
1527 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001528 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001529}
1530
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001531
1532/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1533class EmitCaseTestCallback {
1534 bool EmitElseIf_;
1535 const OptionDescriptions& OptDescs_;
1536 raw_ostream& O_;
1537public:
1538
1539 EmitCaseTestCallback(bool EmitElseIf,
1540 const OptionDescriptions& OptDescs, raw_ostream& O)
1541 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1542 {}
1543
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001544 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001545 {
1546 if (GetOperatorName(Test) == "default") {
1547 O_.indent(IndentLevel) << "else {\n";
1548 }
1549 else {
1550 O_.indent(IndentLevel)
1551 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001552 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001553 O_ << ") {\n";
1554 }
1555 }
1556};
1557
1558/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1559template <typename F>
1560class EmitCaseStatementCallback {
1561 F Callback_;
1562 raw_ostream& O_;
1563public:
1564
1565 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1566 : Callback_(Callback), O_(O)
1567 {}
1568
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001569 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001570
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001571 // Ignore nested 'case' DAG.
1572 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001573 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case")) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001574 if (typeid(*Statement) == typeid(ListInit)) {
1575 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1576 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1577 B != E; ++B)
1578 Callback_(*B, (IndentLevel + Indent1), O_);
1579 }
1580 else {
1581 Callback_(Statement, (IndentLevel + Indent1), O_);
1582 }
1583 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001584 O_.indent(IndentLevel) << "}\n";
1585 }
1586
1587};
1588
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001589/// EmitCaseConstructHandler - Emit code that handles the 'case'
1590/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001591/// clause. Implemented on top of WalkCase.
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00001592/// Callback's type is void F(const Init* Statement, unsigned IndentLevel,
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001593/// raw_ostream& O).
1594/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001595/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1596/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001597template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001598void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001599 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001600 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001601 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001602 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1603 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001604}
1605
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001606/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001607/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1608/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001609void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001610 const char* Delimiters = " \t\n\v\f\r";
1611 enum TokenizerState
1612 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1613 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001614
1615 if (CmdLine.empty())
1616 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001617 Out.push_back("");
1618
1619 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1620 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001621
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001622 for (; B != E; ++B) {
1623 char cur_ch = CmdLine[B];
1624
1625 switch (cur_st) {
1626 case Normal:
1627 if (cur_ch == '$') {
1628 cur_st = SpecialCommand;
1629 break;
1630 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001631 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001632 // Skip whitespace
1633 B = CmdLine.find_first_not_of(Delimiters, B);
1634 if (B == std::string::npos) {
1635 B = E-1;
1636 continue;
1637 }
1638 --B;
1639 Out.push_back("");
1640 continue;
1641 }
1642 break;
1643
1644
1645 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001646 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001647 cur_st = Normal;
1648 Out.push_back("");
1649 continue;
1650 }
1651 if (cur_ch == '(') {
1652 Out.push_back("");
1653 cur_st = InsideSpecialCommand;
1654 continue;
1655 }
1656 break;
1657
1658 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001659 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001660 continue;
1661 }
1662 if (cur_ch == '\'') {
1663 cur_st = InsideQuotationMarks;
1664 Out.push_back("");
1665 continue;
1666 }
1667 if (cur_ch == ')') {
1668 cur_st = Normal;
1669 Out.push_back("");
1670 }
1671 if (cur_ch == ',') {
1672 continue;
1673 }
1674
1675 break;
1676
1677 case InsideQuotationMarks:
1678 if (cur_ch == '\'') {
1679 cur_st = InsideSpecialCommand;
1680 continue;
1681 }
1682 break;
1683 }
1684
1685 Out.back().push_back(cur_ch);
1686 }
1687}
1688
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001689/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1690/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1691/// SubstituteSpecialCommands().
1692StrVector::const_iterator
1693SubstituteCall (StrVector::const_iterator Pos,
1694 StrVector::const_iterator End,
1695 bool IsJoin, raw_ostream& O)
1696{
1697 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001698 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001699 const std::string& CmdName = *Pos;
1700
1701 if (CmdName == ")")
1702 throw "$CALL invocation: empty argument list!";
1703
1704 O << "hooks::";
1705 O << CmdName << "(";
1706
1707
1708 bool firstIteration = true;
1709 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001710 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001711 const std::string& Arg = *Pos;
1712 assert(Arg.size() != 0);
1713
1714 if (Arg[0] == ')')
1715 break;
1716
1717 if (firstIteration)
1718 firstIteration = false;
1719 else
1720 O << ", ";
1721
1722 if (Arg == "$INFILE") {
1723 if (IsJoin)
1724 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1725 else
1726 O << "inFile.c_str()";
1727 }
1728 else {
1729 O << '"' << Arg << '"';
1730 }
1731 }
1732
1733 O << ')';
1734
1735 return Pos;
1736}
1737
1738/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1739/// function used by SubstituteSpecialCommands().
1740StrVector::const_iterator
1741SubstituteEnv (StrVector::const_iterator Pos,
1742 StrVector::const_iterator End, raw_ostream& O)
1743{
1744 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001745 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001746 const std::string& EnvName = *Pos;
1747
1748 if (EnvName == ")")
1749 throw "$ENV invocation: empty argument list!";
1750
1751 O << "checkCString(std::getenv(\"";
1752 O << EnvName;
1753 O << "\"))";
1754
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001755 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001756
1757 return Pos;
1758}
1759
1760/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1761/// handler code. Helper function used by EmitCmdLineVecFill().
1762StrVector::const_iterator
1763SubstituteSpecialCommands (StrVector::const_iterator Pos,
1764 StrVector::const_iterator End,
1765 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001766{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001767
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001768 const std::string& cmd = *Pos;
1769
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001770 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001771 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001772 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001773 }
1774 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001775 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001776 }
1777 else {
1778 throw "Unknown special command: " + cmd;
1779 }
1780
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001781 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001782 const std::string& Leftover = *Pos;
1783 assert(Leftover.at(0) == ')');
1784 if (Leftover.size() != 1)
1785 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001786
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001787 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001788}
1789
1790/// EmitCmdLineVecFill - Emit code that fills in the command line
1791/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001792void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001793 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001794 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001795 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001796 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001797
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001798 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001799 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001800
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001801 StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001802
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001803 // Emit the command itself.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001804 assert(!StrVec[0].empty());
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001805 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001806 if (StrVec[0][0] == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001807 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1808 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001809 }
1810 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001811 O << '"' << StrVec[0] << '"';
1812 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001813 }
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001814 O << ";\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001815
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001816 // Go through the command arguments.
1817 assert(B <= E);
1818 for (; B != E; ++B) {
1819 const std::string& cmd = *B;
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001820
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001821 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001822 O.indent(IndentLevel);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001823
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001824 if (cmd.at(0) == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001825 O << "vec.push_back(std::make_pair(0, ";
1826 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1827 O << "));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001828 }
1829 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001830 O << "vec.push_back(std::make_pair(0, \"" << cmd << "\"));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001831 }
1832 }
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001833
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001834}
1835
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001836/// EmitForEachListElementCycleHeader - Emit common code for iterating through
1837/// all elements of a list. Helper function used by
1838/// EmitForwardOptionPropertyHandlingCode.
1839void EmitForEachListElementCycleHeader (const OptionDescription& D,
1840 unsigned IndentLevel,
1841 raw_ostream& O) {
1842 unsigned IndentLevel1 = IndentLevel + Indent1;
1843
1844 O.indent(IndentLevel)
1845 << "for (" << D.GenTypeDeclaration()
1846 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1847 O.indent(IndentLevel)
1848 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1849 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1850 << ".getPosition(B - " << D.GenVariableName()
1851 << ".begin());\n";
1852}
1853
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001854/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1855/// implement EmitActionHandler. Emits code for
1856/// handling the (forward) and (forward_as) option properties.
1857void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001858 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001859 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001860 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001861 const std::string& Name = NewName.empty()
1862 ? ("-" + D.Name)
1863 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001864 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001865
1866 switch (D.Type) {
1867 case OptionType::Switch:
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001868 O.indent(IndentLevel)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001869 << "vec.push_back(std::make_pair(" << D.GenVariableName()
1870 << ".getPosition(), \"" << Name << "\"));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001871 break;
1872 case OptionType::Parameter:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001873 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1874 << D.GenVariableName()
1875 <<".getPosition(), \"" << Name;
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001876
1877 if (!D.isForwardNotSplit()) {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001878 O << "\"));\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001879 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1880 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001881 << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001882 }
1883 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001884 O << "=\" + " << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001885 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001886 break;
1887 case OptionType::Prefix:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001888 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1889 << D.GenVariableName() << ".getPosition(), \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001890 << Name << "\" + "
1891 << D.GenVariableName() << "));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001892 break;
1893 case OptionType::PrefixList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001894 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001895 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001896 << Name << "\" + " << "*B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001897 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001898
1899 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001900 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001901 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001902 }
1903
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001904 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001905 break;
1906 case OptionType::ParameterList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001907 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001908 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001909 << Name << "\"));\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001910
1911 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001912 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001913 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001914 }
1915
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001916 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001917 break;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001918 case OptionType::SwitchList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001919 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001920 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
1921 << Name << "\"));\n";
1922 O.indent(IndentLevel1) << "++B;\n";
1923 O.indent(IndentLevel) << "}\n";
1924 break;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001925 case OptionType::Alias:
1926 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001927 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001928 }
1929}
1930
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001931/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1932/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001933struct ActionHandlingCallbackBase
1934{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001935
1936 void onErrorDag(const DagInit& d,
1937 unsigned IndentLevel, raw_ostream& O) const
1938 {
1939 O.indent(IndentLevel)
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00001940 << "PrintError(\""
1941 << (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0)) : "Unknown error!")
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001942 << "\");\n";
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +00001943 O.indent(IndentLevel) << "return 1;\n";
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001944 }
1945
1946 void onWarningDag(const DagInit& d,
1947 unsigned IndentLevel, raw_ostream& O) const
1948 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001949 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001950 O.indent(IndentLevel) << "llvm::errs() << \""
1951 << InitPtrToString(d.getArg(0)) << "\";\n";
1952 }
1953
1954};
1955
1956/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1957/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001958class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001959
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001960typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1961(const DagInit&, unsigned, raw_ostream&) const;
1962
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001963class EmitActionHandlersCallback :
1964 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001965 public HandlerTable<EmitActionHandlersCallbackHandler>
1966{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001967 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001968
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001969 const OptionDescriptions& OptDescs;
1970
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001971 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1972 /// onAppendCmd and onOutputSuffix.
1973 void EmitHookInvocation(const std::string& Str,
1974 const char* BlockOpen, const char* BlockClose,
1975 unsigned IndentLevel, raw_ostream& O) const
1976 {
1977 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001978 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001979
1980 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1981 B != E; ++B) {
1982 const std::string& cmd = *B;
1983
1984 O.indent(IndentLevel) << BlockOpen;
1985
1986 if (cmd.at(0) == '$')
1987 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1988 else
1989 O << '"' << cmd << '"';
1990
1991 O << BlockClose;
1992 }
1993 }
1994
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001995 void onAppendCmd (const DagInit& Dag,
1996 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001997 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001998 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001999 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002000 "vec.push_back(std::make_pair(65536, ", "));\n",
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002001 IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002002 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00002003
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002004 void onForward (const DagInit& Dag,
2005 unsigned IndentLevel, raw_ostream& O) const
2006 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002007 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002008 const std::string& Name = InitPtrToString(Dag.getArg(0));
2009 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2010 IndentLevel, "", O);
2011 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002012
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002013 void onForwardAs (const DagInit& Dag,
2014 unsigned IndentLevel, raw_ostream& O) const
2015 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002016 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002017 const std::string& Name = InitPtrToString(Dag.getArg(0));
2018 const std::string& NewName = InitPtrToString(Dag.getArg(1));
2019 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2020 IndentLevel, NewName, O);
2021 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002022
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002023 void onForwardValue (const DagInit& Dag,
2024 unsigned IndentLevel, raw_ostream& O) const
2025 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002026 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002027 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002028 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002029
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002030 if (D.isSwitchList()) {
2031 throw std::runtime_error
2032 ("forward_value is not allowed with switch_list");
2033 }
2034
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002035 if (D.isParameter()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002036 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2037 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002038 << D.GenVariableName() << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002039 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00002040 else {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002041 O.indent(IndentLevel) << "for (" << D.GenTypeDeclaration()
2042 << "::iterator B = " << D.GenVariableName()
2043 << ".begin(), \n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002044 O.indent(IndentLevel + Indent1) << " E = " << D.GenVariableName()
2045 << ".end(); B != E; ++B)\n";
2046 O.indent(IndentLevel) << "{\n";
2047 O.indent(IndentLevel + Indent1)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002048 << "unsigned pos = " << D.GenVariableName()
2049 << ".getPosition(B - " << D.GenVariableName()
2050 << ".begin());\n";
2051 O.indent(IndentLevel + Indent1)
2052 << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002053 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002054 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002055 }
2056
2057 void onForwardTransformedValue (const DagInit& Dag,
2058 unsigned IndentLevel, raw_ostream& O) const
2059 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002060 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002061 const std::string& Name = InitPtrToString(Dag.getArg(0));
2062 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002063 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002064
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002065 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2066 << D.GenVariableName() << ".getPosition("
2067 << (D.isList() ? "0" : "") << "), "
2068 << "hooks::" << Hook << "(" << D.GenVariableName()
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002069 << (D.isParameter() ? ".c_str()" : "") << ")));\n";
2070 }
2071
2072 void onNoOutFile (const DagInit& Dag,
2073 unsigned IndentLevel, raw_ostream& O) const
2074 {
2075 CheckNumberOfArguments(Dag, 0);
2076 O.indent(IndentLevel) << "no_out_file = true;\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002077 }
2078
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002079 void onOutputSuffix (const DagInit& Dag,
2080 unsigned IndentLevel, raw_ostream& O) const
2081 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002082 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002083 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
2084 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002085 }
2086
2087 void onStopCompilation (const DagInit& Dag,
2088 unsigned IndentLevel, raw_ostream& O) const
2089 {
2090 O.indent(IndentLevel) << "stop_compilation = true;\n";
2091 }
2092
2093
2094 void onUnpackValues (const DagInit& Dag,
2095 unsigned IndentLevel, raw_ostream& O) const
2096 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002097 throw "'unpack_values' is deprecated. "
2098 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002099 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002100
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002101 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002102
2103 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
2104 : OptDescs(OD)
2105 {
2106 if (!staticMembersInitialized_) {
2107 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
2108 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
2109 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
2110 AddHandler("forward", &EmitActionHandlersCallback::onForward);
2111 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002112 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
2113 AddHandler("forward_transformed_value",
2114 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002115 AddHandler("no_out_file",
2116 &EmitActionHandlersCallback::onNoOutFile);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002117 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
2118 AddHandler("stop_compilation",
2119 &EmitActionHandlersCallback::onStopCompilation);
2120 AddHandler("unpack_values",
2121 &EmitActionHandlersCallback::onUnpackValues);
2122
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002123
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002124 staticMembersInitialized_ = true;
2125 }
2126 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002127
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002128 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002129 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002130 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002131 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002132 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002133};
2134
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002135void EmitGenerateActionMethodHeader(const ToolDescription& D,
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002136 bool IsJoin, bool Naked,
2137 raw_ostream& O)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002138{
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002139 O.indent(Indent1) << "int GenerateAction(Action& Out,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002140
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002141 if (IsJoin)
2142 O.indent(Indent2) << "const PathVector& inFiles,\n";
2143 else
2144 O.indent(Indent2) << "const sys::Path& inFile,\n";
2145
2146 O.indent(Indent2) << "const bool HasChildren,\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002147 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2148 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2149 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2150 O.indent(Indent1) << "{\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002151
2152 if (!Naked) {
2153 O.indent(Indent2) << "std::string cmd;\n";
2154 O.indent(Indent2) << "std::string out_file;\n";
2155 O.indent(Indent2)
2156 << "std::vector<std::pair<unsigned, std::string> > vec;\n";
2157 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2158 O.indent(Indent2) << "bool no_out_file = false;\n";
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002159 O.indent(Indent2) << "std::string output_suffix(\""
2160 << D.OutputSuffix << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002161 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002162}
2163
2164// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2165// Tool::GenerateAction() method.
2166void EmitGenerateActionMethod (const ToolDescription& D,
2167 const OptionDescriptions& OptDescs,
2168 bool IsJoin, raw_ostream& O) {
2169
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002170 EmitGenerateActionMethodHeader(D, IsJoin, /* Naked = */ false, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002171
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002172 if (!D.CmdLine)
2173 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002174
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002175 // Process the 'command' property.
2176 O << '\n';
2177 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2178 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002179
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002180 // Process the 'actions' list of this tool.
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002181 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002182 EmitCaseConstructHandler(D.Actions, Indent2,
2183 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002184 false, OptDescs, O);
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002185 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002186
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002187 // Input file (s)
2188 if (!D.InFileOption.empty()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002189 O.indent(Indent2)
2190 << "vec.push_back(std::make_pair(InputFilenames.getPosition(0), \""
2191 << D.InFileOption << "\");\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002192 }
2193
2194 if (IsJoin) {
2195 O.indent(Indent2)
2196 << "for (PathVector::const_iterator B = inFiles.begin(),\n";
2197 O.indent(Indent3) << "E = inFiles.end(); B != E; ++B)\n";
2198 O.indent(Indent2) << "{\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002199 O.indent(Indent3) << "vec.push_back(std::make_pair("
2200 << "InputFilenames.getPosition(B - inFiles.begin()), "
2201 << "B->str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002202 O.indent(Indent2) << "}\n";
2203 }
2204 else {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002205 O.indent(Indent2) << "vec.push_back(std::make_pair("
2206 << "InputFilenames.getPosition(0), inFile.str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002207 }
2208
2209 // Output file
2210 O.indent(Indent2) << "if (!no_out_file) {\n";
2211 if (!D.OutFileOption.empty())
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002212 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002213 << D.OutFileOption << "\"));\n";
2214
2215 O.indent(Indent3) << "out_file = this->OutFilename("
2216 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002217 O.indent(Indent4) <<
2218 "TempDir, stop_compilation, output_suffix.c_str()).str();\n\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002219 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, out_file));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002220
2221 O.indent(Indent2) << "}\n\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002222
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002223 // Handle the Sink property.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002224 std::string SinkOption("autogenerated::");
2225 SinkOption += SinkOptionName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002226 if (D.isSink()) {
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002227 O.indent(Indent2) << "if (!" << SinkOption << ".empty()) {\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002228 O.indent(Indent3) << "for (cl::list<std::string>::iterator B = "
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002229 << SinkOption << ".begin(), E = " << SinkOption
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002230 << ".end(); B != E; ++B)\n";
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002231 O.indent(Indent4) << "vec.push_back(std::make_pair(" << SinkOption
2232 << ".getPosition(B - " << SinkOption
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002233 << ".begin()), *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002234 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002235 }
2236
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002237 O.indent(Indent2) << "Out.Construct(cmd, this->SortArgs(vec), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002238 << "stop_compilation, out_file);\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002239 O.indent(Indent2) << "return 0;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002240 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002241}
2242
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002243/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2244/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002245void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2246 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002247 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002248 if (!ToolDesc.isJoin()) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002249 EmitGenerateActionMethodHeader(ToolDesc, /* IsJoin = */ true,
2250 /* Naked = */ true, O);
2251 O.indent(Indent2) << "PrintError(\"" << ToolDesc.Name
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002252 << " is not a Join tool!\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002253 O.indent(Indent2) << "return -1;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002254 O.indent(Indent1) << "}\n\n";
2255 }
2256 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002257 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002258 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002259
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002260 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002261}
2262
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002263/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2264/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002265void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002266 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2267 O.indent(Indent2) << "return InputLanguages_;\n";
2268 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002269
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002270 O.indent(Indent1) << "const char** OutputLanguages() const {\n";
2271 O.indent(Indent2) << "return OutputLanguages_;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002272 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002273}
2274
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002275/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002276void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002277 O.indent(Indent1) << "const char* Name() const {\n";
2278 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2279 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002280}
2281
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002282/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2283/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002284void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002285 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002286 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002287 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002288 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002289 O.indent(Indent2) << "return false;\n";
2290 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002291}
2292
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002293/// EmitWorksOnEmptyCallback - Callback used by EmitWorksOnEmptyMethod in
2294/// conjunction with EmitCaseConstructHandler.
2295void EmitWorksOnEmptyCallback (const Init* Value,
2296 unsigned IndentLevel, raw_ostream& O) {
2297 CheckBooleanConstant(Value);
2298 O.indent(IndentLevel) << "return " << Value->getAsString() << ";\n";
2299}
2300
2301/// EmitWorksOnEmptyMethod - Emit the WorksOnEmpty() method for a given Tool
2302/// class.
2303void EmitWorksOnEmptyMethod (const ToolDescription& D,
2304 const OptionDescriptions& OptDescs,
2305 raw_ostream& O)
2306{
2307 O.indent(Indent1) << "bool WorksOnEmpty() const {\n";
2308 if (D.OnEmpty == 0)
2309 O.indent(Indent2) << "return false;\n";
2310 else
2311 EmitCaseConstructHandler(D.OnEmpty, Indent2, EmitWorksOnEmptyCallback,
2312 /*EmitElseIf = */ true, OptDescs, O);
2313 O.indent(Indent1) << "}\n\n";
2314}
2315
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002316/// EmitStrArray - Emit definition of a 'const char**' static member
2317/// variable. Helper used by EmitStaticMemberDefinitions();
2318void EmitStrArray(const std::string& Name, const std::string& VarName,
2319 const StrVector& StrVec, raw_ostream& O) {
2320 O << "const char* " << Name << "::" << VarName << "[] = {";
2321 for (StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
2322 B != E; ++B)
2323 O << '\"' << *B << "\", ";
2324 O << "0};\n";
2325}
2326
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002327/// EmitStaticMemberDefinitions - Emit static member definitions for a
2328/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002329void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002330 if (D.InLanguage.empty())
2331 throw "Tool " + D.Name + " has no 'in_language' property!";
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002332 if (D.OutLanguage.empty())
2333 throw "Tool " + D.Name + " has no 'out_language' property!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002334
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002335 EmitStrArray(D.Name, "InputLanguages_", D.InLanguage, O);
2336 EmitStrArray(D.Name, "OutputLanguages_", D.OutLanguage, O);
2337 O << '\n';
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002338}
2339
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002340/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002341void EmitToolClassDefinition (const ToolDescription& D,
2342 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002343 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002344 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002345 return;
2346
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002347 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002348 O << "class " << D.Name << " : public ";
2349 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002350 O << "JoinTool";
2351 else
2352 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002353
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002354 O << " {\nprivate:\n";
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002355 O.indent(Indent1) << "static const char* InputLanguages_[];\n";
2356 O.indent(Indent1) << "static const char* OutputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002357
2358 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002359 EmitNameMethod(D, O);
2360 EmitInOutLanguageMethods(D, O);
2361 EmitIsJoinMethod(D, O);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002362 EmitWorksOnEmptyMethod(D, OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002363 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002364
2365 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002366 O << "};\n";
2367
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002368 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002369
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002370}
2371
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002372/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002373/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002374void EmitOptionDefinitions (const OptionDescriptions& descs,
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002375 bool HasSink, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002376{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002377 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002378
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002379 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002380 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002381 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002382 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002383
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002384 if (val.Type == OptionType::Alias) {
2385 Aliases.push_back(val);
2386 continue;
2387 }
2388
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002389 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002390 << val.GenPlainVariableName();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002391
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002392 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002393
2394 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2395 O << ", cl::Prefix";
2396
2397 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002398 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002399 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002400 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002401 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002402 }
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002403
2404 if (val.isOptional())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002405 O << ", cl::Optional";
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002406
2407 if (val.isOneOrMore())
2408 O << ", cl::OneOrMore";
2409
2410 if (val.isZeroOrMore())
2411 O << ", cl::ZeroOrMore";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002412
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002413 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002414 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002415 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002416 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002417
2418 if (val.isCommaSeparated())
2419 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002420
2421 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002422 O << ", cl::multi_val(" << val.MultiVal << ')';
2423
2424 if (val.InitVal) {
2425 const std::string& str = val.InitVal->getAsString();
2426 O << ", cl::init(" << str << ')';
2427 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002428
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002429 if (!val.Help.empty())
2430 O << ", cl::desc(\"" << val.Help << "\")";
2431
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002432 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002433 }
2434
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002435 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002436 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002437 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002438 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002439
2440 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov297514d2010-08-20 18:16:26 +00002441 << val.GenPlainVariableName()
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002442 << "(\"" << val.Name << '\"';
2443
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002444 const OptionDescription& D = descs.FindOption(val.Help);
2445 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002446
2447 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2448 }
2449
2450 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002451 if (HasSink)
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002452 O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002453
2454 O << '\n';
2455}
2456
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002457/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002458/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002459
2460class EmitPreprocessOptionsCallback;
2461
2462typedef void
2463(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2464(const DagInit&, unsigned, raw_ostream&) const;
2465
2466class EmitPreprocessOptionsCallback :
2467 public ActionHandlingCallbackBase,
2468 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2469{
2470 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002471 typedef void
2472 (EmitPreprocessOptionsCallback::* HandlerImpl)
2473 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002474
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002475 const OptionDescriptions& OptDescs_;
2476
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002477 void onListOrDag(const DagInit& d, HandlerImpl h,
2478 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002479 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002480 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002481 const Init* I = d.getArg(0);
2482
2483 // If I is a list, apply h to each element.
2484 if (typeid(*I) == typeid(ListInit)) {
2485 const ListInit& L = *static_cast<const ListInit*>(I);
2486 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B)
2487 ((this)->*(h))(*B, IndentLevel, O);
2488 }
2489 // Otherwise, apply h to I.
2490 else {
2491 ((this)->*(h))(I, IndentLevel, O);
2492 }
2493 }
2494
2495 void onUnsetOptionImpl(const Init* I,
2496 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002497 {
2498 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002499 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002500
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002501 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002502 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2503 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002504 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002505 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2506 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002507 else if (OptDesc.isList()) {
2508 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2509 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002510 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002511 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002512 }
2513 }
2514
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002515 void onUnsetOption(const DagInit& d,
2516 unsigned IndentLevel, raw_ostream& O) const
2517 {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002518 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2519 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002520 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002521
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002522 void onSetOptionImpl(const DagInit& d,
2523 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002524 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002525 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002526 const Init* Value = d.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002527 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2528
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002529 if (OptDesc.isList()) {
2530 const ListInit& List = InitPtrToList(Value);
2531
2532 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2533 for (ListInit::const_iterator B = List.begin(), E = List.end();
2534 B != E; ++B) {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002535 const Init* CurElem = *B;
2536 if (OptDesc.isSwitchList())
2537 CheckBooleanConstant(CurElem);
2538
2539 O.indent(IndentLevel)
2540 << OptDesc.GenVariableName() << ".push_back(\""
2541 << (OptDesc.isSwitchList() ? CurElem->getAsString()
2542 : InitPtrToString(CurElem))
2543 << "\");\n";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002544 }
2545 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002546 else if (OptDesc.isSwitch()) {
2547 CheckBooleanConstant(Value);
2548 O.indent(IndentLevel) << OptDesc.GenVariableName()
2549 << " = " << Value->getAsString() << ";\n";
2550 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002551 else if (OptDesc.isParameter()) {
2552 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002553 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002554 << " = \"" << Str << "\";\n";
2555 }
2556 else {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002557 throw "Can't apply 'set_option' to alias option -" + OptName + " !";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002558 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002559 }
2560
2561 void onSetSwitch(const Init* I,
2562 unsigned IndentLevel, raw_ostream& O) const {
2563 const std::string& OptName = InitPtrToString(I);
2564 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2565
2566 if (OptDesc.isSwitch())
2567 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2568 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002569 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002570 }
2571
2572 void onSetOption(const DagInit& d,
2573 unsigned IndentLevel, raw_ostream& O) const
2574 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002575 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002576
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002577 // Two arguments: (set_option "parameter", VALUE), where VALUE can be a
2578 // boolean, a string or a string list.
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002579 if (d.getNumArgs() > 1)
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002580 this->onSetOptionImpl(d, IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002581 // One argument: (set_option "switch")
2582 // or (set_option ["switch1", "switch2", ...])
2583 else
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002584 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2585 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002586 }
2587
2588public:
2589
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002590 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002591 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002592 {
2593 if (!staticMembersInitialized_) {
2594 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2595 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2596 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002597 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002598
2599 staticMembersInitialized_ = true;
2600 }
2601 }
2602
2603 void operator()(const Init* I,
2604 unsigned IndentLevel, raw_ostream& O) const
2605 {
2606 InvokeDagInitHandler(this, I, IndentLevel, O);
2607 }
2608
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002609};
2610
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002611/// EmitPreprocessOptions - Emit the PreprocessOptions() function.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002612void EmitPreprocessOptions (const RecordKeeper& Records,
2613 const OptionDescriptions& OptDecs, raw_ostream& O)
2614{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002615 O << "int PreprocessOptions () {\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002616
2617 const RecordVector& OptionPreprocessors =
2618 Records.getAllDerivedDefinitions("OptionPreprocessor");
2619
2620 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2621 E = OptionPreprocessors.end(); B!=E; ++B) {
2622 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002623 EmitCaseConstructHandler(Case, Indent1,
2624 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002625 false, OptDecs, O);
2626 }
2627
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002628 O << '\n';
2629 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002630 O << "}\n\n";
2631}
2632
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002633class DoEmitPopulateLanguageMap;
2634typedef void (DoEmitPopulateLanguageMap::* DoEmitPopulateLanguageMapHandler)
2635(const DagInit& D);
2636
2637class DoEmitPopulateLanguageMap
2638: public HandlerTable<DoEmitPopulateLanguageMapHandler>
2639{
2640private:
2641 raw_ostream& O_;
2642
2643public:
2644
2645 explicit DoEmitPopulateLanguageMap (raw_ostream& O) : O_(O) {
2646 if (!staticMembersInitialized_) {
2647 AddHandler("lang_to_suffixes",
2648 &DoEmitPopulateLanguageMap::onLangToSuffixes);
2649
2650 staticMembersInitialized_ = true;
2651 }
2652 }
2653
2654 void operator() (Init* I) {
2655 InvokeDagInitHandler(this, I);
2656 }
2657
2658private:
2659
2660 void onLangToSuffixes (const DagInit& d) {
2661 CheckNumberOfArguments(d, 2);
2662
2663 const std::string& Lang = InitPtrToString(d.getArg(0));
2664 Init* Suffixes = d.getArg(1);
2665
2666 // Second argument to lang_to_suffixes is either a single string...
2667 if (typeid(*Suffixes) == typeid(StringInit)) {
2668 O_.indent(Indent1) << "langMap[\"" << InitPtrToString(Suffixes)
2669 << "\"] = \"" << Lang << "\";\n";
2670 }
2671 // ...or a list of strings.
2672 else {
2673 const ListInit& Lst = InitPtrToList(Suffixes);
2674 assert(Lst.size() != 0);
2675 for (ListInit::const_iterator B = Lst.begin(), E = Lst.end();
2676 B != E; ++B) {
2677 O_.indent(Indent1) << "langMap[\"" << InitPtrToString(*B)
2678 << "\"] = \"" << Lang << "\";\n";
2679 }
2680 }
2681 }
2682
2683};
2684
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002685/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002686void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002687{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002688 O << "int PopulateLanguageMap (LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002689
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002690 // For each LangMap:
2691 const RecordVector& LangMaps =
Mikhail Glushenkov00a5b5b2010-08-23 19:24:16 +00002692 Records.getAllDerivedDefinitions("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002693
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002694 for (RecordVector::const_iterator B = LangMaps.begin(),
2695 E = LangMaps.end(); B!=E; ++B) {
2696 ListInit* LangMap = (*B)->getValueAsListInit("map");
2697 std::for_each(LangMap->begin(), LangMap->end(),
2698 DoEmitPopulateLanguageMap(O));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002699 }
2700
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002701 O << '\n';
2702 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002703 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002704}
2705
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002706/// EmitEdgePropertyHandlerCallback - Emits code that handles edge
2707/// properties. Helper function passed to EmitCaseConstructHandler() by
2708/// EmitEdgeClass().
2709void EmitEdgePropertyHandlerCallback (const Init* i, unsigned IndentLevel,
2710 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002711 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002712 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002713
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002714 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002715 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002716 }
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002717 else if (OpName == "error") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002718 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002719 O.indent(IndentLevel) << "PrintError(\""
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002720 << InitPtrToString(d.getArg(0))
2721 << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002722 O.indent(IndentLevel) << "return -1;\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002723 return;
2724 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002725 else {
2726 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002727 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002728 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002729
2730 if (d.getNumArgs() > 0)
2731 O << InitPtrToInt(d.getArg(0)) << ";\n";
2732 else
2733 O << "2;\n";
2734
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002735}
2736
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002737/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002738void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002739 const DagInit& Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002740 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002741
2742 // Class constructor.
2743 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002744 << "public:\n";
2745 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2746 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002747
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002748 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002749 O.indent(Indent1)
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002750 << "int Weight(const InputLanguagesSet& InLangs) const {\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002751 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002752
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002753 // Handle the 'case' construct.
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002754 EmitCaseConstructHandler(&Case, Indent2, EmitEdgePropertyHandlerCallback,
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002755 false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002756
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002757 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002758 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002759}
2760
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002761/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002762void EmitEdgeClasses (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002763 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002764 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002765 int i = 0;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002766 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002767 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002768 const DagInit& Edge = **B;
2769 const std::string& Name = GetOperatorName(Edge);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002770
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002771 if (Name == "optional_edge") {
2772 assert(IsOptionalEdge(Edge));
2773 const std::string& NodeB = InitPtrToString(Edge.getArg(1));
2774
2775 const DagInit& Weight = InitPtrToDag(Edge.getArg(2));
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002776 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
2777 }
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002778 else if (Name != "edge") {
2779 throw "Unknown edge class: '" + Name + "'!";
2780 }
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002781
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002782 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002783 }
2784}
2785
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002786/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph() function.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002787void EmitPopulateCompilationGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002788 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002789 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002790{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002791 O << "int PopulateCompilationGraph (CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002792
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002793 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2794 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002795 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002796
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002797 O << '\n';
2798
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002799 // Insert edges.
2800
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002801 int i = 0;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002802 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002803 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002804 const DagInit& Edge = **B;
2805 const std::string& NodeA = InitPtrToString(Edge.getArg(0));
2806 const std::string& NodeB = InitPtrToString(Edge.getArg(1));
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002807
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002808 O.indent(Indent1) << "if (int ret = G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002809
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002810 if (IsOptionalEdge(Edge))
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002811 O << "new Edge" << i << "()";
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002812 else
2813 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002814
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002815 O << "))\n";
2816 O.indent(Indent2) << "return ret;\n";
2817
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002818 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002819 }
2820
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002821 O << '\n';
2822 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002823 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002824}
2825
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002826/// HookInfo - Information about the hook type and number of arguments.
2827struct HookInfo {
2828
2829 // A hook can either have a single parameter of type std::vector<std::string>,
2830 // or NumArgs parameters of type const char*.
2831 enum HookType { ListHook, ArgHook };
2832
2833 HookType Type;
2834 unsigned NumArgs;
2835
2836 HookInfo() : Type(ArgHook), NumArgs(1)
2837 {}
2838
2839 HookInfo(HookType T) : Type(T), NumArgs(1)
2840 {}
2841
2842 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2843 {}
2844};
2845
2846typedef llvm::StringMap<HookInfo> HookInfoMap;
2847
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002848/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002849/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002850/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002851class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002852 HookInfoMap& HookNames_;
2853 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002854public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002855 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2856 : HookNames_(HookNames), OptDescs_(OptDescs)
2857 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002858
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002859 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002860 const std::string& Name = GetOperatorName(Dag);
2861
2862 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002863 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002864 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2865 const std::string& HookName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002866 const OptionDescription& D =
2867 OptDescs_.FindParameterListOrParameter(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002868
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002869 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2870 : HookInfo::ArgHook);
2871 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002872 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002873 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002874 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2875 }
2876 }
2877
2878 void onCmdLine(const std::string& Cmd) {
2879 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002880 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002881
2882 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2883 B != E; ++B) {
2884 const std::string& cmd = *B;
2885
2886 if (cmd == "$CALL") {
2887 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002888 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002889 const std::string& HookName = *B;
2890
2891 if (HookName.at(0) == ')')
2892 throw "$CALL invoked with no arguments!";
2893
2894 while (++B != E && B->at(0) != ')') {
2895 ++NumArgs;
2896 }
2897
2898 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2899
2900 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2901 H->second.Type != HookInfo::ArgHook)
2902 throw "Overloading of hooks is not allowed. Overloaded hook: "
2903 + HookName;
2904 else
2905 HookNames_[HookName] = HookInfo(NumArgs);
2906 }
2907 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002908 }
2909
2910 void operator()(const Init* Arg) {
2911
2912 // We're invoked on an action (either a dag or a dag list).
2913 if (typeid(*Arg) == typeid(DagInit)) {
2914 const DagInit& Dag = InitPtrToDag(Arg);
2915 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002916 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002917 }
2918 else if (typeid(*Arg) == typeid(ListInit)) {
2919 const ListInit& List = InitPtrToList(Arg);
2920 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2921 ++B) {
2922 const DagInit& Dag = InitPtrToDag(*B);
2923 this->onAction(Dag);
2924 }
2925 return;
2926 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002927
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002928 // We're invoked on a command line.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002929 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002930 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002931
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002932 void operator()(const Init* Statement, unsigned) {
2933 this->operator()(Statement);
2934 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002935};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002936
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002937/// FillInHookNames - Actually extract the hook names from all command
2938/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002939void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002940 const OptionDescriptions& OptDescs,
2941 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002942{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002943 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002944 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2945 E = ToolDescs.end(); B != E; ++B) {
2946 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002947
2948 // Look for 'forward_transformed_value' in 'actions'.
2949 if (D.Actions)
2950 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2951
2952 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002953 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002954 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002955 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002956 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002957 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002958 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002959 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002960 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002961 }
2962}
2963
2964/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2965/// property records and emit hook function declaration for each
2966/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002967void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2968 const OptionDescriptions& OptDescs, raw_ostream& O) {
2969 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002970
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002971 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002972 if (HookNames.empty())
2973 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002974
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002975 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002976 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002977 const char* HookName = B->first();
2978 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002979
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002980 O.indent(Indent1) << "std::string " << HookName << "(";
2981
2982 if (Info.Type == HookInfo::ArgHook) {
2983 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2984 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2985 }
2986 }
2987 else {
2988 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002989 }
2990
2991 O <<");\n";
2992 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002993}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002994
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002995/// EmitIncludes - Emit necessary #include directives and some
2996/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002997void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002998 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2999 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00003000 << "#include \"llvm/CompilerDriver/Error.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00003001 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00003002
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00003003 << "#include \"llvm/Support/CommandLine.h\"\n"
3004 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00003005
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003006 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00003007 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003008 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00003009 << "#include <stdexcept>\n\n"
3010
3011 << "using namespace llvm;\n"
3012 << "using namespace llvmc;\n\n"
3013
Mikhail Glushenkov67665722008-11-12 12:41:18 +00003014 << "inline const char* checkCString(const char* s)\n"
3015 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00003016}
3017
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003018
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003019/// DriverData - Holds all information about the driver.
3020struct DriverData {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003021 OptionDescriptions OptDescs;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003022 ToolDescriptions ToolDescs;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003023 DagVector Edges;
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003024 bool HasSink;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003025};
3026
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003027/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003028/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003029bool HasSink(const ToolDescriptions& ToolDescs) {
3030 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
3031 E = ToolDescs.end(); B != E; ++B)
3032 if ((*B)->isSink())
3033 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003034
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003035 return false;
3036}
3037
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003038/// CollectDriverData - Collect compilation graph edges, tool properties and
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003039/// option properties from the parse tree.
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003040void CollectDriverData (const RecordKeeper& Records, DriverData& Data) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003041 // Collect option properties.
3042 const RecordVector& OptionLists =
3043 Records.getAllDerivedDefinitions("OptionList");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003044 CollectOptionDescriptions(OptionLists, Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003045
3046 // Collect tool properties.
3047 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003048 CollectToolDescriptions(Tools, Data.ToolDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003049 Data.HasSink = HasSink(Data.ToolDescs);
3050
3051 // Collect compilation graph edges.
3052 const RecordVector& CompilationGraphs =
3053 Records.getAllDerivedDefinitions("CompilationGraph");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003054 FillInEdgeVector(CompilationGraphs, Data.Edges);
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00003055}
3056
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003057/// CheckDriverData - Perform some sanity checks on the collected data.
3058void CheckDriverData(DriverData& Data) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003059 // Filter out all tools not mentioned in the compilation graph.
3060 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003061
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003062 // Typecheck the compilation graph.
3063 TypecheckGraph(Data.Edges, Data.ToolDescs);
3064
3065 // Check that there are no options without side effects (specified
3066 // only in the OptionList).
3067 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003068}
3069
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003070void EmitDriverCode(const DriverData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003071 // Emit file header.
3072 EmitIncludes(O);
3073
3074 // Emit global option registration code.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003075 O << "namespace llvmc {\n"
3076 << "namespace autogenerated {\n\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003077 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003078 O << "} // End namespace autogenerated.\n"
3079 << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003080
3081 // Emit hook declarations.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003082 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003083 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003084 O << "} // End namespace hooks.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003085
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003086 O << "namespace {\n\n";
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003087 O << "using namespace llvmc::autogenerated;\n\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003088
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003089 // Emit Tool classes.
3090 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
3091 E = Data.ToolDescs.end(); B!=E; ++B)
3092 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
3093
3094 // Emit Edge# classes.
3095 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
3096
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003097 O << "} // End anonymous namespace.\n\n";
3098
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003099 O << "namespace llvmc {\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003100 O << "namespace autogenerated {\n\n";
3101
3102 // Emit PreprocessOptions() function.
3103 EmitPreprocessOptions(Records, Data.OptDescs, O);
3104
3105 // Emit PopulateLanguageMap() function
3106 // (language map maps from file extensions to language names).
3107 EmitPopulateLanguageMap(Records, O);
3108
3109 // Emit PopulateCompilationGraph() function.
3110 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
3111
3112 O << "} // End namespace autogenerated.\n";
3113 O << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003114
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003115 // EOF
3116}
3117
3118
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003119// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00003120}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003121
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003122/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00003123void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003124 try {
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003125 DriverData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003126
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003127 CollectDriverData(Records, Data);
3128 CheckDriverData(Data);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003129
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003130 this->EmitSourceFileHeader("llvmc-based driver: auto-generated code", O);
3131 EmitDriverCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003132
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003133 } catch (std::exception& Error) {
3134 throw Error.what() + std::string(" - usually this means a syntax error.");
3135 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003136}