blob: 6572595862bc351408c930abfc68e0cba2208046 [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
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +000028
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000029using namespace llvm;
30
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000031namespace {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000032
33//===----------------------------------------------------------------------===//
34/// Typedefs
35
36typedef std::vector<Record*> RecordVector;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +000037typedef std::vector<const DagInit*> DagVector;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000038typedef std::vector<std::string> StrVector;
39
40//===----------------------------------------------------------------------===//
41/// Constants
42
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +000043// Indentation.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000044const unsigned TabWidth = 4;
45const unsigned Indent1 = TabWidth*1;
46const unsigned Indent2 = TabWidth*2;
47const unsigned Indent3 = TabWidth*3;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +000048const unsigned Indent4 = TabWidth*4;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000049
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000050// Default help string.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000051const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000052
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000053// Name for the "sink" option.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +000054const char * const SinkOptionName = "SinkOption";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000055
56//===----------------------------------------------------------------------===//
57/// Helper functions
58
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000059/// Id - An 'identity' function object.
60struct Id {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000061 template<typename T0>
62 void operator()(const T0&) const {
63 }
64 template<typename T0, typename T1>
65 void operator()(const T0&, const T1&) const {
66 }
67 template<typename T0, typename T1, typename T2>
68 void operator()(const T0&, const T1&, const T2&) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000069 }
70};
71
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000072int InitPtrToInt(const Init* ptr) {
73 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000074 return val.getValue();
75}
76
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000077const std::string& InitPtrToString(const Init* ptr) {
78 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
79 return val.getValue();
80}
81
82const ListInit& InitPtrToList(const Init* ptr) {
83 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
84 return val;
85}
86
87const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000088 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000089 return val;
90}
91
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000092const std::string GetOperatorName(const DagInit& D) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +000093 return D.getOperator()->getAsString();
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000094}
95
Mikhail Glushenkove0b65702009-12-23 12:49:30 +000096/// CheckBooleanConstant - Check that the provided value is a boolean constant.
97void CheckBooleanConstant(const Init* I) {
98 const DefInit& val = dynamic_cast<const DefInit&>(*I);
99 const std::string& str = val.getAsString();
100
101 if (str != "true" && str != "false") {
102 throw "Incorrect boolean value: '" + str +
103 "': must be either 'true' or 'false'";
104 }
105}
106
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000107// CheckNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +0000108// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000109void CheckNumberOfArguments (const DagInit& d, unsigned minArgs) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000110 if (d.getNumArgs() < minArgs)
111 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +0000112}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000113
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000114// EscapeVariableName - Escape commas and other symbols not allowed
115// in the C++ variable names. Makes it possible to use options named
116// like "Wa," (useful for prefix options).
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000117std::string EscapeVariableName (const std::string& Var) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000118 std::string ret;
119 for (unsigned i = 0; i != Var.size(); ++i) {
120 char cur_char = Var[i];
121 if (cur_char == ',') {
122 ret += "_comma_";
123 }
124 else if (cur_char == '+') {
125 ret += "_plus_";
126 }
127 else if (cur_char == '-') {
128 ret += "_dash_";
129 }
130 else {
131 ret.push_back(cur_char);
132 }
133 }
134 return ret;
135}
136
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000137/// EscapeQuotes - Replace '"' with '\"'.
138std::string EscapeQuotes (const std::string& Var) {
139 std::string ret;
140 for (unsigned i = 0; i != Var.size(); ++i) {
141 char cur_char = Var[i];
142 if (cur_char == '"') {
143 ret += "\\\"";
144 }
145 else {
146 ret.push_back(cur_char);
147 }
148 }
149 return ret;
150}
151
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000152/// OneOf - Does the input string contain this character?
153bool OneOf(const char* lst, char c) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000154 while (*lst) {
155 if (*lst++ == c)
156 return true;
157 }
158 return false;
159}
160
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000161template <class I, class S>
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000162void CheckedIncrement(I& P, I E, S ErrorString) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000163 ++P;
164 if (P == E)
165 throw ErrorString;
166}
167
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000168//===----------------------------------------------------------------------===//
169/// Back-end specific code
170
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000171
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000172/// OptionType - One of six different option types. See the
173/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000174namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000175
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000176 enum OptionType { Alias, Switch, SwitchList,
177 Parameter, ParameterList, Prefix, PrefixList };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000178
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000179 bool IsAlias(OptionType t) {
180 return (t == Alias);
181 }
182
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000183 bool IsList (OptionType t) {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000184 return (t == SwitchList || t == ParameterList || t == PrefixList);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000185 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000186
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000187 bool IsSwitch (OptionType t) {
188 return (t == Switch);
189 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000190
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000191 bool IsSwitchList (OptionType t) {
192 return (t == SwitchList);
193 }
194
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000195 bool IsParameter (OptionType t) {
196 return (t == Parameter || t == Prefix);
197 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000198
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000199}
200
201OptionType::OptionType stringToOptionType(const std::string& T) {
202 if (T == "alias_option")
203 return OptionType::Alias;
204 else if (T == "switch_option")
205 return OptionType::Switch;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000206 else if (T == "switch_list_option")
207 return OptionType::SwitchList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000208 else if (T == "parameter_option")
209 return OptionType::Parameter;
210 else if (T == "parameter_list_option")
211 return OptionType::ParameterList;
212 else if (T == "prefix_option")
213 return OptionType::Prefix;
214 else if (T == "prefix_list_option")
215 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000216 else
217 throw "Unknown option type: " + T + '!';
218}
219
220namespace OptionDescriptionFlags {
221 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000222 ReallyHidden = 0x4, OneOrMore = 0x8,
223 Optional = 0x10, CommaSeparated = 0x20,
224 ForwardNotSplit = 0x40, ZeroOrMore = 0x80 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000225}
226
227/// OptionDescription - Represents data contained in a single
228/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000229struct OptionDescription {
230 OptionType::OptionType Type;
231 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000232 unsigned Flags;
233 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000234 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000235 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000236
237 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000238 const std::string& n = "",
239 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000240 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000241 {}
242
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000243 /// GenTypeDeclaration - Returns the C++ variable type of this
244 /// option.
245 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000246
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000247 /// GenVariableName - Returns the variable name used in the
248 /// generated C++ code.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000249 std::string GenVariableName() const
250 { return "autogenerated::" + GenOptionType() + EscapeVariableName(Name); }
251
252 /// GenPlainVariableName - Returns the variable name without the namespace
253 /// prefix.
254 std::string GenPlainVariableName() const
255 { return GenOptionType() + EscapeVariableName(Name); }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000256
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000257 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000258 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000259
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000260 /// CheckConsistency - Check that the flags are consistent.
261 void CheckConsistency() const;
262
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000263 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000264
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000265 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000266
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000267 bool isMultiVal() const;
268
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000269 bool isCommaSeparated() const;
270 void setCommaSeparated();
271
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000272 bool isForwardNotSplit() const;
273 void setForwardNotSplit();
274
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000275 bool isRequired() const;
276 void setRequired();
277
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000278 bool isOneOrMore() const;
279 void setOneOrMore();
280
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000281 bool isZeroOrMore() const;
282 void setZeroOrMore();
283
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000284 bool isOptional() const;
285 void setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000286
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000287 bool isHidden() const;
288 void setHidden();
289
290 bool isReallyHidden() const;
291 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000292
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000293 bool isSwitch() const
294 { return OptionType::IsSwitch(this->Type); }
295
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000296 bool isSwitchList() const
297 { return OptionType::IsSwitchList(this->Type); }
298
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000299 bool isParameter() const
300 { return OptionType::IsParameter(this->Type); }
301
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000302 bool isList() const
303 { return OptionType::IsList(this->Type); }
304
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000305 bool isParameterList() const
306 { return (OptionType::IsList(this->Type)
307 && !OptionType::IsSwitchList(this->Type)); }
308
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000309private:
310
311 // GenOptionType - Helper function used by GenVariableName().
312 std::string GenOptionType() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000313};
314
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000315void OptionDescription::CheckConsistency() const {
316 unsigned i = 0;
317
318 i += this->isRequired();
319 i += this->isOptional();
320 i += this->isOneOrMore();
321 i += this->isZeroOrMore();
322
323 if (i > 1) {
324 throw "Only one of (required), (optional), (one_or_more) or "
325 "(zero_or_more) properties is allowed!";
326 }
327}
328
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000329void OptionDescription::Merge (const OptionDescription& other)
330{
331 if (other.Type != Type)
332 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000333
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000334 if (Help == other.Help || Help == DefaultHelpString)
335 Help = other.Help;
336 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000337 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000338 " defined for option " + Name + "\n";
339 }
340
341 Flags |= other.Flags;
342}
343
344bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000345 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000346}
347
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000348bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000349 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000350}
351
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000352bool OptionDescription::isCommaSeparated() const {
353 return Flags & OptionDescriptionFlags::CommaSeparated;
354}
355void OptionDescription::setCommaSeparated() {
356 Flags |= OptionDescriptionFlags::CommaSeparated;
357}
358
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000359bool OptionDescription::isForwardNotSplit() const {
360 return Flags & OptionDescriptionFlags::ForwardNotSplit;
361}
362void OptionDescription::setForwardNotSplit() {
363 Flags |= OptionDescriptionFlags::ForwardNotSplit;
364}
365
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000366bool OptionDescription::isRequired() const {
367 return Flags & OptionDescriptionFlags::Required;
368}
369void OptionDescription::setRequired() {
370 Flags |= OptionDescriptionFlags::Required;
371}
372
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000373bool OptionDescription::isOneOrMore() const {
374 return Flags & OptionDescriptionFlags::OneOrMore;
375}
376void OptionDescription::setOneOrMore() {
377 Flags |= OptionDescriptionFlags::OneOrMore;
378}
379
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000380bool OptionDescription::isZeroOrMore() const {
381 return Flags & OptionDescriptionFlags::ZeroOrMore;
382}
383void OptionDescription::setZeroOrMore() {
384 Flags |= OptionDescriptionFlags::ZeroOrMore;
385}
386
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000387bool OptionDescription::isOptional() const {
388 return Flags & OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000389}
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000390void OptionDescription::setOptional() {
391 Flags |= OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000392}
393
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000394bool OptionDescription::isHidden() const {
395 return Flags & OptionDescriptionFlags::Hidden;
396}
397void OptionDescription::setHidden() {
398 Flags |= OptionDescriptionFlags::Hidden;
399}
400
401bool OptionDescription::isReallyHidden() const {
402 return Flags & OptionDescriptionFlags::ReallyHidden;
403}
404void OptionDescription::setReallyHidden() {
405 Flags |= OptionDescriptionFlags::ReallyHidden;
406}
407
408const char* OptionDescription::GenTypeDeclaration() const {
409 switch (Type) {
410 case OptionType::Alias:
411 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000412 case OptionType::PrefixList:
413 case OptionType::ParameterList:
414 return "cl::list<std::string>";
415 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000416 return "cl::opt<bool>";
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000417 case OptionType::SwitchList:
418 return "cl::list<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000419 case OptionType::Parameter:
420 case OptionType::Prefix:
421 default:
422 return "cl::opt<std::string>";
423 }
424}
425
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000426std::string OptionDescription::GenOptionType() const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000427 switch (Type) {
428 case OptionType::Alias:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000429 return "Alias_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000430 case OptionType::PrefixList:
431 case OptionType::ParameterList:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000432 return "List_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000433 case OptionType::Switch:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000434 return "Switch_";
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000435 case OptionType::SwitchList:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000436 return "SwitchList_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000437 case OptionType::Prefix:
438 case OptionType::Parameter:
439 default:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000440 return "Parameter_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000441 }
442}
443
444/// OptionDescriptions - An OptionDescription array plus some helper
445/// functions.
446class OptionDescriptions {
447 typedef StringMap<OptionDescription> container_type;
448
449 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000450 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000451
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000452public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000453 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000454 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000455
456 // Wrappers for FindOption that throw an exception in case the option has a
457 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000458 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000459 const OptionDescription& FindParameter(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000460 const OptionDescription& FindParameterList(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000461 const OptionDescription&
462 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000463 const OptionDescription&
464 FindParameterListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000465
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000466 /// insertDescription - Insert new OptionDescription into
467 /// OptionDescriptions list
468 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000469
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000470 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000471 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000472 const_iterator begin() const { return Descriptions.begin(); }
473 const_iterator end() const { return Descriptions.end(); }
474};
475
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000476const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000477OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000478 const_iterator I = Descriptions.find(OptName);
479 if (I != Descriptions.end())
480 return I->second;
481 else
482 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000483}
484
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000485const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000486OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000487 const OptionDescription& OptDesc = this->FindOption(OptName);
488 if (!OptDesc.isSwitch())
489 throw OptName + ": incorrect option type - should be a switch!";
490 return OptDesc;
491}
492
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000493const OptionDescription&
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000494OptionDescriptions::FindParameterList(const std::string& OptName) const {
495 const OptionDescription& OptDesc = this->FindOption(OptName);
496 if (!OptDesc.isList() || OptDesc.isSwitchList())
497 throw OptName + ": incorrect option type - should be a parameter list!";
498 return OptDesc;
499}
500
501const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000502OptionDescriptions::FindParameter(const std::string& OptName) const {
503 const OptionDescription& OptDesc = this->FindOption(OptName);
504 if (!OptDesc.isParameter())
505 throw OptName + ": incorrect option type - should be a parameter!";
506 return OptDesc;
507}
508
509const OptionDescription&
510OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
511 const OptionDescription& OptDesc = this->FindOption(OptName);
512 if (!OptDesc.isList() && !OptDesc.isParameter())
513 throw OptName
514 + ": incorrect option type - should be a list or parameter!";
515 return OptDesc;
516}
517
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000518const OptionDescription&
519OptionDescriptions::FindParameterListOrParameter
520(const std::string& OptName) const {
521 const OptionDescription& OptDesc = this->FindOption(OptName);
522 if ((!OptDesc.isList() && !OptDesc.isParameter()) || OptDesc.isSwitchList())
523 throw OptName
524 + ": incorrect option type - should be a parameter list or parameter!";
525 return OptDesc;
526}
527
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000528void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000529 container_type::iterator I = Descriptions.find(o.Name);
530 if (I != Descriptions.end()) {
531 OptionDescription& D = I->second;
532 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000533 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000534 else {
535 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000536 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000537}
538
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000539/// HandlerTable - A base class for function objects implemented as
540/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000541template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000542class HandlerTable {
543protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000544 // Implementation details.
545
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000546 /// HandlerMap - A map from property names to property handlers
547 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000548
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000549 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000550 static bool staticMembersInitialized_;
551
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000552public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000553
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000554 Handler GetHandler (const std::string& HandlerName) const {
555 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000556
557 if (method != Handlers_.end()) {
558 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000559 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000560 }
561 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000562 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000563 }
564 }
565
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000566 void AddHandler(const char* Property, Handler H) {
567 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000568 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000569
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000570};
571
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000572template <class Handler, class FunctionObject>
573Handler GetHandler(FunctionObject* Obj, const DagInit& Dag) {
574 const std::string& HandlerName = GetOperatorName(Dag);
575 return Obj->GetHandler(HandlerName);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000576}
577
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000578template <class FunctionObject>
579void InvokeDagInitHandler(FunctionObject* Obj, Init* I) {
580 typedef void (FunctionObject::*Handler) (const DagInit&);
581
582 const DagInit& Dag = InitPtrToDag(I);
583 Handler h = GetHandler<Handler>(Obj, Dag);
584
585 ((Obj)->*(h))(Dag);
586}
587
588template <class FunctionObject>
589void InvokeDagInitHandler(const FunctionObject* const Obj,
590 const Init* I, unsigned IndentLevel, raw_ostream& O)
591{
592 typedef void (FunctionObject::*Handler)
593 (const DagInit&, unsigned IndentLevel, raw_ostream& O) const;
594
595 const DagInit& Dag = InitPtrToDag(I);
596 Handler h = GetHandler<Handler>(Obj, Dag);
597
598 ((Obj)->*(h))(Dag, IndentLevel, O);
599}
600
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000601template <typename H>
602typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
603
604template <typename H>
605bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000606
607
608/// CollectOptionProperties - Function object for iterating over an
609/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000610class CollectOptionProperties;
611typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000612(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000613
614class CollectOptionProperties
615: public HandlerTable<CollectOptionPropertiesHandler>
616{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000617private:
618
619 /// optDescs_ - OptionDescriptions table. This is where the
620 /// information is stored.
621 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000622
623public:
624
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000625 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000626 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000627 {
628 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000629 AddHandler("help", &CollectOptionProperties::onHelp);
630 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000631 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000632 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
633 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000634 AddHandler("zero_or_more", &CollectOptionProperties::onZeroOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000635 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
636 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000637 AddHandler("optional", &CollectOptionProperties::onOptional);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000638 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000639 AddHandler("forward_not_split",
640 &CollectOptionProperties::onForwardNotSplit);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000641
642 staticMembersInitialized_ = true;
643 }
644 }
645
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000646 /// operator() - Just forwards to the corresponding property
647 /// handler.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000648 void operator() (Init* I) {
649 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000650 }
651
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000652private:
653
654 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000655 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000656
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000657 void onHelp (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000658 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000659 optDesc_.Help = EscapeQuotes(InitPtrToString(d.getArg(0)));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000660 }
661
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000662 void onHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000663 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000664 optDesc_.setHidden();
665 }
666
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000667 void onReallyHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000668 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000669 optDesc_.setReallyHidden();
670 }
671
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000672 void onCommaSeparated (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000673 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000674 if (!optDesc_.isParameterList())
675 throw "'comma_separated' is valid only on parameter list options!";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000676 optDesc_.setCommaSeparated();
677 }
678
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000679 void onForwardNotSplit (const DagInit& d) {
680 CheckNumberOfArguments(d, 0);
681 if (!optDesc_.isParameter())
682 throw "'forward_not_split' is valid only for parameter options!";
683 optDesc_.setForwardNotSplit();
684 }
685
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000686 void onRequired (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000687 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000688
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000689 optDesc_.setRequired();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000690 optDesc_.CheckConsistency();
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000691 }
692
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000693 void onInit (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000694 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000695 Init* i = d.getArg(0);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000696 const std::string& str = i->getAsString();
697
698 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
699 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
700
701 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000702 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000703
704 optDesc_.InitVal = i;
705 }
706
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000707 void onOneOrMore (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000708 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000709
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000710 optDesc_.setOneOrMore();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000711 optDesc_.CheckConsistency();
712 }
713
714 void onZeroOrMore (const DagInit& d) {
715 CheckNumberOfArguments(d, 0);
716
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000717 if (optDesc_.isList())
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000718 llvm::errs() << "Warning: specifying the 'zero_or_more' property "
719 "on a list option has no effect.\n";
720
721 optDesc_.setZeroOrMore();
722 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000723 }
724
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000725 void onOptional (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000726 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000727
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000728 if (!optDesc_.isList())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000729 llvm::errs() << "Warning: specifying the 'optional' property"
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000730 "on a non-list option has no effect.\n";
731
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000732 optDesc_.setOptional();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000733 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000734 }
735
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000736 void onMultiVal (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000737 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000738 int val = InitPtrToInt(d.getArg(0));
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000739 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000740 throw "Error in the 'multi_val' property: "
741 "the value must be greater than 1!";
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000742 if (!optDesc_.isParameterList())
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000743 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000744 optDesc_.MultiVal = val;
745 }
746
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000747};
748
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000749/// AddOption - A function object that is applied to every option
750/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000751class AddOption {
752private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000753 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000754
755public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000756 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000757 {}
758
759 void operator()(const Init* i) {
760 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000761 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000762
763 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000764 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000765 const std::string& Name = InitPtrToString(d.getArg(0));
766
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000767 OptionDescription OD(Type, Name);
768
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000769 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000770
Mikhail Glushenkov87685e82010-12-15 01:21:59 +0000771 // Alias option store the aliased option name in the 'Help' field and do not
772 // have any properties.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000773 if (OD.isAlias()) {
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000774 OD.Help = InitPtrToString(d.getArg(1));
775 }
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000776 else {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000777 processOptionProperties(d, OD);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000778 }
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000779
Mikhail Glushenkov87685e82010-12-15 01:21:59 +0000780 // Switch options are ZeroOrMore by default.
781 if (OD.isSwitch()) {
782 if (!(OD.isOptional() || OD.isOneOrMore() || OD.isRequired()))
783 OD.setZeroOrMore();
784 }
785
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000786 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000787 }
788
789private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000790 /// processOptionProperties - Go through the list of option
791 /// properties and call a corresponding handler for each.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000792 static void processOptionProperties (const DagInit& d, OptionDescription& o) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000793 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000794 DagInit::const_arg_iterator B = d.arg_begin();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000795 // Skip the first argument: it's always the option name.
796 ++B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000797 std::for_each(B, d.arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000798 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000799
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000800};
801
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000802/// CollectOptionDescriptions - Collects option properties from all
803/// OptionLists.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000804void CollectOptionDescriptions (const RecordVector& V,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000805 OptionDescriptions& OptDescs)
806{
807 // For every OptionList:
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000808 for (RecordVector::const_iterator B = V.begin(), E = V.end(); B!=E; ++B)
809 {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000810 // Throws an exception if the value does not exist.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000811 ListInit* PropList = (*B)->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000812
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000813 // For every option description in this list: invoke AddOption.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000814 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
815 }
816}
817
818// Tool information record
819
820namespace ToolFlags {
821 enum ToolFlags { Join = 0x1, Sink = 0x2 };
822}
823
824struct ToolDescription : public RefCountedBase<ToolDescription> {
825 std::string Name;
826 Init* CmdLine;
827 Init* Actions;
828 StrVector InLanguage;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000829 std::string InFileOption;
830 std::string OutFileOption;
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000831 StrVector OutLanguage;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000832 std::string OutputSuffix;
833 unsigned Flags;
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000834 const Init* OnEmpty;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000835
836 // Various boolean properties
837 void setSink() { Flags |= ToolFlags::Sink; }
838 bool isSink() const { return Flags & ToolFlags::Sink; }
839 void setJoin() { Flags |= ToolFlags::Join; }
840 bool isJoin() const { return Flags & ToolFlags::Join; }
841
842 // Default ctor here is needed because StringMap can only store
843 // DefaultConstructible objects
Chris Lattner2d900582010-08-28 03:43:50 +0000844 ToolDescription (const std::string &n = "")
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000845 : Name(n), CmdLine(0), Actions(0), OutFileOption("-o"),
846 Flags(0), OnEmpty(0)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000847 {}
848};
849
850/// ToolDescriptions - A list of Tool information records.
851typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
852
853
854/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000855/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000856
857class CollectToolProperties;
858typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000859(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000860
861class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
862{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000863private:
864
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000865 /// toolDesc_ - Properties of the current Tool. This is where the
866 /// information is stored.
867 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000868
869public:
870
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000871 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000872 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000873 {
874 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000875
876 AddHandler("actions", &CollectToolProperties::onActions);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000877 AddHandler("command", &CollectToolProperties::onCommand);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000878 AddHandler("in_language", &CollectToolProperties::onInLanguage);
879 AddHandler("join", &CollectToolProperties::onJoin);
880 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000881
882 AddHandler("out_file_option", &CollectToolProperties::onOutFileOption);
883 AddHandler("in_file_option", &CollectToolProperties::onInFileOption);
884
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000885 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
886 AddHandler("sink", &CollectToolProperties::onSink);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000887 AddHandler("works_on_empty", &CollectToolProperties::onWorksOnEmpty);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000888
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000889 staticMembersInitialized_ = true;
890 }
891 }
892
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000893 void operator() (Init* I) {
894 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000895 }
896
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000897private:
898
899 /// Property handlers --
900 /// Functions that extract information about tool properties from
901 /// DAG representation.
902
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000903 void onActions (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000904 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000905 Init* Case = d.getArg(0);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000906 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000907 GetOperatorName(static_cast<DagInit&>(*Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000908 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000909 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000910 }
911
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000912 void onCommand (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000913 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000914 toolDesc_.CmdLine = d.getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000915 }
916
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000917 /// onInOutLanguage - Common implementation of on{In,Out}Language().
918 void onInOutLanguage (const DagInit& d, StrVector& OutVec) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000919 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000920
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000921 // Copy strings to the output vector.
922 for (unsigned i = 0, NumArgs = d.getNumArgs(); i < NumArgs; ++i) {
923 OutVec.push_back(InitPtrToString(d.getArg(i)));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000924 }
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000925
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000926 // Remove duplicates.
927 std::sort(OutVec.begin(), OutVec.end());
928 StrVector::iterator newE = std::unique(OutVec.begin(), OutVec.end());
929 OutVec.erase(newE, OutVec.end());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000930 }
931
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000932
933 void onInLanguage (const DagInit& d) {
934 this->onInOutLanguage(d, toolDesc_.InLanguage);
935 }
936
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000937 void onJoin (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000938 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000939 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000940 }
941
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000942 void onOutLanguage (const DagInit& d) {
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000943 this->onInOutLanguage(d, toolDesc_.OutLanguage);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000944 }
945
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000946 void onOutFileOption (const DagInit& d) {
947 CheckNumberOfArguments(d, 1);
948 toolDesc_.OutFileOption = InitPtrToString(d.getArg(0));
949 }
950
951 void onInFileOption (const DagInit& d) {
952 CheckNumberOfArguments(d, 1);
953 toolDesc_.InFileOption = InitPtrToString(d.getArg(0));
954 }
955
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000956 void onOutputSuffix (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000957 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000958 toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000959 }
960
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000961 void onSink (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000962 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000963 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000964 }
965
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000966 void onWorksOnEmpty (const DagInit& d) {
967 toolDesc_.OnEmpty = d.getArg(0);
968 }
969
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000970};
971
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000972/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000973/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000974/// CollectToolProperties function object).
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000975void CollectToolDescriptions (const RecordVector& Tools,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000976 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000977{
978 // Iterate over a properties list of every Tool definition
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000979 for (RecordVector::const_iterator B = Tools.begin(),
980 E = Tools.end(); B!=E; ++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000981 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000982 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000983 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000984
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000985 IntrusiveRefCntPtr<ToolDescription>
986 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000987
988 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000989 CollectToolProperties(*ToolDesc));
990 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000991 }
992}
993
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000994/// FillInEdgeVector - Merge all compilation graph definitions into
995/// one single edge list.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000996void FillInEdgeVector(const RecordVector& CompilationGraphs,
997 DagVector& Out) {
998 for (RecordVector::const_iterator B = CompilationGraphs.begin(),
999 E = CompilationGraphs.end(); B != E; ++B) {
1000 const ListInit* Edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +00001001
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001002 for (ListInit::const_iterator B = Edges->begin(),
1003 E = Edges->end(); B != E; ++B) {
1004 Out.push_back(&InitPtrToDag(*B));
1005 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001006 }
1007}
1008
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001009/// NotInGraph - Helper function object for FilterNotInGraph.
1010struct NotInGraph {
1011private:
1012 const llvm::StringSet<>& ToolsInGraph_;
1013
1014public:
1015 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
1016 : ToolsInGraph_(ToolsInGraph)
1017 {}
1018
1019 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
1020 return (ToolsInGraph_.count(x->Name) == 0);
1021 }
1022};
1023
1024/// FilterNotInGraph - Filter out from ToolDescs all Tools not
1025/// mentioned in the compilation graph definition.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001026void FilterNotInGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001027 ToolDescriptions& ToolDescs) {
1028
1029 // List all tools mentioned in the graph.
1030 llvm::StringSet<> ToolsInGraph;
1031
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001032 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001033 E = EdgeVector.end(); B != E; ++B) {
1034
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001035 const DagInit* Edge = *B;
1036 const std::string& NodeA = InitPtrToString(Edge->getArg(0));
1037 const std::string& NodeB = InitPtrToString(Edge->getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001038
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001039 if (NodeA != "root")
1040 ToolsInGraph.insert(NodeA);
1041 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001042 }
1043
1044 // Filter ToolPropertiesList.
1045 ToolDescriptions::iterator new_end =
1046 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
1047 NotInGraph(ToolsInGraph));
1048 ToolDescs.erase(new_end, ToolDescs.end());
1049}
1050
1051/// FillInToolToLang - Fills in two tables that map tool names to
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001052/// input & output language names. Helper function used by TypecheckGraph().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001053void FillInToolToLang (const ToolDescriptions& ToolDescs,
1054 StringMap<StringSet<> >& ToolToInLang,
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001055 StringMap<StringSet<> >& ToolToOutLang) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001056 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1057 E = ToolDescs.end(); B != E; ++B) {
1058 const ToolDescription& D = *(*B);
1059 for (StrVector::const_iterator B = D.InLanguage.begin(),
1060 E = D.InLanguage.end(); B != E; ++B)
1061 ToolToInLang[D.Name].insert(*B);
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001062 for (StrVector::const_iterator B = D.OutLanguage.begin(),
1063 E = D.OutLanguage.end(); B != E; ++B)
1064 ToolToOutLang[D.Name].insert(*B);
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001065 }
1066}
1067
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001068/// Intersect - Is set intersection non-empty?
1069bool Intersect (const StringSet<>& S1, const StringSet<>& S2) {
1070 for (StringSet<>::const_iterator B = S1.begin(), E = S1.end(); B != E; ++B) {
1071 if (S2.count(B->first()) != 0)
1072 return true;
1073 }
1074 return false;
1075}
1076
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001077/// TypecheckGraph - Check that names for output and input languages
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00001078/// on all edges do match.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001079void TypecheckGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001080 const ToolDescriptions& ToolDescs) {
1081 StringMap<StringSet<> > ToolToInLang;
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001082 StringMap<StringSet<> > ToolToOutLang;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001083
1084 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001085
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001086 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001087 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001088 const DagInit* Edge = *B;
1089 const std::string& NodeA = InitPtrToString(Edge->getArg(0));
1090 const std::string& NodeB = InitPtrToString(Edge->getArg(1));
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001091 StringMap<StringSet<> >::iterator IA = ToolToOutLang.find(NodeA);
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001092 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001093
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001094 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001095 throw "Edges back to the root are not allowed!";
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001096
1097 if (NodeA != "root") {
1098 if (IA == ToolToOutLang.end())
1099 throw NodeA + ": no output language defined!";
1100 if (IB == ToolToInLang.end())
1101 throw NodeB + ": no input language defined!";
1102
1103 if (!Intersect(IA->second, IB->second)) {
1104 throw "Edge " + NodeA + "->" + NodeB
1105 + ": output->input language mismatch";
1106 }
1107 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001108 }
1109}
1110
1111/// WalkCase - Walks the 'case' expression DAG and invokes
1112/// TestCallback on every test, and StatementCallback on every
1113/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001114/// combinators (that is, they are passed directly to TestCallback).
1115/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1116/// IndentLevel, bool FirstTest)'.
1117/// StatementCallback must have type 'void StatementCallback(const Init*,
1118/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001119template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001120void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1121 unsigned IndentLevel = 0)
1122{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001123 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001124
1125 // Error checks.
1126 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001127 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001128
1129 if (d.getNumArgs() < 2)
1130 throw "There should be at least one clause in the 'case' expression:\n"
1131 + d.getAsString();
1132
1133 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001134 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001135 const unsigned numArgs = d.getNumArgs();
1136 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001137 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1138 B != E; ++B) {
1139 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001140
1141 if (!even)
1142 {
1143 // Handle test.
1144 const DagInit& Test = InitPtrToDag(arg);
1145
1146 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001147 throw "The 'default' clause should be the last in the "
1148 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001149 if (i == numArgs)
1150 throw "Case construct handler: no corresponding action "
1151 "found for the test " + Test.getAsString() + '!';
1152
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001153 TestCallback(Test, IndentLevel, (i == 1));
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001154 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001155 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001156 {
1157 if (dynamic_cast<DagInit*>(arg)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001158 && GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001159 // Nested 'case'.
1160 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1161 }
1162
1163 // Handle statement.
1164 StatementCallback(arg, IndentLevel);
1165 }
1166
1167 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001168 even = !even;
1169 }
1170}
1171
1172/// ExtractOptionNames - A helper function object used by
1173/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1174class ExtractOptionNames {
1175 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001176
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001177 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001178 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001179 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001180 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001181 ActionName == "forward_value" ||
1182 ActionName == "forward_transformed_value" ||
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001183 ActionName == "parameter_equals" || ActionName == "element_in_list") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001184 CheckNumberOfArguments(Stmt, 1);
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001185
1186 Init* Arg = Stmt.getArg(0);
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001187 if (typeid(*Arg) == typeid(StringInit))
1188 OptionNames_.insert(InitPtrToString(Arg));
1189 }
1190 else if (ActionName == "any_switch_on" || ActionName == "switch_on" ||
1191 ActionName == "any_not_empty" || ActionName == "any_empty" ||
1192 ActionName == "not_empty" || ActionName == "empty") {
1193 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
1194 Init* Arg = Stmt.getArg(i);
1195 if (typeid(*Arg) == typeid(StringInit))
1196 OptionNames_.insert(InitPtrToString(Arg));
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001197 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001198 }
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001199 else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001200 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001201 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001202 }
1203 }
1204 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001205
1206public:
1207 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1208 {}
1209
1210 void operator()(const Init* Statement) {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001211 // Statement is either a dag, or a list of dags.
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001212 if (typeid(*Statement) == typeid(ListInit)) {
1213 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1214 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1215 B != E; ++B)
1216 this->processDag(*B);
1217 }
1218 else {
1219 this->processDag(Statement);
1220 }
1221 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001222
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001223 void operator()(const DagInit& Test, unsigned, bool) {
1224 this->operator()(&Test);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001225 }
1226 void operator()(const Init* Statement, unsigned) {
1227 this->operator()(Statement);
1228 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001229};
1230
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00001231/// IsOptionalEdge - Validate that the 'optional_edge' has proper structure.
1232bool IsOptionalEdge (const DagInit& Edg) {
1233 return (GetOperatorName(Edg) == "optional_edge") && (Edg.getNumArgs() > 2);
1234}
1235
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001236/// CheckForSuperfluousOptions - Check that there are no side
1237/// effect-free options (specified only in the OptionList). Otherwise,
1238/// output a warning.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001239void CheckForSuperfluousOptions (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001240 const ToolDescriptions& ToolDescs,
1241 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001242 llvm::StringSet<> nonSuperfluousOptions;
1243
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001244 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001245 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001246 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1247 E = ToolDescs.end(); B != E; ++B) {
1248 const ToolDescription& TD = *(*B);
1249 ExtractOptionNames Callback(nonSuperfluousOptions);
1250 if (TD.Actions)
1251 WalkCase(TD.Actions, Callback, Callback);
1252 }
1253
1254 // Add all options mentioned in the 'case' clauses of the
1255 // OptionalEdges of the compilation graph to the set of
1256 // non-superfluous options.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001257 for (DagVector::const_iterator B = EdgeVector.begin(),
1258 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00001259 const DagInit& Edge = **B;
1260 if (IsOptionalEdge(Edge)) {
1261 const DagInit& Weight = InitPtrToDag(Edge.getArg(2));
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001262 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001263 }
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001264 }
1265
1266 // Check that all options in OptDescs belong to the set of
1267 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001268 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001269 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001270 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001271 if (!nonSuperfluousOptions.count(Val.Name)
1272 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001273 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001274 "Probable cause: this option is specified only in the OptionList.\n";
1275 }
1276}
1277
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001278/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1279bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1280 if (TestName == "single_input_file") {
1281 O << "InputFilenames.size() == 1";
1282 return true;
1283 }
1284 else if (TestName == "multiple_input_files") {
1285 O << "InputFilenames.size() > 1";
1286 return true;
1287 }
1288
1289 return false;
1290}
1291
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001292/// EmitMultipleArgumentTest - Helper function used by
1293/// EmitCaseTestMultipleArgs()
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001294template <typename F>
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001295void EmitMultipleArgumentTest(const DagInit& D, const char* LogicOp,
1296 F Callback, raw_ostream& O)
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001297{
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001298 for (unsigned i = 0, NumArgs = D.getNumArgs(); i < NumArgs; ++i) {
1299 if (i != 0)
1300 O << ' ' << LogicOp << ' ';
1301 Callback(InitPtrToString(D.getArg(i)), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001302 }
1303}
1304
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001305// Callbacks for use with EmitMultipleArgumentTest
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001306
1307class EmitSwitchOn {
1308 const OptionDescriptions& OptDescs_;
1309public:
1310 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1311 {}
1312
1313 void operator()(const std::string& OptName, raw_ostream& O) const {
1314 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1315 O << OptDesc.GenVariableName();
1316 }
1317};
1318
1319class EmitEmptyTest {
1320 bool EmitNegate_;
1321 const OptionDescriptions& OptDescs_;
1322public:
1323 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1324 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1325 {}
1326
1327 void operator()(const std::string& OptName, raw_ostream& O) const {
1328 const char* Neg = (EmitNegate_ ? "!" : "");
1329 if (OptName == "o") {
1330 O << Neg << "OutputFilename.empty()";
1331 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001332 else if (OptName == "save-temps") {
1333 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1334 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001335 else {
1336 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1337 O << Neg << OptDesc.GenVariableName() << ".empty()";
1338 }
1339 }
1340};
1341
1342
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001343/// EmitCaseTestMultipleArgs - Helper function used by EmitCaseTest1Arg()
1344bool EmitCaseTestMultipleArgs (const std::string& TestName,
1345 const DagInit& d,
1346 const OptionDescriptions& OptDescs,
1347 raw_ostream& O) {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001348 if (TestName == "any_switch_on") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001349 EmitMultipleArgumentTest(d, "||", EmitSwitchOn(OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001350 return true;
1351 }
1352 else if (TestName == "switch_on") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001353 EmitMultipleArgumentTest(d, "&&", EmitSwitchOn(OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001354 return true;
1355 }
1356 else if (TestName == "any_not_empty") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001357 EmitMultipleArgumentTest(d, "||", EmitEmptyTest(true, OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001358 return true;
1359 }
1360 else if (TestName == "any_empty") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001361 EmitMultipleArgumentTest(d, "||", EmitEmptyTest(false, OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001362 return true;
1363 }
1364 else if (TestName == "not_empty") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001365 EmitMultipleArgumentTest(d, "&&", EmitEmptyTest(true, OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001366 return true;
1367 }
1368 else if (TestName == "empty") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001369 EmitMultipleArgumentTest(d, "&&", EmitEmptyTest(false, OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001370 return true;
1371 }
1372
1373 return false;
1374}
1375
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001376/// EmitCaseTest1Arg - Helper function used by EmitCaseTest1OrMoreArgs()
1377bool EmitCaseTest1Arg (const std::string& TestName,
1378 const DagInit& d,
1379 const OptionDescriptions& OptDescs,
1380 raw_ostream& O) {
1381 const std::string& Arg = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001382
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001383 if (TestName == "input_languages_contain") {
1384 O << "InLangs.count(\"" << Arg << "\") != 0";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001385 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001386 }
1387 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001388 // This works only for single-argument Tool::GenerateAction. Join
1389 // tools can process several files in different languages simultaneously.
1390
1391 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001392 O << "LangMap.GetLanguage(inFile) == \"" << Arg << '\"';
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001393 return true;
1394 }
1395
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001396 return false;
1397}
1398
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001399/// EmitCaseTest1OrMoreArgs - Helper function used by
1400/// EmitCaseConstructHandler()
1401bool EmitCaseTest1OrMoreArgs(const std::string& TestName,
1402 const DagInit& d,
1403 const OptionDescriptions& OptDescs,
1404 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001405 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001406 return EmitCaseTest1Arg(TestName, d, OptDescs, O) ||
1407 EmitCaseTestMultipleArgs(TestName, d, OptDescs, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001408}
1409
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001410/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001411bool EmitCaseTest2Args(const std::string& TestName,
1412 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001413 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001414 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001415 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001416 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001417 const std::string& OptName = InitPtrToString(d.getArg(0));
1418 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001419
1420 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001421 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001422 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1423 return true;
1424 }
1425 else if (TestName == "element_in_list") {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00001426 const OptionDescription& OptDesc = OptDescs.FindParameterList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001427 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001428 O << "std::find(" << VarName << ".begin(),\n";
1429 O.indent(IndentLevel + Indent1)
1430 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001431 << OptArg << "\") != " << VarName << ".end()";
1432 return true;
1433 }
1434
1435 return false;
1436}
1437
1438// Forward declaration.
1439// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001440void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001441 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001442 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001443
1444/// EmitLogicalOperationTest - Helper function used by
1445/// EmitCaseConstructHandler.
1446void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001447 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001448 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001449 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001450 O << '(';
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001451 for (unsigned i = 0, NumArgs = d.getNumArgs(); i < NumArgs; ++i) {
1452 const DagInit& InnerTest = InitPtrToDag(d.getArg(i));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001453 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001454 if (i != NumArgs - 1) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001455 O << ")\n";
1456 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1457 }
1458 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001459 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001460 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001461 }
1462}
1463
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001464void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001465 const OptionDescriptions& OptDescs, raw_ostream& O)
1466{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001467 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001468 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1469 O << "! (";
1470 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1471 O << ")";
1472}
1473
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001474/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001475void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001476 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001477 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001478 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001479
1480 if (TestName == "and")
1481 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1482 else if (TestName == "or")
1483 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001484 else if (TestName == "not")
1485 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001486 else if (EmitCaseTest0Args(TestName, O))
1487 return;
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001488 else if (EmitCaseTest1OrMoreArgs(TestName, d, OptDescs, O))
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001489 return;
1490 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1491 return;
1492 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001493 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001494}
1495
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001496
1497/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1498class EmitCaseTestCallback {
1499 bool EmitElseIf_;
1500 const OptionDescriptions& OptDescs_;
1501 raw_ostream& O_;
1502public:
1503
1504 EmitCaseTestCallback(bool EmitElseIf,
1505 const OptionDescriptions& OptDescs, raw_ostream& O)
1506 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1507 {}
1508
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001509 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001510 {
1511 if (GetOperatorName(Test) == "default") {
1512 O_.indent(IndentLevel) << "else {\n";
1513 }
1514 else {
1515 O_.indent(IndentLevel)
1516 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001517 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001518 O_ << ") {\n";
1519 }
1520 }
1521};
1522
1523/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1524template <typename F>
1525class EmitCaseStatementCallback {
1526 F Callback_;
1527 raw_ostream& O_;
1528public:
1529
1530 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1531 : Callback_(Callback), O_(O)
1532 {}
1533
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001534 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001535 // Is this a nested 'case'?
1536 bool IsCase = dynamic_cast<const DagInit*>(Statement) &&
1537 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case";
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001538
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001539 // If so, ignore it, it is handled by our caller, WalkCase.
1540 if (!IsCase) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001541 if (typeid(*Statement) == typeid(ListInit)) {
1542 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1543 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1544 B != E; ++B)
1545 Callback_(*B, (IndentLevel + Indent1), O_);
1546 }
1547 else {
1548 Callback_(Statement, (IndentLevel + Indent1), O_);
1549 }
1550 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001551 O_.indent(IndentLevel) << "}\n";
1552 }
1553
1554};
1555
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001556/// EmitCaseConstructHandler - Emit code that handles the 'case'
1557/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001558/// clause. Implemented on top of WalkCase.
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00001559/// Callback's type is void F(const Init* Statement, unsigned IndentLevel,
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001560/// raw_ostream& O).
1561/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001562/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1563/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001564template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001565void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001566 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001567 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001568 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001569 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1570 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001571}
1572
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001573/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001574/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1575/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001576void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001577 const char* Delimiters = " \t\n\v\f\r";
1578 enum TokenizerState
1579 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1580 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001581
1582 if (CmdLine.empty())
1583 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001584 Out.push_back("");
1585
1586 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1587 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001588
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001589 for (; B != E; ++B) {
1590 char cur_ch = CmdLine[B];
1591
1592 switch (cur_st) {
1593 case Normal:
1594 if (cur_ch == '$') {
1595 cur_st = SpecialCommand;
1596 break;
1597 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001598 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001599 // Skip whitespace
1600 B = CmdLine.find_first_not_of(Delimiters, B);
1601 if (B == std::string::npos) {
1602 B = E-1;
1603 continue;
1604 }
1605 --B;
1606 Out.push_back("");
1607 continue;
1608 }
1609 break;
1610
1611
1612 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001613 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001614 cur_st = Normal;
1615 Out.push_back("");
1616 continue;
1617 }
1618 if (cur_ch == '(') {
1619 Out.push_back("");
1620 cur_st = InsideSpecialCommand;
1621 continue;
1622 }
1623 break;
1624
1625 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001626 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001627 continue;
1628 }
1629 if (cur_ch == '\'') {
1630 cur_st = InsideQuotationMarks;
1631 Out.push_back("");
1632 continue;
1633 }
1634 if (cur_ch == ')') {
1635 cur_st = Normal;
1636 Out.push_back("");
1637 }
1638 if (cur_ch == ',') {
1639 continue;
1640 }
1641
1642 break;
1643
1644 case InsideQuotationMarks:
1645 if (cur_ch == '\'') {
1646 cur_st = InsideSpecialCommand;
1647 continue;
1648 }
1649 break;
1650 }
1651
1652 Out.back().push_back(cur_ch);
1653 }
1654}
1655
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001656/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1657/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1658/// SubstituteSpecialCommands().
1659StrVector::const_iterator
1660SubstituteCall (StrVector::const_iterator Pos,
1661 StrVector::const_iterator End,
1662 bool IsJoin, raw_ostream& O)
1663{
1664 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001665 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001666 const std::string& CmdName = *Pos;
1667
1668 if (CmdName == ")")
1669 throw "$CALL invocation: empty argument list!";
1670
1671 O << "hooks::";
1672 O << CmdName << "(";
1673
1674
1675 bool firstIteration = true;
1676 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001677 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001678 const std::string& Arg = *Pos;
1679 assert(Arg.size() != 0);
1680
1681 if (Arg[0] == ')')
1682 break;
1683
1684 if (firstIteration)
1685 firstIteration = false;
1686 else
1687 O << ", ";
1688
1689 if (Arg == "$INFILE") {
1690 if (IsJoin)
1691 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1692 else
1693 O << "inFile.c_str()";
1694 }
1695 else {
1696 O << '"' << Arg << '"';
1697 }
1698 }
1699
1700 O << ')';
1701
1702 return Pos;
1703}
1704
1705/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1706/// function used by SubstituteSpecialCommands().
1707StrVector::const_iterator
1708SubstituteEnv (StrVector::const_iterator Pos,
1709 StrVector::const_iterator End, raw_ostream& O)
1710{
1711 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001712 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001713 const std::string& EnvName = *Pos;
1714
1715 if (EnvName == ")")
1716 throw "$ENV invocation: empty argument list!";
1717
1718 O << "checkCString(std::getenv(\"";
1719 O << EnvName;
1720 O << "\"))";
1721
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001722 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001723
1724 return Pos;
1725}
1726
1727/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1728/// handler code. Helper function used by EmitCmdLineVecFill().
1729StrVector::const_iterator
1730SubstituteSpecialCommands (StrVector::const_iterator Pos,
1731 StrVector::const_iterator End,
1732 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001733{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001734
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001735 const std::string& cmd = *Pos;
1736
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001737 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001738 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001739 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001740 }
1741 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001742 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001743 }
1744 else {
1745 throw "Unknown special command: " + cmd;
1746 }
1747
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001748 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001749 const std::string& Leftover = *Pos;
1750 assert(Leftover.at(0) == ')');
1751 if (Leftover.size() != 1)
1752 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001753
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001754 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001755}
1756
1757/// EmitCmdLineVecFill - Emit code that fills in the command line
1758/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001759void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001760 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001761 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001762 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001763 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001764
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001765 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001766 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001767
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001768 StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001769
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001770 // Emit the command itself.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001771 assert(!StrVec[0].empty());
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001772 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001773 if (StrVec[0][0] == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001774 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1775 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001776 }
1777 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001778 O << '"' << StrVec[0] << '"';
1779 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001780 }
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001781 O << ";\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001782
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001783 // Go through the command arguments.
1784 assert(B <= E);
1785 for (; B != E; ++B) {
1786 const std::string& cmd = *B;
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001787
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001788 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001789 O.indent(IndentLevel);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001790
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001791 if (cmd.at(0) == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001792 O << "vec.push_back(std::make_pair(0, ";
1793 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1794 O << "));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001795 }
1796 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001797 O << "vec.push_back(std::make_pair(0, \"" << cmd << "\"));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001798 }
1799 }
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001800
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001801}
1802
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001803/// EmitForEachListElementCycleHeader - Emit common code for iterating through
1804/// all elements of a list. Helper function used by
1805/// EmitForwardOptionPropertyHandlingCode.
1806void EmitForEachListElementCycleHeader (const OptionDescription& D,
1807 unsigned IndentLevel,
1808 raw_ostream& O) {
1809 unsigned IndentLevel1 = IndentLevel + Indent1;
1810
1811 O.indent(IndentLevel)
1812 << "for (" << D.GenTypeDeclaration()
1813 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1814 O.indent(IndentLevel)
1815 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1816 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1817 << ".getPosition(B - " << D.GenVariableName()
1818 << ".begin());\n";
1819}
1820
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001821/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1822/// implement EmitActionHandler. Emits code for
1823/// handling the (forward) and (forward_as) option properties.
1824void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001825 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001826 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001827 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001828 const std::string& Name = NewName.empty()
1829 ? ("-" + D.Name)
1830 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001831 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001832
1833 switch (D.Type) {
1834 case OptionType::Switch:
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001835 O.indent(IndentLevel)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001836 << "vec.push_back(std::make_pair(" << D.GenVariableName()
1837 << ".getPosition(), \"" << Name << "\"));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001838 break;
1839 case OptionType::Parameter:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001840 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1841 << D.GenVariableName()
1842 <<".getPosition(), \"" << Name;
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001843
1844 if (!D.isForwardNotSplit()) {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001845 O << "\"));\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001846 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1847 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001848 << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001849 }
1850 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001851 O << "=\" + " << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001852 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001853 break;
1854 case OptionType::Prefix:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001855 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1856 << D.GenVariableName() << ".getPosition(), \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001857 << Name << "\" + "
1858 << D.GenVariableName() << "));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001859 break;
1860 case OptionType::PrefixList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001861 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001862 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001863 << Name << "\" + " << "*B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001864 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001865
1866 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001867 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001868 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001869 }
1870
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001871 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001872 break;
1873 case OptionType::ParameterList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001874 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001875 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001876 << Name << "\"));\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001877
1878 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001879 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001880 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001881 }
1882
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001883 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001884 break;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001885 case OptionType::SwitchList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001886 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001887 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
1888 << Name << "\"));\n";
1889 O.indent(IndentLevel1) << "++B;\n";
1890 O.indent(IndentLevel) << "}\n";
1891 break;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001892 case OptionType::Alias:
1893 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001894 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001895 }
1896}
1897
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001898/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1899/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001900struct ActionHandlingCallbackBase
1901{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001902
1903 void onErrorDag(const DagInit& d,
1904 unsigned IndentLevel, raw_ostream& O) const
1905 {
1906 O.indent(IndentLevel)
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00001907 << "PrintError(\""
1908 << (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0)) : "Unknown error!")
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001909 << "\");\n";
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +00001910 O.indent(IndentLevel) << "return 1;\n";
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001911 }
1912
1913 void onWarningDag(const DagInit& d,
1914 unsigned IndentLevel, raw_ostream& O) const
1915 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001916 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001917 O.indent(IndentLevel) << "llvm::errs() << \""
1918 << InitPtrToString(d.getArg(0)) << "\";\n";
1919 }
1920
1921};
1922
1923/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1924/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001925class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001926
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001927typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1928(const DagInit&, unsigned, raw_ostream&) const;
1929
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001930class EmitActionHandlersCallback :
1931 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001932 public HandlerTable<EmitActionHandlersCallbackHandler>
1933{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001934 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001935
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001936 const OptionDescriptions& OptDescs;
1937
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001938 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1939 /// onAppendCmd and onOutputSuffix.
1940 void EmitHookInvocation(const std::string& Str,
1941 const char* BlockOpen, const char* BlockClose,
1942 unsigned IndentLevel, raw_ostream& O) const
1943 {
1944 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001945 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001946
1947 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1948 B != E; ++B) {
1949 const std::string& cmd = *B;
1950
1951 O.indent(IndentLevel) << BlockOpen;
1952
1953 if (cmd.at(0) == '$')
1954 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1955 else
1956 O << '"' << cmd << '"';
1957
1958 O << BlockClose;
1959 }
1960 }
1961
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001962 void onAppendCmd (const DagInit& Dag,
1963 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001964 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001965 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001966 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001967 "vec.push_back(std::make_pair(65536, ", "));\n",
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001968 IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001969 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001970
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001971 void onForward (const DagInit& Dag,
1972 unsigned IndentLevel, raw_ostream& O) const
1973 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001974 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001975 const std::string& Name = InitPtrToString(Dag.getArg(0));
1976 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1977 IndentLevel, "", O);
1978 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001979
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001980 void onForwardAs (const DagInit& Dag,
1981 unsigned IndentLevel, raw_ostream& O) const
1982 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001983 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001984 const std::string& Name = InitPtrToString(Dag.getArg(0));
1985 const std::string& NewName = InitPtrToString(Dag.getArg(1));
1986 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1987 IndentLevel, NewName, O);
1988 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001989
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001990 void onForwardValue (const DagInit& Dag,
1991 unsigned IndentLevel, raw_ostream& O) const
1992 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001993 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001994 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00001995 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001996
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001997 if (D.isSwitchList()) {
1998 throw std::runtime_error
1999 ("forward_value is not allowed with switch_list");
2000 }
2001
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002002 if (D.isParameter()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002003 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2004 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002005 << D.GenVariableName() << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002006 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00002007 else {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002008 O.indent(IndentLevel) << "for (" << D.GenTypeDeclaration()
2009 << "::iterator B = " << D.GenVariableName()
2010 << ".begin(), \n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002011 O.indent(IndentLevel + Indent1) << " E = " << D.GenVariableName()
2012 << ".end(); B != E; ++B)\n";
2013 O.indent(IndentLevel) << "{\n";
2014 O.indent(IndentLevel + Indent1)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002015 << "unsigned pos = " << D.GenVariableName()
2016 << ".getPosition(B - " << D.GenVariableName()
2017 << ".begin());\n";
2018 O.indent(IndentLevel + Indent1)
2019 << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002020 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002021 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002022 }
2023
2024 void onForwardTransformedValue (const DagInit& Dag,
2025 unsigned IndentLevel, raw_ostream& O) const
2026 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002027 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002028 const std::string& Name = InitPtrToString(Dag.getArg(0));
2029 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002030 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002031
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002032 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2033 << D.GenVariableName() << ".getPosition("
2034 << (D.isList() ? "0" : "") << "), "
2035 << "hooks::" << Hook << "(" << D.GenVariableName()
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002036 << (D.isParameter() ? ".c_str()" : "") << ")));\n";
2037 }
2038
2039 void onNoOutFile (const DagInit& Dag,
2040 unsigned IndentLevel, raw_ostream& O) const
2041 {
2042 CheckNumberOfArguments(Dag, 0);
2043 O.indent(IndentLevel) << "no_out_file = true;\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002044 }
2045
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002046 void onOutputSuffix (const DagInit& Dag,
2047 unsigned IndentLevel, raw_ostream& O) const
2048 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002049 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002050 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
2051 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002052 }
2053
2054 void onStopCompilation (const DagInit& Dag,
2055 unsigned IndentLevel, raw_ostream& O) const
2056 {
2057 O.indent(IndentLevel) << "stop_compilation = true;\n";
2058 }
2059
2060
2061 void onUnpackValues (const DagInit& Dag,
2062 unsigned IndentLevel, raw_ostream& O) const
2063 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002064 throw "'unpack_values' is deprecated. "
2065 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002066 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002067
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002068 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002069
2070 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
2071 : OptDescs(OD)
2072 {
2073 if (!staticMembersInitialized_) {
2074 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
2075 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
2076 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
2077 AddHandler("forward", &EmitActionHandlersCallback::onForward);
2078 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002079 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
2080 AddHandler("forward_transformed_value",
2081 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002082 AddHandler("no_out_file",
2083 &EmitActionHandlersCallback::onNoOutFile);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002084 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
2085 AddHandler("stop_compilation",
2086 &EmitActionHandlersCallback::onStopCompilation);
2087 AddHandler("unpack_values",
2088 &EmitActionHandlersCallback::onUnpackValues);
2089
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002090
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002091 staticMembersInitialized_ = true;
2092 }
2093 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002094
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002095 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002096 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002097 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002098 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002099 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002100};
2101
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002102void EmitGenerateActionMethodHeader(const ToolDescription& D,
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002103 bool IsJoin, bool Naked,
2104 raw_ostream& O)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002105{
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002106 O.indent(Indent1) << "int GenerateAction(Action& Out,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002107
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002108 if (IsJoin)
2109 O.indent(Indent2) << "const PathVector& inFiles,\n";
2110 else
2111 O.indent(Indent2) << "const sys::Path& inFile,\n";
2112
2113 O.indent(Indent2) << "const bool HasChildren,\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002114 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2115 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2116 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2117 O.indent(Indent1) << "{\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002118
2119 if (!Naked) {
2120 O.indent(Indent2) << "std::string cmd;\n";
2121 O.indent(Indent2) << "std::string out_file;\n";
2122 O.indent(Indent2)
2123 << "std::vector<std::pair<unsigned, std::string> > vec;\n";
2124 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2125 O.indent(Indent2) << "bool no_out_file = false;\n";
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002126 O.indent(Indent2) << "std::string output_suffix(\""
2127 << D.OutputSuffix << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002128 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002129}
2130
2131// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2132// Tool::GenerateAction() method.
2133void EmitGenerateActionMethod (const ToolDescription& D,
2134 const OptionDescriptions& OptDescs,
2135 bool IsJoin, raw_ostream& O) {
2136
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002137 EmitGenerateActionMethodHeader(D, IsJoin, /* Naked = */ false, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002138
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002139 if (!D.CmdLine)
2140 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002141
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002142 // Process the 'command' property.
2143 O << '\n';
2144 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2145 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002146
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002147 // Process the 'actions' list of this tool.
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002148 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002149 EmitCaseConstructHandler(D.Actions, Indent2,
2150 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002151 false, OptDescs, O);
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002152 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002153
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002154 // Input file (s)
2155 if (!D.InFileOption.empty()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002156 O.indent(Indent2)
2157 << "vec.push_back(std::make_pair(InputFilenames.getPosition(0), \""
2158 << D.InFileOption << "\");\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002159 }
2160
2161 if (IsJoin) {
2162 O.indent(Indent2)
2163 << "for (PathVector::const_iterator B = inFiles.begin(),\n";
2164 O.indent(Indent3) << "E = inFiles.end(); B != E; ++B)\n";
2165 O.indent(Indent2) << "{\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002166 O.indent(Indent3) << "vec.push_back(std::make_pair("
2167 << "InputFilenames.getPosition(B - inFiles.begin()), "
2168 << "B->str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002169 O.indent(Indent2) << "}\n";
2170 }
2171 else {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002172 O.indent(Indent2) << "vec.push_back(std::make_pair("
2173 << "InputFilenames.getPosition(0), inFile.str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002174 }
2175
2176 // Output file
2177 O.indent(Indent2) << "if (!no_out_file) {\n";
2178 if (!D.OutFileOption.empty())
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002179 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002180 << D.OutFileOption << "\"));\n";
2181
2182 O.indent(Indent3) << "out_file = this->OutFilename("
2183 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002184 O.indent(Indent4) <<
2185 "TempDir, stop_compilation, output_suffix.c_str()).str();\n\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002186 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, out_file));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002187
2188 O.indent(Indent2) << "}\n\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002189
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002190 // Handle the Sink property.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002191 std::string SinkOption("autogenerated::");
2192 SinkOption += SinkOptionName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002193 if (D.isSink()) {
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002194 O.indent(Indent2) << "if (!" << SinkOption << ".empty()) {\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002195 O.indent(Indent3) << "for (cl::list<std::string>::iterator B = "
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002196 << SinkOption << ".begin(), E = " << SinkOption
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002197 << ".end(); B != E; ++B)\n";
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002198 O.indent(Indent4) << "vec.push_back(std::make_pair(" << SinkOption
2199 << ".getPosition(B - " << SinkOption
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002200 << ".begin()), *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002201 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002202 }
2203
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002204 O.indent(Indent2) << "Out.Construct(cmd, this->SortArgs(vec), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002205 << "stop_compilation, out_file);\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002206 O.indent(Indent2) << "return 0;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002207 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002208}
2209
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002210/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2211/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002212void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2213 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002214 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002215 if (!ToolDesc.isJoin()) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002216 EmitGenerateActionMethodHeader(ToolDesc, /* IsJoin = */ true,
2217 /* Naked = */ true, O);
2218 O.indent(Indent2) << "PrintError(\"" << ToolDesc.Name
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002219 << " is not a Join tool!\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002220 O.indent(Indent2) << "return -1;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002221 O.indent(Indent1) << "}\n\n";
2222 }
2223 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002224 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002225 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002226
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002227 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002228}
2229
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002230/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2231/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002232void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002233 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2234 O.indent(Indent2) << "return InputLanguages_;\n";
2235 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002236
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002237 O.indent(Indent1) << "const char** OutputLanguages() const {\n";
2238 O.indent(Indent2) << "return OutputLanguages_;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002239 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002240}
2241
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002242/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002243void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002244 O.indent(Indent1) << "const char* Name() const {\n";
2245 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2246 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002247}
2248
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002249/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2250/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002251void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002252 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002253 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002254 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002255 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002256 O.indent(Indent2) << "return false;\n";
2257 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002258}
2259
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002260/// EmitWorksOnEmptyCallback - Callback used by EmitWorksOnEmptyMethod in
2261/// conjunction with EmitCaseConstructHandler.
2262void EmitWorksOnEmptyCallback (const Init* Value,
2263 unsigned IndentLevel, raw_ostream& O) {
2264 CheckBooleanConstant(Value);
2265 O.indent(IndentLevel) << "return " << Value->getAsString() << ";\n";
2266}
2267
2268/// EmitWorksOnEmptyMethod - Emit the WorksOnEmpty() method for a given Tool
2269/// class.
2270void EmitWorksOnEmptyMethod (const ToolDescription& D,
2271 const OptionDescriptions& OptDescs,
2272 raw_ostream& O)
2273{
2274 O.indent(Indent1) << "bool WorksOnEmpty() const {\n";
2275 if (D.OnEmpty == 0)
2276 O.indent(Indent2) << "return false;\n";
2277 else
2278 EmitCaseConstructHandler(D.OnEmpty, Indent2, EmitWorksOnEmptyCallback,
2279 /*EmitElseIf = */ true, OptDescs, O);
2280 O.indent(Indent1) << "}\n\n";
2281}
2282
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002283/// EmitStrArray - Emit definition of a 'const char**' static member
2284/// variable. Helper used by EmitStaticMemberDefinitions();
2285void EmitStrArray(const std::string& Name, const std::string& VarName,
2286 const StrVector& StrVec, raw_ostream& O) {
2287 O << "const char* " << Name << "::" << VarName << "[] = {";
2288 for (StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
2289 B != E; ++B)
2290 O << '\"' << *B << "\", ";
2291 O << "0};\n";
2292}
2293
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002294/// EmitStaticMemberDefinitions - Emit static member definitions for a
2295/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002296void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002297 if (D.InLanguage.empty())
2298 throw "Tool " + D.Name + " has no 'in_language' property!";
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002299 if (D.OutLanguage.empty())
2300 throw "Tool " + D.Name + " has no 'out_language' property!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002301
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002302 EmitStrArray(D.Name, "InputLanguages_", D.InLanguage, O);
2303 EmitStrArray(D.Name, "OutputLanguages_", D.OutLanguage, O);
2304 O << '\n';
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002305}
2306
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002307/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002308void EmitToolClassDefinition (const ToolDescription& D,
2309 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002310 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002311 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002312 return;
2313
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002314 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002315 O << "class " << D.Name << " : public ";
2316 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002317 O << "JoinTool";
2318 else
2319 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002320
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002321 O << " {\nprivate:\n";
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002322 O.indent(Indent1) << "static const char* InputLanguages_[];\n";
2323 O.indent(Indent1) << "static const char* OutputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002324
2325 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002326 EmitNameMethod(D, O);
2327 EmitInOutLanguageMethods(D, O);
2328 EmitIsJoinMethod(D, O);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002329 EmitWorksOnEmptyMethod(D, OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002330 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002331
2332 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002333 O << "};\n";
2334
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002335 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002336
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002337}
2338
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002339/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002340/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002341void EmitOptionDefinitions (const OptionDescriptions& descs,
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002342 bool HasSink, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002343{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002344 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002345
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002346 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002347 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002348 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002349 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002350
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002351 if (val.Type == OptionType::Alias) {
2352 Aliases.push_back(val);
2353 continue;
2354 }
2355
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002356 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002357 << val.GenPlainVariableName();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002358
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002359 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002360
2361 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2362 O << ", cl::Prefix";
2363
2364 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002365 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002366 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002367 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002368 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002369 }
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002370
2371 if (val.isOptional())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002372 O << ", cl::Optional";
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002373
2374 if (val.isOneOrMore())
2375 O << ", cl::OneOrMore";
2376
2377 if (val.isZeroOrMore())
2378 O << ", cl::ZeroOrMore";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002379
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002380 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002381 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002382 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002383 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002384
2385 if (val.isCommaSeparated())
2386 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002387
2388 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002389 O << ", cl::multi_val(" << val.MultiVal << ')';
2390
2391 if (val.InitVal) {
2392 const std::string& str = val.InitVal->getAsString();
2393 O << ", cl::init(" << str << ')';
2394 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002395
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002396 if (!val.Help.empty())
2397 O << ", cl::desc(\"" << val.Help << "\")";
2398
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002399 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002400 }
2401
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002402 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002403 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002404 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002405 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002406
2407 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov297514d2010-08-20 18:16:26 +00002408 << val.GenPlainVariableName()
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002409 << "(\"" << val.Name << '\"';
2410
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002411 const OptionDescription& D = descs.FindOption(val.Help);
2412 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002413
2414 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2415 }
2416
2417 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002418 if (HasSink)
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002419 O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002420
2421 O << '\n';
2422}
2423
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002424/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002425/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002426
2427class EmitPreprocessOptionsCallback;
2428
2429typedef void
2430(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2431(const DagInit&, unsigned, raw_ostream&) const;
2432
2433class EmitPreprocessOptionsCallback :
2434 public ActionHandlingCallbackBase,
2435 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2436{
2437 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002438 typedef void
2439 (EmitPreprocessOptionsCallback::* HandlerImpl)
2440 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002441
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002442 const OptionDescriptions& OptDescs_;
2443
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002444 void onEachArgument(const DagInit& d, HandlerImpl h,
2445 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002446 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002447 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002448
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002449 for (unsigned i = 0, NumArgs = d.getNumArgs(); i < NumArgs; ++i) {
2450 ((this)->*(h))(d.getArg(i), IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002451 }
2452 }
2453
2454 void onUnsetOptionImpl(const Init* I,
2455 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002456 {
2457 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002458 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002459
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002460 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002461 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2462 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002463 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002464 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2465 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002466 else if (OptDesc.isList()) {
2467 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2468 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002469 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002470 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002471 }
2472 }
2473
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002474 void onUnsetOption(const DagInit& d,
2475 unsigned IndentLevel, raw_ostream& O) const
2476 {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002477 this->onEachArgument(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2478 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002479 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002480
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002481 void onSetOptionImpl(const DagInit& D,
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002482 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002483 CheckNumberOfArguments(D, 2);
2484
2485 const std::string& OptName = InitPtrToString(D.getArg(0));
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002486 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002487 const Init* Value = D.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002488
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002489 if (OptDesc.isList()) {
2490 const ListInit& List = InitPtrToList(Value);
2491
2492 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2493 for (ListInit::const_iterator B = List.begin(), E = List.end();
2494 B != E; ++B) {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002495 const Init* CurElem = *B;
2496 if (OptDesc.isSwitchList())
2497 CheckBooleanConstant(CurElem);
2498
2499 O.indent(IndentLevel)
2500 << OptDesc.GenVariableName() << ".push_back(\""
2501 << (OptDesc.isSwitchList() ? CurElem->getAsString()
2502 : InitPtrToString(CurElem))
2503 << "\");\n";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002504 }
2505 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002506 else if (OptDesc.isSwitch()) {
2507 CheckBooleanConstant(Value);
2508 O.indent(IndentLevel) << OptDesc.GenVariableName()
2509 << " = " << Value->getAsString() << ";\n";
2510 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002511 else if (OptDesc.isParameter()) {
2512 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002513 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002514 << " = \"" << Str << "\";\n";
2515 }
2516 else {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002517 throw "Can't apply 'set_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002518 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002519 }
2520
2521 void onSetSwitch(const Init* I,
2522 unsigned IndentLevel, raw_ostream& O) const {
2523 const std::string& OptName = InitPtrToString(I);
2524 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2525
2526 if (OptDesc.isSwitch())
2527 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2528 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002529 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002530 }
2531
2532 void onSetOption(const DagInit& d,
2533 unsigned IndentLevel, raw_ostream& O) const
2534 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002535 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002536
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002537 // 2-argument form: (set_option "A", true), (set_option "B", "C"),
2538 // (set_option "D", ["E", "F"])
2539 if (d.getNumArgs() == 2) {
2540 const OptionDescription& OptDesc =
2541 OptDescs_.FindOption(InitPtrToString(d.getArg(0)));
2542 const Init* Opt2 = d.getArg(1);
2543
2544 if (!OptDesc.isSwitch() || typeid(*Opt2) != typeid(StringInit)) {
2545 this->onSetOptionImpl(d, IndentLevel, O);
2546 return;
2547 }
2548 }
2549
2550 // Multiple argument form: (set_option "A"), (set_option "B", "C", "D")
2551 this->onEachArgument(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2552 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002553 }
2554
2555public:
2556
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002557 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002558 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002559 {
2560 if (!staticMembersInitialized_) {
2561 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2562 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2563 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002564 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002565
2566 staticMembersInitialized_ = true;
2567 }
2568 }
2569
2570 void operator()(const Init* I,
2571 unsigned IndentLevel, raw_ostream& O) const
2572 {
2573 InvokeDagInitHandler(this, I, IndentLevel, O);
2574 }
2575
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002576};
2577
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002578/// EmitPreprocessOptions - Emit the PreprocessOptions() function.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002579void EmitPreprocessOptions (const RecordKeeper& Records,
2580 const OptionDescriptions& OptDecs, raw_ostream& O)
2581{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002582 O << "int PreprocessOptions () {\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002583
2584 const RecordVector& OptionPreprocessors =
2585 Records.getAllDerivedDefinitions("OptionPreprocessor");
2586
2587 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2588 E = OptionPreprocessors.end(); B!=E; ++B) {
2589 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002590 EmitCaseConstructHandler(Case, Indent1,
2591 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002592 false, OptDecs, O);
2593 }
2594
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002595 O << '\n';
2596 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002597 O << "}\n\n";
2598}
2599
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002600class DoEmitPopulateLanguageMap;
2601typedef void (DoEmitPopulateLanguageMap::* DoEmitPopulateLanguageMapHandler)
2602(const DagInit& D);
2603
2604class DoEmitPopulateLanguageMap
2605: public HandlerTable<DoEmitPopulateLanguageMapHandler>
2606{
2607private:
2608 raw_ostream& O_;
2609
2610public:
2611
2612 explicit DoEmitPopulateLanguageMap (raw_ostream& O) : O_(O) {
2613 if (!staticMembersInitialized_) {
2614 AddHandler("lang_to_suffixes",
2615 &DoEmitPopulateLanguageMap::onLangToSuffixes);
2616
2617 staticMembersInitialized_ = true;
2618 }
2619 }
2620
2621 void operator() (Init* I) {
2622 InvokeDagInitHandler(this, I);
2623 }
2624
2625private:
2626
2627 void onLangToSuffixes (const DagInit& d) {
2628 CheckNumberOfArguments(d, 2);
2629
2630 const std::string& Lang = InitPtrToString(d.getArg(0));
2631 Init* Suffixes = d.getArg(1);
2632
2633 // Second argument to lang_to_suffixes is either a single string...
2634 if (typeid(*Suffixes) == typeid(StringInit)) {
2635 O_.indent(Indent1) << "langMap[\"" << InitPtrToString(Suffixes)
2636 << "\"] = \"" << Lang << "\";\n";
2637 }
2638 // ...or a list of strings.
2639 else {
2640 const ListInit& Lst = InitPtrToList(Suffixes);
2641 assert(Lst.size() != 0);
2642 for (ListInit::const_iterator B = Lst.begin(), E = Lst.end();
2643 B != E; ++B) {
2644 O_.indent(Indent1) << "langMap[\"" << InitPtrToString(*B)
2645 << "\"] = \"" << Lang << "\";\n";
2646 }
2647 }
2648 }
2649
2650};
2651
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002652/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002653void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002654{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002655 O << "int PopulateLanguageMap (LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002656
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002657 // For each LanguageMap:
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002658 const RecordVector& LangMaps =
Mikhail Glushenkov00a5b5b2010-08-23 19:24:16 +00002659 Records.getAllDerivedDefinitions("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002660
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002661 // Call DoEmitPopulateLanguageMap.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002662 for (RecordVector::const_iterator B = LangMaps.begin(),
2663 E = LangMaps.end(); B!=E; ++B) {
2664 ListInit* LangMap = (*B)->getValueAsListInit("map");
2665 std::for_each(LangMap->begin(), LangMap->end(),
2666 DoEmitPopulateLanguageMap(O));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002667 }
2668
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002669 O << '\n';
2670 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002671 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002672}
2673
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002674/// EmitEdgePropertyHandlerCallback - Emits code that handles edge
2675/// properties. Helper function passed to EmitCaseConstructHandler() by
2676/// EmitEdgeClass().
2677void EmitEdgePropertyHandlerCallback (const Init* i, unsigned IndentLevel,
2678 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002679 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002680 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002681
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002682 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002683 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002684 }
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002685 else if (OpName == "error") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002686 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002687 O.indent(IndentLevel) << "PrintError(\""
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002688 << InitPtrToString(d.getArg(0))
2689 << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002690 O.indent(IndentLevel) << "return -1;\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002691 return;
2692 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002693 else {
2694 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002695 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002696 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002697
2698 if (d.getNumArgs() > 0)
2699 O << InitPtrToInt(d.getArg(0)) << ";\n";
2700 else
2701 O << "2;\n";
2702
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002703}
2704
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002705/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002706void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002707 const DagInit& Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002708 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002709
2710 // Class constructor.
2711 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002712 << "public:\n";
2713 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2714 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002715
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002716 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002717 O.indent(Indent1)
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002718 << "int Weight(const InputLanguagesSet& InLangs) const {\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002719 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002720
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002721 // Handle the 'case' construct.
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002722 EmitCaseConstructHandler(&Case, Indent2, EmitEdgePropertyHandlerCallback,
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002723 false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002724
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002725 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002726 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002727}
2728
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002729/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002730void EmitEdgeClasses (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002731 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002732 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002733 int i = 0;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002734 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002735 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002736 const DagInit& Edge = **B;
2737 const std::string& Name = GetOperatorName(Edge);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002738
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002739 if (Name == "optional_edge") {
2740 assert(IsOptionalEdge(Edge));
2741 const std::string& NodeB = InitPtrToString(Edge.getArg(1));
2742
2743 const DagInit& Weight = InitPtrToDag(Edge.getArg(2));
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002744 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
2745 }
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002746 else if (Name != "edge") {
2747 throw "Unknown edge class: '" + Name + "'!";
2748 }
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002749
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002750 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002751 }
2752}
2753
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002754/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph() function.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002755void EmitPopulateCompilationGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002756 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002757 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002758{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002759 O << "int PopulateCompilationGraph (CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002760
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002761 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2762 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002763 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002764
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002765 O << '\n';
2766
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002767 // Insert edges.
2768
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002769 int i = 0;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002770 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002771 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002772 const DagInit& Edge = **B;
2773 const std::string& NodeA = InitPtrToString(Edge.getArg(0));
2774 const std::string& NodeB = InitPtrToString(Edge.getArg(1));
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002775
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002776 O.indent(Indent1) << "if (int ret = G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002777
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002778 if (IsOptionalEdge(Edge))
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002779 O << "new Edge" << i << "()";
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002780 else
2781 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002782
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002783 O << "))\n";
2784 O.indent(Indent2) << "return ret;\n";
2785
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002786 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002787 }
2788
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002789 O << '\n';
2790 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002791 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002792}
2793
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002794/// HookInfo - Information about the hook type and number of arguments.
2795struct HookInfo {
2796
2797 // A hook can either have a single parameter of type std::vector<std::string>,
2798 // or NumArgs parameters of type const char*.
2799 enum HookType { ListHook, ArgHook };
2800
2801 HookType Type;
2802 unsigned NumArgs;
2803
2804 HookInfo() : Type(ArgHook), NumArgs(1)
2805 {}
2806
2807 HookInfo(HookType T) : Type(T), NumArgs(1)
2808 {}
2809
2810 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2811 {}
2812};
2813
2814typedef llvm::StringMap<HookInfo> HookInfoMap;
2815
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002816/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002817/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002818/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002819class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002820 HookInfoMap& HookNames_;
2821 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002822public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002823 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2824 : HookNames_(HookNames), OptDescs_(OptDescs)
2825 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002826
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002827 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002828 const std::string& Name = GetOperatorName(Dag);
2829
2830 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002831 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002832 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2833 const std::string& HookName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002834 const OptionDescription& D =
2835 OptDescs_.FindParameterListOrParameter(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002836
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002837 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2838 : HookInfo::ArgHook);
2839 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002840 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002841 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002842 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2843 }
2844 }
2845
2846 void onCmdLine(const std::string& Cmd) {
2847 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002848 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002849
2850 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2851 B != E; ++B) {
2852 const std::string& cmd = *B;
2853
2854 if (cmd == "$CALL") {
2855 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002856 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002857 const std::string& HookName = *B;
2858
2859 if (HookName.at(0) == ')')
2860 throw "$CALL invoked with no arguments!";
2861
2862 while (++B != E && B->at(0) != ')') {
2863 ++NumArgs;
2864 }
2865
2866 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2867
2868 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2869 H->second.Type != HookInfo::ArgHook)
2870 throw "Overloading of hooks is not allowed. Overloaded hook: "
2871 + HookName;
2872 else
2873 HookNames_[HookName] = HookInfo(NumArgs);
2874 }
2875 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002876 }
2877
2878 void operator()(const Init* Arg) {
2879
2880 // We're invoked on an action (either a dag or a dag list).
2881 if (typeid(*Arg) == typeid(DagInit)) {
2882 const DagInit& Dag = InitPtrToDag(Arg);
2883 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002884 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002885 }
2886 else if (typeid(*Arg) == typeid(ListInit)) {
2887 const ListInit& List = InitPtrToList(Arg);
2888 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2889 ++B) {
2890 const DagInit& Dag = InitPtrToDag(*B);
2891 this->onAction(Dag);
2892 }
2893 return;
2894 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002895
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002896 // We're invoked on a command line string.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002897 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002898 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002899
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002900 void operator()(const Init* Statement, unsigned) {
2901 this->operator()(Statement);
2902 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002903};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002904
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002905/// FillInHookNames - Actually extract the hook names from all command
2906/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002907void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002908 const OptionDescriptions& OptDescs,
2909 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002910{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002911 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002912 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2913 E = ToolDescs.end(); B != E; ++B) {
2914 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002915
2916 // Look for 'forward_transformed_value' in 'actions'.
2917 if (D.Actions)
2918 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2919
2920 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002921 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002922 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002923 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002924 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002925 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002926 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002927 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002928 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002929 }
2930}
2931
2932/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2933/// property records and emit hook function declaration for each
2934/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002935void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2936 const OptionDescriptions& OptDescs, raw_ostream& O) {
2937 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002938
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002939 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002940 if (HookNames.empty())
2941 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002942
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002943 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002944 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002945 const char* HookName = B->first();
2946 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002947
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002948 O.indent(Indent1) << "std::string " << HookName << "(";
2949
2950 if (Info.Type == HookInfo::ArgHook) {
2951 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2952 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2953 }
2954 }
2955 else {
2956 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002957 }
2958
2959 O <<");\n";
2960 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002961}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002962
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002963/// EmitIncludes - Emit necessary #include directives and some
2964/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002965void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002966 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2967 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002968 << "#include \"llvm/CompilerDriver/Error.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002969 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002970
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002971 << "#include \"llvm/Support/CommandLine.h\"\n"
2972 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002973
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002974 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002975 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002976 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002977 << "#include <stdexcept>\n\n"
2978
2979 << "using namespace llvm;\n"
2980 << "using namespace llvmc;\n\n"
2981
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002982 << "inline const char* checkCString(const char* s)\n"
2983 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002984}
2985
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002986
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00002987/// DriverData - Holds all information about the driver.
2988struct DriverData {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002989 OptionDescriptions OptDescs;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002990 ToolDescriptions ToolDescs;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002991 DagVector Edges;
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00002992 bool HasSink;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002993};
2994
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002995/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002996/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002997bool HasSink(const ToolDescriptions& ToolDescs) {
2998 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2999 E = ToolDescs.end(); B != E; ++B)
3000 if ((*B)->isSink())
3001 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003002
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003003 return false;
3004}
3005
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003006/// CollectDriverData - Collect compilation graph edges, tool properties and
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003007/// option properties from the parse tree.
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003008void CollectDriverData (const RecordKeeper& Records, DriverData& Data) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003009 // Collect option properties.
3010 const RecordVector& OptionLists =
3011 Records.getAllDerivedDefinitions("OptionList");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003012 CollectOptionDescriptions(OptionLists, Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003013
3014 // Collect tool properties.
3015 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003016 CollectToolDescriptions(Tools, Data.ToolDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003017 Data.HasSink = HasSink(Data.ToolDescs);
3018
3019 // Collect compilation graph edges.
3020 const RecordVector& CompilationGraphs =
3021 Records.getAllDerivedDefinitions("CompilationGraph");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003022 FillInEdgeVector(CompilationGraphs, Data.Edges);
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00003023}
3024
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003025/// CheckDriverData - Perform some sanity checks on the collected data.
3026void CheckDriverData(DriverData& Data) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003027 // Filter out all tools not mentioned in the compilation graph.
3028 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003029
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003030 // Typecheck the compilation graph.
Mikhail Glushenkov4e699cf2011-04-24 14:17:41 +00003031 // TODO: use a genuine graph representation instead of a vector and check for
3032 // multiple edges.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003033 TypecheckGraph(Data.Edges, Data.ToolDescs);
3034
3035 // Check that there are no options without side effects (specified
3036 // only in the OptionList).
3037 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003038}
3039
Chris Lattner67db8832010-12-13 00:23:57 +00003040void EmitDriverCode(const DriverData& Data,
3041 raw_ostream& O, RecordKeeper &Records) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003042 // Emit file header.
3043 EmitIncludes(O);
3044
3045 // Emit global option registration code.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003046 O << "namespace llvmc {\n"
3047 << "namespace autogenerated {\n\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003048 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003049 O << "} // End namespace autogenerated.\n"
3050 << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003051
3052 // Emit hook declarations.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003053 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003054 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003055 O << "} // End namespace hooks.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003056
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003057 O << "namespace {\n\n";
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003058 O << "using namespace llvmc::autogenerated;\n\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003059
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003060 // Emit Tool classes.
3061 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
3062 E = Data.ToolDescs.end(); B!=E; ++B)
3063 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
3064
3065 // Emit Edge# classes.
3066 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
3067
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003068 O << "} // End anonymous namespace.\n\n";
3069
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003070 O << "namespace llvmc {\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003071 O << "namespace autogenerated {\n\n";
3072
3073 // Emit PreprocessOptions() function.
3074 EmitPreprocessOptions(Records, Data.OptDescs, O);
3075
3076 // Emit PopulateLanguageMap() function
3077 // (language map maps from file extensions to language names).
3078 EmitPopulateLanguageMap(Records, O);
3079
3080 // Emit PopulateCompilationGraph() function.
3081 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
3082
3083 O << "} // End namespace autogenerated.\n";
3084 O << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003085
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003086 // EOF
3087}
3088
3089
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003090// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00003091}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003092
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003093/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00003094void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003095 try {
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003096 DriverData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003097
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003098 CollectDriverData(Records, Data);
3099 CheckDriverData(Data);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003100
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003101 this->EmitSourceFileHeader("llvmc-based driver: auto-generated code", O);
Chris Lattner67db8832010-12-13 00:23:57 +00003102 EmitDriverCode(Data, O, Records);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003103
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003104 } catch (std::exception& Error) {
3105 throw Error.what() + std::string(" - usually this means a syntax error.");
3106 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003107}