blob: 91803309cb082b64c71214b89a62d4b9e2134932 [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 Glushenkov3a21c552011-05-05 04:24:58 +000077bool InitPtrToBool(const Init* ptr) {
78 bool ret = false;
79 const DefInit& val = dynamic_cast<const DefInit&>(*ptr);
80 const std::string& str = val.getAsString();
81
82 if (str == "true") {
83 ret = true;
84 }
85 else if (str == "false") {
86 ret = false;
87 }
88 else {
89 throw "Incorrect boolean value: '" + str +
90 "': must be either 'true' or 'false'";
91 }
92
93 return ret;
94}
95
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000096const std::string& InitPtrToString(const Init* ptr) {
97 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
98 return val.getValue();
99}
100
101const ListInit& InitPtrToList(const Init* ptr) {
102 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
103 return val;
104}
105
106const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +0000107 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000108 return val;
109}
110
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000111const std::string GetOperatorName(const DagInit& D) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000112 return D.getOperator()->getAsString();
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000113}
114
Mikhail Glushenkove0b65702009-12-23 12:49:30 +0000115/// CheckBooleanConstant - Check that the provided value is a boolean constant.
116void CheckBooleanConstant(const Init* I) {
Mikhail Glushenkov3a21c552011-05-05 04:24:58 +0000117 InitPtrToBool(I);
Mikhail Glushenkove0b65702009-12-23 12:49:30 +0000118}
119
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000120// CheckNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +0000121// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000122void CheckNumberOfArguments (const DagInit& d, unsigned minArgs) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000123 if (d.getNumArgs() < minArgs)
124 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +0000125}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000126
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000127// EscapeVariableName - Escape commas and other symbols not allowed
128// in the C++ variable names. Makes it possible to use options named
129// like "Wa," (useful for prefix options).
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000130std::string EscapeVariableName (const std::string& Var) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000131 std::string ret;
132 for (unsigned i = 0; i != Var.size(); ++i) {
133 char cur_char = Var[i];
134 if (cur_char == ',') {
135 ret += "_comma_";
136 }
137 else if (cur_char == '+') {
138 ret += "_plus_";
139 }
140 else if (cur_char == '-') {
141 ret += "_dash_";
142 }
143 else {
144 ret.push_back(cur_char);
145 }
146 }
147 return ret;
148}
149
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000150/// EscapeQuotes - Replace '"' with '\"'.
151std::string EscapeQuotes (const std::string& Var) {
152 std::string ret;
153 for (unsigned i = 0; i != Var.size(); ++i) {
154 char cur_char = Var[i];
155 if (cur_char == '"') {
156 ret += "\\\"";
157 }
158 else {
159 ret.push_back(cur_char);
160 }
161 }
162 return ret;
163}
164
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000165/// OneOf - Does the input string contain this character?
166bool OneOf(const char* lst, char c) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000167 while (*lst) {
168 if (*lst++ == c)
169 return true;
170 }
171 return false;
172}
173
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000174template <class I, class S>
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000175void CheckedIncrement(I& P, I E, S ErrorString) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000176 ++P;
177 if (P == E)
178 throw ErrorString;
179}
180
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000181//===----------------------------------------------------------------------===//
182/// Back-end specific code
183
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000184
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000185/// OptionType - One of six different option types. See the
186/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000187namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000188
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000189 enum OptionType { Alias, Switch, SwitchList,
190 Parameter, ParameterList, Prefix, PrefixList };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000191
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000192 bool IsAlias(OptionType t) {
193 return (t == Alias);
194 }
195
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000196 bool IsList (OptionType t) {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000197 return (t == SwitchList || t == ParameterList || t == PrefixList);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000198 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000199
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000200 bool IsSwitch (OptionType t) {
201 return (t == Switch);
202 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000203
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000204 bool IsSwitchList (OptionType t) {
205 return (t == SwitchList);
206 }
207
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000208 bool IsParameter (OptionType t) {
209 return (t == Parameter || t == Prefix);
210 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000211
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000212}
213
214OptionType::OptionType stringToOptionType(const std::string& T) {
215 if (T == "alias_option")
216 return OptionType::Alias;
217 else if (T == "switch_option")
218 return OptionType::Switch;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000219 else if (T == "switch_list_option")
220 return OptionType::SwitchList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000221 else if (T == "parameter_option")
222 return OptionType::Parameter;
223 else if (T == "parameter_list_option")
224 return OptionType::ParameterList;
225 else if (T == "prefix_option")
226 return OptionType::Prefix;
227 else if (T == "prefix_list_option")
228 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000229 else
230 throw "Unknown option type: " + T + '!';
231}
232
233namespace OptionDescriptionFlags {
234 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000235 ReallyHidden = 0x4, OneOrMore = 0x8,
236 Optional = 0x10, CommaSeparated = 0x20,
237 ForwardNotSplit = 0x40, ZeroOrMore = 0x80 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000238}
239
240/// OptionDescription - Represents data contained in a single
241/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000242struct OptionDescription {
243 OptionType::OptionType Type;
244 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000245 unsigned Flags;
246 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000247 unsigned MultiVal;
David Greened4a90662011-07-11 18:25:51 +0000248 const Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000249
250 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000251 const std::string& n = "",
252 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000253 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000254 {}
255
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000256 /// GenTypeDeclaration - Returns the C++ variable type of this
257 /// option.
258 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000259
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000260 /// GenVariableName - Returns the variable name used in the
261 /// generated C++ code.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000262 std::string GenVariableName() const
263 { return "autogenerated::" + GenOptionType() + EscapeVariableName(Name); }
264
265 /// GenPlainVariableName - Returns the variable name without the namespace
266 /// prefix.
267 std::string GenPlainVariableName() const
268 { return GenOptionType() + EscapeVariableName(Name); }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000269
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000270 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000271 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000272
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000273 /// CheckConsistency - Check that the flags are consistent.
274 void CheckConsistency() const;
275
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000276 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000277
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000278 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000279
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000280 bool isMultiVal() const;
281
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000282 bool isCommaSeparated() const;
283 void setCommaSeparated();
284
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000285 bool isForwardNotSplit() const;
286 void setForwardNotSplit();
287
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000288 bool isRequired() const;
289 void setRequired();
290
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000291 bool isOneOrMore() const;
292 void setOneOrMore();
293
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000294 bool isZeroOrMore() const;
295 void setZeroOrMore();
296
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000297 bool isOptional() const;
298 void setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000299
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000300 bool isHidden() const;
301 void setHidden();
302
303 bool isReallyHidden() const;
304 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000305
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000306 bool isSwitch() const
307 { return OptionType::IsSwitch(this->Type); }
308
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000309 bool isSwitchList() const
310 { return OptionType::IsSwitchList(this->Type); }
311
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000312 bool isParameter() const
313 { return OptionType::IsParameter(this->Type); }
314
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000315 bool isList() const
316 { return OptionType::IsList(this->Type); }
317
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000318 bool isParameterList() const
319 { return (OptionType::IsList(this->Type)
320 && !OptionType::IsSwitchList(this->Type)); }
321
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000322private:
323
324 // GenOptionType - Helper function used by GenVariableName().
325 std::string GenOptionType() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000326};
327
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000328void OptionDescription::CheckConsistency() const {
329 unsigned i = 0;
330
331 i += this->isRequired();
332 i += this->isOptional();
333 i += this->isOneOrMore();
334 i += this->isZeroOrMore();
335
336 if (i > 1) {
337 throw "Only one of (required), (optional), (one_or_more) or "
338 "(zero_or_more) properties is allowed!";
339 }
340}
341
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000342void OptionDescription::Merge (const OptionDescription& other)
343{
344 if (other.Type != Type)
345 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000346
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000347 if (Help == other.Help || Help == DefaultHelpString)
348 Help = other.Help;
349 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000350 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000351 " defined for option " + Name + "\n";
352 }
353
354 Flags |= other.Flags;
355}
356
357bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000358 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000359}
360
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000361bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000362 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000363}
364
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000365bool OptionDescription::isCommaSeparated() const {
366 return Flags & OptionDescriptionFlags::CommaSeparated;
367}
368void OptionDescription::setCommaSeparated() {
369 Flags |= OptionDescriptionFlags::CommaSeparated;
370}
371
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000372bool OptionDescription::isForwardNotSplit() const {
373 return Flags & OptionDescriptionFlags::ForwardNotSplit;
374}
375void OptionDescription::setForwardNotSplit() {
376 Flags |= OptionDescriptionFlags::ForwardNotSplit;
377}
378
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000379bool OptionDescription::isRequired() const {
380 return Flags & OptionDescriptionFlags::Required;
381}
382void OptionDescription::setRequired() {
383 Flags |= OptionDescriptionFlags::Required;
384}
385
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000386bool OptionDescription::isOneOrMore() const {
387 return Flags & OptionDescriptionFlags::OneOrMore;
388}
389void OptionDescription::setOneOrMore() {
390 Flags |= OptionDescriptionFlags::OneOrMore;
391}
392
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000393bool OptionDescription::isZeroOrMore() const {
394 return Flags & OptionDescriptionFlags::ZeroOrMore;
395}
396void OptionDescription::setZeroOrMore() {
397 Flags |= OptionDescriptionFlags::ZeroOrMore;
398}
399
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000400bool OptionDescription::isOptional() const {
401 return Flags & OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000402}
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000403void OptionDescription::setOptional() {
404 Flags |= OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000405}
406
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000407bool OptionDescription::isHidden() const {
408 return Flags & OptionDescriptionFlags::Hidden;
409}
410void OptionDescription::setHidden() {
411 Flags |= OptionDescriptionFlags::Hidden;
412}
413
414bool OptionDescription::isReallyHidden() const {
415 return Flags & OptionDescriptionFlags::ReallyHidden;
416}
417void OptionDescription::setReallyHidden() {
418 Flags |= OptionDescriptionFlags::ReallyHidden;
419}
420
421const char* OptionDescription::GenTypeDeclaration() const {
422 switch (Type) {
423 case OptionType::Alias:
424 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000425 case OptionType::PrefixList:
426 case OptionType::ParameterList:
427 return "cl::list<std::string>";
428 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000429 return "cl::opt<bool>";
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000430 case OptionType::SwitchList:
431 return "cl::list<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000432 case OptionType::Parameter:
433 case OptionType::Prefix:
434 default:
435 return "cl::opt<std::string>";
436 }
437}
438
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000439std::string OptionDescription::GenOptionType() const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000440 switch (Type) {
441 case OptionType::Alias:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000442 return "Alias_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000443 case OptionType::PrefixList:
444 case OptionType::ParameterList:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000445 return "List_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000446 case OptionType::Switch:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000447 return "Switch_";
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000448 case OptionType::SwitchList:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000449 return "SwitchList_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000450 case OptionType::Prefix:
451 case OptionType::Parameter:
452 default:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000453 return "Parameter_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000454 }
455}
456
457/// OptionDescriptions - An OptionDescription array plus some helper
458/// functions.
459class OptionDescriptions {
460 typedef StringMap<OptionDescription> container_type;
461
462 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000463 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000464
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000465public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000466 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000467 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000468
469 // Wrappers for FindOption that throw an exception in case the option has a
470 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000471 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000472 const OptionDescription& FindParameter(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000473 const OptionDescription& FindParameterList(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000474 const OptionDescription&
475 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000476 const OptionDescription&
477 FindParameterListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000478
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000479 /// insertDescription - Insert new OptionDescription into
480 /// OptionDescriptions list
481 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000482
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000483 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000484 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000485 const_iterator begin() const { return Descriptions.begin(); }
486 const_iterator end() const { return Descriptions.end(); }
487};
488
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000489const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000490OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000491 const_iterator I = Descriptions.find(OptName);
492 if (I != Descriptions.end())
493 return I->second;
494 else
495 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000496}
497
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000498const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000499OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000500 const OptionDescription& OptDesc = this->FindOption(OptName);
501 if (!OptDesc.isSwitch())
502 throw OptName + ": incorrect option type - should be a switch!";
503 return OptDesc;
504}
505
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000506const OptionDescription&
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000507OptionDescriptions::FindParameterList(const std::string& OptName) const {
508 const OptionDescription& OptDesc = this->FindOption(OptName);
509 if (!OptDesc.isList() || OptDesc.isSwitchList())
510 throw OptName + ": incorrect option type - should be a parameter list!";
511 return OptDesc;
512}
513
514const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000515OptionDescriptions::FindParameter(const std::string& OptName) const {
516 const OptionDescription& OptDesc = this->FindOption(OptName);
517 if (!OptDesc.isParameter())
518 throw OptName + ": incorrect option type - should be a parameter!";
519 return OptDesc;
520}
521
522const OptionDescription&
523OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
524 const OptionDescription& OptDesc = this->FindOption(OptName);
525 if (!OptDesc.isList() && !OptDesc.isParameter())
526 throw OptName
527 + ": incorrect option type - should be a list or parameter!";
528 return OptDesc;
529}
530
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000531const OptionDescription&
532OptionDescriptions::FindParameterListOrParameter
533(const std::string& OptName) const {
534 const OptionDescription& OptDesc = this->FindOption(OptName);
535 if ((!OptDesc.isList() && !OptDesc.isParameter()) || OptDesc.isSwitchList())
536 throw OptName
537 + ": incorrect option type - should be a parameter list or parameter!";
538 return OptDesc;
539}
540
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000541void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000542 container_type::iterator I = Descriptions.find(o.Name);
543 if (I != Descriptions.end()) {
544 OptionDescription& D = I->second;
545 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000546 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000547 else {
548 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000549 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000550}
551
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000552/// HandlerTable - A base class for function objects implemented as
553/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000554template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000555class HandlerTable {
556protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000557 // Implementation details.
558
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000559 /// HandlerMap - A map from property names to property handlers
560 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000561
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000562 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000563 static bool staticMembersInitialized_;
564
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000565public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000566
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000567 Handler GetHandler (const std::string& HandlerName) const {
568 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000569
570 if (method != Handlers_.end()) {
571 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000572 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000573 }
574 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000575 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000576 }
577 }
578
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000579 void AddHandler(const char* Property, Handler H) {
580 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000581 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000582
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000583};
584
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000585template <class Handler, class FunctionObject>
586Handler GetHandler(FunctionObject* Obj, const DagInit& Dag) {
587 const std::string& HandlerName = GetOperatorName(Dag);
588 return Obj->GetHandler(HandlerName);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000589}
590
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000591template <class FunctionObject>
David Greened4a90662011-07-11 18:25:51 +0000592void InvokeDagInitHandler(FunctionObject* Obj, const Init* I) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000593 typedef void (FunctionObject::*Handler) (const DagInit&);
594
595 const DagInit& Dag = InitPtrToDag(I);
596 Handler h = GetHandler<Handler>(Obj, Dag);
597
598 ((Obj)->*(h))(Dag);
599}
600
601template <class FunctionObject>
602void InvokeDagInitHandler(const FunctionObject* const Obj,
603 const Init* I, unsigned IndentLevel, raw_ostream& O)
604{
605 typedef void (FunctionObject::*Handler)
606 (const DagInit&, unsigned IndentLevel, raw_ostream& O) const;
607
608 const DagInit& Dag = InitPtrToDag(I);
609 Handler h = GetHandler<Handler>(Obj, Dag);
610
611 ((Obj)->*(h))(Dag, IndentLevel, O);
612}
613
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000614template <typename H>
615typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
616
617template <typename H>
618bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000619
620
621/// CollectOptionProperties - Function object for iterating over an
622/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000623class CollectOptionProperties;
624typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000625(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000626
627class CollectOptionProperties
628: public HandlerTable<CollectOptionPropertiesHandler>
629{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000630private:
631
632 /// optDescs_ - OptionDescriptions table. This is where the
633 /// information is stored.
634 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000635
636public:
637
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000638 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000639 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000640 {
641 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000642 AddHandler("help", &CollectOptionProperties::onHelp);
643 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000644 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000645 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
646 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000647 AddHandler("zero_or_more", &CollectOptionProperties::onZeroOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000648 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
649 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000650 AddHandler("optional", &CollectOptionProperties::onOptional);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000651 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000652 AddHandler("forward_not_split",
653 &CollectOptionProperties::onForwardNotSplit);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000654
655 staticMembersInitialized_ = true;
656 }
657 }
658
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000659 /// operator() - Just forwards to the corresponding property
660 /// handler.
David Greened4a90662011-07-11 18:25:51 +0000661 void operator() (const Init* I) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000662 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000663 }
664
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000665private:
666
667 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000668 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000669
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000670 void onHelp (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000671 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000672 optDesc_.Help = EscapeQuotes(InitPtrToString(d.getArg(0)));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000673 }
674
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000675 void onHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000676 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000677 optDesc_.setHidden();
678 }
679
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000680 void onReallyHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000681 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000682 optDesc_.setReallyHidden();
683 }
684
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000685 void onCommaSeparated (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000686 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000687 if (!optDesc_.isParameterList())
688 throw "'comma_separated' is valid only on parameter list options!";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000689 optDesc_.setCommaSeparated();
690 }
691
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000692 void onForwardNotSplit (const DagInit& d) {
693 CheckNumberOfArguments(d, 0);
694 if (!optDesc_.isParameter())
695 throw "'forward_not_split' is valid only for parameter options!";
696 optDesc_.setForwardNotSplit();
697 }
698
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000699 void onRequired (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000700 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000701
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000702 optDesc_.setRequired();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000703 optDesc_.CheckConsistency();
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000704 }
705
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000706 void onInit (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000707 CheckNumberOfArguments(d, 1);
David Greened4a90662011-07-11 18:25:51 +0000708 const Init* i = d.getArg(0);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000709 const std::string& str = i->getAsString();
710
David Greened4a90662011-07-11 18:25:51 +0000711 bool correct = optDesc_.isParameter() && dynamic_cast<const StringInit*>(i);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000712 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
713
714 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000715 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000716
717 optDesc_.InitVal = i;
718 }
719
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000720 void onOneOrMore (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000721 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000722
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000723 optDesc_.setOneOrMore();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000724 optDesc_.CheckConsistency();
725 }
726
727 void onZeroOrMore (const DagInit& d) {
728 CheckNumberOfArguments(d, 0);
729
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000730 if (optDesc_.isList())
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000731 llvm::errs() << "Warning: specifying the 'zero_or_more' property "
732 "on a list option has no effect.\n";
733
734 optDesc_.setZeroOrMore();
735 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000736 }
737
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000738 void onOptional (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000739 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000740
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000741 if (!optDesc_.isList())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000742 llvm::errs() << "Warning: specifying the 'optional' property"
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000743 "on a non-list option has no effect.\n";
744
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000745 optDesc_.setOptional();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000746 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000747 }
748
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000749 void onMultiVal (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000750 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000751 int val = InitPtrToInt(d.getArg(0));
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000752 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000753 throw "Error in the 'multi_val' property: "
754 "the value must be greater than 1!";
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000755 if (!optDesc_.isParameterList())
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000756 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000757 optDesc_.MultiVal = val;
758 }
759
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000760};
761
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000762/// AddOption - A function object that is applied to every option
763/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000764class AddOption {
765private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000766 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000767
768public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000769 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000770 {}
771
772 void operator()(const Init* i) {
773 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000774 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000775
776 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000777 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000778 const std::string& Name = InitPtrToString(d.getArg(0));
779
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000780 OptionDescription OD(Type, Name);
781
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000782 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000783
Mikhail Glushenkov87685e82010-12-15 01:21:59 +0000784 // Alias option store the aliased option name in the 'Help' field and do not
785 // have any properties.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000786 if (OD.isAlias()) {
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000787 OD.Help = InitPtrToString(d.getArg(1));
788 }
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000789 else {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000790 processOptionProperties(d, OD);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000791 }
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000792
Mikhail Glushenkov87685e82010-12-15 01:21:59 +0000793 // Switch options are ZeroOrMore by default.
794 if (OD.isSwitch()) {
795 if (!(OD.isOptional() || OD.isOneOrMore() || OD.isRequired()))
796 OD.setZeroOrMore();
797 }
798
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000799 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000800 }
801
802private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000803 /// processOptionProperties - Go through the list of option
804 /// properties and call a corresponding handler for each.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000805 static void processOptionProperties (const DagInit& d, OptionDescription& o) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000806 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000807 DagInit::const_arg_iterator B = d.arg_begin();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000808 // Skip the first argument: it's always the option name.
809 ++B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000810 std::for_each(B, d.arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000811 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000812
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000813};
814
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000815/// CollectOptionDescriptions - Collects option properties from all
816/// OptionLists.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +0000817void CollectOptionDescriptions (const RecordVector& V,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000818 OptionDescriptions& OptDescs)
819{
820 // For every OptionList:
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000821 for (RecordVector::const_iterator B = V.begin(), E = V.end(); B!=E; ++B)
822 {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000823 // Throws an exception if the value does not exist.
David Greened4a90662011-07-11 18:25:51 +0000824 const ListInit* PropList = (*B)->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000825
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000826 // For every option description in this list: invoke AddOption.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000827 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
828 }
829}
830
831// Tool information record
832
833namespace ToolFlags {
834 enum ToolFlags { Join = 0x1, Sink = 0x2 };
835}
836
837struct ToolDescription : public RefCountedBase<ToolDescription> {
838 std::string Name;
David Greened4a90662011-07-11 18:25:51 +0000839 const Init* CmdLine;
840 const Init* Actions;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000841 StrVector InLanguage;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000842 std::string InFileOption;
843 std::string OutFileOption;
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000844 StrVector OutLanguage;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000845 std::string OutputSuffix;
846 unsigned Flags;
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000847 const Init* OnEmpty;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000848
849 // Various boolean properties
850 void setSink() { Flags |= ToolFlags::Sink; }
851 bool isSink() const { return Flags & ToolFlags::Sink; }
852 void setJoin() { Flags |= ToolFlags::Join; }
853 bool isJoin() const { return Flags & ToolFlags::Join; }
854
855 // Default ctor here is needed because StringMap can only store
856 // DefaultConstructible objects
Chris Lattner2d900582010-08-28 03:43:50 +0000857 ToolDescription (const std::string &n = "")
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000858 : Name(n), CmdLine(0), Actions(0), OutFileOption("-o"),
859 Flags(0), OnEmpty(0)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000860 {}
861};
862
863/// ToolDescriptions - A list of Tool information records.
864typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
865
866
867/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000868/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000869
870class CollectToolProperties;
871typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000872(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000873
874class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
875{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000876private:
877
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000878 /// toolDesc_ - Properties of the current Tool. This is where the
879 /// information is stored.
880 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000881
882public:
883
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000884 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000885 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000886 {
887 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000888
889 AddHandler("actions", &CollectToolProperties::onActions);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000890 AddHandler("command", &CollectToolProperties::onCommand);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000891 AddHandler("in_language", &CollectToolProperties::onInLanguage);
892 AddHandler("join", &CollectToolProperties::onJoin);
893 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000894
895 AddHandler("out_file_option", &CollectToolProperties::onOutFileOption);
896 AddHandler("in_file_option", &CollectToolProperties::onInFileOption);
897
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000898 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
899 AddHandler("sink", &CollectToolProperties::onSink);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000900 AddHandler("works_on_empty", &CollectToolProperties::onWorksOnEmpty);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000901
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000902 staticMembersInitialized_ = true;
903 }
904 }
905
David Greened4a90662011-07-11 18:25:51 +0000906 void operator() (const Init* I) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000907 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000908 }
909
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000910private:
911
912 /// Property handlers --
913 /// Functions that extract information about tool properties from
914 /// DAG representation.
915
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000916 void onActions (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000917 CheckNumberOfArguments(d, 1);
David Greened4a90662011-07-11 18:25:51 +0000918 const Init* Case = d.getArg(0);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000919 if (typeid(*Case) != typeid(DagInit) ||
David Greened4a90662011-07-11 18:25:51 +0000920 GetOperatorName(static_cast<const DagInit&>(*Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000921 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000922 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000923 }
924
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000925 void onCommand (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000926 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000927 toolDesc_.CmdLine = d.getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000928 }
929
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000930 /// onInOutLanguage - Common implementation of on{In,Out}Language().
931 void onInOutLanguage (const DagInit& d, StrVector& OutVec) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000932 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000933
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000934 // Copy strings to the output vector.
935 for (unsigned i = 0, NumArgs = d.getNumArgs(); i < NumArgs; ++i) {
936 OutVec.push_back(InitPtrToString(d.getArg(i)));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000937 }
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000938
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +0000939 // Remove duplicates.
940 std::sort(OutVec.begin(), OutVec.end());
941 StrVector::iterator newE = std::unique(OutVec.begin(), OutVec.end());
942 OutVec.erase(newE, OutVec.end());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000943 }
944
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000945
946 void onInLanguage (const DagInit& d) {
947 this->onInOutLanguage(d, toolDesc_.InLanguage);
948 }
949
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000950 void onJoin (const DagInit& d) {
Mikhail Glushenkov3a21c552011-05-05 04:24:58 +0000951 bool isReallyJoin = false;
952
953 if (d.getNumArgs() == 0) {
954 isReallyJoin = true;
955 }
956 else {
David Greened4a90662011-07-11 18:25:51 +0000957 const Init* I = d.getArg(0);
Mikhail Glushenkov3a21c552011-05-05 04:24:58 +0000958 isReallyJoin = InitPtrToBool(I);
959 }
960
961 // Is this *really* a join tool? We allow (join false) for generating two
962 // tool descriptions from a single generic one.
963 // TOFIX: come up with a cleaner solution.
964 if (isReallyJoin) {
965 toolDesc_.setJoin();
966 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000967 }
968
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000969 void onOutLanguage (const DagInit& d) {
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000970 this->onInOutLanguage(d, toolDesc_.OutLanguage);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000971 }
972
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000973 void onOutFileOption (const DagInit& d) {
974 CheckNumberOfArguments(d, 1);
975 toolDesc_.OutFileOption = InitPtrToString(d.getArg(0));
976 }
977
978 void onInFileOption (const DagInit& d) {
979 CheckNumberOfArguments(d, 1);
980 toolDesc_.InFileOption = InitPtrToString(d.getArg(0));
981 }
982
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000983 void onOutputSuffix (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000984 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000985 toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000986 }
987
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000988 void onSink (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000989 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000990 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000991 }
992
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000993 void onWorksOnEmpty (const DagInit& d) {
994 toolDesc_.OnEmpty = d.getArg(0);
995 }
996
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000997};
998
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000999/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001000/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001001/// CollectToolProperties function object).
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001002void CollectToolDescriptions (const RecordVector& Tools,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001003 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001004{
1005 // Iterate over a properties list of every Tool definition
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001006 for (RecordVector::const_iterator B = Tools.begin(),
1007 E = Tools.end(); B!=E; ++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00001008 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001009 // Throws an exception if the value does not exist.
David Greened4a90662011-07-11 18:25:51 +00001010 const ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001011
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001012 IntrusiveRefCntPtr<ToolDescription>
1013 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001014
1015 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001016 CollectToolProperties(*ToolDesc));
1017 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001018 }
1019}
1020
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001021/// FillInEdgeVector - Merge all compilation graph definitions into
1022/// one single edge list.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001023void FillInEdgeVector(const RecordVector& CompilationGraphs,
1024 DagVector& Out) {
1025 for (RecordVector::const_iterator B = CompilationGraphs.begin(),
1026 E = CompilationGraphs.end(); B != E; ++B) {
1027 const ListInit* Edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +00001028
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001029 for (ListInit::const_iterator B = Edges->begin(),
1030 E = Edges->end(); B != E; ++B) {
1031 Out.push_back(&InitPtrToDag(*B));
1032 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001033 }
1034}
1035
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001036/// NotInGraph - Helper function object for FilterNotInGraph.
1037struct NotInGraph {
1038private:
1039 const llvm::StringSet<>& ToolsInGraph_;
1040
1041public:
1042 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
1043 : ToolsInGraph_(ToolsInGraph)
1044 {}
1045
1046 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
1047 return (ToolsInGraph_.count(x->Name) == 0);
1048 }
1049};
1050
1051/// FilterNotInGraph - Filter out from ToolDescs all Tools not
1052/// mentioned in the compilation graph definition.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001053void FilterNotInGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001054 ToolDescriptions& ToolDescs) {
1055
1056 // List all tools mentioned in the graph.
1057 llvm::StringSet<> ToolsInGraph;
1058
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001059 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001060 E = EdgeVector.end(); B != E; ++B) {
1061
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001062 const DagInit* Edge = *B;
1063 const std::string& NodeA = InitPtrToString(Edge->getArg(0));
1064 const std::string& NodeB = InitPtrToString(Edge->getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001065
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001066 if (NodeA != "root")
1067 ToolsInGraph.insert(NodeA);
1068 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001069 }
1070
1071 // Filter ToolPropertiesList.
1072 ToolDescriptions::iterator new_end =
1073 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
1074 NotInGraph(ToolsInGraph));
1075 ToolDescs.erase(new_end, ToolDescs.end());
1076}
1077
1078/// FillInToolToLang - Fills in two tables that map tool names to
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001079/// input & output language names. Helper function used by TypecheckGraph().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001080void FillInToolToLang (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 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1084 E = ToolDescs.end(); B != E; ++B) {
1085 const ToolDescription& D = *(*B);
1086 for (StrVector::const_iterator B = D.InLanguage.begin(),
1087 E = D.InLanguage.end(); B != E; ++B)
1088 ToolToInLang[D.Name].insert(*B);
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001089 for (StrVector::const_iterator B = D.OutLanguage.begin(),
1090 E = D.OutLanguage.end(); B != E; ++B)
1091 ToolToOutLang[D.Name].insert(*B);
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001092 }
1093}
1094
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001095/// Intersect - Is set intersection non-empty?
1096bool Intersect (const StringSet<>& S1, const StringSet<>& S2) {
1097 for (StringSet<>::const_iterator B = S1.begin(), E = S1.end(); B != E; ++B) {
1098 if (S2.count(B->first()) != 0)
1099 return true;
1100 }
1101 return false;
1102}
1103
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001104/// TypecheckGraph - Check that names for output and input languages
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00001105/// on all edges do match.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001106void TypecheckGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001107 const ToolDescriptions& ToolDescs) {
1108 StringMap<StringSet<> > ToolToInLang;
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001109 StringMap<StringSet<> > ToolToOutLang;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001110
1111 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001112
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001113 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001114 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001115 const DagInit* Edge = *B;
1116 const std::string& NodeA = InitPtrToString(Edge->getArg(0));
1117 const std::string& NodeB = InitPtrToString(Edge->getArg(1));
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001118 StringMap<StringSet<> >::iterator IA = ToolToOutLang.find(NodeA);
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001119 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001120
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001121 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001122 throw "Edges back to the root are not allowed!";
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00001123
1124 if (NodeA != "root") {
1125 if (IA == ToolToOutLang.end())
1126 throw NodeA + ": no output language defined!";
1127 if (IB == ToolToInLang.end())
1128 throw NodeB + ": no input language defined!";
1129
1130 if (!Intersect(IA->second, IB->second)) {
1131 throw "Edge " + NodeA + "->" + NodeB
1132 + ": output->input language mismatch";
1133 }
1134 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001135 }
1136}
1137
1138/// WalkCase - Walks the 'case' expression DAG and invokes
1139/// TestCallback on every test, and StatementCallback on every
1140/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001141/// combinators (that is, they are passed directly to TestCallback).
1142/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1143/// IndentLevel, bool FirstTest)'.
1144/// StatementCallback must have type 'void StatementCallback(const Init*,
1145/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001146template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001147void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1148 unsigned IndentLevel = 0)
1149{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001150 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001151
1152 // Error checks.
1153 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001154 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001155
1156 if (d.getNumArgs() < 2)
1157 throw "There should be at least one clause in the 'case' expression:\n"
1158 + d.getAsString();
1159
1160 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001161 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001162 const unsigned numArgs = d.getNumArgs();
1163 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001164 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1165 B != E; ++B) {
David Greened4a90662011-07-11 18:25:51 +00001166 const Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001167
1168 if (!even)
1169 {
1170 // Handle test.
1171 const DagInit& Test = InitPtrToDag(arg);
1172
1173 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001174 throw "The 'default' clause should be the last in the "
1175 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001176 if (i == numArgs)
1177 throw "Case construct handler: no corresponding action "
1178 "found for the test " + Test.getAsString() + '!';
1179
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001180 TestCallback(Test, IndentLevel, (i == 1));
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001181 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001182 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001183 {
David Greened4a90662011-07-11 18:25:51 +00001184 if (dynamic_cast<const DagInit*>(arg)
1185 && GetOperatorName(static_cast<const DagInit&>(*arg)) == "case") {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001186 // Nested 'case'.
1187 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1188 }
1189
1190 // Handle statement.
1191 StatementCallback(arg, IndentLevel);
1192 }
1193
1194 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001195 even = !even;
1196 }
1197}
1198
1199/// ExtractOptionNames - A helper function object used by
1200/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1201class ExtractOptionNames {
1202 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001203
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001204 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001205 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001206 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001207 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001208 ActionName == "forward_value" ||
1209 ActionName == "forward_transformed_value" ||
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001210 ActionName == "parameter_equals" || ActionName == "element_in_list") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001211 CheckNumberOfArguments(Stmt, 1);
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001212
David Greened4a90662011-07-11 18:25:51 +00001213 const Init* Arg = Stmt.getArg(0);
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001214 if (typeid(*Arg) == typeid(StringInit))
1215 OptionNames_.insert(InitPtrToString(Arg));
1216 }
1217 else if (ActionName == "any_switch_on" || ActionName == "switch_on" ||
1218 ActionName == "any_not_empty" || ActionName == "any_empty" ||
1219 ActionName == "not_empty" || ActionName == "empty") {
1220 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
David Greened4a90662011-07-11 18:25:51 +00001221 const Init* Arg = Stmt.getArg(i);
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001222 if (typeid(*Arg) == typeid(StringInit))
1223 OptionNames_.insert(InitPtrToString(Arg));
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001224 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001225 }
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001226 else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001227 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001228 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001229 }
1230 }
1231 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001232
1233public:
1234 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1235 {}
1236
1237 void operator()(const Init* Statement) {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001238 // Statement is either a dag, or a list of dags.
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001239 if (typeid(*Statement) == typeid(ListInit)) {
1240 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1241 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1242 B != E; ++B)
1243 this->processDag(*B);
1244 }
1245 else {
1246 this->processDag(Statement);
1247 }
1248 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001249
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001250 void operator()(const DagInit& Test, unsigned, bool) {
1251 this->operator()(&Test);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001252 }
1253 void operator()(const Init* Statement, unsigned) {
1254 this->operator()(Statement);
1255 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001256};
1257
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00001258/// IsOptionalEdge - Validate that the 'optional_edge' has proper structure.
1259bool IsOptionalEdge (const DagInit& Edg) {
1260 return (GetOperatorName(Edg) == "optional_edge") && (Edg.getNumArgs() > 2);
1261}
1262
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001263/// CheckForSuperfluousOptions - Check that there are no side
1264/// effect-free options (specified only in the OptionList). Otherwise,
1265/// output a warning.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001266void CheckForSuperfluousOptions (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001267 const ToolDescriptions& ToolDescs,
1268 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001269 llvm::StringSet<> nonSuperfluousOptions;
1270
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001271 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001272 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001273 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1274 E = ToolDescs.end(); B != E; ++B) {
1275 const ToolDescription& TD = *(*B);
1276 ExtractOptionNames Callback(nonSuperfluousOptions);
1277 if (TD.Actions)
1278 WalkCase(TD.Actions, Callback, Callback);
1279 }
1280
1281 // Add all options mentioned in the 'case' clauses of the
1282 // OptionalEdges of the compilation graph to the set of
1283 // non-superfluous options.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001284 for (DagVector::const_iterator B = EdgeVector.begin(),
1285 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00001286 const DagInit& Edge = **B;
1287 if (IsOptionalEdge(Edge)) {
1288 const DagInit& Weight = InitPtrToDag(Edge.getArg(2));
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001289 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00001290 }
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001291 }
1292
1293 // Check that all options in OptDescs belong to the set of
1294 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001295 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001296 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001297 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001298 if (!nonSuperfluousOptions.count(Val.Name)
1299 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001300 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001301 "Probable cause: this option is specified only in the OptionList.\n";
1302 }
1303}
1304
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001305/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1306bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1307 if (TestName == "single_input_file") {
1308 O << "InputFilenames.size() == 1";
1309 return true;
1310 }
1311 else if (TestName == "multiple_input_files") {
1312 O << "InputFilenames.size() > 1";
1313 return true;
1314 }
1315
1316 return false;
1317}
1318
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001319/// EmitMultipleArgumentTest - Helper function used by
1320/// EmitCaseTestMultipleArgs()
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001321template <typename F>
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001322void EmitMultipleArgumentTest(const DagInit& D, const char* LogicOp,
1323 F Callback, raw_ostream& O)
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001324{
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001325 for (unsigned i = 0, NumArgs = D.getNumArgs(); i < NumArgs; ++i) {
1326 if (i != 0)
1327 O << ' ' << LogicOp << ' ';
1328 Callback(InitPtrToString(D.getArg(i)), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001329 }
1330}
1331
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001332// Callbacks for use with EmitMultipleArgumentTest
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001333
1334class EmitSwitchOn {
1335 const OptionDescriptions& OptDescs_;
1336public:
1337 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1338 {}
1339
1340 void operator()(const std::string& OptName, raw_ostream& O) const {
1341 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1342 O << OptDesc.GenVariableName();
1343 }
1344};
1345
1346class EmitEmptyTest {
1347 bool EmitNegate_;
1348 const OptionDescriptions& OptDescs_;
1349public:
1350 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1351 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1352 {}
1353
1354 void operator()(const std::string& OptName, raw_ostream& O) const {
1355 const char* Neg = (EmitNegate_ ? "!" : "");
1356 if (OptName == "o") {
1357 O << Neg << "OutputFilename.empty()";
1358 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001359 else if (OptName == "save-temps") {
1360 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1361 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001362 else {
1363 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1364 O << Neg << OptDesc.GenVariableName() << ".empty()";
1365 }
1366 }
1367};
1368
1369
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001370/// EmitCaseTestMultipleArgs - Helper function used by EmitCaseTest1Arg()
1371bool EmitCaseTestMultipleArgs (const std::string& TestName,
1372 const DagInit& d,
1373 const OptionDescriptions& OptDescs,
1374 raw_ostream& O) {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001375 if (TestName == "any_switch_on") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001376 EmitMultipleArgumentTest(d, "||", EmitSwitchOn(OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001377 return true;
1378 }
1379 else if (TestName == "switch_on") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001380 EmitMultipleArgumentTest(d, "&&", EmitSwitchOn(OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001381 return true;
1382 }
1383 else if (TestName == "any_not_empty") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001384 EmitMultipleArgumentTest(d, "||", EmitEmptyTest(true, OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001385 return true;
1386 }
1387 else if (TestName == "any_empty") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001388 EmitMultipleArgumentTest(d, "||", EmitEmptyTest(false, OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001389 return true;
1390 }
1391 else if (TestName == "not_empty") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001392 EmitMultipleArgumentTest(d, "&&", EmitEmptyTest(true, OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001393 return true;
1394 }
1395 else if (TestName == "empty") {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001396 EmitMultipleArgumentTest(d, "&&", EmitEmptyTest(false, OptDescs), O);
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001397 return true;
1398 }
1399
1400 return false;
1401}
1402
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001403/// EmitCaseTest1Arg - Helper function used by EmitCaseTest1OrMoreArgs()
1404bool EmitCaseTest1Arg (const std::string& TestName,
1405 const DagInit& d,
1406 const OptionDescriptions& OptDescs,
1407 raw_ostream& O) {
1408 const std::string& Arg = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001409
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001410 if (TestName == "input_languages_contain") {
1411 O << "InLangs.count(\"" << Arg << "\") != 0";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001412 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001413 }
1414 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001415 // This works only for single-argument Tool::GenerateAction. Join
1416 // tools can process several files in different languages simultaneously.
1417
1418 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001419 O << "LangMap.GetLanguage(inFile) == \"" << Arg << '\"';
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001420 return true;
1421 }
1422
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001423 return false;
1424}
1425
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001426/// EmitCaseTest1OrMoreArgs - Helper function used by
1427/// EmitCaseConstructHandler()
1428bool EmitCaseTest1OrMoreArgs(const std::string& TestName,
1429 const DagInit& d,
1430 const OptionDescriptions& OptDescs,
1431 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001432 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001433 return EmitCaseTest1Arg(TestName, d, OptDescs, O) ||
1434 EmitCaseTestMultipleArgs(TestName, d, OptDescs, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001435}
1436
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001437/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001438bool EmitCaseTest2Args(const std::string& TestName,
1439 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001440 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 Glushenkov9bef1bd2009-12-23 12:49:41 +00001443 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001444 const std::string& OptName = InitPtrToString(d.getArg(0));
1445 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001446
1447 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001448 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001449 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1450 return true;
1451 }
1452 else if (TestName == "element_in_list") {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00001453 const OptionDescription& OptDesc = OptDescs.FindParameterList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001454 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001455 O << "std::find(" << VarName << ".begin(),\n";
1456 O.indent(IndentLevel + Indent1)
1457 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001458 << OptArg << "\") != " << VarName << ".end()";
1459 return true;
1460 }
1461
1462 return false;
1463}
1464
1465// Forward declaration.
1466// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001467void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001468 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001469 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001470
1471/// EmitLogicalOperationTest - Helper function used by
1472/// EmitCaseConstructHandler.
1473void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001474 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001475 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001476 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001477 O << '(';
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001478 for (unsigned i = 0, NumArgs = d.getNumArgs(); i < NumArgs; ++i) {
1479 const DagInit& InnerTest = InitPtrToDag(d.getArg(i));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001480 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001481 if (i != NumArgs - 1) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001482 O << ")\n";
1483 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1484 }
1485 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001486 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001487 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001488 }
1489}
1490
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001491void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001492 const OptionDescriptions& OptDescs, raw_ostream& O)
1493{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001494 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001495 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1496 O << "! (";
1497 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1498 O << ")";
1499}
1500
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001501/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001502void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001503 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001504 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001505 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001506
1507 if (TestName == "and")
1508 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1509 else if (TestName == "or")
1510 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001511 else if (TestName == "not")
1512 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001513 else if (EmitCaseTest0Args(TestName, O))
1514 return;
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001515 else if (EmitCaseTest1OrMoreArgs(TestName, d, OptDescs, O))
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001516 return;
1517 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1518 return;
1519 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001520 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001521}
1522
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001523
1524/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1525class EmitCaseTestCallback {
1526 bool EmitElseIf_;
1527 const OptionDescriptions& OptDescs_;
1528 raw_ostream& O_;
1529public:
1530
1531 EmitCaseTestCallback(bool EmitElseIf,
1532 const OptionDescriptions& OptDescs, raw_ostream& O)
1533 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1534 {}
1535
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001536 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001537 {
1538 if (GetOperatorName(Test) == "default") {
1539 O_.indent(IndentLevel) << "else {\n";
1540 }
1541 else {
1542 O_.indent(IndentLevel)
1543 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001544 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001545 O_ << ") {\n";
1546 }
1547 }
1548};
1549
1550/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1551template <typename F>
1552class EmitCaseStatementCallback {
1553 F Callback_;
1554 raw_ostream& O_;
1555public:
1556
1557 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1558 : Callback_(Callback), O_(O)
1559 {}
1560
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001561 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001562 // Is this a nested 'case'?
1563 bool IsCase = dynamic_cast<const DagInit*>(Statement) &&
1564 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case";
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001565
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00001566 // If so, ignore it, it is handled by our caller, WalkCase.
1567 if (!IsCase) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001568 if (typeid(*Statement) == typeid(ListInit)) {
1569 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1570 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1571 B != E; ++B)
1572 Callback_(*B, (IndentLevel + Indent1), O_);
1573 }
1574 else {
1575 Callback_(Statement, (IndentLevel + Indent1), O_);
1576 }
1577 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001578 O_.indent(IndentLevel) << "}\n";
1579 }
1580
1581};
1582
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001583/// EmitCaseConstructHandler - Emit code that handles the 'case'
1584/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001585/// clause. Implemented on top of WalkCase.
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00001586/// Callback's type is void F(const Init* Statement, unsigned IndentLevel,
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001587/// raw_ostream& O).
1588/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001589/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1590/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001591template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001592void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001593 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001594 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001595 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001596 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1597 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001598}
1599
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001600/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001601/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1602/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001603void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001604 const char* Delimiters = " \t\n\v\f\r";
1605 enum TokenizerState
1606 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1607 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001608
1609 if (CmdLine.empty())
1610 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001611 Out.push_back("");
1612
1613 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1614 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001615
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001616 for (; B != E; ++B) {
1617 char cur_ch = CmdLine[B];
1618
1619 switch (cur_st) {
1620 case Normal:
1621 if (cur_ch == '$') {
1622 cur_st = SpecialCommand;
1623 break;
1624 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001625 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001626 // Skip whitespace
1627 B = CmdLine.find_first_not_of(Delimiters, B);
1628 if (B == std::string::npos) {
1629 B = E-1;
1630 continue;
1631 }
1632 --B;
1633 Out.push_back("");
1634 continue;
1635 }
1636 break;
1637
1638
1639 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001640 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001641 cur_st = Normal;
1642 Out.push_back("");
1643 continue;
1644 }
1645 if (cur_ch == '(') {
1646 Out.push_back("");
1647 cur_st = InsideSpecialCommand;
1648 continue;
1649 }
1650 break;
1651
1652 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001653 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001654 continue;
1655 }
1656 if (cur_ch == '\'') {
1657 cur_st = InsideQuotationMarks;
1658 Out.push_back("");
1659 continue;
1660 }
1661 if (cur_ch == ')') {
1662 cur_st = Normal;
1663 Out.push_back("");
1664 }
1665 if (cur_ch == ',') {
1666 continue;
1667 }
1668
1669 break;
1670
1671 case InsideQuotationMarks:
1672 if (cur_ch == '\'') {
1673 cur_st = InsideSpecialCommand;
1674 continue;
1675 }
1676 break;
1677 }
1678
1679 Out.back().push_back(cur_ch);
1680 }
1681}
1682
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001683/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1684/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1685/// SubstituteSpecialCommands().
1686StrVector::const_iterator
1687SubstituteCall (StrVector::const_iterator Pos,
1688 StrVector::const_iterator End,
1689 bool IsJoin, raw_ostream& O)
1690{
1691 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001692 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001693 const std::string& CmdName = *Pos;
1694
1695 if (CmdName == ")")
1696 throw "$CALL invocation: empty argument list!";
1697
1698 O << "hooks::";
1699 O << CmdName << "(";
1700
1701
1702 bool firstIteration = true;
1703 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001704 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001705 const std::string& Arg = *Pos;
1706 assert(Arg.size() != 0);
1707
1708 if (Arg[0] == ')')
1709 break;
1710
1711 if (firstIteration)
1712 firstIteration = false;
1713 else
1714 O << ", ";
1715
1716 if (Arg == "$INFILE") {
1717 if (IsJoin)
1718 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1719 else
1720 O << "inFile.c_str()";
1721 }
1722 else {
1723 O << '"' << Arg << '"';
1724 }
1725 }
1726
1727 O << ')';
1728
1729 return Pos;
1730}
1731
1732/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1733/// function used by SubstituteSpecialCommands().
1734StrVector::const_iterator
1735SubstituteEnv (StrVector::const_iterator Pos,
1736 StrVector::const_iterator End, raw_ostream& O)
1737{
1738 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001739 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001740 const std::string& EnvName = *Pos;
1741
1742 if (EnvName == ")")
1743 throw "$ENV invocation: empty argument list!";
1744
1745 O << "checkCString(std::getenv(\"";
1746 O << EnvName;
1747 O << "\"))";
1748
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001749 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001750
1751 return Pos;
1752}
1753
1754/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1755/// handler code. Helper function used by EmitCmdLineVecFill().
1756StrVector::const_iterator
1757SubstituteSpecialCommands (StrVector::const_iterator Pos,
1758 StrVector::const_iterator End,
1759 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001760{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001761
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001762 const std::string& cmd = *Pos;
1763
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001764 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001765 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001766 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001767 }
1768 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001769 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001770 }
1771 else {
1772 throw "Unknown special command: " + cmd;
1773 }
1774
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001775 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001776 const std::string& Leftover = *Pos;
1777 assert(Leftover.at(0) == ')');
1778 if (Leftover.size() != 1)
1779 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001780
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001781 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001782}
1783
1784/// EmitCmdLineVecFill - Emit code that fills in the command line
1785/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001786void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001787 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001788 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001789 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001790 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001791
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001792 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001793 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001794
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001795 StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001796
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001797 // Emit the command itself.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001798 assert(!StrVec[0].empty());
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001799 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001800 if (StrVec[0][0] == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001801 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1802 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001803 }
1804 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001805 O << '"' << StrVec[0] << '"';
1806 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001807 }
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001808 O << ";\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001809
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001810 // Go through the command arguments.
1811 assert(B <= E);
1812 for (; B != E; ++B) {
1813 const std::string& cmd = *B;
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001814
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001815 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001816 O.indent(IndentLevel);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001817
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001818 if (cmd.at(0) == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001819 O << "vec.push_back(std::make_pair(0, ";
1820 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1821 O << "));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001822 }
1823 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001824 O << "vec.push_back(std::make_pair(0, \"" << cmd << "\"));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001825 }
1826 }
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001827
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001828}
1829
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001830/// EmitForEachListElementCycleHeader - Emit common code for iterating through
1831/// all elements of a list. Helper function used by
1832/// EmitForwardOptionPropertyHandlingCode.
1833void EmitForEachListElementCycleHeader (const OptionDescription& D,
1834 unsigned IndentLevel,
1835 raw_ostream& O) {
1836 unsigned IndentLevel1 = IndentLevel + Indent1;
1837
1838 O.indent(IndentLevel)
1839 << "for (" << D.GenTypeDeclaration()
1840 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1841 O.indent(IndentLevel)
1842 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1843 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1844 << ".getPosition(B - " << D.GenVariableName()
1845 << ".begin());\n";
1846}
1847
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001848/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1849/// implement EmitActionHandler. Emits code for
1850/// handling the (forward) and (forward_as) option properties.
1851void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001852 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001853 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001854 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001855 const std::string& Name = NewName.empty()
1856 ? ("-" + D.Name)
1857 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001858 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001859
1860 switch (D.Type) {
1861 case OptionType::Switch:
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001862 O.indent(IndentLevel)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001863 << "vec.push_back(std::make_pair(" << D.GenVariableName()
1864 << ".getPosition(), \"" << Name << "\"));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001865 break;
1866 case OptionType::Parameter:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001867 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1868 << D.GenVariableName()
1869 <<".getPosition(), \"" << Name;
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001870
1871 if (!D.isForwardNotSplit()) {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001872 O << "\"));\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001873 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1874 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001875 << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001876 }
1877 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001878 O << "=\" + " << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001879 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001880 break;
1881 case OptionType::Prefix:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001882 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1883 << D.GenVariableName() << ".getPosition(), \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001884 << Name << "\" + "
1885 << D.GenVariableName() << "));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001886 break;
1887 case OptionType::PrefixList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001888 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001889 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001890 << Name << "\" + " << "*B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001891 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001892
1893 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001894 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001895 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001896 }
1897
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001898 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001899 break;
1900 case OptionType::ParameterList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001901 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001902 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001903 << Name << "\"));\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001904
1905 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001906 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001907 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001908 }
1909
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001910 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001911 break;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001912 case OptionType::SwitchList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001913 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001914 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
1915 << Name << "\"));\n";
1916 O.indent(IndentLevel1) << "++B;\n";
1917 O.indent(IndentLevel) << "}\n";
1918 break;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001919 case OptionType::Alias:
1920 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001921 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001922 }
1923}
1924
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001925/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1926/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001927struct ActionHandlingCallbackBase
1928{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001929
1930 void onErrorDag(const DagInit& d,
1931 unsigned IndentLevel, raw_ostream& O) const
1932 {
1933 O.indent(IndentLevel)
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00001934 << "PrintError(\""
1935 << (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0)) : "Unknown error!")
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001936 << "\");\n";
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +00001937 O.indent(IndentLevel) << "return 1;\n";
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001938 }
1939
1940 void onWarningDag(const DagInit& d,
1941 unsigned IndentLevel, raw_ostream& O) const
1942 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001943 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001944 O.indent(IndentLevel) << "llvm::errs() << \""
1945 << InitPtrToString(d.getArg(0)) << "\";\n";
1946 }
1947
1948};
1949
1950/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1951/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001952class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001953
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001954typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1955(const DagInit&, unsigned, raw_ostream&) const;
1956
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001957class EmitActionHandlersCallback :
1958 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001959 public HandlerTable<EmitActionHandlersCallbackHandler>
1960{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001961 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001962
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001963 const OptionDescriptions& OptDescs;
1964
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001965 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1966 /// onAppendCmd and onOutputSuffix.
1967 void EmitHookInvocation(const std::string& Str,
1968 const char* BlockOpen, const char* BlockClose,
1969 unsigned IndentLevel, raw_ostream& O) const
1970 {
1971 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001972 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001973
1974 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1975 B != E; ++B) {
1976 const std::string& cmd = *B;
1977
1978 O.indent(IndentLevel) << BlockOpen;
1979
1980 if (cmd.at(0) == '$')
1981 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1982 else
1983 O << '"' << cmd << '"';
1984
1985 O << BlockClose;
1986 }
1987 }
1988
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001989 void onAppendCmd (const DagInit& Dag,
1990 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001991 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001992 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001993 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001994 "vec.push_back(std::make_pair(65536, ", "));\n",
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001995 IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001996 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001997
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001998 void onForward (const DagInit& Dag,
1999 unsigned IndentLevel, raw_ostream& O) const
2000 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002001 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002002 const std::string& Name = InitPtrToString(Dag.getArg(0));
2003 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2004 IndentLevel, "", O);
2005 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002006
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002007 void onForwardAs (const DagInit& Dag,
2008 unsigned IndentLevel, raw_ostream& O) const
2009 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002010 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002011 const std::string& Name = InitPtrToString(Dag.getArg(0));
2012 const std::string& NewName = InitPtrToString(Dag.getArg(1));
2013 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2014 IndentLevel, NewName, O);
2015 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002016
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002017 void onForwardValue (const DagInit& Dag,
2018 unsigned IndentLevel, raw_ostream& O) const
2019 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002020 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002021 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002022 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002023
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002024 if (D.isSwitchList()) {
2025 throw std::runtime_error
2026 ("forward_value is not allowed with switch_list");
2027 }
2028
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002029 if (D.isParameter()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002030 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2031 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002032 << D.GenVariableName() << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002033 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00002034 else {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002035 O.indent(IndentLevel) << "for (" << D.GenTypeDeclaration()
2036 << "::iterator B = " << D.GenVariableName()
2037 << ".begin(), \n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002038 O.indent(IndentLevel + Indent1) << " E = " << D.GenVariableName()
2039 << ".end(); B != E; ++B)\n";
2040 O.indent(IndentLevel) << "{\n";
2041 O.indent(IndentLevel + Indent1)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002042 << "unsigned pos = " << D.GenVariableName()
2043 << ".getPosition(B - " << D.GenVariableName()
2044 << ".begin());\n";
2045 O.indent(IndentLevel + Indent1)
2046 << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002047 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002048 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002049 }
2050
2051 void onForwardTransformedValue (const DagInit& Dag,
2052 unsigned IndentLevel, raw_ostream& O) const
2053 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002054 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002055 const std::string& Name = InitPtrToString(Dag.getArg(0));
2056 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002057 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002058
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002059 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2060 << D.GenVariableName() << ".getPosition("
2061 << (D.isList() ? "0" : "") << "), "
2062 << "hooks::" << Hook << "(" << D.GenVariableName()
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002063 << (D.isParameter() ? ".c_str()" : "") << ")));\n";
2064 }
2065
2066 void onNoOutFile (const DagInit& Dag,
2067 unsigned IndentLevel, raw_ostream& O) const
2068 {
2069 CheckNumberOfArguments(Dag, 0);
2070 O.indent(IndentLevel) << "no_out_file = true;\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002071 }
2072
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002073 void onOutputSuffix (const DagInit& Dag,
2074 unsigned IndentLevel, raw_ostream& O) const
2075 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002076 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002077 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
2078 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002079 }
2080
2081 void onStopCompilation (const DagInit& Dag,
2082 unsigned IndentLevel, raw_ostream& O) const
2083 {
2084 O.indent(IndentLevel) << "stop_compilation = true;\n";
2085 }
2086
2087
2088 void onUnpackValues (const DagInit& Dag,
2089 unsigned IndentLevel, raw_ostream& O) const
2090 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002091 throw "'unpack_values' is deprecated. "
2092 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002093 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002094
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002095 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002096
2097 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
2098 : OptDescs(OD)
2099 {
2100 if (!staticMembersInitialized_) {
2101 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
2102 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
2103 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
2104 AddHandler("forward", &EmitActionHandlersCallback::onForward);
2105 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002106 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
2107 AddHandler("forward_transformed_value",
2108 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002109 AddHandler("no_out_file",
2110 &EmitActionHandlersCallback::onNoOutFile);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002111 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
2112 AddHandler("stop_compilation",
2113 &EmitActionHandlersCallback::onStopCompilation);
2114 AddHandler("unpack_values",
2115 &EmitActionHandlersCallback::onUnpackValues);
2116
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002117
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002118 staticMembersInitialized_ = true;
2119 }
2120 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002121
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002122 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002123 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002124 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002125 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002126 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002127};
2128
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002129void EmitGenerateActionMethodHeader(const ToolDescription& D,
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002130 bool IsJoin, bool Naked,
2131 raw_ostream& O)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002132{
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002133 O.indent(Indent1) << "int GenerateAction(Action& Out,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002134
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002135 if (IsJoin)
2136 O.indent(Indent2) << "const PathVector& inFiles,\n";
2137 else
2138 O.indent(Indent2) << "const sys::Path& inFile,\n";
2139
2140 O.indent(Indent2) << "const bool HasChildren,\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002141 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2142 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2143 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2144 O.indent(Indent1) << "{\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002145
2146 if (!Naked) {
2147 O.indent(Indent2) << "std::string cmd;\n";
2148 O.indent(Indent2) << "std::string out_file;\n";
2149 O.indent(Indent2)
2150 << "std::vector<std::pair<unsigned, std::string> > vec;\n";
2151 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2152 O.indent(Indent2) << "bool no_out_file = false;\n";
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002153 O.indent(Indent2) << "std::string output_suffix(\""
2154 << D.OutputSuffix << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002155 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002156}
2157
2158// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2159// Tool::GenerateAction() method.
2160void EmitGenerateActionMethod (const ToolDescription& D,
2161 const OptionDescriptions& OptDescs,
2162 bool IsJoin, raw_ostream& O) {
2163
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002164 EmitGenerateActionMethodHeader(D, IsJoin, /* Naked = */ false, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002165
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002166 if (!D.CmdLine)
2167 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002168
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002169 // Process the 'command' property.
2170 O << '\n';
2171 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2172 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002173
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002174 // Process the 'actions' list of this tool.
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002175 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002176 EmitCaseConstructHandler(D.Actions, Indent2,
2177 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002178 false, OptDescs, O);
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002179 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002180
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002181 // Input file (s)
2182 if (!D.InFileOption.empty()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002183 O.indent(Indent2)
2184 << "vec.push_back(std::make_pair(InputFilenames.getPosition(0), \""
2185 << D.InFileOption << "\");\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002186 }
2187
2188 if (IsJoin) {
2189 O.indent(Indent2)
2190 << "for (PathVector::const_iterator B = inFiles.begin(),\n";
2191 O.indent(Indent3) << "E = inFiles.end(); B != E; ++B)\n";
2192 O.indent(Indent2) << "{\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002193 O.indent(Indent3) << "vec.push_back(std::make_pair("
2194 << "InputFilenames.getPosition(B - inFiles.begin()), "
2195 << "B->str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002196 O.indent(Indent2) << "}\n";
2197 }
2198 else {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002199 O.indent(Indent2) << "vec.push_back(std::make_pair("
2200 << "InputFilenames.getPosition(0), inFile.str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002201 }
2202
2203 // Output file
2204 O.indent(Indent2) << "if (!no_out_file) {\n";
2205 if (!D.OutFileOption.empty())
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002206 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002207 << D.OutFileOption << "\"));\n";
2208
2209 O.indent(Indent3) << "out_file = this->OutFilename("
2210 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002211 O.indent(Indent4) <<
2212 "TempDir, stop_compilation, output_suffix.c_str()).str();\n\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002213 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, out_file));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002214
2215 O.indent(Indent2) << "}\n\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002216
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002217 // Handle the Sink property.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002218 std::string SinkOption("autogenerated::");
2219 SinkOption += SinkOptionName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002220 if (D.isSink()) {
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002221 O.indent(Indent2) << "if (!" << SinkOption << ".empty()) {\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002222 O.indent(Indent3) << "for (cl::list<std::string>::iterator B = "
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002223 << SinkOption << ".begin(), E = " << SinkOption
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002224 << ".end(); B != E; ++B)\n";
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002225 O.indent(Indent4) << "vec.push_back(std::make_pair(" << SinkOption
2226 << ".getPosition(B - " << SinkOption
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002227 << ".begin()), *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002228 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002229 }
2230
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002231 O.indent(Indent2) << "Out.Construct(cmd, this->SortArgs(vec), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002232 << "stop_compilation, out_file);\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002233 O.indent(Indent2) << "return 0;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002234 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002235}
2236
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002237/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2238/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002239void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2240 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002241 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002242 if (!ToolDesc.isJoin()) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002243 EmitGenerateActionMethodHeader(ToolDesc, /* IsJoin = */ true,
2244 /* Naked = */ true, O);
2245 O.indent(Indent2) << "PrintError(\"" << ToolDesc.Name
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002246 << " is not a Join tool!\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002247 O.indent(Indent2) << "return -1;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002248 O.indent(Indent1) << "}\n\n";
2249 }
2250 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002251 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002252 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002253
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002254 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002255}
2256
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002257/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2258/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002259void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002260 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2261 O.indent(Indent2) << "return InputLanguages_;\n";
2262 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002263
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002264 O.indent(Indent1) << "const char** OutputLanguages() const {\n";
2265 O.indent(Indent2) << "return OutputLanguages_;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002266 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002267}
2268
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002269/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002270void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002271 O.indent(Indent1) << "const char* Name() const {\n";
2272 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2273 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002274}
2275
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002276/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2277/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002278void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002279 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002280 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002281 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002282 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002283 O.indent(Indent2) << "return false;\n";
2284 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002285}
2286
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002287/// EmitWorksOnEmptyCallback - Callback used by EmitWorksOnEmptyMethod in
2288/// conjunction with EmitCaseConstructHandler.
2289void EmitWorksOnEmptyCallback (const Init* Value,
2290 unsigned IndentLevel, raw_ostream& O) {
2291 CheckBooleanConstant(Value);
2292 O.indent(IndentLevel) << "return " << Value->getAsString() << ";\n";
2293}
2294
2295/// EmitWorksOnEmptyMethod - Emit the WorksOnEmpty() method for a given Tool
2296/// class.
2297void EmitWorksOnEmptyMethod (const ToolDescription& D,
2298 const OptionDescriptions& OptDescs,
2299 raw_ostream& O)
2300{
2301 O.indent(Indent1) << "bool WorksOnEmpty() const {\n";
2302 if (D.OnEmpty == 0)
2303 O.indent(Indent2) << "return false;\n";
2304 else
2305 EmitCaseConstructHandler(D.OnEmpty, Indent2, EmitWorksOnEmptyCallback,
2306 /*EmitElseIf = */ true, OptDescs, O);
2307 O.indent(Indent1) << "}\n\n";
2308}
2309
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002310/// EmitStrArray - Emit definition of a 'const char**' static member
2311/// variable. Helper used by EmitStaticMemberDefinitions();
2312void EmitStrArray(const std::string& Name, const std::string& VarName,
2313 const StrVector& StrVec, raw_ostream& O) {
2314 O << "const char* " << Name << "::" << VarName << "[] = {";
2315 for (StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
2316 B != E; ++B)
2317 O << '\"' << *B << "\", ";
2318 O << "0};\n";
2319}
2320
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002321/// EmitStaticMemberDefinitions - Emit static member definitions for a
2322/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002323void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002324 if (D.InLanguage.empty())
2325 throw "Tool " + D.Name + " has no 'in_language' property!";
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002326 if (D.OutLanguage.empty())
2327 throw "Tool " + D.Name + " has no 'out_language' property!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002328
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002329 EmitStrArray(D.Name, "InputLanguages_", D.InLanguage, O);
2330 EmitStrArray(D.Name, "OutputLanguages_", D.OutLanguage, O);
2331 O << '\n';
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002332}
2333
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002334/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002335void EmitToolClassDefinition (const ToolDescription& D,
2336 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002337 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002338 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002339 return;
2340
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002341 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002342 O << "class " << D.Name << " : public ";
2343 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002344 O << "JoinTool";
2345 else
2346 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002347
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002348 O << " {\nprivate:\n";
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +00002349 O.indent(Indent1) << "static const char* InputLanguages_[];\n";
2350 O.indent(Indent1) << "static const char* OutputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002351
2352 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002353 EmitNameMethod(D, O);
2354 EmitInOutLanguageMethods(D, O);
2355 EmitIsJoinMethod(D, O);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002356 EmitWorksOnEmptyMethod(D, OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002357 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002358
2359 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002360 O << "};\n";
2361
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002362 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002363
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002364}
2365
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002366/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002367/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002368void EmitOptionDefinitions (const OptionDescriptions& descs,
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002369 bool HasSink, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002370{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002371 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002372
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002373 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002374 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002375 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002376 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002377
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002378 if (val.Type == OptionType::Alias) {
2379 Aliases.push_back(val);
2380 continue;
2381 }
2382
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002383 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002384 << val.GenPlainVariableName();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002385
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002386 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002387
2388 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2389 O << ", cl::Prefix";
2390
2391 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002392 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002393 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002394 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002395 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002396 }
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002397
2398 if (val.isOptional())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002399 O << ", cl::Optional";
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002400
2401 if (val.isOneOrMore())
2402 O << ", cl::OneOrMore";
2403
2404 if (val.isZeroOrMore())
2405 O << ", cl::ZeroOrMore";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002406
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002407 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002408 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002409 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002410 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002411
2412 if (val.isCommaSeparated())
2413 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002414
2415 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002416 O << ", cl::multi_val(" << val.MultiVal << ')';
2417
2418 if (val.InitVal) {
2419 const std::string& str = val.InitVal->getAsString();
2420 O << ", cl::init(" << str << ')';
2421 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002422
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002423 if (!val.Help.empty())
2424 O << ", cl::desc(\"" << val.Help << "\")";
2425
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002426 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002427 }
2428
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002429 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002430 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002431 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002432 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002433
2434 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov297514d2010-08-20 18:16:26 +00002435 << val.GenPlainVariableName()
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002436 << "(\"" << val.Name << '\"';
2437
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002438 const OptionDescription& D = descs.FindOption(val.Help);
2439 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002440
2441 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2442 }
2443
2444 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002445 if (HasSink)
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002446 O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002447
2448 O << '\n';
2449}
2450
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002451/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002452/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002453
2454class EmitPreprocessOptionsCallback;
2455
2456typedef void
2457(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2458(const DagInit&, unsigned, raw_ostream&) const;
2459
2460class EmitPreprocessOptionsCallback :
2461 public ActionHandlingCallbackBase,
2462 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2463{
2464 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002465 typedef void
2466 (EmitPreprocessOptionsCallback::* HandlerImpl)
2467 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002468
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002469 const OptionDescriptions& OptDescs_;
2470
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002471 void onEachArgument(const DagInit& d, HandlerImpl h,
2472 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002473 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002474 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002475
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002476 for (unsigned i = 0, NumArgs = d.getNumArgs(); i < NumArgs; ++i) {
2477 ((this)->*(h))(d.getArg(i), IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002478 }
2479 }
2480
2481 void onUnsetOptionImpl(const Init* I,
2482 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002483 {
2484 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002485 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002486
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002487 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002488 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2489 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002490 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002491 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2492 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002493 else if (OptDesc.isList()) {
2494 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2495 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002496 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002497 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002498 }
2499 }
2500
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002501 void onUnsetOption(const DagInit& d,
2502 unsigned IndentLevel, raw_ostream& O) const
2503 {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002504 this->onEachArgument(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2505 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002506 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002507
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002508 void onSetOptionImpl(const DagInit& D,
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002509 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002510 CheckNumberOfArguments(D, 2);
2511
2512 const std::string& OptName = InitPtrToString(D.getArg(0));
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002513 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002514 const Init* Value = D.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002515
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002516 if (OptDesc.isList()) {
2517 const ListInit& List = InitPtrToList(Value);
2518
2519 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2520 for (ListInit::const_iterator B = List.begin(), E = List.end();
2521 B != E; ++B) {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002522 const Init* CurElem = *B;
2523 if (OptDesc.isSwitchList())
2524 CheckBooleanConstant(CurElem);
2525
2526 O.indent(IndentLevel)
2527 << OptDesc.GenVariableName() << ".push_back(\""
2528 << (OptDesc.isSwitchList() ? CurElem->getAsString()
2529 : InitPtrToString(CurElem))
2530 << "\");\n";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002531 }
2532 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002533 else if (OptDesc.isSwitch()) {
2534 CheckBooleanConstant(Value);
2535 O.indent(IndentLevel) << OptDesc.GenVariableName()
2536 << " = " << Value->getAsString() << ";\n";
2537 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002538 else if (OptDesc.isParameter()) {
2539 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002540 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002541 << " = \"" << Str << "\";\n";
2542 }
2543 else {
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002544 throw "Can't apply 'set_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002545 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002546 }
2547
2548 void onSetSwitch(const Init* I,
2549 unsigned IndentLevel, raw_ostream& O) const {
2550 const std::string& OptName = InitPtrToString(I);
2551 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2552
2553 if (OptDesc.isSwitch())
2554 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2555 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002556 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002557 }
2558
2559 void onSetOption(const DagInit& d,
2560 unsigned IndentLevel, raw_ostream& O) const
2561 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002562 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002563
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002564 // 2-argument form: (set_option "A", true), (set_option "B", "C"),
2565 // (set_option "D", ["E", "F"])
2566 if (d.getNumArgs() == 2) {
2567 const OptionDescription& OptDesc =
2568 OptDescs_.FindOption(InitPtrToString(d.getArg(0)));
2569 const Init* Opt2 = d.getArg(1);
2570
2571 if (!OptDesc.isSwitch() || typeid(*Opt2) != typeid(StringInit)) {
2572 this->onSetOptionImpl(d, IndentLevel, O);
2573 return;
2574 }
2575 }
2576
2577 // Multiple argument form: (set_option "A"), (set_option "B", "C", "D")
2578 this->onEachArgument(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2579 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002580 }
2581
2582public:
2583
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002584 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002585 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002586 {
2587 if (!staticMembersInitialized_) {
2588 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2589 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2590 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002591 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002592
2593 staticMembersInitialized_ = true;
2594 }
2595 }
2596
2597 void operator()(const Init* I,
2598 unsigned IndentLevel, raw_ostream& O) const
2599 {
2600 InvokeDagInitHandler(this, I, IndentLevel, O);
2601 }
2602
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002603};
2604
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002605/// EmitPreprocessOptions - Emit the PreprocessOptions() function.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002606void EmitPreprocessOptions (const RecordKeeper& Records,
2607 const OptionDescriptions& OptDecs, raw_ostream& O)
2608{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002609 O << "int PreprocessOptions () {\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002610
2611 const RecordVector& OptionPreprocessors =
2612 Records.getAllDerivedDefinitions("OptionPreprocessor");
2613
2614 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2615 E = OptionPreprocessors.end(); B!=E; ++B) {
David Greened4a90662011-07-11 18:25:51 +00002616 const DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002617 EmitCaseConstructHandler(Case, Indent1,
2618 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002619 false, OptDecs, O);
2620 }
2621
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002622 O << '\n';
2623 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002624 O << "}\n\n";
2625}
2626
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002627class DoEmitPopulateLanguageMap;
2628typedef void (DoEmitPopulateLanguageMap::* DoEmitPopulateLanguageMapHandler)
2629(const DagInit& D);
2630
2631class DoEmitPopulateLanguageMap
2632: public HandlerTable<DoEmitPopulateLanguageMapHandler>
2633{
2634private:
2635 raw_ostream& O_;
2636
2637public:
2638
2639 explicit DoEmitPopulateLanguageMap (raw_ostream& O) : O_(O) {
2640 if (!staticMembersInitialized_) {
2641 AddHandler("lang_to_suffixes",
2642 &DoEmitPopulateLanguageMap::onLangToSuffixes);
2643
2644 staticMembersInitialized_ = true;
2645 }
2646 }
2647
David Greened4a90662011-07-11 18:25:51 +00002648 void operator() (const Init* I) {
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002649 InvokeDagInitHandler(this, I);
2650 }
2651
2652private:
2653
2654 void onLangToSuffixes (const DagInit& d) {
2655 CheckNumberOfArguments(d, 2);
2656
2657 const std::string& Lang = InitPtrToString(d.getArg(0));
David Greened4a90662011-07-11 18:25:51 +00002658 const Init* Suffixes = d.getArg(1);
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002659
2660 // Second argument to lang_to_suffixes is either a single string...
2661 if (typeid(*Suffixes) == typeid(StringInit)) {
2662 O_.indent(Indent1) << "langMap[\"" << InitPtrToString(Suffixes)
2663 << "\"] = \"" << Lang << "\";\n";
2664 }
2665 // ...or a list of strings.
2666 else {
2667 const ListInit& Lst = InitPtrToList(Suffixes);
2668 assert(Lst.size() != 0);
2669 for (ListInit::const_iterator B = Lst.begin(), E = Lst.end();
2670 B != E; ++B) {
2671 O_.indent(Indent1) << "langMap[\"" << InitPtrToString(*B)
2672 << "\"] = \"" << Lang << "\";\n";
2673 }
2674 }
2675 }
2676
2677};
2678
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002679/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002680void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002681{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002682 O << "int PopulateLanguageMap (LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002683
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002684 // For each LanguageMap:
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002685 const RecordVector& LangMaps =
Mikhail Glushenkov00a5b5b2010-08-23 19:24:16 +00002686 Records.getAllDerivedDefinitions("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002687
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002688 // Call DoEmitPopulateLanguageMap.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002689 for (RecordVector::const_iterator B = LangMaps.begin(),
2690 E = LangMaps.end(); B!=E; ++B) {
David Greened4a90662011-07-11 18:25:51 +00002691 const ListInit* LangMap = (*B)->getValueAsListInit("map");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002692 std::for_each(LangMap->begin(), LangMap->end(),
2693 DoEmitPopulateLanguageMap(O));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002694 }
2695
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002696 O << '\n';
2697 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002698 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002699}
2700
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002701/// EmitEdgePropertyHandlerCallback - Emits code that handles edge
2702/// properties. Helper function passed to EmitCaseConstructHandler() by
2703/// EmitEdgeClass().
2704void EmitEdgePropertyHandlerCallback (const Init* i, unsigned IndentLevel,
2705 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002706 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002707 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002708
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002709 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002710 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002711 }
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002712 else if (OpName == "error") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002713 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002714 O.indent(IndentLevel) << "PrintError(\""
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002715 << InitPtrToString(d.getArg(0))
2716 << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002717 O.indent(IndentLevel) << "return -1;\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002718 return;
2719 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002720 else {
2721 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002722 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002723 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002724
2725 if (d.getNumArgs() > 0)
2726 O << InitPtrToInt(d.getArg(0)) << ";\n";
2727 else
2728 O << "2;\n";
2729
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002730}
2731
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002732/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002733void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002734 const DagInit& Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002735 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002736
2737 // Class constructor.
2738 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002739 << "public:\n";
2740 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2741 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002742
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002743 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002744 O.indent(Indent1)
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002745 << "int Weight(const InputLanguagesSet& InLangs) const {\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002746 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002747
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002748 // Handle the 'case' construct.
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002749 EmitCaseConstructHandler(&Case, Indent2, EmitEdgePropertyHandlerCallback,
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002750 false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002751
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002752 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002753 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002754}
2755
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002756/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002757void EmitEdgeClasses (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002758 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002759 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002760 int i = 0;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002761 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002762 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002763 const DagInit& Edge = **B;
2764 const std::string& Name = GetOperatorName(Edge);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002765
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002766 if (Name == "optional_edge") {
2767 assert(IsOptionalEdge(Edge));
2768 const std::string& NodeB = InitPtrToString(Edge.getArg(1));
2769
2770 const DagInit& Weight = InitPtrToDag(Edge.getArg(2));
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002771 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
2772 }
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002773 else if (Name != "edge") {
2774 throw "Unknown edge class: '" + Name + "'!";
2775 }
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002776
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002777 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002778 }
2779}
2780
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002781/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph() function.
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002782void EmitPopulateCompilationGraph (const DagVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002783 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002784 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002785{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002786 O << "int PopulateCompilationGraph (CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002787
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002788 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2789 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002790 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002791
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002792 O << '\n';
2793
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002794 // Insert edges.
2795
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002796 int i = 0;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002797 for (DagVector::const_iterator B = EdgeVector.begin(),
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002798 E = EdgeVector.end(); B != E; ++B) {
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002799 const DagInit& Edge = **B;
2800 const std::string& NodeA = InitPtrToString(Edge.getArg(0));
2801 const std::string& NodeB = InitPtrToString(Edge.getArg(1));
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002802
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002803 O.indent(Indent1) << "if (int ret = G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002804
Mikhail Glushenkov8c67e4c2010-08-24 01:10:22 +00002805 if (IsOptionalEdge(Edge))
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002806 O << "new Edge" << i << "()";
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00002807 else
2808 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002809
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002810 O << "))\n";
2811 O.indent(Indent2) << "return ret;\n";
2812
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002813 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002814 }
2815
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002816 O << '\n';
2817 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002818 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002819}
2820
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002821/// HookInfo - Information about the hook type and number of arguments.
2822struct HookInfo {
2823
2824 // A hook can either have a single parameter of type std::vector<std::string>,
2825 // or NumArgs parameters of type const char*.
2826 enum HookType { ListHook, ArgHook };
2827
2828 HookType Type;
2829 unsigned NumArgs;
2830
2831 HookInfo() : Type(ArgHook), NumArgs(1)
2832 {}
2833
2834 HookInfo(HookType T) : Type(T), NumArgs(1)
2835 {}
2836
2837 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2838 {}
2839};
2840
2841typedef llvm::StringMap<HookInfo> HookInfoMap;
2842
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002843/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002844/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002845/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002846class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002847 HookInfoMap& HookNames_;
2848 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002849public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002850 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2851 : HookNames_(HookNames), OptDescs_(OptDescs)
2852 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002853
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002854 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002855 const std::string& Name = GetOperatorName(Dag);
2856
2857 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002858 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002859 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2860 const std::string& HookName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002861 const OptionDescription& D =
2862 OptDescs_.FindParameterListOrParameter(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002863
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002864 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2865 : HookInfo::ArgHook);
2866 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002867 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002868 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002869 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2870 }
2871 }
2872
2873 void onCmdLine(const std::string& Cmd) {
2874 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002875 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002876
2877 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2878 B != E; ++B) {
2879 const std::string& cmd = *B;
2880
2881 if (cmd == "$CALL") {
2882 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002883 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002884 const std::string& HookName = *B;
2885
2886 if (HookName.at(0) == ')')
2887 throw "$CALL invoked with no arguments!";
2888
2889 while (++B != E && B->at(0) != ')') {
2890 ++NumArgs;
2891 }
2892
2893 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2894
2895 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2896 H->second.Type != HookInfo::ArgHook)
2897 throw "Overloading of hooks is not allowed. Overloaded hook: "
2898 + HookName;
2899 else
2900 HookNames_[HookName] = HookInfo(NumArgs);
2901 }
2902 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002903 }
2904
2905 void operator()(const Init* Arg) {
2906
2907 // We're invoked on an action (either a dag or a dag list).
2908 if (typeid(*Arg) == typeid(DagInit)) {
2909 const DagInit& Dag = InitPtrToDag(Arg);
2910 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002911 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002912 }
2913 else if (typeid(*Arg) == typeid(ListInit)) {
2914 const ListInit& List = InitPtrToList(Arg);
2915 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2916 ++B) {
2917 const DagInit& Dag = InitPtrToDag(*B);
2918 this->onAction(Dag);
2919 }
2920 return;
2921 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002922
Mikhail Glushenkov17ef94f2010-10-23 07:32:46 +00002923 // We're invoked on a command line string.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002924 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002925 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002926
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002927 void operator()(const Init* Statement, unsigned) {
2928 this->operator()(Statement);
2929 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002930};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002931
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002932/// FillInHookNames - Actually extract the hook names from all command
2933/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002934void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002935 const OptionDescriptions& OptDescs,
2936 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002937{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002938 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002939 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2940 E = ToolDescs.end(); B != E; ++B) {
2941 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002942
2943 // Look for 'forward_transformed_value' in 'actions'.
2944 if (D.Actions)
2945 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2946
2947 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002948 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002949 continue;
David Greened4a90662011-07-11 18:25:51 +00002950 if (dynamic_cast<const StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002951 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002952 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002953 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002954 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002955 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002956 }
2957}
2958
2959/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2960/// property records and emit hook function declaration for each
2961/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002962void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2963 const OptionDescriptions& OptDescs, raw_ostream& O) {
2964 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002965
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002966 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002967 if (HookNames.empty())
2968 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002969
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002970 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002971 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002972 const char* HookName = B->first();
2973 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002974
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002975 O.indent(Indent1) << "std::string " << HookName << "(";
2976
2977 if (Info.Type == HookInfo::ArgHook) {
2978 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2979 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2980 }
2981 }
2982 else {
2983 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002984 }
2985
2986 O <<");\n";
2987 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002988}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002989
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002990/// EmitIncludes - Emit necessary #include directives and some
2991/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002992void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002993 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2994 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002995 << "#include \"llvm/CompilerDriver/Error.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002996 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002997
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002998 << "#include \"llvm/Support/CommandLine.h\"\n"
2999 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00003000
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003001 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00003002 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003003 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00003004 << "#include <stdexcept>\n\n"
3005
3006 << "using namespace llvm;\n"
3007 << "using namespace llvmc;\n\n"
3008
Mikhail Glushenkov67665722008-11-12 12:41:18 +00003009 << "inline const char* checkCString(const char* s)\n"
3010 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00003011}
3012
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003013
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003014/// DriverData - Holds all information about the driver.
3015struct DriverData {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003016 OptionDescriptions OptDescs;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003017 ToolDescriptions ToolDescs;
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003018 DagVector Edges;
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003019 bool HasSink;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003020};
3021
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003022/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003023/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003024bool HasSink(const ToolDescriptions& ToolDescs) {
3025 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
3026 E = ToolDescs.end(); B != E; ++B)
3027 if ((*B)->isSink())
3028 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003029
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003030 return false;
3031}
3032
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003033/// CollectDriverData - Collect compilation graph edges, tool properties and
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003034/// option properties from the parse tree.
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003035void CollectDriverData (const RecordKeeper& Records, DriverData& Data) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003036 // Collect option properties.
3037 const RecordVector& OptionLists =
3038 Records.getAllDerivedDefinitions("OptionList");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003039 CollectOptionDescriptions(OptionLists, Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003040
3041 // Collect tool properties.
3042 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003043 CollectToolDescriptions(Tools, Data.ToolDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003044 Data.HasSink = HasSink(Data.ToolDescs);
3045
3046 // Collect compilation graph edges.
3047 const RecordVector& CompilationGraphs =
3048 Records.getAllDerivedDefinitions("CompilationGraph");
Mikhail Glushenkovd9a73162010-08-23 23:21:23 +00003049 FillInEdgeVector(CompilationGraphs, Data.Edges);
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00003050}
3051
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003052/// CheckDriverData - Perform some sanity checks on the collected data.
3053void CheckDriverData(DriverData& Data) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003054 // Filter out all tools not mentioned in the compilation graph.
3055 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003056
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003057 // Typecheck the compilation graph.
Mikhail Glushenkov4e699cf2011-04-24 14:17:41 +00003058 // TODO: use a genuine graph representation instead of a vector and check for
3059 // multiple edges.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003060 TypecheckGraph(Data.Edges, Data.ToolDescs);
3061
3062 // Check that there are no options without side effects (specified
3063 // only in the OptionList).
3064 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003065}
3066
Chris Lattner67db8832010-12-13 00:23:57 +00003067void EmitDriverCode(const DriverData& Data,
3068 raw_ostream& O, RecordKeeper &Records) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003069 // Emit file header.
3070 EmitIncludes(O);
3071
3072 // Emit global option registration code.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003073 O << "namespace llvmc {\n"
3074 << "namespace autogenerated {\n\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003075 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003076 O << "} // End namespace autogenerated.\n"
3077 << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003078
3079 // Emit hook declarations.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003080 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003081 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003082 O << "} // End namespace hooks.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003083
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003084 O << "namespace {\n\n";
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003085 O << "using namespace llvmc::autogenerated;\n\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003086
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003087 // Emit Tool classes.
3088 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
3089 E = Data.ToolDescs.end(); B!=E; ++B)
3090 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
3091
3092 // Emit Edge# classes.
3093 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
3094
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003095 O << "} // End anonymous namespace.\n\n";
3096
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003097 O << "namespace llvmc {\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003098 O << "namespace autogenerated {\n\n";
3099
3100 // Emit PreprocessOptions() function.
3101 EmitPreprocessOptions(Records, Data.OptDescs, O);
3102
3103 // Emit PopulateLanguageMap() function
3104 // (language map maps from file extensions to language names).
3105 EmitPopulateLanguageMap(Records, O);
3106
3107 // Emit PopulateCompilationGraph() function.
3108 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
3109
3110 O << "} // End namespace autogenerated.\n";
3111 O << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003112
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003113 // EOF
3114}
3115
3116
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003117// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00003118}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003119
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003120/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00003121void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003122 try {
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003123 DriverData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003124
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003125 CollectDriverData(Records, Data);
3126 CheckDriverData(Data);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003127
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003128 this->EmitSourceFileHeader("llvmc-based driver: auto-generated code", O);
Chris Lattner67db8832010-12-13 00:23:57 +00003129 EmitDriverCode(Data, O, Records);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003130
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003131 } catch (std::exception& Error) {
3132 throw Error.what() + std::string(" - usually this means a syntax error.");
3133 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003134}