blob: 9dbbfe48486c7584190ec681c6c6cb2008be3982 [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 Glushenkov8c67e4c2010-08-24 01:10:22 +00001246/// IsOptionalEdge - Validate that the 'optional_edge' has proper structure.
1247bool IsOptionalEdge (const DagInit& Edg) {
1248 return (GetOperatorName(Edg) == "optional_edge") && (Edg.getNumArgs() > 2);
1249}
1250
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001251/// CheckForSuperfluousOptions - Check that there are no side
1252/// effect-free options (specified only in the OptionList). Otherwise,
1253/// output a warning.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001254void CheckForSuperfluousOptions (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001255 const ToolDescriptions& ToolDescs,
1256 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001257 llvm::StringSet<> nonSuperfluousOptions;
1258
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001259 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001260 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001261 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1262 E = ToolDescs.end(); B != E; ++B) {
1263 const ToolDescription& TD = *(*B);
1264 ExtractOptionNames Callback(nonSuperfluousOptions);
1265 if (TD.Actions)
1266 WalkCase(TD.Actions, Callback, Callback);
1267 }
1268
1269 // Add all options mentioned in the 'case' clauses of the
1270 // OptionalEdges of the compilation graph to the set of
1271 // non-superfluous options.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001272 for (DagVector::const_iterator B = EdgeVector.begin(),
1273 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00001274 const DagInit& Edge = **B;
1275 if (IsOptionalEdge(Edge)) {
1276 const DagInit& Weight = InitPtrToDag(Edge.getArg(2));
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001277 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001278 }
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001279 }
1280
1281 // Check that all options in OptDescs belong to the set of
1282 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001283 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001284 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001285 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001286 if (!nonSuperfluousOptions.count(Val.Name)
1287 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001288 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001289 "Probable cause: this option is specified only in the OptionList.\n";
1290 }
1291}
1292
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001293/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1294bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1295 if (TestName == "single_input_file") {
1296 O << "InputFilenames.size() == 1";
1297 return true;
1298 }
1299 else if (TestName == "multiple_input_files") {
1300 O << "InputFilenames.size() > 1";
1301 return true;
1302 }
1303
1304 return false;
1305}
1306
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001307/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1308template <typename F>
1309void EmitListTest(const ListInit& L, const char* LogicOp,
1310 F Callback, raw_ostream& O)
1311{
1312 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1313 // of Dags...
1314 bool isFirst = true;
1315 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1316 if (isFirst)
1317 isFirst = false;
1318 else
Mikhail Glushenkovb7935e02010-01-01 04:40:54 +00001319 O << ' ' << LogicOp << ' ';
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001320 Callback(InitPtrToString(*B), O);
1321 }
1322}
1323
1324// Callbacks for use with EmitListTest.
1325
1326class EmitSwitchOn {
1327 const OptionDescriptions& OptDescs_;
1328public:
1329 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1330 {}
1331
1332 void operator()(const std::string& OptName, raw_ostream& O) const {
1333 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1334 O << OptDesc.GenVariableName();
1335 }
1336};
1337
1338class EmitEmptyTest {
1339 bool EmitNegate_;
1340 const OptionDescriptions& OptDescs_;
1341public:
1342 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1343 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1344 {}
1345
1346 void operator()(const std::string& OptName, raw_ostream& O) const {
1347 const char* Neg = (EmitNegate_ ? "!" : "");
1348 if (OptName == "o") {
1349 O << Neg << "OutputFilename.empty()";
1350 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001351 else if (OptName == "save-temps") {
1352 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1353 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001354 else {
1355 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1356 O << Neg << OptDesc.GenVariableName() << ".empty()";
1357 }
1358 }
1359};
1360
1361
1362/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1363bool EmitCaseTest1ArgList(const std::string& TestName,
1364 const DagInit& d,
1365 const OptionDescriptions& OptDescs,
1366 raw_ostream& O) {
Mikhail Glushenkov3a481e32010-01-01 03:50:51 +00001367 const ListInit& L = InitPtrToList(d.getArg(0));
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001368
1369 if (TestName == "any_switch_on") {
1370 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1371 return true;
1372 }
1373 else if (TestName == "switch_on") {
1374 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1375 return true;
1376 }
1377 else if (TestName == "any_not_empty") {
1378 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1379 return true;
1380 }
1381 else if (TestName == "any_empty") {
1382 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1383 return true;
1384 }
1385 else if (TestName == "not_empty") {
1386 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1387 return true;
1388 }
1389 else if (TestName == "empty") {
1390 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1391 return true;
1392 }
1393
1394 return false;
1395}
1396
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001397/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1398bool EmitCaseTest1ArgStr(const std::string& TestName,
1399 const DagInit& d,
1400 const OptionDescriptions& OptDescs,
1401 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001402 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001403
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001404 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001405 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001406 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001407 }
1408 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001409 O << "InLangs.count(\"" << OptName << "\") != 0";
1410 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001411 }
1412 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001413 // This works only for single-argument Tool::GenerateAction. Join
1414 // tools can process several files in different languages simultaneously.
1415
1416 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001417 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001418 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001419 }
1420 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001421 bool EmitNegate = (TestName == "not_empty");
1422 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001423 return true;
1424 }
1425
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001426 return false;
1427}
1428
1429/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1430bool EmitCaseTest1Arg(const std::string& TestName,
1431 const DagInit& d,
1432 const OptionDescriptions& OptDescs,
1433 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001434 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001435 if (typeid(*d.getArg(0)) == typeid(ListInit))
1436 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1437 else
1438 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1439}
1440
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001441/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001442bool EmitCaseTest2Args(const std::string& TestName,
1443 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001444 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001445 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001446 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001447 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001448 const std::string& OptName = InitPtrToString(d.getArg(0));
1449 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001450
1451 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001452 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001453 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1454 return true;
1455 }
1456 else if (TestName == "element_in_list") {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00001457 const OptionDescription& OptDesc = OptDescs.FindParameterList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001458 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001459 O << "std::find(" << VarName << ".begin(),\n";
1460 O.indent(IndentLevel + Indent1)
1461 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001462 << OptArg << "\") != " << VarName << ".end()";
1463 return true;
1464 }
1465
1466 return false;
1467}
1468
1469// Forward declaration.
1470// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001471void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001472 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001473 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001474
1475/// EmitLogicalOperationTest - Helper function used by
1476/// EmitCaseConstructHandler.
1477void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001478 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001479 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001480 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001481 O << '(';
1482 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001483 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001484 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001485 if (j != NumArgs - 1) {
1486 O << ")\n";
1487 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1488 }
1489 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001490 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001491 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001492 }
1493}
1494
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001495void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001496 const OptionDescriptions& OptDescs, raw_ostream& O)
1497{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001498 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001499 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1500 O << "! (";
1501 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1502 O << ")";
1503}
1504
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001505/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001506void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001507 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001508 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001509 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001510
1511 if (TestName == "and")
1512 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1513 else if (TestName == "or")
1514 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001515 else if (TestName == "not")
1516 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001517 else if (EmitCaseTest0Args(TestName, O))
1518 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001519 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1520 return;
1521 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1522 return;
1523 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001524 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001525}
1526
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001527
1528/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1529class EmitCaseTestCallback {
1530 bool EmitElseIf_;
1531 const OptionDescriptions& OptDescs_;
1532 raw_ostream& O_;
1533public:
1534
1535 EmitCaseTestCallback(bool EmitElseIf,
1536 const OptionDescriptions& OptDescs, raw_ostream& O)
1537 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1538 {}
1539
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001540 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001541 {
1542 if (GetOperatorName(Test) == "default") {
1543 O_.indent(IndentLevel) << "else {\n";
1544 }
1545 else {
1546 O_.indent(IndentLevel)
1547 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001548 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001549 O_ << ") {\n";
1550 }
1551 }
1552};
1553
1554/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1555template <typename F>
1556class EmitCaseStatementCallback {
1557 F Callback_;
1558 raw_ostream& O_;
1559public:
1560
1561 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1562 : Callback_(Callback), O_(O)
1563 {}
1564
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001565 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001566
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001567 // Ignore nested 'case' DAG.
1568 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001569 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case")) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001570 if (typeid(*Statement) == typeid(ListInit)) {
1571 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1572 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1573 B != E; ++B)
1574 Callback_(*B, (IndentLevel + Indent1), O_);
1575 }
1576 else {
1577 Callback_(Statement, (IndentLevel + Indent1), O_);
1578 }
1579 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001580 O_.indent(IndentLevel) << "}\n";
1581 }
1582
1583};
1584
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001585/// EmitCaseConstructHandler - Emit code that handles the 'case'
1586/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001587/// clause. Implemented on top of WalkCase.
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00001588/// Callback's type is void F(const Init* Statement, unsigned IndentLevel,
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001589/// raw_ostream& O).
1590/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001591/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1592/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001593template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001594void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001595 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001596 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001597 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001598 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1599 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001600}
1601
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001602/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001603/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1604/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001605void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001606 const char* Delimiters = " \t\n\v\f\r";
1607 enum TokenizerState
1608 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1609 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001610
1611 if (CmdLine.empty())
1612 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001613 Out.push_back("");
1614
1615 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1616 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001617
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001618 for (; B != E; ++B) {
1619 char cur_ch = CmdLine[B];
1620
1621 switch (cur_st) {
1622 case Normal:
1623 if (cur_ch == '$') {
1624 cur_st = SpecialCommand;
1625 break;
1626 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001627 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001628 // Skip whitespace
1629 B = CmdLine.find_first_not_of(Delimiters, B);
1630 if (B == std::string::npos) {
1631 B = E-1;
1632 continue;
1633 }
1634 --B;
1635 Out.push_back("");
1636 continue;
1637 }
1638 break;
1639
1640
1641 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001642 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001643 cur_st = Normal;
1644 Out.push_back("");
1645 continue;
1646 }
1647 if (cur_ch == '(') {
1648 Out.push_back("");
1649 cur_st = InsideSpecialCommand;
1650 continue;
1651 }
1652 break;
1653
1654 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001655 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001656 continue;
1657 }
1658 if (cur_ch == '\'') {
1659 cur_st = InsideQuotationMarks;
1660 Out.push_back("");
1661 continue;
1662 }
1663 if (cur_ch == ')') {
1664 cur_st = Normal;
1665 Out.push_back("");
1666 }
1667 if (cur_ch == ',') {
1668 continue;
1669 }
1670
1671 break;
1672
1673 case InsideQuotationMarks:
1674 if (cur_ch == '\'') {
1675 cur_st = InsideSpecialCommand;
1676 continue;
1677 }
1678 break;
1679 }
1680
1681 Out.back().push_back(cur_ch);
1682 }
1683}
1684
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001685/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1686/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1687/// SubstituteSpecialCommands().
1688StrVector::const_iterator
1689SubstituteCall (StrVector::const_iterator Pos,
1690 StrVector::const_iterator End,
1691 bool IsJoin, raw_ostream& O)
1692{
1693 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001694 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001695 const std::string& CmdName = *Pos;
1696
1697 if (CmdName == ")")
1698 throw "$CALL invocation: empty argument list!";
1699
1700 O << "hooks::";
1701 O << CmdName << "(";
1702
1703
1704 bool firstIteration = true;
1705 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001706 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001707 const std::string& Arg = *Pos;
1708 assert(Arg.size() != 0);
1709
1710 if (Arg[0] == ')')
1711 break;
1712
1713 if (firstIteration)
1714 firstIteration = false;
1715 else
1716 O << ", ";
1717
1718 if (Arg == "$INFILE") {
1719 if (IsJoin)
1720 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1721 else
1722 O << "inFile.c_str()";
1723 }
1724 else {
1725 O << '"' << Arg << '"';
1726 }
1727 }
1728
1729 O << ')';
1730
1731 return Pos;
1732}
1733
1734/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1735/// function used by SubstituteSpecialCommands().
1736StrVector::const_iterator
1737SubstituteEnv (StrVector::const_iterator Pos,
1738 StrVector::const_iterator End, raw_ostream& O)
1739{
1740 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001741 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001742 const std::string& EnvName = *Pos;
1743
1744 if (EnvName == ")")
1745 throw "$ENV invocation: empty argument list!";
1746
1747 O << "checkCString(std::getenv(\"";
1748 O << EnvName;
1749 O << "\"))";
1750
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001751 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001752
1753 return Pos;
1754}
1755
1756/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1757/// handler code. Helper function used by EmitCmdLineVecFill().
1758StrVector::const_iterator
1759SubstituteSpecialCommands (StrVector::const_iterator Pos,
1760 StrVector::const_iterator End,
1761 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001762{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001763
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001764 const std::string& cmd = *Pos;
1765
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001766 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001767 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001768 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001769 }
1770 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001771 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001772 }
1773 else {
1774 throw "Unknown special command: " + cmd;
1775 }
1776
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001777 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001778 const std::string& Leftover = *Pos;
1779 assert(Leftover.at(0) == ')');
1780 if (Leftover.size() != 1)
1781 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001782
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001783 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001784}
1785
1786/// EmitCmdLineVecFill - Emit code that fills in the command line
1787/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001788void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001789 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001790 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001791 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001792 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001793
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001794 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001795 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001796
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001797 StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001798
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001799 // Emit the command itself.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001800 assert(!StrVec[0].empty());
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001801 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001802 if (StrVec[0][0] == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001803 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1804 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001805 }
1806 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001807 O << '"' << StrVec[0] << '"';
1808 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001809 }
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001810 O << ";\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001811
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001812 // Go through the command arguments.
1813 assert(B <= E);
1814 for (; B != E; ++B) {
1815 const std::string& cmd = *B;
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001816
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001817 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001818 O.indent(IndentLevel);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001819
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001820 if (cmd.at(0) == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001821 O << "vec.push_back(std::make_pair(0, ";
1822 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1823 O << "));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001824 }
1825 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001826 O << "vec.push_back(std::make_pair(0, \"" << cmd << "\"));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001827 }
1828 }
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001829
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001830}
1831
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001832/// EmitForEachListElementCycleHeader - Emit common code for iterating through
1833/// all elements of a list. Helper function used by
1834/// EmitForwardOptionPropertyHandlingCode.
1835void EmitForEachListElementCycleHeader (const OptionDescription& D,
1836 unsigned IndentLevel,
1837 raw_ostream& O) {
1838 unsigned IndentLevel1 = IndentLevel + Indent1;
1839
1840 O.indent(IndentLevel)
1841 << "for (" << D.GenTypeDeclaration()
1842 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1843 O.indent(IndentLevel)
1844 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1845 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1846 << ".getPosition(B - " << D.GenVariableName()
1847 << ".begin());\n";
1848}
1849
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001850/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1851/// implement EmitActionHandler. Emits code for
1852/// handling the (forward) and (forward_as) option properties.
1853void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001854 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001855 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001856 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001857 const std::string& Name = NewName.empty()
1858 ? ("-" + D.Name)
1859 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001860 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001861
1862 switch (D.Type) {
1863 case OptionType::Switch:
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001864 O.indent(IndentLevel)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001865 << "vec.push_back(std::make_pair(" << D.GenVariableName()
1866 << ".getPosition(), \"" << Name << "\"));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001867 break;
1868 case OptionType::Parameter:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001869 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1870 << D.GenVariableName()
1871 <<".getPosition(), \"" << Name;
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001872
1873 if (!D.isForwardNotSplit()) {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001874 O << "\"));\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001875 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1876 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001877 << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001878 }
1879 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001880 O << "=\" + " << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001881 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001882 break;
1883 case OptionType::Prefix:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001884 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1885 << D.GenVariableName() << ".getPosition(), \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001886 << Name << "\" + "
1887 << D.GenVariableName() << "));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001888 break;
1889 case OptionType::PrefixList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001890 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001891 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001892 << Name << "\" + " << "*B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001893 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001894
1895 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001896 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001897 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001898 }
1899
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001900 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001901 break;
1902 case OptionType::ParameterList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001903 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001904 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001905 << Name << "\"));\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001906
1907 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001908 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001909 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001910 }
1911
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001912 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001913 break;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001914 case OptionType::SwitchList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001915 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001916 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
1917 << Name << "\"));\n";
1918 O.indent(IndentLevel1) << "++B;\n";
1919 O.indent(IndentLevel) << "}\n";
1920 break;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001921 case OptionType::Alias:
1922 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001923 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001924 }
1925}
1926
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001927/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1928/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001929struct ActionHandlingCallbackBase
1930{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001931
1932 void onErrorDag(const DagInit& d,
1933 unsigned IndentLevel, raw_ostream& O) const
1934 {
1935 O.indent(IndentLevel)
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00001936 << "PrintError(\""
1937 << (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0)) : "Unknown error!")
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001938 << "\");\n";
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +00001939 O.indent(IndentLevel) << "return 1;\n";
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001940 }
1941
1942 void onWarningDag(const DagInit& d,
1943 unsigned IndentLevel, raw_ostream& O) const
1944 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001945 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001946 O.indent(IndentLevel) << "llvm::errs() << \""
1947 << InitPtrToString(d.getArg(0)) << "\";\n";
1948 }
1949
1950};
1951
1952/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1953/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001954class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001955
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001956typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1957(const DagInit&, unsigned, raw_ostream&) const;
1958
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001959class EmitActionHandlersCallback :
1960 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001961 public HandlerTable<EmitActionHandlersCallbackHandler>
1962{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001963 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001964
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001965 const OptionDescriptions& OptDescs;
1966
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001967 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1968 /// onAppendCmd and onOutputSuffix.
1969 void EmitHookInvocation(const std::string& Str,
1970 const char* BlockOpen, const char* BlockClose,
1971 unsigned IndentLevel, raw_ostream& O) const
1972 {
1973 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001974 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001975
1976 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1977 B != E; ++B) {
1978 const std::string& cmd = *B;
1979
1980 O.indent(IndentLevel) << BlockOpen;
1981
1982 if (cmd.at(0) == '$')
1983 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1984 else
1985 O << '"' << cmd << '"';
1986
1987 O << BlockClose;
1988 }
1989 }
1990
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001991 void onAppendCmd (const DagInit& Dag,
1992 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001993 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001994 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001995 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001996 "vec.push_back(std::make_pair(65536, ", "));\n",
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001997 IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001998 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001999
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002000 void onForward (const DagInit& Dag,
2001 unsigned IndentLevel, raw_ostream& O) const
2002 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002003 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002004 const std::string& Name = InitPtrToString(Dag.getArg(0));
2005 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2006 IndentLevel, "", O);
2007 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002008
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002009 void onForwardAs (const DagInit& Dag,
2010 unsigned IndentLevel, raw_ostream& O) const
2011 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002012 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002013 const std::string& Name = InitPtrToString(Dag.getArg(0));
2014 const std::string& NewName = InitPtrToString(Dag.getArg(1));
2015 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2016 IndentLevel, NewName, O);
2017 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002018
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002019 void onForwardValue (const DagInit& Dag,
2020 unsigned IndentLevel, raw_ostream& O) const
2021 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002022 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002023 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002024 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002025
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002026 if (D.isSwitchList()) {
2027 throw std::runtime_error
2028 ("forward_value is not allowed with switch_list");
2029 }
2030
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002031 if (D.isParameter()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002032 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2033 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002034 << D.GenVariableName() << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002035 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00002036 else {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002037 O.indent(IndentLevel) << "for (" << D.GenTypeDeclaration()
2038 << "::iterator B = " << D.GenVariableName()
2039 << ".begin(), \n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002040 O.indent(IndentLevel + Indent1) << " E = " << D.GenVariableName()
2041 << ".end(); B != E; ++B)\n";
2042 O.indent(IndentLevel) << "{\n";
2043 O.indent(IndentLevel + Indent1)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002044 << "unsigned pos = " << D.GenVariableName()
2045 << ".getPosition(B - " << D.GenVariableName()
2046 << ".begin());\n";
2047 O.indent(IndentLevel + Indent1)
2048 << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002049 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002050 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002051 }
2052
2053 void onForwardTransformedValue (const DagInit& Dag,
2054 unsigned IndentLevel, raw_ostream& O) const
2055 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002056 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002057 const std::string& Name = InitPtrToString(Dag.getArg(0));
2058 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002059 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002060
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002061 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2062 << D.GenVariableName() << ".getPosition("
2063 << (D.isList() ? "0" : "") << "), "
2064 << "hooks::" << Hook << "(" << D.GenVariableName()
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002065 << (D.isParameter() ? ".c_str()" : "") << ")));\n";
2066 }
2067
2068 void onNoOutFile (const DagInit& Dag,
2069 unsigned IndentLevel, raw_ostream& O) const
2070 {
2071 CheckNumberOfArguments(Dag, 0);
2072 O.indent(IndentLevel) << "no_out_file = true;\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002073 }
2074
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002075 void onOutputSuffix (const DagInit& Dag,
2076 unsigned IndentLevel, raw_ostream& O) const
2077 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002078 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002079 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
2080 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002081 }
2082
2083 void onStopCompilation (const DagInit& Dag,
2084 unsigned IndentLevel, raw_ostream& O) const
2085 {
2086 O.indent(IndentLevel) << "stop_compilation = true;\n";
2087 }
2088
2089
2090 void onUnpackValues (const DagInit& Dag,
2091 unsigned IndentLevel, raw_ostream& O) const
2092 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002093 throw "'unpack_values' is deprecated. "
2094 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002095 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002096
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002097 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002098
2099 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
2100 : OptDescs(OD)
2101 {
2102 if (!staticMembersInitialized_) {
2103 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
2104 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
2105 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
2106 AddHandler("forward", &EmitActionHandlersCallback::onForward);
2107 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002108 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
2109 AddHandler("forward_transformed_value",
2110 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002111 AddHandler("no_out_file",
2112 &EmitActionHandlersCallback::onNoOutFile);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002113 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
2114 AddHandler("stop_compilation",
2115 &EmitActionHandlersCallback::onStopCompilation);
2116 AddHandler("unpack_values",
2117 &EmitActionHandlersCallback::onUnpackValues);
2118
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002119
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002120 staticMembersInitialized_ = true;
2121 }
2122 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002123
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002124 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002125 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002126 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002127 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002128 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002129};
2130
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002131void EmitGenerateActionMethodHeader(const ToolDescription& D,
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002132 bool IsJoin, bool Naked,
2133 raw_ostream& O)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002134{
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002135 O.indent(Indent1) << "int GenerateAction(Action& Out,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002136
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002137 if (IsJoin)
2138 O.indent(Indent2) << "const PathVector& inFiles,\n";
2139 else
2140 O.indent(Indent2) << "const sys::Path& inFile,\n";
2141
2142 O.indent(Indent2) << "const bool HasChildren,\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002143 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2144 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2145 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2146 O.indent(Indent1) << "{\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002147
2148 if (!Naked) {
2149 O.indent(Indent2) << "std::string cmd;\n";
2150 O.indent(Indent2) << "std::string out_file;\n";
2151 O.indent(Indent2)
2152 << "std::vector<std::pair<unsigned, std::string> > vec;\n";
2153 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2154 O.indent(Indent2) << "bool no_out_file = false;\n";
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002155 O.indent(Indent2) << "std::string output_suffix(\""
2156 << D.OutputSuffix << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002157 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002158}
2159
2160// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2161// Tool::GenerateAction() method.
2162void EmitGenerateActionMethod (const ToolDescription& D,
2163 const OptionDescriptions& OptDescs,
2164 bool IsJoin, raw_ostream& O) {
2165
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002166 EmitGenerateActionMethodHeader(D, IsJoin, /* Naked = */ false, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002167
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002168 if (!D.CmdLine)
2169 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002170
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002171 // Process the 'command' property.
2172 O << '\n';
2173 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2174 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002175
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002176 // Process the 'actions' list of this tool.
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002177 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002178 EmitCaseConstructHandler(D.Actions, Indent2,
2179 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002180 false, OptDescs, O);
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002181 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002182
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002183 // Input file (s)
2184 if (!D.InFileOption.empty()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002185 O.indent(Indent2)
2186 << "vec.push_back(std::make_pair(InputFilenames.getPosition(0), \""
2187 << D.InFileOption << "\");\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002188 }
2189
2190 if (IsJoin) {
2191 O.indent(Indent2)
2192 << "for (PathVector::const_iterator B = inFiles.begin(),\n";
2193 O.indent(Indent3) << "E = inFiles.end(); B != E; ++B)\n";
2194 O.indent(Indent2) << "{\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002195 O.indent(Indent3) << "vec.push_back(std::make_pair("
2196 << "InputFilenames.getPosition(B - inFiles.begin()), "
2197 << "B->str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002198 O.indent(Indent2) << "}\n";
2199 }
2200 else {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002201 O.indent(Indent2) << "vec.push_back(std::make_pair("
2202 << "InputFilenames.getPosition(0), inFile.str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002203 }
2204
2205 // Output file
2206 O.indent(Indent2) << "if (!no_out_file) {\n";
2207 if (!D.OutFileOption.empty())
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002208 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002209 << D.OutFileOption << "\"));\n";
2210
2211 O.indent(Indent3) << "out_file = this->OutFilename("
2212 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002213 O.indent(Indent4) <<
2214 "TempDir, stop_compilation, output_suffix.c_str()).str();\n\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002215 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, out_file));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002216
2217 O.indent(Indent2) << "}\n\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002218
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002219 // Handle the Sink property.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002220 std::string SinkOption("autogenerated::");
2221 SinkOption += SinkOptionName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002222 if (D.isSink()) {
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002223 O.indent(Indent2) << "if (!" << SinkOption << ".empty()) {\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002224 O.indent(Indent3) << "for (cl::list<std::string>::iterator B = "
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002225 << SinkOption << ".begin(), E = " << SinkOption
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002226 << ".end(); B != E; ++B)\n";
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002227 O.indent(Indent4) << "vec.push_back(std::make_pair(" << SinkOption
2228 << ".getPosition(B - " << SinkOption
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002229 << ".begin()), *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002230 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002231 }
2232
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002233 O.indent(Indent2) << "Out.Construct(cmd, this->SortArgs(vec), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002234 << "stop_compilation, out_file);\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002235 O.indent(Indent2) << "return 0;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002236 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002237}
2238
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002239/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2240/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002241void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2242 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002243 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002244 if (!ToolDesc.isJoin()) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002245 EmitGenerateActionMethodHeader(ToolDesc, /* IsJoin = */ true,
2246 /* Naked = */ true, O);
2247 O.indent(Indent2) << "PrintError(\"" << ToolDesc.Name
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002248 << " is not a Join tool!\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002249 O.indent(Indent2) << "return -1;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002250 O.indent(Indent1) << "}\n\n";
2251 }
2252 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002253 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002254 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002255
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002256 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002257}
2258
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002259/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2260/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002261void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002262 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2263 O.indent(Indent2) << "return InputLanguages_;\n";
2264 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002265
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002266 if (D.OutLanguage.empty())
2267 throw "Tool " + D.Name + " has no 'out_language' property!";
2268
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002269 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2270 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2271 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002272}
2273
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002274/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002275void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002276 O.indent(Indent1) << "const char* Name() const {\n";
2277 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2278 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002279}
2280
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002281/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2282/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002283void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002284 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002285 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002286 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002287 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002288 O.indent(Indent2) << "return false;\n";
2289 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002290}
2291
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002292/// EmitWorksOnEmptyCallback - Callback used by EmitWorksOnEmptyMethod in
2293/// conjunction with EmitCaseConstructHandler.
2294void EmitWorksOnEmptyCallback (const Init* Value,
2295 unsigned IndentLevel, raw_ostream& O) {
2296 CheckBooleanConstant(Value);
2297 O.indent(IndentLevel) << "return " << Value->getAsString() << ";\n";
2298}
2299
2300/// EmitWorksOnEmptyMethod - Emit the WorksOnEmpty() method for a given Tool
2301/// class.
2302void EmitWorksOnEmptyMethod (const ToolDescription& D,
2303 const OptionDescriptions& OptDescs,
2304 raw_ostream& O)
2305{
2306 O.indent(Indent1) << "bool WorksOnEmpty() const {\n";
2307 if (D.OnEmpty == 0)
2308 O.indent(Indent2) << "return false;\n";
2309 else
2310 EmitCaseConstructHandler(D.OnEmpty, Indent2, EmitWorksOnEmptyCallback,
2311 /*EmitElseIf = */ true, OptDescs, O);
2312 O.indent(Indent1) << "}\n\n";
2313}
2314
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002315/// EmitStaticMemberDefinitions - Emit static member definitions for a
2316/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002317void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002318 if (D.InLanguage.empty())
2319 throw "Tool " + D.Name + " has no 'in_language' property!";
2320
2321 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2322 for (StrVector::const_iterator B = D.InLanguage.begin(),
2323 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002324 O << '\"' << *B << "\", ";
2325 O << "0};\n\n";
2326}
2327
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002328/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002329void EmitToolClassDefinition (const ToolDescription& D,
2330 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002331 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002332 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002333 return;
2334
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002335 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002336 O << "class " << D.Name << " : public ";
2337 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002338 O << "JoinTool";
2339 else
2340 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002341
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002342 O << " {\nprivate:\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002343 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002344
2345 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002346 EmitNameMethod(D, O);
2347 EmitInOutLanguageMethods(D, O);
2348 EmitIsJoinMethod(D, O);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002349 EmitWorksOnEmptyMethod(D, OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002350 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002351
2352 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002353 O << "};\n";
2354
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002355 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002356
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002357}
2358
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002359/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002360/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002361void EmitOptionDefinitions (const OptionDescriptions& descs,
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002362 bool HasSink, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002363{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002364 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002365
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002366 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002367 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002368 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002369 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002370
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002371 if (val.Type == OptionType::Alias) {
2372 Aliases.push_back(val);
2373 continue;
2374 }
2375
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002376 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002377 << val.GenPlainVariableName();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002378
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002379 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002380
2381 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2382 O << ", cl::Prefix";
2383
2384 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002385 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002386 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002387 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002388 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002389 }
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002390
2391 if (val.isOptional())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002392 O << ", cl::Optional";
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002393
2394 if (val.isOneOrMore())
2395 O << ", cl::OneOrMore";
2396
2397 if (val.isZeroOrMore())
2398 O << ", cl::ZeroOrMore";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002399
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002400 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002401 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002402 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002403 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002404
2405 if (val.isCommaSeparated())
2406 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002407
2408 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002409 O << ", cl::multi_val(" << val.MultiVal << ')';
2410
2411 if (val.InitVal) {
2412 const std::string& str = val.InitVal->getAsString();
2413 O << ", cl::init(" << str << ')';
2414 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002415
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002416 if (!val.Help.empty())
2417 O << ", cl::desc(\"" << val.Help << "\")";
2418
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002419 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002420 }
2421
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002422 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002423 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002424 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002425 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002426
2427 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov297514d2010-08-20 18:16:26 +00002428 << val.GenPlainVariableName()
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002429 << "(\"" << val.Name << '\"';
2430
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002431 const OptionDescription& D = descs.FindOption(val.Help);
2432 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002433
2434 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2435 }
2436
2437 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002438 if (HasSink)
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002439 O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002440
2441 O << '\n';
2442}
2443
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002444/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002445/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002446
2447class EmitPreprocessOptionsCallback;
2448
2449typedef void
2450(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2451(const DagInit&, unsigned, raw_ostream&) const;
2452
2453class EmitPreprocessOptionsCallback :
2454 public ActionHandlingCallbackBase,
2455 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2456{
2457 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002458 typedef void
2459 (EmitPreprocessOptionsCallback::* HandlerImpl)
2460 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002461
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002462 const OptionDescriptions& OptDescs_;
2463
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002464 void onListOrDag(const DagInit& d, HandlerImpl h,
2465 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002466 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002467 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002468 const Init* I = d.getArg(0);
2469
2470 // If I is a list, apply h to each element.
2471 if (typeid(*I) == typeid(ListInit)) {
2472 const ListInit& L = *static_cast<const ListInit*>(I);
2473 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B)
2474 ((this)->*(h))(*B, IndentLevel, O);
2475 }
2476 // Otherwise, apply h to I.
2477 else {
2478 ((this)->*(h))(I, IndentLevel, O);
2479 }
2480 }
2481
2482 void onUnsetOptionImpl(const Init* I,
2483 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002484 {
2485 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002486 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002487
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002488 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002489 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2490 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002491 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002492 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2493 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002494 else if (OptDesc.isList()) {
2495 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2496 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002497 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002498 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002499 }
2500 }
2501
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002502 void onUnsetOption(const DagInit& d,
2503 unsigned IndentLevel, raw_ostream& O) const
2504 {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002505 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2506 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002507 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002508
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002509 void onSetOptionImpl(const DagInit& d,
2510 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002511 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002512 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002513 const Init* Value = d.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002514 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2515
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002516 if (OptDesc.isList()) {
2517 const ListInit& List = InitPtrToList(Value);
2518
2519 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2520 for (ListInit::const_iterator B = List.begin(), E = List.end();
2521 B != E; ++B) {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002522 const Init* CurElem = *B;
2523 if (OptDesc.isSwitchList())
2524 CheckBooleanConstant(CurElem);
2525
2526 O.indent(IndentLevel)
2527 << OptDesc.GenVariableName() << ".push_back(\""
2528 << (OptDesc.isSwitchList() ? CurElem->getAsString()
2529 : InitPtrToString(CurElem))
2530 << "\");\n";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002531 }
2532 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002533 else if (OptDesc.isSwitch()) {
2534 CheckBooleanConstant(Value);
2535 O.indent(IndentLevel) << OptDesc.GenVariableName()
2536 << " = " << Value->getAsString() << ";\n";
2537 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002538 else if (OptDesc.isParameter()) {
2539 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002540 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002541 << " = \"" << Str << "\";\n";
2542 }
2543 else {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002544 throw "Can't apply 'set_option' to alias option -" + OptName + " !";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002545 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002546 }
2547
2548 void onSetSwitch(const Init* I,
2549 unsigned IndentLevel, raw_ostream& O) const {
2550 const std::string& OptName = InitPtrToString(I);
2551 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2552
2553 if (OptDesc.isSwitch())
2554 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2555 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002556 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002557 }
2558
2559 void onSetOption(const DagInit& d,
2560 unsigned IndentLevel, raw_ostream& O) const
2561 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002562 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002563
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002564 // Two arguments: (set_option "parameter", VALUE), where VALUE can be a
2565 // boolean, a string or a string list.
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002566 if (d.getNumArgs() > 1)
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002567 this->onSetOptionImpl(d, IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002568 // One argument: (set_option "switch")
2569 // or (set_option ["switch1", "switch2", ...])
2570 else
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002571 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2572 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002573 }
2574
2575public:
2576
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002577 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002578 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002579 {
2580 if (!staticMembersInitialized_) {
2581 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2582 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2583 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002584 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002585
2586 staticMembersInitialized_ = true;
2587 }
2588 }
2589
2590 void operator()(const Init* I,
2591 unsigned IndentLevel, raw_ostream& O) const
2592 {
2593 InvokeDagInitHandler(this, I, IndentLevel, O);
2594 }
2595
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002596};
2597
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002598/// EmitPreprocessOptions - Emit the PreprocessOptions() function.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002599void EmitPreprocessOptions (const RecordKeeper& Records,
2600 const OptionDescriptions& OptDecs, raw_ostream& O)
2601{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002602 O << "int PreprocessOptions () {\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002603
2604 const RecordVector& OptionPreprocessors =
2605 Records.getAllDerivedDefinitions("OptionPreprocessor");
2606
2607 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2608 E = OptionPreprocessors.end(); B!=E; ++B) {
2609 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002610 EmitCaseConstructHandler(Case, Indent1,
2611 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002612 false, OptDecs, O);
2613 }
2614
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002615 O << '\n';
2616 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002617 O << "}\n\n";
2618}
2619
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002620class DoEmitPopulateLanguageMap;
2621typedef void (DoEmitPopulateLanguageMap::* DoEmitPopulateLanguageMapHandler)
2622(const DagInit& D);
2623
2624class DoEmitPopulateLanguageMap
2625: public HandlerTable<DoEmitPopulateLanguageMapHandler>
2626{
2627private:
2628 raw_ostream& O_;
2629
2630public:
2631
2632 explicit DoEmitPopulateLanguageMap (raw_ostream& O) : O_(O) {
2633 if (!staticMembersInitialized_) {
2634 AddHandler("lang_to_suffixes",
2635 &DoEmitPopulateLanguageMap::onLangToSuffixes);
2636
2637 staticMembersInitialized_ = true;
2638 }
2639 }
2640
2641 void operator() (Init* I) {
2642 InvokeDagInitHandler(this, I);
2643 }
2644
2645private:
2646
2647 void onLangToSuffixes (const DagInit& d) {
2648 CheckNumberOfArguments(d, 2);
2649
2650 const std::string& Lang = InitPtrToString(d.getArg(0));
2651 Init* Suffixes = d.getArg(1);
2652
2653 // Second argument to lang_to_suffixes is either a single string...
2654 if (typeid(*Suffixes) == typeid(StringInit)) {
2655 O_.indent(Indent1) << "langMap[\"" << InitPtrToString(Suffixes)
2656 << "\"] = \"" << Lang << "\";\n";
2657 }
2658 // ...or a list of strings.
2659 else {
2660 const ListInit& Lst = InitPtrToList(Suffixes);
2661 assert(Lst.size() != 0);
2662 for (ListInit::const_iterator B = Lst.begin(), E = Lst.end();
2663 B != E; ++B) {
2664 O_.indent(Indent1) << "langMap[\"" << InitPtrToString(*B)
2665 << "\"] = \"" << Lang << "\";\n";
2666 }
2667 }
2668 }
2669
2670};
2671
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002672/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002673void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002674{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002675 O << "int PopulateLanguageMap (LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002676
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002677 // For each LangMap:
2678 const RecordVector& LangMaps =
Mikhail Glushenkov00a5b5b2010-08-23 19:24:16 +00002679 Records.getAllDerivedDefinitions("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002680
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002681 for (RecordVector::const_iterator B = LangMaps.begin(),
2682 E = LangMaps.end(); B!=E; ++B) {
2683 ListInit* LangMap = (*B)->getValueAsListInit("map");
2684 std::for_each(LangMap->begin(), LangMap->end(),
2685 DoEmitPopulateLanguageMap(O));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002686 }
2687
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002688 O << '\n';
2689 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002690 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002691}
2692
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002693/// EmitEdgePropertyHandlerCallback - Emits code that handles edge
2694/// properties. Helper function passed to EmitCaseConstructHandler() by
2695/// EmitEdgeClass().
2696void EmitEdgePropertyHandlerCallback (const Init* i, unsigned IndentLevel,
2697 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002698 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002699 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002700
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002701 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002702 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002703 }
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002704 else if (OpName == "error") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002705 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002706 O.indent(IndentLevel) << "PrintError(\""
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002707 << InitPtrToString(d.getArg(0))
2708 << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002709 O.indent(IndentLevel) << "return -1;\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002710 return;
2711 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002712 else {
2713 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002714 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002715 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002716
2717 if (d.getNumArgs() > 0)
2718 O << InitPtrToInt(d.getArg(0)) << ";\n";
2719 else
2720 O << "2;\n";
2721
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002722}
2723
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002724/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002725void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002726 const DagInit& Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002727 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002728
2729 // Class constructor.
2730 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002731 << "public:\n";
2732 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2733 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002734
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002735 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002736 O.indent(Indent1)
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002737 << "int Weight(const InputLanguagesSet& InLangs) const {\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002738 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002739
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002740 // Handle the 'case' construct.
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002741 EmitCaseConstructHandler(&Case, Indent2, EmitEdgePropertyHandlerCallback,
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002742 false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002743
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002744 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002745 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002746}
2747
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002748/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002749void EmitEdgeClasses (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002750 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002751 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002752 int i = 0;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002753 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002754 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002755 const DagInit& Edge = **B;
2756 const std::string& Name = GetOperatorName(Edge);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002757
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002758 if (Name == "optional_edge") {
2759 assert(IsOptionalEdge(Edge));
2760 const std::string& NodeB = InitPtrToString(Edge.getArg(1));
2761
2762 const DagInit& Weight = InitPtrToDag(Edge.getArg(2));
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002763 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
2764 }
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002765 else if (Name != "edge") {
2766 throw "Unknown edge class: '" + Name + "'!";
2767 }
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002768
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002769 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002770 }
2771}
2772
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002773/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph() function.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002774void EmitPopulateCompilationGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002775 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002776 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002777{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002778 O << "int PopulateCompilationGraph (CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002779
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002780 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2781 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002782 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002783
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002784 O << '\n';
2785
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002786 // Insert edges.
2787
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002788 int i = 0;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002789 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002790 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002791 const DagInit& Edge = **B;
2792 const std::string& NodeA = InitPtrToString(Edge.getArg(0));
2793 const std::string& NodeB = InitPtrToString(Edge.getArg(1));
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002794
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002795 O.indent(Indent1) << "if (int ret = G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002796
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002797 if (IsOptionalEdge(Edge))
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002798 O << "new Edge" << i << "()";
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002799 else
2800 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002801
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002802 O << "))\n";
2803 O.indent(Indent2) << "return ret;\n";
2804
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002805 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002806 }
2807
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002808 O << '\n';
2809 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002810 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002811}
2812
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002813/// HookInfo - Information about the hook type and number of arguments.
2814struct HookInfo {
2815
2816 // A hook can either have a single parameter of type std::vector<std::string>,
2817 // or NumArgs parameters of type const char*.
2818 enum HookType { ListHook, ArgHook };
2819
2820 HookType Type;
2821 unsigned NumArgs;
2822
2823 HookInfo() : Type(ArgHook), NumArgs(1)
2824 {}
2825
2826 HookInfo(HookType T) : Type(T), NumArgs(1)
2827 {}
2828
2829 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2830 {}
2831};
2832
2833typedef llvm::StringMap<HookInfo> HookInfoMap;
2834
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002835/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002836/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002837/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002838class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002839 HookInfoMap& HookNames_;
2840 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002841public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002842 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2843 : HookNames_(HookNames), OptDescs_(OptDescs)
2844 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002845
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002846 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002847 const std::string& Name = GetOperatorName(Dag);
2848
2849 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002850 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002851 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2852 const std::string& HookName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002853 const OptionDescription& D =
2854 OptDescs_.FindParameterListOrParameter(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002855
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002856 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2857 : HookInfo::ArgHook);
2858 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002859 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002860 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002861 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2862 }
2863 }
2864
2865 void onCmdLine(const std::string& Cmd) {
2866 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002867 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002868
2869 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2870 B != E; ++B) {
2871 const std::string& cmd = *B;
2872
2873 if (cmd == "$CALL") {
2874 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002875 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002876 const std::string& HookName = *B;
2877
2878 if (HookName.at(0) == ')')
2879 throw "$CALL invoked with no arguments!";
2880
2881 while (++B != E && B->at(0) != ')') {
2882 ++NumArgs;
2883 }
2884
2885 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2886
2887 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2888 H->second.Type != HookInfo::ArgHook)
2889 throw "Overloading of hooks is not allowed. Overloaded hook: "
2890 + HookName;
2891 else
2892 HookNames_[HookName] = HookInfo(NumArgs);
2893 }
2894 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002895 }
2896
2897 void operator()(const Init* Arg) {
2898
2899 // We're invoked on an action (either a dag or a dag list).
2900 if (typeid(*Arg) == typeid(DagInit)) {
2901 const DagInit& Dag = InitPtrToDag(Arg);
2902 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002903 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002904 }
2905 else if (typeid(*Arg) == typeid(ListInit)) {
2906 const ListInit& List = InitPtrToList(Arg);
2907 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2908 ++B) {
2909 const DagInit& Dag = InitPtrToDag(*B);
2910 this->onAction(Dag);
2911 }
2912 return;
2913 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002914
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002915 // We're invoked on a command line.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002916 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002917 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002918
2919 void operator()(const DagInit* Test, unsigned, bool) {
2920 this->operator()(Test);
2921 }
2922 void operator()(const Init* Statement, unsigned) {
2923 this->operator()(Statement);
2924 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002925};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002926
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002927/// FillInHookNames - Actually extract the hook names from all command
2928/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002929void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002930 const OptionDescriptions& OptDescs,
2931 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002932{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002933 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002934 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2935 E = ToolDescs.end(); B != E; ++B) {
2936 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002937
2938 // Look for 'forward_transformed_value' in 'actions'.
2939 if (D.Actions)
2940 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2941
2942 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002943 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002944 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002945 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002946 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002947 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002948 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002949 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002950 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002951 }
2952}
2953
2954/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2955/// property records and emit hook function declaration for each
2956/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002957void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2958 const OptionDescriptions& OptDescs, raw_ostream& O) {
2959 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002960
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002961 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002962 if (HookNames.empty())
2963 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002964
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002965 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002966 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002967 const char* HookName = B->first();
2968 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002969
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002970 O.indent(Indent1) << "std::string " << HookName << "(";
2971
2972 if (Info.Type == HookInfo::ArgHook) {
2973 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2974 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2975 }
2976 }
2977 else {
2978 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002979 }
2980
2981 O <<");\n";
2982 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002983}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002984
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002985/// EmitIncludes - Emit necessary #include directives and some
2986/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002987void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002988 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2989 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002990 << "#include \"llvm/CompilerDriver/Error.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002991 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002992
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002993 << "#include \"llvm/Support/CommandLine.h\"\n"
2994 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002995
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002996 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002997 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002998 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002999 << "#include <stdexcept>\n\n"
3000
3001 << "using namespace llvm;\n"
3002 << "using namespace llvmc;\n\n"
3003
Mikhail Glushenkov67665722008-11-12 12:41:18 +00003004 << "inline const char* checkCString(const char* s)\n"
3005 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00003006}
3007
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003008
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003009/// DriverData - Holds all information about the driver.
3010struct DriverData {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003011 OptionDescriptions OptDescs;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003012 ToolDescriptions ToolDescs;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003013 DagVector Edges;
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003014 bool HasSink;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003015};
3016
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003017/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003018/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003019bool HasSink(const ToolDescriptions& ToolDescs) {
3020 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
3021 E = ToolDescs.end(); B != E; ++B)
3022 if ((*B)->isSink())
3023 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003024
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003025 return false;
3026}
3027
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003028/// CollectDriverData - Collect compilation graph edges, tool properties and
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003029/// option properties from the parse tree.
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003030void CollectDriverData (const RecordKeeper& Records, DriverData& Data) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003031 // Collect option properties.
3032 const RecordVector& OptionLists =
3033 Records.getAllDerivedDefinitions("OptionList");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003034 CollectOptionDescriptions(OptionLists, Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003035
3036 // Collect tool properties.
3037 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003038 CollectToolDescriptions(Tools, Data.ToolDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003039 Data.HasSink = HasSink(Data.ToolDescs);
3040
3041 // Collect compilation graph edges.
3042 const RecordVector& CompilationGraphs =
3043 Records.getAllDerivedDefinitions("CompilationGraph");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003044 FillInEdgeVector(CompilationGraphs, Data.Edges);
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00003045}
3046
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003047/// CheckDriverData - Perform some sanity checks on the collected data.
3048void CheckDriverData(DriverData& Data) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003049 // Filter out all tools not mentioned in the compilation graph.
3050 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003051
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003052 // Typecheck the compilation graph.
3053 TypecheckGraph(Data.Edges, Data.ToolDescs);
3054
3055 // Check that there are no options without side effects (specified
3056 // only in the OptionList).
3057 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003058}
3059
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003060void EmitDriverCode(const DriverData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003061 // Emit file header.
3062 EmitIncludes(O);
3063
3064 // Emit global option registration code.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003065 O << "namespace llvmc {\n"
3066 << "namespace autogenerated {\n\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003067 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003068 O << "} // End namespace autogenerated.\n"
3069 << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003070
3071 // Emit hook declarations.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003072 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003073 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003074 O << "} // End namespace hooks.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003075
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003076 O << "namespace {\n\n";
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003077 O << "using namespace llvmc::autogenerated;\n\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003078
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003079 // Emit Tool classes.
3080 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
3081 E = Data.ToolDescs.end(); B!=E; ++B)
3082 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
3083
3084 // Emit Edge# classes.
3085 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
3086
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003087 O << "} // End anonymous namespace.\n\n";
3088
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003089 O << "namespace llvmc {\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003090 O << "namespace autogenerated {\n\n";
3091
3092 // Emit PreprocessOptions() function.
3093 EmitPreprocessOptions(Records, Data.OptDescs, O);
3094
3095 // Emit PopulateLanguageMap() function
3096 // (language map maps from file extensions to language names).
3097 EmitPopulateLanguageMap(Records, O);
3098
3099 // Emit PopulateCompilationGraph() function.
3100 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
3101
3102 O << "} // End namespace autogenerated.\n";
3103 O << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003104
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003105 // EOF
3106}
3107
3108
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003109// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00003110}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003111
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003112/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00003113void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003114 try {
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003115 DriverData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003116
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003117 CollectDriverData(Records, Data);
3118 CheckDriverData(Data);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003119
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003120 this->EmitSourceFileHeader("llvmc-based driver: auto-generated code", O);
3121 EmitDriverCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003122
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003123 } catch (std::exception& Error) {
3124 throw Error.what() + std::string(" - usually this means a syntax error.");
3125 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003126}