blob: cdeeea358977269a333181574b8e3873ff3d8d45 [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;
471 const OptionDescription& FindList(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000472 const OptionDescription& FindParameterList(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000473 const OptionDescription&
474 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000475 const OptionDescription&
476 FindParameterListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000477
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000478 /// insertDescription - Insert new OptionDescription into
479 /// OptionDescriptions list
480 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000481
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000482 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000483 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000484 const_iterator begin() const { return Descriptions.begin(); }
485 const_iterator end() const { return Descriptions.end(); }
486};
487
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000488const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000489OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000490 const_iterator I = Descriptions.find(OptName);
491 if (I != Descriptions.end())
492 return I->second;
493 else
494 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000495}
496
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000497const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000498OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000499 const OptionDescription& OptDesc = this->FindOption(OptName);
500 if (!OptDesc.isSwitch())
501 throw OptName + ": incorrect option type - should be a switch!";
502 return OptDesc;
503}
504
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000505const OptionDescription&
506OptionDescriptions::FindList(const std::string& OptName) const {
507 const OptionDescription& OptDesc = this->FindOption(OptName);
508 if (!OptDesc.isList())
509 throw OptName + ": incorrect option type - should be a list!";
510 return OptDesc;
511}
512
513const OptionDescription&
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000514OptionDescriptions::FindParameterList(const std::string& OptName) const {
515 const OptionDescription& OptDesc = this->FindOption(OptName);
516 if (!OptDesc.isList() || OptDesc.isSwitchList())
517 throw OptName + ": incorrect option type - should be a parameter list!";
518 return OptDesc;
519}
520
521const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000522OptionDescriptions::FindParameter(const std::string& OptName) const {
523 const OptionDescription& OptDesc = this->FindOption(OptName);
524 if (!OptDesc.isParameter())
525 throw OptName + ": incorrect option type - should be a parameter!";
526 return OptDesc;
527}
528
529const OptionDescription&
530OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
531 const OptionDescription& OptDesc = this->FindOption(OptName);
532 if (!OptDesc.isList() && !OptDesc.isParameter())
533 throw OptName
534 + ": incorrect option type - should be a list or parameter!";
535 return OptDesc;
536}
537
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000538const OptionDescription&
539OptionDescriptions::FindParameterListOrParameter
540(const std::string& OptName) const {
541 const OptionDescription& OptDesc = this->FindOption(OptName);
542 if ((!OptDesc.isList() && !OptDesc.isParameter()) || OptDesc.isSwitchList())
543 throw OptName
544 + ": incorrect option type - should be a parameter list or parameter!";
545 return OptDesc;
546}
547
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000548void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000549 container_type::iterator I = Descriptions.find(o.Name);
550 if (I != Descriptions.end()) {
551 OptionDescription& D = I->second;
552 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000553 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000554 else {
555 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000556 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000557}
558
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000559/// HandlerTable - A base class for function objects implemented as
560/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000561template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000562class HandlerTable {
563protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000564 // Implementation details.
565
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000566 /// HandlerMap - A map from property names to property handlers
567 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000568
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000569 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000570 static bool staticMembersInitialized_;
571
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000572public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000573
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000574 Handler GetHandler (const std::string& HandlerName) const {
575 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000576
577 if (method != Handlers_.end()) {
578 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000579 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000580 }
581 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000582 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000583 }
584 }
585
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000586 void AddHandler(const char* Property, Handler H) {
587 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000588 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000589
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000590};
591
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000592template <class Handler, class FunctionObject>
593Handler GetHandler(FunctionObject* Obj, const DagInit& Dag) {
594 const std::string& HandlerName = GetOperatorName(Dag);
595 return Obj->GetHandler(HandlerName);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000596}
597
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000598template <class FunctionObject>
599void InvokeDagInitHandler(FunctionObject* Obj, Init* I) {
600 typedef void (FunctionObject::*Handler) (const DagInit&);
601
602 const DagInit& Dag = InitPtrToDag(I);
603 Handler h = GetHandler<Handler>(Obj, Dag);
604
605 ((Obj)->*(h))(Dag);
606}
607
608template <class FunctionObject>
609void InvokeDagInitHandler(const FunctionObject* const Obj,
610 const Init* I, unsigned IndentLevel, raw_ostream& O)
611{
612 typedef void (FunctionObject::*Handler)
613 (const DagInit&, unsigned IndentLevel, raw_ostream& O) const;
614
615 const DagInit& Dag = InitPtrToDag(I);
616 Handler h = GetHandler<Handler>(Obj, Dag);
617
618 ((Obj)->*(h))(Dag, IndentLevel, O);
619}
620
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000621template <typename H>
622typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
623
624template <typename H>
625bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000626
627
628/// CollectOptionProperties - Function object for iterating over an
629/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000630class CollectOptionProperties;
631typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000632(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000633
634class CollectOptionProperties
635: public HandlerTable<CollectOptionPropertiesHandler>
636{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000637private:
638
639 /// optDescs_ - OptionDescriptions table. This is where the
640 /// information is stored.
641 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000642
643public:
644
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000645 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000646 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000647 {
648 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000649 AddHandler("help", &CollectOptionProperties::onHelp);
650 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000651 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000652 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
653 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000654 AddHandler("zero_or_more", &CollectOptionProperties::onZeroOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000655 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
656 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000657 AddHandler("optional", &CollectOptionProperties::onOptional);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000658 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000659 AddHandler("forward_not_split",
660 &CollectOptionProperties::onForwardNotSplit);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000661
662 staticMembersInitialized_ = true;
663 }
664 }
665
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000666 /// operator() - Just forwards to the corresponding property
667 /// handler.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000668 void operator() (Init* I) {
669 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000670 }
671
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000672private:
673
674 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000675 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000676
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000677 void onHelp (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000678 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000679 optDesc_.Help = EscapeQuotes(InitPtrToString(d.getArg(0)));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000680 }
681
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000682 void onHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000683 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000684 optDesc_.setHidden();
685 }
686
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000687 void onReallyHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000688 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000689 optDesc_.setReallyHidden();
690 }
691
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000692 void onCommaSeparated (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000693 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000694 if (!optDesc_.isParameterList())
695 throw "'comma_separated' is valid only on parameter list options!";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000696 optDesc_.setCommaSeparated();
697 }
698
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000699 void onForwardNotSplit (const DagInit& d) {
700 CheckNumberOfArguments(d, 0);
701 if (!optDesc_.isParameter())
702 throw "'forward_not_split' is valid only for parameter options!";
703 optDesc_.setForwardNotSplit();
704 }
705
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000706 void onRequired (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000707 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000708
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000709 optDesc_.setRequired();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000710 optDesc_.CheckConsistency();
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000711 }
712
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000713 void onInit (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000714 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000715 Init* i = d.getArg(0);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000716 const std::string& str = i->getAsString();
717
718 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
719 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
720
721 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000722 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000723
724 optDesc_.InitVal = i;
725 }
726
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000727 void onOneOrMore (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000728 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000729
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000730 optDesc_.setOneOrMore();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000731 optDesc_.CheckConsistency();
732 }
733
734 void onZeroOrMore (const DagInit& d) {
735 CheckNumberOfArguments(d, 0);
736
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000737 if (optDesc_.isList())
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000738 llvm::errs() << "Warning: specifying the 'zero_or_more' property "
739 "on a list option has no effect.\n";
740
741 optDesc_.setZeroOrMore();
742 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000743 }
744
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000745 void onOptional (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000746 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000747
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000748 if (!optDesc_.isList())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000749 llvm::errs() << "Warning: specifying the 'optional' property"
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000750 "on a non-list option has no effect.\n";
751
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000752 optDesc_.setOptional();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000753 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000754 }
755
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000756 void onMultiVal (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000757 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000758 int val = InitPtrToInt(d.getArg(0));
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000759 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000760 throw "Error in the 'multi_val' property: "
761 "the value must be greater than 1!";
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000762 if (!optDesc_.isParameterList())
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000763 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000764 optDesc_.MultiVal = val;
765 }
766
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000767};
768
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000769/// AddOption - A function object that is applied to every option
770/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000771class AddOption {
772private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000773 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000774
775public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000776 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000777 {}
778
779 void operator()(const Init* i) {
780 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000781 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000782
783 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000784 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000785 const std::string& Name = InitPtrToString(d.getArg(0));
786
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000787 OptionDescription OD(Type, Name);
788
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000789 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000790
791 if (OD.isAlias()) {
792 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000793 OD.Help = InitPtrToString(d.getArg(1));
794 }
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000795 else {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000796 processOptionProperties(d, OD);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000797 }
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000798
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000799 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000800 }
801
802private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000803 /// processOptionProperties - Go through the list of option
804 /// properties and call a corresponding handler for each.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000805 static void processOptionProperties (const DagInit& d, OptionDescription& o) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000806 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000807 DagInit::const_arg_iterator B = d.arg_begin();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000808 // Skip the first argument: it's always the option name.
809 ++B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000810 std::for_each(B, d.arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000811 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000812
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000813};
814
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000815/// CollectOptionDescriptions - Collects option properties from all
816/// OptionLists.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000817void CollectOptionDescriptions (const RecordVector& V,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000818 OptionDescriptions& OptDescs)
819{
820 // For every OptionList:
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000821 for (RecordVector::const_iterator B = V.begin(),
822 E = V.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000823 // Throws an exception if the value does not exist.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000824 ListInit* PropList = (*B)->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000825
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000826 // For every option description in this list:
827 // collect the information and
828 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
829 }
830}
831
832// Tool information record
833
834namespace ToolFlags {
835 enum ToolFlags { Join = 0x1, Sink = 0x2 };
836}
837
838struct ToolDescription : public RefCountedBase<ToolDescription> {
839 std::string Name;
840 Init* CmdLine;
841 Init* Actions;
842 StrVector InLanguage;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000843 std::string InFileOption;
844 std::string OutFileOption;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000845 std::string OutLanguage;
846 std::string OutputSuffix;
847 unsigned Flags;
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000848 const Init* OnEmpty;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000849
850 // Various boolean properties
851 void setSink() { Flags |= ToolFlags::Sink; }
852 bool isSink() const { return Flags & ToolFlags::Sink; }
853 void setJoin() { Flags |= ToolFlags::Join; }
854 bool isJoin() const { return Flags & ToolFlags::Join; }
855
856 // Default ctor here is needed because StringMap can only store
857 // DefaultConstructible objects
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000858 ToolDescription ()
Mikhail Glushenkovc4301292010-02-23 09:05:01 +0000859 : CmdLine(0), Actions(0), OutFileOption("-o"),
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000860 Flags(0), OnEmpty(0)
861 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000862 ToolDescription (const std::string& n)
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000863 : Name(n), CmdLine(0), Actions(0), OutFileOption("-o"),
864 Flags(0), OnEmpty(0)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000865 {}
866};
867
868/// ToolDescriptions - A list of Tool information records.
869typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
870
871
872/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000873/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000874
875class CollectToolProperties;
876typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000877(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000878
879class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
880{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000881private:
882
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000883 /// toolDesc_ - Properties of the current Tool. This is where the
884 /// information is stored.
885 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000886
887public:
888
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000889 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000890 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000891 {
892 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000893
894 AddHandler("actions", &CollectToolProperties::onActions);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000895 AddHandler("command", &CollectToolProperties::onCommand);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000896 AddHandler("in_language", &CollectToolProperties::onInLanguage);
897 AddHandler("join", &CollectToolProperties::onJoin);
898 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000899
900 AddHandler("out_file_option", &CollectToolProperties::onOutFileOption);
901 AddHandler("in_file_option", &CollectToolProperties::onInFileOption);
902
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000903 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
904 AddHandler("sink", &CollectToolProperties::onSink);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000905 AddHandler("works_on_empty", &CollectToolProperties::onWorksOnEmpty);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000906
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000907 staticMembersInitialized_ = true;
908 }
909 }
910
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000911 void operator() (Init* I) {
912 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000913 }
914
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000915private:
916
917 /// Property handlers --
918 /// Functions that extract information about tool properties from
919 /// DAG representation.
920
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000921 void onActions (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000922 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000923 Init* Case = d.getArg(0);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000924 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000925 GetOperatorName(static_cast<DagInit&>(*Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000926 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000927 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000928 }
929
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000930 void onCommand (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000931 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000932 toolDesc_.CmdLine = d.getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000933 }
934
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000935 void onInLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000936 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000937 Init* arg = d.getArg(0);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000938
939 // Find out the argument's type.
940 if (typeid(*arg) == typeid(StringInit)) {
941 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000942 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000943 }
944 else {
945 // It's a list.
946 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000947 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000948
949 // Copy strings to the output vector.
950 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
951 B != E; ++B) {
952 out.push_back(InitPtrToString(*B));
953 }
954
955 // Remove duplicates.
956 std::sort(out.begin(), out.end());
957 StrVector::iterator newE = std::unique(out.begin(), out.end());
958 out.erase(newE, out.end());
959 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000960 }
961
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000962 void onJoin (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000963 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000964 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000965 }
966
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000967 void onOutLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000968 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000969 toolDesc_.OutLanguage = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000970 }
971
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000972 void onOutFileOption (const DagInit& d) {
973 CheckNumberOfArguments(d, 1);
974 toolDesc_.OutFileOption = InitPtrToString(d.getArg(0));
975 }
976
977 void onInFileOption (const DagInit& d) {
978 CheckNumberOfArguments(d, 1);
979 toolDesc_.InFileOption = InitPtrToString(d.getArg(0));
980 }
981
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000982 void onOutputSuffix (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000983 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000984 toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000985 }
986
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000987 void onSink (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000988 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000989 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000990 }
991
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000992 void onWorksOnEmpty (const DagInit& d) {
993 toolDesc_.OnEmpty = d.getArg(0);
994 }
995
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000996};
997
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000998/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000999/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001000/// CollectToolProperties function object).
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001001void CollectToolDescriptions (const RecordVector& Tools,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001002 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001003{
1004 // Iterate over a properties list of every Tool definition
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001005 for (RecordVector::const_iterator B = Tools.begin(),
1006 E = Tools.end(); B!=E; ++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00001007 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001008 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001009 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001010
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001011 IntrusiveRefCntPtr<ToolDescription>
1012 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001013
1014 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001015 CollectToolProperties(*ToolDesc));
1016 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001017 }
1018}
1019
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001020/// FillInEdgeVector - Merge all compilation graph definitions into
1021/// one single edge list.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001022void FillInEdgeVector(const RecordVector& CompilationGraphs,
1023 DagVector& Out) {
1024 for (RecordVector::const_iterator B = CompilationGraphs.begin(),
1025 E = CompilationGraphs.end(); B != E; ++B) {
1026 const ListInit* Edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +00001027
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001028 for (ListInit::const_iterator B = Edges->begin(),
1029 E = Edges->end(); B != E; ++B) {
1030 Out.push_back(&InitPtrToDag(*B));
1031 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001032 }
1033}
1034
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001035/// NotInGraph - Helper function object for FilterNotInGraph.
1036struct NotInGraph {
1037private:
1038 const llvm::StringSet<>& ToolsInGraph_;
1039
1040public:
1041 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
1042 : ToolsInGraph_(ToolsInGraph)
1043 {}
1044
1045 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
1046 return (ToolsInGraph_.count(x->Name) == 0);
1047 }
1048};
1049
1050/// FilterNotInGraph - Filter out from ToolDescs all Tools not
1051/// mentioned in the compilation graph definition.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001052void FilterNotInGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001053 ToolDescriptions& ToolDescs) {
1054
1055 // List all tools mentioned in the graph.
1056 llvm::StringSet<> ToolsInGraph;
1057
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001058 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001059 E = EdgeVector.end(); B != E; ++B) {
1060
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001061 const DagInit* Edge = *B;
1062 const std::string& NodeA = InitPtrToString(Edge->getArg(0));
1063 const std::string& NodeB = InitPtrToString(Edge->getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001064
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001065 if (NodeA != "root")
1066 ToolsInGraph.insert(NodeA);
1067 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001068 }
1069
1070 // Filter ToolPropertiesList.
1071 ToolDescriptions::iterator new_end =
1072 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
1073 NotInGraph(ToolsInGraph));
1074 ToolDescs.erase(new_end, ToolDescs.end());
1075}
1076
1077/// FillInToolToLang - Fills in two tables that map tool names to
1078/// (input, output) languages. Helper function used by TypecheckGraph().
1079void FillInToolToLang (const ToolDescriptions& ToolDescs,
1080 StringMap<StringSet<> >& ToolToInLang,
1081 StringMap<std::string>& ToolToOutLang) {
1082 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1083 E = ToolDescs.end(); B != E; ++B) {
1084 const ToolDescription& D = *(*B);
1085 for (StrVector::const_iterator B = D.InLanguage.begin(),
1086 E = D.InLanguage.end(); B != E; ++B)
1087 ToolToInLang[D.Name].insert(*B);
1088 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001089 }
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;
1097 StringMap<std::string> ToolToOutLang;
1098
1099 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
1100 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
1101 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
1102
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001103 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001104 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001105 const DagInit* Edge = *B;
1106 const std::string& NodeA = InitPtrToString(Edge->getArg(0));
1107 const std::string& NodeB = InitPtrToString(Edge->getArg(1));
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001108 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
1109 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001110
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001111 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001112 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001113 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001114 + ": output->input language mismatch";
1115 }
1116
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001117 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001118 throw "Edges back to the root are not allowed!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001119 }
1120}
1121
1122/// WalkCase - Walks the 'case' expression DAG and invokes
1123/// TestCallback on every test, and StatementCallback on every
1124/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001125/// combinators (that is, they are passed directly to TestCallback).
1126/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1127/// IndentLevel, bool FirstTest)'.
1128/// StatementCallback must have type 'void StatementCallback(const Init*,
1129/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001130template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001131void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1132 unsigned IndentLevel = 0)
1133{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001134 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001135
1136 // Error checks.
1137 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001138 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001139
1140 if (d.getNumArgs() < 2)
1141 throw "There should be at least one clause in the 'case' expression:\n"
1142 + d.getAsString();
1143
1144 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001145 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001146 const unsigned numArgs = d.getNumArgs();
1147 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001148 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1149 B != E; ++B) {
1150 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001151
1152 if (!even)
1153 {
1154 // Handle test.
1155 const DagInit& Test = InitPtrToDag(arg);
1156
1157 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001158 throw "The 'default' clause should be the last in the "
1159 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001160 if (i == numArgs)
1161 throw "Case construct handler: no corresponding action "
1162 "found for the test " + Test.getAsString() + '!';
1163
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001164 TestCallback(Test, IndentLevel, (i == 1));
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001165 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001166 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001167 {
1168 if (dynamic_cast<DagInit*>(arg)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001169 && GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001170 // Nested 'case'.
1171 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1172 }
1173
1174 // Handle statement.
1175 StatementCallback(arg, IndentLevel);
1176 }
1177
1178 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001179 even = !even;
1180 }
1181}
1182
1183/// ExtractOptionNames - A helper function object used by
1184/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1185class ExtractOptionNames {
1186 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001187
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001188 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001189 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001190 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001191 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001192 ActionName == "forward_value" ||
1193 ActionName == "forward_transformed_value" ||
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001194 ActionName == "switch_on" || ActionName == "any_switch_on" ||
1195 ActionName == "parameter_equals" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001196 ActionName == "element_in_list" || ActionName == "not_empty" ||
1197 ActionName == "empty") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001198 CheckNumberOfArguments(Stmt, 1);
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001199
1200 Init* Arg = Stmt.getArg(0);
1201 if (typeid(*Arg) == typeid(StringInit)) {
1202 const std::string& Name = InitPtrToString(Arg);
1203 OptionNames_.insert(Name);
1204 }
1205 else {
1206 // It's a list.
1207 const ListInit& List = InitPtrToList(Arg);
1208 for (ListInit::const_iterator B = List.begin(), E = List.end();
1209 B != E; ++B) {
1210 const std::string& Name = InitPtrToString(*B);
1211 OptionNames_.insert(Name);
1212 }
1213 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001214 }
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001215 else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001216 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001217 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001218 }
1219 }
1220 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001221
1222public:
1223 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1224 {}
1225
1226 void operator()(const Init* Statement) {
1227 if (typeid(*Statement) == typeid(ListInit)) {
1228 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1229 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1230 B != E; ++B)
1231 this->processDag(*B);
1232 }
1233 else {
1234 this->processDag(Statement);
1235 }
1236 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001237
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001238 void operator()(const DagInit& Test, unsigned, bool) {
1239 this->operator()(&Test);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001240 }
1241 void operator()(const Init* Statement, unsigned) {
1242 this->operator()(Statement);
1243 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001244};
1245
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001246/// CheckForSuperfluousOptions - Check that there are no side
1247/// effect-free options (specified only in the OptionList). Otherwise,
1248/// output a warning.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001249void CheckForSuperfluousOptions (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001250 const ToolDescriptions& ToolDescs,
1251 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001252 llvm::StringSet<> nonSuperfluousOptions;
1253
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001254 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001255 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001256 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1257 E = ToolDescs.end(); B != E; ++B) {
1258 const ToolDescription& TD = *(*B);
1259 ExtractOptionNames Callback(nonSuperfluousOptions);
1260 if (TD.Actions)
1261 WalkCase(TD.Actions, Callback, Callback);
1262 }
1263
1264 // Add all options mentioned in the 'case' clauses of the
1265 // OptionalEdges of the compilation graph to the set of
1266 // non-superfluous options.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001267 for (DagVector::const_iterator B = EdgeVector.begin(),
1268 E = EdgeVector.end(); B != E; ++B) {
1269 const DagInit* Edge = *B;
1270 if (Edge->getNumArgs() > 2) {
1271 const DagInit& Weight = InitPtrToDag(Edge->getArg(2));
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001272 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001273 }
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001274 }
1275
1276 // Check that all options in OptDescs belong to the set of
1277 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001278 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001279 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001280 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001281 if (!nonSuperfluousOptions.count(Val.Name)
1282 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001283 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001284 "Probable cause: this option is specified only in the OptionList.\n";
1285 }
1286}
1287
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001288/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1289bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1290 if (TestName == "single_input_file") {
1291 O << "InputFilenames.size() == 1";
1292 return true;
1293 }
1294 else if (TestName == "multiple_input_files") {
1295 O << "InputFilenames.size() > 1";
1296 return true;
1297 }
1298
1299 return false;
1300}
1301
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001302/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1303template <typename F>
1304void EmitListTest(const ListInit& L, const char* LogicOp,
1305 F Callback, raw_ostream& O)
1306{
1307 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1308 // of Dags...
1309 bool isFirst = true;
1310 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1311 if (isFirst)
1312 isFirst = false;
1313 else
Mikhail Glushenkovb7935e02010-01-01 04:40:54 +00001314 O << ' ' << LogicOp << ' ';
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001315 Callback(InitPtrToString(*B), O);
1316 }
1317}
1318
1319// Callbacks for use with EmitListTest.
1320
1321class EmitSwitchOn {
1322 const OptionDescriptions& OptDescs_;
1323public:
1324 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1325 {}
1326
1327 void operator()(const std::string& OptName, raw_ostream& O) const {
1328 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1329 O << OptDesc.GenVariableName();
1330 }
1331};
1332
1333class EmitEmptyTest {
1334 bool EmitNegate_;
1335 const OptionDescriptions& OptDescs_;
1336public:
1337 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1338 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1339 {}
1340
1341 void operator()(const std::string& OptName, raw_ostream& O) const {
1342 const char* Neg = (EmitNegate_ ? "!" : "");
1343 if (OptName == "o") {
1344 O << Neg << "OutputFilename.empty()";
1345 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001346 else if (OptName == "save-temps") {
1347 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1348 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001349 else {
1350 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1351 O << Neg << OptDesc.GenVariableName() << ".empty()";
1352 }
1353 }
1354};
1355
1356
1357/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1358bool EmitCaseTest1ArgList(const std::string& TestName,
1359 const DagInit& d,
1360 const OptionDescriptions& OptDescs,
1361 raw_ostream& O) {
Mikhail Glushenkov3a481e32010-01-01 03:50:51 +00001362 const ListInit& L = InitPtrToList(d.getArg(0));
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001363
1364 if (TestName == "any_switch_on") {
1365 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1366 return true;
1367 }
1368 else if (TestName == "switch_on") {
1369 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1370 return true;
1371 }
1372 else if (TestName == "any_not_empty") {
1373 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1374 return true;
1375 }
1376 else if (TestName == "any_empty") {
1377 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1378 return true;
1379 }
1380 else if (TestName == "not_empty") {
1381 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1382 return true;
1383 }
1384 else if (TestName == "empty") {
1385 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1386 return true;
1387 }
1388
1389 return false;
1390}
1391
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001392/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1393bool EmitCaseTest1ArgStr(const std::string& TestName,
1394 const DagInit& d,
1395 const OptionDescriptions& OptDescs,
1396 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001397 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001398
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001399 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001400 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001401 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001402 }
1403 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001404 O << "InLangs.count(\"" << OptName << "\") != 0";
1405 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001406 }
1407 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001408 // This works only for single-argument Tool::GenerateAction. Join
1409 // tools can process several files in different languages simultaneously.
1410
1411 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001412 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001413 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001414 }
1415 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001416 bool EmitNegate = (TestName == "not_empty");
1417 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001418 return true;
1419 }
1420
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001421 return false;
1422}
1423
1424/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1425bool EmitCaseTest1Arg(const std::string& TestName,
1426 const DagInit& d,
1427 const OptionDescriptions& OptDescs,
1428 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001429 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001430 if (typeid(*d.getArg(0)) == typeid(ListInit))
1431 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1432 else
1433 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1434}
1435
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001436/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001437bool EmitCaseTest2Args(const std::string& TestName,
1438 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001439 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001440 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001441 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001442 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001443 const std::string& OptName = InitPtrToString(d.getArg(0));
1444 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001445
1446 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001447 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001448 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1449 return true;
1450 }
1451 else if (TestName == "element_in_list") {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00001452 const OptionDescription& OptDesc = OptDescs.FindParameterList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001453 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001454 O << "std::find(" << VarName << ".begin(),\n";
1455 O.indent(IndentLevel + Indent1)
1456 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001457 << OptArg << "\") != " << VarName << ".end()";
1458 return true;
1459 }
1460
1461 return false;
1462}
1463
1464// Forward declaration.
1465// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001466void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001467 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001468 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001469
1470/// EmitLogicalOperationTest - Helper function used by
1471/// EmitCaseConstructHandler.
1472void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001473 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001474 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001475 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001476 O << '(';
1477 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001478 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001479 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001480 if (j != NumArgs - 1) {
1481 O << ")\n";
1482 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1483 }
1484 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001485 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001486 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001487 }
1488}
1489
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001490void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001491 const OptionDescriptions& OptDescs, raw_ostream& O)
1492{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001493 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001494 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1495 O << "! (";
1496 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1497 O << ")";
1498}
1499
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001500/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001501void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001502 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001503 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001504 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001505
1506 if (TestName == "and")
1507 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1508 else if (TestName == "or")
1509 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001510 else if (TestName == "not")
1511 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001512 else if (EmitCaseTest0Args(TestName, O))
1513 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001514 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1515 return;
1516 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1517 return;
1518 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001519 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001520}
1521
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001522
1523/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1524class EmitCaseTestCallback {
1525 bool EmitElseIf_;
1526 const OptionDescriptions& OptDescs_;
1527 raw_ostream& O_;
1528public:
1529
1530 EmitCaseTestCallback(bool EmitElseIf,
1531 const OptionDescriptions& OptDescs, raw_ostream& O)
1532 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1533 {}
1534
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001535 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001536 {
1537 if (GetOperatorName(Test) == "default") {
1538 O_.indent(IndentLevel) << "else {\n";
1539 }
1540 else {
1541 O_.indent(IndentLevel)
1542 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001543 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001544 O_ << ") {\n";
1545 }
1546 }
1547};
1548
1549/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1550template <typename F>
1551class EmitCaseStatementCallback {
1552 F Callback_;
1553 raw_ostream& O_;
1554public:
1555
1556 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1557 : Callback_(Callback), O_(O)
1558 {}
1559
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001560 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001561
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001562 // Ignore nested 'case' DAG.
1563 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001564 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case")) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001565 if (typeid(*Statement) == typeid(ListInit)) {
1566 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1567 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1568 B != E; ++B)
1569 Callback_(*B, (IndentLevel + Indent1), O_);
1570 }
1571 else {
1572 Callback_(Statement, (IndentLevel + Indent1), O_);
1573 }
1574 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001575 O_.indent(IndentLevel) << "}\n";
1576 }
1577
1578};
1579
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001580/// EmitCaseConstructHandler - Emit code that handles the 'case'
1581/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001582/// clause. Implemented on top of WalkCase.
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00001583/// Callback's type is void F(const Init* Statement, unsigned IndentLevel,
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001584/// raw_ostream& O).
1585/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001586/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1587/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001588template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001589void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001590 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001591 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001592 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001593 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1594 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001595}
1596
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001597/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001598/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1599/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001600void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001601 const char* Delimiters = " \t\n\v\f\r";
1602 enum TokenizerState
1603 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1604 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001605
1606 if (CmdLine.empty())
1607 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001608 Out.push_back("");
1609
1610 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1611 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001612
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001613 for (; B != E; ++B) {
1614 char cur_ch = CmdLine[B];
1615
1616 switch (cur_st) {
1617 case Normal:
1618 if (cur_ch == '$') {
1619 cur_st = SpecialCommand;
1620 break;
1621 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001622 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001623 // Skip whitespace
1624 B = CmdLine.find_first_not_of(Delimiters, B);
1625 if (B == std::string::npos) {
1626 B = E-1;
1627 continue;
1628 }
1629 --B;
1630 Out.push_back("");
1631 continue;
1632 }
1633 break;
1634
1635
1636 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001637 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001638 cur_st = Normal;
1639 Out.push_back("");
1640 continue;
1641 }
1642 if (cur_ch == '(') {
1643 Out.push_back("");
1644 cur_st = InsideSpecialCommand;
1645 continue;
1646 }
1647 break;
1648
1649 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001650 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001651 continue;
1652 }
1653 if (cur_ch == '\'') {
1654 cur_st = InsideQuotationMarks;
1655 Out.push_back("");
1656 continue;
1657 }
1658 if (cur_ch == ')') {
1659 cur_st = Normal;
1660 Out.push_back("");
1661 }
1662 if (cur_ch == ',') {
1663 continue;
1664 }
1665
1666 break;
1667
1668 case InsideQuotationMarks:
1669 if (cur_ch == '\'') {
1670 cur_st = InsideSpecialCommand;
1671 continue;
1672 }
1673 break;
1674 }
1675
1676 Out.back().push_back(cur_ch);
1677 }
1678}
1679
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001680/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1681/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1682/// SubstituteSpecialCommands().
1683StrVector::const_iterator
1684SubstituteCall (StrVector::const_iterator Pos,
1685 StrVector::const_iterator End,
1686 bool IsJoin, raw_ostream& O)
1687{
1688 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001689 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001690 const std::string& CmdName = *Pos;
1691
1692 if (CmdName == ")")
1693 throw "$CALL invocation: empty argument list!";
1694
1695 O << "hooks::";
1696 O << CmdName << "(";
1697
1698
1699 bool firstIteration = true;
1700 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001701 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001702 const std::string& Arg = *Pos;
1703 assert(Arg.size() != 0);
1704
1705 if (Arg[0] == ')')
1706 break;
1707
1708 if (firstIteration)
1709 firstIteration = false;
1710 else
1711 O << ", ";
1712
1713 if (Arg == "$INFILE") {
1714 if (IsJoin)
1715 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1716 else
1717 O << "inFile.c_str()";
1718 }
1719 else {
1720 O << '"' << Arg << '"';
1721 }
1722 }
1723
1724 O << ')';
1725
1726 return Pos;
1727}
1728
1729/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1730/// function used by SubstituteSpecialCommands().
1731StrVector::const_iterator
1732SubstituteEnv (StrVector::const_iterator Pos,
1733 StrVector::const_iterator End, raw_ostream& O)
1734{
1735 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001736 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001737 const std::string& EnvName = *Pos;
1738
1739 if (EnvName == ")")
1740 throw "$ENV invocation: empty argument list!";
1741
1742 O << "checkCString(std::getenv(\"";
1743 O << EnvName;
1744 O << "\"))";
1745
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001746 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001747
1748 return Pos;
1749}
1750
1751/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1752/// handler code. Helper function used by EmitCmdLineVecFill().
1753StrVector::const_iterator
1754SubstituteSpecialCommands (StrVector::const_iterator Pos,
1755 StrVector::const_iterator End,
1756 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001757{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001758
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001759 const std::string& cmd = *Pos;
1760
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001761 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001762 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001763 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001764 }
1765 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001766 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001767 }
1768 else {
1769 throw "Unknown special command: " + cmd;
1770 }
1771
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001772 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001773 const std::string& Leftover = *Pos;
1774 assert(Leftover.at(0) == ')');
1775 if (Leftover.size() != 1)
1776 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001777
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001778 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001779}
1780
1781/// EmitCmdLineVecFill - Emit code that fills in the command line
1782/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001783void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001784 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001785 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001786 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001787 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001788
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001789 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001790 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001791
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001792 StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001793
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001794 // Emit the command itself.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001795 assert(!StrVec[0].empty());
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001796 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001797 if (StrVec[0][0] == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001798 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1799 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001800 }
1801 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001802 O << '"' << StrVec[0] << '"';
1803 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001804 }
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001805 O << ";\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001806
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001807 // Go through the command arguments.
1808 assert(B <= E);
1809 for (; B != E; ++B) {
1810 const std::string& cmd = *B;
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001811
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001812 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001813 O.indent(IndentLevel);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001814
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001815 if (cmd.at(0) == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001816 O << "vec.push_back(std::make_pair(0, ";
1817 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1818 O << "));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001819 }
1820 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001821 O << "vec.push_back(std::make_pair(0, \"" << cmd << "\"));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001822 }
1823 }
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001824
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001825}
1826
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001827/// EmitForEachListElementCycleHeader - Emit common code for iterating through
1828/// all elements of a list. Helper function used by
1829/// EmitForwardOptionPropertyHandlingCode.
1830void EmitForEachListElementCycleHeader (const OptionDescription& D,
1831 unsigned IndentLevel,
1832 raw_ostream& O) {
1833 unsigned IndentLevel1 = IndentLevel + Indent1;
1834
1835 O.indent(IndentLevel)
1836 << "for (" << D.GenTypeDeclaration()
1837 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1838 O.indent(IndentLevel)
1839 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1840 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1841 << ".getPosition(B - " << D.GenVariableName()
1842 << ".begin());\n";
1843}
1844
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001845/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1846/// implement EmitActionHandler. Emits code for
1847/// handling the (forward) and (forward_as) option properties.
1848void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001849 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001850 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001851 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001852 const std::string& Name = NewName.empty()
1853 ? ("-" + D.Name)
1854 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001855 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001856
1857 switch (D.Type) {
1858 case OptionType::Switch:
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001859 O.indent(IndentLevel)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001860 << "vec.push_back(std::make_pair(" << D.GenVariableName()
1861 << ".getPosition(), \"" << Name << "\"));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001862 break;
1863 case OptionType::Parameter:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001864 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1865 << D.GenVariableName()
1866 <<".getPosition(), \"" << Name;
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001867
1868 if (!D.isForwardNotSplit()) {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001869 O << "\"));\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001870 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1871 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001872 << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001873 }
1874 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001875 O << "=\" + " << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001876 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001877 break;
1878 case OptionType::Prefix:
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 << Name << "\" + "
1882 << D.GenVariableName() << "));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001883 break;
1884 case OptionType::PrefixList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001885 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001886 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001887 << Name << "\" + " << "*B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001888 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001889
1890 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001891 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001892 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001893 }
1894
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001895 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001896 break;
1897 case OptionType::ParameterList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001898 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001899 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001900 << Name << "\"));\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001901
1902 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001903 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001904 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001905 }
1906
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001907 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001908 break;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001909 case OptionType::SwitchList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001910 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001911 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
1912 << Name << "\"));\n";
1913 O.indent(IndentLevel1) << "++B;\n";
1914 O.indent(IndentLevel) << "}\n";
1915 break;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001916 case OptionType::Alias:
1917 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001918 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001919 }
1920}
1921
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001922/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1923/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001924struct ActionHandlingCallbackBase
1925{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001926
1927 void onErrorDag(const DagInit& d,
1928 unsigned IndentLevel, raw_ostream& O) const
1929 {
1930 O.indent(IndentLevel)
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00001931 << "PrintError(\""
1932 << (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0)) : "Unknown error!")
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001933 << "\");\n";
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +00001934 O.indent(IndentLevel) << "return 1;\n";
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001935 }
1936
1937 void onWarningDag(const DagInit& d,
1938 unsigned IndentLevel, raw_ostream& O) const
1939 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001940 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001941 O.indent(IndentLevel) << "llvm::errs() << \""
1942 << InitPtrToString(d.getArg(0)) << "\";\n";
1943 }
1944
1945};
1946
1947/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1948/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001949class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001950
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001951typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1952(const DagInit&, unsigned, raw_ostream&) const;
1953
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001954class EmitActionHandlersCallback :
1955 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001956 public HandlerTable<EmitActionHandlersCallbackHandler>
1957{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001958 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001959
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001960 const OptionDescriptions& OptDescs;
1961
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001962 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1963 /// onAppendCmd and onOutputSuffix.
1964 void EmitHookInvocation(const std::string& Str,
1965 const char* BlockOpen, const char* BlockClose,
1966 unsigned IndentLevel, raw_ostream& O) const
1967 {
1968 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001969 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001970
1971 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1972 B != E; ++B) {
1973 const std::string& cmd = *B;
1974
1975 O.indent(IndentLevel) << BlockOpen;
1976
1977 if (cmd.at(0) == '$')
1978 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1979 else
1980 O << '"' << cmd << '"';
1981
1982 O << BlockClose;
1983 }
1984 }
1985
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001986 void onAppendCmd (const DagInit& Dag,
1987 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001988 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001989 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001990 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001991 "vec.push_back(std::make_pair(65536, ", "));\n",
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001992 IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001993 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001994
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001995 void onForward (const DagInit& Dag,
1996 unsigned IndentLevel, raw_ostream& O) const
1997 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001998 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001999 const std::string& Name = InitPtrToString(Dag.getArg(0));
2000 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2001 IndentLevel, "", O);
2002 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002003
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002004 void onForwardAs (const DagInit& Dag,
2005 unsigned IndentLevel, raw_ostream& O) const
2006 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002007 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002008 const std::string& Name = InitPtrToString(Dag.getArg(0));
2009 const std::string& NewName = InitPtrToString(Dag.getArg(1));
2010 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2011 IndentLevel, NewName, O);
2012 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002013
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002014 void onForwardValue (const DagInit& Dag,
2015 unsigned IndentLevel, raw_ostream& O) const
2016 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002017 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002018 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002019 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002020
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002021 if (D.isSwitchList()) {
2022 throw std::runtime_error
2023 ("forward_value is not allowed with switch_list");
2024 }
2025
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002026 if (D.isParameter()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002027 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2028 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002029 << D.GenVariableName() << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002030 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00002031 else {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002032 O.indent(IndentLevel) << "for (" << D.GenTypeDeclaration()
2033 << "::iterator B = " << D.GenVariableName()
2034 << ".begin(), \n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002035 O.indent(IndentLevel + Indent1) << " E = " << D.GenVariableName()
2036 << ".end(); B != E; ++B)\n";
2037 O.indent(IndentLevel) << "{\n";
2038 O.indent(IndentLevel + Indent1)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002039 << "unsigned pos = " << D.GenVariableName()
2040 << ".getPosition(B - " << D.GenVariableName()
2041 << ".begin());\n";
2042 O.indent(IndentLevel + Indent1)
2043 << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002044 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002045 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002046 }
2047
2048 void onForwardTransformedValue (const DagInit& Dag,
2049 unsigned IndentLevel, raw_ostream& O) const
2050 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002051 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002052 const std::string& Name = InitPtrToString(Dag.getArg(0));
2053 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002054 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002055
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002056 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2057 << D.GenVariableName() << ".getPosition("
2058 << (D.isList() ? "0" : "") << "), "
2059 << "hooks::" << Hook << "(" << D.GenVariableName()
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002060 << (D.isParameter() ? ".c_str()" : "") << ")));\n";
2061 }
2062
2063 void onNoOutFile (const DagInit& Dag,
2064 unsigned IndentLevel, raw_ostream& O) const
2065 {
2066 CheckNumberOfArguments(Dag, 0);
2067 O.indent(IndentLevel) << "no_out_file = true;\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002068 }
2069
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002070 void onOutputSuffix (const DagInit& Dag,
2071 unsigned IndentLevel, raw_ostream& O) const
2072 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002073 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002074 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
2075 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002076 }
2077
2078 void onStopCompilation (const DagInit& Dag,
2079 unsigned IndentLevel, raw_ostream& O) const
2080 {
2081 O.indent(IndentLevel) << "stop_compilation = true;\n";
2082 }
2083
2084
2085 void onUnpackValues (const DagInit& Dag,
2086 unsigned IndentLevel, raw_ostream& O) const
2087 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002088 throw "'unpack_values' is deprecated. "
2089 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002090 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002091
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002092 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002093
2094 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
2095 : OptDescs(OD)
2096 {
2097 if (!staticMembersInitialized_) {
2098 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
2099 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
2100 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
2101 AddHandler("forward", &EmitActionHandlersCallback::onForward);
2102 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002103 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
2104 AddHandler("forward_transformed_value",
2105 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002106 AddHandler("no_out_file",
2107 &EmitActionHandlersCallback::onNoOutFile);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002108 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
2109 AddHandler("stop_compilation",
2110 &EmitActionHandlersCallback::onStopCompilation);
2111 AddHandler("unpack_values",
2112 &EmitActionHandlersCallback::onUnpackValues);
2113
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002114
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002115 staticMembersInitialized_ = true;
2116 }
2117 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002118
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002119 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002120 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002121 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002122 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002123 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002124};
2125
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002126void EmitGenerateActionMethodHeader(const ToolDescription& D,
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002127 bool IsJoin, bool Naked,
2128 raw_ostream& O)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002129{
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002130 O.indent(Indent1) << "int GenerateAction(Action& Out,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002131
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002132 if (IsJoin)
2133 O.indent(Indent2) << "const PathVector& inFiles,\n";
2134 else
2135 O.indent(Indent2) << "const sys::Path& inFile,\n";
2136
2137 O.indent(Indent2) << "const bool HasChildren,\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002138 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2139 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2140 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2141 O.indent(Indent1) << "{\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002142
2143 if (!Naked) {
2144 O.indent(Indent2) << "std::string cmd;\n";
2145 O.indent(Indent2) << "std::string out_file;\n";
2146 O.indent(Indent2)
2147 << "std::vector<std::pair<unsigned, std::string> > vec;\n";
2148 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2149 O.indent(Indent2) << "bool no_out_file = false;\n";
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002150 O.indent(Indent2) << "std::string output_suffix(\""
2151 << D.OutputSuffix << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002152 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002153}
2154
2155// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2156// Tool::GenerateAction() method.
2157void EmitGenerateActionMethod (const ToolDescription& D,
2158 const OptionDescriptions& OptDescs,
2159 bool IsJoin, raw_ostream& O) {
2160
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002161 EmitGenerateActionMethodHeader(D, IsJoin, /* Naked = */ false, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002162
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002163 if (!D.CmdLine)
2164 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002165
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002166 // Process the 'command' property.
2167 O << '\n';
2168 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2169 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002170
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002171 // Process the 'actions' list of this tool.
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002172 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002173 EmitCaseConstructHandler(D.Actions, Indent2,
2174 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002175 false, OptDescs, O);
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002176 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002177
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002178 // Input file (s)
2179 if (!D.InFileOption.empty()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002180 O.indent(Indent2)
2181 << "vec.push_back(std::make_pair(InputFilenames.getPosition(0), \""
2182 << D.InFileOption << "\");\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002183 }
2184
2185 if (IsJoin) {
2186 O.indent(Indent2)
2187 << "for (PathVector::const_iterator B = inFiles.begin(),\n";
2188 O.indent(Indent3) << "E = inFiles.end(); B != E; ++B)\n";
2189 O.indent(Indent2) << "{\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002190 O.indent(Indent3) << "vec.push_back(std::make_pair("
2191 << "InputFilenames.getPosition(B - inFiles.begin()), "
2192 << "B->str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002193 O.indent(Indent2) << "}\n";
2194 }
2195 else {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002196 O.indent(Indent2) << "vec.push_back(std::make_pair("
2197 << "InputFilenames.getPosition(0), inFile.str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002198 }
2199
2200 // Output file
2201 O.indent(Indent2) << "if (!no_out_file) {\n";
2202 if (!D.OutFileOption.empty())
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002203 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002204 << D.OutFileOption << "\"));\n";
2205
2206 O.indent(Indent3) << "out_file = this->OutFilename("
2207 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002208 O.indent(Indent4) <<
2209 "TempDir, stop_compilation, output_suffix.c_str()).str();\n\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002210 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, out_file));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002211
2212 O.indent(Indent2) << "}\n\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002213
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002214 // Handle the Sink property.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002215 std::string SinkOption("autogenerated::");
2216 SinkOption += SinkOptionName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002217 if (D.isSink()) {
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002218 O.indent(Indent2) << "if (!" << SinkOption << ".empty()) {\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002219 O.indent(Indent3) << "for (cl::list<std::string>::iterator B = "
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002220 << SinkOption << ".begin(), E = " << SinkOption
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002221 << ".end(); B != E; ++B)\n";
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002222 O.indent(Indent4) << "vec.push_back(std::make_pair(" << SinkOption
2223 << ".getPosition(B - " << SinkOption
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002224 << ".begin()), *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002225 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002226 }
2227
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002228 O.indent(Indent2) << "Out.Construct(cmd, this->SortArgs(vec), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002229 << "stop_compilation, out_file);\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002230 O.indent(Indent2) << "return 0;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002231 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002232}
2233
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002234/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2235/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002236void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2237 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002238 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002239 if (!ToolDesc.isJoin()) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002240 EmitGenerateActionMethodHeader(ToolDesc, /* IsJoin = */ true,
2241 /* Naked = */ true, O);
2242 O.indent(Indent2) << "PrintError(\"" << ToolDesc.Name
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002243 << " is not a Join tool!\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002244 O.indent(Indent2) << "return -1;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002245 O.indent(Indent1) << "}\n\n";
2246 }
2247 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002248 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002249 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002250
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002251 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002252}
2253
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002254/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2255/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002256void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002257 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2258 O.indent(Indent2) << "return InputLanguages_;\n";
2259 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002260
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002261 if (D.OutLanguage.empty())
2262 throw "Tool " + D.Name + " has no 'out_language' property!";
2263
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002264 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2265 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2266 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002267}
2268
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002269/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002270void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002271 O.indent(Indent1) << "const char* Name() const {\n";
2272 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2273 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002274}
2275
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002276/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2277/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002278void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002279 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002280 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002281 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002282 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002283 O.indent(Indent2) << "return false;\n";
2284 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002285}
2286
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002287/// EmitWorksOnEmptyCallback - Callback used by EmitWorksOnEmptyMethod in
2288/// conjunction with EmitCaseConstructHandler.
2289void EmitWorksOnEmptyCallback (const Init* Value,
2290 unsigned IndentLevel, raw_ostream& O) {
2291 CheckBooleanConstant(Value);
2292 O.indent(IndentLevel) << "return " << Value->getAsString() << ";\n";
2293}
2294
2295/// EmitWorksOnEmptyMethod - Emit the WorksOnEmpty() method for a given Tool
2296/// class.
2297void EmitWorksOnEmptyMethod (const ToolDescription& D,
2298 const OptionDescriptions& OptDescs,
2299 raw_ostream& O)
2300{
2301 O.indent(Indent1) << "bool WorksOnEmpty() const {\n";
2302 if (D.OnEmpty == 0)
2303 O.indent(Indent2) << "return false;\n";
2304 else
2305 EmitCaseConstructHandler(D.OnEmpty, Indent2, EmitWorksOnEmptyCallback,
2306 /*EmitElseIf = */ true, OptDescs, O);
2307 O.indent(Indent1) << "}\n\n";
2308}
2309
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002310/// EmitStaticMemberDefinitions - Emit static member definitions for a
2311/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002312void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002313 if (D.InLanguage.empty())
2314 throw "Tool " + D.Name + " has no 'in_language' property!";
2315
2316 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2317 for (StrVector::const_iterator B = D.InLanguage.begin(),
2318 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002319 O << '\"' << *B << "\", ";
2320 O << "0};\n\n";
2321}
2322
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002323/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002324void EmitToolClassDefinition (const ToolDescription& D,
2325 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002326 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002327 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002328 return;
2329
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002330 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002331 O << "class " << D.Name << " : public ";
2332 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002333 O << "JoinTool";
2334 else
2335 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002336
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002337 O << " {\nprivate:\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002338 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002339
2340 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002341 EmitNameMethod(D, O);
2342 EmitInOutLanguageMethods(D, O);
2343 EmitIsJoinMethod(D, O);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002344 EmitWorksOnEmptyMethod(D, OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002345 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002346
2347 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002348 O << "};\n";
2349
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002350 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002351
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002352}
2353
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002354/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002355/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002356void EmitOptionDefinitions (const OptionDescriptions& descs,
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002357 bool HasSink, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002358{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002359 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002360
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002361 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002362 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002363 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002364 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002365
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002366 if (val.Type == OptionType::Alias) {
2367 Aliases.push_back(val);
2368 continue;
2369 }
2370
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002371 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002372 << val.GenPlainVariableName();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002373
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002374 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002375
2376 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2377 O << ", cl::Prefix";
2378
2379 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002380 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002381 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002382 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002383 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002384 }
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002385
2386 if (val.isOptional())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002387 O << ", cl::Optional";
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002388
2389 if (val.isOneOrMore())
2390 O << ", cl::OneOrMore";
2391
2392 if (val.isZeroOrMore())
2393 O << ", cl::ZeroOrMore";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002394
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002395 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002396 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002397 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002398 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002399
2400 if (val.isCommaSeparated())
2401 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002402
2403 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002404 O << ", cl::multi_val(" << val.MultiVal << ')';
2405
2406 if (val.InitVal) {
2407 const std::string& str = val.InitVal->getAsString();
2408 O << ", cl::init(" << str << ')';
2409 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002410
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002411 if (!val.Help.empty())
2412 O << ", cl::desc(\"" << val.Help << "\")";
2413
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002414 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002415 }
2416
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002417 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002418 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002419 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002420 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002421
2422 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov297514d2010-08-20 18:16:26 +00002423 << val.GenPlainVariableName()
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002424 << "(\"" << val.Name << '\"';
2425
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002426 const OptionDescription& D = descs.FindOption(val.Help);
2427 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002428
2429 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2430 }
2431
2432 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002433 if (HasSink)
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002434 O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002435
2436 O << '\n';
2437}
2438
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002439/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002440/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002441
2442class EmitPreprocessOptionsCallback;
2443
2444typedef void
2445(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2446(const DagInit&, unsigned, raw_ostream&) const;
2447
2448class EmitPreprocessOptionsCallback :
2449 public ActionHandlingCallbackBase,
2450 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2451{
2452 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002453 typedef void
2454 (EmitPreprocessOptionsCallback::* HandlerImpl)
2455 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002456
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002457 const OptionDescriptions& OptDescs_;
2458
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002459 void onListOrDag(const DagInit& d, HandlerImpl h,
2460 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002461 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002462 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002463 const Init* I = d.getArg(0);
2464
2465 // If I is a list, apply h to each element.
2466 if (typeid(*I) == typeid(ListInit)) {
2467 const ListInit& L = *static_cast<const ListInit*>(I);
2468 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B)
2469 ((this)->*(h))(*B, IndentLevel, O);
2470 }
2471 // Otherwise, apply h to I.
2472 else {
2473 ((this)->*(h))(I, IndentLevel, O);
2474 }
2475 }
2476
2477 void onUnsetOptionImpl(const Init* I,
2478 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002479 {
2480 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002481 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002482
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002483 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002484 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2485 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002486 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002487 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2488 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002489 else if (OptDesc.isList()) {
2490 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2491 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002492 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002493 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002494 }
2495 }
2496
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002497 void onUnsetOption(const DagInit& d,
2498 unsigned IndentLevel, raw_ostream& O) const
2499 {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002500 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2501 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002502 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002503
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002504 void onSetOptionImpl(const DagInit& d,
2505 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002506 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002507 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002508 const Init* Value = d.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002509 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2510
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002511 if (OptDesc.isList()) {
2512 const ListInit& List = InitPtrToList(Value);
2513
2514 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2515 for (ListInit::const_iterator B = List.begin(), E = List.end();
2516 B != E; ++B) {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002517 const Init* CurElem = *B;
2518 if (OptDesc.isSwitchList())
2519 CheckBooleanConstant(CurElem);
2520
2521 O.indent(IndentLevel)
2522 << OptDesc.GenVariableName() << ".push_back(\""
2523 << (OptDesc.isSwitchList() ? CurElem->getAsString()
2524 : InitPtrToString(CurElem))
2525 << "\");\n";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002526 }
2527 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002528 else if (OptDesc.isSwitch()) {
2529 CheckBooleanConstant(Value);
2530 O.indent(IndentLevel) << OptDesc.GenVariableName()
2531 << " = " << Value->getAsString() << ";\n";
2532 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002533 else if (OptDesc.isParameter()) {
2534 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002535 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002536 << " = \"" << Str << "\";\n";
2537 }
2538 else {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002539 throw "Can't apply 'set_option' to alias option -" + OptName + " !";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002540 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002541 }
2542
2543 void onSetSwitch(const Init* I,
2544 unsigned IndentLevel, raw_ostream& O) const {
2545 const std::string& OptName = InitPtrToString(I);
2546 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2547
2548 if (OptDesc.isSwitch())
2549 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2550 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002551 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002552 }
2553
2554 void onSetOption(const DagInit& d,
2555 unsigned IndentLevel, raw_ostream& O) const
2556 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002557 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002558
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002559 // Two arguments: (set_option "parameter", VALUE), where VALUE can be a
2560 // boolean, a string or a string list.
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002561 if (d.getNumArgs() > 1)
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002562 this->onSetOptionImpl(d, IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002563 // One argument: (set_option "switch")
2564 // or (set_option ["switch1", "switch2", ...])
2565 else
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002566 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2567 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002568 }
2569
2570public:
2571
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002572 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002573 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002574 {
2575 if (!staticMembersInitialized_) {
2576 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2577 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2578 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002579 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002580
2581 staticMembersInitialized_ = true;
2582 }
2583 }
2584
2585 void operator()(const Init* I,
2586 unsigned IndentLevel, raw_ostream& O) const
2587 {
2588 InvokeDagInitHandler(this, I, IndentLevel, O);
2589 }
2590
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002591};
2592
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002593/// EmitPreprocessOptions - Emit the PreprocessOptions() function.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002594void EmitPreprocessOptions (const RecordKeeper& Records,
2595 const OptionDescriptions& OptDecs, raw_ostream& O)
2596{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002597 O << "int PreprocessOptions () {\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002598
2599 const RecordVector& OptionPreprocessors =
2600 Records.getAllDerivedDefinitions("OptionPreprocessor");
2601
2602 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2603 E = OptionPreprocessors.end(); B!=E; ++B) {
2604 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002605 EmitCaseConstructHandler(Case, Indent1,
2606 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002607 false, OptDecs, O);
2608 }
2609
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002610 O << '\n';
2611 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002612 O << "}\n\n";
2613}
2614
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002615class DoEmitPopulateLanguageMap;
2616typedef void (DoEmitPopulateLanguageMap::* DoEmitPopulateLanguageMapHandler)
2617(const DagInit& D);
2618
2619class DoEmitPopulateLanguageMap
2620: public HandlerTable<DoEmitPopulateLanguageMapHandler>
2621{
2622private:
2623 raw_ostream& O_;
2624
2625public:
2626
2627 explicit DoEmitPopulateLanguageMap (raw_ostream& O) : O_(O) {
2628 if (!staticMembersInitialized_) {
2629 AddHandler("lang_to_suffixes",
2630 &DoEmitPopulateLanguageMap::onLangToSuffixes);
2631
2632 staticMembersInitialized_ = true;
2633 }
2634 }
2635
2636 void operator() (Init* I) {
2637 InvokeDagInitHandler(this, I);
2638 }
2639
2640private:
2641
2642 void onLangToSuffixes (const DagInit& d) {
2643 CheckNumberOfArguments(d, 2);
2644
2645 const std::string& Lang = InitPtrToString(d.getArg(0));
2646 Init* Suffixes = d.getArg(1);
2647
2648 // Second argument to lang_to_suffixes is either a single string...
2649 if (typeid(*Suffixes) == typeid(StringInit)) {
2650 O_.indent(Indent1) << "langMap[\"" << InitPtrToString(Suffixes)
2651 << "\"] = \"" << Lang << "\";\n";
2652 }
2653 // ...or a list of strings.
2654 else {
2655 const ListInit& Lst = InitPtrToList(Suffixes);
2656 assert(Lst.size() != 0);
2657 for (ListInit::const_iterator B = Lst.begin(), E = Lst.end();
2658 B != E; ++B) {
2659 O_.indent(Indent1) << "langMap[\"" << InitPtrToString(*B)
2660 << "\"] = \"" << Lang << "\";\n";
2661 }
2662 }
2663 }
2664
2665};
2666
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002667/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002668void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002669{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002670 O << "int PopulateLanguageMap (LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002671
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002672 // For each LangMap:
2673 const RecordVector& LangMaps =
Mikhail Glushenkov00a5b5b2010-08-23 19:24:16 +00002674 Records.getAllDerivedDefinitions("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002675
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002676 for (RecordVector::const_iterator B = LangMaps.begin(),
2677 E = LangMaps.end(); B!=E; ++B) {
2678 ListInit* LangMap = (*B)->getValueAsListInit("map");
2679 std::for_each(LangMap->begin(), LangMap->end(),
2680 DoEmitPopulateLanguageMap(O));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002681 }
2682
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002683 O << '\n';
2684 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002685 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002686}
2687
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002688/// EmitEdgePropertyHandlerCallback - Emits code that handles edge
2689/// properties. Helper function passed to EmitCaseConstructHandler() by
2690/// EmitEdgeClass().
2691void EmitEdgePropertyHandlerCallback (const Init* i, unsigned IndentLevel,
2692 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002693 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002694 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002695
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002696 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002697 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002698 }
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002699 else if (OpName == "error") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002700 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002701 O.indent(IndentLevel) << "PrintError(\""
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002702 << InitPtrToString(d.getArg(0))
2703 << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002704 O.indent(IndentLevel) << "return -1;\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002705 return;
2706 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002707 else {
2708 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002709 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002710 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002711
2712 if (d.getNumArgs() > 0)
2713 O << InitPtrToInt(d.getArg(0)) << ";\n";
2714 else
2715 O << "2;\n";
2716
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002717}
2718
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002719/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002720void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002721 const DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002722 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002723
2724 // Class constructor.
2725 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002726 << "public:\n";
2727 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2728 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002729
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002730 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002731 O.indent(Indent1)
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002732 << "int Weight(const InputLanguagesSet& InLangs) const {\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002733 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002734
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002735 // Handle the 'case' construct.
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002736 EmitCaseConstructHandler(Case, Indent2, EmitEdgePropertyHandlerCallback,
2737 false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002738
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002739 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002740 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002741}
2742
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002743/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002744void EmitEdgeClasses (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002745 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002746 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002747 int i = 0;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002748 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002749 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002750 const DagInit* Edge = *B;
2751 const std::string& NodeB = InitPtrToString(Edge->getArg(1));
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002752
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002753 if (Edge->getNumArgs() > 2) {
2754 const DagInit* Weight = &InitPtrToDag(Edge->getArg(2));
2755 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
2756 }
2757
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002758 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002759 }
2760}
2761
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002762/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph() function.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002763void EmitPopulateCompilationGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002764 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002765 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002766{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002767 O << "int PopulateCompilationGraph (CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002768
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002769 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2770 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002771 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002772
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002773 O << '\n';
2774
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002775 // Insert edges.
2776
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002777 int i = 0;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002778 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002779 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002780 const DagInit* Edge = *B;
2781 const std::string& NodeA = InitPtrToString(Edge->getArg(0));
2782 const std::string& NodeB = InitPtrToString(Edge->getArg(1));
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002783
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002784 O.indent(Indent1) << "if (int ret = G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002785
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002786 if (Edge->getNumArgs() > 2)
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002787 O << "new Edge" << i << "()";
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002788 else
2789 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002790
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002791 O << "))\n";
2792 O.indent(Indent2) << "return ret;\n";
2793
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002794 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002795 }
2796
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002797 O << '\n';
2798 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002799 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002800}
2801
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002802/// HookInfo - Information about the hook type and number of arguments.
2803struct HookInfo {
2804
2805 // A hook can either have a single parameter of type std::vector<std::string>,
2806 // or NumArgs parameters of type const char*.
2807 enum HookType { ListHook, ArgHook };
2808
2809 HookType Type;
2810 unsigned NumArgs;
2811
2812 HookInfo() : Type(ArgHook), NumArgs(1)
2813 {}
2814
2815 HookInfo(HookType T) : Type(T), NumArgs(1)
2816 {}
2817
2818 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2819 {}
2820};
2821
2822typedef llvm::StringMap<HookInfo> HookInfoMap;
2823
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002824/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002825/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002826/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002827class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002828 HookInfoMap& HookNames_;
2829 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002830public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002831 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2832 : HookNames_(HookNames), OptDescs_(OptDescs)
2833 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002834
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002835 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002836 const std::string& Name = GetOperatorName(Dag);
2837
2838 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002839 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002840 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2841 const std::string& HookName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002842 const OptionDescription& D =
2843 OptDescs_.FindParameterListOrParameter(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002844
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002845 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2846 : HookInfo::ArgHook);
2847 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002848 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002849 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002850 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2851 }
2852 }
2853
2854 void onCmdLine(const std::string& Cmd) {
2855 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002856 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002857
2858 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2859 B != E; ++B) {
2860 const std::string& cmd = *B;
2861
2862 if (cmd == "$CALL") {
2863 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002864 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002865 const std::string& HookName = *B;
2866
2867 if (HookName.at(0) == ')')
2868 throw "$CALL invoked with no arguments!";
2869
2870 while (++B != E && B->at(0) != ')') {
2871 ++NumArgs;
2872 }
2873
2874 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2875
2876 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2877 H->second.Type != HookInfo::ArgHook)
2878 throw "Overloading of hooks is not allowed. Overloaded hook: "
2879 + HookName;
2880 else
2881 HookNames_[HookName] = HookInfo(NumArgs);
2882 }
2883 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002884 }
2885
2886 void operator()(const Init* Arg) {
2887
2888 // We're invoked on an action (either a dag or a dag list).
2889 if (typeid(*Arg) == typeid(DagInit)) {
2890 const DagInit& Dag = InitPtrToDag(Arg);
2891 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002892 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002893 }
2894 else if (typeid(*Arg) == typeid(ListInit)) {
2895 const ListInit& List = InitPtrToList(Arg);
2896 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2897 ++B) {
2898 const DagInit& Dag = InitPtrToDag(*B);
2899 this->onAction(Dag);
2900 }
2901 return;
2902 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002903
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002904 // We're invoked on a command line.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002905 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002906 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002907
2908 void operator()(const DagInit* Test, unsigned, bool) {
2909 this->operator()(Test);
2910 }
2911 void operator()(const Init* Statement, unsigned) {
2912 this->operator()(Statement);
2913 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002914};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002915
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002916/// FillInHookNames - Actually extract the hook names from all command
2917/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002918void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002919 const OptionDescriptions& OptDescs,
2920 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002921{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002922 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002923 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2924 E = ToolDescs.end(); B != E; ++B) {
2925 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002926
2927 // Look for 'forward_transformed_value' in 'actions'.
2928 if (D.Actions)
2929 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2930
2931 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002932 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002933 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002934 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002935 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002936 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002937 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002938 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002939 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002940 }
2941}
2942
2943/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2944/// property records and emit hook function declaration for each
2945/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002946void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2947 const OptionDescriptions& OptDescs, raw_ostream& O) {
2948 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002949
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002950 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002951 if (HookNames.empty())
2952 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002953
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002954 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002955 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002956 const char* HookName = B->first();
2957 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002958
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002959 O.indent(Indent1) << "std::string " << HookName << "(";
2960
2961 if (Info.Type == HookInfo::ArgHook) {
2962 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2963 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2964 }
2965 }
2966 else {
2967 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002968 }
2969
2970 O <<");\n";
2971 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002972}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002973
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002974/// EmitIncludes - Emit necessary #include directives and some
2975/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002976void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002977 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2978 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002979 << "#include \"llvm/CompilerDriver/Error.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002980 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002981
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002982 << "#include \"llvm/Support/CommandLine.h\"\n"
2983 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002984
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002985 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002986 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002987 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002988 << "#include <stdexcept>\n\n"
2989
2990 << "using namespace llvm;\n"
2991 << "using namespace llvmc;\n\n"
2992
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002993 << "inline const char* checkCString(const char* s)\n"
2994 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002995}
2996
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002997
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00002998/// DriverData - Holds all information about the driver.
2999struct DriverData {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003000 OptionDescriptions OptDescs;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003001 ToolDescriptions ToolDescs;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003002 DagVector Edges;
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003003 bool HasSink;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003004};
3005
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003006/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003007/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003008bool HasSink(const ToolDescriptions& ToolDescs) {
3009 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
3010 E = ToolDescs.end(); B != E; ++B)
3011 if ((*B)->isSink())
3012 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003013
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003014 return false;
3015}
3016
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003017/// CollectDriverData - Collect compilation graph edges, tool properties and
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003018/// option properties from the parse tree.
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003019void CollectDriverData (const RecordKeeper& Records, DriverData& Data) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003020 // Collect option properties.
3021 const RecordVector& OptionLists =
3022 Records.getAllDerivedDefinitions("OptionList");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003023 CollectOptionDescriptions(OptionLists, Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003024
3025 // Collect tool properties.
3026 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003027 CollectToolDescriptions(Tools, Data.ToolDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003028 Data.HasSink = HasSink(Data.ToolDescs);
3029
3030 // Collect compilation graph edges.
3031 const RecordVector& CompilationGraphs =
3032 Records.getAllDerivedDefinitions("CompilationGraph");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003033 FillInEdgeVector(CompilationGraphs, Data.Edges);
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00003034}
3035
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003036/// CheckDriverData - Perform some sanity checks on the collected data.
3037void CheckDriverData(DriverData& Data) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003038 // Filter out all tools not mentioned in the compilation graph.
3039 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003040
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003041 // Typecheck the compilation graph.
3042 TypecheckGraph(Data.Edges, Data.ToolDescs);
3043
3044 // Check that there are no options without side effects (specified
3045 // only in the OptionList).
3046 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003047}
3048
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003049void EmitDriverCode(const DriverData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003050 // Emit file header.
3051 EmitIncludes(O);
3052
3053 // Emit global option registration code.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003054 O << "namespace llvmc {\n"
3055 << "namespace autogenerated {\n\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003056 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003057 O << "} // End namespace autogenerated.\n"
3058 << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003059
3060 // Emit hook declarations.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003061 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003062 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003063 O << "} // End namespace hooks.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003064
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003065 O << "namespace {\n\n";
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003066 O << "using namespace llvmc::autogenerated;\n\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003067
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003068 // Emit Tool classes.
3069 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
3070 E = Data.ToolDescs.end(); B!=E; ++B)
3071 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
3072
3073 // Emit Edge# classes.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003074 // TODO: check for edge/optional_edge dag markers.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003075 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
3076
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003077 O << "} // End anonymous namespace.\n\n";
3078
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003079 O << "namespace llvmc {\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003080 O << "namespace autogenerated {\n\n";
3081
3082 // Emit PreprocessOptions() function.
3083 EmitPreprocessOptions(Records, Data.OptDescs, O);
3084
3085 // Emit PopulateLanguageMap() function
3086 // (language map maps from file extensions to language names).
3087 EmitPopulateLanguageMap(Records, O);
3088
3089 // Emit PopulateCompilationGraph() function.
3090 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
3091
3092 O << "} // End namespace autogenerated.\n";
3093 O << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003094
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003095 // EOF
3096}
3097
3098
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003099// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00003100}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003101
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003102/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00003103void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003104 try {
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003105 DriverData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003106
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003107 CollectDriverData(Records, Data);
3108 CheckDriverData(Data);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003109
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003110 this->EmitSourceFileHeader("llvmc-based driver: auto-generated code", O);
3111 EmitDriverCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003112
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003113 } catch (std::exception& Error) {
3114 throw Error.what() + std::string(" - usually this means a syntax error.");
3115 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003116}