blob: 98952606c9e1348b9e61a7c8768d93a8502c54e5 [file] [log] [blame]
Mikhail Glushenkovfb37f392008-05-30 06:20:54 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000010// This tablegen backend is responsible for emitting LLVMC configuration code.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkovecbdcf22008-05-06 18:09:29 +000014#include "LLVMCConfigurationEmitter.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000015#include "Record.h"
16
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000018#include "llvm/ADT/StringMap.h"
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000019#include "llvm/ADT/StringSet.h"
Mikhail Glushenkove0b65702009-12-23 12:49:30 +000020
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000021#include <algorithm>
22#include <cassert>
23#include <functional>
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +000024#include <stdexcept>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000025#include <string>
Chris Lattner32a9e7a2008-06-04 04:46:14 +000026#include <typeinfo>
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +000027
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000028using namespace llvm;
29
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000030namespace {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
32//===----------------------------------------------------------------------===//
33/// Typedefs
34
35typedef std::vector<Record*> RecordVector;
36typedef std::vector<std::string> StrVector;
37
38//===----------------------------------------------------------------------===//
39/// Constants
40
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +000041// Indentation.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000042const unsigned TabWidth = 4;
43const unsigned Indent1 = TabWidth*1;
44const unsigned Indent2 = TabWidth*2;
45const unsigned Indent3 = TabWidth*3;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000046
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000047// Default help string.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000048const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000049
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000050// Name for the "sink" option.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000051const char * const SinkOptionName = "AutoGeneratedSinkOption";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000052
53//===----------------------------------------------------------------------===//
54/// Helper functions
55
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000056/// Id - An 'identity' function object.
57struct Id {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000058 template<typename T0>
59 void operator()(const T0&) const {
60 }
61 template<typename T0, typename T1>
62 void operator()(const T0&, const T1&) const {
63 }
64 template<typename T0, typename T1, typename T2>
65 void operator()(const T0&, const T1&, const T2&) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000066 }
67};
68
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000069int InitPtrToInt(const Init* ptr) {
70 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000071 return val.getValue();
72}
73
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000074const std::string& InitPtrToString(const Init* ptr) {
75 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
76 return val.getValue();
77}
78
79const ListInit& InitPtrToList(const Init* ptr) {
80 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
81 return val;
82}
83
84const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000085 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000086 return val;
87}
88
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000089const std::string GetOperatorName(const DagInit& D) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +000090 return D.getOperator()->getAsString();
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000091}
92
Mikhail Glushenkove0b65702009-12-23 12:49:30 +000093/// CheckBooleanConstant - Check that the provided value is a boolean constant.
94void CheckBooleanConstant(const Init* I) {
95 const DefInit& val = dynamic_cast<const DefInit&>(*I);
96 const std::string& str = val.getAsString();
97
98 if (str != "true" && str != "false") {
99 throw "Incorrect boolean value: '" + str +
100 "': must be either 'true' or 'false'";
101 }
102}
103
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000104// CheckNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +0000105// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000106void CheckNumberOfArguments (const DagInit& d, unsigned minArgs) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000107 if (d.getNumArgs() < minArgs)
108 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +0000109}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000110
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000111// IsDagEmpty - is this DAG marked with an empty marker?
112bool IsDagEmpty (const DagInit& d) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000113 return GetOperatorName(d) == "empty_dag_marker";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000114}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000115
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000116// EscapeVariableName - Escape commas and other symbols not allowed
117// in the C++ variable names. Makes it possible to use options named
118// like "Wa," (useful for prefix options).
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000119std::string EscapeVariableName (const std::string& Var) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000120 std::string ret;
121 for (unsigned i = 0; i != Var.size(); ++i) {
122 char cur_char = Var[i];
123 if (cur_char == ',') {
124 ret += "_comma_";
125 }
126 else if (cur_char == '+') {
127 ret += "_plus_";
128 }
129 else if (cur_char == '-') {
130 ret += "_dash_";
131 }
132 else {
133 ret.push_back(cur_char);
134 }
135 }
136 return ret;
137}
138
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000139/// EscapeQuotes - Replace '"' with '\"'.
140std::string EscapeQuotes (const std::string& Var) {
141 std::string ret;
142 for (unsigned i = 0; i != Var.size(); ++i) {
143 char cur_char = Var[i];
144 if (cur_char == '"') {
145 ret += "\\\"";
146 }
147 else {
148 ret.push_back(cur_char);
149 }
150 }
151 return ret;
152}
153
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000154/// OneOf - Does the input string contain this character?
155bool OneOf(const char* lst, char c) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000156 while (*lst) {
157 if (*lst++ == c)
158 return true;
159 }
160 return false;
161}
162
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000163template <class I, class S>
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000164void CheckedIncrement(I& P, I E, S ErrorString) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000165 ++P;
166 if (P == E)
167 throw ErrorString;
168}
169
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000170// apply is needed because C++'s syntax doesn't let us construct a function
171// object and call it in the same statement.
172template<typename F, typename T0>
173void apply(F Fun, T0& Arg0) {
174 return Fun(Arg0);
175}
176
177template<typename F, typename T0, typename T1>
178void apply(F Fun, T0& Arg0, T1& Arg1) {
179 return Fun(Arg0, Arg1);
180}
181
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000182//===----------------------------------------------------------------------===//
183/// Back-end specific code
184
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000185
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000186/// OptionType - One of six different option types. See the
187/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000188namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000189
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000190 enum OptionType { Alias, Switch, Parameter, ParameterList,
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000191 Prefix, PrefixList};
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000192
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000193 bool IsAlias(OptionType t) {
194 return (t == Alias);
195 }
196
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000197 bool IsList (OptionType t) {
198 return (t == ParameterList || t == PrefixList);
199 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000200
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000201 bool IsSwitch (OptionType t) {
202 return (t == Switch);
203 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000204
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000205 bool IsParameter (OptionType t) {
206 return (t == Parameter || t == Prefix);
207 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000208
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000209}
210
211OptionType::OptionType stringToOptionType(const std::string& T) {
212 if (T == "alias_option")
213 return OptionType::Alias;
214 else if (T == "switch_option")
215 return OptionType::Switch;
216 else if (T == "parameter_option")
217 return OptionType::Parameter;
218 else if (T == "parameter_list_option")
219 return OptionType::ParameterList;
220 else if (T == "prefix_option")
221 return OptionType::Prefix;
222 else if (T == "prefix_list_option")
223 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000224 else
225 throw "Unknown option type: " + T + '!';
226}
227
228namespace OptionDescriptionFlags {
229 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000230 ReallyHidden = 0x4, Extern = 0x8,
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000231 OneOrMore = 0x10, Optional = 0x20,
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000232 CommaSeparated = 0x40, ForwardNotSplit = 0x80 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000233}
234
235/// OptionDescription - Represents data contained in a single
236/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000237struct OptionDescription {
238 OptionType::OptionType Type;
239 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000240 unsigned Flags;
241 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000242 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000243 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000244
245 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000246 const std::string& n = "",
247 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000248 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000249 {}
250
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000251 /// GenTypeDeclaration - Returns the C++ variable type of this
252 /// option.
253 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000254
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000255 /// GenVariableName - Returns the variable name used in the
256 /// generated C++ code.
257 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000258
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000259 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000260 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000261
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000262 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000263
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000264 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000265
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000266 bool isMultiVal() const;
267
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000268 bool isCommaSeparated() const;
269 void setCommaSeparated();
270
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000271 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000272 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000273
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000274 bool isForwardNotSplit() const;
275 void setForwardNotSplit();
276
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000277 bool isRequired() const;
278 void setRequired();
279
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000280 bool isOneOrMore() const;
281 void setOneOrMore();
282
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000283 bool isOptional() const;
284 void setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000285
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000286 bool isHidden() const;
287 void setHidden();
288
289 bool isReallyHidden() const;
290 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000291
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000292 bool isSwitch() const
293 { return OptionType::IsSwitch(this->Type); }
294
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000295 bool isParameter() const
296 { return OptionType::IsParameter(this->Type); }
297
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000298 bool isList() const
299 { return OptionType::IsList(this->Type); }
300
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000301};
302
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000303void OptionDescription::Merge (const OptionDescription& other)
304{
305 if (other.Type != Type)
306 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000307
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000308 if (Help == other.Help || Help == DefaultHelpString)
309 Help = other.Help;
310 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000311 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000312 " defined for option " + Name + "\n";
313 }
314
315 Flags |= other.Flags;
316}
317
318bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000319 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000320}
321
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000322bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000323 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000324}
325
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000326bool OptionDescription::isCommaSeparated() const {
327 return Flags & OptionDescriptionFlags::CommaSeparated;
328}
329void OptionDescription::setCommaSeparated() {
330 Flags |= OptionDescriptionFlags::CommaSeparated;
331}
332
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000333bool OptionDescription::isForwardNotSplit() const {
334 return Flags & OptionDescriptionFlags::ForwardNotSplit;
335}
336void OptionDescription::setForwardNotSplit() {
337 Flags |= OptionDescriptionFlags::ForwardNotSplit;
338}
339
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000340bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000341 return Flags & OptionDescriptionFlags::Extern;
342}
343void OptionDescription::setExtern() {
344 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000345}
346
347bool OptionDescription::isRequired() const {
348 return Flags & OptionDescriptionFlags::Required;
349}
350void OptionDescription::setRequired() {
351 Flags |= OptionDescriptionFlags::Required;
352}
353
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000354bool OptionDescription::isOneOrMore() const {
355 return Flags & OptionDescriptionFlags::OneOrMore;
356}
357void OptionDescription::setOneOrMore() {
358 Flags |= OptionDescriptionFlags::OneOrMore;
359}
360
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000361bool OptionDescription::isOptional() const {
362 return Flags & OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000363}
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000364void OptionDescription::setOptional() {
365 Flags |= OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000366}
367
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000368bool OptionDescription::isHidden() const {
369 return Flags & OptionDescriptionFlags::Hidden;
370}
371void OptionDescription::setHidden() {
372 Flags |= OptionDescriptionFlags::Hidden;
373}
374
375bool OptionDescription::isReallyHidden() const {
376 return Flags & OptionDescriptionFlags::ReallyHidden;
377}
378void OptionDescription::setReallyHidden() {
379 Flags |= OptionDescriptionFlags::ReallyHidden;
380}
381
382const char* OptionDescription::GenTypeDeclaration() const {
383 switch (Type) {
384 case OptionType::Alias:
385 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000386 case OptionType::PrefixList:
387 case OptionType::ParameterList:
388 return "cl::list<std::string>";
389 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000390 return "cl::opt<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000391 case OptionType::Parameter:
392 case OptionType::Prefix:
393 default:
394 return "cl::opt<std::string>";
395 }
396}
397
398std::string OptionDescription::GenVariableName() const {
399 const std::string& EscapedName = EscapeVariableName(Name);
400 switch (Type) {
401 case OptionType::Alias:
402 return "AutoGeneratedAlias_" + EscapedName;
403 case OptionType::PrefixList:
404 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000405 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000406 case OptionType::Switch:
407 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000408 case OptionType::Prefix:
409 case OptionType::Parameter:
410 default:
411 return "AutoGeneratedParameter_" + EscapedName;
412 }
413}
414
415/// OptionDescriptions - An OptionDescription array plus some helper
416/// functions.
417class OptionDescriptions {
418 typedef StringMap<OptionDescription> container_type;
419
420 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000421 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000422
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000423public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000424 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000425 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000426
427 // Wrappers for FindOption that throw an exception in case the option has a
428 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000429 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000430 const OptionDescription& FindParameter(const std::string& OptName) const;
431 const OptionDescription& FindList(const std::string& OptName) const;
432 const OptionDescription&
433 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000434
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000435 /// insertDescription - Insert new OptionDescription into
436 /// OptionDescriptions list
437 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000438
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000439 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000440 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000441 const_iterator begin() const { return Descriptions.begin(); }
442 const_iterator end() const { return Descriptions.end(); }
443};
444
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000445const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000446OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000447 const_iterator I = Descriptions.find(OptName);
448 if (I != Descriptions.end())
449 return I->second;
450 else
451 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000452}
453
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000454const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000455OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000456 const OptionDescription& OptDesc = this->FindOption(OptName);
457 if (!OptDesc.isSwitch())
458 throw OptName + ": incorrect option type - should be a switch!";
459 return OptDesc;
460}
461
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000462const OptionDescription&
463OptionDescriptions::FindList(const std::string& OptName) const {
464 const OptionDescription& OptDesc = this->FindOption(OptName);
465 if (!OptDesc.isList())
466 throw OptName + ": incorrect option type - should be a list!";
467 return OptDesc;
468}
469
470const OptionDescription&
471OptionDescriptions::FindParameter(const std::string& OptName) const {
472 const OptionDescription& OptDesc = this->FindOption(OptName);
473 if (!OptDesc.isParameter())
474 throw OptName + ": incorrect option type - should be a parameter!";
475 return OptDesc;
476}
477
478const OptionDescription&
479OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
480 const OptionDescription& OptDesc = this->FindOption(OptName);
481 if (!OptDesc.isList() && !OptDesc.isParameter())
482 throw OptName
483 + ": incorrect option type - should be a list or parameter!";
484 return OptDesc;
485}
486
487void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000488 container_type::iterator I = Descriptions.find(o.Name);
489 if (I != Descriptions.end()) {
490 OptionDescription& D = I->second;
491 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000492 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000493 else {
494 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000495 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000496}
497
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000498/// HandlerTable - A base class for function objects implemented as
499/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000500template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000501class HandlerTable {
502protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000503 // Implementation details.
504
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000505 /// HandlerMap - A map from property names to property handlers
506 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000507
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000508 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000509 static bool staticMembersInitialized_;
510
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000511public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000512
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000513 Handler GetHandler (const std::string& HandlerName) const {
514 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000515
516 if (method != Handlers_.end()) {
517 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000518 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000519 }
520 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000521 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000522 }
523 }
524
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000525 void AddHandler(const char* Property, Handler H) {
526 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000527 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000528
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000529};
530
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000531template <class Handler, class FunctionObject>
532Handler GetHandler(FunctionObject* Obj, const DagInit& Dag) {
533 const std::string& HandlerName = GetOperatorName(Dag);
534 return Obj->GetHandler(HandlerName);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000535}
536
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000537template <class FunctionObject>
538void InvokeDagInitHandler(FunctionObject* Obj, Init* I) {
539 typedef void (FunctionObject::*Handler) (const DagInit&);
540
541 const DagInit& Dag = InitPtrToDag(I);
542 Handler h = GetHandler<Handler>(Obj, Dag);
543
544 ((Obj)->*(h))(Dag);
545}
546
547template <class FunctionObject>
548void InvokeDagInitHandler(const FunctionObject* const Obj,
549 const Init* I, unsigned IndentLevel, raw_ostream& O)
550{
551 typedef void (FunctionObject::*Handler)
552 (const DagInit&, unsigned IndentLevel, raw_ostream& O) const;
553
554 const DagInit& Dag = InitPtrToDag(I);
555 Handler h = GetHandler<Handler>(Obj, Dag);
556
557 ((Obj)->*(h))(Dag, IndentLevel, O);
558}
559
560
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000561template <typename H>
562typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
563
564template <typename H>
565bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000566
567
568/// CollectOptionProperties - Function object for iterating over an
569/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000570class CollectOptionProperties;
571typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000572(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000573
574class CollectOptionProperties
575: public HandlerTable<CollectOptionPropertiesHandler>
576{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000577private:
578
579 /// optDescs_ - OptionDescriptions table. This is where the
580 /// information is stored.
581 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000582
583public:
584
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000585 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000586 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000587 {
588 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000589 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000590 AddHandler("help", &CollectOptionProperties::onHelp);
591 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000592 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000593 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
594 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000595 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
596 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000597 AddHandler("optional", &CollectOptionProperties::onOptional);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000598 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000599 AddHandler("forward_not_split",
600 &CollectOptionProperties::onForwardNotSplit);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000601
602 staticMembersInitialized_ = true;
603 }
604 }
605
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000606 /// operator() - Just forwards to the corresponding property
607 /// handler.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000608 void operator() (Init* I) {
609 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000610 }
611
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000612private:
613
614 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000615 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000616
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000617 void onExtern (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000618 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000619 optDesc_.setExtern();
620 }
621
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000622 void onHelp (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000623 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000624 optDesc_.Help = EscapeQuotes(InitPtrToString(d.getArg(0)));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000625 }
626
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000627 void onHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000628 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000629 optDesc_.setHidden();
630 }
631
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000632 void onReallyHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000633 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000634 optDesc_.setReallyHidden();
635 }
636
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000637 void onCommaSeparated (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000638 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000639 if (!optDesc_.isList())
640 throw "'comma_separated' is valid only on list options!";
641 optDesc_.setCommaSeparated();
642 }
643
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000644 void onForwardNotSplit (const DagInit& d) {
645 CheckNumberOfArguments(d, 0);
646 if (!optDesc_.isParameter())
647 throw "'forward_not_split' is valid only for parameter options!";
648 optDesc_.setForwardNotSplit();
649 }
650
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000651 void onRequired (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000652 CheckNumberOfArguments(d, 0);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000653 if (optDesc_.isOneOrMore() || optDesc_.isOptional())
654 throw "Only one of (required), (optional) or "
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000655 "(one_or_more) properties is allowed!";
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000656 optDesc_.setRequired();
657 }
658
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000659 void onInit (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000660 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000661 Init* i = d.getArg(0);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000662 const std::string& str = i->getAsString();
663
664 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
665 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
666
667 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000668 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000669
670 optDesc_.InitVal = i;
671 }
672
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000673 void onOneOrMore (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000674 CheckNumberOfArguments(d, 0);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000675 if (optDesc_.isRequired() || optDesc_.isOptional())
676 throw "Only one of (required), (optional) or "
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000677 "(one_or_more) properties is allowed!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000678 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000679 llvm::errs() << "Warning: specifying the 'one_or_more' property "
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000680 "on a non-list option will have no effect.\n";
681 optDesc_.setOneOrMore();
682 }
683
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000684 void onOptional (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000685 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000686 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000687 throw "Only one of (required), (optional) or "
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000688 "(one_or_more) properties is allowed!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000689 if (!OptionType::IsList(optDesc_.Type))
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000690 llvm::errs() << "Warning: specifying the 'optional' property"
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000691 "on a non-list option will have no effect.\n";
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000692 optDesc_.setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000693 }
694
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000695 void onMultiVal (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000696 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000697 int val = InitPtrToInt(d.getArg(0));
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000698 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000699 throw "Error in the 'multi_val' property: "
700 "the value must be greater than 1!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000701 if (!OptionType::IsList(optDesc_.Type))
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000702 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000703 optDesc_.MultiVal = val;
704 }
705
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000706};
707
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000708/// AddOption - A function object that is applied to every option
709/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000710class AddOption {
711private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000712 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000713
714public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000715 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000716 {}
717
718 void operator()(const Init* i) {
719 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000720 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000721
722 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000723 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000724 const std::string& Name = InitPtrToString(d.getArg(0));
725
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000726 OptionDescription OD(Type, Name);
727
728 if (!OD.isExtern())
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000729 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000730
731 if (OD.isAlias()) {
732 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000733 OD.Help = InitPtrToString(d.getArg(1));
734 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000735 else if (!OD.isExtern()) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000736 processOptionProperties(d, OD);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000737 }
738 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000739 }
740
741private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000742 /// processOptionProperties - Go through the list of option
743 /// properties and call a corresponding handler for each.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000744 static void processOptionProperties (const DagInit& d, OptionDescription& o) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000745 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000746 DagInit::const_arg_iterator B = d.arg_begin();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000747 // Skip the first argument: it's always the option name.
748 ++B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000749 std::for_each(B, d.arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000750 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000751
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000752};
753
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000754/// CollectOptionDescriptions - Collects option properties from all
755/// OptionLists.
756void CollectOptionDescriptions (RecordVector::const_iterator B,
757 RecordVector::const_iterator E,
758 OptionDescriptions& OptDescs)
759{
760 // For every OptionList:
761 for (; B!=E; ++B) {
762 RecordVector::value_type T = *B;
763 // Throws an exception if the value does not exist.
764 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000765
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000766 // For every option description in this list:
767 // collect the information and
768 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
769 }
770}
771
772// Tool information record
773
774namespace ToolFlags {
775 enum ToolFlags { Join = 0x1, Sink = 0x2 };
776}
777
778struct ToolDescription : public RefCountedBase<ToolDescription> {
779 std::string Name;
780 Init* CmdLine;
781 Init* Actions;
782 StrVector InLanguage;
783 std::string OutLanguage;
784 std::string OutputSuffix;
785 unsigned Flags;
786
787 // Various boolean properties
788 void setSink() { Flags |= ToolFlags::Sink; }
789 bool isSink() const { return Flags & ToolFlags::Sink; }
790 void setJoin() { Flags |= ToolFlags::Join; }
791 bool isJoin() const { return Flags & ToolFlags::Join; }
792
793 // Default ctor here is needed because StringMap can only store
794 // DefaultConstructible objects
795 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
796 ToolDescription (const std::string& n)
797 : Name(n), CmdLine(0), Actions(0), Flags(0)
798 {}
799};
800
801/// ToolDescriptions - A list of Tool information records.
802typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
803
804
805/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000806/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000807
808class CollectToolProperties;
809typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000810(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000811
812class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
813{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000814private:
815
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000816 /// toolDesc_ - Properties of the current Tool. This is where the
817 /// information is stored.
818 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000819
820public:
821
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000822 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000823 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000824 {
825 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000826
827 AddHandler("actions", &CollectToolProperties::onActions);
828 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
829 AddHandler("in_language", &CollectToolProperties::onInLanguage);
830 AddHandler("join", &CollectToolProperties::onJoin);
831 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
832 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
833 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000834
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000835 staticMembersInitialized_ = true;
836 }
837 }
838
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000839 void operator() (Init* I) {
840 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000841 }
842
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000843private:
844
845 /// Property handlers --
846 /// Functions that extract information about tool properties from
847 /// DAG representation.
848
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000849 void onActions (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000850 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000851 Init* Case = d.getArg(0);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000852 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000853 GetOperatorName(static_cast<DagInit&>(*Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000854 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000855 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000856 }
857
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000858 void onCmdLine (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000859 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000860 toolDesc_.CmdLine = d.getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000861 }
862
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000863 void onInLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000864 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000865 Init* arg = d.getArg(0);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000866
867 // Find out the argument's type.
868 if (typeid(*arg) == typeid(StringInit)) {
869 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000870 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000871 }
872 else {
873 // It's a list.
874 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000875 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000876
877 // Copy strings to the output vector.
878 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
879 B != E; ++B) {
880 out.push_back(InitPtrToString(*B));
881 }
882
883 // Remove duplicates.
884 std::sort(out.begin(), out.end());
885 StrVector::iterator newE = std::unique(out.begin(), out.end());
886 out.erase(newE, out.end());
887 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000888 }
889
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000890 void onJoin (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000891 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000892 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000893 }
894
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000895 void onOutLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000896 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000897 toolDesc_.OutLanguage = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000898 }
899
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000900 void onOutputSuffix (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000901 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000902 toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000903 }
904
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000905 void onSink (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000906 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000907 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000908 }
909
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000910};
911
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000912/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000913/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000914/// CollectToolProperties function object).
915void CollectToolDescriptions (RecordVector::const_iterator B,
916 RecordVector::const_iterator E,
917 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000918{
919 // Iterate over a properties list of every Tool definition
920 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000921 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000922 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000923 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000924
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000925 IntrusiveRefCntPtr<ToolDescription>
926 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000927
928 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000929 CollectToolProperties(*ToolDesc));
930 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000931 }
932}
933
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000934/// FillInEdgeVector - Merge all compilation graph definitions into
935/// one single edge list.
936void FillInEdgeVector(RecordVector::const_iterator B,
937 RecordVector::const_iterator E, RecordVector& Out) {
938 for (; B != E; ++B) {
939 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000940
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000941 for (unsigned i = 0; i < edges->size(); ++i)
942 Out.push_back(edges->getElementAsRecord(i));
943 }
944}
945
946/// CalculatePriority - Calculate the priority of this plugin.
947int CalculatePriority(RecordVector::const_iterator B,
948 RecordVector::const_iterator E) {
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000949 int priority = 0;
950
951 if (B != E) {
952 priority = static_cast<int>((*B)->getValueAsInt("priority"));
953
954 if (++B != E)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000955 throw "More than one 'PluginPriority' instance found: "
956 "most probably an error!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000957 }
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000958
959 return priority;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000960}
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000961
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000962/// NotInGraph - Helper function object for FilterNotInGraph.
963struct NotInGraph {
964private:
965 const llvm::StringSet<>& ToolsInGraph_;
966
967public:
968 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
969 : ToolsInGraph_(ToolsInGraph)
970 {}
971
972 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
973 return (ToolsInGraph_.count(x->Name) == 0);
974 }
975};
976
977/// FilterNotInGraph - Filter out from ToolDescs all Tools not
978/// mentioned in the compilation graph definition.
979void FilterNotInGraph (const RecordVector& EdgeVector,
980 ToolDescriptions& ToolDescs) {
981
982 // List all tools mentioned in the graph.
983 llvm::StringSet<> ToolsInGraph;
984
985 for (RecordVector::const_iterator B = EdgeVector.begin(),
986 E = EdgeVector.end(); B != E; ++B) {
987
988 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000989 const std::string& NodeA = Edge->getValueAsString("a");
990 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000991
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000992 if (NodeA != "root")
993 ToolsInGraph.insert(NodeA);
994 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000995 }
996
997 // Filter ToolPropertiesList.
998 ToolDescriptions::iterator new_end =
999 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
1000 NotInGraph(ToolsInGraph));
1001 ToolDescs.erase(new_end, ToolDescs.end());
1002}
1003
1004/// FillInToolToLang - Fills in two tables that map tool names to
1005/// (input, output) languages. Helper function used by TypecheckGraph().
1006void FillInToolToLang (const ToolDescriptions& ToolDescs,
1007 StringMap<StringSet<> >& ToolToInLang,
1008 StringMap<std::string>& ToolToOutLang) {
1009 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1010 E = ToolDescs.end(); B != E; ++B) {
1011 const ToolDescription& D = *(*B);
1012 for (StrVector::const_iterator B = D.InLanguage.begin(),
1013 E = D.InLanguage.end(); B != E; ++B)
1014 ToolToInLang[D.Name].insert(*B);
1015 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001016 }
1017}
1018
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001019/// TypecheckGraph - Check that names for output and input languages
1020/// on all edges do match. This doesn't do much when the information
1021/// about the whole graph is not available (i.e. when compiling most
1022/// plugins).
1023void TypecheckGraph (const RecordVector& EdgeVector,
1024 const ToolDescriptions& ToolDescs) {
1025 StringMap<StringSet<> > ToolToInLang;
1026 StringMap<std::string> ToolToOutLang;
1027
1028 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
1029 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
1030 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
1031
1032 for (RecordVector::const_iterator B = EdgeVector.begin(),
1033 E = EdgeVector.end(); B != E; ++B) {
1034 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001035 const std::string& NodeA = Edge->getValueAsString("a");
1036 const std::string& NodeB = Edge->getValueAsString("b");
1037 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
1038 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001039
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001040 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001041 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001042 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001043 + ": output->input language mismatch";
1044 }
1045
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001046 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001047 throw "Edges back to the root are not allowed!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001048 }
1049}
1050
1051/// WalkCase - Walks the 'case' expression DAG and invokes
1052/// TestCallback on every test, and StatementCallback on every
1053/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001054/// combinators (that is, they are passed directly to TestCallback).
1055/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1056/// IndentLevel, bool FirstTest)'.
1057/// StatementCallback must have type 'void StatementCallback(const Init*,
1058/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001059template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001060void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1061 unsigned IndentLevel = 0)
1062{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001063 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001064
1065 // Error checks.
1066 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001067 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001068
1069 if (d.getNumArgs() < 2)
1070 throw "There should be at least one clause in the 'case' expression:\n"
1071 + d.getAsString();
1072
1073 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001074 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001075 const unsigned numArgs = d.getNumArgs();
1076 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001077 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1078 B != E; ++B) {
1079 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001080
1081 if (!even)
1082 {
1083 // Handle test.
1084 const DagInit& Test = InitPtrToDag(arg);
1085
1086 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001087 throw "The 'default' clause should be the last in the "
1088 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001089 if (i == numArgs)
1090 throw "Case construct handler: no corresponding action "
1091 "found for the test " + Test.getAsString() + '!';
1092
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001093 TestCallback(Test, IndentLevel, (i == 1));
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001094 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001095 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001096 {
1097 if (dynamic_cast<DagInit*>(arg)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001098 && GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001099 // Nested 'case'.
1100 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1101 }
1102
1103 // Handle statement.
1104 StatementCallback(arg, IndentLevel);
1105 }
1106
1107 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001108 even = !even;
1109 }
1110}
1111
1112/// ExtractOptionNames - A helper function object used by
1113/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1114class ExtractOptionNames {
1115 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001116
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001117 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001118 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001119 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001120 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001121 ActionName == "forward_value" ||
1122 ActionName == "forward_transformed_value" ||
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001123 ActionName == "switch_on" || ActionName == "any_switch_on" ||
1124 ActionName == "parameter_equals" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001125 ActionName == "element_in_list" || ActionName == "not_empty" ||
1126 ActionName == "empty") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001127 CheckNumberOfArguments(Stmt, 1);
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001128
1129 Init* Arg = Stmt.getArg(0);
1130 if (typeid(*Arg) == typeid(StringInit)) {
1131 const std::string& Name = InitPtrToString(Arg);
1132 OptionNames_.insert(Name);
1133 }
1134 else {
1135 // It's a list.
1136 const ListInit& List = InitPtrToList(Arg);
1137 for (ListInit::const_iterator B = List.begin(), E = List.end();
1138 B != E; ++B) {
1139 const std::string& Name = InitPtrToString(*B);
1140 OptionNames_.insert(Name);
1141 }
1142 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001143 }
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001144 else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001145 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001146 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001147 }
1148 }
1149 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001150
1151public:
1152 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1153 {}
1154
1155 void operator()(const Init* Statement) {
1156 if (typeid(*Statement) == typeid(ListInit)) {
1157 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1158 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1159 B != E; ++B)
1160 this->processDag(*B);
1161 }
1162 else {
1163 this->processDag(Statement);
1164 }
1165 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001166
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001167 void operator()(const DagInit& Test, unsigned, bool) {
1168 this->operator()(&Test);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001169 }
1170 void operator()(const Init* Statement, unsigned) {
1171 this->operator()(Statement);
1172 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001173};
1174
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001175/// CheckForSuperfluousOptions - Check that there are no side
1176/// effect-free options (specified only in the OptionList). Otherwise,
1177/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001178void CheckForSuperfluousOptions (const RecordVector& Edges,
1179 const ToolDescriptions& ToolDescs,
1180 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001181 llvm::StringSet<> nonSuperfluousOptions;
1182
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001183 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001184 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001185 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1186 E = ToolDescs.end(); B != E; ++B) {
1187 const ToolDescription& TD = *(*B);
1188 ExtractOptionNames Callback(nonSuperfluousOptions);
1189 if (TD.Actions)
1190 WalkCase(TD.Actions, Callback, Callback);
1191 }
1192
1193 // Add all options mentioned in the 'case' clauses of the
1194 // OptionalEdges of the compilation graph to the set of
1195 // non-superfluous options.
1196 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1197 B != E; ++B) {
1198 const Record* Edge = *B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001199 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001200
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001201 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001202 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001203 }
1204
1205 // Check that all options in OptDescs belong to the set of
1206 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001207 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001208 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001209 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001210 if (!nonSuperfluousOptions.count(Val.Name)
1211 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001212 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001213 "Probable cause: this option is specified only in the OptionList.\n";
1214 }
1215}
1216
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001217/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1218bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1219 if (TestName == "single_input_file") {
1220 O << "InputFilenames.size() == 1";
1221 return true;
1222 }
1223 else if (TestName == "multiple_input_files") {
1224 O << "InputFilenames.size() > 1";
1225 return true;
1226 }
1227
1228 return false;
1229}
1230
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001231/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1232template <typename F>
1233void EmitListTest(const ListInit& L, const char* LogicOp,
1234 F Callback, raw_ostream& O)
1235{
1236 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1237 // of Dags...
1238 bool isFirst = true;
1239 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1240 if (isFirst)
1241 isFirst = false;
1242 else
Mikhail Glushenkovb7935e02010-01-01 04:40:54 +00001243 O << ' ' << LogicOp << ' ';
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001244 Callback(InitPtrToString(*B), O);
1245 }
1246}
1247
1248// Callbacks for use with EmitListTest.
1249
1250class EmitSwitchOn {
1251 const OptionDescriptions& OptDescs_;
1252public:
1253 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1254 {}
1255
1256 void operator()(const std::string& OptName, raw_ostream& O) const {
1257 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1258 O << OptDesc.GenVariableName();
1259 }
1260};
1261
1262class EmitEmptyTest {
1263 bool EmitNegate_;
1264 const OptionDescriptions& OptDescs_;
1265public:
1266 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1267 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1268 {}
1269
1270 void operator()(const std::string& OptName, raw_ostream& O) const {
1271 const char* Neg = (EmitNegate_ ? "!" : "");
1272 if (OptName == "o") {
1273 O << Neg << "OutputFilename.empty()";
1274 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001275 else if (OptName == "save-temps") {
1276 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1277 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001278 else {
1279 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1280 O << Neg << OptDesc.GenVariableName() << ".empty()";
1281 }
1282 }
1283};
1284
1285
1286/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1287bool EmitCaseTest1ArgList(const std::string& TestName,
1288 const DagInit& d,
1289 const OptionDescriptions& OptDescs,
1290 raw_ostream& O) {
Mikhail Glushenkov3a481e32010-01-01 03:50:51 +00001291 const ListInit& L = InitPtrToList(d.getArg(0));
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001292
1293 if (TestName == "any_switch_on") {
1294 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1295 return true;
1296 }
1297 else if (TestName == "switch_on") {
1298 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1299 return true;
1300 }
1301 else if (TestName == "any_not_empty") {
1302 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1303 return true;
1304 }
1305 else if (TestName == "any_empty") {
1306 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1307 return true;
1308 }
1309 else if (TestName == "not_empty") {
1310 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1311 return true;
1312 }
1313 else if (TestName == "empty") {
1314 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1315 return true;
1316 }
1317
1318 return false;
1319}
1320
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001321/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1322bool EmitCaseTest1ArgStr(const std::string& TestName,
1323 const DagInit& d,
1324 const OptionDescriptions& OptDescs,
1325 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001326 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001327
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001328 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001329 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001330 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001331 }
1332 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001333 O << "InLangs.count(\"" << OptName << "\") != 0";
1334 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001335 }
1336 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001337 // This works only for single-argument Tool::GenerateAction. Join
1338 // tools can process several files in different languages simultaneously.
1339
1340 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001341 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001342 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001343 }
1344 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001345 bool EmitNegate = (TestName == "not_empty");
1346 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001347 return true;
1348 }
1349
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001350 return false;
1351}
1352
1353/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1354bool EmitCaseTest1Arg(const std::string& TestName,
1355 const DagInit& d,
1356 const OptionDescriptions& OptDescs,
1357 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001358 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001359 if (typeid(*d.getArg(0)) == typeid(ListInit))
1360 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1361 else
1362 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1363}
1364
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001365/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001366bool EmitCaseTest2Args(const std::string& TestName,
1367 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001368 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001369 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001370 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001371 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001372 const std::string& OptName = InitPtrToString(d.getArg(0));
1373 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001374
1375 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001376 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001377 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1378 return true;
1379 }
1380 else if (TestName == "element_in_list") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001381 const OptionDescription& OptDesc = OptDescs.FindList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001382 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001383 O << "std::find(" << VarName << ".begin(),\n";
1384 O.indent(IndentLevel + Indent1)
1385 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001386 << OptArg << "\") != " << VarName << ".end()";
1387 return true;
1388 }
1389
1390 return false;
1391}
1392
1393// Forward declaration.
1394// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001395void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001396 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001397 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001398
1399/// EmitLogicalOperationTest - Helper function used by
1400/// EmitCaseConstructHandler.
1401void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001402 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001403 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001404 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001405 O << '(';
1406 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001407 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001408 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001409 if (j != NumArgs - 1) {
1410 O << ")\n";
1411 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1412 }
1413 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001414 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001415 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001416 }
1417}
1418
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001419void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001420 const OptionDescriptions& OptDescs, raw_ostream& O)
1421{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001422 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001423 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1424 O << "! (";
1425 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1426 O << ")";
1427}
1428
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001429/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001430void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001431 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001432 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001433 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001434
1435 if (TestName == "and")
1436 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1437 else if (TestName == "or")
1438 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001439 else if (TestName == "not")
1440 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001441 else if (EmitCaseTest0Args(TestName, O))
1442 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001443 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1444 return;
1445 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1446 return;
1447 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001448 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001449}
1450
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001451
1452/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1453class EmitCaseTestCallback {
1454 bool EmitElseIf_;
1455 const OptionDescriptions& OptDescs_;
1456 raw_ostream& O_;
1457public:
1458
1459 EmitCaseTestCallback(bool EmitElseIf,
1460 const OptionDescriptions& OptDescs, raw_ostream& O)
1461 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1462 {}
1463
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001464 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001465 {
1466 if (GetOperatorName(Test) == "default") {
1467 O_.indent(IndentLevel) << "else {\n";
1468 }
1469 else {
1470 O_.indent(IndentLevel)
1471 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001472 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001473 O_ << ") {\n";
1474 }
1475 }
1476};
1477
1478/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1479template <typename F>
1480class EmitCaseStatementCallback {
1481 F Callback_;
1482 raw_ostream& O_;
1483public:
1484
1485 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1486 : Callback_(Callback), O_(O)
1487 {}
1488
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001489 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001490
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001491 // Ignore nested 'case' DAG.
1492 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001493 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case")) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001494 if (typeid(*Statement) == typeid(ListInit)) {
1495 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1496 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1497 B != E; ++B)
1498 Callback_(*B, (IndentLevel + Indent1), O_);
1499 }
1500 else {
1501 Callback_(Statement, (IndentLevel + Indent1), O_);
1502 }
1503 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001504 O_.indent(IndentLevel) << "}\n";
1505 }
1506
1507};
1508
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001509/// EmitCaseConstructHandler - Emit code that handles the 'case'
1510/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001511/// clause. Implemented on top of WalkCase.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001512/// Callback's type is void F(Init* Statement, unsigned IndentLevel,
1513/// raw_ostream& O).
1514/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001515/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1516/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001517template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001518void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001519 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001520 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001521 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001522 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1523 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001524}
1525
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001526/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001527/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1528/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001529void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001530 const char* Delimiters = " \t\n\v\f\r";
1531 enum TokenizerState
1532 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1533 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001534
1535 if (CmdLine.empty())
1536 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001537 Out.push_back("");
1538
1539 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1540 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001541
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001542 for (; B != E; ++B) {
1543 char cur_ch = CmdLine[B];
1544
1545 switch (cur_st) {
1546 case Normal:
1547 if (cur_ch == '$') {
1548 cur_st = SpecialCommand;
1549 break;
1550 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001551 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001552 // Skip whitespace
1553 B = CmdLine.find_first_not_of(Delimiters, B);
1554 if (B == std::string::npos) {
1555 B = E-1;
1556 continue;
1557 }
1558 --B;
1559 Out.push_back("");
1560 continue;
1561 }
1562 break;
1563
1564
1565 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001566 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001567 cur_st = Normal;
1568 Out.push_back("");
1569 continue;
1570 }
1571 if (cur_ch == '(') {
1572 Out.push_back("");
1573 cur_st = InsideSpecialCommand;
1574 continue;
1575 }
1576 break;
1577
1578 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001579 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001580 continue;
1581 }
1582 if (cur_ch == '\'') {
1583 cur_st = InsideQuotationMarks;
1584 Out.push_back("");
1585 continue;
1586 }
1587 if (cur_ch == ')') {
1588 cur_st = Normal;
1589 Out.push_back("");
1590 }
1591 if (cur_ch == ',') {
1592 continue;
1593 }
1594
1595 break;
1596
1597 case InsideQuotationMarks:
1598 if (cur_ch == '\'') {
1599 cur_st = InsideSpecialCommand;
1600 continue;
1601 }
1602 break;
1603 }
1604
1605 Out.back().push_back(cur_ch);
1606 }
1607}
1608
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001609/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1610/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1611/// SubstituteSpecialCommands().
1612StrVector::const_iterator
1613SubstituteCall (StrVector::const_iterator Pos,
1614 StrVector::const_iterator End,
1615 bool IsJoin, raw_ostream& O)
1616{
1617 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001618 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001619 const std::string& CmdName = *Pos;
1620
1621 if (CmdName == ")")
1622 throw "$CALL invocation: empty argument list!";
1623
1624 O << "hooks::";
1625 O << CmdName << "(";
1626
1627
1628 bool firstIteration = true;
1629 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001630 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001631 const std::string& Arg = *Pos;
1632 assert(Arg.size() != 0);
1633
1634 if (Arg[0] == ')')
1635 break;
1636
1637 if (firstIteration)
1638 firstIteration = false;
1639 else
1640 O << ", ";
1641
1642 if (Arg == "$INFILE") {
1643 if (IsJoin)
1644 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1645 else
1646 O << "inFile.c_str()";
1647 }
1648 else {
1649 O << '"' << Arg << '"';
1650 }
1651 }
1652
1653 O << ')';
1654
1655 return Pos;
1656}
1657
1658/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1659/// function used by SubstituteSpecialCommands().
1660StrVector::const_iterator
1661SubstituteEnv (StrVector::const_iterator Pos,
1662 StrVector::const_iterator End, raw_ostream& O)
1663{
1664 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001665 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001666 const std::string& EnvName = *Pos;
1667
1668 if (EnvName == ")")
1669 throw "$ENV invocation: empty argument list!";
1670
1671 O << "checkCString(std::getenv(\"";
1672 O << EnvName;
1673 O << "\"))";
1674
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001675 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001676
1677 return Pos;
1678}
1679
1680/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1681/// handler code. Helper function used by EmitCmdLineVecFill().
1682StrVector::const_iterator
1683SubstituteSpecialCommands (StrVector::const_iterator Pos,
1684 StrVector::const_iterator End,
1685 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001686{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001687
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001688 const std::string& cmd = *Pos;
1689
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001690 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001691 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001692 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001693 }
1694 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001695 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001696 }
1697 else {
1698 throw "Unknown special command: " + cmd;
1699 }
1700
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001701 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001702 const std::string& Leftover = *Pos;
1703 assert(Leftover.at(0) == ')');
1704 if (Leftover.size() != 1)
1705 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001706
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001707 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001708}
1709
1710/// EmitCmdLineVecFill - Emit code that fills in the command line
1711/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001712void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001713 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001714 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001715 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001716 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001717
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001718 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001719 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001720
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001721 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1722
1723 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001724 assert(!StrVec[0].empty());
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001725 if (StrVec[0][0] == '$') {
1726 while (I != E && (*I)[0] != ')' )
1727 ++I;
1728
1729 // Skip the ')' symbol.
1730 ++I;
1731 }
1732 else {
1733 ++I;
1734 }
1735
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001736 bool hasINFILE = false;
1737
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001738 for (; I != E; ++I) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001739 const std::string& cmd = *I;
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001740 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001741 O.indent(IndentLevel);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001742 if (cmd.at(0) == '$') {
1743 if (cmd == "$INFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001744 hasINFILE = true;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001745 if (IsJoin) {
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001746 O << "for (PathVector::const_iterator B = inFiles.begin()"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001747 << ", E = inFiles.end();\n";
1748 O.indent(IndentLevel) << "B != E; ++B)\n";
1749 O.indent(IndentLevel + Indent1) << "vec.push_back(B->str());\n";
1750 }
1751 else {
Chris Lattner74382b72009-08-23 22:45:37 +00001752 O << "vec.push_back(inFile.str());\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001753 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001754 }
1755 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001756 O << "vec.push_back(\"\");\n";
1757 O.indent(IndentLevel) << "out_file_index = vec.size()-1;\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001758 }
1759 else {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001760 O << "vec.push_back(";
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001761 I = SubstituteSpecialCommands(I, E, IsJoin, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001762 O << ");\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001763 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001764 }
1765 else {
1766 O << "vec.push_back(\"" << cmd << "\");\n";
1767 }
1768 }
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001769 if (!hasINFILE)
1770 throw "Tool '" + ToolName + "' doesn't take any input!";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001771
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001772 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001773 if (StrVec[0][0] == '$')
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001774 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001775 else
1776 O << '"' << StrVec[0] << '"';
1777 O << ";\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001778}
1779
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001780/// EmitCmdLineVecFillCallback - A function object wrapper around
1781/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1782/// argument to EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001783class EmitCmdLineVecFillCallback {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001784 bool IsJoin;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001785 const std::string& ToolName;
1786 public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001787 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1788 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001789
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001790 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001791 raw_ostream& O) const
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001792 {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001793 EmitCmdLineVecFill(Statement, ToolName, IsJoin, IndentLevel, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001794 }
1795};
1796
1797/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1798/// implement EmitActionHandler. Emits code for
1799/// handling the (forward) and (forward_as) option properties.
1800void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001801 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001802 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001803 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001804 const std::string& Name = NewName.empty()
1805 ? ("-" + D.Name)
1806 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001807 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001808
1809 switch (D.Type) {
1810 case OptionType::Switch:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001811 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001812 break;
1813 case OptionType::Parameter:
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001814 O.indent(IndentLevel) << "vec.push_back(\"" << Name;
1815
1816 if (!D.isForwardNotSplit()) {
1817 O << "\");\n";
1818 O.indent(IndentLevel) << "vec.push_back("
1819 << D.GenVariableName() << ");\n";
1820 }
1821 else {
1822 O << "=\" + " << D.GenVariableName() << ");\n";
1823 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001824 break;
1825 case OptionType::Prefix:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001826 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\" + "
1827 << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001828 break;
1829 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001830 O.indent(IndentLevel)
1831 << "for (" << D.GenTypeDeclaration()
1832 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1833 O.indent(IndentLevel)
1834 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1835 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\" + " << "*B);\n";
1836 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001837
1838 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001839 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1840 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001841 }
1842
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001843 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001844 break;
1845 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001846 O.indent(IndentLevel)
1847 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1848 << D.GenVariableName() << ".begin(),\n";
1849 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1850 << ".end() ; B != E;) {\n";
1851 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001852
1853 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001854 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1855 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001856 }
1857
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001858 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001859 break;
1860 case OptionType::Alias:
1861 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001862 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001863 }
1864}
1865
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001866/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1867/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001868struct ActionHandlingCallbackBase
1869{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001870
1871 void onErrorDag(const DagInit& d,
1872 unsigned IndentLevel, raw_ostream& O) const
1873 {
1874 O.indent(IndentLevel)
1875 << "throw std::runtime_error(\"" <<
1876 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1877 : "Unknown error!")
1878 << "\");\n";
1879 }
1880
1881 void onWarningDag(const DagInit& d,
1882 unsigned IndentLevel, raw_ostream& O) const
1883 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001884 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001885 O.indent(IndentLevel) << "llvm::errs() << \""
1886 << InitPtrToString(d.getArg(0)) << "\";\n";
1887 }
1888
1889};
1890
1891/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1892/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001893
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001894class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001895
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001896typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1897(const DagInit&, unsigned, raw_ostream&) const;
1898
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001899class EmitActionHandlersCallback :
1900 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001901 public HandlerTable<EmitActionHandlersCallbackHandler>
1902{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001903 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001904
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001905 const OptionDescriptions& OptDescs;
1906
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001907 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1908 /// onAppendCmd and onOutputSuffix.
1909 void EmitHookInvocation(const std::string& Str,
1910 const char* BlockOpen, const char* BlockClose,
1911 unsigned IndentLevel, raw_ostream& O) const
1912 {
1913 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001914 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001915
1916 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1917 B != E; ++B) {
1918 const std::string& cmd = *B;
1919
1920 O.indent(IndentLevel) << BlockOpen;
1921
1922 if (cmd.at(0) == '$')
1923 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1924 else
1925 O << '"' << cmd << '"';
1926
1927 O << BlockClose;
1928 }
1929 }
1930
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001931 void onAppendCmd (const DagInit& Dag,
1932 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001933 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001934 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001935 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
1936 "vec.push_back(", ");\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001937 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001938
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001939 void onForward (const DagInit& Dag,
1940 unsigned IndentLevel, raw_ostream& O) const
1941 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001942 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001943 const std::string& Name = InitPtrToString(Dag.getArg(0));
1944 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1945 IndentLevel, "", O);
1946 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001947
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001948 void onForwardAs (const DagInit& Dag,
1949 unsigned IndentLevel, raw_ostream& O) const
1950 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001951 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001952 const std::string& Name = InitPtrToString(Dag.getArg(0));
1953 const std::string& NewName = InitPtrToString(Dag.getArg(1));
1954 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1955 IndentLevel, NewName, O);
1956 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001957
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001958 void onForwardValue (const DagInit& Dag,
1959 unsigned IndentLevel, raw_ostream& O) const
1960 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001961 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001962 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001963 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001964
1965 if (D.isParameter()) {
1966 O.indent(IndentLevel) << "vec.push_back("
1967 << D.GenVariableName() << ");\n";
1968 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001969 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001970 O.indent(IndentLevel) << "std::copy(" << D.GenVariableName()
1971 << ".begin(), " << D.GenVariableName()
1972 << ".end(), std::back_inserter(vec));\n";
1973 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001974 }
1975
1976 void onForwardTransformedValue (const DagInit& Dag,
1977 unsigned IndentLevel, raw_ostream& O) const
1978 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001979 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001980 const std::string& Name = InitPtrToString(Dag.getArg(0));
1981 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001982 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001983
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001984 O.indent(IndentLevel) << "vec.push_back(" << "hooks::"
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00001985 << Hook << "(" << D.GenVariableName()
1986 << (D.isParameter() ? ".c_str()" : "") << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001987 }
1988
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001989 void onOutputSuffix (const DagInit& Dag,
1990 unsigned IndentLevel, raw_ostream& O) const
1991 {
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)),
1994 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001995 }
1996
1997 void onStopCompilation (const DagInit& Dag,
1998 unsigned IndentLevel, raw_ostream& O) const
1999 {
2000 O.indent(IndentLevel) << "stop_compilation = true;\n";
2001 }
2002
2003
2004 void onUnpackValues (const DagInit& Dag,
2005 unsigned IndentLevel, raw_ostream& O) const
2006 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002007 throw "'unpack_values' is deprecated. "
2008 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002009 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002010
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002011 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002012
2013 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
2014 : OptDescs(OD)
2015 {
2016 if (!staticMembersInitialized_) {
2017 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
2018 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
2019 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
2020 AddHandler("forward", &EmitActionHandlersCallback::onForward);
2021 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002022 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
2023 AddHandler("forward_transformed_value",
2024 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002025 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
2026 AddHandler("stop_compilation",
2027 &EmitActionHandlersCallback::onStopCompilation);
2028 AddHandler("unpack_values",
2029 &EmitActionHandlersCallback::onUnpackValues);
2030
2031 staticMembersInitialized_ = true;
2032 }
2033 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002034
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002035 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002036 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002037 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002038 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002039 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002040};
2041
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002042bool IsOutFileIndexCheckRequiredStr (const Init* CmdLine) {
2043 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002044 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002045
2046 for (StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
2047 I != E; ++I) {
2048 if (*I == "$OUTFILE")
2049 return false;
2050 }
2051
2052 return true;
2053}
2054
2055class IsOutFileIndexCheckRequiredStrCallback {
2056 bool* ret_;
2057
2058public:
2059 IsOutFileIndexCheckRequiredStrCallback(bool* ret) : ret_(ret)
2060 {}
2061
2062 void operator()(const Init* CmdLine) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002063 // Ignore nested 'case' DAG.
2064 if (typeid(*CmdLine) == typeid(DagInit))
2065 return;
2066
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002067 if (IsOutFileIndexCheckRequiredStr(CmdLine))
2068 *ret_ = true;
2069 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002070
2071 void operator()(const DagInit* Test, unsigned, bool) {
2072 this->operator()(Test);
2073 }
2074 void operator()(const Init* Statement, unsigned) {
2075 this->operator()(Statement);
2076 }
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002077};
2078
2079bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
2080 bool ret = false;
2081 WalkCase(CmdLine, Id(), IsOutFileIndexCheckRequiredStrCallback(&ret));
2082 return ret;
2083}
2084
2085/// IsOutFileIndexCheckRequired - Should we emit an "out_file_index != -1" check
2086/// in EmitGenerateActionMethod() ?
2087bool IsOutFileIndexCheckRequired (Init* CmdLine) {
2088 if (typeid(*CmdLine) == typeid(StringInit))
2089 return IsOutFileIndexCheckRequiredStr(CmdLine);
2090 else
2091 return IsOutFileIndexCheckRequiredCase(CmdLine);
2092}
2093
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002094void EmitGenerateActionMethodHeader(const ToolDescription& D,
2095 bool IsJoin, raw_ostream& O)
2096{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002097 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002098 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002099 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002100 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002101
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002102 O.indent(Indent2) << "bool HasChildren,\n";
2103 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2104 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2105 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2106 O.indent(Indent1) << "{\n";
2107 O.indent(Indent2) << "std::string cmd;\n";
2108 O.indent(Indent2) << "std::vector<std::string> vec;\n";
2109 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2110 O.indent(Indent2) << "const char* output_suffix = \""
2111 << D.OutputSuffix << "\";\n";
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002112}
2113
2114// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2115// Tool::GenerateAction() method.
2116void EmitGenerateActionMethod (const ToolDescription& D,
2117 const OptionDescriptions& OptDescs,
2118 bool IsJoin, raw_ostream& O) {
2119
2120 EmitGenerateActionMethodHeader(D, IsJoin, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002121
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002122 if (!D.CmdLine)
2123 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002124
2125 bool IndexCheckRequired = IsOutFileIndexCheckRequired(D.CmdLine);
2126 O.indent(Indent2) << "int out_file_index"
2127 << (IndexCheckRequired ? " = -1" : "")
2128 << ";\n\n";
2129
2130 // Process the cmd_line property.
2131 if (typeid(*D.CmdLine) == typeid(StringInit))
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002132 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2133 else
2134 EmitCaseConstructHandler(D.CmdLine, Indent2,
2135 EmitCmdLineVecFillCallback(IsJoin, D.Name),
2136 true, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002137
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002138 // For every understood option, emit handling code.
2139 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002140 EmitCaseConstructHandler(D.Actions, Indent2,
2141 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002142 false, OptDescs, O);
2143
2144 O << '\n';
2145 O.indent(Indent2)
2146 << "std::string out_file = OutFilename("
2147 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
2148 O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002149
2150 if (IndexCheckRequired)
2151 O.indent(Indent2) << "if (out_file_index != -1)\n";
2152 O.indent(IndexCheckRequired ? Indent3 : Indent2)
2153 << "vec[out_file_index] = out_file;\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002154
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002155 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002156 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002157 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
2158 O.indent(Indent3) << "vec.insert(vec.end(), "
2159 << SinkOptionName << ".begin(), " << SinkOptionName
2160 << ".end());\n";
2161 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002162 }
2163
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002164 O.indent(Indent2) << "return Action(cmd, vec, stop_compilation, out_file);\n";
2165 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002166}
2167
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002168/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2169/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002170void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2171 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002172 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002173 if (!ToolDesc.isJoin()) {
2174 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
2175 O.indent(Indent2) << "bool HasChildren,\n";
2176 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2177 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2178 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2179 O.indent(Indent1) << "{\n";
2180 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
2181 << " is not a Join tool!\");\n";
2182 O.indent(Indent1) << "}\n\n";
2183 }
2184 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002185 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002186 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002187
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002188 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002189}
2190
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002191/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2192/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002193void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002194 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2195 O.indent(Indent2) << "return InputLanguages_;\n";
2196 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002197
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002198 if (D.OutLanguage.empty())
2199 throw "Tool " + D.Name + " has no 'out_language' property!";
2200
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002201 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2202 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2203 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002204}
2205
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002206/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002207void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002208 O.indent(Indent1) << "const char* Name() const {\n";
2209 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2210 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002211}
2212
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002213/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2214/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002215void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002216 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002217 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002218 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002219 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002220 O.indent(Indent2) << "return false;\n";
2221 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002222}
2223
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002224/// EmitStaticMemberDefinitions - Emit static member definitions for a
2225/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002226void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002227 if (D.InLanguage.empty())
2228 throw "Tool " + D.Name + " has no 'in_language' property!";
2229
2230 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2231 for (StrVector::const_iterator B = D.InLanguage.begin(),
2232 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002233 O << '\"' << *B << "\", ";
2234 O << "0};\n\n";
2235}
2236
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002237/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002238void EmitToolClassDefinition (const ToolDescription& D,
2239 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002240 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002241 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002242 return;
2243
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002244 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002245 O << "class " << D.Name << " : public ";
2246 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002247 O << "JoinTool";
2248 else
2249 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002250
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002251 O << " {\nprivate:\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002252 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002253
2254 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002255 EmitNameMethod(D, O);
2256 EmitInOutLanguageMethods(D, O);
2257 EmitIsJoinMethod(D, O);
2258 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002259
2260 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002261 O << "};\n";
2262
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002263 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002264
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002265}
2266
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002267/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002268/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002269void EmitOptionDefinitions (const OptionDescriptions& descs,
2270 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002271 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002272{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002273 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002274
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002275 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002276 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002277 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002278 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002279
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002280 if (val.Type == OptionType::Alias) {
2281 Aliases.push_back(val);
2282 continue;
2283 }
2284
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002285 if (val.isExtern())
2286 O << "extern ";
2287
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002288 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002289 << val.GenVariableName();
2290
2291 if (val.isExtern()) {
2292 O << ";\n";
2293 continue;
2294 }
2295
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002296 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002297
2298 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2299 O << ", cl::Prefix";
2300
2301 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002302 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002303 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002304 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002305 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002306 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002307 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002308 O << ", cl::OneOrMore";
2309 }
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002310 else if (val.isOptional() && val.isList()) {
2311 O << ", cl::Optional";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002312 }
2313
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002314 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002315 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002316 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002317 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002318
2319 if (val.isCommaSeparated())
2320 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002321
2322 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002323 O << ", cl::multi_val(" << val.MultiVal << ')';
2324
2325 if (val.InitVal) {
2326 const std::string& str = val.InitVal->getAsString();
2327 O << ", cl::init(" << str << ')';
2328 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002329
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002330 if (!val.Help.empty())
2331 O << ", cl::desc(\"" << val.Help << "\")";
2332
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002333 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002334 }
2335
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002336 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002337 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002338 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002339 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002340
2341 O << val.GenTypeDeclaration() << ' '
2342 << val.GenVariableName()
2343 << "(\"" << val.Name << '\"';
2344
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002345 const OptionDescription& D = descs.FindOption(val.Help);
2346 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002347
2348 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2349 }
2350
2351 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002352 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002353 O << (HasExterns ? "extern cl" : "cl")
2354 << "::list<std::string> " << SinkOptionName
2355 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002356
2357 O << '\n';
2358}
2359
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002360/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002361/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002362
2363class EmitPreprocessOptionsCallback;
2364
2365typedef void
2366(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2367(const DagInit&, unsigned, raw_ostream&) const;
2368
2369class EmitPreprocessOptionsCallback :
2370 public ActionHandlingCallbackBase,
2371 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2372{
2373 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002374 typedef void
2375 (EmitPreprocessOptionsCallback::* HandlerImpl)
2376 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002377
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002378 const OptionDescriptions& OptDescs_;
2379
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002380 void onListOrDag(const DagInit& d, HandlerImpl h,
2381 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002382 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002383 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002384 const Init* I = d.getArg(0);
2385
2386 // If I is a list, apply h to each element.
2387 if (typeid(*I) == typeid(ListInit)) {
2388 const ListInit& L = *static_cast<const ListInit*>(I);
2389 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B)
2390 ((this)->*(h))(*B, IndentLevel, O);
2391 }
2392 // Otherwise, apply h to I.
2393 else {
2394 ((this)->*(h))(I, IndentLevel, O);
2395 }
2396 }
2397
2398 void onUnsetOptionImpl(const Init* I,
2399 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002400 {
2401 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002402 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002403
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002404 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002405 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2406 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002407 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002408 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2409 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002410 else if (OptDesc.isList()) {
2411 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2412 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002413 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002414 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002415 }
2416 }
2417
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002418 void onUnsetOption(const DagInit& d,
2419 unsigned IndentLevel, raw_ostream& O) const
2420 {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002421 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2422 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002423 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002424
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002425 void onSetOptionImpl(const DagInit& d,
2426 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002427 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002428 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002429 const Init* Value = d.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002430 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2431
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002432 if (OptDesc.isList()) {
2433 const ListInit& List = InitPtrToList(Value);
2434
2435 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2436 for (ListInit::const_iterator B = List.begin(), E = List.end();
2437 B != E; ++B) {
2438 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".push_back(\""
2439 << InitPtrToString(*B) << "\");\n";
2440 }
2441 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002442 else if (OptDesc.isSwitch()) {
2443 CheckBooleanConstant(Value);
2444 O.indent(IndentLevel) << OptDesc.GenVariableName()
2445 << " = " << Value->getAsString() << ";\n";
2446 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002447 else if (OptDesc.isParameter()) {
2448 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002449 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002450 << " = \"" << Str << "\";\n";
2451 }
2452 else {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002453 throw "Can't apply 'set_option' to alias option -" + OptName + " !";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002454 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002455 }
2456
2457 void onSetSwitch(const Init* I,
2458 unsigned IndentLevel, raw_ostream& O) const {
2459 const std::string& OptName = InitPtrToString(I);
2460 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2461
2462 if (OptDesc.isSwitch())
2463 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2464 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002465 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002466 }
2467
2468 void onSetOption(const DagInit& d,
2469 unsigned IndentLevel, raw_ostream& O) const
2470 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002471 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002472
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002473 // Two arguments: (set_option "parameter", VALUE), where VALUE can be a
2474 // boolean, a string or a string list.
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002475 if (d.getNumArgs() > 1)
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002476 this->onSetOptionImpl(d, IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002477 // One argument: (set_option "switch")
2478 // or (set_option ["switch1", "switch2", ...])
2479 else
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002480 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2481 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002482 }
2483
2484public:
2485
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002486 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002487 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002488 {
2489 if (!staticMembersInitialized_) {
2490 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2491 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2492 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002493 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002494
2495 staticMembersInitialized_ = true;
2496 }
2497 }
2498
2499 void operator()(const Init* I,
2500 unsigned IndentLevel, raw_ostream& O) const
2501 {
2502 InvokeDagInitHandler(this, I, IndentLevel, O);
2503 }
2504
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002505};
2506
2507/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2508void EmitPreprocessOptions (const RecordKeeper& Records,
2509 const OptionDescriptions& OptDecs, raw_ostream& O)
2510{
2511 O << "void PreprocessOptionsLocal() {\n";
2512
2513 const RecordVector& OptionPreprocessors =
2514 Records.getAllDerivedDefinitions("OptionPreprocessor");
2515
2516 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2517 E = OptionPreprocessors.end(); B!=E; ++B) {
2518 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002519 EmitCaseConstructHandler(Case, Indent1,
2520 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002521 false, OptDecs, O);
2522 }
2523
2524 O << "}\n\n";
2525}
2526
2527/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002528void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002529{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002530 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002531
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002532 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002533 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002534
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002535 // It is allowed for a plugin to have no language map.
2536 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002537
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002538 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2539 if (!LangsToSuffixesList)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00002540 throw "Error in the language map definition!";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002541
2542 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002543 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002544
2545 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2546 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2547
2548 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002549 O.indent(Indent1) << "langMap[\""
2550 << InitPtrToString(Suffixes->getElement(i))
2551 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002552 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002553 }
2554
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002555 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002556}
2557
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002558/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2559/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002560void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002561 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002562 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002563 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002564
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002565 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002566 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002567 }
2568 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002569 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002570 }
2571 else if (OpName == "error") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002572 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002573 O.indent(IndentLevel) << "throw std::runtime_error(\""
2574 << InitPtrToString(d.getArg(0))
2575 << "\");\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002576 return;
2577 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002578 else {
2579 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002580 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002581 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002582
2583 if (d.getNumArgs() > 0)
2584 O << InitPtrToInt(d.getArg(0)) << ";\n";
2585 else
2586 O << "2;\n";
2587
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002588}
2589
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002590/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002591void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002592 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002593 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002594
2595 // Class constructor.
2596 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002597 << "public:\n";
2598 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2599 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002600
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002601 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002602 O.indent(Indent1)
2603 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2604 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002605
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002606 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002607 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002608
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002609 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002610 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002611}
2612
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002613/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002614void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002615 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002616 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002617 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002618 for (RecordVector::const_iterator B = EdgeVector.begin(),
2619 E = EdgeVector.end(); B != E; ++B) {
2620 const Record* Edge = *B;
2621 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002622 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002623
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002624 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002625 EmitEdgeClass(i, NodeB, &Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002626 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002627 }
2628}
2629
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002630/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002631/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002632void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002633 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002634 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002635{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002636 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002637
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002638 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2639 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002640 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002641
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002642 O << '\n';
2643
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002644 // Insert edges.
2645
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002646 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002647 for (RecordVector::const_iterator B = EdgeVector.begin(),
2648 E = EdgeVector.end(); B != E; ++B) {
2649 const Record* Edge = *B;
2650 const std::string& NodeA = Edge->getValueAsString("a");
2651 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002652 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002653
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002654 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002655
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002656 if (IsDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002657 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002658 else
2659 O << "new Edge" << i << "()";
2660
2661 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002662 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002663 }
2664
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002665 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002666}
2667
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002668/// HookInfo - Information about the hook type and number of arguments.
2669struct HookInfo {
2670
2671 // A hook can either have a single parameter of type std::vector<std::string>,
2672 // or NumArgs parameters of type const char*.
2673 enum HookType { ListHook, ArgHook };
2674
2675 HookType Type;
2676 unsigned NumArgs;
2677
2678 HookInfo() : Type(ArgHook), NumArgs(1)
2679 {}
2680
2681 HookInfo(HookType T) : Type(T), NumArgs(1)
2682 {}
2683
2684 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2685 {}
2686};
2687
2688typedef llvm::StringMap<HookInfo> HookInfoMap;
2689
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002690/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002691/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002692/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002693class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002694 HookInfoMap& HookNames_;
2695 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002696public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002697 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2698 : HookNames_(HookNames), OptDescs_(OptDescs)
2699 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002700
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002701 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002702 const std::string& Name = GetOperatorName(Dag);
2703
2704 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002705 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002706 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2707 const std::string& HookName = InitPtrToString(Dag.getArg(1));
2708 const OptionDescription& D = OptDescs_.FindOption(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002709
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002710 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2711 : HookInfo::ArgHook);
2712 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002713 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002714 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002715 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2716 }
2717 }
2718
2719 void onCmdLine(const std::string& Cmd) {
2720 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002721 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002722
2723 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2724 B != E; ++B) {
2725 const std::string& cmd = *B;
2726
2727 if (cmd == "$CALL") {
2728 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002729 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002730 const std::string& HookName = *B;
2731
2732 if (HookName.at(0) == ')')
2733 throw "$CALL invoked with no arguments!";
2734
2735 while (++B != E && B->at(0) != ')') {
2736 ++NumArgs;
2737 }
2738
2739 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2740
2741 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2742 H->second.Type != HookInfo::ArgHook)
2743 throw "Overloading of hooks is not allowed. Overloaded hook: "
2744 + HookName;
2745 else
2746 HookNames_[HookName] = HookInfo(NumArgs);
2747 }
2748 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002749 }
2750
2751 void operator()(const Init* Arg) {
2752
2753 // We're invoked on an action (either a dag or a dag list).
2754 if (typeid(*Arg) == typeid(DagInit)) {
2755 const DagInit& Dag = InitPtrToDag(Arg);
2756 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002757 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002758 }
2759 else if (typeid(*Arg) == typeid(ListInit)) {
2760 const ListInit& List = InitPtrToList(Arg);
2761 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2762 ++B) {
2763 const DagInit& Dag = InitPtrToDag(*B);
2764 this->onAction(Dag);
2765 }
2766 return;
2767 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002768
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002769 // We're invoked on a command line.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002770 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002771 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002772
2773 void operator()(const DagInit* Test, unsigned, bool) {
2774 this->operator()(Test);
2775 }
2776 void operator()(const Init* Statement, unsigned) {
2777 this->operator()(Statement);
2778 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002779};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002780
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002781/// FillInHookNames - Actually extract the hook names from all command
2782/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002783void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002784 const OptionDescriptions& OptDescs,
2785 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002786{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002787 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002788 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2789 E = ToolDescs.end(); B != E; ++B) {
2790 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002791
2792 // Look for 'forward_transformed_value' in 'actions'.
2793 if (D.Actions)
2794 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2795
2796 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002797 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002798 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002799 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002800 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002801 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002802 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002803 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002804 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002805 }
2806}
2807
2808/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2809/// property records and emit hook function declaration for each
2810/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002811void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2812 const OptionDescriptions& OptDescs, raw_ostream& O) {
2813 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002814
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002815 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002816 if (HookNames.empty())
2817 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002818
2819 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002820 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002821 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002822 const char* HookName = B->first();
2823 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002824
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002825 O.indent(Indent1) << "std::string " << HookName << "(";
2826
2827 if (Info.Type == HookInfo::ArgHook) {
2828 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2829 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2830 }
2831 }
2832 else {
2833 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002834 }
2835
2836 O <<");\n";
2837 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002838 O << "}\n\n";
2839}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002840
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002841/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002842void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002843 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2844 O.indent(Indent1) << "int Priority() const { return "
2845 << Priority << "; }\n\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002846 O.indent(Indent1) << "void PreprocessOptions() const\n";
2847 O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002848 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2849 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2850 O.indent(Indent1)
2851 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2852 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2853 << "};\n\n"
2854 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002855}
2856
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002857/// EmitIncludes - Emit necessary #include directives and some
2858/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002859void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002860 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2861 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002862 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002863 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2864 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002865
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002866 << "#include \"llvm/Support/CommandLine.h\"\n"
2867 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002868
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002869 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002870 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002871 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002872 << "#include <stdexcept>\n\n"
2873
2874 << "using namespace llvm;\n"
2875 << "using namespace llvmc;\n\n"
2876
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002877 << "extern cl::opt<std::string> OutputFilename;\n\n"
2878
2879 << "inline const char* checkCString(const char* s)\n"
2880 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002881}
2882
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002883
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002884/// PluginData - Holds all information about a plugin.
2885struct PluginData {
2886 OptionDescriptions OptDescs;
2887 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002888 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002889 ToolDescriptions ToolDescs;
2890 RecordVector Edges;
2891 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002892};
2893
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002894/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002895/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002896bool HasSink(const ToolDescriptions& ToolDescs) {
2897 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2898 E = ToolDescs.end(); B != E; ++B)
2899 if ((*B)->isSink())
2900 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002901
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002902 return false;
2903}
2904
2905/// HasExterns - Go through the list of option descriptions and check
2906/// if there are any external options.
2907bool HasExterns(const OptionDescriptions& OptDescs) {
2908 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2909 E = OptDescs.end(); B != E; ++B)
2910 if (B->second.isExtern())
2911 return true;
2912
2913 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002914}
2915
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002916/// CollectPluginData - Collect tool and option properties,
2917/// compilation graph edges and plugin priority from the parse tree.
2918void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2919 // Collect option properties.
2920 const RecordVector& OptionLists =
2921 Records.getAllDerivedDefinitions("OptionList");
2922 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2923 Data.OptDescs);
2924
2925 // Collect tool properties.
2926 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2927 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2928 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002929 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002930
2931 // Collect compilation graph edges.
2932 const RecordVector& CompilationGraphs =
2933 Records.getAllDerivedDefinitions("CompilationGraph");
2934 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2935 Data.Edges);
2936
2937 // Calculate the priority of this plugin.
2938 const RecordVector& Priorities =
2939 Records.getAllDerivedDefinitions("PluginPriority");
2940 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002941}
2942
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002943/// CheckPluginData - Perform some sanity checks on the collected data.
2944void CheckPluginData(PluginData& Data) {
2945 // Filter out all tools not mentioned in the compilation graph.
2946 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002947
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002948 // Typecheck the compilation graph.
2949 TypecheckGraph(Data.Edges, Data.ToolDescs);
2950
2951 // Check that there are no options without side effects (specified
2952 // only in the OptionList).
2953 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002954}
2955
Daniel Dunbar1a551802009-07-03 00:10:29 +00002956void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002957 // Emit file header.
2958 EmitIncludes(O);
2959
2960 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002961 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002962
2963 // Emit hook declarations.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002964 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002965
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002966 O << "namespace {\n\n";
2967
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002968 // Emit PreprocessOptionsLocal() function.
2969 EmitPreprocessOptions(Records, Data.OptDescs, O);
2970
2971 // Emit PopulateLanguageMapLocal() function
2972 // (language map maps from file extensions to language names).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002973 EmitPopulateLanguageMap(Records, O);
2974
2975 // Emit Tool classes.
2976 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2977 E = Data.ToolDescs.end(); B!=E; ++B)
2978 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2979
2980 // Emit Edge# classes.
2981 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2982
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002983 // Emit PopulateCompilationGraphLocal() function.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002984 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2985
2986 // Emit code for plugin registration.
2987 EmitRegisterPlugin(Data.Priority, O);
2988
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002989 O << "} // End anonymous namespace.\n\n";
2990
2991 // Force linkage magic.
2992 O << "namespace llvmc {\n";
2993 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2994 O << "}\n";
2995
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002996 // EOF
2997}
2998
2999
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003000// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00003001}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003002
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003003/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00003004void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003005 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003006 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003007
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003008 CollectPluginData(Records, Data);
3009 CheckPluginData(Data);
3010
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +00003011 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003012 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003013
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003014 } catch (std::exception& Error) {
3015 throw Error.what() + std::string(" - usually this means a syntax error.");
3016 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003017}