blob: 836a7750c71e788ceba19e341c2a183b11c82884 [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;
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000786 const Init* OnEmpty;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000787
788 // Various boolean properties
789 void setSink() { Flags |= ToolFlags::Sink; }
790 bool isSink() const { return Flags & ToolFlags::Sink; }
791 void setJoin() { Flags |= ToolFlags::Join; }
792 bool isJoin() const { return Flags & ToolFlags::Join; }
793
794 // Default ctor here is needed because StringMap can only store
795 // DefaultConstructible objects
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000796 ToolDescription() : CmdLine(0), Actions(0), Flags(0), OnEmpty(0) {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000797 ToolDescription (const std::string& n)
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000798 : Name(n), CmdLine(0), Actions(0), Flags(0), OnEmpty(0)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000799 {}
800};
801
802/// ToolDescriptions - A list of Tool information records.
803typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
804
805
806/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000807/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000808
809class CollectToolProperties;
810typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000811(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000812
813class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
814{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000815private:
816
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000817 /// toolDesc_ - Properties of the current Tool. This is where the
818 /// information is stored.
819 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000820
821public:
822
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000823 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000824 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000825 {
826 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000827
828 AddHandler("actions", &CollectToolProperties::onActions);
829 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
830 AddHandler("in_language", &CollectToolProperties::onInLanguage);
831 AddHandler("join", &CollectToolProperties::onJoin);
832 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
833 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
834 AddHandler("sink", &CollectToolProperties::onSink);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000835 AddHandler("works_on_empty", &CollectToolProperties::onWorksOnEmpty);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000836
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000837 staticMembersInitialized_ = true;
838 }
839 }
840
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000841 void operator() (Init* I) {
842 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000843 }
844
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000845private:
846
847 /// Property handlers --
848 /// Functions that extract information about tool properties from
849 /// DAG representation.
850
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000851 void onActions (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000852 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000853 Init* Case = d.getArg(0);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000854 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000855 GetOperatorName(static_cast<DagInit&>(*Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000856 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000857 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000858 }
859
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000860 void onCmdLine (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000861 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000862 toolDesc_.CmdLine = d.getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000863 }
864
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000865 void onInLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000866 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000867 Init* arg = d.getArg(0);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000868
869 // Find out the argument's type.
870 if (typeid(*arg) == typeid(StringInit)) {
871 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000872 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000873 }
874 else {
875 // It's a list.
876 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000877 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000878
879 // Copy strings to the output vector.
880 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
881 B != E; ++B) {
882 out.push_back(InitPtrToString(*B));
883 }
884
885 // Remove duplicates.
886 std::sort(out.begin(), out.end());
887 StrVector::iterator newE = std::unique(out.begin(), out.end());
888 out.erase(newE, out.end());
889 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000890 }
891
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000892 void onJoin (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000893 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000894 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000895 }
896
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000897 void onOutLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000898 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000899 toolDesc_.OutLanguage = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000900 }
901
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000902 void onOutputSuffix (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000903 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000904 toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000905 }
906
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000907 void onSink (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000908 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000909 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000910 }
911
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000912 void onWorksOnEmpty (const DagInit& d) {
913 toolDesc_.OnEmpty = d.getArg(0);
914 }
915
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000916};
917
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000918/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000919/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000920/// CollectToolProperties function object).
921void CollectToolDescriptions (RecordVector::const_iterator B,
922 RecordVector::const_iterator E,
923 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000924{
925 // Iterate over a properties list of every Tool definition
926 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000927 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000928 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000929 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000930
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000931 IntrusiveRefCntPtr<ToolDescription>
932 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000933
934 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000935 CollectToolProperties(*ToolDesc));
936 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000937 }
938}
939
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000940/// FillInEdgeVector - Merge all compilation graph definitions into
941/// one single edge list.
942void FillInEdgeVector(RecordVector::const_iterator B,
943 RecordVector::const_iterator E, RecordVector& Out) {
944 for (; B != E; ++B) {
945 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000946
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000947 for (unsigned i = 0; i < edges->size(); ++i)
948 Out.push_back(edges->getElementAsRecord(i));
949 }
950}
951
952/// CalculatePriority - Calculate the priority of this plugin.
953int CalculatePriority(RecordVector::const_iterator B,
954 RecordVector::const_iterator E) {
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000955 int priority = 0;
956
957 if (B != E) {
958 priority = static_cast<int>((*B)->getValueAsInt("priority"));
959
960 if (++B != E)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000961 throw "More than one 'PluginPriority' instance found: "
962 "most probably an error!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000963 }
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000964
965 return priority;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000966}
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000967
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000968/// NotInGraph - Helper function object for FilterNotInGraph.
969struct NotInGraph {
970private:
971 const llvm::StringSet<>& ToolsInGraph_;
972
973public:
974 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
975 : ToolsInGraph_(ToolsInGraph)
976 {}
977
978 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
979 return (ToolsInGraph_.count(x->Name) == 0);
980 }
981};
982
983/// FilterNotInGraph - Filter out from ToolDescs all Tools not
984/// mentioned in the compilation graph definition.
985void FilterNotInGraph (const RecordVector& EdgeVector,
986 ToolDescriptions& ToolDescs) {
987
988 // List all tools mentioned in the graph.
989 llvm::StringSet<> ToolsInGraph;
990
991 for (RecordVector::const_iterator B = EdgeVector.begin(),
992 E = EdgeVector.end(); B != E; ++B) {
993
994 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000995 const std::string& NodeA = Edge->getValueAsString("a");
996 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000997
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000998 if (NodeA != "root")
999 ToolsInGraph.insert(NodeA);
1000 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001001 }
1002
1003 // Filter ToolPropertiesList.
1004 ToolDescriptions::iterator new_end =
1005 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
1006 NotInGraph(ToolsInGraph));
1007 ToolDescs.erase(new_end, ToolDescs.end());
1008}
1009
1010/// FillInToolToLang - Fills in two tables that map tool names to
1011/// (input, output) languages. Helper function used by TypecheckGraph().
1012void FillInToolToLang (const ToolDescriptions& ToolDescs,
1013 StringMap<StringSet<> >& ToolToInLang,
1014 StringMap<std::string>& ToolToOutLang) {
1015 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1016 E = ToolDescs.end(); B != E; ++B) {
1017 const ToolDescription& D = *(*B);
1018 for (StrVector::const_iterator B = D.InLanguage.begin(),
1019 E = D.InLanguage.end(); B != E; ++B)
1020 ToolToInLang[D.Name].insert(*B);
1021 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001022 }
1023}
1024
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001025/// TypecheckGraph - Check that names for output and input languages
1026/// on all edges do match. This doesn't do much when the information
1027/// about the whole graph is not available (i.e. when compiling most
1028/// plugins).
1029void TypecheckGraph (const RecordVector& EdgeVector,
1030 const ToolDescriptions& ToolDescs) {
1031 StringMap<StringSet<> > ToolToInLang;
1032 StringMap<std::string> ToolToOutLang;
1033
1034 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
1035 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
1036 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
1037
1038 for (RecordVector::const_iterator B = EdgeVector.begin(),
1039 E = EdgeVector.end(); B != E; ++B) {
1040 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001041 const std::string& NodeA = Edge->getValueAsString("a");
1042 const std::string& NodeB = Edge->getValueAsString("b");
1043 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
1044 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001045
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001046 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001047 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001048 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001049 + ": output->input language mismatch";
1050 }
1051
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001052 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001053 throw "Edges back to the root are not allowed!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001054 }
1055}
1056
1057/// WalkCase - Walks the 'case' expression DAG and invokes
1058/// TestCallback on every test, and StatementCallback on every
1059/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001060/// combinators (that is, they are passed directly to TestCallback).
1061/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1062/// IndentLevel, bool FirstTest)'.
1063/// StatementCallback must have type 'void StatementCallback(const Init*,
1064/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001065template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001066void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1067 unsigned IndentLevel = 0)
1068{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001069 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001070
1071 // Error checks.
1072 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001073 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001074
1075 if (d.getNumArgs() < 2)
1076 throw "There should be at least one clause in the 'case' expression:\n"
1077 + d.getAsString();
1078
1079 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001080 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001081 const unsigned numArgs = d.getNumArgs();
1082 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001083 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1084 B != E; ++B) {
1085 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001086
1087 if (!even)
1088 {
1089 // Handle test.
1090 const DagInit& Test = InitPtrToDag(arg);
1091
1092 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001093 throw "The 'default' clause should be the last in the "
1094 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001095 if (i == numArgs)
1096 throw "Case construct handler: no corresponding action "
1097 "found for the test " + Test.getAsString() + '!';
1098
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001099 TestCallback(Test, IndentLevel, (i == 1));
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001100 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001101 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001102 {
1103 if (dynamic_cast<DagInit*>(arg)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001104 && GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001105 // Nested 'case'.
1106 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1107 }
1108
1109 // Handle statement.
1110 StatementCallback(arg, IndentLevel);
1111 }
1112
1113 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001114 even = !even;
1115 }
1116}
1117
1118/// ExtractOptionNames - A helper function object used by
1119/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1120class ExtractOptionNames {
1121 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001122
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001123 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001124 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001125 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001126 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001127 ActionName == "forward_value" ||
1128 ActionName == "forward_transformed_value" ||
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001129 ActionName == "switch_on" || ActionName == "any_switch_on" ||
1130 ActionName == "parameter_equals" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001131 ActionName == "element_in_list" || ActionName == "not_empty" ||
1132 ActionName == "empty") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001133 CheckNumberOfArguments(Stmt, 1);
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001134
1135 Init* Arg = Stmt.getArg(0);
1136 if (typeid(*Arg) == typeid(StringInit)) {
1137 const std::string& Name = InitPtrToString(Arg);
1138 OptionNames_.insert(Name);
1139 }
1140 else {
1141 // It's a list.
1142 const ListInit& List = InitPtrToList(Arg);
1143 for (ListInit::const_iterator B = List.begin(), E = List.end();
1144 B != E; ++B) {
1145 const std::string& Name = InitPtrToString(*B);
1146 OptionNames_.insert(Name);
1147 }
1148 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001149 }
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001150 else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001151 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001152 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001153 }
1154 }
1155 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001156
1157public:
1158 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1159 {}
1160
1161 void operator()(const Init* Statement) {
1162 if (typeid(*Statement) == typeid(ListInit)) {
1163 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1164 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1165 B != E; ++B)
1166 this->processDag(*B);
1167 }
1168 else {
1169 this->processDag(Statement);
1170 }
1171 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001172
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001173 void operator()(const DagInit& Test, unsigned, bool) {
1174 this->operator()(&Test);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001175 }
1176 void operator()(const Init* Statement, unsigned) {
1177 this->operator()(Statement);
1178 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001179};
1180
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001181/// CheckForSuperfluousOptions - Check that there are no side
1182/// effect-free options (specified only in the OptionList). Otherwise,
1183/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001184void CheckForSuperfluousOptions (const RecordVector& Edges,
1185 const ToolDescriptions& ToolDescs,
1186 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001187 llvm::StringSet<> nonSuperfluousOptions;
1188
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001189 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001190 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001191 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1192 E = ToolDescs.end(); B != E; ++B) {
1193 const ToolDescription& TD = *(*B);
1194 ExtractOptionNames Callback(nonSuperfluousOptions);
1195 if (TD.Actions)
1196 WalkCase(TD.Actions, Callback, Callback);
1197 }
1198
1199 // Add all options mentioned in the 'case' clauses of the
1200 // OptionalEdges of the compilation graph to the set of
1201 // non-superfluous options.
1202 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1203 B != E; ++B) {
1204 const Record* Edge = *B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001205 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001206
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001207 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001208 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001209 }
1210
1211 // Check that all options in OptDescs belong to the set of
1212 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001213 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001214 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001215 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001216 if (!nonSuperfluousOptions.count(Val.Name)
1217 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001218 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001219 "Probable cause: this option is specified only in the OptionList.\n";
1220 }
1221}
1222
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001223/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1224bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1225 if (TestName == "single_input_file") {
1226 O << "InputFilenames.size() == 1";
1227 return true;
1228 }
1229 else if (TestName == "multiple_input_files") {
1230 O << "InputFilenames.size() > 1";
1231 return true;
1232 }
1233
1234 return false;
1235}
1236
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001237/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1238template <typename F>
1239void EmitListTest(const ListInit& L, const char* LogicOp,
1240 F Callback, raw_ostream& O)
1241{
1242 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1243 // of Dags...
1244 bool isFirst = true;
1245 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1246 if (isFirst)
1247 isFirst = false;
1248 else
Mikhail Glushenkovb7935e02010-01-01 04:40:54 +00001249 O << ' ' << LogicOp << ' ';
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001250 Callback(InitPtrToString(*B), O);
1251 }
1252}
1253
1254// Callbacks for use with EmitListTest.
1255
1256class EmitSwitchOn {
1257 const OptionDescriptions& OptDescs_;
1258public:
1259 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1260 {}
1261
1262 void operator()(const std::string& OptName, raw_ostream& O) const {
1263 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1264 O << OptDesc.GenVariableName();
1265 }
1266};
1267
1268class EmitEmptyTest {
1269 bool EmitNegate_;
1270 const OptionDescriptions& OptDescs_;
1271public:
1272 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1273 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1274 {}
1275
1276 void operator()(const std::string& OptName, raw_ostream& O) const {
1277 const char* Neg = (EmitNegate_ ? "!" : "");
1278 if (OptName == "o") {
1279 O << Neg << "OutputFilename.empty()";
1280 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001281 else if (OptName == "save-temps") {
1282 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1283 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001284 else {
1285 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1286 O << Neg << OptDesc.GenVariableName() << ".empty()";
1287 }
1288 }
1289};
1290
1291
1292/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1293bool EmitCaseTest1ArgList(const std::string& TestName,
1294 const DagInit& d,
1295 const OptionDescriptions& OptDescs,
1296 raw_ostream& O) {
Mikhail Glushenkov3a481e32010-01-01 03:50:51 +00001297 const ListInit& L = InitPtrToList(d.getArg(0));
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001298
1299 if (TestName == "any_switch_on") {
1300 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1301 return true;
1302 }
1303 else if (TestName == "switch_on") {
1304 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1305 return true;
1306 }
1307 else if (TestName == "any_not_empty") {
1308 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1309 return true;
1310 }
1311 else if (TestName == "any_empty") {
1312 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1313 return true;
1314 }
1315 else if (TestName == "not_empty") {
1316 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1317 return true;
1318 }
1319 else if (TestName == "empty") {
1320 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1321 return true;
1322 }
1323
1324 return false;
1325}
1326
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001327/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1328bool EmitCaseTest1ArgStr(const std::string& TestName,
1329 const DagInit& d,
1330 const OptionDescriptions& OptDescs,
1331 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001332 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001333
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001334 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001335 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001336 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001337 }
1338 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001339 O << "InLangs.count(\"" << OptName << "\") != 0";
1340 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001341 }
1342 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001343 // This works only for single-argument Tool::GenerateAction. Join
1344 // tools can process several files in different languages simultaneously.
1345
1346 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001347 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001348 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001349 }
1350 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001351 bool EmitNegate = (TestName == "not_empty");
1352 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001353 return true;
1354 }
1355
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001356 return false;
1357}
1358
1359/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1360bool EmitCaseTest1Arg(const std::string& TestName,
1361 const DagInit& d,
1362 const OptionDescriptions& OptDescs,
1363 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001364 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001365 if (typeid(*d.getArg(0)) == typeid(ListInit))
1366 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1367 else
1368 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1369}
1370
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001371/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001372bool EmitCaseTest2Args(const std::string& TestName,
1373 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001374 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001375 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001376 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001377 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001378 const std::string& OptName = InitPtrToString(d.getArg(0));
1379 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001380
1381 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001382 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001383 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1384 return true;
1385 }
1386 else if (TestName == "element_in_list") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001387 const OptionDescription& OptDesc = OptDescs.FindList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001388 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001389 O << "std::find(" << VarName << ".begin(),\n";
1390 O.indent(IndentLevel + Indent1)
1391 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001392 << OptArg << "\") != " << VarName << ".end()";
1393 return true;
1394 }
1395
1396 return false;
1397}
1398
1399// Forward declaration.
1400// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001401void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001402 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001403 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001404
1405/// EmitLogicalOperationTest - Helper function used by
1406/// EmitCaseConstructHandler.
1407void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001408 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001409 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001410 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001411 O << '(';
1412 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001413 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001414 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001415 if (j != NumArgs - 1) {
1416 O << ")\n";
1417 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1418 }
1419 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001420 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001421 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001422 }
1423}
1424
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001425void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001426 const OptionDescriptions& OptDescs, raw_ostream& O)
1427{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001428 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001429 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1430 O << "! (";
1431 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1432 O << ")";
1433}
1434
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001435/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001436void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001437 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001438 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001439 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001440
1441 if (TestName == "and")
1442 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1443 else if (TestName == "or")
1444 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001445 else if (TestName == "not")
1446 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001447 else if (EmitCaseTest0Args(TestName, O))
1448 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001449 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1450 return;
1451 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1452 return;
1453 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001454 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001455}
1456
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001457
1458/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1459class EmitCaseTestCallback {
1460 bool EmitElseIf_;
1461 const OptionDescriptions& OptDescs_;
1462 raw_ostream& O_;
1463public:
1464
1465 EmitCaseTestCallback(bool EmitElseIf,
1466 const OptionDescriptions& OptDescs, raw_ostream& O)
1467 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1468 {}
1469
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001470 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001471 {
1472 if (GetOperatorName(Test) == "default") {
1473 O_.indent(IndentLevel) << "else {\n";
1474 }
1475 else {
1476 O_.indent(IndentLevel)
1477 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001478 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001479 O_ << ") {\n";
1480 }
1481 }
1482};
1483
1484/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1485template <typename F>
1486class EmitCaseStatementCallback {
1487 F Callback_;
1488 raw_ostream& O_;
1489public:
1490
1491 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1492 : Callback_(Callback), O_(O)
1493 {}
1494
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001495 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001496
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001497 // Ignore nested 'case' DAG.
1498 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001499 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case")) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001500 if (typeid(*Statement) == typeid(ListInit)) {
1501 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1502 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1503 B != E; ++B)
1504 Callback_(*B, (IndentLevel + Indent1), O_);
1505 }
1506 else {
1507 Callback_(Statement, (IndentLevel + Indent1), O_);
1508 }
1509 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001510 O_.indent(IndentLevel) << "}\n";
1511 }
1512
1513};
1514
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001515/// EmitCaseConstructHandler - Emit code that handles the 'case'
1516/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001517/// clause. Implemented on top of WalkCase.
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00001518/// Callback's type is void F(const Init* Statement, unsigned IndentLevel,
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001519/// raw_ostream& O).
1520/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001521/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1522/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001523template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001524void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001525 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001526 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001527 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001528 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1529 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001530}
1531
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001532/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001533/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1534/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001535void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001536 const char* Delimiters = " \t\n\v\f\r";
1537 enum TokenizerState
1538 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1539 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001540
1541 if (CmdLine.empty())
1542 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001543 Out.push_back("");
1544
1545 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1546 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001547
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001548 for (; B != E; ++B) {
1549 char cur_ch = CmdLine[B];
1550
1551 switch (cur_st) {
1552 case Normal:
1553 if (cur_ch == '$') {
1554 cur_st = SpecialCommand;
1555 break;
1556 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001557 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001558 // Skip whitespace
1559 B = CmdLine.find_first_not_of(Delimiters, B);
1560 if (B == std::string::npos) {
1561 B = E-1;
1562 continue;
1563 }
1564 --B;
1565 Out.push_back("");
1566 continue;
1567 }
1568 break;
1569
1570
1571 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001572 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001573 cur_st = Normal;
1574 Out.push_back("");
1575 continue;
1576 }
1577 if (cur_ch == '(') {
1578 Out.push_back("");
1579 cur_st = InsideSpecialCommand;
1580 continue;
1581 }
1582 break;
1583
1584 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001585 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001586 continue;
1587 }
1588 if (cur_ch == '\'') {
1589 cur_st = InsideQuotationMarks;
1590 Out.push_back("");
1591 continue;
1592 }
1593 if (cur_ch == ')') {
1594 cur_st = Normal;
1595 Out.push_back("");
1596 }
1597 if (cur_ch == ',') {
1598 continue;
1599 }
1600
1601 break;
1602
1603 case InsideQuotationMarks:
1604 if (cur_ch == '\'') {
1605 cur_st = InsideSpecialCommand;
1606 continue;
1607 }
1608 break;
1609 }
1610
1611 Out.back().push_back(cur_ch);
1612 }
1613}
1614
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001615/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1616/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1617/// SubstituteSpecialCommands().
1618StrVector::const_iterator
1619SubstituteCall (StrVector::const_iterator Pos,
1620 StrVector::const_iterator End,
1621 bool IsJoin, raw_ostream& O)
1622{
1623 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001624 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001625 const std::string& CmdName = *Pos;
1626
1627 if (CmdName == ")")
1628 throw "$CALL invocation: empty argument list!";
1629
1630 O << "hooks::";
1631 O << CmdName << "(";
1632
1633
1634 bool firstIteration = true;
1635 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001636 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001637 const std::string& Arg = *Pos;
1638 assert(Arg.size() != 0);
1639
1640 if (Arg[0] == ')')
1641 break;
1642
1643 if (firstIteration)
1644 firstIteration = false;
1645 else
1646 O << ", ";
1647
1648 if (Arg == "$INFILE") {
1649 if (IsJoin)
1650 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1651 else
1652 O << "inFile.c_str()";
1653 }
1654 else {
1655 O << '"' << Arg << '"';
1656 }
1657 }
1658
1659 O << ')';
1660
1661 return Pos;
1662}
1663
1664/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1665/// function used by SubstituteSpecialCommands().
1666StrVector::const_iterator
1667SubstituteEnv (StrVector::const_iterator Pos,
1668 StrVector::const_iterator End, raw_ostream& O)
1669{
1670 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001671 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001672 const std::string& EnvName = *Pos;
1673
1674 if (EnvName == ")")
1675 throw "$ENV invocation: empty argument list!";
1676
1677 O << "checkCString(std::getenv(\"";
1678 O << EnvName;
1679 O << "\"))";
1680
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001681 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001682
1683 return Pos;
1684}
1685
1686/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1687/// handler code. Helper function used by EmitCmdLineVecFill().
1688StrVector::const_iterator
1689SubstituteSpecialCommands (StrVector::const_iterator Pos,
1690 StrVector::const_iterator End,
1691 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001692{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001693
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001694 const std::string& cmd = *Pos;
1695
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001696 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001697 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001698 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001699 }
1700 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001701 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001702 }
1703 else {
1704 throw "Unknown special command: " + cmd;
1705 }
1706
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001707 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001708 const std::string& Leftover = *Pos;
1709 assert(Leftover.at(0) == ')');
1710 if (Leftover.size() != 1)
1711 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001712
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001713 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001714}
1715
1716/// EmitCmdLineVecFill - Emit code that fills in the command line
1717/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001718void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001719 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001720 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001721 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001722 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001723
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001724 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001725 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001726
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001727 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1728
1729 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001730 assert(!StrVec[0].empty());
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001731 if (StrVec[0][0] == '$') {
1732 while (I != E && (*I)[0] != ')' )
1733 ++I;
1734
1735 // Skip the ')' symbol.
1736 ++I;
1737 }
1738 else {
1739 ++I;
1740 }
1741
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001742 bool hasINFILE = false;
1743
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001744 for (; I != E; ++I) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001745 const std::string& cmd = *I;
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001746 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001747 O.indent(IndentLevel);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001748 if (cmd.at(0) == '$') {
1749 if (cmd == "$INFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001750 hasINFILE = true;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001751 if (IsJoin) {
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001752 O << "for (PathVector::const_iterator B = inFiles.begin()"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001753 << ", E = inFiles.end();\n";
1754 O.indent(IndentLevel) << "B != E; ++B)\n";
1755 O.indent(IndentLevel + Indent1) << "vec.push_back(B->str());\n";
1756 }
1757 else {
Chris Lattner74382b72009-08-23 22:45:37 +00001758 O << "vec.push_back(inFile.str());\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001759 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001760 }
1761 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001762 O << "vec.push_back(\"\");\n";
1763 O.indent(IndentLevel) << "out_file_index = vec.size()-1;\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001764 }
1765 else {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001766 O << "vec.push_back(";
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001767 I = SubstituteSpecialCommands(I, E, IsJoin, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001768 O << ");\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001769 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001770 }
1771 else {
1772 O << "vec.push_back(\"" << cmd << "\");\n";
1773 }
1774 }
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001775 if (!hasINFILE)
1776 throw "Tool '" + ToolName + "' doesn't take any input!";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001777
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001778 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001779 if (StrVec[0][0] == '$')
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001780 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001781 else
1782 O << '"' << StrVec[0] << '"';
1783 O << ";\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001784}
1785
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001786/// EmitCmdLineVecFillCallback - A function object wrapper around
1787/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1788/// argument to EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001789class EmitCmdLineVecFillCallback {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001790 bool IsJoin;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001791 const std::string& ToolName;
1792 public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001793 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1794 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001795
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001796 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001797 raw_ostream& O) const
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001798 {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001799 EmitCmdLineVecFill(Statement, ToolName, IsJoin, IndentLevel, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001800 }
1801};
1802
1803/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1804/// implement EmitActionHandler. Emits code for
1805/// handling the (forward) and (forward_as) option properties.
1806void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001807 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001808 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001809 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001810 const std::string& Name = NewName.empty()
1811 ? ("-" + D.Name)
1812 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001813 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001814
1815 switch (D.Type) {
1816 case OptionType::Switch:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001817 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001818 break;
1819 case OptionType::Parameter:
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001820 O.indent(IndentLevel) << "vec.push_back(\"" << Name;
1821
1822 if (!D.isForwardNotSplit()) {
1823 O << "\");\n";
1824 O.indent(IndentLevel) << "vec.push_back("
1825 << D.GenVariableName() << ");\n";
1826 }
1827 else {
1828 O << "=\" + " << D.GenVariableName() << ");\n";
1829 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001830 break;
1831 case OptionType::Prefix:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001832 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\" + "
1833 << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001834 break;
1835 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001836 O.indent(IndentLevel)
1837 << "for (" << D.GenTypeDeclaration()
1838 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1839 O.indent(IndentLevel)
1840 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1841 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\" + " << "*B);\n";
1842 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001843
1844 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001845 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1846 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001847 }
1848
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001849 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001850 break;
1851 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001852 O.indent(IndentLevel)
1853 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1854 << D.GenVariableName() << ".begin(),\n";
1855 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1856 << ".end() ; B != E;) {\n";
1857 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001858
1859 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001860 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1861 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001862 }
1863
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001864 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001865 break;
1866 case OptionType::Alias:
1867 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001868 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001869 }
1870}
1871
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001872/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1873/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001874struct ActionHandlingCallbackBase
1875{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001876
1877 void onErrorDag(const DagInit& d,
1878 unsigned IndentLevel, raw_ostream& O) const
1879 {
1880 O.indent(IndentLevel)
1881 << "throw std::runtime_error(\"" <<
1882 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1883 : "Unknown error!")
1884 << "\");\n";
1885 }
1886
1887 void onWarningDag(const DagInit& d,
1888 unsigned IndentLevel, raw_ostream& O) const
1889 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001890 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001891 O.indent(IndentLevel) << "llvm::errs() << \""
1892 << InitPtrToString(d.getArg(0)) << "\";\n";
1893 }
1894
1895};
1896
1897/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1898/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001899
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001900class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001901
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001902typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1903(const DagInit&, unsigned, raw_ostream&) const;
1904
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001905class EmitActionHandlersCallback :
1906 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001907 public HandlerTable<EmitActionHandlersCallbackHandler>
1908{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001909 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001910
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001911 const OptionDescriptions& OptDescs;
1912
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001913 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1914 /// onAppendCmd and onOutputSuffix.
1915 void EmitHookInvocation(const std::string& Str,
1916 const char* BlockOpen, const char* BlockClose,
1917 unsigned IndentLevel, raw_ostream& O) const
1918 {
1919 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001920 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001921
1922 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1923 B != E; ++B) {
1924 const std::string& cmd = *B;
1925
1926 O.indent(IndentLevel) << BlockOpen;
1927
1928 if (cmd.at(0) == '$')
1929 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1930 else
1931 O << '"' << cmd << '"';
1932
1933 O << BlockClose;
1934 }
1935 }
1936
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001937 void onAppendCmd (const DagInit& Dag,
1938 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001939 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001940 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001941 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
1942 "vec.push_back(", ");\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001943 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001944
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001945 void onForward (const DagInit& Dag,
1946 unsigned IndentLevel, raw_ostream& O) const
1947 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001948 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001949 const std::string& Name = InitPtrToString(Dag.getArg(0));
1950 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1951 IndentLevel, "", O);
1952 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001953
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001954 void onForwardAs (const DagInit& Dag,
1955 unsigned IndentLevel, raw_ostream& O) const
1956 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001957 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001958 const std::string& Name = InitPtrToString(Dag.getArg(0));
1959 const std::string& NewName = InitPtrToString(Dag.getArg(1));
1960 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1961 IndentLevel, NewName, O);
1962 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001963
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001964 void onForwardValue (const DagInit& Dag,
1965 unsigned IndentLevel, raw_ostream& O) const
1966 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001967 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001968 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001969 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001970
1971 if (D.isParameter()) {
1972 O.indent(IndentLevel) << "vec.push_back("
1973 << D.GenVariableName() << ");\n";
1974 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001975 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001976 O.indent(IndentLevel) << "std::copy(" << D.GenVariableName()
1977 << ".begin(), " << D.GenVariableName()
1978 << ".end(), std::back_inserter(vec));\n";
1979 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001980 }
1981
1982 void onForwardTransformedValue (const DagInit& Dag,
1983 unsigned IndentLevel, raw_ostream& O) const
1984 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001985 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001986 const std::string& Name = InitPtrToString(Dag.getArg(0));
1987 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001988 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001989
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001990 O.indent(IndentLevel) << "vec.push_back(" << "hooks::"
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00001991 << Hook << "(" << D.GenVariableName()
1992 << (D.isParameter() ? ".c_str()" : "") << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001993 }
1994
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001995 void onOutputSuffix (const DagInit& Dag,
1996 unsigned IndentLevel, raw_ostream& O) const
1997 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001998 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001999 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
2000 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002001 }
2002
2003 void onStopCompilation (const DagInit& Dag,
2004 unsigned IndentLevel, raw_ostream& O) const
2005 {
2006 O.indent(IndentLevel) << "stop_compilation = true;\n";
2007 }
2008
2009
2010 void onUnpackValues (const DagInit& Dag,
2011 unsigned IndentLevel, raw_ostream& O) const
2012 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002013 throw "'unpack_values' is deprecated. "
2014 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002015 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002016
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002017 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002018
2019 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
2020 : OptDescs(OD)
2021 {
2022 if (!staticMembersInitialized_) {
2023 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
2024 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
2025 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
2026 AddHandler("forward", &EmitActionHandlersCallback::onForward);
2027 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002028 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
2029 AddHandler("forward_transformed_value",
2030 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002031 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
2032 AddHandler("stop_compilation",
2033 &EmitActionHandlersCallback::onStopCompilation);
2034 AddHandler("unpack_values",
2035 &EmitActionHandlersCallback::onUnpackValues);
2036
2037 staticMembersInitialized_ = true;
2038 }
2039 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002040
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002041 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002042 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002043 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002044 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002045 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002046};
2047
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002048bool IsOutFileIndexCheckRequiredStr (const Init* CmdLine) {
2049 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002050 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002051
2052 for (StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
2053 I != E; ++I) {
2054 if (*I == "$OUTFILE")
2055 return false;
2056 }
2057
2058 return true;
2059}
2060
2061class IsOutFileIndexCheckRequiredStrCallback {
2062 bool* ret_;
2063
2064public:
2065 IsOutFileIndexCheckRequiredStrCallback(bool* ret) : ret_(ret)
2066 {}
2067
2068 void operator()(const Init* CmdLine) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002069 // Ignore nested 'case' DAG.
2070 if (typeid(*CmdLine) == typeid(DagInit))
2071 return;
2072
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002073 if (IsOutFileIndexCheckRequiredStr(CmdLine))
2074 *ret_ = true;
2075 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002076
2077 void operator()(const DagInit* Test, unsigned, bool) {
2078 this->operator()(Test);
2079 }
2080 void operator()(const Init* Statement, unsigned) {
2081 this->operator()(Statement);
2082 }
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002083};
2084
2085bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
2086 bool ret = false;
2087 WalkCase(CmdLine, Id(), IsOutFileIndexCheckRequiredStrCallback(&ret));
2088 return ret;
2089}
2090
2091/// IsOutFileIndexCheckRequired - Should we emit an "out_file_index != -1" check
2092/// in EmitGenerateActionMethod() ?
2093bool IsOutFileIndexCheckRequired (Init* CmdLine) {
2094 if (typeid(*CmdLine) == typeid(StringInit))
2095 return IsOutFileIndexCheckRequiredStr(CmdLine);
2096 else
2097 return IsOutFileIndexCheckRequiredCase(CmdLine);
2098}
2099
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002100void EmitGenerateActionMethodHeader(const ToolDescription& D,
2101 bool IsJoin, raw_ostream& O)
2102{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002103 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002104 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002105 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002106 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002107
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002108 O.indent(Indent2) << "bool HasChildren,\n";
2109 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2110 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2111 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2112 O.indent(Indent1) << "{\n";
2113 O.indent(Indent2) << "std::string cmd;\n";
2114 O.indent(Indent2) << "std::vector<std::string> vec;\n";
2115 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2116 O.indent(Indent2) << "const char* output_suffix = \""
2117 << D.OutputSuffix << "\";\n";
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002118}
2119
2120// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2121// Tool::GenerateAction() method.
2122void EmitGenerateActionMethod (const ToolDescription& D,
2123 const OptionDescriptions& OptDescs,
2124 bool IsJoin, raw_ostream& O) {
2125
2126 EmitGenerateActionMethodHeader(D, IsJoin, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002127
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002128 if (!D.CmdLine)
2129 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002130
2131 bool IndexCheckRequired = IsOutFileIndexCheckRequired(D.CmdLine);
2132 O.indent(Indent2) << "int out_file_index"
2133 << (IndexCheckRequired ? " = -1" : "")
2134 << ";\n\n";
2135
2136 // Process the cmd_line property.
2137 if (typeid(*D.CmdLine) == typeid(StringInit))
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002138 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2139 else
2140 EmitCaseConstructHandler(D.CmdLine, Indent2,
2141 EmitCmdLineVecFillCallback(IsJoin, D.Name),
2142 true, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002143
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002144 // For every understood option, emit handling code.
2145 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002146 EmitCaseConstructHandler(D.Actions, Indent2,
2147 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002148 false, OptDescs, O);
2149
2150 O << '\n';
2151 O.indent(Indent2)
2152 << "std::string out_file = OutFilename("
2153 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
2154 O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002155
2156 if (IndexCheckRequired)
2157 O.indent(Indent2) << "if (out_file_index != -1)\n";
2158 O.indent(IndexCheckRequired ? Indent3 : Indent2)
2159 << "vec[out_file_index] = out_file;\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002160
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002161 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002162 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002163 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
2164 O.indent(Indent3) << "vec.insert(vec.end(), "
2165 << SinkOptionName << ".begin(), " << SinkOptionName
2166 << ".end());\n";
2167 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002168 }
2169
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002170 O.indent(Indent2) << "return Action(cmd, vec, stop_compilation, out_file);\n";
2171 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002172}
2173
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002174/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2175/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002176void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2177 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002178 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002179 if (!ToolDesc.isJoin()) {
2180 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
2181 O.indent(Indent2) << "bool HasChildren,\n";
2182 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2183 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2184 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2185 O.indent(Indent1) << "{\n";
2186 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
2187 << " is not a Join tool!\");\n";
2188 O.indent(Indent1) << "}\n\n";
2189 }
2190 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002191 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002192 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002193
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002194 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002195}
2196
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002197/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2198/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002199void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002200 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2201 O.indent(Indent2) << "return InputLanguages_;\n";
2202 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002203
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002204 if (D.OutLanguage.empty())
2205 throw "Tool " + D.Name + " has no 'out_language' property!";
2206
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002207 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2208 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2209 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002210}
2211
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002212/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002213void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002214 O.indent(Indent1) << "const char* Name() const {\n";
2215 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2216 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002217}
2218
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002219/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2220/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002221void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002222 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002223 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002224 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002225 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002226 O.indent(Indent2) << "return false;\n";
2227 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002228}
2229
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002230/// EmitWorksOnEmptyCallback - Callback used by EmitWorksOnEmptyMethod in
2231/// conjunction with EmitCaseConstructHandler.
2232void EmitWorksOnEmptyCallback (const Init* Value,
2233 unsigned IndentLevel, raw_ostream& O) {
2234 CheckBooleanConstant(Value);
2235 O.indent(IndentLevel) << "return " << Value->getAsString() << ";\n";
2236}
2237
2238/// EmitWorksOnEmptyMethod - Emit the WorksOnEmpty() method for a given Tool
2239/// class.
2240void EmitWorksOnEmptyMethod (const ToolDescription& D,
2241 const OptionDescriptions& OptDescs,
2242 raw_ostream& O)
2243{
2244 O.indent(Indent1) << "bool WorksOnEmpty() const {\n";
2245 if (D.OnEmpty == 0)
2246 O.indent(Indent2) << "return false;\n";
2247 else
2248 EmitCaseConstructHandler(D.OnEmpty, Indent2, EmitWorksOnEmptyCallback,
2249 /*EmitElseIf = */ true, OptDescs, O);
2250 O.indent(Indent1) << "}\n\n";
2251}
2252
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002253/// EmitStaticMemberDefinitions - Emit static member definitions for a
2254/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002255void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002256 if (D.InLanguage.empty())
2257 throw "Tool " + D.Name + " has no 'in_language' property!";
2258
2259 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2260 for (StrVector::const_iterator B = D.InLanguage.begin(),
2261 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002262 O << '\"' << *B << "\", ";
2263 O << "0};\n\n";
2264}
2265
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002266/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002267void EmitToolClassDefinition (const ToolDescription& D,
2268 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002269 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002270 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002271 return;
2272
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002273 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002274 O << "class " << D.Name << " : public ";
2275 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002276 O << "JoinTool";
2277 else
2278 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002279
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002280 O << " {\nprivate:\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002281 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002282
2283 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002284 EmitNameMethod(D, O);
2285 EmitInOutLanguageMethods(D, O);
2286 EmitIsJoinMethod(D, O);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002287 EmitWorksOnEmptyMethod(D, OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002288 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002289
2290 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002291 O << "};\n";
2292
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002293 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002294
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002295}
2296
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002297/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002298/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002299void EmitOptionDefinitions (const OptionDescriptions& descs,
2300 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002301 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002302{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002303 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002304
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002305 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002306 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002307 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002308 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002309
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002310 if (val.Type == OptionType::Alias) {
2311 Aliases.push_back(val);
2312 continue;
2313 }
2314
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002315 if (val.isExtern())
2316 O << "extern ";
2317
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002318 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002319 << val.GenVariableName();
2320
2321 if (val.isExtern()) {
2322 O << ";\n";
2323 continue;
2324 }
2325
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002326 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002327
2328 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2329 O << ", cl::Prefix";
2330
2331 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002332 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002333 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002334 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002335 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002336 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002337 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002338 O << ", cl::OneOrMore";
2339 }
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002340 else if (val.isOptional() && val.isList()) {
2341 O << ", cl::Optional";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002342 }
2343
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002344 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002345 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002346 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002347 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002348
2349 if (val.isCommaSeparated())
2350 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002351
2352 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002353 O << ", cl::multi_val(" << val.MultiVal << ')';
2354
2355 if (val.InitVal) {
2356 const std::string& str = val.InitVal->getAsString();
2357 O << ", cl::init(" << str << ')';
2358 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002359
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002360 if (!val.Help.empty())
2361 O << ", cl::desc(\"" << val.Help << "\")";
2362
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002363 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002364 }
2365
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002366 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002367 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002368 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002369 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002370
2371 O << val.GenTypeDeclaration() << ' '
2372 << val.GenVariableName()
2373 << "(\"" << val.Name << '\"';
2374
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002375 const OptionDescription& D = descs.FindOption(val.Help);
2376 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002377
2378 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2379 }
2380
2381 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002382 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002383 O << (HasExterns ? "extern cl" : "cl")
2384 << "::list<std::string> " << SinkOptionName
2385 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002386
2387 O << '\n';
2388}
2389
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002390/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002391/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002392
2393class EmitPreprocessOptionsCallback;
2394
2395typedef void
2396(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2397(const DagInit&, unsigned, raw_ostream&) const;
2398
2399class EmitPreprocessOptionsCallback :
2400 public ActionHandlingCallbackBase,
2401 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2402{
2403 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002404 typedef void
2405 (EmitPreprocessOptionsCallback::* HandlerImpl)
2406 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002407
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002408 const OptionDescriptions& OptDescs_;
2409
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002410 void onListOrDag(const DagInit& d, HandlerImpl h,
2411 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002412 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002413 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002414 const Init* I = d.getArg(0);
2415
2416 // If I is a list, apply h to each element.
2417 if (typeid(*I) == typeid(ListInit)) {
2418 const ListInit& L = *static_cast<const ListInit*>(I);
2419 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B)
2420 ((this)->*(h))(*B, IndentLevel, O);
2421 }
2422 // Otherwise, apply h to I.
2423 else {
2424 ((this)->*(h))(I, IndentLevel, O);
2425 }
2426 }
2427
2428 void onUnsetOptionImpl(const Init* I,
2429 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002430 {
2431 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002432 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002433
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002434 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002435 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2436 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002437 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002438 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2439 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002440 else if (OptDesc.isList()) {
2441 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2442 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002443 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002444 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002445 }
2446 }
2447
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002448 void onUnsetOption(const DagInit& d,
2449 unsigned IndentLevel, raw_ostream& O) const
2450 {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002451 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2452 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002453 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002454
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002455 void onSetOptionImpl(const DagInit& d,
2456 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002457 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002458 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002459 const Init* Value = d.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002460 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2461
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002462 if (OptDesc.isList()) {
2463 const ListInit& List = InitPtrToList(Value);
2464
2465 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2466 for (ListInit::const_iterator B = List.begin(), E = List.end();
2467 B != E; ++B) {
2468 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".push_back(\""
2469 << InitPtrToString(*B) << "\");\n";
2470 }
2471 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002472 else if (OptDesc.isSwitch()) {
2473 CheckBooleanConstant(Value);
2474 O.indent(IndentLevel) << OptDesc.GenVariableName()
2475 << " = " << Value->getAsString() << ";\n";
2476 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002477 else if (OptDesc.isParameter()) {
2478 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002479 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002480 << " = \"" << Str << "\";\n";
2481 }
2482 else {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002483 throw "Can't apply 'set_option' to alias option -" + OptName + " !";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002484 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002485 }
2486
2487 void onSetSwitch(const Init* I,
2488 unsigned IndentLevel, raw_ostream& O) const {
2489 const std::string& OptName = InitPtrToString(I);
2490 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2491
2492 if (OptDesc.isSwitch())
2493 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2494 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002495 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002496 }
2497
2498 void onSetOption(const DagInit& d,
2499 unsigned IndentLevel, raw_ostream& O) const
2500 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002501 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002502
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002503 // Two arguments: (set_option "parameter", VALUE), where VALUE can be a
2504 // boolean, a string or a string list.
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002505 if (d.getNumArgs() > 1)
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002506 this->onSetOptionImpl(d, IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002507 // One argument: (set_option "switch")
2508 // or (set_option ["switch1", "switch2", ...])
2509 else
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002510 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2511 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002512 }
2513
2514public:
2515
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002516 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002517 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002518 {
2519 if (!staticMembersInitialized_) {
2520 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2521 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2522 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002523 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002524
2525 staticMembersInitialized_ = true;
2526 }
2527 }
2528
2529 void operator()(const Init* I,
2530 unsigned IndentLevel, raw_ostream& O) const
2531 {
2532 InvokeDagInitHandler(this, I, IndentLevel, O);
2533 }
2534
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002535};
2536
2537/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2538void EmitPreprocessOptions (const RecordKeeper& Records,
2539 const OptionDescriptions& OptDecs, raw_ostream& O)
2540{
2541 O << "void PreprocessOptionsLocal() {\n";
2542
2543 const RecordVector& OptionPreprocessors =
2544 Records.getAllDerivedDefinitions("OptionPreprocessor");
2545
2546 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2547 E = OptionPreprocessors.end(); B!=E; ++B) {
2548 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002549 EmitCaseConstructHandler(Case, Indent1,
2550 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002551 false, OptDecs, O);
2552 }
2553
2554 O << "}\n\n";
2555}
2556
2557/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002558void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002559{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002560 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002561
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002562 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002563 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002564
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002565 // It is allowed for a plugin to have no language map.
2566 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002567
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002568 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2569 if (!LangsToSuffixesList)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00002570 throw "Error in the language map definition!";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002571
2572 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002573 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002574
2575 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2576 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2577
2578 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002579 O.indent(Indent1) << "langMap[\""
2580 << InitPtrToString(Suffixes->getElement(i))
2581 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002582 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002583 }
2584
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002585 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002586}
2587
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002588/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2589/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002590void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002591 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002592 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002593 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002594
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002595 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002596 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002597 }
2598 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002599 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002600 }
2601 else if (OpName == "error") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002602 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002603 O.indent(IndentLevel) << "throw std::runtime_error(\""
2604 << InitPtrToString(d.getArg(0))
2605 << "\");\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002606 return;
2607 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002608 else {
2609 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002610 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002611 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002612
2613 if (d.getNumArgs() > 0)
2614 O << InitPtrToInt(d.getArg(0)) << ";\n";
2615 else
2616 O << "2;\n";
2617
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002618}
2619
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002620/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002621void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002622 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002623 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002624
2625 // Class constructor.
2626 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002627 << "public:\n";
2628 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2629 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002630
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002631 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002632 O.indent(Indent1)
2633 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2634 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002635
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002636 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002637 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002638
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002639 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002640 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002641}
2642
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002643/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002644void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002645 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002646 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002647 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002648 for (RecordVector::const_iterator B = EdgeVector.begin(),
2649 E = EdgeVector.end(); B != E; ++B) {
2650 const Record* Edge = *B;
2651 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002652 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002653
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002654 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002655 EmitEdgeClass(i, NodeB, &Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002656 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002657 }
2658}
2659
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002660/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002661/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002662void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002663 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002664 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002665{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002666 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002667
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002668 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2669 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002670 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002671
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002672 O << '\n';
2673
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002674 // Insert edges.
2675
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002676 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002677 for (RecordVector::const_iterator B = EdgeVector.begin(),
2678 E = EdgeVector.end(); B != E; ++B) {
2679 const Record* Edge = *B;
2680 const std::string& NodeA = Edge->getValueAsString("a");
2681 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002682 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002683
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002684 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002685
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002686 if (IsDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002687 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002688 else
2689 O << "new Edge" << i << "()";
2690
2691 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002692 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002693 }
2694
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002695 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002696}
2697
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002698/// HookInfo - Information about the hook type and number of arguments.
2699struct HookInfo {
2700
2701 // A hook can either have a single parameter of type std::vector<std::string>,
2702 // or NumArgs parameters of type const char*.
2703 enum HookType { ListHook, ArgHook };
2704
2705 HookType Type;
2706 unsigned NumArgs;
2707
2708 HookInfo() : Type(ArgHook), NumArgs(1)
2709 {}
2710
2711 HookInfo(HookType T) : Type(T), NumArgs(1)
2712 {}
2713
2714 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2715 {}
2716};
2717
2718typedef llvm::StringMap<HookInfo> HookInfoMap;
2719
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002720/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002721/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002722/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002723class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002724 HookInfoMap& HookNames_;
2725 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002726public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002727 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2728 : HookNames_(HookNames), OptDescs_(OptDescs)
2729 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002730
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002731 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002732 const std::string& Name = GetOperatorName(Dag);
2733
2734 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002735 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002736 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2737 const std::string& HookName = InitPtrToString(Dag.getArg(1));
2738 const OptionDescription& D = OptDescs_.FindOption(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002739
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002740 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2741 : HookInfo::ArgHook);
2742 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002743 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002744 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002745 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2746 }
2747 }
2748
2749 void onCmdLine(const std::string& Cmd) {
2750 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002751 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002752
2753 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2754 B != E; ++B) {
2755 const std::string& cmd = *B;
2756
2757 if (cmd == "$CALL") {
2758 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002759 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002760 const std::string& HookName = *B;
2761
2762 if (HookName.at(0) == ')')
2763 throw "$CALL invoked with no arguments!";
2764
2765 while (++B != E && B->at(0) != ')') {
2766 ++NumArgs;
2767 }
2768
2769 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2770
2771 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2772 H->second.Type != HookInfo::ArgHook)
2773 throw "Overloading of hooks is not allowed. Overloaded hook: "
2774 + HookName;
2775 else
2776 HookNames_[HookName] = HookInfo(NumArgs);
2777 }
2778 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002779 }
2780
2781 void operator()(const Init* Arg) {
2782
2783 // We're invoked on an action (either a dag or a dag list).
2784 if (typeid(*Arg) == typeid(DagInit)) {
2785 const DagInit& Dag = InitPtrToDag(Arg);
2786 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002787 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002788 }
2789 else if (typeid(*Arg) == typeid(ListInit)) {
2790 const ListInit& List = InitPtrToList(Arg);
2791 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2792 ++B) {
2793 const DagInit& Dag = InitPtrToDag(*B);
2794 this->onAction(Dag);
2795 }
2796 return;
2797 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002798
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002799 // We're invoked on a command line.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002800 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002801 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002802
2803 void operator()(const DagInit* Test, unsigned, bool) {
2804 this->operator()(Test);
2805 }
2806 void operator()(const Init* Statement, unsigned) {
2807 this->operator()(Statement);
2808 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002809};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002810
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002811/// FillInHookNames - Actually extract the hook names from all command
2812/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002813void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002814 const OptionDescriptions& OptDescs,
2815 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002816{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002817 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002818 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2819 E = ToolDescs.end(); B != E; ++B) {
2820 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002821
2822 // Look for 'forward_transformed_value' in 'actions'.
2823 if (D.Actions)
2824 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2825
2826 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002827 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002828 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002829 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002830 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002831 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002832 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002833 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002834 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002835 }
2836}
2837
2838/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2839/// property records and emit hook function declaration for each
2840/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002841void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2842 const OptionDescriptions& OptDescs, raw_ostream& O) {
2843 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002844
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002845 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002846 if (HookNames.empty())
2847 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002848
2849 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002850 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002851 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002852 const char* HookName = B->first();
2853 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002854
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002855 O.indent(Indent1) << "std::string " << HookName << "(";
2856
2857 if (Info.Type == HookInfo::ArgHook) {
2858 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2859 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2860 }
2861 }
2862 else {
2863 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002864 }
2865
2866 O <<");\n";
2867 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002868 O << "}\n\n";
2869}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002870
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002871/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002872void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002873 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2874 O.indent(Indent1) << "int Priority() const { return "
2875 << Priority << "; }\n\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002876 O.indent(Indent1) << "void PreprocessOptions() const\n";
2877 O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002878 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2879 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2880 O.indent(Indent1)
2881 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2882 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2883 << "};\n\n"
2884 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002885}
2886
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002887/// EmitIncludes - Emit necessary #include directives and some
2888/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002889void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002890 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2891 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002892 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002893 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2894 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002895
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002896 << "#include \"llvm/Support/CommandLine.h\"\n"
2897 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002898
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002899 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002900 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002901 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002902 << "#include <stdexcept>\n\n"
2903
2904 << "using namespace llvm;\n"
2905 << "using namespace llvmc;\n\n"
2906
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002907 << "extern cl::opt<std::string> OutputFilename;\n\n"
2908
2909 << "inline const char* checkCString(const char* s)\n"
2910 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002911}
2912
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002913
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002914/// PluginData - Holds all information about a plugin.
2915struct PluginData {
2916 OptionDescriptions OptDescs;
2917 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002918 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002919 ToolDescriptions ToolDescs;
2920 RecordVector Edges;
2921 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002922};
2923
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002924/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002925/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002926bool HasSink(const ToolDescriptions& ToolDescs) {
2927 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2928 E = ToolDescs.end(); B != E; ++B)
2929 if ((*B)->isSink())
2930 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002931
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002932 return false;
2933}
2934
2935/// HasExterns - Go through the list of option descriptions and check
2936/// if there are any external options.
2937bool HasExterns(const OptionDescriptions& OptDescs) {
2938 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2939 E = OptDescs.end(); B != E; ++B)
2940 if (B->second.isExtern())
2941 return true;
2942
2943 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002944}
2945
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002946/// CollectPluginData - Collect tool and option properties,
2947/// compilation graph edges and plugin priority from the parse tree.
2948void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2949 // Collect option properties.
2950 const RecordVector& OptionLists =
2951 Records.getAllDerivedDefinitions("OptionList");
2952 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2953 Data.OptDescs);
2954
2955 // Collect tool properties.
2956 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2957 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2958 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002959 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002960
2961 // Collect compilation graph edges.
2962 const RecordVector& CompilationGraphs =
2963 Records.getAllDerivedDefinitions("CompilationGraph");
2964 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2965 Data.Edges);
2966
2967 // Calculate the priority of this plugin.
2968 const RecordVector& Priorities =
2969 Records.getAllDerivedDefinitions("PluginPriority");
2970 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002971}
2972
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002973/// CheckPluginData - Perform some sanity checks on the collected data.
2974void CheckPluginData(PluginData& Data) {
2975 // Filter out all tools not mentioned in the compilation graph.
2976 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002977
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002978 // Typecheck the compilation graph.
2979 TypecheckGraph(Data.Edges, Data.ToolDescs);
2980
2981 // Check that there are no options without side effects (specified
2982 // only in the OptionList).
2983 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002984}
2985
Daniel Dunbar1a551802009-07-03 00:10:29 +00002986void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002987 // Emit file header.
2988 EmitIncludes(O);
2989
2990 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002991 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002992
2993 // Emit hook declarations.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002994 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002995
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002996 O << "namespace {\n\n";
2997
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002998 // Emit PreprocessOptionsLocal() function.
2999 EmitPreprocessOptions(Records, Data.OptDescs, O);
3000
3001 // Emit PopulateLanguageMapLocal() function
3002 // (language map maps from file extensions to language names).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003003 EmitPopulateLanguageMap(Records, O);
3004
3005 // Emit Tool classes.
3006 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
3007 E = Data.ToolDescs.end(); B!=E; ++B)
3008 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
3009
3010 // Emit Edge# classes.
3011 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
3012
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00003013 // Emit PopulateCompilationGraphLocal() function.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003014 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
3015
3016 // Emit code for plugin registration.
3017 EmitRegisterPlugin(Data.Priority, O);
3018
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003019 O << "} // End anonymous namespace.\n\n";
3020
3021 // Force linkage magic.
3022 O << "namespace llvmc {\n";
3023 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
3024 O << "}\n";
3025
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003026 // EOF
3027}
3028
3029
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003030// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00003031}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003032
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003033/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00003034void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003035 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003036 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003037
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003038 CollectPluginData(Records, Data);
3039 CheckPluginData(Data);
3040
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +00003041 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003042 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003043
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003044 } catch (std::exception& Error) {
3045 throw Error.what() + std::string(" - usually this means a syntax error.");
3046 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003047}