blob: fac84a851f723bd136f774a383ab01fbdb672438 [file] [log] [blame]
Mikhail Glushenkovfb37f392008-05-30 06:20:54 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000010// This tablegen backend is responsible for emitting LLVMC configuration code.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkovecbdcf22008-05-06 18:09:29 +000014#include "LLVMCConfigurationEmitter.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000015#include "Record.h"
16
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000018#include "llvm/ADT/StringMap.h"
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000019#include "llvm/ADT/StringSet.h"
Mikhail Glushenkove0b65702009-12-23 12:49:30 +000020
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000021#include <algorithm>
22#include <cassert>
23#include <functional>
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +000024#include <stdexcept>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000025#include <string>
Chris Lattner32a9e7a2008-06-04 04:46:14 +000026#include <typeinfo>
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +000027
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000028using namespace llvm;
29
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000030namespace {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
32//===----------------------------------------------------------------------===//
33/// Typedefs
34
35typedef std::vector<Record*> RecordVector;
36typedef std::vector<std::string> StrVector;
37
38//===----------------------------------------------------------------------===//
39/// Constants
40
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +000041// Indentation.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000042const unsigned TabWidth = 4;
43const unsigned Indent1 = TabWidth*1;
44const unsigned Indent2 = TabWidth*2;
45const unsigned Indent3 = TabWidth*3;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +000046const unsigned Indent4 = TabWidth*4;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000047
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000048// Default help string.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000049const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000050
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000051// Name for the "sink" option.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000052const char * const SinkOptionName = "AutoGeneratedSinkOption";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000053
54//===----------------------------------------------------------------------===//
55/// Helper functions
56
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000057/// Id - An 'identity' function object.
58struct Id {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000059 template<typename T0>
60 void operator()(const T0&) const {
61 }
62 template<typename T0, typename T1>
63 void operator()(const T0&, const T1&) const {
64 }
65 template<typename T0, typename T1, typename T2>
66 void operator()(const T0&, const T1&, const T2&) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000067 }
68};
69
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000070int InitPtrToInt(const Init* ptr) {
71 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000072 return val.getValue();
73}
74
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000075const std::string& InitPtrToString(const Init* ptr) {
76 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
77 return val.getValue();
78}
79
80const ListInit& InitPtrToList(const Init* ptr) {
81 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
82 return val;
83}
84
85const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000086 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000087 return val;
88}
89
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000090const std::string GetOperatorName(const DagInit& D) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +000091 return D.getOperator()->getAsString();
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000092}
93
Mikhail Glushenkove0b65702009-12-23 12:49:30 +000094/// CheckBooleanConstant - Check that the provided value is a boolean constant.
95void CheckBooleanConstant(const Init* I) {
96 const DefInit& val = dynamic_cast<const DefInit&>(*I);
97 const std::string& str = val.getAsString();
98
99 if (str != "true" && str != "false") {
100 throw "Incorrect boolean value: '" + str +
101 "': must be either 'true' or 'false'";
102 }
103}
104
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000105// CheckNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +0000106// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000107void CheckNumberOfArguments (const DagInit& d, unsigned minArgs) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000108 if (d.getNumArgs() < minArgs)
109 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +0000110}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000111
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000112// IsDagEmpty - is this DAG marked with an empty marker?
113bool IsDagEmpty (const DagInit& d) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000114 return GetOperatorName(d) == "empty_dag_marker";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000115}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000116
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000117// EscapeVariableName - Escape commas and other symbols not allowed
118// in the C++ variable names. Makes it possible to use options named
119// like "Wa," (useful for prefix options).
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000120std::string EscapeVariableName (const std::string& Var) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000121 std::string ret;
122 for (unsigned i = 0; i != Var.size(); ++i) {
123 char cur_char = Var[i];
124 if (cur_char == ',') {
125 ret += "_comma_";
126 }
127 else if (cur_char == '+') {
128 ret += "_plus_";
129 }
130 else if (cur_char == '-') {
131 ret += "_dash_";
132 }
133 else {
134 ret.push_back(cur_char);
135 }
136 }
137 return ret;
138}
139
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000140/// EscapeQuotes - Replace '"' with '\"'.
141std::string EscapeQuotes (const std::string& Var) {
142 std::string ret;
143 for (unsigned i = 0; i != Var.size(); ++i) {
144 char cur_char = Var[i];
145 if (cur_char == '"') {
146 ret += "\\\"";
147 }
148 else {
149 ret.push_back(cur_char);
150 }
151 }
152 return ret;
153}
154
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000155/// OneOf - Does the input string contain this character?
156bool OneOf(const char* lst, char c) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000157 while (*lst) {
158 if (*lst++ == c)
159 return true;
160 }
161 return false;
162}
163
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000164template <class I, class S>
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000165void CheckedIncrement(I& P, I E, S ErrorString) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000166 ++P;
167 if (P == E)
168 throw ErrorString;
169}
170
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000171// apply is needed because C++'s syntax doesn't let us construct a function
172// object and call it in the same statement.
173template<typename F, typename T0>
174void apply(F Fun, T0& Arg0) {
175 return Fun(Arg0);
176}
177
178template<typename F, typename T0, typename T1>
179void apply(F Fun, T0& Arg0, T1& Arg1) {
180 return Fun(Arg0, Arg1);
181}
182
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000183//===----------------------------------------------------------------------===//
184/// Back-end specific code
185
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000186
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000187/// OptionType - One of six different option types. See the
188/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000189namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000190
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000191 enum OptionType { Alias, Switch, Parameter, ParameterList,
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000192 Prefix, PrefixList};
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000193
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000194 bool IsAlias(OptionType t) {
195 return (t == Alias);
196 }
197
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000198 bool IsList (OptionType t) {
199 return (t == ParameterList || t == PrefixList);
200 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000201
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000202 bool IsSwitch (OptionType t) {
203 return (t == Switch);
204 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000205
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000206 bool IsParameter (OptionType t) {
207 return (t == Parameter || t == Prefix);
208 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000209
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000210}
211
212OptionType::OptionType stringToOptionType(const std::string& T) {
213 if (T == "alias_option")
214 return OptionType::Alias;
215 else if (T == "switch_option")
216 return OptionType::Switch;
217 else if (T == "parameter_option")
218 return OptionType::Parameter;
219 else if (T == "parameter_list_option")
220 return OptionType::ParameterList;
221 else if (T == "prefix_option")
222 return OptionType::Prefix;
223 else if (T == "prefix_list_option")
224 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000225 else
226 throw "Unknown option type: " + T + '!';
227}
228
229namespace OptionDescriptionFlags {
230 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000231 ReallyHidden = 0x4, Extern = 0x8,
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000232 OneOrMore = 0x10, Optional = 0x20,
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000233 CommaSeparated = 0x40, ForwardNotSplit = 0x80 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000234}
235
236/// OptionDescription - Represents data contained in a single
237/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000238struct OptionDescription {
239 OptionType::OptionType Type;
240 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000241 unsigned Flags;
242 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000243 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000244 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000245
246 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000247 const std::string& n = "",
248 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000249 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000250 {}
251
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000252 /// GenTypeDeclaration - Returns the C++ variable type of this
253 /// option.
254 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000255
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000256 /// GenVariableName - Returns the variable name used in the
257 /// generated C++ code.
258 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000259
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000260 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000261 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000262
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000263 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000264
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000265 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000266
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000267 bool isMultiVal() const;
268
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000269 bool isCommaSeparated() const;
270 void setCommaSeparated();
271
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000272 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000273 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000274
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000275 bool isForwardNotSplit() const;
276 void setForwardNotSplit();
277
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000278 bool isRequired() const;
279 void setRequired();
280
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000281 bool isOneOrMore() const;
282 void setOneOrMore();
283
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000284 bool isOptional() const;
285 void setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000286
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000287 bool isHidden() const;
288 void setHidden();
289
290 bool isReallyHidden() const;
291 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000292
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000293 bool isSwitch() const
294 { return OptionType::IsSwitch(this->Type); }
295
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000296 bool isParameter() const
297 { return OptionType::IsParameter(this->Type); }
298
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000299 bool isList() const
300 { return OptionType::IsList(this->Type); }
301
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000302};
303
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000304void OptionDescription::Merge (const OptionDescription& other)
305{
306 if (other.Type != Type)
307 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000308
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000309 if (Help == other.Help || Help == DefaultHelpString)
310 Help = other.Help;
311 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000312 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000313 " defined for option " + Name + "\n";
314 }
315
316 Flags |= other.Flags;
317}
318
319bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000320 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000321}
322
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000323bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000324 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000325}
326
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000327bool OptionDescription::isCommaSeparated() const {
328 return Flags & OptionDescriptionFlags::CommaSeparated;
329}
330void OptionDescription::setCommaSeparated() {
331 Flags |= OptionDescriptionFlags::CommaSeparated;
332}
333
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000334bool OptionDescription::isForwardNotSplit() const {
335 return Flags & OptionDescriptionFlags::ForwardNotSplit;
336}
337void OptionDescription::setForwardNotSplit() {
338 Flags |= OptionDescriptionFlags::ForwardNotSplit;
339}
340
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000341bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000342 return Flags & OptionDescriptionFlags::Extern;
343}
344void OptionDescription::setExtern() {
345 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000346}
347
348bool OptionDescription::isRequired() const {
349 return Flags & OptionDescriptionFlags::Required;
350}
351void OptionDescription::setRequired() {
352 Flags |= OptionDescriptionFlags::Required;
353}
354
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000355bool OptionDescription::isOneOrMore() const {
356 return Flags & OptionDescriptionFlags::OneOrMore;
357}
358void OptionDescription::setOneOrMore() {
359 Flags |= OptionDescriptionFlags::OneOrMore;
360}
361
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000362bool OptionDescription::isOptional() const {
363 return Flags & OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000364}
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000365void OptionDescription::setOptional() {
366 Flags |= OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000367}
368
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000369bool OptionDescription::isHidden() const {
370 return Flags & OptionDescriptionFlags::Hidden;
371}
372void OptionDescription::setHidden() {
373 Flags |= OptionDescriptionFlags::Hidden;
374}
375
376bool OptionDescription::isReallyHidden() const {
377 return Flags & OptionDescriptionFlags::ReallyHidden;
378}
379void OptionDescription::setReallyHidden() {
380 Flags |= OptionDescriptionFlags::ReallyHidden;
381}
382
383const char* OptionDescription::GenTypeDeclaration() const {
384 switch (Type) {
385 case OptionType::Alias:
386 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000387 case OptionType::PrefixList:
388 case OptionType::ParameterList:
389 return "cl::list<std::string>";
390 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000391 return "cl::opt<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000392 case OptionType::Parameter:
393 case OptionType::Prefix:
394 default:
395 return "cl::opt<std::string>";
396 }
397}
398
399std::string OptionDescription::GenVariableName() const {
400 const std::string& EscapedName = EscapeVariableName(Name);
401 switch (Type) {
402 case OptionType::Alias:
403 return "AutoGeneratedAlias_" + EscapedName;
404 case OptionType::PrefixList:
405 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000406 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000407 case OptionType::Switch:
408 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000409 case OptionType::Prefix:
410 case OptionType::Parameter:
411 default:
412 return "AutoGeneratedParameter_" + EscapedName;
413 }
414}
415
416/// OptionDescriptions - An OptionDescription array plus some helper
417/// functions.
418class OptionDescriptions {
419 typedef StringMap<OptionDescription> container_type;
420
421 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000422 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000423
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000424public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000425 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000426 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000427
428 // Wrappers for FindOption that throw an exception in case the option has a
429 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000430 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000431 const OptionDescription& FindParameter(const std::string& OptName) const;
432 const OptionDescription& FindList(const std::string& OptName) const;
433 const OptionDescription&
434 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000435
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000436 /// insertDescription - Insert new OptionDescription into
437 /// OptionDescriptions list
438 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000439
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000440 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000441 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000442 const_iterator begin() const { return Descriptions.begin(); }
443 const_iterator end() const { return Descriptions.end(); }
444};
445
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000446const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000447OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000448 const_iterator I = Descriptions.find(OptName);
449 if (I != Descriptions.end())
450 return I->second;
451 else
452 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000453}
454
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000455const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000456OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000457 const OptionDescription& OptDesc = this->FindOption(OptName);
458 if (!OptDesc.isSwitch())
459 throw OptName + ": incorrect option type - should be a switch!";
460 return OptDesc;
461}
462
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000463const OptionDescription&
464OptionDescriptions::FindList(const std::string& OptName) const {
465 const OptionDescription& OptDesc = this->FindOption(OptName);
466 if (!OptDesc.isList())
467 throw OptName + ": incorrect option type - should be a list!";
468 return OptDesc;
469}
470
471const OptionDescription&
472OptionDescriptions::FindParameter(const std::string& OptName) const {
473 const OptionDescription& OptDesc = this->FindOption(OptName);
474 if (!OptDesc.isParameter())
475 throw OptName + ": incorrect option type - should be a parameter!";
476 return OptDesc;
477}
478
479const OptionDescription&
480OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
481 const OptionDescription& OptDesc = this->FindOption(OptName);
482 if (!OptDesc.isList() && !OptDesc.isParameter())
483 throw OptName
484 + ": incorrect option type - should be a list or parameter!";
485 return OptDesc;
486}
487
488void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000489 container_type::iterator I = Descriptions.find(o.Name);
490 if (I != Descriptions.end()) {
491 OptionDescription& D = I->second;
492 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000493 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000494 else {
495 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000496 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000497}
498
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000499/// HandlerTable - A base class for function objects implemented as
500/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000501template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000502class HandlerTable {
503protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000504 // Implementation details.
505
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000506 /// HandlerMap - A map from property names to property handlers
507 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000508
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000509 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000510 static bool staticMembersInitialized_;
511
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000512public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000513
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000514 Handler GetHandler (const std::string& HandlerName) const {
515 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000516
517 if (method != Handlers_.end()) {
518 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000519 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000520 }
521 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000522 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000523 }
524 }
525
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000526 void AddHandler(const char* Property, Handler H) {
527 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000528 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000529
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000530};
531
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000532template <class Handler, class FunctionObject>
533Handler GetHandler(FunctionObject* Obj, const DagInit& Dag) {
534 const std::string& HandlerName = GetOperatorName(Dag);
535 return Obj->GetHandler(HandlerName);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000536}
537
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000538template <class FunctionObject>
539void InvokeDagInitHandler(FunctionObject* Obj, Init* I) {
540 typedef void (FunctionObject::*Handler) (const DagInit&);
541
542 const DagInit& Dag = InitPtrToDag(I);
543 Handler h = GetHandler<Handler>(Obj, Dag);
544
545 ((Obj)->*(h))(Dag);
546}
547
548template <class FunctionObject>
549void InvokeDagInitHandler(const FunctionObject* const Obj,
550 const Init* I, unsigned IndentLevel, raw_ostream& O)
551{
552 typedef void (FunctionObject::*Handler)
553 (const DagInit&, unsigned IndentLevel, raw_ostream& O) const;
554
555 const DagInit& Dag = InitPtrToDag(I);
556 Handler h = GetHandler<Handler>(Obj, Dag);
557
558 ((Obj)->*(h))(Dag, IndentLevel, O);
559}
560
561
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000562template <typename H>
563typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
564
565template <typename H>
566bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000567
568
569/// CollectOptionProperties - Function object for iterating over an
570/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000571class CollectOptionProperties;
572typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000573(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000574
575class CollectOptionProperties
576: public HandlerTable<CollectOptionPropertiesHandler>
577{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000578private:
579
580 /// optDescs_ - OptionDescriptions table. This is where the
581 /// information is stored.
582 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000583
584public:
585
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000586 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000587 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000588 {
589 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000590 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000591 AddHandler("help", &CollectOptionProperties::onHelp);
592 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000593 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000594 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
595 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000596 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
597 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000598 AddHandler("optional", &CollectOptionProperties::onOptional);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000599 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000600 AddHandler("forward_not_split",
601 &CollectOptionProperties::onForwardNotSplit);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000602
603 staticMembersInitialized_ = true;
604 }
605 }
606
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000607 /// operator() - Just forwards to the corresponding property
608 /// handler.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000609 void operator() (Init* I) {
610 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000611 }
612
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000613private:
614
615 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000616 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000617
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000618 void onExtern (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000619 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000620 optDesc_.setExtern();
621 }
622
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000623 void onHelp (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000624 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000625 optDesc_.Help = EscapeQuotes(InitPtrToString(d.getArg(0)));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000626 }
627
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000628 void onHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000629 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000630 optDesc_.setHidden();
631 }
632
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000633 void onReallyHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000634 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000635 optDesc_.setReallyHidden();
636 }
637
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000638 void onCommaSeparated (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000639 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000640 if (!optDesc_.isList())
641 throw "'comma_separated' is valid only on list options!";
642 optDesc_.setCommaSeparated();
643 }
644
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000645 void onForwardNotSplit (const DagInit& d) {
646 CheckNumberOfArguments(d, 0);
647 if (!optDesc_.isParameter())
648 throw "'forward_not_split' is valid only for parameter options!";
649 optDesc_.setForwardNotSplit();
650 }
651
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000652 void onRequired (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000653 CheckNumberOfArguments(d, 0);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000654 if (optDesc_.isOneOrMore() || optDesc_.isOptional())
655 throw "Only one of (required), (optional) or "
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000656 "(one_or_more) properties is allowed!";
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000657 optDesc_.setRequired();
658 }
659
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000660 void onInit (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000661 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000662 Init* i = d.getArg(0);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000663 const std::string& str = i->getAsString();
664
665 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
666 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
667
668 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000669 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000670
671 optDesc_.InitVal = i;
672 }
673
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000674 void onOneOrMore (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000675 CheckNumberOfArguments(d, 0);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000676 if (optDesc_.isRequired() || optDesc_.isOptional())
677 throw "Only one of (required), (optional) or "
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000678 "(one_or_more) properties is allowed!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000679 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000680 llvm::errs() << "Warning: specifying the 'one_or_more' property "
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000681 "on a non-list option will have no effect.\n";
682 optDesc_.setOneOrMore();
683 }
684
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000685 void onOptional (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000686 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000687 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000688 throw "Only one of (required), (optional) or "
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000689 "(one_or_more) properties is allowed!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000690 if (!OptionType::IsList(optDesc_.Type))
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000691 llvm::errs() << "Warning: specifying the 'optional' property"
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000692 "on a non-list option will have no effect.\n";
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000693 optDesc_.setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000694 }
695
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000696 void onMultiVal (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000697 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000698 int val = InitPtrToInt(d.getArg(0));
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000699 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000700 throw "Error in the 'multi_val' property: "
701 "the value must be greater than 1!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000702 if (!OptionType::IsList(optDesc_.Type))
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000703 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000704 optDesc_.MultiVal = val;
705 }
706
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000707};
708
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000709/// AddOption - A function object that is applied to every option
710/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000711class AddOption {
712private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000713 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000714
715public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000716 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000717 {}
718
719 void operator()(const Init* i) {
720 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000721 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000722
723 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000724 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000725 const std::string& Name = InitPtrToString(d.getArg(0));
726
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000727 OptionDescription OD(Type, Name);
728
729 if (!OD.isExtern())
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000730 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000731
732 if (OD.isAlias()) {
733 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000734 OD.Help = InitPtrToString(d.getArg(1));
735 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000736 else if (!OD.isExtern()) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000737 processOptionProperties(d, OD);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000738 }
739 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000740 }
741
742private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000743 /// processOptionProperties - Go through the list of option
744 /// properties and call a corresponding handler for each.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000745 static void processOptionProperties (const DagInit& d, OptionDescription& o) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000746 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000747 DagInit::const_arg_iterator B = d.arg_begin();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000748 // Skip the first argument: it's always the option name.
749 ++B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000750 std::for_each(B, d.arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000751 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000752
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000753};
754
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000755/// CollectOptionDescriptions - Collects option properties from all
756/// OptionLists.
757void CollectOptionDescriptions (RecordVector::const_iterator B,
758 RecordVector::const_iterator E,
759 OptionDescriptions& OptDescs)
760{
761 // For every OptionList:
762 for (; B!=E; ++B) {
763 RecordVector::value_type T = *B;
764 // Throws an exception if the value does not exist.
765 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000766
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000767 // For every option description in this list:
768 // collect the information and
769 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
770 }
771}
772
773// Tool information record
774
775namespace ToolFlags {
776 enum ToolFlags { Join = 0x1, Sink = 0x2 };
777}
778
779struct ToolDescription : public RefCountedBase<ToolDescription> {
780 std::string Name;
781 Init* CmdLine;
782 Init* Actions;
783 StrVector InLanguage;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000784 std::string InFileOption;
785 std::string OutFileOption;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000786 std::string OutLanguage;
787 std::string OutputSuffix;
788 unsigned Flags;
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000789 const Init* OnEmpty;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000790
791 // Various boolean properties
792 void setSink() { Flags |= ToolFlags::Sink; }
793 bool isSink() const { return Flags & ToolFlags::Sink; }
794 void setJoin() { Flags |= ToolFlags::Join; }
795 bool isJoin() const { return Flags & ToolFlags::Join; }
796
797 // Default ctor here is needed because StringMap can only store
798 // DefaultConstructible objects
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000799 ToolDescription ()
800 : CmdLine(0), Actions(0), OutFileOption("-o"),
801 Flags(0), OnEmpty(0)
802 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000803 ToolDescription (const std::string& n)
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000804 : Name(n), CmdLine(0), Actions(0), OutFileOption("-o"),
805 Flags(0), OnEmpty(0)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000806 {}
807};
808
809/// ToolDescriptions - A list of Tool information records.
810typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
811
812
813/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000814/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000815
816class CollectToolProperties;
817typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000818(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000819
820class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
821{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000822private:
823
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000824 /// toolDesc_ - Properties of the current Tool. This is where the
825 /// information is stored.
826 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000827
828public:
829
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000830 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000831 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000832 {
833 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000834
835 AddHandler("actions", &CollectToolProperties::onActions);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000836 AddHandler("command", &CollectToolProperties::onCommand);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000837 AddHandler("in_language", &CollectToolProperties::onInLanguage);
838 AddHandler("join", &CollectToolProperties::onJoin);
839 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000840
841 AddHandler("out_file_option", &CollectToolProperties::onOutFileOption);
842 AddHandler("in_file_option", &CollectToolProperties::onInFileOption);
843
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000844 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
845 AddHandler("sink", &CollectToolProperties::onSink);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000846 AddHandler("works_on_empty", &CollectToolProperties::onWorksOnEmpty);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000847
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000848 staticMembersInitialized_ = true;
849 }
850 }
851
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000852 void operator() (Init* I) {
853 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000854 }
855
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000856private:
857
858 /// Property handlers --
859 /// Functions that extract information about tool properties from
860 /// DAG representation.
861
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000862 void onActions (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000863 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000864 Init* Case = d.getArg(0);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000865 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000866 GetOperatorName(static_cast<DagInit&>(*Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000867 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000868 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000869 }
870
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000871 void onCommand (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000872 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000873 toolDesc_.CmdLine = d.getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000874 }
875
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000876 void onInLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000877 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000878 Init* arg = d.getArg(0);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000879
880 // Find out the argument's type.
881 if (typeid(*arg) == typeid(StringInit)) {
882 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000883 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000884 }
885 else {
886 // It's a list.
887 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000888 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000889
890 // Copy strings to the output vector.
891 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
892 B != E; ++B) {
893 out.push_back(InitPtrToString(*B));
894 }
895
896 // Remove duplicates.
897 std::sort(out.begin(), out.end());
898 StrVector::iterator newE = std::unique(out.begin(), out.end());
899 out.erase(newE, out.end());
900 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000901 }
902
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000903 void onJoin (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000904 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000905 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000906 }
907
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000908 void onOutLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000909 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000910 toolDesc_.OutLanguage = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000911 }
912
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000913 void onOutFileOption (const DagInit& d) {
914 CheckNumberOfArguments(d, 1);
915 toolDesc_.OutFileOption = InitPtrToString(d.getArg(0));
916 }
917
918 void onInFileOption (const DagInit& d) {
919 CheckNumberOfArguments(d, 1);
920 toolDesc_.InFileOption = InitPtrToString(d.getArg(0));
921 }
922
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000923 void onOutputSuffix (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000924 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000925 toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000926 }
927
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000928 void onSink (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000929 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000930 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000931 }
932
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000933 void onWorksOnEmpty (const DagInit& d) {
934 toolDesc_.OnEmpty = d.getArg(0);
935 }
936
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000937};
938
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000939/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000940/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000941/// CollectToolProperties function object).
942void CollectToolDescriptions (RecordVector::const_iterator B,
943 RecordVector::const_iterator E,
944 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000945{
946 // Iterate over a properties list of every Tool definition
947 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000948 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000949 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000950 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000951
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000952 IntrusiveRefCntPtr<ToolDescription>
953 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000954
955 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000956 CollectToolProperties(*ToolDesc));
957 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000958 }
959}
960
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000961/// FillInEdgeVector - Merge all compilation graph definitions into
962/// one single edge list.
963void FillInEdgeVector(RecordVector::const_iterator B,
964 RecordVector::const_iterator E, RecordVector& Out) {
965 for (; B != E; ++B) {
966 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000967
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000968 for (unsigned i = 0; i < edges->size(); ++i)
969 Out.push_back(edges->getElementAsRecord(i));
970 }
971}
972
973/// CalculatePriority - Calculate the priority of this plugin.
974int CalculatePriority(RecordVector::const_iterator B,
975 RecordVector::const_iterator E) {
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000976 int priority = 0;
977
978 if (B != E) {
979 priority = static_cast<int>((*B)->getValueAsInt("priority"));
980
981 if (++B != E)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000982 throw "More than one 'PluginPriority' instance found: "
983 "most probably an error!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000984 }
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000985
986 return priority;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000987}
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000988
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000989/// NotInGraph - Helper function object for FilterNotInGraph.
990struct NotInGraph {
991private:
992 const llvm::StringSet<>& ToolsInGraph_;
993
994public:
995 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
996 : ToolsInGraph_(ToolsInGraph)
997 {}
998
999 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
1000 return (ToolsInGraph_.count(x->Name) == 0);
1001 }
1002};
1003
1004/// FilterNotInGraph - Filter out from ToolDescs all Tools not
1005/// mentioned in the compilation graph definition.
1006void FilterNotInGraph (const RecordVector& EdgeVector,
1007 ToolDescriptions& ToolDescs) {
1008
1009 // List all tools mentioned in the graph.
1010 llvm::StringSet<> ToolsInGraph;
1011
1012 for (RecordVector::const_iterator B = EdgeVector.begin(),
1013 E = EdgeVector.end(); B != E; ++B) {
1014
1015 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001016 const std::string& NodeA = Edge->getValueAsString("a");
1017 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001018
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001019 if (NodeA != "root")
1020 ToolsInGraph.insert(NodeA);
1021 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001022 }
1023
1024 // Filter ToolPropertiesList.
1025 ToolDescriptions::iterator new_end =
1026 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
1027 NotInGraph(ToolsInGraph));
1028 ToolDescs.erase(new_end, ToolDescs.end());
1029}
1030
1031/// FillInToolToLang - Fills in two tables that map tool names to
1032/// (input, output) languages. Helper function used by TypecheckGraph().
1033void FillInToolToLang (const ToolDescriptions& ToolDescs,
1034 StringMap<StringSet<> >& ToolToInLang,
1035 StringMap<std::string>& ToolToOutLang) {
1036 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1037 E = ToolDescs.end(); B != E; ++B) {
1038 const ToolDescription& D = *(*B);
1039 for (StrVector::const_iterator B = D.InLanguage.begin(),
1040 E = D.InLanguage.end(); B != E; ++B)
1041 ToolToInLang[D.Name].insert(*B);
1042 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001043 }
1044}
1045
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001046/// TypecheckGraph - Check that names for output and input languages
1047/// on all edges do match. This doesn't do much when the information
1048/// about the whole graph is not available (i.e. when compiling most
1049/// plugins).
1050void TypecheckGraph (const RecordVector& EdgeVector,
1051 const ToolDescriptions& ToolDescs) {
1052 StringMap<StringSet<> > ToolToInLang;
1053 StringMap<std::string> ToolToOutLang;
1054
1055 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
1056 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
1057 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
1058
1059 for (RecordVector::const_iterator B = EdgeVector.begin(),
1060 E = EdgeVector.end(); B != E; ++B) {
1061 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001062 const std::string& NodeA = Edge->getValueAsString("a");
1063 const std::string& NodeB = Edge->getValueAsString("b");
1064 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
1065 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001066
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001067 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001068 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001069 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001070 + ": output->input language mismatch";
1071 }
1072
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001073 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001074 throw "Edges back to the root are not allowed!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001075 }
1076}
1077
1078/// WalkCase - Walks the 'case' expression DAG and invokes
1079/// TestCallback on every test, and StatementCallback on every
1080/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001081/// combinators (that is, they are passed directly to TestCallback).
1082/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1083/// IndentLevel, bool FirstTest)'.
1084/// StatementCallback must have type 'void StatementCallback(const Init*,
1085/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001086template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001087void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1088 unsigned IndentLevel = 0)
1089{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001090 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001091
1092 // Error checks.
1093 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001094 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001095
1096 if (d.getNumArgs() < 2)
1097 throw "There should be at least one clause in the 'case' expression:\n"
1098 + d.getAsString();
1099
1100 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001101 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001102 const unsigned numArgs = d.getNumArgs();
1103 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001104 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1105 B != E; ++B) {
1106 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001107
1108 if (!even)
1109 {
1110 // Handle test.
1111 const DagInit& Test = InitPtrToDag(arg);
1112
1113 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001114 throw "The 'default' clause should be the last in the "
1115 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001116 if (i == numArgs)
1117 throw "Case construct handler: no corresponding action "
1118 "found for the test " + Test.getAsString() + '!';
1119
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001120 TestCallback(Test, IndentLevel, (i == 1));
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001121 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001122 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001123 {
1124 if (dynamic_cast<DagInit*>(arg)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001125 && GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001126 // Nested 'case'.
1127 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1128 }
1129
1130 // Handle statement.
1131 StatementCallback(arg, IndentLevel);
1132 }
1133
1134 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001135 even = !even;
1136 }
1137}
1138
1139/// ExtractOptionNames - A helper function object used by
1140/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1141class ExtractOptionNames {
1142 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001143
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001144 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001145 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001146 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001147 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001148 ActionName == "forward_value" ||
1149 ActionName == "forward_transformed_value" ||
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001150 ActionName == "switch_on" || ActionName == "any_switch_on" ||
1151 ActionName == "parameter_equals" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001152 ActionName == "element_in_list" || ActionName == "not_empty" ||
1153 ActionName == "empty") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001154 CheckNumberOfArguments(Stmt, 1);
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001155
1156 Init* Arg = Stmt.getArg(0);
1157 if (typeid(*Arg) == typeid(StringInit)) {
1158 const std::string& Name = InitPtrToString(Arg);
1159 OptionNames_.insert(Name);
1160 }
1161 else {
1162 // It's a list.
1163 const ListInit& List = InitPtrToList(Arg);
1164 for (ListInit::const_iterator B = List.begin(), E = List.end();
1165 B != E; ++B) {
1166 const std::string& Name = InitPtrToString(*B);
1167 OptionNames_.insert(Name);
1168 }
1169 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001170 }
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001171 else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001172 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001173 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001174 }
1175 }
1176 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001177
1178public:
1179 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1180 {}
1181
1182 void operator()(const Init* Statement) {
1183 if (typeid(*Statement) == typeid(ListInit)) {
1184 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1185 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1186 B != E; ++B)
1187 this->processDag(*B);
1188 }
1189 else {
1190 this->processDag(Statement);
1191 }
1192 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001193
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001194 void operator()(const DagInit& Test, unsigned, bool) {
1195 this->operator()(&Test);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001196 }
1197 void operator()(const Init* Statement, unsigned) {
1198 this->operator()(Statement);
1199 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001200};
1201
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001202/// CheckForSuperfluousOptions - Check that there are no side
1203/// effect-free options (specified only in the OptionList). Otherwise,
1204/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001205void CheckForSuperfluousOptions (const RecordVector& Edges,
1206 const ToolDescriptions& ToolDescs,
1207 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001208 llvm::StringSet<> nonSuperfluousOptions;
1209
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001210 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001211 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001212 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1213 E = ToolDescs.end(); B != E; ++B) {
1214 const ToolDescription& TD = *(*B);
1215 ExtractOptionNames Callback(nonSuperfluousOptions);
1216 if (TD.Actions)
1217 WalkCase(TD.Actions, Callback, Callback);
1218 }
1219
1220 // Add all options mentioned in the 'case' clauses of the
1221 // OptionalEdges of the compilation graph to the set of
1222 // non-superfluous options.
1223 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1224 B != E; ++B) {
1225 const Record* Edge = *B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001226 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001227
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001228 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001229 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001230 }
1231
1232 // Check that all options in OptDescs belong to the set of
1233 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001234 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001235 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001236 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001237 if (!nonSuperfluousOptions.count(Val.Name)
1238 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001239 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001240 "Probable cause: this option is specified only in the OptionList.\n";
1241 }
1242}
1243
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001244/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1245bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1246 if (TestName == "single_input_file") {
1247 O << "InputFilenames.size() == 1";
1248 return true;
1249 }
1250 else if (TestName == "multiple_input_files") {
1251 O << "InputFilenames.size() > 1";
1252 return true;
1253 }
1254
1255 return false;
1256}
1257
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001258/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1259template <typename F>
1260void EmitListTest(const ListInit& L, const char* LogicOp,
1261 F Callback, raw_ostream& O)
1262{
1263 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1264 // of Dags...
1265 bool isFirst = true;
1266 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1267 if (isFirst)
1268 isFirst = false;
1269 else
Mikhail Glushenkovb7935e02010-01-01 04:40:54 +00001270 O << ' ' << LogicOp << ' ';
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001271 Callback(InitPtrToString(*B), O);
1272 }
1273}
1274
1275// Callbacks for use with EmitListTest.
1276
1277class EmitSwitchOn {
1278 const OptionDescriptions& OptDescs_;
1279public:
1280 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1281 {}
1282
1283 void operator()(const std::string& OptName, raw_ostream& O) const {
1284 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1285 O << OptDesc.GenVariableName();
1286 }
1287};
1288
1289class EmitEmptyTest {
1290 bool EmitNegate_;
1291 const OptionDescriptions& OptDescs_;
1292public:
1293 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1294 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1295 {}
1296
1297 void operator()(const std::string& OptName, raw_ostream& O) const {
1298 const char* Neg = (EmitNegate_ ? "!" : "");
1299 if (OptName == "o") {
1300 O << Neg << "OutputFilename.empty()";
1301 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001302 else if (OptName == "save-temps") {
1303 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1304 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001305 else {
1306 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1307 O << Neg << OptDesc.GenVariableName() << ".empty()";
1308 }
1309 }
1310};
1311
1312
1313/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1314bool EmitCaseTest1ArgList(const std::string& TestName,
1315 const DagInit& d,
1316 const OptionDescriptions& OptDescs,
1317 raw_ostream& O) {
Mikhail Glushenkov3a481e32010-01-01 03:50:51 +00001318 const ListInit& L = InitPtrToList(d.getArg(0));
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001319
1320 if (TestName == "any_switch_on") {
1321 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1322 return true;
1323 }
1324 else if (TestName == "switch_on") {
1325 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1326 return true;
1327 }
1328 else if (TestName == "any_not_empty") {
1329 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1330 return true;
1331 }
1332 else if (TestName == "any_empty") {
1333 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1334 return true;
1335 }
1336 else if (TestName == "not_empty") {
1337 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1338 return true;
1339 }
1340 else if (TestName == "empty") {
1341 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1342 return true;
1343 }
1344
1345 return false;
1346}
1347
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001348/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1349bool EmitCaseTest1ArgStr(const std::string& TestName,
1350 const DagInit& d,
1351 const OptionDescriptions& OptDescs,
1352 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001353 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001354
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001355 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001356 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001357 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001358 }
1359 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001360 O << "InLangs.count(\"" << OptName << "\") != 0";
1361 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001362 }
1363 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001364 // This works only for single-argument Tool::GenerateAction. Join
1365 // tools can process several files in different languages simultaneously.
1366
1367 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001368 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001369 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001370 }
1371 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001372 bool EmitNegate = (TestName == "not_empty");
1373 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001374 return true;
1375 }
1376
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001377 return false;
1378}
1379
1380/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1381bool EmitCaseTest1Arg(const std::string& TestName,
1382 const DagInit& d,
1383 const OptionDescriptions& OptDescs,
1384 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001385 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001386 if (typeid(*d.getArg(0)) == typeid(ListInit))
1387 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1388 else
1389 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1390}
1391
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001392/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001393bool EmitCaseTest2Args(const std::string& TestName,
1394 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001395 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001396 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001397 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001398 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001399 const std::string& OptName = InitPtrToString(d.getArg(0));
1400 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001401
1402 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001403 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001404 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1405 return true;
1406 }
1407 else if (TestName == "element_in_list") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001408 const OptionDescription& OptDesc = OptDescs.FindList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001409 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001410 O << "std::find(" << VarName << ".begin(),\n";
1411 O.indent(IndentLevel + Indent1)
1412 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001413 << OptArg << "\") != " << VarName << ".end()";
1414 return true;
1415 }
1416
1417 return false;
1418}
1419
1420// Forward declaration.
1421// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001422void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001423 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001424 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001425
1426/// EmitLogicalOperationTest - Helper function used by
1427/// EmitCaseConstructHandler.
1428void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001429 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001430 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001431 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001432 O << '(';
1433 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001434 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001435 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001436 if (j != NumArgs - 1) {
1437 O << ")\n";
1438 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1439 }
1440 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001441 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001442 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001443 }
1444}
1445
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001446void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001447 const OptionDescriptions& OptDescs, raw_ostream& O)
1448{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001449 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001450 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1451 O << "! (";
1452 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1453 O << ")";
1454}
1455
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001456/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001457void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001458 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001459 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001460 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001461
1462 if (TestName == "and")
1463 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1464 else if (TestName == "or")
1465 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001466 else if (TestName == "not")
1467 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001468 else if (EmitCaseTest0Args(TestName, O))
1469 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001470 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1471 return;
1472 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1473 return;
1474 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001475 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001476}
1477
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001478
1479/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1480class EmitCaseTestCallback {
1481 bool EmitElseIf_;
1482 const OptionDescriptions& OptDescs_;
1483 raw_ostream& O_;
1484public:
1485
1486 EmitCaseTestCallback(bool EmitElseIf,
1487 const OptionDescriptions& OptDescs, raw_ostream& O)
1488 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1489 {}
1490
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001491 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001492 {
1493 if (GetOperatorName(Test) == "default") {
1494 O_.indent(IndentLevel) << "else {\n";
1495 }
1496 else {
1497 O_.indent(IndentLevel)
1498 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001499 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001500 O_ << ") {\n";
1501 }
1502 }
1503};
1504
1505/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1506template <typename F>
1507class EmitCaseStatementCallback {
1508 F Callback_;
1509 raw_ostream& O_;
1510public:
1511
1512 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1513 : Callback_(Callback), O_(O)
1514 {}
1515
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001516 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001517
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001518 // Ignore nested 'case' DAG.
1519 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001520 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case")) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001521 if (typeid(*Statement) == typeid(ListInit)) {
1522 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1523 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1524 B != E; ++B)
1525 Callback_(*B, (IndentLevel + Indent1), O_);
1526 }
1527 else {
1528 Callback_(Statement, (IndentLevel + Indent1), O_);
1529 }
1530 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001531 O_.indent(IndentLevel) << "}\n";
1532 }
1533
1534};
1535
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001536/// EmitCaseConstructHandler - Emit code that handles the 'case'
1537/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001538/// clause. Implemented on top of WalkCase.
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00001539/// Callback's type is void F(const Init* Statement, unsigned IndentLevel,
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001540/// raw_ostream& O).
1541/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001542/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1543/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001544template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001545void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001546 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001547 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001548 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001549 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1550 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001551}
1552
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001553/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001554/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1555/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001556void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001557 const char* Delimiters = " \t\n\v\f\r";
1558 enum TokenizerState
1559 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1560 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001561
1562 if (CmdLine.empty())
1563 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001564 Out.push_back("");
1565
1566 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1567 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001568
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001569 for (; B != E; ++B) {
1570 char cur_ch = CmdLine[B];
1571
1572 switch (cur_st) {
1573 case Normal:
1574 if (cur_ch == '$') {
1575 cur_st = SpecialCommand;
1576 break;
1577 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001578 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001579 // Skip whitespace
1580 B = CmdLine.find_first_not_of(Delimiters, B);
1581 if (B == std::string::npos) {
1582 B = E-1;
1583 continue;
1584 }
1585 --B;
1586 Out.push_back("");
1587 continue;
1588 }
1589 break;
1590
1591
1592 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001593 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001594 cur_st = Normal;
1595 Out.push_back("");
1596 continue;
1597 }
1598 if (cur_ch == '(') {
1599 Out.push_back("");
1600 cur_st = InsideSpecialCommand;
1601 continue;
1602 }
1603 break;
1604
1605 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001606 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001607 continue;
1608 }
1609 if (cur_ch == '\'') {
1610 cur_st = InsideQuotationMarks;
1611 Out.push_back("");
1612 continue;
1613 }
1614 if (cur_ch == ')') {
1615 cur_st = Normal;
1616 Out.push_back("");
1617 }
1618 if (cur_ch == ',') {
1619 continue;
1620 }
1621
1622 break;
1623
1624 case InsideQuotationMarks:
1625 if (cur_ch == '\'') {
1626 cur_st = InsideSpecialCommand;
1627 continue;
1628 }
1629 break;
1630 }
1631
1632 Out.back().push_back(cur_ch);
1633 }
1634}
1635
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001636/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1637/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1638/// SubstituteSpecialCommands().
1639StrVector::const_iterator
1640SubstituteCall (StrVector::const_iterator Pos,
1641 StrVector::const_iterator End,
1642 bool IsJoin, raw_ostream& O)
1643{
1644 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001645 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001646 const std::string& CmdName = *Pos;
1647
1648 if (CmdName == ")")
1649 throw "$CALL invocation: empty argument list!";
1650
1651 O << "hooks::";
1652 O << CmdName << "(";
1653
1654
1655 bool firstIteration = true;
1656 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001657 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001658 const std::string& Arg = *Pos;
1659 assert(Arg.size() != 0);
1660
1661 if (Arg[0] == ')')
1662 break;
1663
1664 if (firstIteration)
1665 firstIteration = false;
1666 else
1667 O << ", ";
1668
1669 if (Arg == "$INFILE") {
1670 if (IsJoin)
1671 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1672 else
1673 O << "inFile.c_str()";
1674 }
1675 else {
1676 O << '"' << Arg << '"';
1677 }
1678 }
1679
1680 O << ')';
1681
1682 return Pos;
1683}
1684
1685/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1686/// function used by SubstituteSpecialCommands().
1687StrVector::const_iterator
1688SubstituteEnv (StrVector::const_iterator Pos,
1689 StrVector::const_iterator End, raw_ostream& O)
1690{
1691 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001692 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001693 const std::string& EnvName = *Pos;
1694
1695 if (EnvName == ")")
1696 throw "$ENV invocation: empty argument list!";
1697
1698 O << "checkCString(std::getenv(\"";
1699 O << EnvName;
1700 O << "\"))";
1701
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001702 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001703
1704 return Pos;
1705}
1706
1707/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1708/// handler code. Helper function used by EmitCmdLineVecFill().
1709StrVector::const_iterator
1710SubstituteSpecialCommands (StrVector::const_iterator Pos,
1711 StrVector::const_iterator End,
1712 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001713{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001714
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001715 const std::string& cmd = *Pos;
1716
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001717 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001718 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001719 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001720 }
1721 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001722 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001723 }
1724 else {
1725 throw "Unknown special command: " + cmd;
1726 }
1727
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001728 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001729 const std::string& Leftover = *Pos;
1730 assert(Leftover.at(0) == ')');
1731 if (Leftover.size() != 1)
1732 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001733
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001734 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001735}
1736
1737/// EmitCmdLineVecFill - Emit code that fills in the command line
1738/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001739void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001740 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001741 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001742 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001743 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001744
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001745 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001746 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001747
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001748 StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001749
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001750 // Emit the command itself.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001751 assert(!StrVec[0].empty());
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001752 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001753 if (StrVec[0][0] == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001754 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1755 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001756 }
1757 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001758 O << '"' << StrVec[0] << '"';
1759 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001760 }
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001761 O << ";\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001762
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001763 // Go through the command arguments.
1764 assert(B <= E);
1765 for (; B != E; ++B) {
1766 const std::string& cmd = *B;
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001767
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001768 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001769 O.indent(IndentLevel);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001770
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001771 if (cmd.at(0) == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001772 O << "vec.push_back(std::make_pair(0, ";
1773 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1774 O << "));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001775 }
1776 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001777 O << "vec.push_back(std::make_pair(0, \"" << cmd << "\"));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001778 }
1779 }
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001780
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001781}
1782
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001783/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1784/// implement EmitActionHandler. Emits code for
1785/// handling the (forward) and (forward_as) option properties.
1786void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001787 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001788 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001789 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001790 const std::string& Name = NewName.empty()
1791 ? ("-" + D.Name)
1792 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001793 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001794
1795 switch (D.Type) {
1796 case OptionType::Switch:
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001797 O.indent(IndentLevel)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001798 << "vec.push_back(std::make_pair(" << D.GenVariableName()
1799 << ".getPosition(), \"" << Name << "\"));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001800 break;
1801 case OptionType::Parameter:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001802 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1803 << D.GenVariableName()
1804 <<".getPosition(), \"" << Name;
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001805
1806 if (!D.isForwardNotSplit()) {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001807 O << "\"));\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001808 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1809 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001810 << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001811 }
1812 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001813 O << "=\" + " << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001814 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001815 break;
1816 case OptionType::Prefix:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001817 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1818 << D.GenVariableName() << ".getPosition(), \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001819 << Name << "\" + "
1820 << D.GenVariableName() << "));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001821 break;
1822 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001823 O.indent(IndentLevel)
1824 << "for (" << D.GenTypeDeclaration()
1825 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1826 O.indent(IndentLevel)
1827 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001828 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1829 << ".getPosition(B - " << D.GenVariableName()
1830 << ".begin());\n";
1831 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001832 << Name << "\" + " << "*B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001833 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001834
1835 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001836 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001837 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001838 }
1839
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001840 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001841 break;
1842 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001843 O.indent(IndentLevel)
1844 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1845 << D.GenVariableName() << ".begin(),\n";
1846 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1847 << ".end() ; B != E;) {\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001848 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1849 << ".getPosition(B - " << D.GenVariableName()
1850 << ".begin());\n";
1851 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001852 << Name << "\"));\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001853
1854 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001855 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001856 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001857 }
1858
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001859 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001860 break;
1861 case OptionType::Alias:
1862 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001863 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001864 }
1865}
1866
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001867/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1868/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001869struct ActionHandlingCallbackBase
1870{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001871
1872 void onErrorDag(const DagInit& d,
1873 unsigned IndentLevel, raw_ostream& O) const
1874 {
1875 O.indent(IndentLevel)
1876 << "throw std::runtime_error(\"" <<
1877 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1878 : "Unknown error!")
1879 << "\");\n";
1880 }
1881
1882 void onWarningDag(const DagInit& d,
1883 unsigned IndentLevel, raw_ostream& O) const
1884 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001885 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001886 O.indent(IndentLevel) << "llvm::errs() << \""
1887 << InitPtrToString(d.getArg(0)) << "\";\n";
1888 }
1889
1890};
1891
1892/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1893/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001894
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001895class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001896
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001897typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1898(const DagInit&, unsigned, raw_ostream&) const;
1899
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001900class EmitActionHandlersCallback :
1901 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001902 public HandlerTable<EmitActionHandlersCallbackHandler>
1903{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001904 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001905
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001906 const OptionDescriptions& OptDescs;
1907
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001908 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1909 /// onAppendCmd and onOutputSuffix.
1910 void EmitHookInvocation(const std::string& Str,
1911 const char* BlockOpen, const char* BlockClose,
1912 unsigned IndentLevel, raw_ostream& O) const
1913 {
1914 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001915 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001916
1917 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1918 B != E; ++B) {
1919 const std::string& cmd = *B;
1920
1921 O.indent(IndentLevel) << BlockOpen;
1922
1923 if (cmd.at(0) == '$')
1924 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1925 else
1926 O << '"' << cmd << '"';
1927
1928 O << BlockClose;
1929 }
1930 }
1931
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001932 void onAppendCmd (const DagInit& Dag,
1933 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001934 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001935 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001936 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001937 "vec.push_back(std::make_pair(65536, ", "));\n",
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001938 IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001939 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001940
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001941 void onForward (const DagInit& Dag,
1942 unsigned IndentLevel, raw_ostream& O) const
1943 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001944 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001945 const std::string& Name = InitPtrToString(Dag.getArg(0));
1946 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1947 IndentLevel, "", O);
1948 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001949
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001950 void onForwardAs (const DagInit& Dag,
1951 unsigned IndentLevel, raw_ostream& O) const
1952 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001953 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001954 const std::string& Name = InitPtrToString(Dag.getArg(0));
1955 const std::string& NewName = InitPtrToString(Dag.getArg(1));
1956 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1957 IndentLevel, NewName, O);
1958 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001959
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001960 void onForwardValue (const DagInit& Dag,
1961 unsigned IndentLevel, raw_ostream& O) const
1962 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001963 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001964 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001965 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001966
1967 if (D.isParameter()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001968 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1969 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001970 << D.GenVariableName() << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001971 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001972 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001973 O.indent(IndentLevel) << "for (cl::list<std::string>::iterator B = "
1974 << D.GenVariableName() << ".begin(), \n";
1975 O.indent(IndentLevel + Indent1) << " E = " << D.GenVariableName()
1976 << ".end(); B != E; ++B)\n";
1977 O.indent(IndentLevel) << "{\n";
1978 O.indent(IndentLevel + Indent1)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001979 << "unsigned pos = " << D.GenVariableName()
1980 << ".getPosition(B - " << D.GenVariableName()
1981 << ".begin());\n";
1982 O.indent(IndentLevel + Indent1)
1983 << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001984 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001985 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001986 }
1987
1988 void onForwardTransformedValue (const DagInit& Dag,
1989 unsigned IndentLevel, raw_ostream& O) const
1990 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001991 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001992 const std::string& Name = InitPtrToString(Dag.getArg(0));
1993 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001994 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001995
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001996 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1997 << D.GenVariableName() << ".getPosition("
1998 << (D.isList() ? "0" : "") << "), "
1999 << "hooks::" << Hook << "(" << D.GenVariableName()
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002000 << (D.isParameter() ? ".c_str()" : "") << ")));\n";
2001 }
2002
2003 void onNoOutFile (const DagInit& Dag,
2004 unsigned IndentLevel, raw_ostream& O) const
2005 {
2006 CheckNumberOfArguments(Dag, 0);
2007 O.indent(IndentLevel) << "no_out_file = true;\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002008 }
2009
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002010 void onOutputSuffix (const DagInit& Dag,
2011 unsigned IndentLevel, raw_ostream& O) const
2012 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002013 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002014 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
2015 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002016 }
2017
2018 void onStopCompilation (const DagInit& Dag,
2019 unsigned IndentLevel, raw_ostream& O) const
2020 {
2021 O.indent(IndentLevel) << "stop_compilation = true;\n";
2022 }
2023
2024
2025 void onUnpackValues (const DagInit& Dag,
2026 unsigned IndentLevel, raw_ostream& O) const
2027 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002028 throw "'unpack_values' is deprecated. "
2029 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002030 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002031
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002032 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002033
2034 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
2035 : OptDescs(OD)
2036 {
2037 if (!staticMembersInitialized_) {
2038 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
2039 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
2040 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
2041 AddHandler("forward", &EmitActionHandlersCallback::onForward);
2042 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002043 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
2044 AddHandler("forward_transformed_value",
2045 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002046 AddHandler("no_out_file",
2047 &EmitActionHandlersCallback::onNoOutFile);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002048 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
2049 AddHandler("stop_compilation",
2050 &EmitActionHandlersCallback::onStopCompilation);
2051 AddHandler("unpack_values",
2052 &EmitActionHandlersCallback::onUnpackValues);
2053
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002054
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002055 staticMembersInitialized_ = true;
2056 }
2057 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002058
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002059 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002060 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002061 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002062 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002063 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002064};
2065
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002066void EmitGenerateActionMethodHeader(const ToolDescription& D,
2067 bool IsJoin, raw_ostream& O)
2068{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002069 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002070 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002071 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002072 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002073
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002074 O.indent(Indent2) << "bool HasChildren,\n";
2075 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2076 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2077 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2078 O.indent(Indent1) << "{\n";
2079 O.indent(Indent2) << "std::string cmd;\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002080 O.indent(Indent2) << "std::string out_file;\n";
2081 O.indent(Indent2) << "std::vector<std::pair<unsigned, std::string> > vec;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002082 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002083 O.indent(Indent2) << "bool no_out_file = false;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002084 O.indent(Indent2) << "const char* output_suffix = \""
2085 << D.OutputSuffix << "\";\n";
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002086}
2087
2088// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2089// Tool::GenerateAction() method.
2090void EmitGenerateActionMethod (const ToolDescription& D,
2091 const OptionDescriptions& OptDescs,
2092 bool IsJoin, raw_ostream& O) {
2093
2094 EmitGenerateActionMethodHeader(D, IsJoin, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002095
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002096 if (!D.CmdLine)
2097 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002098
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002099 // Process the 'command' property.
2100 O << '\n';
2101 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2102 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002103
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002104 // Process the 'actions' list of this tool.
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002105 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002106 EmitCaseConstructHandler(D.Actions, Indent2,
2107 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002108 false, OptDescs, O);
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002109 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002110
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002111 // Input file (s)
2112 if (!D.InFileOption.empty()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002113 O.indent(Indent2)
2114 << "vec.push_back(std::make_pair(InputFilenames.getPosition(0), \""
2115 << D.InFileOption << "\");\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002116 }
2117
2118 if (IsJoin) {
2119 O.indent(Indent2)
2120 << "for (PathVector::const_iterator B = inFiles.begin(),\n";
2121 O.indent(Indent3) << "E = inFiles.end(); B != E; ++B)\n";
2122 O.indent(Indent2) << "{\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002123 O.indent(Indent3) << "vec.push_back(std::make_pair("
2124 << "InputFilenames.getPosition(B - inFiles.begin()), "
2125 << "B->str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002126 O.indent(Indent2) << "}\n";
2127 }
2128 else {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002129 O.indent(Indent2) << "vec.push_back(std::make_pair("
2130 << "InputFilenames.getPosition(0), inFile.str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002131 }
2132
2133 // Output file
2134 O.indent(Indent2) << "if (!no_out_file) {\n";
2135 if (!D.OutFileOption.empty())
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002136 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002137 << D.OutFileOption << "\"));\n";
2138
2139 O.indent(Indent3) << "out_file = this->OutFilename("
2140 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
2141 O.indent(Indent4) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002142 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, out_file));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002143
2144 O.indent(Indent2) << "}\n\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002145
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002146 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002147 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002148 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002149 O.indent(Indent3) << "for (cl::list<std::string>::iterator B = "
2150 << SinkOptionName << ".begin(), E = " << SinkOptionName
2151 << ".end(); B != E; ++B)\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002152 O.indent(Indent4) << "vec.push_back(std::make_pair(" << SinkOptionName
2153 << ".getPosition(B - " << SinkOptionName
2154 << ".begin()), *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002155 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002156 }
2157
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002158 O.indent(Indent2) << "return Action(cmd, this->SortArgs(vec), "
2159 << "stop_compilation, out_file);\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002160 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002161}
2162
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002163/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2164/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002165void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2166 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002167 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002168 if (!ToolDesc.isJoin()) {
2169 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
2170 O.indent(Indent2) << "bool HasChildren,\n";
2171 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2172 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2173 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2174 O.indent(Indent1) << "{\n";
2175 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
2176 << " is not a Join tool!\");\n";
2177 O.indent(Indent1) << "}\n\n";
2178 }
2179 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002180 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002181 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002182
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002183 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002184}
2185
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002186/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2187/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002188void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002189 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2190 O.indent(Indent2) << "return InputLanguages_;\n";
2191 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002192
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002193 if (D.OutLanguage.empty())
2194 throw "Tool " + D.Name + " has no 'out_language' property!";
2195
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002196 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2197 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2198 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002199}
2200
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002201/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002202void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002203 O.indent(Indent1) << "const char* Name() const {\n";
2204 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2205 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002206}
2207
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002208/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2209/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002210void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002211 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002212 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002213 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002214 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002215 O.indent(Indent2) << "return false;\n";
2216 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002217}
2218
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002219/// EmitWorksOnEmptyCallback - Callback used by EmitWorksOnEmptyMethod in
2220/// conjunction with EmitCaseConstructHandler.
2221void EmitWorksOnEmptyCallback (const Init* Value,
2222 unsigned IndentLevel, raw_ostream& O) {
2223 CheckBooleanConstant(Value);
2224 O.indent(IndentLevel) << "return " << Value->getAsString() << ";\n";
2225}
2226
2227/// EmitWorksOnEmptyMethod - Emit the WorksOnEmpty() method for a given Tool
2228/// class.
2229void EmitWorksOnEmptyMethod (const ToolDescription& D,
2230 const OptionDescriptions& OptDescs,
2231 raw_ostream& O)
2232{
2233 O.indent(Indent1) << "bool WorksOnEmpty() const {\n";
2234 if (D.OnEmpty == 0)
2235 O.indent(Indent2) << "return false;\n";
2236 else
2237 EmitCaseConstructHandler(D.OnEmpty, Indent2, EmitWorksOnEmptyCallback,
2238 /*EmitElseIf = */ true, OptDescs, O);
2239 O.indent(Indent1) << "}\n\n";
2240}
2241
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002242/// EmitStaticMemberDefinitions - Emit static member definitions for a
2243/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002244void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002245 if (D.InLanguage.empty())
2246 throw "Tool " + D.Name + " has no 'in_language' property!";
2247
2248 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2249 for (StrVector::const_iterator B = D.InLanguage.begin(),
2250 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002251 O << '\"' << *B << "\", ";
2252 O << "0};\n\n";
2253}
2254
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002255/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002256void EmitToolClassDefinition (const ToolDescription& D,
2257 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002258 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002259 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002260 return;
2261
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002262 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002263 O << "class " << D.Name << " : public ";
2264 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002265 O << "JoinTool";
2266 else
2267 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002268
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002269 O << " {\nprivate:\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002270 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002271
2272 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002273 EmitNameMethod(D, O);
2274 EmitInOutLanguageMethods(D, O);
2275 EmitIsJoinMethod(D, O);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002276 EmitWorksOnEmptyMethod(D, OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002277 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002278
2279 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002280 O << "};\n";
2281
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002282 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002283
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002284}
2285
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002286/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002287/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002288void EmitOptionDefinitions (const OptionDescriptions& descs,
2289 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002290 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002291{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002292 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002293
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002294 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002295 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002296 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002297 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002298
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002299 if (val.Type == OptionType::Alias) {
2300 Aliases.push_back(val);
2301 continue;
2302 }
2303
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002304 if (val.isExtern())
2305 O << "extern ";
2306
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002307 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002308 << val.GenVariableName();
2309
2310 if (val.isExtern()) {
2311 O << ";\n";
2312 continue;
2313 }
2314
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002315 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002316
2317 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2318 O << ", cl::Prefix";
2319
2320 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002321 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002322 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002323 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002324 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002325 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002326 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002327 O << ", cl::OneOrMore";
2328 }
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002329 else if (val.isOptional() && val.isList()) {
2330 O << ", cl::Optional";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002331 }
2332
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002333 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002334 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002335 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002336 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002337
2338 if (val.isCommaSeparated())
2339 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002340
2341 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002342 O << ", cl::multi_val(" << val.MultiVal << ')';
2343
2344 if (val.InitVal) {
2345 const std::string& str = val.InitVal->getAsString();
2346 O << ", cl::init(" << str << ')';
2347 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002348
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002349 if (!val.Help.empty())
2350 O << ", cl::desc(\"" << val.Help << "\")";
2351
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002352 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002353 }
2354
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002355 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002356 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002357 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002358 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002359
2360 O << val.GenTypeDeclaration() << ' '
2361 << val.GenVariableName()
2362 << "(\"" << val.Name << '\"';
2363
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002364 const OptionDescription& D = descs.FindOption(val.Help);
2365 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002366
2367 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2368 }
2369
2370 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002371 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002372 O << (HasExterns ? "extern cl" : "cl")
2373 << "::list<std::string> " << SinkOptionName
2374 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002375
2376 O << '\n';
2377}
2378
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002379/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002380/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002381
2382class EmitPreprocessOptionsCallback;
2383
2384typedef void
2385(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2386(const DagInit&, unsigned, raw_ostream&) const;
2387
2388class EmitPreprocessOptionsCallback :
2389 public ActionHandlingCallbackBase,
2390 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2391{
2392 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002393 typedef void
2394 (EmitPreprocessOptionsCallback::* HandlerImpl)
2395 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002396
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002397 const OptionDescriptions& OptDescs_;
2398
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002399 void onListOrDag(const DagInit& d, HandlerImpl h,
2400 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002401 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002402 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002403 const Init* I = d.getArg(0);
2404
2405 // If I is a list, apply h to each element.
2406 if (typeid(*I) == typeid(ListInit)) {
2407 const ListInit& L = *static_cast<const ListInit*>(I);
2408 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B)
2409 ((this)->*(h))(*B, IndentLevel, O);
2410 }
2411 // Otherwise, apply h to I.
2412 else {
2413 ((this)->*(h))(I, IndentLevel, O);
2414 }
2415 }
2416
2417 void onUnsetOptionImpl(const Init* I,
2418 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002419 {
2420 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002421 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002422
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002423 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002424 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2425 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002426 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002427 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2428 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002429 else if (OptDesc.isList()) {
2430 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2431 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002432 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002433 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002434 }
2435 }
2436
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002437 void onUnsetOption(const DagInit& d,
2438 unsigned IndentLevel, raw_ostream& O) const
2439 {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002440 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2441 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002442 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002443
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002444 void onSetOptionImpl(const DagInit& d,
2445 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002446 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002447 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002448 const Init* Value = d.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002449 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2450
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002451 if (OptDesc.isList()) {
2452 const ListInit& List = InitPtrToList(Value);
2453
2454 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2455 for (ListInit::const_iterator B = List.begin(), E = List.end();
2456 B != E; ++B) {
2457 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".push_back(\""
2458 << InitPtrToString(*B) << "\");\n";
2459 }
2460 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002461 else if (OptDesc.isSwitch()) {
2462 CheckBooleanConstant(Value);
2463 O.indent(IndentLevel) << OptDesc.GenVariableName()
2464 << " = " << Value->getAsString() << ";\n";
2465 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002466 else if (OptDesc.isParameter()) {
2467 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002468 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002469 << " = \"" << Str << "\";\n";
2470 }
2471 else {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002472 throw "Can't apply 'set_option' to alias option -" + OptName + " !";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002473 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002474 }
2475
2476 void onSetSwitch(const Init* I,
2477 unsigned IndentLevel, raw_ostream& O) const {
2478 const std::string& OptName = InitPtrToString(I);
2479 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2480
2481 if (OptDesc.isSwitch())
2482 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2483 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002484 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002485 }
2486
2487 void onSetOption(const DagInit& d,
2488 unsigned IndentLevel, raw_ostream& O) const
2489 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002490 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002491
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002492 // Two arguments: (set_option "parameter", VALUE), where VALUE can be a
2493 // boolean, a string or a string list.
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002494 if (d.getNumArgs() > 1)
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002495 this->onSetOptionImpl(d, IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002496 // One argument: (set_option "switch")
2497 // or (set_option ["switch1", "switch2", ...])
2498 else
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002499 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2500 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002501 }
2502
2503public:
2504
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002505 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002506 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002507 {
2508 if (!staticMembersInitialized_) {
2509 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2510 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2511 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002512 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002513
2514 staticMembersInitialized_ = true;
2515 }
2516 }
2517
2518 void operator()(const Init* I,
2519 unsigned IndentLevel, raw_ostream& O) const
2520 {
2521 InvokeDagInitHandler(this, I, IndentLevel, O);
2522 }
2523
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002524};
2525
2526/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2527void EmitPreprocessOptions (const RecordKeeper& Records,
2528 const OptionDescriptions& OptDecs, raw_ostream& O)
2529{
2530 O << "void PreprocessOptionsLocal() {\n";
2531
2532 const RecordVector& OptionPreprocessors =
2533 Records.getAllDerivedDefinitions("OptionPreprocessor");
2534
2535 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2536 E = OptionPreprocessors.end(); B!=E; ++B) {
2537 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002538 EmitCaseConstructHandler(Case, Indent1,
2539 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002540 false, OptDecs, O);
2541 }
2542
2543 O << "}\n\n";
2544}
2545
2546/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002547void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002548{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002549 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002550
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002551 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002552 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002553
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002554 // It is allowed for a plugin to have no language map.
2555 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002556
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002557 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2558 if (!LangsToSuffixesList)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00002559 throw "Error in the language map definition!";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002560
2561 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002562 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002563
2564 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2565 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2566
2567 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002568 O.indent(Indent1) << "langMap[\""
2569 << InitPtrToString(Suffixes->getElement(i))
2570 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002571 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002572 }
2573
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002574 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002575}
2576
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002577/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2578/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002579void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002580 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002581 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002582 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002583
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002584 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002585 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002586 }
2587 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002588 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002589 }
2590 else if (OpName == "error") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002591 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002592 O.indent(IndentLevel) << "throw std::runtime_error(\""
2593 << InitPtrToString(d.getArg(0))
2594 << "\");\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002595 return;
2596 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002597 else {
2598 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002599 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002600 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002601
2602 if (d.getNumArgs() > 0)
2603 O << InitPtrToInt(d.getArg(0)) << ";\n";
2604 else
2605 O << "2;\n";
2606
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002607}
2608
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002609/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002610void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002611 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002612 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002613
2614 // Class constructor.
2615 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002616 << "public:\n";
2617 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2618 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002619
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002620 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002621 O.indent(Indent1)
2622 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2623 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002624
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002625 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002626 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002627
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002628 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002629 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002630}
2631
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002632/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002633void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002634 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002635 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002636 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002637 for (RecordVector::const_iterator B = EdgeVector.begin(),
2638 E = EdgeVector.end(); B != E; ++B) {
2639 const Record* Edge = *B;
2640 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002641 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002642
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002643 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002644 EmitEdgeClass(i, NodeB, &Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002645 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002646 }
2647}
2648
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002649/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002650/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002651void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002652 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002653 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002654{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002655 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002656
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002657 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2658 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002659 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002660
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002661 O << '\n';
2662
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002663 // Insert edges.
2664
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002665 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002666 for (RecordVector::const_iterator B = EdgeVector.begin(),
2667 E = EdgeVector.end(); B != E; ++B) {
2668 const Record* Edge = *B;
2669 const std::string& NodeA = Edge->getValueAsString("a");
2670 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002671 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002672
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002673 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002674
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002675 if (IsDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002676 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002677 else
2678 O << "new Edge" << i << "()";
2679
2680 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002681 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002682 }
2683
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002684 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002685}
2686
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002687/// HookInfo - Information about the hook type and number of arguments.
2688struct HookInfo {
2689
2690 // A hook can either have a single parameter of type std::vector<std::string>,
2691 // or NumArgs parameters of type const char*.
2692 enum HookType { ListHook, ArgHook };
2693
2694 HookType Type;
2695 unsigned NumArgs;
2696
2697 HookInfo() : Type(ArgHook), NumArgs(1)
2698 {}
2699
2700 HookInfo(HookType T) : Type(T), NumArgs(1)
2701 {}
2702
2703 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2704 {}
2705};
2706
2707typedef llvm::StringMap<HookInfo> HookInfoMap;
2708
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002709/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002710/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002711/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002712class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002713 HookInfoMap& HookNames_;
2714 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002715public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002716 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2717 : HookNames_(HookNames), OptDescs_(OptDescs)
2718 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002719
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002720 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002721 const std::string& Name = GetOperatorName(Dag);
2722
2723 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002724 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002725 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2726 const std::string& HookName = InitPtrToString(Dag.getArg(1));
2727 const OptionDescription& D = OptDescs_.FindOption(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002728
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002729 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2730 : HookInfo::ArgHook);
2731 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002732 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002733 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002734 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2735 }
2736 }
2737
2738 void onCmdLine(const std::string& Cmd) {
2739 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002740 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002741
2742 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2743 B != E; ++B) {
2744 const std::string& cmd = *B;
2745
2746 if (cmd == "$CALL") {
2747 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002748 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002749 const std::string& HookName = *B;
2750
2751 if (HookName.at(0) == ')')
2752 throw "$CALL invoked with no arguments!";
2753
2754 while (++B != E && B->at(0) != ')') {
2755 ++NumArgs;
2756 }
2757
2758 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2759
2760 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2761 H->second.Type != HookInfo::ArgHook)
2762 throw "Overloading of hooks is not allowed. Overloaded hook: "
2763 + HookName;
2764 else
2765 HookNames_[HookName] = HookInfo(NumArgs);
2766 }
2767 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002768 }
2769
2770 void operator()(const Init* Arg) {
2771
2772 // We're invoked on an action (either a dag or a dag list).
2773 if (typeid(*Arg) == typeid(DagInit)) {
2774 const DagInit& Dag = InitPtrToDag(Arg);
2775 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002776 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002777 }
2778 else if (typeid(*Arg) == typeid(ListInit)) {
2779 const ListInit& List = InitPtrToList(Arg);
2780 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2781 ++B) {
2782 const DagInit& Dag = InitPtrToDag(*B);
2783 this->onAction(Dag);
2784 }
2785 return;
2786 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002787
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002788 // We're invoked on a command line.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002789 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002790 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002791
2792 void operator()(const DagInit* Test, unsigned, bool) {
2793 this->operator()(Test);
2794 }
2795 void operator()(const Init* Statement, unsigned) {
2796 this->operator()(Statement);
2797 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002798};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002799
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002800/// FillInHookNames - Actually extract the hook names from all command
2801/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002802void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002803 const OptionDescriptions& OptDescs,
2804 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002805{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002806 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002807 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2808 E = ToolDescs.end(); B != E; ++B) {
2809 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002810
2811 // Look for 'forward_transformed_value' in 'actions'.
2812 if (D.Actions)
2813 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2814
2815 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002816 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002817 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002818 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002819 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002820 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002821 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002822 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002823 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002824 }
2825}
2826
2827/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2828/// property records and emit hook function declaration for each
2829/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002830void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2831 const OptionDescriptions& OptDescs, raw_ostream& O) {
2832 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002833
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002834 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002835 if (HookNames.empty())
2836 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002837
2838 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002839 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002840 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002841 const char* HookName = B->first();
2842 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002843
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002844 O.indent(Indent1) << "std::string " << HookName << "(";
2845
2846 if (Info.Type == HookInfo::ArgHook) {
2847 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2848 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2849 }
2850 }
2851 else {
2852 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002853 }
2854
2855 O <<");\n";
2856 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002857 O << "}\n\n";
2858}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002859
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002860/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002861void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002862 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2863 O.indent(Indent1) << "int Priority() const { return "
2864 << Priority << "; }\n\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002865 O.indent(Indent1) << "void PreprocessOptions() const\n";
2866 O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002867 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2868 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2869 O.indent(Indent1)
2870 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2871 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2872 << "};\n\n"
2873 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002874}
2875
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002876/// EmitIncludes - Emit necessary #include directives and some
2877/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002878void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002879 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2880 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002881 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002882 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2883 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002884
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002885 << "#include \"llvm/Support/CommandLine.h\"\n"
2886 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002887
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002888 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002889 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002890 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002891 << "#include <stdexcept>\n\n"
2892
2893 << "using namespace llvm;\n"
2894 << "using namespace llvmc;\n\n"
2895
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002896 << "extern cl::opt<std::string> OutputFilename;\n\n"
2897
2898 << "inline const char* checkCString(const char* s)\n"
2899 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002900}
2901
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002902
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002903/// PluginData - Holds all information about a plugin.
2904struct PluginData {
2905 OptionDescriptions OptDescs;
2906 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002907 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002908 ToolDescriptions ToolDescs;
2909 RecordVector Edges;
2910 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002911};
2912
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002913/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002914/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002915bool HasSink(const ToolDescriptions& ToolDescs) {
2916 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2917 E = ToolDescs.end(); B != E; ++B)
2918 if ((*B)->isSink())
2919 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002920
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002921 return false;
2922}
2923
2924/// HasExterns - Go through the list of option descriptions and check
2925/// if there are any external options.
2926bool HasExterns(const OptionDescriptions& OptDescs) {
2927 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2928 E = OptDescs.end(); B != E; ++B)
2929 if (B->second.isExtern())
2930 return true;
2931
2932 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002933}
2934
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002935/// CollectPluginData - Collect tool and option properties,
2936/// compilation graph edges and plugin priority from the parse tree.
2937void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2938 // Collect option properties.
2939 const RecordVector& OptionLists =
2940 Records.getAllDerivedDefinitions("OptionList");
2941 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2942 Data.OptDescs);
2943
2944 // Collect tool properties.
2945 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2946 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2947 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002948 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002949
2950 // Collect compilation graph edges.
2951 const RecordVector& CompilationGraphs =
2952 Records.getAllDerivedDefinitions("CompilationGraph");
2953 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2954 Data.Edges);
2955
2956 // Calculate the priority of this plugin.
2957 const RecordVector& Priorities =
2958 Records.getAllDerivedDefinitions("PluginPriority");
2959 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002960}
2961
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002962/// CheckPluginData - Perform some sanity checks on the collected data.
2963void CheckPluginData(PluginData& Data) {
2964 // Filter out all tools not mentioned in the compilation graph.
2965 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002966
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002967 // Typecheck the compilation graph.
2968 TypecheckGraph(Data.Edges, Data.ToolDescs);
2969
2970 // Check that there are no options without side effects (specified
2971 // only in the OptionList).
2972 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002973}
2974
Daniel Dunbar1a551802009-07-03 00:10:29 +00002975void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002976 // Emit file header.
2977 EmitIncludes(O);
2978
2979 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002980 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002981
2982 // Emit hook declarations.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002983 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002984
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002985 O << "namespace {\n\n";
2986
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002987 // Emit PreprocessOptionsLocal() function.
2988 EmitPreprocessOptions(Records, Data.OptDescs, O);
2989
2990 // Emit PopulateLanguageMapLocal() function
2991 // (language map maps from file extensions to language names).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002992 EmitPopulateLanguageMap(Records, O);
2993
2994 // Emit Tool classes.
2995 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2996 E = Data.ToolDescs.end(); B!=E; ++B)
2997 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2998
2999 // Emit Edge# classes.
3000 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
3001
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00003002 // Emit PopulateCompilationGraphLocal() function.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003003 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
3004
3005 // Emit code for plugin registration.
3006 EmitRegisterPlugin(Data.Priority, O);
3007
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003008 O << "} // End anonymous namespace.\n\n";
3009
3010 // Force linkage magic.
3011 O << "namespace llvmc {\n";
3012 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
3013 O << "}\n";
3014
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003015 // EOF
3016}
3017
3018
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003019// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00003020}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003021
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003022/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00003023void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003024 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003025 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003026
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003027 CollectPluginData(Records, Data);
3028 CheckPluginData(Data);
3029
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00003030 this->EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003031 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003032
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003033 } catch (std::exception& Error) {
3034 throw Error.what() + std::string(" - usually this means a syntax error.");
3035 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003036}