blob: 48c7cc375f001e0c17bcc2a434c222e2576de4b2 [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;
36typedef std::vector<std::string> StrVector;
37
38//===----------------------------------------------------------------------===//
39/// Constants
40
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +000041// Indentation.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000042const unsigned TabWidth = 4;
43const unsigned Indent1 = TabWidth*1;
44const unsigned Indent2 = TabWidth*2;
45const unsigned Indent3 = TabWidth*3;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +000046const unsigned Indent4 = TabWidth*4;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000047
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000048// Default help string.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000049const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000050
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000051// Name for the "sink" option.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000052const char * const SinkOptionName = "AutoGeneratedSinkOption";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000053
54//===----------------------------------------------------------------------===//
55/// Helper functions
56
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000057/// Id - An 'identity' function object.
58struct Id {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000059 template<typename T0>
60 void operator()(const T0&) const {
61 }
62 template<typename T0, typename T1>
63 void operator()(const T0&, const T1&) const {
64 }
65 template<typename T0, typename T1, typename T2>
66 void operator()(const T0&, const T1&, const T2&) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000067 }
68};
69
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000070int InitPtrToInt(const Init* ptr) {
71 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000072 return val.getValue();
73}
74
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000075const std::string& InitPtrToString(const Init* ptr) {
76 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
77 return val.getValue();
78}
79
80const ListInit& InitPtrToList(const Init* ptr) {
81 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
82 return val;
83}
84
85const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000086 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000087 return val;
88}
89
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000090const std::string GetOperatorName(const DagInit& D) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +000091 return D.getOperator()->getAsString();
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000092}
93
Mikhail Glushenkove0b65702009-12-23 12:49:30 +000094/// CheckBooleanConstant - Check that the provided value is a boolean constant.
95void CheckBooleanConstant(const Init* I) {
96 const DefInit& val = dynamic_cast<const DefInit&>(*I);
97 const std::string& str = val.getAsString();
98
99 if (str != "true" && str != "false") {
100 throw "Incorrect boolean value: '" + str +
101 "': must be either 'true' or 'false'";
102 }
103}
104
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000105// CheckNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +0000106// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000107void CheckNumberOfArguments (const DagInit& d, unsigned minArgs) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000108 if (d.getNumArgs() < minArgs)
109 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +0000110}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000111
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000112// IsDagEmpty - is this DAG marked with an empty marker?
113bool IsDagEmpty (const DagInit& d) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000114 return GetOperatorName(d) == "empty_dag_marker";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000115}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000116
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000117// EscapeVariableName - Escape commas and other symbols not allowed
118// in the C++ variable names. Makes it possible to use options named
119// like "Wa," (useful for prefix options).
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000120std::string EscapeVariableName (const std::string& Var) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000121 std::string ret;
122 for (unsigned i = 0; i != Var.size(); ++i) {
123 char cur_char = Var[i];
124 if (cur_char == ',') {
125 ret += "_comma_";
126 }
127 else if (cur_char == '+') {
128 ret += "_plus_";
129 }
130 else if (cur_char == '-') {
131 ret += "_dash_";
132 }
133 else {
134 ret.push_back(cur_char);
135 }
136 }
137 return ret;
138}
139
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000140/// EscapeQuotes - Replace '"' with '\"'.
141std::string EscapeQuotes (const std::string& Var) {
142 std::string ret;
143 for (unsigned i = 0; i != Var.size(); ++i) {
144 char cur_char = Var[i];
145 if (cur_char == '"') {
146 ret += "\\\"";
147 }
148 else {
149 ret.push_back(cur_char);
150 }
151 }
152 return ret;
153}
154
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000155/// OneOf - Does the input string contain this character?
156bool OneOf(const char* lst, char c) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000157 while (*lst) {
158 if (*lst++ == c)
159 return true;
160 }
161 return false;
162}
163
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000164template <class I, class S>
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000165void CheckedIncrement(I& P, I E, S ErrorString) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000166 ++P;
167 if (P == E)
168 throw ErrorString;
169}
170
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000171// apply is needed because C++'s syntax doesn't let us construct a function
172// object and call it in the same statement.
173template<typename F, typename T0>
174void apply(F Fun, T0& Arg0) {
175 return Fun(Arg0);
176}
177
178template<typename F, typename T0, typename T1>
179void apply(F Fun, T0& Arg0, T1& Arg1) {
180 return Fun(Arg0, Arg1);
181}
182
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000183//===----------------------------------------------------------------------===//
184/// Back-end specific code
185
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000186
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000187/// OptionType - One of six different option types. See the
188/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000189namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000190
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000191 enum OptionType { Alias, Switch, SwitchList,
192 Parameter, ParameterList, Prefix, PrefixList };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000193
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000194 bool IsAlias(OptionType t) {
195 return (t == Alias);
196 }
197
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000198 bool IsList (OptionType t) {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000199 return (t == SwitchList || t == ParameterList || t == PrefixList);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000200 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000201
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000202 bool IsSwitch (OptionType t) {
203 return (t == Switch);
204 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000205
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000206 bool IsSwitchList (OptionType t) {
207 return (t == SwitchList);
208 }
209
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000210 bool IsParameter (OptionType t) {
211 return (t == Parameter || t == Prefix);
212 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000213
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000214}
215
216OptionType::OptionType stringToOptionType(const std::string& T) {
217 if (T == "alias_option")
218 return OptionType::Alias;
219 else if (T == "switch_option")
220 return OptionType::Switch;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000221 else if (T == "switch_list_option")
222 return OptionType::SwitchList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000223 else if (T == "parameter_option")
224 return OptionType::Parameter;
225 else if (T == "parameter_list_option")
226 return OptionType::ParameterList;
227 else if (T == "prefix_option")
228 return OptionType::Prefix;
229 else if (T == "prefix_list_option")
230 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000231 else
232 throw "Unknown option type: " + T + '!';
233}
234
235namespace OptionDescriptionFlags {
236 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000237 ReallyHidden = 0x4, OneOrMore = 0x8,
238 Optional = 0x10, CommaSeparated = 0x20,
239 ForwardNotSplit = 0x40, ZeroOrMore = 0x80 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000240}
241
242/// OptionDescription - Represents data contained in a single
243/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000244struct OptionDescription {
245 OptionType::OptionType Type;
246 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000247 unsigned Flags;
248 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000249 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000250 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000251
252 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000253 const std::string& n = "",
254 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000255 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000256 {}
257
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000258 /// GenTypeDeclaration - Returns the C++ variable type of this
259 /// option.
260 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000261
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000262 /// GenVariableName - Returns the variable name used in the
263 /// generated C++ code.
264 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000265
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000266 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000267 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000268
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000269 /// CheckConsistency - Check that the flags are consistent.
270 void CheckConsistency() const;
271
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000272 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000273
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000274 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000275
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000276 bool isMultiVal() const;
277
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000278 bool isCommaSeparated() const;
279 void setCommaSeparated();
280
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000281 bool isForwardNotSplit() const;
282 void setForwardNotSplit();
283
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000284 bool isRequired() const;
285 void setRequired();
286
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000287 bool isOneOrMore() const;
288 void setOneOrMore();
289
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000290 bool isZeroOrMore() const;
291 void setZeroOrMore();
292
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000293 bool isOptional() const;
294 void setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000295
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000296 bool isHidden() const;
297 void setHidden();
298
299 bool isReallyHidden() const;
300 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000301
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000302 bool isSwitch() const
303 { return OptionType::IsSwitch(this->Type); }
304
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000305 bool isSwitchList() const
306 { return OptionType::IsSwitchList(this->Type); }
307
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000308 bool isParameter() const
309 { return OptionType::IsParameter(this->Type); }
310
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000311 bool isList() const
312 { return OptionType::IsList(this->Type); }
313
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000314 bool isParameterList() const
315 { return (OptionType::IsList(this->Type)
316 && !OptionType::IsSwitchList(this->Type)); }
317
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000318};
319
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000320void OptionDescription::CheckConsistency() const {
321 unsigned i = 0;
322
323 i += this->isRequired();
324 i += this->isOptional();
325 i += this->isOneOrMore();
326 i += this->isZeroOrMore();
327
328 if (i > 1) {
329 throw "Only one of (required), (optional), (one_or_more) or "
330 "(zero_or_more) properties is allowed!";
331 }
332}
333
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000334void OptionDescription::Merge (const OptionDescription& other)
335{
336 if (other.Type != Type)
337 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000338
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000339 if (Help == other.Help || Help == DefaultHelpString)
340 Help = other.Help;
341 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000342 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000343 " defined for option " + Name + "\n";
344 }
345
346 Flags |= other.Flags;
347}
348
349bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000350 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000351}
352
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000353bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000354 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000355}
356
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000357bool OptionDescription::isCommaSeparated() const {
358 return Flags & OptionDescriptionFlags::CommaSeparated;
359}
360void OptionDescription::setCommaSeparated() {
361 Flags |= OptionDescriptionFlags::CommaSeparated;
362}
363
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000364bool OptionDescription::isForwardNotSplit() const {
365 return Flags & OptionDescriptionFlags::ForwardNotSplit;
366}
367void OptionDescription::setForwardNotSplit() {
368 Flags |= OptionDescriptionFlags::ForwardNotSplit;
369}
370
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000371bool OptionDescription::isRequired() const {
372 return Flags & OptionDescriptionFlags::Required;
373}
374void OptionDescription::setRequired() {
375 Flags |= OptionDescriptionFlags::Required;
376}
377
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000378bool OptionDescription::isOneOrMore() const {
379 return Flags & OptionDescriptionFlags::OneOrMore;
380}
381void OptionDescription::setOneOrMore() {
382 Flags |= OptionDescriptionFlags::OneOrMore;
383}
384
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000385bool OptionDescription::isZeroOrMore() const {
386 return Flags & OptionDescriptionFlags::ZeroOrMore;
387}
388void OptionDescription::setZeroOrMore() {
389 Flags |= OptionDescriptionFlags::ZeroOrMore;
390}
391
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000392bool OptionDescription::isOptional() const {
393 return Flags & OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000394}
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000395void OptionDescription::setOptional() {
396 Flags |= OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000397}
398
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000399bool OptionDescription::isHidden() const {
400 return Flags & OptionDescriptionFlags::Hidden;
401}
402void OptionDescription::setHidden() {
403 Flags |= OptionDescriptionFlags::Hidden;
404}
405
406bool OptionDescription::isReallyHidden() const {
407 return Flags & OptionDescriptionFlags::ReallyHidden;
408}
409void OptionDescription::setReallyHidden() {
410 Flags |= OptionDescriptionFlags::ReallyHidden;
411}
412
413const char* OptionDescription::GenTypeDeclaration() const {
414 switch (Type) {
415 case OptionType::Alias:
416 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000417 case OptionType::PrefixList:
418 case OptionType::ParameterList:
419 return "cl::list<std::string>";
420 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000421 return "cl::opt<bool>";
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000422 case OptionType::SwitchList:
423 return "cl::list<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000424 case OptionType::Parameter:
425 case OptionType::Prefix:
426 default:
427 return "cl::opt<std::string>";
428 }
429}
430
431std::string OptionDescription::GenVariableName() const {
432 const std::string& EscapedName = EscapeVariableName(Name);
433 switch (Type) {
434 case OptionType::Alias:
435 return "AutoGeneratedAlias_" + EscapedName;
436 case OptionType::PrefixList:
437 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000438 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000439 case OptionType::Switch:
440 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000441 case OptionType::SwitchList:
442 return "AutoGeneratedSwitchList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000443 case OptionType::Prefix:
444 case OptionType::Parameter:
445 default:
446 return "AutoGeneratedParameter_" + EscapedName;
447 }
448}
449
450/// OptionDescriptions - An OptionDescription array plus some helper
451/// functions.
452class OptionDescriptions {
453 typedef StringMap<OptionDescription> container_type;
454
455 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000456 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000457
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000458public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000459 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000460 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000461
462 // Wrappers for FindOption that throw an exception in case the option has a
463 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000464 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000465 const OptionDescription& FindParameter(const std::string& OptName) const;
466 const OptionDescription& FindList(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000467 const OptionDescription& FindParameterList(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000468 const OptionDescription&
469 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000470 const OptionDescription&
471 FindParameterListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000472
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000473 /// insertDescription - Insert new OptionDescription into
474 /// OptionDescriptions list
475 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000476
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000477 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000478 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000479 const_iterator begin() const { return Descriptions.begin(); }
480 const_iterator end() const { return Descriptions.end(); }
481};
482
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000483const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000484OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000485 const_iterator I = Descriptions.find(OptName);
486 if (I != Descriptions.end())
487 return I->second;
488 else
489 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000490}
491
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000492const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000493OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000494 const OptionDescription& OptDesc = this->FindOption(OptName);
495 if (!OptDesc.isSwitch())
496 throw OptName + ": incorrect option type - should be a switch!";
497 return OptDesc;
498}
499
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000500const OptionDescription&
501OptionDescriptions::FindList(const std::string& OptName) const {
502 const OptionDescription& OptDesc = this->FindOption(OptName);
503 if (!OptDesc.isList())
504 throw OptName + ": incorrect option type - should be a list!";
505 return OptDesc;
506}
507
508const OptionDescription&
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000509OptionDescriptions::FindParameterList(const std::string& OptName) const {
510 const OptionDescription& OptDesc = this->FindOption(OptName);
511 if (!OptDesc.isList() || OptDesc.isSwitchList())
512 throw OptName + ": incorrect option type - should be a parameter list!";
513 return OptDesc;
514}
515
516const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000517OptionDescriptions::FindParameter(const std::string& OptName) const {
518 const OptionDescription& OptDesc = this->FindOption(OptName);
519 if (!OptDesc.isParameter())
520 throw OptName + ": incorrect option type - should be a parameter!";
521 return OptDesc;
522}
523
524const OptionDescription&
525OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
526 const OptionDescription& OptDesc = this->FindOption(OptName);
527 if (!OptDesc.isList() && !OptDesc.isParameter())
528 throw OptName
529 + ": incorrect option type - should be a list or parameter!";
530 return OptDesc;
531}
532
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000533const OptionDescription&
534OptionDescriptions::FindParameterListOrParameter
535(const std::string& OptName) const {
536 const OptionDescription& OptDesc = this->FindOption(OptName);
537 if ((!OptDesc.isList() && !OptDesc.isParameter()) || OptDesc.isSwitchList())
538 throw OptName
539 + ": incorrect option type - should be a parameter list or parameter!";
540 return OptDesc;
541}
542
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000543void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000544 container_type::iterator I = Descriptions.find(o.Name);
545 if (I != Descriptions.end()) {
546 OptionDescription& D = I->second;
547 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000548 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000549 else {
550 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000551 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000552}
553
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000554/// HandlerTable - A base class for function objects implemented as
555/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000556template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000557class HandlerTable {
558protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000559 // Implementation details.
560
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000561 /// HandlerMap - A map from property names to property handlers
562 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000563
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000564 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000565 static bool staticMembersInitialized_;
566
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000567public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000568
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000569 Handler GetHandler (const std::string& HandlerName) const {
570 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000571
572 if (method != Handlers_.end()) {
573 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000574 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000575 }
576 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000577 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000578 }
579 }
580
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000581 void AddHandler(const char* Property, Handler H) {
582 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000583 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000584
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000585};
586
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000587template <class Handler, class FunctionObject>
588Handler GetHandler(FunctionObject* Obj, const DagInit& Dag) {
589 const std::string& HandlerName = GetOperatorName(Dag);
590 return Obj->GetHandler(HandlerName);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000591}
592
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000593template <class FunctionObject>
594void InvokeDagInitHandler(FunctionObject* Obj, Init* I) {
595 typedef void (FunctionObject::*Handler) (const DagInit&);
596
597 const DagInit& Dag = InitPtrToDag(I);
598 Handler h = GetHandler<Handler>(Obj, Dag);
599
600 ((Obj)->*(h))(Dag);
601}
602
603template <class FunctionObject>
604void InvokeDagInitHandler(const FunctionObject* const Obj,
605 const Init* I, unsigned IndentLevel, raw_ostream& O)
606{
607 typedef void (FunctionObject::*Handler)
608 (const DagInit&, unsigned IndentLevel, raw_ostream& O) const;
609
610 const DagInit& Dag = InitPtrToDag(I);
611 Handler h = GetHandler<Handler>(Obj, Dag);
612
613 ((Obj)->*(h))(Dag, IndentLevel, O);
614}
615
616
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000617template <typename H>
618typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
619
620template <typename H>
621bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000622
623
624/// CollectOptionProperties - Function object for iterating over an
625/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000626class CollectOptionProperties;
627typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000628(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000629
630class CollectOptionProperties
631: public HandlerTable<CollectOptionPropertiesHandler>
632{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000633private:
634
635 /// optDescs_ - OptionDescriptions table. This is where the
636 /// information is stored.
637 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000638
639public:
640
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000641 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000642 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000643 {
644 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000645 AddHandler("help", &CollectOptionProperties::onHelp);
646 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000647 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000648 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
649 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000650 AddHandler("zero_or_more", &CollectOptionProperties::onZeroOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000651 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
652 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000653 AddHandler("optional", &CollectOptionProperties::onOptional);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000654 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000655 AddHandler("forward_not_split",
656 &CollectOptionProperties::onForwardNotSplit);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000657
658 staticMembersInitialized_ = true;
659 }
660 }
661
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000662 /// operator() - Just forwards to the corresponding property
663 /// handler.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000664 void operator() (Init* I) {
665 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000666 }
667
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000668private:
669
670 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000671 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000672
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000673 void onHelp (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000674 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000675 optDesc_.Help = EscapeQuotes(InitPtrToString(d.getArg(0)));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000676 }
677
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000678 void onHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000679 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000680 optDesc_.setHidden();
681 }
682
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000683 void onReallyHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000684 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000685 optDesc_.setReallyHidden();
686 }
687
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000688 void onCommaSeparated (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000689 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000690 if (!optDesc_.isParameterList())
691 throw "'comma_separated' is valid only on parameter list options!";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000692 optDesc_.setCommaSeparated();
693 }
694
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000695 void onForwardNotSplit (const DagInit& d) {
696 CheckNumberOfArguments(d, 0);
697 if (!optDesc_.isParameter())
698 throw "'forward_not_split' is valid only for parameter options!";
699 optDesc_.setForwardNotSplit();
700 }
701
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000702 void onRequired (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000703 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000704
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000705 optDesc_.setRequired();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000706 optDesc_.CheckConsistency();
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000707 }
708
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000709 void onInit (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000710 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000711 Init* i = d.getArg(0);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000712 const std::string& str = i->getAsString();
713
714 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
715 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
716
717 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000718 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000719
720 optDesc_.InitVal = i;
721 }
722
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000723 void onOneOrMore (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000724 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000725
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000726 optDesc_.setOneOrMore();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000727 optDesc_.CheckConsistency();
728 }
729
730 void onZeroOrMore (const DagInit& d) {
731 CheckNumberOfArguments(d, 0);
732
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000733 if (optDesc_.isList())
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000734 llvm::errs() << "Warning: specifying the 'zero_or_more' property "
735 "on a list option has no effect.\n";
736
737 optDesc_.setZeroOrMore();
738 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000739 }
740
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000741 void onOptional (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000742 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000743
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000744 if (!optDesc_.isList())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000745 llvm::errs() << "Warning: specifying the 'optional' property"
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000746 "on a non-list option has no effect.\n";
747
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000748 optDesc_.setOptional();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000749 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000750 }
751
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000752 void onMultiVal (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000753 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000754 int val = InitPtrToInt(d.getArg(0));
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000755 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000756 throw "Error in the 'multi_val' property: "
757 "the value must be greater than 1!";
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000758 if (!optDesc_.isParameterList())
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000759 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000760 optDesc_.MultiVal = val;
761 }
762
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000763};
764
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000765/// AddOption - A function object that is applied to every option
766/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000767class AddOption {
768private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000769 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000770
771public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000772 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000773 {}
774
775 void operator()(const Init* i) {
776 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000777 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000778
779 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000780 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000781 const std::string& Name = InitPtrToString(d.getArg(0));
782
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000783 OptionDescription OD(Type, Name);
784
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000785
786 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000787
788 if (OD.isAlias()) {
789 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000790 OD.Help = InitPtrToString(d.getArg(1));
791 }
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000792 else {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000793 processOptionProperties(d, OD);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000794 }
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000795
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000796 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000797 }
798
799private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000800 /// processOptionProperties - Go through the list of option
801 /// properties and call a corresponding handler for each.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000802 static void processOptionProperties (const DagInit& d, OptionDescription& o) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000803 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000804 DagInit::const_arg_iterator B = d.arg_begin();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000805 // Skip the first argument: it's always the option name.
806 ++B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000807 std::for_each(B, d.arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000808 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000809
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000810};
811
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000812/// CollectOptionDescriptions - Collects option properties from all
813/// OptionLists.
814void CollectOptionDescriptions (RecordVector::const_iterator B,
815 RecordVector::const_iterator E,
816 OptionDescriptions& OptDescs)
817{
818 // For every OptionList:
819 for (; B!=E; ++B) {
820 RecordVector::value_type T = *B;
821 // Throws an exception if the value does not exist.
822 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000823
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000824 // For every option description in this list:
825 // collect the information and
826 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
827 }
828}
829
830// Tool information record
831
832namespace ToolFlags {
833 enum ToolFlags { Join = 0x1, Sink = 0x2 };
834}
835
836struct ToolDescription : public RefCountedBase<ToolDescription> {
837 std::string Name;
838 Init* CmdLine;
839 Init* Actions;
840 StrVector InLanguage;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000841 std::string InFileOption;
842 std::string OutFileOption;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000843 std::string OutLanguage;
844 std::string OutputSuffix;
845 unsigned Flags;
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000846 const Init* OnEmpty;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000847
848 // Various boolean properties
849 void setSink() { Flags |= ToolFlags::Sink; }
850 bool isSink() const { return Flags & ToolFlags::Sink; }
851 void setJoin() { Flags |= ToolFlags::Join; }
852 bool isJoin() const { return Flags & ToolFlags::Join; }
853
854 // Default ctor here is needed because StringMap can only store
855 // DefaultConstructible objects
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000856 ToolDescription ()
Mikhail Glushenkovc4301292010-02-23 09:05:01 +0000857 : CmdLine(0), Actions(0), OutFileOption("-o"),
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000858 Flags(0), OnEmpty(0)
859 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000860 ToolDescription (const std::string& n)
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000861 : Name(n), CmdLine(0), Actions(0), OutFileOption("-o"),
862 Flags(0), OnEmpty(0)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000863 {}
864};
865
866/// ToolDescriptions - A list of Tool information records.
867typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
868
869
870/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000871/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000872
873class CollectToolProperties;
874typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000875(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000876
877class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
878{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000879private:
880
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000881 /// toolDesc_ - Properties of the current Tool. This is where the
882 /// information is stored.
883 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000884
885public:
886
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000887 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000888 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000889 {
890 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000891
892 AddHandler("actions", &CollectToolProperties::onActions);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000893 AddHandler("command", &CollectToolProperties::onCommand);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000894 AddHandler("in_language", &CollectToolProperties::onInLanguage);
895 AddHandler("join", &CollectToolProperties::onJoin);
896 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000897
898 AddHandler("out_file_option", &CollectToolProperties::onOutFileOption);
899 AddHandler("in_file_option", &CollectToolProperties::onInFileOption);
900
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000901 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
902 AddHandler("sink", &CollectToolProperties::onSink);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000903 AddHandler("works_on_empty", &CollectToolProperties::onWorksOnEmpty);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000904
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000905 staticMembersInitialized_ = true;
906 }
907 }
908
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000909 void operator() (Init* I) {
910 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000911 }
912
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000913private:
914
915 /// Property handlers --
916 /// Functions that extract information about tool properties from
917 /// DAG representation.
918
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000919 void onActions (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000920 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000921 Init* Case = d.getArg(0);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000922 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000923 GetOperatorName(static_cast<DagInit&>(*Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000924 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000925 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000926 }
927
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000928 void onCommand (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000929 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000930 toolDesc_.CmdLine = d.getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000931 }
932
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000933 void onInLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000934 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000935 Init* arg = d.getArg(0);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000936
937 // Find out the argument's type.
938 if (typeid(*arg) == typeid(StringInit)) {
939 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000940 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000941 }
942 else {
943 // It's a list.
944 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000945 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000946
947 // Copy strings to the output vector.
948 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
949 B != E; ++B) {
950 out.push_back(InitPtrToString(*B));
951 }
952
953 // Remove duplicates.
954 std::sort(out.begin(), out.end());
955 StrVector::iterator newE = std::unique(out.begin(), out.end());
956 out.erase(newE, out.end());
957 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000958 }
959
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000960 void onJoin (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000961 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000962 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000963 }
964
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000965 void onOutLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000966 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000967 toolDesc_.OutLanguage = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000968 }
969
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000970 void onOutFileOption (const DagInit& d) {
971 CheckNumberOfArguments(d, 1);
972 toolDesc_.OutFileOption = InitPtrToString(d.getArg(0));
973 }
974
975 void onInFileOption (const DagInit& d) {
976 CheckNumberOfArguments(d, 1);
977 toolDesc_.InFileOption = InitPtrToString(d.getArg(0));
978 }
979
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000980 void onOutputSuffix (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000981 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000982 toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000983 }
984
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000985 void onSink (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000986 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000987 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000988 }
989
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000990 void onWorksOnEmpty (const DagInit& d) {
991 toolDesc_.OnEmpty = d.getArg(0);
992 }
993
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000994};
995
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000996/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000997/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000998/// CollectToolProperties function object).
999void CollectToolDescriptions (RecordVector::const_iterator B,
1000 RecordVector::const_iterator E,
1001 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001002{
1003 // Iterate over a properties list of every Tool definition
1004 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00001005 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001006 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001007 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001008
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001009 IntrusiveRefCntPtr<ToolDescription>
1010 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001011
1012 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001013 CollectToolProperties(*ToolDesc));
1014 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001015 }
1016}
1017
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001018/// FillInEdgeVector - Merge all compilation graph definitions into
1019/// one single edge list.
1020void FillInEdgeVector(RecordVector::const_iterator B,
1021 RecordVector::const_iterator E, RecordVector& Out) {
1022 for (; B != E; ++B) {
1023 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +00001024
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001025 for (unsigned i = 0; i < edges->size(); ++i)
1026 Out.push_back(edges->getElementAsRecord(i));
1027 }
1028}
1029
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001030/// NotInGraph - Helper function object for FilterNotInGraph.
1031struct NotInGraph {
1032private:
1033 const llvm::StringSet<>& ToolsInGraph_;
1034
1035public:
1036 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
1037 : ToolsInGraph_(ToolsInGraph)
1038 {}
1039
1040 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
1041 return (ToolsInGraph_.count(x->Name) == 0);
1042 }
1043};
1044
1045/// FilterNotInGraph - Filter out from ToolDescs all Tools not
1046/// mentioned in the compilation graph definition.
1047void FilterNotInGraph (const RecordVector& EdgeVector,
1048 ToolDescriptions& ToolDescs) {
1049
1050 // List all tools mentioned in the graph.
1051 llvm::StringSet<> ToolsInGraph;
1052
1053 for (RecordVector::const_iterator B = EdgeVector.begin(),
1054 E = EdgeVector.end(); B != E; ++B) {
1055
1056 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001057 const std::string& NodeA = Edge->getValueAsString("a");
1058 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001059
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001060 if (NodeA != "root")
1061 ToolsInGraph.insert(NodeA);
1062 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001063 }
1064
1065 // Filter ToolPropertiesList.
1066 ToolDescriptions::iterator new_end =
1067 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
1068 NotInGraph(ToolsInGraph));
1069 ToolDescs.erase(new_end, ToolDescs.end());
1070}
1071
1072/// FillInToolToLang - Fills in two tables that map tool names to
1073/// (input, output) languages. Helper function used by TypecheckGraph().
1074void FillInToolToLang (const ToolDescriptions& ToolDescs,
1075 StringMap<StringSet<> >& ToolToInLang,
1076 StringMap<std::string>& ToolToOutLang) {
1077 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1078 E = ToolDescs.end(); B != E; ++B) {
1079 const ToolDescription& D = *(*B);
1080 for (StrVector::const_iterator B = D.InLanguage.begin(),
1081 E = D.InLanguage.end(); B != E; ++B)
1082 ToolToInLang[D.Name].insert(*B);
1083 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001084 }
1085}
1086
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001087/// TypecheckGraph - Check that names for output and input languages
1088/// on all edges do match. This doesn't do much when the information
1089/// about the whole graph is not available (i.e. when compiling most
1090/// plugins).
1091void TypecheckGraph (const RecordVector& EdgeVector,
1092 const ToolDescriptions& ToolDescs) {
1093 StringMap<StringSet<> > ToolToInLang;
1094 StringMap<std::string> ToolToOutLang;
1095
1096 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
1097 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
1098 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
1099
1100 for (RecordVector::const_iterator B = EdgeVector.begin(),
1101 E = EdgeVector.end(); B != E; ++B) {
1102 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001103 const std::string& NodeA = Edge->getValueAsString("a");
1104 const std::string& NodeB = Edge->getValueAsString("b");
1105 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
1106 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001107
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001108 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001109 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001110 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001111 + ": output->input language mismatch";
1112 }
1113
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001114 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001115 throw "Edges back to the root are not allowed!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001116 }
1117}
1118
1119/// WalkCase - Walks the 'case' expression DAG and invokes
1120/// TestCallback on every test, and StatementCallback on every
1121/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001122/// combinators (that is, they are passed directly to TestCallback).
1123/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1124/// IndentLevel, bool FirstTest)'.
1125/// StatementCallback must have type 'void StatementCallback(const Init*,
1126/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001127template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001128void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1129 unsigned IndentLevel = 0)
1130{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001131 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001132
1133 // Error checks.
1134 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001135 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001136
1137 if (d.getNumArgs() < 2)
1138 throw "There should be at least one clause in the 'case' expression:\n"
1139 + d.getAsString();
1140
1141 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001142 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001143 const unsigned numArgs = d.getNumArgs();
1144 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001145 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1146 B != E; ++B) {
1147 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001148
1149 if (!even)
1150 {
1151 // Handle test.
1152 const DagInit& Test = InitPtrToDag(arg);
1153
1154 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001155 throw "The 'default' clause should be the last in the "
1156 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001157 if (i == numArgs)
1158 throw "Case construct handler: no corresponding action "
1159 "found for the test " + Test.getAsString() + '!';
1160
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001161 TestCallback(Test, IndentLevel, (i == 1));
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001162 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001163 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001164 {
1165 if (dynamic_cast<DagInit*>(arg)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001166 && GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001167 // Nested 'case'.
1168 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1169 }
1170
1171 // Handle statement.
1172 StatementCallback(arg, IndentLevel);
1173 }
1174
1175 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001176 even = !even;
1177 }
1178}
1179
1180/// ExtractOptionNames - A helper function object used by
1181/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1182class ExtractOptionNames {
1183 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001184
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001185 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001186 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001187 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001188 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001189 ActionName == "forward_value" ||
1190 ActionName == "forward_transformed_value" ||
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001191 ActionName == "switch_on" || ActionName == "any_switch_on" ||
1192 ActionName == "parameter_equals" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001193 ActionName == "element_in_list" || ActionName == "not_empty" ||
1194 ActionName == "empty") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001195 CheckNumberOfArguments(Stmt, 1);
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001196
1197 Init* Arg = Stmt.getArg(0);
1198 if (typeid(*Arg) == typeid(StringInit)) {
1199 const std::string& Name = InitPtrToString(Arg);
1200 OptionNames_.insert(Name);
1201 }
1202 else {
1203 // It's a list.
1204 const ListInit& List = InitPtrToList(Arg);
1205 for (ListInit::const_iterator B = List.begin(), E = List.end();
1206 B != E; ++B) {
1207 const std::string& Name = InitPtrToString(*B);
1208 OptionNames_.insert(Name);
1209 }
1210 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001211 }
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001212 else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001213 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001214 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001215 }
1216 }
1217 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001218
1219public:
1220 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1221 {}
1222
1223 void operator()(const Init* Statement) {
1224 if (typeid(*Statement) == typeid(ListInit)) {
1225 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1226 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1227 B != E; ++B)
1228 this->processDag(*B);
1229 }
1230 else {
1231 this->processDag(Statement);
1232 }
1233 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001234
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001235 void operator()(const DagInit& Test, unsigned, bool) {
1236 this->operator()(&Test);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001237 }
1238 void operator()(const Init* Statement, unsigned) {
1239 this->operator()(Statement);
1240 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001241};
1242
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001243/// CheckForSuperfluousOptions - Check that there are no side
1244/// effect-free options (specified only in the OptionList). Otherwise,
1245/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001246void CheckForSuperfluousOptions (const RecordVector& Edges,
1247 const ToolDescriptions& ToolDescs,
1248 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001249 llvm::StringSet<> nonSuperfluousOptions;
1250
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001251 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001252 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001253 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1254 E = ToolDescs.end(); B != E; ++B) {
1255 const ToolDescription& TD = *(*B);
1256 ExtractOptionNames Callback(nonSuperfluousOptions);
1257 if (TD.Actions)
1258 WalkCase(TD.Actions, Callback, Callback);
1259 }
1260
1261 // Add all options mentioned in the 'case' clauses of the
1262 // OptionalEdges of the compilation graph to the set of
1263 // non-superfluous options.
1264 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1265 B != E; ++B) {
1266 const Record* Edge = *B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001267 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001268
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001269 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001270 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001271 }
1272
1273 // Check that all options in OptDescs belong to the set of
1274 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001275 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001276 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001277 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001278 if (!nonSuperfluousOptions.count(Val.Name)
1279 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001280 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001281 "Probable cause: this option is specified only in the OptionList.\n";
1282 }
1283}
1284
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001285/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1286bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1287 if (TestName == "single_input_file") {
1288 O << "InputFilenames.size() == 1";
1289 return true;
1290 }
1291 else if (TestName == "multiple_input_files") {
1292 O << "InputFilenames.size() > 1";
1293 return true;
1294 }
1295
1296 return false;
1297}
1298
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001299/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1300template <typename F>
1301void EmitListTest(const ListInit& L, const char* LogicOp,
1302 F Callback, raw_ostream& O)
1303{
1304 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1305 // of Dags...
1306 bool isFirst = true;
1307 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1308 if (isFirst)
1309 isFirst = false;
1310 else
Mikhail Glushenkovb7935e02010-01-01 04:40:54 +00001311 O << ' ' << LogicOp << ' ';
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001312 Callback(InitPtrToString(*B), O);
1313 }
1314}
1315
1316// Callbacks for use with EmitListTest.
1317
1318class EmitSwitchOn {
1319 const OptionDescriptions& OptDescs_;
1320public:
1321 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1322 {}
1323
1324 void operator()(const std::string& OptName, raw_ostream& O) const {
1325 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1326 O << OptDesc.GenVariableName();
1327 }
1328};
1329
1330class EmitEmptyTest {
1331 bool EmitNegate_;
1332 const OptionDescriptions& OptDescs_;
1333public:
1334 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1335 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1336 {}
1337
1338 void operator()(const std::string& OptName, raw_ostream& O) const {
1339 const char* Neg = (EmitNegate_ ? "!" : "");
1340 if (OptName == "o") {
1341 O << Neg << "OutputFilename.empty()";
1342 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001343 else if (OptName == "save-temps") {
1344 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1345 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001346 else {
1347 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1348 O << Neg << OptDesc.GenVariableName() << ".empty()";
1349 }
1350 }
1351};
1352
1353
1354/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1355bool EmitCaseTest1ArgList(const std::string& TestName,
1356 const DagInit& d,
1357 const OptionDescriptions& OptDescs,
1358 raw_ostream& O) {
Mikhail Glushenkov3a481e32010-01-01 03:50:51 +00001359 const ListInit& L = InitPtrToList(d.getArg(0));
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001360
1361 if (TestName == "any_switch_on") {
1362 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1363 return true;
1364 }
1365 else if (TestName == "switch_on") {
1366 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1367 return true;
1368 }
1369 else if (TestName == "any_not_empty") {
1370 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1371 return true;
1372 }
1373 else if (TestName == "any_empty") {
1374 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1375 return true;
1376 }
1377 else if (TestName == "not_empty") {
1378 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1379 return true;
1380 }
1381 else if (TestName == "empty") {
1382 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1383 return true;
1384 }
1385
1386 return false;
1387}
1388
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001389/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1390bool EmitCaseTest1ArgStr(const std::string& TestName,
1391 const DagInit& d,
1392 const OptionDescriptions& OptDescs,
1393 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001394 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001395
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001396 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001397 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001398 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001399 }
1400 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001401 O << "InLangs.count(\"" << OptName << "\") != 0";
1402 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001403 }
1404 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001405 // This works only for single-argument Tool::GenerateAction. Join
1406 // tools can process several files in different languages simultaneously.
1407
1408 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001409 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001410 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001411 }
1412 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001413 bool EmitNegate = (TestName == "not_empty");
1414 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001415 return true;
1416 }
1417
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001418 return false;
1419}
1420
1421/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1422bool EmitCaseTest1Arg(const std::string& TestName,
1423 const DagInit& d,
1424 const OptionDescriptions& OptDescs,
1425 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001426 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001427 if (typeid(*d.getArg(0)) == typeid(ListInit))
1428 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1429 else
1430 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1431}
1432
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001433/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001434bool EmitCaseTest2Args(const std::string& TestName,
1435 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001436 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001437 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001438 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001439 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001440 const std::string& OptName = InitPtrToString(d.getArg(0));
1441 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001442
1443 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001444 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001445 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1446 return true;
1447 }
1448 else if (TestName == "element_in_list") {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00001449 const OptionDescription& OptDesc = OptDescs.FindParameterList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001450 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001451 O << "std::find(" << VarName << ".begin(),\n";
1452 O.indent(IndentLevel + Indent1)
1453 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001454 << OptArg << "\") != " << VarName << ".end()";
1455 return true;
1456 }
1457
1458 return false;
1459}
1460
1461// Forward declaration.
1462// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001463void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001464 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001465 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001466
1467/// EmitLogicalOperationTest - Helper function used by
1468/// EmitCaseConstructHandler.
1469void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001470 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001471 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001472 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001473 O << '(';
1474 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001475 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001476 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001477 if (j != NumArgs - 1) {
1478 O << ")\n";
1479 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1480 }
1481 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001482 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001483 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001484 }
1485}
1486
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001487void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001488 const OptionDescriptions& OptDescs, raw_ostream& O)
1489{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001490 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001491 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1492 O << "! (";
1493 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1494 O << ")";
1495}
1496
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001497/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001498void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001499 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001500 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001501 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001502
1503 if (TestName == "and")
1504 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1505 else if (TestName == "or")
1506 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001507 else if (TestName == "not")
1508 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001509 else if (EmitCaseTest0Args(TestName, O))
1510 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001511 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1512 return;
1513 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1514 return;
1515 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001516 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001517}
1518
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001519
1520/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1521class EmitCaseTestCallback {
1522 bool EmitElseIf_;
1523 const OptionDescriptions& OptDescs_;
1524 raw_ostream& O_;
1525public:
1526
1527 EmitCaseTestCallback(bool EmitElseIf,
1528 const OptionDescriptions& OptDescs, raw_ostream& O)
1529 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1530 {}
1531
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001532 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001533 {
1534 if (GetOperatorName(Test) == "default") {
1535 O_.indent(IndentLevel) << "else {\n";
1536 }
1537 else {
1538 O_.indent(IndentLevel)
1539 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001540 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001541 O_ << ") {\n";
1542 }
1543 }
1544};
1545
1546/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1547template <typename F>
1548class EmitCaseStatementCallback {
1549 F Callback_;
1550 raw_ostream& O_;
1551public:
1552
1553 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1554 : Callback_(Callback), O_(O)
1555 {}
1556
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001557 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001558
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001559 // Ignore nested 'case' DAG.
1560 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001561 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case")) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001562 if (typeid(*Statement) == typeid(ListInit)) {
1563 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1564 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1565 B != E; ++B)
1566 Callback_(*B, (IndentLevel + Indent1), O_);
1567 }
1568 else {
1569 Callback_(Statement, (IndentLevel + Indent1), O_);
1570 }
1571 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001572 O_.indent(IndentLevel) << "}\n";
1573 }
1574
1575};
1576
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001577/// EmitCaseConstructHandler - Emit code that handles the 'case'
1578/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001579/// clause. Implemented on top of WalkCase.
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00001580/// Callback's type is void F(const Init* Statement, unsigned IndentLevel,
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001581/// raw_ostream& O).
1582/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001583/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1584/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001585template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001586void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001587 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001588 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001589 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001590 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1591 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001592}
1593
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001594/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001595/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1596/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001597void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001598 const char* Delimiters = " \t\n\v\f\r";
1599 enum TokenizerState
1600 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1601 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001602
1603 if (CmdLine.empty())
1604 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001605 Out.push_back("");
1606
1607 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1608 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001609
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001610 for (; B != E; ++B) {
1611 char cur_ch = CmdLine[B];
1612
1613 switch (cur_st) {
1614 case Normal:
1615 if (cur_ch == '$') {
1616 cur_st = SpecialCommand;
1617 break;
1618 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001619 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001620 // Skip whitespace
1621 B = CmdLine.find_first_not_of(Delimiters, B);
1622 if (B == std::string::npos) {
1623 B = E-1;
1624 continue;
1625 }
1626 --B;
1627 Out.push_back("");
1628 continue;
1629 }
1630 break;
1631
1632
1633 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001634 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001635 cur_st = Normal;
1636 Out.push_back("");
1637 continue;
1638 }
1639 if (cur_ch == '(') {
1640 Out.push_back("");
1641 cur_st = InsideSpecialCommand;
1642 continue;
1643 }
1644 break;
1645
1646 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001647 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001648 continue;
1649 }
1650 if (cur_ch == '\'') {
1651 cur_st = InsideQuotationMarks;
1652 Out.push_back("");
1653 continue;
1654 }
1655 if (cur_ch == ')') {
1656 cur_st = Normal;
1657 Out.push_back("");
1658 }
1659 if (cur_ch == ',') {
1660 continue;
1661 }
1662
1663 break;
1664
1665 case InsideQuotationMarks:
1666 if (cur_ch == '\'') {
1667 cur_st = InsideSpecialCommand;
1668 continue;
1669 }
1670 break;
1671 }
1672
1673 Out.back().push_back(cur_ch);
1674 }
1675}
1676
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001677/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1678/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1679/// SubstituteSpecialCommands().
1680StrVector::const_iterator
1681SubstituteCall (StrVector::const_iterator Pos,
1682 StrVector::const_iterator End,
1683 bool IsJoin, raw_ostream& O)
1684{
1685 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001686 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001687 const std::string& CmdName = *Pos;
1688
1689 if (CmdName == ")")
1690 throw "$CALL invocation: empty argument list!";
1691
1692 O << "hooks::";
1693 O << CmdName << "(";
1694
1695
1696 bool firstIteration = true;
1697 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001698 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001699 const std::string& Arg = *Pos;
1700 assert(Arg.size() != 0);
1701
1702 if (Arg[0] == ')')
1703 break;
1704
1705 if (firstIteration)
1706 firstIteration = false;
1707 else
1708 O << ", ";
1709
1710 if (Arg == "$INFILE") {
1711 if (IsJoin)
1712 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1713 else
1714 O << "inFile.c_str()";
1715 }
1716 else {
1717 O << '"' << Arg << '"';
1718 }
1719 }
1720
1721 O << ')';
1722
1723 return Pos;
1724}
1725
1726/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1727/// function used by SubstituteSpecialCommands().
1728StrVector::const_iterator
1729SubstituteEnv (StrVector::const_iterator Pos,
1730 StrVector::const_iterator End, raw_ostream& O)
1731{
1732 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001733 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001734 const std::string& EnvName = *Pos;
1735
1736 if (EnvName == ")")
1737 throw "$ENV invocation: empty argument list!";
1738
1739 O << "checkCString(std::getenv(\"";
1740 O << EnvName;
1741 O << "\"))";
1742
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001743 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001744
1745 return Pos;
1746}
1747
1748/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1749/// handler code. Helper function used by EmitCmdLineVecFill().
1750StrVector::const_iterator
1751SubstituteSpecialCommands (StrVector::const_iterator Pos,
1752 StrVector::const_iterator End,
1753 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001754{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001755
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001756 const std::string& cmd = *Pos;
1757
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001758 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001759 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001760 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001761 }
1762 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001763 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001764 }
1765 else {
1766 throw "Unknown special command: " + cmd;
1767 }
1768
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001769 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001770 const std::string& Leftover = *Pos;
1771 assert(Leftover.at(0) == ')');
1772 if (Leftover.size() != 1)
1773 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001774
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001775 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001776}
1777
1778/// EmitCmdLineVecFill - Emit code that fills in the command line
1779/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001780void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001781 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001782 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001783 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001784 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001785
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001786 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001787 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001788
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001789 StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001790
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001791 // Emit the command itself.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001792 assert(!StrVec[0].empty());
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001793 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001794 if (StrVec[0][0] == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001795 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1796 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001797 }
1798 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001799 O << '"' << StrVec[0] << '"';
1800 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001801 }
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001802 O << ";\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001803
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001804 // Go through the command arguments.
1805 assert(B <= E);
1806 for (; B != E; ++B) {
1807 const std::string& cmd = *B;
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001808
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001809 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001810 O.indent(IndentLevel);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001811
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001812 if (cmd.at(0) == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001813 O << "vec.push_back(std::make_pair(0, ";
1814 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1815 O << "));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001816 }
1817 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001818 O << "vec.push_back(std::make_pair(0, \"" << cmd << "\"));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001819 }
1820 }
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001821
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001822}
1823
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001824/// EmitForEachListElementCycleHeader - Emit common code for iterating through
1825/// all elements of a list. Helper function used by
1826/// EmitForwardOptionPropertyHandlingCode.
1827void EmitForEachListElementCycleHeader (const OptionDescription& D,
1828 unsigned IndentLevel,
1829 raw_ostream& O) {
1830 unsigned IndentLevel1 = IndentLevel + Indent1;
1831
1832 O.indent(IndentLevel)
1833 << "for (" << D.GenTypeDeclaration()
1834 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1835 O.indent(IndentLevel)
1836 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1837 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1838 << ".getPosition(B - " << D.GenVariableName()
1839 << ".begin());\n";
1840}
1841
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001842/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1843/// implement EmitActionHandler. Emits code for
1844/// handling the (forward) and (forward_as) option properties.
1845void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001846 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001847 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001848 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001849 const std::string& Name = NewName.empty()
1850 ? ("-" + D.Name)
1851 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001852 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001853
1854 switch (D.Type) {
1855 case OptionType::Switch:
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001856 O.indent(IndentLevel)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001857 << "vec.push_back(std::make_pair(" << D.GenVariableName()
1858 << ".getPosition(), \"" << Name << "\"));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001859 break;
1860 case OptionType::Parameter:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001861 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1862 << D.GenVariableName()
1863 <<".getPosition(), \"" << Name;
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001864
1865 if (!D.isForwardNotSplit()) {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001866 O << "\"));\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001867 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1868 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001869 << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001870 }
1871 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001872 O << "=\" + " << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001873 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001874 break;
1875 case OptionType::Prefix:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001876 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1877 << D.GenVariableName() << ".getPosition(), \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001878 << Name << "\" + "
1879 << D.GenVariableName() << "));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001880 break;
1881 case OptionType::PrefixList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001882 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001883 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001884 << Name << "\" + " << "*B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001885 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001886
1887 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001888 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001889 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001890 }
1891
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001892 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001893 break;
1894 case OptionType::ParameterList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001895 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001896 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001897 << Name << "\"));\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001898
1899 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001900 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001901 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001902 }
1903
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001904 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001905 break;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001906 case OptionType::SwitchList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001907 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001908 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
1909 << Name << "\"));\n";
1910 O.indent(IndentLevel1) << "++B;\n";
1911 O.indent(IndentLevel) << "}\n";
1912 break;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001913 case OptionType::Alias:
1914 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001915 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001916 }
1917}
1918
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001919/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1920/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001921struct ActionHandlingCallbackBase
1922{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001923
1924 void onErrorDag(const DagInit& d,
1925 unsigned IndentLevel, raw_ostream& O) const
1926 {
1927 O.indent(IndentLevel)
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00001928 << "PrintError(\""
1929 << (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0)) : "Unknown error!")
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001930 << "\");\n";
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +00001931 O.indent(IndentLevel) << "return 1;\n";
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001932 }
1933
1934 void onWarningDag(const DagInit& d,
1935 unsigned IndentLevel, raw_ostream& O) const
1936 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001937 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001938 O.indent(IndentLevel) << "llvm::errs() << \""
1939 << InitPtrToString(d.getArg(0)) << "\";\n";
1940 }
1941
1942};
1943
1944/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1945/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001946
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001947class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001948
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001949typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1950(const DagInit&, unsigned, raw_ostream&) const;
1951
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001952class EmitActionHandlersCallback :
1953 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001954 public HandlerTable<EmitActionHandlersCallbackHandler>
1955{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001956 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001957
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001958 const OptionDescriptions& OptDescs;
1959
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001960 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1961 /// onAppendCmd and onOutputSuffix.
1962 void EmitHookInvocation(const std::string& Str,
1963 const char* BlockOpen, const char* BlockClose,
1964 unsigned IndentLevel, raw_ostream& O) const
1965 {
1966 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001967 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001968
1969 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1970 B != E; ++B) {
1971 const std::string& cmd = *B;
1972
1973 O.indent(IndentLevel) << BlockOpen;
1974
1975 if (cmd.at(0) == '$')
1976 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1977 else
1978 O << '"' << cmd << '"';
1979
1980 O << BlockClose;
1981 }
1982 }
1983
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001984 void onAppendCmd (const DagInit& Dag,
1985 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001986 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001987 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001988 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001989 "vec.push_back(std::make_pair(65536, ", "));\n",
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001990 IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001991 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001992
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001993 void onForward (const DagInit& Dag,
1994 unsigned IndentLevel, raw_ostream& O) const
1995 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001996 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001997 const std::string& Name = InitPtrToString(Dag.getArg(0));
1998 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1999 IndentLevel, "", O);
2000 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002001
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002002 void onForwardAs (const DagInit& Dag,
2003 unsigned IndentLevel, raw_ostream& O) const
2004 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002005 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002006 const std::string& Name = InitPtrToString(Dag.getArg(0));
2007 const std::string& NewName = InitPtrToString(Dag.getArg(1));
2008 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2009 IndentLevel, NewName, O);
2010 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002011
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002012 void onForwardValue (const DagInit& Dag,
2013 unsigned IndentLevel, raw_ostream& O) const
2014 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002015 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002016 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002017 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002018
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002019 if (D.isSwitchList()) {
2020 throw std::runtime_error
2021 ("forward_value is not allowed with switch_list");
2022 }
2023
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002024 if (D.isParameter()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002025 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2026 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002027 << D.GenVariableName() << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002028 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00002029 else {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002030 O.indent(IndentLevel) << "for (" << D.GenTypeDeclaration()
2031 << "::iterator B = " << D.GenVariableName()
2032 << ".begin(), \n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002033 O.indent(IndentLevel + Indent1) << " E = " << D.GenVariableName()
2034 << ".end(); B != E; ++B)\n";
2035 O.indent(IndentLevel) << "{\n";
2036 O.indent(IndentLevel + Indent1)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002037 << "unsigned pos = " << D.GenVariableName()
2038 << ".getPosition(B - " << D.GenVariableName()
2039 << ".begin());\n";
2040 O.indent(IndentLevel + Indent1)
2041 << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002042 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002043 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002044 }
2045
2046 void onForwardTransformedValue (const DagInit& Dag,
2047 unsigned IndentLevel, raw_ostream& O) const
2048 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002049 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002050 const std::string& Name = InitPtrToString(Dag.getArg(0));
2051 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002052 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002053
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002054 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2055 << D.GenVariableName() << ".getPosition("
2056 << (D.isList() ? "0" : "") << "), "
2057 << "hooks::" << Hook << "(" << D.GenVariableName()
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002058 << (D.isParameter() ? ".c_str()" : "") << ")));\n";
2059 }
2060
2061 void onNoOutFile (const DagInit& Dag,
2062 unsigned IndentLevel, raw_ostream& O) const
2063 {
2064 CheckNumberOfArguments(Dag, 0);
2065 O.indent(IndentLevel) << "no_out_file = true;\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002066 }
2067
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002068 void onOutputSuffix (const DagInit& Dag,
2069 unsigned IndentLevel, raw_ostream& O) const
2070 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002071 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002072 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
2073 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002074 }
2075
2076 void onStopCompilation (const DagInit& Dag,
2077 unsigned IndentLevel, raw_ostream& O) const
2078 {
2079 O.indent(IndentLevel) << "stop_compilation = true;\n";
2080 }
2081
2082
2083 void onUnpackValues (const DagInit& Dag,
2084 unsigned IndentLevel, raw_ostream& O) const
2085 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002086 throw "'unpack_values' is deprecated. "
2087 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002088 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002089
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002090 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002091
2092 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
2093 : OptDescs(OD)
2094 {
2095 if (!staticMembersInitialized_) {
2096 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
2097 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
2098 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
2099 AddHandler("forward", &EmitActionHandlersCallback::onForward);
2100 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002101 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
2102 AddHandler("forward_transformed_value",
2103 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002104 AddHandler("no_out_file",
2105 &EmitActionHandlersCallback::onNoOutFile);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002106 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
2107 AddHandler("stop_compilation",
2108 &EmitActionHandlersCallback::onStopCompilation);
2109 AddHandler("unpack_values",
2110 &EmitActionHandlersCallback::onUnpackValues);
2111
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002112
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002113 staticMembersInitialized_ = true;
2114 }
2115 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002116
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002117 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002118 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002119 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002120 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002121 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002122};
2123
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002124void EmitGenerateActionMethodHeader(const ToolDescription& D,
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002125 bool IsJoin, bool Naked,
2126 raw_ostream& O)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002127{
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002128 O.indent(Indent1) << "int GenerateAction(Action& Out,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002129
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002130 if (IsJoin)
2131 O.indent(Indent2) << "const PathVector& inFiles,\n";
2132 else
2133 O.indent(Indent2) << "const sys::Path& inFile,\n";
2134
2135 O.indent(Indent2) << "const bool HasChildren,\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002136 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2137 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2138 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2139 O.indent(Indent1) << "{\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002140
2141 if (!Naked) {
2142 O.indent(Indent2) << "std::string cmd;\n";
2143 O.indent(Indent2) << "std::string out_file;\n";
2144 O.indent(Indent2)
2145 << "std::vector<std::pair<unsigned, std::string> > vec;\n";
2146 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2147 O.indent(Indent2) << "bool no_out_file = false;\n";
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002148 O.indent(Indent2) << "std::string output_suffix(\""
2149 << D.OutputSuffix << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002150 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002151}
2152
2153// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2154// Tool::GenerateAction() method.
2155void EmitGenerateActionMethod (const ToolDescription& D,
2156 const OptionDescriptions& OptDescs,
2157 bool IsJoin, raw_ostream& O) {
2158
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002159 EmitGenerateActionMethodHeader(D, IsJoin, /* Naked = */ false, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002160
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002161 if (!D.CmdLine)
2162 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002163
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002164 // Process the 'command' property.
2165 O << '\n';
2166 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2167 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002168
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002169 // Process the 'actions' list of this tool.
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002170 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002171 EmitCaseConstructHandler(D.Actions, Indent2,
2172 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002173 false, OptDescs, O);
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002174 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002175
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002176 // Input file (s)
2177 if (!D.InFileOption.empty()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002178 O.indent(Indent2)
2179 << "vec.push_back(std::make_pair(InputFilenames.getPosition(0), \""
2180 << D.InFileOption << "\");\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002181 }
2182
2183 if (IsJoin) {
2184 O.indent(Indent2)
2185 << "for (PathVector::const_iterator B = inFiles.begin(),\n";
2186 O.indent(Indent3) << "E = inFiles.end(); B != E; ++B)\n";
2187 O.indent(Indent2) << "{\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002188 O.indent(Indent3) << "vec.push_back(std::make_pair("
2189 << "InputFilenames.getPosition(B - inFiles.begin()), "
2190 << "B->str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002191 O.indent(Indent2) << "}\n";
2192 }
2193 else {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002194 O.indent(Indent2) << "vec.push_back(std::make_pair("
2195 << "InputFilenames.getPosition(0), inFile.str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002196 }
2197
2198 // Output file
2199 O.indent(Indent2) << "if (!no_out_file) {\n";
2200 if (!D.OutFileOption.empty())
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002201 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002202 << D.OutFileOption << "\"));\n";
2203
2204 O.indent(Indent3) << "out_file = this->OutFilename("
2205 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002206 O.indent(Indent4) <<
2207 "TempDir, stop_compilation, output_suffix.c_str()).str();\n\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002208 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, out_file));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002209
2210 O.indent(Indent2) << "}\n\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002211
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002212 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002213 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002214 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002215 O.indent(Indent3) << "for (cl::list<std::string>::iterator B = "
2216 << SinkOptionName << ".begin(), E = " << SinkOptionName
2217 << ".end(); B != E; ++B)\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002218 O.indent(Indent4) << "vec.push_back(std::make_pair(" << SinkOptionName
2219 << ".getPosition(B - " << SinkOptionName
2220 << ".begin()), *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002221 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002222 }
2223
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002224 O.indent(Indent2) << "Out.Construct(cmd, this->SortArgs(vec), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002225 << "stop_compilation, out_file);\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002226 O.indent(Indent2) << "return 0;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002227 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002228}
2229
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002230/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2231/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002232void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2233 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002234 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002235 if (!ToolDesc.isJoin()) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002236 EmitGenerateActionMethodHeader(ToolDesc, /* IsJoin = */ true,
2237 /* Naked = */ true, O);
2238 O.indent(Indent2) << "PrintError(\"" << ToolDesc.Name
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002239 << " is not a Join tool!\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002240 O.indent(Indent2) << "return -1;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002241 O.indent(Indent1) << "}\n\n";
2242 }
2243 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002244 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002245 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002246
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002247 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002248}
2249
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002250/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2251/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002252void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002253 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2254 O.indent(Indent2) << "return InputLanguages_;\n";
2255 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002256
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002257 if (D.OutLanguage.empty())
2258 throw "Tool " + D.Name + " has no 'out_language' property!";
2259
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002260 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2261 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2262 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002263}
2264
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002265/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002266void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002267 O.indent(Indent1) << "const char* Name() const {\n";
2268 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2269 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002270}
2271
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002272/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2273/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002274void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002275 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002276 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002277 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002278 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002279 O.indent(Indent2) << "return false;\n";
2280 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002281}
2282
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002283/// EmitWorksOnEmptyCallback - Callback used by EmitWorksOnEmptyMethod in
2284/// conjunction with EmitCaseConstructHandler.
2285void EmitWorksOnEmptyCallback (const Init* Value,
2286 unsigned IndentLevel, raw_ostream& O) {
2287 CheckBooleanConstant(Value);
2288 O.indent(IndentLevel) << "return " << Value->getAsString() << ";\n";
2289}
2290
2291/// EmitWorksOnEmptyMethod - Emit the WorksOnEmpty() method for a given Tool
2292/// class.
2293void EmitWorksOnEmptyMethod (const ToolDescription& D,
2294 const OptionDescriptions& OptDescs,
2295 raw_ostream& O)
2296{
2297 O.indent(Indent1) << "bool WorksOnEmpty() const {\n";
2298 if (D.OnEmpty == 0)
2299 O.indent(Indent2) << "return false;\n";
2300 else
2301 EmitCaseConstructHandler(D.OnEmpty, Indent2, EmitWorksOnEmptyCallback,
2302 /*EmitElseIf = */ true, OptDescs, O);
2303 O.indent(Indent1) << "}\n\n";
2304}
2305
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002306/// EmitStaticMemberDefinitions - Emit static member definitions for a
2307/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002308void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002309 if (D.InLanguage.empty())
2310 throw "Tool " + D.Name + " has no 'in_language' property!";
2311
2312 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2313 for (StrVector::const_iterator B = D.InLanguage.begin(),
2314 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002315 O << '\"' << *B << "\", ";
2316 O << "0};\n\n";
2317}
2318
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002319/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002320void EmitToolClassDefinition (const ToolDescription& D,
2321 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002322 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002323 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002324 return;
2325
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002326 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002327 O << "class " << D.Name << " : public ";
2328 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002329 O << "JoinTool";
2330 else
2331 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002332
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002333 O << " {\nprivate:\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002334 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002335
2336 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002337 EmitNameMethod(D, O);
2338 EmitInOutLanguageMethods(D, O);
2339 EmitIsJoinMethod(D, O);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002340 EmitWorksOnEmptyMethod(D, OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002341 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002342
2343 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002344 O << "};\n";
2345
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002346 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002347
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002348}
2349
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002350/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002351/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002352void EmitOptionDefinitions (const OptionDescriptions& descs,
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002353 bool HasSink, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002354{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002355 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002356
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002357 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002358 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002359 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002360 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002361
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002362 if (val.Type == OptionType::Alias) {
2363 Aliases.push_back(val);
2364 continue;
2365 }
2366
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002367 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002368 << val.GenVariableName();
2369
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002370 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002371
2372 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2373 O << ", cl::Prefix";
2374
2375 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002376 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002377 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002378 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002379 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002380 }
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002381
2382 if (val.isOptional())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002383 O << ", cl::Optional";
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002384
2385 if (val.isOneOrMore())
2386 O << ", cl::OneOrMore";
2387
2388 if (val.isZeroOrMore())
2389 O << ", cl::ZeroOrMore";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002390
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002391 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002392 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002393 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002394 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002395
2396 if (val.isCommaSeparated())
2397 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002398
2399 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002400 O << ", cl::multi_val(" << val.MultiVal << ')';
2401
2402 if (val.InitVal) {
2403 const std::string& str = val.InitVal->getAsString();
2404 O << ", cl::init(" << str << ')';
2405 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002406
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002407 if (!val.Help.empty())
2408 O << ", cl::desc(\"" << val.Help << "\")";
2409
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002410 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002411 }
2412
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002413 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002414 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002415 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002416 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002417
2418 O << val.GenTypeDeclaration() << ' '
2419 << val.GenVariableName()
2420 << "(\"" << val.Name << '\"';
2421
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002422 const OptionDescription& D = descs.FindOption(val.Help);
2423 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002424
2425 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2426 }
2427
2428 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002429 if (HasSink)
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002430 O << "cl" << "::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002431
2432 O << '\n';
2433}
2434
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002435/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002436/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002437
2438class EmitPreprocessOptionsCallback;
2439
2440typedef void
2441(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2442(const DagInit&, unsigned, raw_ostream&) const;
2443
2444class EmitPreprocessOptionsCallback :
2445 public ActionHandlingCallbackBase,
2446 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2447{
2448 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002449 typedef void
2450 (EmitPreprocessOptionsCallback::* HandlerImpl)
2451 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002452
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002453 const OptionDescriptions& OptDescs_;
2454
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002455 void onListOrDag(const DagInit& d, HandlerImpl h,
2456 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002457 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002458 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002459 const Init* I = d.getArg(0);
2460
2461 // If I is a list, apply h to each element.
2462 if (typeid(*I) == typeid(ListInit)) {
2463 const ListInit& L = *static_cast<const ListInit*>(I);
2464 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B)
2465 ((this)->*(h))(*B, IndentLevel, O);
2466 }
2467 // Otherwise, apply h to I.
2468 else {
2469 ((this)->*(h))(I, IndentLevel, O);
2470 }
2471 }
2472
2473 void onUnsetOptionImpl(const Init* I,
2474 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002475 {
2476 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002477 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002478
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002479 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002480 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2481 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002482 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002483 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2484 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002485 else if (OptDesc.isList()) {
2486 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2487 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002488 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002489 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002490 }
2491 }
2492
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002493 void onUnsetOption(const DagInit& d,
2494 unsigned IndentLevel, raw_ostream& O) const
2495 {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002496 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2497 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002498 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002499
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002500 void onSetOptionImpl(const DagInit& d,
2501 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002502 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002503 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002504 const Init* Value = d.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002505 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2506
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002507 if (OptDesc.isList()) {
2508 const ListInit& List = InitPtrToList(Value);
2509
2510 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2511 for (ListInit::const_iterator B = List.begin(), E = List.end();
2512 B != E; ++B) {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002513 const Init* CurElem = *B;
2514 if (OptDesc.isSwitchList())
2515 CheckBooleanConstant(CurElem);
2516
2517 O.indent(IndentLevel)
2518 << OptDesc.GenVariableName() << ".push_back(\""
2519 << (OptDesc.isSwitchList() ? CurElem->getAsString()
2520 : InitPtrToString(CurElem))
2521 << "\");\n";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002522 }
2523 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002524 else if (OptDesc.isSwitch()) {
2525 CheckBooleanConstant(Value);
2526 O.indent(IndentLevel) << OptDesc.GenVariableName()
2527 << " = " << Value->getAsString() << ";\n";
2528 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002529 else if (OptDesc.isParameter()) {
2530 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002531 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002532 << " = \"" << Str << "\";\n";
2533 }
2534 else {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002535 throw "Can't apply 'set_option' to alias option -" + OptName + " !";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002536 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002537 }
2538
2539 void onSetSwitch(const Init* I,
2540 unsigned IndentLevel, raw_ostream& O) const {
2541 const std::string& OptName = InitPtrToString(I);
2542 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2543
2544 if (OptDesc.isSwitch())
2545 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2546 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002547 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002548 }
2549
2550 void onSetOption(const DagInit& d,
2551 unsigned IndentLevel, raw_ostream& O) const
2552 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002553 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002554
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002555 // Two arguments: (set_option "parameter", VALUE), where VALUE can be a
2556 // boolean, a string or a string list.
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002557 if (d.getNumArgs() > 1)
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002558 this->onSetOptionImpl(d, IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002559 // One argument: (set_option "switch")
2560 // or (set_option ["switch1", "switch2", ...])
2561 else
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002562 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2563 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002564 }
2565
2566public:
2567
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002568 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002569 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002570 {
2571 if (!staticMembersInitialized_) {
2572 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2573 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2574 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002575 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002576
2577 staticMembersInitialized_ = true;
2578 }
2579 }
2580
2581 void operator()(const Init* I,
2582 unsigned IndentLevel, raw_ostream& O) const
2583 {
2584 InvokeDagInitHandler(this, I, IndentLevel, O);
2585 }
2586
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002587};
2588
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002589/// EmitPreprocessOptions - Emit the PreprocessOptions() function.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002590void EmitPreprocessOptions (const RecordKeeper& Records,
2591 const OptionDescriptions& OptDecs, raw_ostream& O)
2592{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002593 O << "int PreprocessOptions () {\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002594
2595 const RecordVector& OptionPreprocessors =
2596 Records.getAllDerivedDefinitions("OptionPreprocessor");
2597
2598 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2599 E = OptionPreprocessors.end(); B!=E; ++B) {
2600 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002601 EmitCaseConstructHandler(Case, Indent1,
2602 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002603 false, OptDecs, O);
2604 }
2605
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002606 O << '\n';
2607 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002608 O << "}\n\n";
2609}
2610
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002611/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002612void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002613{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002614 O << "int PopulateLanguageMap (LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002615
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002616 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002617 // TODO: change this to getAllDerivedDefinitions.
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002618 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002619
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002620 // It is allowed for a plugin to have no language map.
2621 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002622
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002623 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2624 if (!LangsToSuffixesList)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00002625 throw "Error in the language map definition!";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002626
2627 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002628 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002629
2630 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2631 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2632
2633 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002634 O.indent(Indent1) << "langMap[\""
2635 << InitPtrToString(Suffixes->getElement(i))
2636 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002637 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002638 }
2639
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002640 O << '\n';
2641 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002642 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002643}
2644
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002645/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2646/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002647void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002648 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002649 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002650 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002651
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002652 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002653 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002654 }
2655 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002656 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002657 }
2658 else if (OpName == "error") {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002659 // TODO: fix this
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002660 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002661 O.indent(IndentLevel) << "PrintError(\""
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002662 << InitPtrToString(d.getArg(0))
2663 << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002664 O.indent(IndentLevel) << "return -1;\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002665 return;
2666 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002667 else {
2668 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002669 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002670 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002671
2672 if (d.getNumArgs() > 0)
2673 O << InitPtrToInt(d.getArg(0)) << ";\n";
2674 else
2675 O << "2;\n";
2676
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002677}
2678
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002679/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002680void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002681 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002682 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002683
2684 // Class constructor.
2685 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002686 << "public:\n";
2687 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2688 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002689
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002690 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002691 O.indent(Indent1)
2692 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2693 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002694
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002695 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002696 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002697
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002698 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002699 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002700}
2701
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002702/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002703void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002704 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002705 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002706 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002707 for (RecordVector::const_iterator B = EdgeVector.begin(),
2708 E = EdgeVector.end(); B != E; ++B) {
2709 const Record* Edge = *B;
2710 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002711 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002712
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002713 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002714 EmitEdgeClass(i, NodeB, &Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002715 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002716 }
2717}
2718
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002719/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph() function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002720void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002721 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002722 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002723{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002724 O << "int PopulateCompilationGraph (CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002725
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002726 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2727 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002728 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002729
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002730 O << '\n';
2731
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002732 // Insert edges.
2733
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002734 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002735 for (RecordVector::const_iterator B = EdgeVector.begin(),
2736 E = EdgeVector.end(); B != E; ++B) {
2737 const Record* Edge = *B;
2738 const std::string& NodeA = Edge->getValueAsString("a");
2739 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002740 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002741
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002742 O.indent(Indent1) << "if (int ret = G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002743
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002744 if (IsDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002745 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002746 else
2747 O << "new Edge" << i << "()";
2748
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002749 O << "))\n";
2750 O.indent(Indent2) << "return ret;\n";
2751
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002752 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002753 }
2754
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002755 O << '\n';
2756 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002757 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002758}
2759
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002760/// HookInfo - Information about the hook type and number of arguments.
2761struct HookInfo {
2762
2763 // A hook can either have a single parameter of type std::vector<std::string>,
2764 // or NumArgs parameters of type const char*.
2765 enum HookType { ListHook, ArgHook };
2766
2767 HookType Type;
2768 unsigned NumArgs;
2769
2770 HookInfo() : Type(ArgHook), NumArgs(1)
2771 {}
2772
2773 HookInfo(HookType T) : Type(T), NumArgs(1)
2774 {}
2775
2776 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2777 {}
2778};
2779
2780typedef llvm::StringMap<HookInfo> HookInfoMap;
2781
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002782/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002783/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002784/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002785class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002786 HookInfoMap& HookNames_;
2787 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002788public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002789 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2790 : HookNames_(HookNames), OptDescs_(OptDescs)
2791 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002792
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002793 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002794 const std::string& Name = GetOperatorName(Dag);
2795
2796 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002797 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002798 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2799 const std::string& HookName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002800 const OptionDescription& D =
2801 OptDescs_.FindParameterListOrParameter(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002802
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002803 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2804 : HookInfo::ArgHook);
2805 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002806 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002807 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002808 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2809 }
2810 }
2811
2812 void onCmdLine(const std::string& Cmd) {
2813 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002814 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002815
2816 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2817 B != E; ++B) {
2818 const std::string& cmd = *B;
2819
2820 if (cmd == "$CALL") {
2821 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002822 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002823 const std::string& HookName = *B;
2824
2825 if (HookName.at(0) == ')')
2826 throw "$CALL invoked with no arguments!";
2827
2828 while (++B != E && B->at(0) != ')') {
2829 ++NumArgs;
2830 }
2831
2832 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2833
2834 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2835 H->second.Type != HookInfo::ArgHook)
2836 throw "Overloading of hooks is not allowed. Overloaded hook: "
2837 + HookName;
2838 else
2839 HookNames_[HookName] = HookInfo(NumArgs);
2840 }
2841 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002842 }
2843
2844 void operator()(const Init* Arg) {
2845
2846 // We're invoked on an action (either a dag or a dag list).
2847 if (typeid(*Arg) == typeid(DagInit)) {
2848 const DagInit& Dag = InitPtrToDag(Arg);
2849 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002850 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002851 }
2852 else if (typeid(*Arg) == typeid(ListInit)) {
2853 const ListInit& List = InitPtrToList(Arg);
2854 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2855 ++B) {
2856 const DagInit& Dag = InitPtrToDag(*B);
2857 this->onAction(Dag);
2858 }
2859 return;
2860 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002861
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002862 // We're invoked on a command line.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002863 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002864 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002865
2866 void operator()(const DagInit* Test, unsigned, bool) {
2867 this->operator()(Test);
2868 }
2869 void operator()(const Init* Statement, unsigned) {
2870 this->operator()(Statement);
2871 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002872};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002873
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002874/// FillInHookNames - Actually extract the hook names from all command
2875/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002876void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002877 const OptionDescriptions& OptDescs,
2878 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002879{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002880 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002881 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2882 E = ToolDescs.end(); B != E; ++B) {
2883 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002884
2885 // Look for 'forward_transformed_value' in 'actions'.
2886 if (D.Actions)
2887 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2888
2889 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002890 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002891 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002892 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002893 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002894 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002895 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002896 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002897 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002898 }
2899}
2900
2901/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2902/// property records and emit hook function declaration for each
2903/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002904void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2905 const OptionDescriptions& OptDescs, raw_ostream& O) {
2906 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002907
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002908 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002909 if (HookNames.empty())
2910 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002911
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002912 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002913 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002914 const char* HookName = B->first();
2915 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002916
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002917 O.indent(Indent1) << "std::string " << HookName << "(";
2918
2919 if (Info.Type == HookInfo::ArgHook) {
2920 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2921 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2922 }
2923 }
2924 else {
2925 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002926 }
2927
2928 O <<");\n";
2929 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002930}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002931
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002932/// EmitIncludes - Emit necessary #include directives and some
2933/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002934void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002935 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2936 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002937 << "#include \"llvm/CompilerDriver/Error.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002938 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002939
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002940 << "#include \"llvm/Support/CommandLine.h\"\n"
2941 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002942
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002943 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002944 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002945 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002946 << "#include <stdexcept>\n\n"
2947
2948 << "using namespace llvm;\n"
2949 << "using namespace llvmc;\n\n"
2950
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002951 << "inline const char* checkCString(const char* s)\n"
2952 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002953}
2954
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002955
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002956/// PluginData - Holds all information about a plugin.
2957struct PluginData {
2958 OptionDescriptions OptDescs;
2959 bool HasSink;
2960 ToolDescriptions ToolDescs;
2961 RecordVector Edges;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002962};
2963
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002964/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002965/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002966bool HasSink(const ToolDescriptions& ToolDescs) {
2967 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2968 E = ToolDescs.end(); B != E; ++B)
2969 if ((*B)->isSink())
2970 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002971
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002972 return false;
2973}
2974
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002975/// CollectPluginData - Collect compilation graph edges, tool properties and
2976/// option properties from the parse tree.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002977void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2978 // Collect option properties.
2979 const RecordVector& OptionLists =
2980 Records.getAllDerivedDefinitions("OptionList");
2981 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2982 Data.OptDescs);
2983
2984 // Collect tool properties.
2985 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2986 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2987 Data.HasSink = HasSink(Data.ToolDescs);
2988
2989 // Collect compilation graph edges.
2990 const RecordVector& CompilationGraphs =
2991 Records.getAllDerivedDefinitions("CompilationGraph");
2992 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2993 Data.Edges);
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002994}
2995
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002996/// CheckPluginData - Perform some sanity checks on the collected data.
2997void CheckPluginData(PluginData& Data) {
2998 // Filter out all tools not mentioned in the compilation graph.
2999 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003000
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003001 // Typecheck the compilation graph.
3002 TypecheckGraph(Data.Edges, Data.ToolDescs);
3003
3004 // Check that there are no options without side effects (specified
3005 // only in the OptionList).
3006 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003007}
3008
Daniel Dunbar1a551802009-07-03 00:10:29 +00003009void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003010 // Emit file header.
3011 EmitIncludes(O);
3012
3013 // Emit global option registration code.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003014 O << "namespace llvmc {\n"
3015 << "namespace autogenerated {\n\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003016 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003017 O << "} // End namespace autogenerated.\n"
3018 << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003019
3020 // Emit hook declarations.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003021 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003022 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003023 O << "} // End namespace hooks.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003024
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003025 O << "namespace {\n\n";
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003026 O << "using namespace llvmc::autogenerated;\n\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003027
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003028 // Emit Tool classes.
3029 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
3030 E = Data.ToolDescs.end(); B!=E; ++B)
3031 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
3032
3033 // Emit Edge# classes.
3034 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
3035
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003036 O << "} // End anonymous namespace.\n\n";
3037
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003038 O << "namespace llvmc {\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003039 O << "namespace autogenerated {\n\n";
3040
3041 // Emit PreprocessOptions() function.
3042 EmitPreprocessOptions(Records, Data.OptDescs, O);
3043
3044 // Emit PopulateLanguageMap() function
3045 // (language map maps from file extensions to language names).
3046 EmitPopulateLanguageMap(Records, O);
3047
3048 // Emit PopulateCompilationGraph() function.
3049 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
3050
3051 O << "} // End namespace autogenerated.\n";
3052 O << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003053
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003054 // EOF
3055}
3056
3057
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003058// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00003059}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003060
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003061/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00003062void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003063 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003064 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003065
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003066 CollectPluginData(Records, Data);
3067 CheckPluginData(Data);
3068
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00003069 this->EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003070 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003071
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003072 } catch (std::exception& Error) {
3073 throw Error.what() + std::string(" - usually this means a syntax error.");
3074 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003075}