blob: 8b81e14cc26a23578beda090c0f41dde5dfd6901 [file] [log] [blame]
Mikhail Glushenkovfb37f392008-05-30 06:20:54 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000010// This tablegen backend is responsible for emitting LLVMC configuration code.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkovecbdcf22008-05-06 18:09:29 +000014#include "LLVMCConfigurationEmitter.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000015#include "Record.h"
16
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000018#include "llvm/ADT/StringMap.h"
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000019#include "llvm/ADT/StringSet.h"
Mikhail Glushenkove0b65702009-12-23 12:49:30 +000020
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000021#include <algorithm>
22#include <cassert>
23#include <functional>
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +000024#include <stdexcept>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000025#include <string>
Chris Lattner32a9e7a2008-06-04 04:46:14 +000026#include <typeinfo>
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +000027
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000028using namespace llvm;
29
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000030namespace {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
32//===----------------------------------------------------------------------===//
33/// Typedefs
34
35typedef std::vector<Record*> RecordVector;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +000036typedef std::vector<const DagInit*> DagVector;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000037typedef std::vector<std::string> StrVector;
38
39//===----------------------------------------------------------------------===//
40/// Constants
41
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +000042// Indentation.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000043const unsigned TabWidth = 4;
44const unsigned Indent1 = TabWidth*1;
45const unsigned Indent2 = TabWidth*2;
46const unsigned Indent3 = TabWidth*3;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +000047const unsigned Indent4 = TabWidth*4;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000048
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000049// Default help string.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000050const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000051
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000052// Name for the "sink" option.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +000053const char * const SinkOptionName = "SinkOption";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000054
55//===----------------------------------------------------------------------===//
56/// Helper functions
57
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000058/// Id - An 'identity' function object.
59struct Id {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000060 template<typename T0>
61 void operator()(const T0&) const {
62 }
63 template<typename T0, typename T1>
64 void operator()(const T0&, const T1&) const {
65 }
66 template<typename T0, typename T1, typename T2>
67 void operator()(const T0&, const T1&, const T2&) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000068 }
69};
70
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000071int InitPtrToInt(const Init* ptr) {
72 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000073 return val.getValue();
74}
75
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000076const std::string& InitPtrToString(const Init* ptr) {
77 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
78 return val.getValue();
79}
80
81const ListInit& InitPtrToList(const Init* ptr) {
82 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
83 return val;
84}
85
86const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000087 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000088 return val;
89}
90
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000091const std::string GetOperatorName(const DagInit& D) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +000092 return D.getOperator()->getAsString();
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000093}
94
Mikhail Glushenkove0b65702009-12-23 12:49:30 +000095/// CheckBooleanConstant - Check that the provided value is a boolean constant.
96void CheckBooleanConstant(const Init* I) {
97 const DefInit& val = dynamic_cast<const DefInit&>(*I);
98 const std::string& str = val.getAsString();
99
100 if (str != "true" && str != "false") {
101 throw "Incorrect boolean value: '" + str +
102 "': must be either 'true' or 'false'";
103 }
104}
105
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000106// CheckNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +0000107// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000108void CheckNumberOfArguments (const DagInit& d, unsigned minArgs) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000109 if (d.getNumArgs() < minArgs)
110 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +0000111}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000112
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000113// EscapeVariableName - Escape commas and other symbols not allowed
114// in the C++ variable names. Makes it possible to use options named
115// like "Wa," (useful for prefix options).
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000116std::string EscapeVariableName (const std::string& Var) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000117 std::string ret;
118 for (unsigned i = 0; i != Var.size(); ++i) {
119 char cur_char = Var[i];
120 if (cur_char == ',') {
121 ret += "_comma_";
122 }
123 else if (cur_char == '+') {
124 ret += "_plus_";
125 }
126 else if (cur_char == '-') {
127 ret += "_dash_";
128 }
129 else {
130 ret.push_back(cur_char);
131 }
132 }
133 return ret;
134}
135
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000136/// EscapeQuotes - Replace '"' with '\"'.
137std::string EscapeQuotes (const std::string& Var) {
138 std::string ret;
139 for (unsigned i = 0; i != Var.size(); ++i) {
140 char cur_char = Var[i];
141 if (cur_char == '"') {
142 ret += "\\\"";
143 }
144 else {
145 ret.push_back(cur_char);
146 }
147 }
148 return ret;
149}
150
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000151/// OneOf - Does the input string contain this character?
152bool OneOf(const char* lst, char c) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000153 while (*lst) {
154 if (*lst++ == c)
155 return true;
156 }
157 return false;
158}
159
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000160template <class I, class S>
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000161void CheckedIncrement(I& P, I E, S ErrorString) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000162 ++P;
163 if (P == E)
164 throw ErrorString;
165}
166
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000167// apply is needed because C++'s syntax doesn't let us construct a function
168// object and call it in the same statement.
169template<typename F, typename T0>
170void apply(F Fun, T0& Arg0) {
171 return Fun(Arg0);
172}
173
174template<typename F, typename T0, typename T1>
175void apply(F Fun, T0& Arg0, T1& Arg1) {
176 return Fun(Arg0, Arg1);
177}
178
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000179//===----------------------------------------------------------------------===//
180/// Back-end specific code
181
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000182
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000183/// OptionType - One of six different option types. See the
184/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000185namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000186
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000187 enum OptionType { Alias, Switch, SwitchList,
188 Parameter, ParameterList, Prefix, PrefixList };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000189
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000190 bool IsAlias(OptionType t) {
191 return (t == Alias);
192 }
193
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000194 bool IsList (OptionType t) {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000195 return (t == SwitchList || t == ParameterList || t == PrefixList);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000196 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000197
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000198 bool IsSwitch (OptionType t) {
199 return (t == Switch);
200 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000201
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000202 bool IsSwitchList (OptionType t) {
203 return (t == SwitchList);
204 }
205
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000206 bool IsParameter (OptionType t) {
207 return (t == Parameter || t == Prefix);
208 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000209
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000210}
211
212OptionType::OptionType stringToOptionType(const std::string& T) {
213 if (T == "alias_option")
214 return OptionType::Alias;
215 else if (T == "switch_option")
216 return OptionType::Switch;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000217 else if (T == "switch_list_option")
218 return OptionType::SwitchList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000219 else if (T == "parameter_option")
220 return OptionType::Parameter;
221 else if (T == "parameter_list_option")
222 return OptionType::ParameterList;
223 else if (T == "prefix_option")
224 return OptionType::Prefix;
225 else if (T == "prefix_list_option")
226 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000227 else
228 throw "Unknown option type: " + T + '!';
229}
230
231namespace OptionDescriptionFlags {
232 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000233 ReallyHidden = 0x4, OneOrMore = 0x8,
234 Optional = 0x10, CommaSeparated = 0x20,
235 ForwardNotSplit = 0x40, ZeroOrMore = 0x80 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000236}
237
238/// OptionDescription - Represents data contained in a single
239/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000240struct OptionDescription {
241 OptionType::OptionType Type;
242 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000243 unsigned Flags;
244 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000245 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000246 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000247
248 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000249 const std::string& n = "",
250 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000251 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000252 {}
253
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000254 /// GenTypeDeclaration - Returns the C++ variable type of this
255 /// option.
256 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000257
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000258 /// GenVariableName - Returns the variable name used in the
259 /// generated C++ code.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000260 std::string GenVariableName() const
261 { return "autogenerated::" + GenOptionType() + EscapeVariableName(Name); }
262
263 /// GenPlainVariableName - Returns the variable name without the namespace
264 /// prefix.
265 std::string GenPlainVariableName() const
266 { return GenOptionType() + EscapeVariableName(Name); }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000267
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000268 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000269 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000270
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000271 /// CheckConsistency - Check that the flags are consistent.
272 void CheckConsistency() const;
273
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000274 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000275
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000276 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000277
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000278 bool isMultiVal() const;
279
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000280 bool isCommaSeparated() const;
281 void setCommaSeparated();
282
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000283 bool isForwardNotSplit() const;
284 void setForwardNotSplit();
285
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000286 bool isRequired() const;
287 void setRequired();
288
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000289 bool isOneOrMore() const;
290 void setOneOrMore();
291
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000292 bool isZeroOrMore() const;
293 void setZeroOrMore();
294
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000295 bool isOptional() const;
296 void setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000297
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000298 bool isHidden() const;
299 void setHidden();
300
301 bool isReallyHidden() const;
302 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000303
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000304 bool isSwitch() const
305 { return OptionType::IsSwitch(this->Type); }
306
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000307 bool isSwitchList() const
308 { return OptionType::IsSwitchList(this->Type); }
309
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000310 bool isParameter() const
311 { return OptionType::IsParameter(this->Type); }
312
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000313 bool isList() const
314 { return OptionType::IsList(this->Type); }
315
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000316 bool isParameterList() const
317 { return (OptionType::IsList(this->Type)
318 && !OptionType::IsSwitchList(this->Type)); }
319
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000320private:
321
322 // GenOptionType - Helper function used by GenVariableName().
323 std::string GenOptionType() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000324};
325
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000326void OptionDescription::CheckConsistency() const {
327 unsigned i = 0;
328
329 i += this->isRequired();
330 i += this->isOptional();
331 i += this->isOneOrMore();
332 i += this->isZeroOrMore();
333
334 if (i > 1) {
335 throw "Only one of (required), (optional), (one_or_more) or "
336 "(zero_or_more) properties is allowed!";
337 }
338}
339
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000340void OptionDescription::Merge (const OptionDescription& other)
341{
342 if (other.Type != Type)
343 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000344
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000345 if (Help == other.Help || Help == DefaultHelpString)
346 Help = other.Help;
347 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000348 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000349 " defined for option " + Name + "\n";
350 }
351
352 Flags |= other.Flags;
353}
354
355bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000356 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000357}
358
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000359bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000360 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000361}
362
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000363bool OptionDescription::isCommaSeparated() const {
364 return Flags & OptionDescriptionFlags::CommaSeparated;
365}
366void OptionDescription::setCommaSeparated() {
367 Flags |= OptionDescriptionFlags::CommaSeparated;
368}
369
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000370bool OptionDescription::isForwardNotSplit() const {
371 return Flags & OptionDescriptionFlags::ForwardNotSplit;
372}
373void OptionDescription::setForwardNotSplit() {
374 Flags |= OptionDescriptionFlags::ForwardNotSplit;
375}
376
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000377bool OptionDescription::isRequired() const {
378 return Flags & OptionDescriptionFlags::Required;
379}
380void OptionDescription::setRequired() {
381 Flags |= OptionDescriptionFlags::Required;
382}
383
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000384bool OptionDescription::isOneOrMore() const {
385 return Flags & OptionDescriptionFlags::OneOrMore;
386}
387void OptionDescription::setOneOrMore() {
388 Flags |= OptionDescriptionFlags::OneOrMore;
389}
390
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000391bool OptionDescription::isZeroOrMore() const {
392 return Flags & OptionDescriptionFlags::ZeroOrMore;
393}
394void OptionDescription::setZeroOrMore() {
395 Flags |= OptionDescriptionFlags::ZeroOrMore;
396}
397
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000398bool OptionDescription::isOptional() const {
399 return Flags & OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000400}
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000401void OptionDescription::setOptional() {
402 Flags |= OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000403}
404
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000405bool OptionDescription::isHidden() const {
406 return Flags & OptionDescriptionFlags::Hidden;
407}
408void OptionDescription::setHidden() {
409 Flags |= OptionDescriptionFlags::Hidden;
410}
411
412bool OptionDescription::isReallyHidden() const {
413 return Flags & OptionDescriptionFlags::ReallyHidden;
414}
415void OptionDescription::setReallyHidden() {
416 Flags |= OptionDescriptionFlags::ReallyHidden;
417}
418
419const char* OptionDescription::GenTypeDeclaration() const {
420 switch (Type) {
421 case OptionType::Alias:
422 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000423 case OptionType::PrefixList:
424 case OptionType::ParameterList:
425 return "cl::list<std::string>";
426 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000427 return "cl::opt<bool>";
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000428 case OptionType::SwitchList:
429 return "cl::list<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000430 case OptionType::Parameter:
431 case OptionType::Prefix:
432 default:
433 return "cl::opt<std::string>";
434 }
435}
436
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000437std::string OptionDescription::GenOptionType() const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000438 switch (Type) {
439 case OptionType::Alias:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000440 return "Alias_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000441 case OptionType::PrefixList:
442 case OptionType::ParameterList:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000443 return "List_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000444 case OptionType::Switch:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000445 return "Switch_";
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000446 case OptionType::SwitchList:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000447 return "SwitchList_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000448 case OptionType::Prefix:
449 case OptionType::Parameter:
450 default:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000451 return "Parameter_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000452 }
453}
454
455/// OptionDescriptions - An OptionDescription array plus some helper
456/// functions.
457class OptionDescriptions {
458 typedef StringMap<OptionDescription> container_type;
459
460 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000461 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000462
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000463public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000464 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000465 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000466
467 // Wrappers for FindOption that throw an exception in case the option has a
468 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000469 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000470 const OptionDescription& FindParameter(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000471 const OptionDescription& FindParameterList(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000472 const OptionDescription&
473 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000474 const OptionDescription&
475 FindParameterListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000476
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000477 /// insertDescription - Insert new OptionDescription into
478 /// OptionDescriptions list
479 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000480
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000481 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000482 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000483 const_iterator begin() const { return Descriptions.begin(); }
484 const_iterator end() const { return Descriptions.end(); }
485};
486
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000487const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000488OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000489 const_iterator I = Descriptions.find(OptName);
490 if (I != Descriptions.end())
491 return I->second;
492 else
493 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000494}
495
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000496const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000497OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000498 const OptionDescription& OptDesc = this->FindOption(OptName);
499 if (!OptDesc.isSwitch())
500 throw OptName + ": incorrect option type - should be a switch!";
501 return OptDesc;
502}
503
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000504const OptionDescription&
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000505OptionDescriptions::FindParameterList(const std::string& OptName) const {
506 const OptionDescription& OptDesc = this->FindOption(OptName);
507 if (!OptDesc.isList() || OptDesc.isSwitchList())
508 throw OptName + ": incorrect option type - should be a parameter list!";
509 return OptDesc;
510}
511
512const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000513OptionDescriptions::FindParameter(const std::string& OptName) const {
514 const OptionDescription& OptDesc = this->FindOption(OptName);
515 if (!OptDesc.isParameter())
516 throw OptName + ": incorrect option type - should be a parameter!";
517 return OptDesc;
518}
519
520const OptionDescription&
521OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
522 const OptionDescription& OptDesc = this->FindOption(OptName);
523 if (!OptDesc.isList() && !OptDesc.isParameter())
524 throw OptName
525 + ": incorrect option type - should be a list or parameter!";
526 return OptDesc;
527}
528
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000529const OptionDescription&
530OptionDescriptions::FindParameterListOrParameter
531(const std::string& OptName) const {
532 const OptionDescription& OptDesc = this->FindOption(OptName);
533 if ((!OptDesc.isList() && !OptDesc.isParameter()) || OptDesc.isSwitchList())
534 throw OptName
535 + ": incorrect option type - should be a parameter list or parameter!";
536 return OptDesc;
537}
538
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000539void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000540 container_type::iterator I = Descriptions.find(o.Name);
541 if (I != Descriptions.end()) {
542 OptionDescription& D = I->second;
543 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000544 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000545 else {
546 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000547 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000548}
549
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000550/// HandlerTable - A base class for function objects implemented as
551/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000552template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000553class HandlerTable {
554protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000555 // Implementation details.
556
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000557 /// HandlerMap - A map from property names to property handlers
558 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000559
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000560 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000561 static bool staticMembersInitialized_;
562
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000563public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000564
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000565 Handler GetHandler (const std::string& HandlerName) const {
566 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000567
568 if (method != Handlers_.end()) {
569 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000570 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000571 }
572 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000573 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000574 }
575 }
576
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000577 void AddHandler(const char* Property, Handler H) {
578 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000579 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000580
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000581};
582
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000583template <class Handler, class FunctionObject>
584Handler GetHandler(FunctionObject* Obj, const DagInit& Dag) {
585 const std::string& HandlerName = GetOperatorName(Dag);
586 return Obj->GetHandler(HandlerName);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000587}
588
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000589template <class FunctionObject>
590void InvokeDagInitHandler(FunctionObject* Obj, Init* I) {
591 typedef void (FunctionObject::*Handler) (const DagInit&);
592
593 const DagInit& Dag = InitPtrToDag(I);
594 Handler h = GetHandler<Handler>(Obj, Dag);
595
596 ((Obj)->*(h))(Dag);
597}
598
599template <class FunctionObject>
600void InvokeDagInitHandler(const FunctionObject* const Obj,
601 const Init* I, unsigned IndentLevel, raw_ostream& O)
602{
603 typedef void (FunctionObject::*Handler)
604 (const DagInit&, unsigned IndentLevel, raw_ostream& O) const;
605
606 const DagInit& Dag = InitPtrToDag(I);
607 Handler h = GetHandler<Handler>(Obj, Dag);
608
609 ((Obj)->*(h))(Dag, IndentLevel, O);
610}
611
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000612template <typename H>
613typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
614
615template <typename H>
616bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000617
618
619/// CollectOptionProperties - Function object for iterating over an
620/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000621class CollectOptionProperties;
622typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000623(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000624
625class CollectOptionProperties
626: public HandlerTable<CollectOptionPropertiesHandler>
627{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000628private:
629
630 /// optDescs_ - OptionDescriptions table. This is where the
631 /// information is stored.
632 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000633
634public:
635
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000636 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000637 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000638 {
639 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000640 AddHandler("help", &CollectOptionProperties::onHelp);
641 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000642 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000643 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
644 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000645 AddHandler("zero_or_more", &CollectOptionProperties::onZeroOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000646 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
647 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000648 AddHandler("optional", &CollectOptionProperties::onOptional);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000649 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000650 AddHandler("forward_not_split",
651 &CollectOptionProperties::onForwardNotSplit);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000652
653 staticMembersInitialized_ = true;
654 }
655 }
656
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000657 /// operator() - Just forwards to the corresponding property
658 /// handler.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000659 void operator() (Init* I) {
660 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000661 }
662
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000663private:
664
665 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000666 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000667
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000668 void onHelp (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000669 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000670 optDesc_.Help = EscapeQuotes(InitPtrToString(d.getArg(0)));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000671 }
672
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000673 void onHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000674 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000675 optDesc_.setHidden();
676 }
677
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000678 void onReallyHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000679 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000680 optDesc_.setReallyHidden();
681 }
682
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000683 void onCommaSeparated (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000684 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000685 if (!optDesc_.isParameterList())
686 throw "'comma_separated' is valid only on parameter list options!";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000687 optDesc_.setCommaSeparated();
688 }
689
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000690 void onForwardNotSplit (const DagInit& d) {
691 CheckNumberOfArguments(d, 0);
692 if (!optDesc_.isParameter())
693 throw "'forward_not_split' is valid only for parameter options!";
694 optDesc_.setForwardNotSplit();
695 }
696
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000697 void onRequired (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000698 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000699
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000700 optDesc_.setRequired();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000701 optDesc_.CheckConsistency();
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000702 }
703
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000704 void onInit (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000705 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000706 Init* i = d.getArg(0);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000707 const std::string& str = i->getAsString();
708
709 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
710 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
711
712 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000713 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000714
715 optDesc_.InitVal = i;
716 }
717
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000718 void onOneOrMore (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000719 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000720
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000721 optDesc_.setOneOrMore();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000722 optDesc_.CheckConsistency();
723 }
724
725 void onZeroOrMore (const DagInit& d) {
726 CheckNumberOfArguments(d, 0);
727
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000728 if (optDesc_.isList())
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000729 llvm::errs() << "Warning: specifying the 'zero_or_more' property "
730 "on a list option has no effect.\n";
731
732 optDesc_.setZeroOrMore();
733 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000734 }
735
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000736 void onOptional (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000737 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000738
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000739 if (!optDesc_.isList())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000740 llvm::errs() << "Warning: specifying the 'optional' property"
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000741 "on a non-list option has no effect.\n";
742
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000743 optDesc_.setOptional();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000744 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000745 }
746
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000747 void onMultiVal (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000748 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000749 int val = InitPtrToInt(d.getArg(0));
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000750 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000751 throw "Error in the 'multi_val' property: "
752 "the value must be greater than 1!";
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000753 if (!optDesc_.isParameterList())
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000754 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000755 optDesc_.MultiVal = val;
756 }
757
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000758};
759
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000760/// AddOption - A function object that is applied to every option
761/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000762class AddOption {
763private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000764 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000765
766public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000767 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000768 {}
769
770 void operator()(const Init* i) {
771 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000772 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000773
774 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000775 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000776 const std::string& Name = InitPtrToString(d.getArg(0));
777
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000778 OptionDescription OD(Type, Name);
779
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000780 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000781
782 if (OD.isAlias()) {
783 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000784 OD.Help = InitPtrToString(d.getArg(1));
785 }
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000786 else {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000787 processOptionProperties(d, OD);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000788 }
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000789
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000790 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000791 }
792
793private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000794 /// processOptionProperties - Go through the list of option
795 /// properties and call a corresponding handler for each.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000796 static void processOptionProperties (const DagInit& d, OptionDescription& o) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000797 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000798 DagInit::const_arg_iterator B = d.arg_begin();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000799 // Skip the first argument: it's always the option name.
800 ++B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000801 std::for_each(B, d.arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000802 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000803
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000804};
805
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000806/// CollectOptionDescriptions - Collects option properties from all
807/// OptionLists.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000808void CollectOptionDescriptions (const RecordVector& V,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000809 OptionDescriptions& OptDescs)
810{
811 // For every OptionList:
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000812 for (RecordVector::const_iterator B = V.begin(),
813 E = V.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000814 // Throws an exception if the value does not exist.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000815 ListInit* PropList = (*B)->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000816
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000817 // For every option description in this list:
818 // collect the information and
819 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
820 }
821}
822
823// Tool information record
824
825namespace ToolFlags {
826 enum ToolFlags { Join = 0x1, Sink = 0x2 };
827}
828
829struct ToolDescription : public RefCountedBase<ToolDescription> {
830 std::string Name;
831 Init* CmdLine;
832 Init* Actions;
833 StrVector InLanguage;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000834 std::string InFileOption;
835 std::string OutFileOption;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000836 std::string OutLanguage;
837 std::string OutputSuffix;
838 unsigned Flags;
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000839 const Init* OnEmpty;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000840
841 // Various boolean properties
842 void setSink() { Flags |= ToolFlags::Sink; }
843 bool isSink() const { return Flags & ToolFlags::Sink; }
844 void setJoin() { Flags |= ToolFlags::Join; }
845 bool isJoin() const { return Flags & ToolFlags::Join; }
846
847 // Default ctor here is needed because StringMap can only store
848 // DefaultConstructible objects
Chris Lattner2d900582010-08-28 03:43:50 +0000849 ToolDescription (const std::string &n = "")
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000850 : Name(n), CmdLine(0), Actions(0), OutFileOption("-o"),
851 Flags(0), OnEmpty(0)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000852 {}
853};
854
855/// ToolDescriptions - A list of Tool information records.
856typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
857
858
859/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000860/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000861
862class CollectToolProperties;
863typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000864(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000865
866class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
867{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000868private:
869
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000870 /// toolDesc_ - Properties of the current Tool. This is where the
871 /// information is stored.
872 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000873
874public:
875
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000876 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000877 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000878 {
879 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000880
881 AddHandler("actions", &CollectToolProperties::onActions);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000882 AddHandler("command", &CollectToolProperties::onCommand);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000883 AddHandler("in_language", &CollectToolProperties::onInLanguage);
884 AddHandler("join", &CollectToolProperties::onJoin);
885 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000886
887 AddHandler("out_file_option", &CollectToolProperties::onOutFileOption);
888 AddHandler("in_file_option", &CollectToolProperties::onInFileOption);
889
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000890 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
891 AddHandler("sink", &CollectToolProperties::onSink);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000892 AddHandler("works_on_empty", &CollectToolProperties::onWorksOnEmpty);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000893
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000894 staticMembersInitialized_ = true;
895 }
896 }
897
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000898 void operator() (Init* I) {
899 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000900 }
901
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000902private:
903
904 /// Property handlers --
905 /// Functions that extract information about tool properties from
906 /// DAG representation.
907
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000908 void onActions (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000909 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000910 Init* Case = d.getArg(0);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000911 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000912 GetOperatorName(static_cast<DagInit&>(*Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000913 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000914 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000915 }
916
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000917 void onCommand (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000918 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000919 toolDesc_.CmdLine = d.getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000920 }
921
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000922 void onInLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000923 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000924 Init* arg = d.getArg(0);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000925
926 // Find out the argument's type.
927 if (typeid(*arg) == typeid(StringInit)) {
928 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000929 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000930 }
931 else {
932 // It's a list.
933 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000934 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000935
936 // Copy strings to the output vector.
937 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
938 B != E; ++B) {
939 out.push_back(InitPtrToString(*B));
940 }
941
942 // Remove duplicates.
943 std::sort(out.begin(), out.end());
944 StrVector::iterator newE = std::unique(out.begin(), out.end());
945 out.erase(newE, out.end());
946 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000947 }
948
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000949 void onJoin (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000950 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000951 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000952 }
953
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000954 void onOutLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000955 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000956 toolDesc_.OutLanguage = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000957 }
958
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000959 void onOutFileOption (const DagInit& d) {
960 CheckNumberOfArguments(d, 1);
961 toolDesc_.OutFileOption = InitPtrToString(d.getArg(0));
962 }
963
964 void onInFileOption (const DagInit& d) {
965 CheckNumberOfArguments(d, 1);
966 toolDesc_.InFileOption = InitPtrToString(d.getArg(0));
967 }
968
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000969 void onOutputSuffix (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000970 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000971 toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000972 }
973
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000974 void onSink (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000975 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000976 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000977 }
978
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000979 void onWorksOnEmpty (const DagInit& d) {
980 toolDesc_.OnEmpty = d.getArg(0);
981 }
982
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000983};
984
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000985/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000986/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000987/// CollectToolProperties function object).
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000988void CollectToolDescriptions (const RecordVector& Tools,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000989 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000990{
991 // Iterate over a properties list of every Tool definition
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000992 for (RecordVector::const_iterator B = Tools.begin(),
993 E = Tools.end(); B!=E; ++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000994 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000995 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000996 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000997
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000998 IntrusiveRefCntPtr<ToolDescription>
999 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001000
1001 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001002 CollectToolProperties(*ToolDesc));
1003 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001004 }
1005}
1006
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001007/// FillInEdgeVector - Merge all compilation graph definitions into
1008/// one single edge list.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001009void FillInEdgeVector(const RecordVector& CompilationGraphs,
1010 DagVector& Out) {
1011 for (RecordVector::const_iterator B = CompilationGraphs.begin(),
1012 E = CompilationGraphs.end(); B != E; ++B) {
1013 const ListInit* Edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +00001014
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001015 for (ListInit::const_iterator B = Edges->begin(),
1016 E = Edges->end(); B != E; ++B) {
1017 Out.push_back(&InitPtrToDag(*B));
1018 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001019 }
1020}
1021
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001022/// NotInGraph - Helper function object for FilterNotInGraph.
1023struct NotInGraph {
1024private:
1025 const llvm::StringSet<>& ToolsInGraph_;
1026
1027public:
1028 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
1029 : ToolsInGraph_(ToolsInGraph)
1030 {}
1031
1032 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
1033 return (ToolsInGraph_.count(x->Name) == 0);
1034 }
1035};
1036
1037/// FilterNotInGraph - Filter out from ToolDescs all Tools not
1038/// mentioned in the compilation graph definition.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001039void FilterNotInGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001040 ToolDescriptions& ToolDescs) {
1041
1042 // List all tools mentioned in the graph.
1043 llvm::StringSet<> ToolsInGraph;
1044
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001045 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001046 E = EdgeVector.end(); B != E; ++B) {
1047
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001048 const DagInit* Edge = *B;
1049 const std::string& NodeA = InitPtrToString(Edge->getArg(0));
1050 const std::string& NodeB = InitPtrToString(Edge->getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001051
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001052 if (NodeA != "root")
1053 ToolsInGraph.insert(NodeA);
1054 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001055 }
1056
1057 // Filter ToolPropertiesList.
1058 ToolDescriptions::iterator new_end =
1059 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
1060 NotInGraph(ToolsInGraph));
1061 ToolDescs.erase(new_end, ToolDescs.end());
1062}
1063
1064/// FillInToolToLang - Fills in two tables that map tool names to
1065/// (input, output) languages. Helper function used by TypecheckGraph().
1066void FillInToolToLang (const ToolDescriptions& ToolDescs,
1067 StringMap<StringSet<> >& ToolToInLang,
1068 StringMap<std::string>& ToolToOutLang) {
1069 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1070 E = ToolDescs.end(); B != E; ++B) {
1071 const ToolDescription& D = *(*B);
1072 for (StrVector::const_iterator B = D.InLanguage.begin(),
1073 E = D.InLanguage.end(); B != E; ++B)
1074 ToolToInLang[D.Name].insert(*B);
1075 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001076 }
1077}
1078
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001079/// TypecheckGraph - Check that names for output and input languages
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00001080/// on all edges do match.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001081void TypecheckGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001082 const ToolDescriptions& ToolDescs) {
1083 StringMap<StringSet<> > ToolToInLang;
1084 StringMap<std::string> ToolToOutLang;
1085
1086 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
1087 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
1088 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
1089
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001090 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001091 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001092 const DagInit* Edge = *B;
1093 const std::string& NodeA = InitPtrToString(Edge->getArg(0));
1094 const std::string& NodeB = InitPtrToString(Edge->getArg(1));
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001095 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
1096 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001097
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001098 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001099 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001100 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001101 + ": output->input language mismatch";
1102 }
1103
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001104 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001105 throw "Edges back to the root are not allowed!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001106 }
1107}
1108
1109/// WalkCase - Walks the 'case' expression DAG and invokes
1110/// TestCallback on every test, and StatementCallback on every
1111/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001112/// combinators (that is, they are passed directly to TestCallback).
1113/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1114/// IndentLevel, bool FirstTest)'.
1115/// StatementCallback must have type 'void StatementCallback(const Init*,
1116/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001117template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001118void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1119 unsigned IndentLevel = 0)
1120{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001121 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001122
1123 // Error checks.
1124 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001125 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001126
1127 if (d.getNumArgs() < 2)
1128 throw "There should be at least one clause in the 'case' expression:\n"
1129 + d.getAsString();
1130
1131 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001132 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001133 const unsigned numArgs = d.getNumArgs();
1134 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001135 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1136 B != E; ++B) {
1137 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001138
1139 if (!even)
1140 {
1141 // Handle test.
1142 const DagInit& Test = InitPtrToDag(arg);
1143
1144 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001145 throw "The 'default' clause should be the last in the "
1146 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001147 if (i == numArgs)
1148 throw "Case construct handler: no corresponding action "
1149 "found for the test " + Test.getAsString() + '!';
1150
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001151 TestCallback(Test, IndentLevel, (i == 1));
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001152 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001153 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001154 {
1155 if (dynamic_cast<DagInit*>(arg)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001156 && GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001157 // Nested 'case'.
1158 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1159 }
1160
1161 // Handle statement.
1162 StatementCallback(arg, IndentLevel);
1163 }
1164
1165 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001166 even = !even;
1167 }
1168}
1169
1170/// ExtractOptionNames - A helper function object used by
1171/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1172class ExtractOptionNames {
1173 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001174
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001175 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001176 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001177 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001178 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001179 ActionName == "forward_value" ||
1180 ActionName == "forward_transformed_value" ||
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001181 ActionName == "switch_on" || ActionName == "any_switch_on" ||
1182 ActionName == "parameter_equals" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001183 ActionName == "element_in_list" || ActionName == "not_empty" ||
1184 ActionName == "empty") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001185 CheckNumberOfArguments(Stmt, 1);
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001186
1187 Init* Arg = Stmt.getArg(0);
1188 if (typeid(*Arg) == typeid(StringInit)) {
1189 const std::string& Name = InitPtrToString(Arg);
1190 OptionNames_.insert(Name);
1191 }
1192 else {
1193 // It's a list.
1194 const ListInit& List = InitPtrToList(Arg);
1195 for (ListInit::const_iterator B = List.begin(), E = List.end();
1196 B != E; ++B) {
1197 const std::string& Name = InitPtrToString(*B);
1198 OptionNames_.insert(Name);
1199 }
1200 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001201 }
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001202 else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001203 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001204 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001205 }
1206 }
1207 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001208
1209public:
1210 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1211 {}
1212
1213 void operator()(const Init* Statement) {
1214 if (typeid(*Statement) == typeid(ListInit)) {
1215 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1216 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1217 B != E; ++B)
1218 this->processDag(*B);
1219 }
1220 else {
1221 this->processDag(Statement);
1222 }
1223 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001224
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001225 void operator()(const DagInit& Test, unsigned, bool) {
1226 this->operator()(&Test);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001227 }
1228 void operator()(const Init* Statement, unsigned) {
1229 this->operator()(Statement);
1230 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001231};
1232
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00001233/// IsOptionalEdge - Validate that the 'optional_edge' has proper structure.
1234bool IsOptionalEdge (const DagInit& Edg) {
1235 return (GetOperatorName(Edg) == "optional_edge") && (Edg.getNumArgs() > 2);
1236}
1237
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001238/// CheckForSuperfluousOptions - Check that there are no side
1239/// effect-free options (specified only in the OptionList). Otherwise,
1240/// output a warning.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001241void CheckForSuperfluousOptions (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001242 const ToolDescriptions& ToolDescs,
1243 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001244 llvm::StringSet<> nonSuperfluousOptions;
1245
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001246 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001247 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001248 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1249 E = ToolDescs.end(); B != E; ++B) {
1250 const ToolDescription& TD = *(*B);
1251 ExtractOptionNames Callback(nonSuperfluousOptions);
1252 if (TD.Actions)
1253 WalkCase(TD.Actions, Callback, Callback);
1254 }
1255
1256 // Add all options mentioned in the 'case' clauses of the
1257 // OptionalEdges of the compilation graph to the set of
1258 // non-superfluous options.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001259 for (DagVector::const_iterator B = EdgeVector.begin(),
1260 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00001261 const DagInit& Edge = **B;
1262 if (IsOptionalEdge(Edge)) {
1263 const DagInit& Weight = InitPtrToDag(Edge.getArg(2));
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001264 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001265 }
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001266 }
1267
1268 // Check that all options in OptDescs belong to the set of
1269 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001270 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001271 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001272 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001273 if (!nonSuperfluousOptions.count(Val.Name)
1274 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001275 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001276 "Probable cause: this option is specified only in the OptionList.\n";
1277 }
1278}
1279
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001280/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1281bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1282 if (TestName == "single_input_file") {
1283 O << "InputFilenames.size() == 1";
1284 return true;
1285 }
1286 else if (TestName == "multiple_input_files") {
1287 O << "InputFilenames.size() > 1";
1288 return true;
1289 }
1290
1291 return false;
1292}
1293
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001294/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1295template <typename F>
1296void EmitListTest(const ListInit& L, const char* LogicOp,
1297 F Callback, raw_ostream& O)
1298{
1299 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1300 // of Dags...
1301 bool isFirst = true;
1302 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1303 if (isFirst)
1304 isFirst = false;
1305 else
Mikhail Glushenkovb7935e02010-01-01 04:40:54 +00001306 O << ' ' << LogicOp << ' ';
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001307 Callback(InitPtrToString(*B), O);
1308 }
1309}
1310
1311// Callbacks for use with EmitListTest.
1312
1313class EmitSwitchOn {
1314 const OptionDescriptions& OptDescs_;
1315public:
1316 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1317 {}
1318
1319 void operator()(const std::string& OptName, raw_ostream& O) const {
1320 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1321 O << OptDesc.GenVariableName();
1322 }
1323};
1324
1325class EmitEmptyTest {
1326 bool EmitNegate_;
1327 const OptionDescriptions& OptDescs_;
1328public:
1329 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1330 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1331 {}
1332
1333 void operator()(const std::string& OptName, raw_ostream& O) const {
1334 const char* Neg = (EmitNegate_ ? "!" : "");
1335 if (OptName == "o") {
1336 O << Neg << "OutputFilename.empty()";
1337 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001338 else if (OptName == "save-temps") {
1339 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1340 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001341 else {
1342 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1343 O << Neg << OptDesc.GenVariableName() << ".empty()";
1344 }
1345 }
1346};
1347
1348
1349/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1350bool EmitCaseTest1ArgList(const std::string& TestName,
1351 const DagInit& d,
1352 const OptionDescriptions& OptDescs,
1353 raw_ostream& O) {
Mikhail Glushenkov3a481e32010-01-01 03:50:51 +00001354 const ListInit& L = InitPtrToList(d.getArg(0));
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001355
1356 if (TestName == "any_switch_on") {
1357 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1358 return true;
1359 }
1360 else if (TestName == "switch_on") {
1361 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1362 return true;
1363 }
1364 else if (TestName == "any_not_empty") {
1365 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1366 return true;
1367 }
1368 else if (TestName == "any_empty") {
1369 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1370 return true;
1371 }
1372 else if (TestName == "not_empty") {
1373 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1374 return true;
1375 }
1376 else if (TestName == "empty") {
1377 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1378 return true;
1379 }
1380
1381 return false;
1382}
1383
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001384/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1385bool EmitCaseTest1ArgStr(const std::string& TestName,
1386 const DagInit& d,
1387 const OptionDescriptions& OptDescs,
1388 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001389 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001390
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001391 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001392 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001393 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001394 }
1395 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001396 O << "InLangs.count(\"" << OptName << "\") != 0";
1397 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001398 }
1399 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001400 // This works only for single-argument Tool::GenerateAction. Join
1401 // tools can process several files in different languages simultaneously.
1402
1403 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001404 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001405 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001406 }
1407 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001408 bool EmitNegate = (TestName == "not_empty");
1409 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001410 return true;
1411 }
1412
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001413 return false;
1414}
1415
1416/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1417bool EmitCaseTest1Arg(const std::string& TestName,
1418 const DagInit& d,
1419 const OptionDescriptions& OptDescs,
1420 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001421 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001422 if (typeid(*d.getArg(0)) == typeid(ListInit))
1423 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1424 else
1425 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1426}
1427
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001428/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001429bool EmitCaseTest2Args(const std::string& TestName,
1430 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001431 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001432 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001433 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001434 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001435 const std::string& OptName = InitPtrToString(d.getArg(0));
1436 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001437
1438 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001439 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001440 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1441 return true;
1442 }
1443 else if (TestName == "element_in_list") {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00001444 const OptionDescription& OptDesc = OptDescs.FindParameterList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001445 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001446 O << "std::find(" << VarName << ".begin(),\n";
1447 O.indent(IndentLevel + Indent1)
1448 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001449 << OptArg << "\") != " << VarName << ".end()";
1450 return true;
1451 }
1452
1453 return false;
1454}
1455
1456// Forward declaration.
1457// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001458void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001459 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001460 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001461
1462/// EmitLogicalOperationTest - Helper function used by
1463/// EmitCaseConstructHandler.
1464void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001465 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001466 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001467 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001468 O << '(';
1469 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001470 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001471 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001472 if (j != NumArgs - 1) {
1473 O << ")\n";
1474 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1475 }
1476 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001477 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001478 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001479 }
1480}
1481
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001482void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001483 const OptionDescriptions& OptDescs, raw_ostream& O)
1484{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001485 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001486 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1487 O << "! (";
1488 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1489 O << ")";
1490}
1491
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001492/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001493void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001494 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001495 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001496 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001497
1498 if (TestName == "and")
1499 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1500 else if (TestName == "or")
1501 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001502 else if (TestName == "not")
1503 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001504 else if (EmitCaseTest0Args(TestName, O))
1505 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001506 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1507 return;
1508 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1509 return;
1510 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001511 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001512}
1513
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001514
1515/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1516class EmitCaseTestCallback {
1517 bool EmitElseIf_;
1518 const OptionDescriptions& OptDescs_;
1519 raw_ostream& O_;
1520public:
1521
1522 EmitCaseTestCallback(bool EmitElseIf,
1523 const OptionDescriptions& OptDescs, raw_ostream& O)
1524 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1525 {}
1526
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001527 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001528 {
1529 if (GetOperatorName(Test) == "default") {
1530 O_.indent(IndentLevel) << "else {\n";
1531 }
1532 else {
1533 O_.indent(IndentLevel)
1534 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001535 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001536 O_ << ") {\n";
1537 }
1538 }
1539};
1540
1541/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1542template <typename F>
1543class EmitCaseStatementCallback {
1544 F Callback_;
1545 raw_ostream& O_;
1546public:
1547
1548 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1549 : Callback_(Callback), O_(O)
1550 {}
1551
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001552 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001553
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001554 // Ignore nested 'case' DAG.
1555 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001556 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case")) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001557 if (typeid(*Statement) == typeid(ListInit)) {
1558 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1559 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1560 B != E; ++B)
1561 Callback_(*B, (IndentLevel + Indent1), O_);
1562 }
1563 else {
1564 Callback_(Statement, (IndentLevel + Indent1), O_);
1565 }
1566 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001567 O_.indent(IndentLevel) << "}\n";
1568 }
1569
1570};
1571
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001572/// EmitCaseConstructHandler - Emit code that handles the 'case'
1573/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001574/// clause. Implemented on top of WalkCase.
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00001575/// Callback's type is void F(const Init* Statement, unsigned IndentLevel,
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001576/// raw_ostream& O).
1577/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001578/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1579/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001580template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001581void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001582 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001583 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001584 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001585 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1586 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001587}
1588
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001589/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001590/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1591/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001592void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001593 const char* Delimiters = " \t\n\v\f\r";
1594 enum TokenizerState
1595 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1596 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001597
1598 if (CmdLine.empty())
1599 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001600 Out.push_back("");
1601
1602 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1603 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001604
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001605 for (; B != E; ++B) {
1606 char cur_ch = CmdLine[B];
1607
1608 switch (cur_st) {
1609 case Normal:
1610 if (cur_ch == '$') {
1611 cur_st = SpecialCommand;
1612 break;
1613 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001614 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001615 // Skip whitespace
1616 B = CmdLine.find_first_not_of(Delimiters, B);
1617 if (B == std::string::npos) {
1618 B = E-1;
1619 continue;
1620 }
1621 --B;
1622 Out.push_back("");
1623 continue;
1624 }
1625 break;
1626
1627
1628 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001629 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001630 cur_st = Normal;
1631 Out.push_back("");
1632 continue;
1633 }
1634 if (cur_ch == '(') {
1635 Out.push_back("");
1636 cur_st = InsideSpecialCommand;
1637 continue;
1638 }
1639 break;
1640
1641 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001642 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001643 continue;
1644 }
1645 if (cur_ch == '\'') {
1646 cur_st = InsideQuotationMarks;
1647 Out.push_back("");
1648 continue;
1649 }
1650 if (cur_ch == ')') {
1651 cur_st = Normal;
1652 Out.push_back("");
1653 }
1654 if (cur_ch == ',') {
1655 continue;
1656 }
1657
1658 break;
1659
1660 case InsideQuotationMarks:
1661 if (cur_ch == '\'') {
1662 cur_st = InsideSpecialCommand;
1663 continue;
1664 }
1665 break;
1666 }
1667
1668 Out.back().push_back(cur_ch);
1669 }
1670}
1671
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001672/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1673/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1674/// SubstituteSpecialCommands().
1675StrVector::const_iterator
1676SubstituteCall (StrVector::const_iterator Pos,
1677 StrVector::const_iterator End,
1678 bool IsJoin, raw_ostream& O)
1679{
1680 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001681 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001682 const std::string& CmdName = *Pos;
1683
1684 if (CmdName == ")")
1685 throw "$CALL invocation: empty argument list!";
1686
1687 O << "hooks::";
1688 O << CmdName << "(";
1689
1690
1691 bool firstIteration = true;
1692 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001693 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001694 const std::string& Arg = *Pos;
1695 assert(Arg.size() != 0);
1696
1697 if (Arg[0] == ')')
1698 break;
1699
1700 if (firstIteration)
1701 firstIteration = false;
1702 else
1703 O << ", ";
1704
1705 if (Arg == "$INFILE") {
1706 if (IsJoin)
1707 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1708 else
1709 O << "inFile.c_str()";
1710 }
1711 else {
1712 O << '"' << Arg << '"';
1713 }
1714 }
1715
1716 O << ')';
1717
1718 return Pos;
1719}
1720
1721/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1722/// function used by SubstituteSpecialCommands().
1723StrVector::const_iterator
1724SubstituteEnv (StrVector::const_iterator Pos,
1725 StrVector::const_iterator End, raw_ostream& O)
1726{
1727 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001728 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001729 const std::string& EnvName = *Pos;
1730
1731 if (EnvName == ")")
1732 throw "$ENV invocation: empty argument list!";
1733
1734 O << "checkCString(std::getenv(\"";
1735 O << EnvName;
1736 O << "\"))";
1737
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001738 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001739
1740 return Pos;
1741}
1742
1743/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1744/// handler code. Helper function used by EmitCmdLineVecFill().
1745StrVector::const_iterator
1746SubstituteSpecialCommands (StrVector::const_iterator Pos,
1747 StrVector::const_iterator End,
1748 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001749{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001750
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001751 const std::string& cmd = *Pos;
1752
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001753 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001754 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001755 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001756 }
1757 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001758 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001759 }
1760 else {
1761 throw "Unknown special command: " + cmd;
1762 }
1763
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001764 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001765 const std::string& Leftover = *Pos;
1766 assert(Leftover.at(0) == ')');
1767 if (Leftover.size() != 1)
1768 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001769
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001770 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001771}
1772
1773/// EmitCmdLineVecFill - Emit code that fills in the command line
1774/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001775void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001776 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001777 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001778 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001779 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001780
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001781 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001782 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001783
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001784 StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001785
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001786 // Emit the command itself.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001787 assert(!StrVec[0].empty());
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001788 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001789 if (StrVec[0][0] == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001790 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1791 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001792 }
1793 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001794 O << '"' << StrVec[0] << '"';
1795 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001796 }
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001797 O << ";\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001798
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001799 // Go through the command arguments.
1800 assert(B <= E);
1801 for (; B != E; ++B) {
1802 const std::string& cmd = *B;
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001803
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001804 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001805 O.indent(IndentLevel);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001806
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001807 if (cmd.at(0) == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001808 O << "vec.push_back(std::make_pair(0, ";
1809 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1810 O << "));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001811 }
1812 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001813 O << "vec.push_back(std::make_pair(0, \"" << cmd << "\"));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001814 }
1815 }
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001816
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001817}
1818
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001819/// EmitForEachListElementCycleHeader - Emit common code for iterating through
1820/// all elements of a list. Helper function used by
1821/// EmitForwardOptionPropertyHandlingCode.
1822void EmitForEachListElementCycleHeader (const OptionDescription& D,
1823 unsigned IndentLevel,
1824 raw_ostream& O) {
1825 unsigned IndentLevel1 = IndentLevel + Indent1;
1826
1827 O.indent(IndentLevel)
1828 << "for (" << D.GenTypeDeclaration()
1829 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1830 O.indent(IndentLevel)
1831 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1832 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1833 << ".getPosition(B - " << D.GenVariableName()
1834 << ".begin());\n";
1835}
1836
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001837/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1838/// implement EmitActionHandler. Emits code for
1839/// handling the (forward) and (forward_as) option properties.
1840void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001841 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001842 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001843 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001844 const std::string& Name = NewName.empty()
1845 ? ("-" + D.Name)
1846 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001847 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001848
1849 switch (D.Type) {
1850 case OptionType::Switch:
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001851 O.indent(IndentLevel)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001852 << "vec.push_back(std::make_pair(" << D.GenVariableName()
1853 << ".getPosition(), \"" << Name << "\"));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001854 break;
1855 case OptionType::Parameter:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001856 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1857 << D.GenVariableName()
1858 <<".getPosition(), \"" << Name;
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001859
1860 if (!D.isForwardNotSplit()) {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001861 O << "\"));\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001862 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1863 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001864 << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001865 }
1866 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001867 O << "=\" + " << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001868 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001869 break;
1870 case OptionType::Prefix:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001871 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1872 << D.GenVariableName() << ".getPosition(), \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001873 << Name << "\" + "
1874 << D.GenVariableName() << "));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001875 break;
1876 case OptionType::PrefixList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001877 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001878 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001879 << Name << "\" + " << "*B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001880 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001881
1882 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001883 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001884 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001885 }
1886
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001887 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001888 break;
1889 case OptionType::ParameterList:
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 << "\"));\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001893
1894 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001895 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001896 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001897 }
1898
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001899 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001900 break;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001901 case OptionType::SwitchList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001902 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001903 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
1904 << Name << "\"));\n";
1905 O.indent(IndentLevel1) << "++B;\n";
1906 O.indent(IndentLevel) << "}\n";
1907 break;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001908 case OptionType::Alias:
1909 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001910 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001911 }
1912}
1913
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001914/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1915/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001916struct ActionHandlingCallbackBase
1917{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001918
1919 void onErrorDag(const DagInit& d,
1920 unsigned IndentLevel, raw_ostream& O) const
1921 {
1922 O.indent(IndentLevel)
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00001923 << "PrintError(\""
1924 << (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0)) : "Unknown error!")
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001925 << "\");\n";
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +00001926 O.indent(IndentLevel) << "return 1;\n";
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001927 }
1928
1929 void onWarningDag(const DagInit& d,
1930 unsigned IndentLevel, raw_ostream& O) const
1931 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001932 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001933 O.indent(IndentLevel) << "llvm::errs() << \""
1934 << InitPtrToString(d.getArg(0)) << "\";\n";
1935 }
1936
1937};
1938
1939/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1940/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001941class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001942
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001943typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1944(const DagInit&, unsigned, raw_ostream&) const;
1945
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001946class EmitActionHandlersCallback :
1947 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001948 public HandlerTable<EmitActionHandlersCallbackHandler>
1949{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001950 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001951
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001952 const OptionDescriptions& OptDescs;
1953
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001954 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1955 /// onAppendCmd and onOutputSuffix.
1956 void EmitHookInvocation(const std::string& Str,
1957 const char* BlockOpen, const char* BlockClose,
1958 unsigned IndentLevel, raw_ostream& O) const
1959 {
1960 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001961 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001962
1963 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1964 B != E; ++B) {
1965 const std::string& cmd = *B;
1966
1967 O.indent(IndentLevel) << BlockOpen;
1968
1969 if (cmd.at(0) == '$')
1970 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1971 else
1972 O << '"' << cmd << '"';
1973
1974 O << BlockClose;
1975 }
1976 }
1977
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001978 void onAppendCmd (const DagInit& Dag,
1979 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001980 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001981 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001982 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001983 "vec.push_back(std::make_pair(65536, ", "));\n",
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001984 IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001985 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001986
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001987 void onForward (const DagInit& Dag,
1988 unsigned IndentLevel, raw_ostream& O) const
1989 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001990 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001991 const std::string& Name = InitPtrToString(Dag.getArg(0));
1992 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1993 IndentLevel, "", O);
1994 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001995
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001996 void onForwardAs (const DagInit& Dag,
1997 unsigned IndentLevel, raw_ostream& O) const
1998 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001999 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002000 const std::string& Name = InitPtrToString(Dag.getArg(0));
2001 const std::string& NewName = InitPtrToString(Dag.getArg(1));
2002 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2003 IndentLevel, NewName, O);
2004 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002005
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002006 void onForwardValue (const DagInit& Dag,
2007 unsigned IndentLevel, raw_ostream& O) const
2008 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002009 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002010 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002011 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002012
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002013 if (D.isSwitchList()) {
2014 throw std::runtime_error
2015 ("forward_value is not allowed with switch_list");
2016 }
2017
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002018 if (D.isParameter()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002019 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2020 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002021 << D.GenVariableName() << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002022 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00002023 else {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002024 O.indent(IndentLevel) << "for (" << D.GenTypeDeclaration()
2025 << "::iterator B = " << D.GenVariableName()
2026 << ".begin(), \n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002027 O.indent(IndentLevel + Indent1) << " E = " << D.GenVariableName()
2028 << ".end(); B != E; ++B)\n";
2029 O.indent(IndentLevel) << "{\n";
2030 O.indent(IndentLevel + Indent1)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002031 << "unsigned pos = " << D.GenVariableName()
2032 << ".getPosition(B - " << D.GenVariableName()
2033 << ".begin());\n";
2034 O.indent(IndentLevel + Indent1)
2035 << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002036 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002037 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002038 }
2039
2040 void onForwardTransformedValue (const DagInit& Dag,
2041 unsigned IndentLevel, raw_ostream& O) const
2042 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002043 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002044 const std::string& Name = InitPtrToString(Dag.getArg(0));
2045 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002046 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002047
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002048 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2049 << D.GenVariableName() << ".getPosition("
2050 << (D.isList() ? "0" : "") << "), "
2051 << "hooks::" << Hook << "(" << D.GenVariableName()
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002052 << (D.isParameter() ? ".c_str()" : "") << ")));\n";
2053 }
2054
2055 void onNoOutFile (const DagInit& Dag,
2056 unsigned IndentLevel, raw_ostream& O) const
2057 {
2058 CheckNumberOfArguments(Dag, 0);
2059 O.indent(IndentLevel) << "no_out_file = true;\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002060 }
2061
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002062 void onOutputSuffix (const DagInit& Dag,
2063 unsigned IndentLevel, raw_ostream& O) const
2064 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002065 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002066 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
2067 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002068 }
2069
2070 void onStopCompilation (const DagInit& Dag,
2071 unsigned IndentLevel, raw_ostream& O) const
2072 {
2073 O.indent(IndentLevel) << "stop_compilation = true;\n";
2074 }
2075
2076
2077 void onUnpackValues (const DagInit& Dag,
2078 unsigned IndentLevel, raw_ostream& O) const
2079 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002080 throw "'unpack_values' is deprecated. "
2081 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002082 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002083
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002084 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002085
2086 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
2087 : OptDescs(OD)
2088 {
2089 if (!staticMembersInitialized_) {
2090 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
2091 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
2092 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
2093 AddHandler("forward", &EmitActionHandlersCallback::onForward);
2094 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002095 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
2096 AddHandler("forward_transformed_value",
2097 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002098 AddHandler("no_out_file",
2099 &EmitActionHandlersCallback::onNoOutFile);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002100 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
2101 AddHandler("stop_compilation",
2102 &EmitActionHandlersCallback::onStopCompilation);
2103 AddHandler("unpack_values",
2104 &EmitActionHandlersCallback::onUnpackValues);
2105
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002106
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002107 staticMembersInitialized_ = true;
2108 }
2109 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002110
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002111 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002112 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002113 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002114 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002115 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002116};
2117
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002118void EmitGenerateActionMethodHeader(const ToolDescription& D,
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002119 bool IsJoin, bool Naked,
2120 raw_ostream& O)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002121{
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002122 O.indent(Indent1) << "int GenerateAction(Action& Out,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002123
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002124 if (IsJoin)
2125 O.indent(Indent2) << "const PathVector& inFiles,\n";
2126 else
2127 O.indent(Indent2) << "const sys::Path& inFile,\n";
2128
2129 O.indent(Indent2) << "const bool HasChildren,\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002130 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2131 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2132 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2133 O.indent(Indent1) << "{\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002134
2135 if (!Naked) {
2136 O.indent(Indent2) << "std::string cmd;\n";
2137 O.indent(Indent2) << "std::string out_file;\n";
2138 O.indent(Indent2)
2139 << "std::vector<std::pair<unsigned, std::string> > vec;\n";
2140 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2141 O.indent(Indent2) << "bool no_out_file = false;\n";
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002142 O.indent(Indent2) << "std::string output_suffix(\""
2143 << D.OutputSuffix << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002144 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002145}
2146
2147// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2148// Tool::GenerateAction() method.
2149void EmitGenerateActionMethod (const ToolDescription& D,
2150 const OptionDescriptions& OptDescs,
2151 bool IsJoin, raw_ostream& O) {
2152
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002153 EmitGenerateActionMethodHeader(D, IsJoin, /* Naked = */ false, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002154
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002155 if (!D.CmdLine)
2156 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002157
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002158 // Process the 'command' property.
2159 O << '\n';
2160 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2161 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002162
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002163 // Process the 'actions' list of this tool.
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002164 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002165 EmitCaseConstructHandler(D.Actions, Indent2,
2166 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002167 false, OptDescs, O);
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002168 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002169
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002170 // Input file (s)
2171 if (!D.InFileOption.empty()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002172 O.indent(Indent2)
2173 << "vec.push_back(std::make_pair(InputFilenames.getPosition(0), \""
2174 << D.InFileOption << "\");\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002175 }
2176
2177 if (IsJoin) {
2178 O.indent(Indent2)
2179 << "for (PathVector::const_iterator B = inFiles.begin(),\n";
2180 O.indent(Indent3) << "E = inFiles.end(); B != E; ++B)\n";
2181 O.indent(Indent2) << "{\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002182 O.indent(Indent3) << "vec.push_back(std::make_pair("
2183 << "InputFilenames.getPosition(B - inFiles.begin()), "
2184 << "B->str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002185 O.indent(Indent2) << "}\n";
2186 }
2187 else {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002188 O.indent(Indent2) << "vec.push_back(std::make_pair("
2189 << "InputFilenames.getPosition(0), inFile.str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002190 }
2191
2192 // Output file
2193 O.indent(Indent2) << "if (!no_out_file) {\n";
2194 if (!D.OutFileOption.empty())
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002195 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002196 << D.OutFileOption << "\"));\n";
2197
2198 O.indent(Indent3) << "out_file = this->OutFilename("
2199 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002200 O.indent(Indent4) <<
2201 "TempDir, stop_compilation, output_suffix.c_str()).str();\n\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002202 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, out_file));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002203
2204 O.indent(Indent2) << "}\n\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002205
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002206 // Handle the Sink property.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002207 std::string SinkOption("autogenerated::");
2208 SinkOption += SinkOptionName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002209 if (D.isSink()) {
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002210 O.indent(Indent2) << "if (!" << SinkOption << ".empty()) {\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002211 O.indent(Indent3) << "for (cl::list<std::string>::iterator B = "
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002212 << SinkOption << ".begin(), E = " << SinkOption
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002213 << ".end(); B != E; ++B)\n";
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002214 O.indent(Indent4) << "vec.push_back(std::make_pair(" << SinkOption
2215 << ".getPosition(B - " << SinkOption
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002216 << ".begin()), *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002217 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002218 }
2219
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002220 O.indent(Indent2) << "Out.Construct(cmd, this->SortArgs(vec), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002221 << "stop_compilation, out_file);\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002222 O.indent(Indent2) << "return 0;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002223 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002224}
2225
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002226/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2227/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002228void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2229 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002230 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002231 if (!ToolDesc.isJoin()) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002232 EmitGenerateActionMethodHeader(ToolDesc, /* IsJoin = */ true,
2233 /* Naked = */ true, O);
2234 O.indent(Indent2) << "PrintError(\"" << ToolDesc.Name
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002235 << " is not a Join tool!\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002236 O.indent(Indent2) << "return -1;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002237 O.indent(Indent1) << "}\n\n";
2238 }
2239 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002240 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002241 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002242
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002243 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002244}
2245
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002246/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2247/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002248void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002249 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2250 O.indent(Indent2) << "return InputLanguages_;\n";
2251 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002252
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002253 if (D.OutLanguage.empty())
2254 throw "Tool " + D.Name + " has no 'out_language' property!";
2255
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002256 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2257 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2258 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002259}
2260
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002261/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002262void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002263 O.indent(Indent1) << "const char* Name() const {\n";
2264 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2265 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002266}
2267
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002268/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2269/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002270void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002271 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002272 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002273 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002274 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002275 O.indent(Indent2) << "return false;\n";
2276 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002277}
2278
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002279/// EmitWorksOnEmptyCallback - Callback used by EmitWorksOnEmptyMethod in
2280/// conjunction with EmitCaseConstructHandler.
2281void EmitWorksOnEmptyCallback (const Init* Value,
2282 unsigned IndentLevel, raw_ostream& O) {
2283 CheckBooleanConstant(Value);
2284 O.indent(IndentLevel) << "return " << Value->getAsString() << ";\n";
2285}
2286
2287/// EmitWorksOnEmptyMethod - Emit the WorksOnEmpty() method for a given Tool
2288/// class.
2289void EmitWorksOnEmptyMethod (const ToolDescription& D,
2290 const OptionDescriptions& OptDescs,
2291 raw_ostream& O)
2292{
2293 O.indent(Indent1) << "bool WorksOnEmpty() const {\n";
2294 if (D.OnEmpty == 0)
2295 O.indent(Indent2) << "return false;\n";
2296 else
2297 EmitCaseConstructHandler(D.OnEmpty, Indent2, EmitWorksOnEmptyCallback,
2298 /*EmitElseIf = */ true, OptDescs, O);
2299 O.indent(Indent1) << "}\n\n";
2300}
2301
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002302/// EmitStaticMemberDefinitions - Emit static member definitions for a
2303/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002304void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002305 if (D.InLanguage.empty())
2306 throw "Tool " + D.Name + " has no 'in_language' property!";
2307
2308 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2309 for (StrVector::const_iterator B = D.InLanguage.begin(),
2310 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002311 O << '\"' << *B << "\", ";
2312 O << "0};\n\n";
2313}
2314
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002315/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002316void EmitToolClassDefinition (const ToolDescription& D,
2317 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002318 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002319 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002320 return;
2321
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002322 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002323 O << "class " << D.Name << " : public ";
2324 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002325 O << "JoinTool";
2326 else
2327 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002328
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002329 O << " {\nprivate:\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002330 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002331
2332 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002333 EmitNameMethod(D, O);
2334 EmitInOutLanguageMethods(D, O);
2335 EmitIsJoinMethod(D, O);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002336 EmitWorksOnEmptyMethod(D, OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002337 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002338
2339 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002340 O << "};\n";
2341
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002342 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002343
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002344}
2345
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002346/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002347/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002348void EmitOptionDefinitions (const OptionDescriptions& descs,
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002349 bool HasSink, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002350{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002351 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002352
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002353 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002354 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002355 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002356 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002357
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002358 if (val.Type == OptionType::Alias) {
2359 Aliases.push_back(val);
2360 continue;
2361 }
2362
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002363 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002364 << val.GenPlainVariableName();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002365
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002366 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002367
2368 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2369 O << ", cl::Prefix";
2370
2371 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002372 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002373 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002374 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002375 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002376 }
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002377
2378 if (val.isOptional())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002379 O << ", cl::Optional";
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002380
2381 if (val.isOneOrMore())
2382 O << ", cl::OneOrMore";
2383
2384 if (val.isZeroOrMore())
2385 O << ", cl::ZeroOrMore";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002386
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002387 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002388 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002389 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002390 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002391
2392 if (val.isCommaSeparated())
2393 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002394
2395 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002396 O << ", cl::multi_val(" << val.MultiVal << ')';
2397
2398 if (val.InitVal) {
2399 const std::string& str = val.InitVal->getAsString();
2400 O << ", cl::init(" << str << ')';
2401 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002402
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002403 if (!val.Help.empty())
2404 O << ", cl::desc(\"" << val.Help << "\")";
2405
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002406 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002407 }
2408
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002409 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002410 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002411 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002412 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002413
2414 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov297514d2010-08-20 18:16:26 +00002415 << val.GenPlainVariableName()
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002416 << "(\"" << val.Name << '\"';
2417
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002418 const OptionDescription& D = descs.FindOption(val.Help);
2419 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002420
2421 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2422 }
2423
2424 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002425 if (HasSink)
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002426 O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002427
2428 O << '\n';
2429}
2430
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002431/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002432/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002433
2434class EmitPreprocessOptionsCallback;
2435
2436typedef void
2437(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2438(const DagInit&, unsigned, raw_ostream&) const;
2439
2440class EmitPreprocessOptionsCallback :
2441 public ActionHandlingCallbackBase,
2442 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2443{
2444 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002445 typedef void
2446 (EmitPreprocessOptionsCallback::* HandlerImpl)
2447 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002448
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002449 const OptionDescriptions& OptDescs_;
2450
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002451 void onListOrDag(const DagInit& d, HandlerImpl h,
2452 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002453 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002454 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002455 const Init* I = d.getArg(0);
2456
2457 // If I is a list, apply h to each element.
2458 if (typeid(*I) == typeid(ListInit)) {
2459 const ListInit& L = *static_cast<const ListInit*>(I);
2460 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B)
2461 ((this)->*(h))(*B, IndentLevel, O);
2462 }
2463 // Otherwise, apply h to I.
2464 else {
2465 ((this)->*(h))(I, IndentLevel, O);
2466 }
2467 }
2468
2469 void onUnsetOptionImpl(const Init* I,
2470 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002471 {
2472 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002473 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002474
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002475 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002476 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2477 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002478 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002479 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2480 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002481 else if (OptDesc.isList()) {
2482 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2483 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002484 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002485 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002486 }
2487 }
2488
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002489 void onUnsetOption(const DagInit& d,
2490 unsigned IndentLevel, raw_ostream& O) const
2491 {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002492 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2493 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002494 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002495
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002496 void onSetOptionImpl(const DagInit& d,
2497 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002498 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002499 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002500 const Init* Value = d.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002501 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2502
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002503 if (OptDesc.isList()) {
2504 const ListInit& List = InitPtrToList(Value);
2505
2506 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2507 for (ListInit::const_iterator B = List.begin(), E = List.end();
2508 B != E; ++B) {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002509 const Init* CurElem = *B;
2510 if (OptDesc.isSwitchList())
2511 CheckBooleanConstant(CurElem);
2512
2513 O.indent(IndentLevel)
2514 << OptDesc.GenVariableName() << ".push_back(\""
2515 << (OptDesc.isSwitchList() ? CurElem->getAsString()
2516 : InitPtrToString(CurElem))
2517 << "\");\n";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002518 }
2519 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002520 else if (OptDesc.isSwitch()) {
2521 CheckBooleanConstant(Value);
2522 O.indent(IndentLevel) << OptDesc.GenVariableName()
2523 << " = " << Value->getAsString() << ";\n";
2524 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002525 else if (OptDesc.isParameter()) {
2526 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002527 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002528 << " = \"" << Str << "\";\n";
2529 }
2530 else {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002531 throw "Can't apply 'set_option' to alias option -" + OptName + " !";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002532 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002533 }
2534
2535 void onSetSwitch(const Init* I,
2536 unsigned IndentLevel, raw_ostream& O) const {
2537 const std::string& OptName = InitPtrToString(I);
2538 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2539
2540 if (OptDesc.isSwitch())
2541 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2542 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002543 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002544 }
2545
2546 void onSetOption(const DagInit& d,
2547 unsigned IndentLevel, raw_ostream& O) const
2548 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002549 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002550
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002551 // Two arguments: (set_option "parameter", VALUE), where VALUE can be a
2552 // boolean, a string or a string list.
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002553 if (d.getNumArgs() > 1)
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002554 this->onSetOptionImpl(d, IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002555 // One argument: (set_option "switch")
2556 // or (set_option ["switch1", "switch2", ...])
2557 else
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002558 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2559 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002560 }
2561
2562public:
2563
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002564 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002565 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002566 {
2567 if (!staticMembersInitialized_) {
2568 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2569 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2570 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002571 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002572
2573 staticMembersInitialized_ = true;
2574 }
2575 }
2576
2577 void operator()(const Init* I,
2578 unsigned IndentLevel, raw_ostream& O) const
2579 {
2580 InvokeDagInitHandler(this, I, IndentLevel, O);
2581 }
2582
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002583};
2584
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002585/// EmitPreprocessOptions - Emit the PreprocessOptions() function.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002586void EmitPreprocessOptions (const RecordKeeper& Records,
2587 const OptionDescriptions& OptDecs, raw_ostream& O)
2588{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002589 O << "int PreprocessOptions () {\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002590
2591 const RecordVector& OptionPreprocessors =
2592 Records.getAllDerivedDefinitions("OptionPreprocessor");
2593
2594 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2595 E = OptionPreprocessors.end(); B!=E; ++B) {
2596 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002597 EmitCaseConstructHandler(Case, Indent1,
2598 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002599 false, OptDecs, O);
2600 }
2601
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002602 O << '\n';
2603 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002604 O << "}\n\n";
2605}
2606
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002607class DoEmitPopulateLanguageMap;
2608typedef void (DoEmitPopulateLanguageMap::* DoEmitPopulateLanguageMapHandler)
2609(const DagInit& D);
2610
2611class DoEmitPopulateLanguageMap
2612: public HandlerTable<DoEmitPopulateLanguageMapHandler>
2613{
2614private:
2615 raw_ostream& O_;
2616
2617public:
2618
2619 explicit DoEmitPopulateLanguageMap (raw_ostream& O) : O_(O) {
2620 if (!staticMembersInitialized_) {
2621 AddHandler("lang_to_suffixes",
2622 &DoEmitPopulateLanguageMap::onLangToSuffixes);
2623
2624 staticMembersInitialized_ = true;
2625 }
2626 }
2627
2628 void operator() (Init* I) {
2629 InvokeDagInitHandler(this, I);
2630 }
2631
2632private:
2633
2634 void onLangToSuffixes (const DagInit& d) {
2635 CheckNumberOfArguments(d, 2);
2636
2637 const std::string& Lang = InitPtrToString(d.getArg(0));
2638 Init* Suffixes = d.getArg(1);
2639
2640 // Second argument to lang_to_suffixes is either a single string...
2641 if (typeid(*Suffixes) == typeid(StringInit)) {
2642 O_.indent(Indent1) << "langMap[\"" << InitPtrToString(Suffixes)
2643 << "\"] = \"" << Lang << "\";\n";
2644 }
2645 // ...or a list of strings.
2646 else {
2647 const ListInit& Lst = InitPtrToList(Suffixes);
2648 assert(Lst.size() != 0);
2649 for (ListInit::const_iterator B = Lst.begin(), E = Lst.end();
2650 B != E; ++B) {
2651 O_.indent(Indent1) << "langMap[\"" << InitPtrToString(*B)
2652 << "\"] = \"" << Lang << "\";\n";
2653 }
2654 }
2655 }
2656
2657};
2658
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002659/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002660void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002661{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002662 O << "int PopulateLanguageMap (LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002663
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002664 // For each LangMap:
2665 const RecordVector& LangMaps =
Mikhail Glushenkov00a5b5b2010-08-23 19:24:16 +00002666 Records.getAllDerivedDefinitions("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002667
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002668 for (RecordVector::const_iterator B = LangMaps.begin(),
2669 E = LangMaps.end(); B!=E; ++B) {
2670 ListInit* LangMap = (*B)->getValueAsListInit("map");
2671 std::for_each(LangMap->begin(), LangMap->end(),
2672 DoEmitPopulateLanguageMap(O));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002673 }
2674
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002675 O << '\n';
2676 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002677 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002678}
2679
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002680/// EmitEdgePropertyHandlerCallback - Emits code that handles edge
2681/// properties. Helper function passed to EmitCaseConstructHandler() by
2682/// EmitEdgeClass().
2683void EmitEdgePropertyHandlerCallback (const Init* i, unsigned IndentLevel,
2684 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002685 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002686 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002687
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002688 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002689 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002690 }
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002691 else if (OpName == "error") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002692 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002693 O.indent(IndentLevel) << "PrintError(\""
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002694 << InitPtrToString(d.getArg(0))
2695 << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002696 O.indent(IndentLevel) << "return -1;\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002697 return;
2698 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002699 else {
2700 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002701 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002702 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002703
2704 if (d.getNumArgs() > 0)
2705 O << InitPtrToInt(d.getArg(0)) << ";\n";
2706 else
2707 O << "2;\n";
2708
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002709}
2710
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002711/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002712void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002713 const DagInit& Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002714 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002715
2716 // Class constructor.
2717 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002718 << "public:\n";
2719 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2720 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002721
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002722 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002723 O.indent(Indent1)
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002724 << "int Weight(const InputLanguagesSet& InLangs) const {\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002725 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002726
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002727 // Handle the 'case' construct.
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002728 EmitCaseConstructHandler(&Case, Indent2, EmitEdgePropertyHandlerCallback,
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002729 false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002730
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002731 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002732 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002733}
2734
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002735/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002736void EmitEdgeClasses (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002737 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002738 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002739 int i = 0;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002740 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002741 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002742 const DagInit& Edge = **B;
2743 const std::string& Name = GetOperatorName(Edge);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002744
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002745 if (Name == "optional_edge") {
2746 assert(IsOptionalEdge(Edge));
2747 const std::string& NodeB = InitPtrToString(Edge.getArg(1));
2748
2749 const DagInit& Weight = InitPtrToDag(Edge.getArg(2));
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002750 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
2751 }
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002752 else if (Name != "edge") {
2753 throw "Unknown edge class: '" + Name + "'!";
2754 }
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002755
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002756 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002757 }
2758}
2759
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002760/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph() function.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002761void EmitPopulateCompilationGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002762 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002763 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002764{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002765 O << "int PopulateCompilationGraph (CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002766
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002767 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2768 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002769 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002770
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002771 O << '\n';
2772
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002773 // Insert edges.
2774
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002775 int i = 0;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002776 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002777 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002778 const DagInit& Edge = **B;
2779 const std::string& NodeA = InitPtrToString(Edge.getArg(0));
2780 const std::string& NodeB = InitPtrToString(Edge.getArg(1));
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002781
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002782 O.indent(Indent1) << "if (int ret = G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002783
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002784 if (IsOptionalEdge(Edge))
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002785 O << "new Edge" << i << "()";
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002786 else
2787 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002788
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002789 O << "))\n";
2790 O.indent(Indent2) << "return ret;\n";
2791
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002792 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002793 }
2794
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002795 O << '\n';
2796 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002797 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002798}
2799
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002800/// HookInfo - Information about the hook type and number of arguments.
2801struct HookInfo {
2802
2803 // A hook can either have a single parameter of type std::vector<std::string>,
2804 // or NumArgs parameters of type const char*.
2805 enum HookType { ListHook, ArgHook };
2806
2807 HookType Type;
2808 unsigned NumArgs;
2809
2810 HookInfo() : Type(ArgHook), NumArgs(1)
2811 {}
2812
2813 HookInfo(HookType T) : Type(T), NumArgs(1)
2814 {}
2815
2816 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2817 {}
2818};
2819
2820typedef llvm::StringMap<HookInfo> HookInfoMap;
2821
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002822/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002823/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002824/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002825class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002826 HookInfoMap& HookNames_;
2827 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002828public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002829 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2830 : HookNames_(HookNames), OptDescs_(OptDescs)
2831 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002832
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002833 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002834 const std::string& Name = GetOperatorName(Dag);
2835
2836 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002837 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002838 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2839 const std::string& HookName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002840 const OptionDescription& D =
2841 OptDescs_.FindParameterListOrParameter(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002842
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002843 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2844 : HookInfo::ArgHook);
2845 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002846 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002847 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002848 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2849 }
2850 }
2851
2852 void onCmdLine(const std::string& Cmd) {
2853 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002854 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002855
2856 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2857 B != E; ++B) {
2858 const std::string& cmd = *B;
2859
2860 if (cmd == "$CALL") {
2861 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002862 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002863 const std::string& HookName = *B;
2864
2865 if (HookName.at(0) == ')')
2866 throw "$CALL invoked with no arguments!";
2867
2868 while (++B != E && B->at(0) != ')') {
2869 ++NumArgs;
2870 }
2871
2872 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2873
2874 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2875 H->second.Type != HookInfo::ArgHook)
2876 throw "Overloading of hooks is not allowed. Overloaded hook: "
2877 + HookName;
2878 else
2879 HookNames_[HookName] = HookInfo(NumArgs);
2880 }
2881 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002882 }
2883
2884 void operator()(const Init* Arg) {
2885
2886 // We're invoked on an action (either a dag or a dag list).
2887 if (typeid(*Arg) == typeid(DagInit)) {
2888 const DagInit& Dag = InitPtrToDag(Arg);
2889 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002890 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002891 }
2892 else if (typeid(*Arg) == typeid(ListInit)) {
2893 const ListInit& List = InitPtrToList(Arg);
2894 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2895 ++B) {
2896 const DagInit& Dag = InitPtrToDag(*B);
2897 this->onAction(Dag);
2898 }
2899 return;
2900 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002901
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002902 // We're invoked on a command line.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002903 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002904 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002905
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002906 void operator()(const Init* Statement, unsigned) {
2907 this->operator()(Statement);
2908 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002909};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002910
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002911/// FillInHookNames - Actually extract the hook names from all command
2912/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002913void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002914 const OptionDescriptions& OptDescs,
2915 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002916{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002917 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002918 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2919 E = ToolDescs.end(); B != E; ++B) {
2920 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002921
2922 // Look for 'forward_transformed_value' in 'actions'.
2923 if (D.Actions)
2924 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2925
2926 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002927 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002928 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002929 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002930 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002931 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002932 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002933 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002934 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002935 }
2936}
2937
2938/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2939/// property records and emit hook function declaration for each
2940/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002941void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2942 const OptionDescriptions& OptDescs, raw_ostream& O) {
2943 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002944
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002945 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002946 if (HookNames.empty())
2947 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002948
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002949 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002950 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002951 const char* HookName = B->first();
2952 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002953
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002954 O.indent(Indent1) << "std::string " << HookName << "(";
2955
2956 if (Info.Type == HookInfo::ArgHook) {
2957 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2958 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2959 }
2960 }
2961 else {
2962 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002963 }
2964
2965 O <<");\n";
2966 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002967}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002968
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002969/// EmitIncludes - Emit necessary #include directives and some
2970/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002971void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002972 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2973 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002974 << "#include \"llvm/CompilerDriver/Error.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002975 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002976
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002977 << "#include \"llvm/Support/CommandLine.h\"\n"
2978 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002979
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002980 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002981 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002982 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002983 << "#include <stdexcept>\n\n"
2984
2985 << "using namespace llvm;\n"
2986 << "using namespace llvmc;\n\n"
2987
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002988 << "inline const char* checkCString(const char* s)\n"
2989 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002990}
2991
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002992
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00002993/// DriverData - Holds all information about the driver.
2994struct DriverData {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002995 OptionDescriptions OptDescs;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002996 ToolDescriptions ToolDescs;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002997 DagVector Edges;
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00002998 bool HasSink;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002999};
3000
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003001/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003002/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003003bool HasSink(const ToolDescriptions& ToolDescs) {
3004 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
3005 E = ToolDescs.end(); B != E; ++B)
3006 if ((*B)->isSink())
3007 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003008
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003009 return false;
3010}
3011
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003012/// CollectDriverData - Collect compilation graph edges, tool properties and
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003013/// option properties from the parse tree.
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003014void CollectDriverData (const RecordKeeper& Records, DriverData& Data) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003015 // Collect option properties.
3016 const RecordVector& OptionLists =
3017 Records.getAllDerivedDefinitions("OptionList");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003018 CollectOptionDescriptions(OptionLists, Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003019
3020 // Collect tool properties.
3021 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003022 CollectToolDescriptions(Tools, Data.ToolDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003023 Data.HasSink = HasSink(Data.ToolDescs);
3024
3025 // Collect compilation graph edges.
3026 const RecordVector& CompilationGraphs =
3027 Records.getAllDerivedDefinitions("CompilationGraph");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003028 FillInEdgeVector(CompilationGraphs, Data.Edges);
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00003029}
3030
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003031/// CheckDriverData - Perform some sanity checks on the collected data.
3032void CheckDriverData(DriverData& Data) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003033 // Filter out all tools not mentioned in the compilation graph.
3034 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003035
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003036 // Typecheck the compilation graph.
3037 TypecheckGraph(Data.Edges, Data.ToolDescs);
3038
3039 // Check that there are no options without side effects (specified
3040 // only in the OptionList).
3041 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003042}
3043
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003044void EmitDriverCode(const DriverData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003045 // Emit file header.
3046 EmitIncludes(O);
3047
3048 // Emit global option registration code.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003049 O << "namespace llvmc {\n"
3050 << "namespace autogenerated {\n\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003051 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003052 O << "} // End namespace autogenerated.\n"
3053 << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003054
3055 // Emit hook declarations.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003056 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003057 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003058 O << "} // End namespace hooks.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003059
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003060 O << "namespace {\n\n";
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003061 O << "using namespace llvmc::autogenerated;\n\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003062
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003063 // Emit Tool classes.
3064 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
3065 E = Data.ToolDescs.end(); B!=E; ++B)
3066 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
3067
3068 // Emit Edge# classes.
3069 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
3070
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003071 O << "} // End anonymous namespace.\n\n";
3072
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003073 O << "namespace llvmc {\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003074 O << "namespace autogenerated {\n\n";
3075
3076 // Emit PreprocessOptions() function.
3077 EmitPreprocessOptions(Records, Data.OptDescs, O);
3078
3079 // Emit PopulateLanguageMap() function
3080 // (language map maps from file extensions to language names).
3081 EmitPopulateLanguageMap(Records, O);
3082
3083 // Emit PopulateCompilationGraph() function.
3084 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
3085
3086 O << "} // End namespace autogenerated.\n";
3087 O << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003088
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003089 // EOF
3090}
3091
3092
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003093// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00003094}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003095
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003096/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00003097void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003098 try {
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003099 DriverData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003100
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003101 CollectDriverData(Records, Data);
3102 CheckDriverData(Data);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003103
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003104 this->EmitSourceFileHeader("llvmc-based driver: auto-generated code", O);
3105 EmitDriverCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003106
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003107 } catch (std::exception& Error) {
3108 throw Error.what() + std::string(" - usually this means a syntax error.");
3109 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003110}