blob: da2d54f5439b9e31f18d1090a07ac6e48d85bd93 [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 Glushenkovb5c42392010-03-05 04:46:39 +0000233 CommaSeparated = 0x40, ForwardNotSplit = 0x80,
234 ZeroOrMore = 0x100 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000235}
236
237/// OptionDescription - Represents data contained in a single
238/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000239struct OptionDescription {
240 OptionType::OptionType Type;
241 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000242 unsigned Flags;
243 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000244 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000245 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000246
247 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000248 const std::string& n = "",
249 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000250 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000251 {}
252
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000253 /// GenTypeDeclaration - Returns the C++ variable type of this
254 /// option.
255 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000256
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000257 /// GenVariableName - Returns the variable name used in the
258 /// generated C++ code.
259 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000260
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000261 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000262 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000263
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000264 /// CheckConsistency - Check that the flags are consistent.
265 void CheckConsistency() const;
266
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000267 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000268
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000269 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000270
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000271 bool isMultiVal() const;
272
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000273 bool isCommaSeparated() const;
274 void setCommaSeparated();
275
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000276 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000277 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000278
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000279 bool isForwardNotSplit() const;
280 void setForwardNotSplit();
281
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000282 bool isRequired() const;
283 void setRequired();
284
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000285 bool isOneOrMore() const;
286 void setOneOrMore();
287
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000288 bool isZeroOrMore() const;
289 void setZeroOrMore();
290
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000291 bool isOptional() const;
292 void setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000293
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000294 bool isHidden() const;
295 void setHidden();
296
297 bool isReallyHidden() const;
298 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000299
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000300 bool isSwitch() const
301 { return OptionType::IsSwitch(this->Type); }
302
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000303 bool isParameter() const
304 { return OptionType::IsParameter(this->Type); }
305
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000306 bool isList() const
307 { return OptionType::IsList(this->Type); }
308
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000309};
310
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000311void OptionDescription::CheckConsistency() const {
312 unsigned i = 0;
313
314 i += this->isRequired();
315 i += this->isOptional();
316 i += this->isOneOrMore();
317 i += this->isZeroOrMore();
318
319 if (i > 1) {
320 throw "Only one of (required), (optional), (one_or_more) or "
321 "(zero_or_more) properties is allowed!";
322 }
323}
324
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000325void OptionDescription::Merge (const OptionDescription& other)
326{
327 if (other.Type != Type)
328 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000329
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000330 if (Help == other.Help || Help == DefaultHelpString)
331 Help = other.Help;
332 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000333 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000334 " defined for option " + Name + "\n";
335 }
336
337 Flags |= other.Flags;
338}
339
340bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000341 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000342}
343
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000344bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000345 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000346}
347
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000348bool OptionDescription::isCommaSeparated() const {
349 return Flags & OptionDescriptionFlags::CommaSeparated;
350}
351void OptionDescription::setCommaSeparated() {
352 Flags |= OptionDescriptionFlags::CommaSeparated;
353}
354
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000355bool OptionDescription::isForwardNotSplit() const {
356 return Flags & OptionDescriptionFlags::ForwardNotSplit;
357}
358void OptionDescription::setForwardNotSplit() {
359 Flags |= OptionDescriptionFlags::ForwardNotSplit;
360}
361
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000362bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000363 return Flags & OptionDescriptionFlags::Extern;
364}
365void OptionDescription::setExtern() {
366 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000367}
368
369bool OptionDescription::isRequired() const {
370 return Flags & OptionDescriptionFlags::Required;
371}
372void OptionDescription::setRequired() {
373 Flags |= OptionDescriptionFlags::Required;
374}
375
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000376bool OptionDescription::isOneOrMore() const {
377 return Flags & OptionDescriptionFlags::OneOrMore;
378}
379void OptionDescription::setOneOrMore() {
380 Flags |= OptionDescriptionFlags::OneOrMore;
381}
382
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000383bool OptionDescription::isZeroOrMore() const {
384 return Flags & OptionDescriptionFlags::ZeroOrMore;
385}
386void OptionDescription::setZeroOrMore() {
387 Flags |= OptionDescriptionFlags::ZeroOrMore;
388}
389
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000390bool OptionDescription::isOptional() const {
391 return Flags & OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000392}
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000393void OptionDescription::setOptional() {
394 Flags |= OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000395}
396
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000397bool OptionDescription::isHidden() const {
398 return Flags & OptionDescriptionFlags::Hidden;
399}
400void OptionDescription::setHidden() {
401 Flags |= OptionDescriptionFlags::Hidden;
402}
403
404bool OptionDescription::isReallyHidden() const {
405 return Flags & OptionDescriptionFlags::ReallyHidden;
406}
407void OptionDescription::setReallyHidden() {
408 Flags |= OptionDescriptionFlags::ReallyHidden;
409}
410
411const char* OptionDescription::GenTypeDeclaration() const {
412 switch (Type) {
413 case OptionType::Alias:
414 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000415 case OptionType::PrefixList:
416 case OptionType::ParameterList:
417 return "cl::list<std::string>";
418 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000419 return "cl::opt<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000420 case OptionType::Parameter:
421 case OptionType::Prefix:
422 default:
423 return "cl::opt<std::string>";
424 }
425}
426
427std::string OptionDescription::GenVariableName() const {
428 const std::string& EscapedName = EscapeVariableName(Name);
429 switch (Type) {
430 case OptionType::Alias:
431 return "AutoGeneratedAlias_" + EscapedName;
432 case OptionType::PrefixList:
433 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000434 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000435 case OptionType::Switch:
436 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000437 case OptionType::Prefix:
438 case OptionType::Parameter:
439 default:
440 return "AutoGeneratedParameter_" + EscapedName;
441 }
442}
443
444/// OptionDescriptions - An OptionDescription array plus some helper
445/// functions.
446class OptionDescriptions {
447 typedef StringMap<OptionDescription> container_type;
448
449 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000450 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000451
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000452public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000453 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000454 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000455
456 // Wrappers for FindOption that throw an exception in case the option has a
457 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000458 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000459 const OptionDescription& FindParameter(const std::string& OptName) const;
460 const OptionDescription& FindList(const std::string& OptName) const;
461 const OptionDescription&
462 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000463
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000464 /// insertDescription - Insert new OptionDescription into
465 /// OptionDescriptions list
466 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000467
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000468 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000469 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000470 const_iterator begin() const { return Descriptions.begin(); }
471 const_iterator end() const { return Descriptions.end(); }
472};
473
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000474const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000475OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000476 const_iterator I = Descriptions.find(OptName);
477 if (I != Descriptions.end())
478 return I->second;
479 else
480 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000481}
482
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000483const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000484OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000485 const OptionDescription& OptDesc = this->FindOption(OptName);
486 if (!OptDesc.isSwitch())
487 throw OptName + ": incorrect option type - should be a switch!";
488 return OptDesc;
489}
490
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000491const OptionDescription&
492OptionDescriptions::FindList(const std::string& OptName) const {
493 const OptionDescription& OptDesc = this->FindOption(OptName);
494 if (!OptDesc.isList())
495 throw OptName + ": incorrect option type - should be a list!";
496 return OptDesc;
497}
498
499const OptionDescription&
500OptionDescriptions::FindParameter(const std::string& OptName) const {
501 const OptionDescription& OptDesc = this->FindOption(OptName);
502 if (!OptDesc.isParameter())
503 throw OptName + ": incorrect option type - should be a parameter!";
504 return OptDesc;
505}
506
507const OptionDescription&
508OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
509 const OptionDescription& OptDesc = this->FindOption(OptName);
510 if (!OptDesc.isList() && !OptDesc.isParameter())
511 throw OptName
512 + ": incorrect option type - should be a list or parameter!";
513 return OptDesc;
514}
515
516void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000517 container_type::iterator I = Descriptions.find(o.Name);
518 if (I != Descriptions.end()) {
519 OptionDescription& D = I->second;
520 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000521 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000522 else {
523 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000524 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000525}
526
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000527/// HandlerTable - A base class for function objects implemented as
528/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000529template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000530class HandlerTable {
531protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000532 // Implementation details.
533
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000534 /// HandlerMap - A map from property names to property handlers
535 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000536
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000537 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000538 static bool staticMembersInitialized_;
539
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000540public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000541
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000542 Handler GetHandler (const std::string& HandlerName) const {
543 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000544
545 if (method != Handlers_.end()) {
546 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000547 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000548 }
549 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000550 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000551 }
552 }
553
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000554 void AddHandler(const char* Property, Handler H) {
555 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000556 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000557
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000558};
559
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000560template <class Handler, class FunctionObject>
561Handler GetHandler(FunctionObject* Obj, const DagInit& Dag) {
562 const std::string& HandlerName = GetOperatorName(Dag);
563 return Obj->GetHandler(HandlerName);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000564}
565
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000566template <class FunctionObject>
567void InvokeDagInitHandler(FunctionObject* Obj, Init* I) {
568 typedef void (FunctionObject::*Handler) (const DagInit&);
569
570 const DagInit& Dag = InitPtrToDag(I);
571 Handler h = GetHandler<Handler>(Obj, Dag);
572
573 ((Obj)->*(h))(Dag);
574}
575
576template <class FunctionObject>
577void InvokeDagInitHandler(const FunctionObject* const Obj,
578 const Init* I, unsigned IndentLevel, raw_ostream& O)
579{
580 typedef void (FunctionObject::*Handler)
581 (const DagInit&, unsigned IndentLevel, raw_ostream& O) const;
582
583 const DagInit& Dag = InitPtrToDag(I);
584 Handler h = GetHandler<Handler>(Obj, Dag);
585
586 ((Obj)->*(h))(Dag, IndentLevel, O);
587}
588
589
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000590template <typename H>
591typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
592
593template <typename H>
594bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000595
596
597/// CollectOptionProperties - Function object for iterating over an
598/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000599class CollectOptionProperties;
600typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000601(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000602
603class CollectOptionProperties
604: public HandlerTable<CollectOptionPropertiesHandler>
605{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000606private:
607
608 /// optDescs_ - OptionDescriptions table. This is where the
609 /// information is stored.
610 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000611
612public:
613
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000614 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000615 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000616 {
617 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000618 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000619 AddHandler("help", &CollectOptionProperties::onHelp);
620 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000621 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000622 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
623 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000624 AddHandler("zero_or_more", &CollectOptionProperties::onZeroOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000625 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
626 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000627 AddHandler("optional", &CollectOptionProperties::onOptional);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000628 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000629 AddHandler("forward_not_split",
630 &CollectOptionProperties::onForwardNotSplit);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000631
632 staticMembersInitialized_ = true;
633 }
634 }
635
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000636 /// operator() - Just forwards to the corresponding property
637 /// handler.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000638 void operator() (Init* I) {
639 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000640 }
641
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000642private:
643
644 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000645 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000646
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000647 void onExtern (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000648 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000649 optDesc_.setExtern();
650 }
651
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000652 void onHelp (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000653 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000654 optDesc_.Help = EscapeQuotes(InitPtrToString(d.getArg(0)));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000655 }
656
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000657 void onHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000658 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000659 optDesc_.setHidden();
660 }
661
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000662 void onReallyHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000663 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000664 optDesc_.setReallyHidden();
665 }
666
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000667 void onCommaSeparated (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000668 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000669 if (!optDesc_.isList())
670 throw "'comma_separated' is valid only on list options!";
671 optDesc_.setCommaSeparated();
672 }
673
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000674 void onForwardNotSplit (const DagInit& d) {
675 CheckNumberOfArguments(d, 0);
676 if (!optDesc_.isParameter())
677 throw "'forward_not_split' is valid only for parameter options!";
678 optDesc_.setForwardNotSplit();
679 }
680
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000681 void onRequired (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000682 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000683
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000684 optDesc_.setRequired();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000685 optDesc_.CheckConsistency();
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000686 }
687
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000688 void onInit (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000689 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000690 Init* i = d.getArg(0);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000691 const std::string& str = i->getAsString();
692
693 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
694 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
695
696 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000697 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000698
699 optDesc_.InitVal = i;
700 }
701
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000702 void onOneOrMore (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000703 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000704
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000705 optDesc_.setOneOrMore();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000706 optDesc_.CheckConsistency();
707 }
708
709 void onZeroOrMore (const DagInit& d) {
710 CheckNumberOfArguments(d, 0);
711
712 if (OptionType::IsList(optDesc_.Type))
713 llvm::errs() << "Warning: specifying the 'zero_or_more' property "
714 "on a list option has no effect.\n";
715
716 optDesc_.setZeroOrMore();
717 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000718 }
719
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000720 void onOptional (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000721 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000722
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000723 if (!OptionType::IsList(optDesc_.Type))
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000724 llvm::errs() << "Warning: specifying the 'optional' property"
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000725 "on a non-list option has no effect.\n";
726
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000727 optDesc_.setOptional();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000728 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000729 }
730
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000731 void onMultiVal (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000732 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000733 int val = InitPtrToInt(d.getArg(0));
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000734 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000735 throw "Error in the 'multi_val' property: "
736 "the value must be greater than 1!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000737 if (!OptionType::IsList(optDesc_.Type))
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000738 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000739 optDesc_.MultiVal = val;
740 }
741
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000742};
743
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000744/// AddOption - A function object that is applied to every option
745/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000746class AddOption {
747private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000748 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000749
750public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000751 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000752 {}
753
754 void operator()(const Init* i) {
755 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000756 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000757
758 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000759 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000760 const std::string& Name = InitPtrToString(d.getArg(0));
761
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000762 OptionDescription OD(Type, Name);
763
764 if (!OD.isExtern())
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000765 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000766
767 if (OD.isAlias()) {
768 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000769 OD.Help = InitPtrToString(d.getArg(1));
770 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000771 else if (!OD.isExtern()) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000772 processOptionProperties(d, OD);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000773 }
774 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000775 }
776
777private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000778 /// processOptionProperties - Go through the list of option
779 /// properties and call a corresponding handler for each.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000780 static void processOptionProperties (const DagInit& d, OptionDescription& o) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000781 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000782 DagInit::const_arg_iterator B = d.arg_begin();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000783 // Skip the first argument: it's always the option name.
784 ++B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000785 std::for_each(B, d.arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000786 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000787
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000788};
789
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000790/// CollectOptionDescriptions - Collects option properties from all
791/// OptionLists.
792void CollectOptionDescriptions (RecordVector::const_iterator B,
793 RecordVector::const_iterator E,
794 OptionDescriptions& OptDescs)
795{
796 // For every OptionList:
797 for (; B!=E; ++B) {
798 RecordVector::value_type T = *B;
799 // Throws an exception if the value does not exist.
800 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000801
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000802 // For every option description in this list:
803 // collect the information and
804 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
805 }
806}
807
808// Tool information record
809
810namespace ToolFlags {
811 enum ToolFlags { Join = 0x1, Sink = 0x2 };
812}
813
814struct ToolDescription : public RefCountedBase<ToolDescription> {
815 std::string Name;
816 Init* CmdLine;
817 Init* Actions;
818 StrVector InLanguage;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000819 std::string InFileOption;
820 std::string OutFileOption;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000821 std::string OutLanguage;
822 std::string OutputSuffix;
823 unsigned Flags;
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000824 const Init* OnEmpty;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000825
826 // Various boolean properties
827 void setSink() { Flags |= ToolFlags::Sink; }
828 bool isSink() const { return Flags & ToolFlags::Sink; }
829 void setJoin() { Flags |= ToolFlags::Join; }
830 bool isJoin() const { return Flags & ToolFlags::Join; }
831
832 // Default ctor here is needed because StringMap can only store
833 // DefaultConstructible objects
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000834 ToolDescription ()
Mikhail Glushenkovc4301292010-02-23 09:05:01 +0000835 : CmdLine(0), Actions(0), OutFileOption("-o"),
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000836 Flags(0), OnEmpty(0)
837 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000838 ToolDescription (const std::string& n)
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000839 : Name(n), CmdLine(0), Actions(0), OutFileOption("-o"),
840 Flags(0), OnEmpty(0)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000841 {}
842};
843
844/// ToolDescriptions - A list of Tool information records.
845typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
846
847
848/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000849/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000850
851class CollectToolProperties;
852typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000853(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000854
855class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
856{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000857private:
858
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000859 /// toolDesc_ - Properties of the current Tool. This is where the
860 /// information is stored.
861 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000862
863public:
864
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000865 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000866 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000867 {
868 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000869
870 AddHandler("actions", &CollectToolProperties::onActions);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000871 AddHandler("command", &CollectToolProperties::onCommand);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000872 AddHandler("in_language", &CollectToolProperties::onInLanguage);
873 AddHandler("join", &CollectToolProperties::onJoin);
874 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000875
876 AddHandler("out_file_option", &CollectToolProperties::onOutFileOption);
877 AddHandler("in_file_option", &CollectToolProperties::onInFileOption);
878
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000879 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
880 AddHandler("sink", &CollectToolProperties::onSink);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000881 AddHandler("works_on_empty", &CollectToolProperties::onWorksOnEmpty);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000882
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000883 staticMembersInitialized_ = true;
884 }
885 }
886
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000887 void operator() (Init* I) {
888 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000889 }
890
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000891private:
892
893 /// Property handlers --
894 /// Functions that extract information about tool properties from
895 /// DAG representation.
896
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000897 void onActions (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000898 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000899 Init* Case = d.getArg(0);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000900 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000901 GetOperatorName(static_cast<DagInit&>(*Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000902 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000903 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000904 }
905
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000906 void onCommand (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000907 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000908 toolDesc_.CmdLine = d.getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000909 }
910
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000911 void onInLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000912 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000913 Init* arg = d.getArg(0);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000914
915 // Find out the argument's type.
916 if (typeid(*arg) == typeid(StringInit)) {
917 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000918 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000919 }
920 else {
921 // It's a list.
922 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000923 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000924
925 // Copy strings to the output vector.
926 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
927 B != E; ++B) {
928 out.push_back(InitPtrToString(*B));
929 }
930
931 // Remove duplicates.
932 std::sort(out.begin(), out.end());
933 StrVector::iterator newE = std::unique(out.begin(), out.end());
934 out.erase(newE, out.end());
935 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000936 }
937
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000938 void onJoin (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000939 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000940 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000941 }
942
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000943 void onOutLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000944 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000945 toolDesc_.OutLanguage = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000946 }
947
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000948 void onOutFileOption (const DagInit& d) {
949 CheckNumberOfArguments(d, 1);
950 toolDesc_.OutFileOption = InitPtrToString(d.getArg(0));
951 }
952
953 void onInFileOption (const DagInit& d) {
954 CheckNumberOfArguments(d, 1);
955 toolDesc_.InFileOption = InitPtrToString(d.getArg(0));
956 }
957
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000958 void onOutputSuffix (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000959 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000960 toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000961 }
962
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000963 void onSink (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000964 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000965 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000966 }
967
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000968 void onWorksOnEmpty (const DagInit& d) {
969 toolDesc_.OnEmpty = d.getArg(0);
970 }
971
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000972};
973
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000974/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000975/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000976/// CollectToolProperties function object).
977void CollectToolDescriptions (RecordVector::const_iterator B,
978 RecordVector::const_iterator E,
979 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000980{
981 // Iterate over a properties list of every Tool definition
982 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000983 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000984 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000985 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000986
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000987 IntrusiveRefCntPtr<ToolDescription>
988 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000989
990 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000991 CollectToolProperties(*ToolDesc));
992 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000993 }
994}
995
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000996/// FillInEdgeVector - Merge all compilation graph definitions into
997/// one single edge list.
998void FillInEdgeVector(RecordVector::const_iterator B,
999 RecordVector::const_iterator E, RecordVector& Out) {
1000 for (; B != E; ++B) {
1001 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +00001002
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001003 for (unsigned i = 0; i < edges->size(); ++i)
1004 Out.push_back(edges->getElementAsRecord(i));
1005 }
1006}
1007
1008/// CalculatePriority - Calculate the priority of this plugin.
1009int CalculatePriority(RecordVector::const_iterator B,
1010 RecordVector::const_iterator E) {
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +00001011 int priority = 0;
1012
1013 if (B != E) {
1014 priority = static_cast<int>((*B)->getValueAsInt("priority"));
1015
1016 if (++B != E)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001017 throw "More than one 'PluginPriority' instance found: "
1018 "most probably an error!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001019 }
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +00001020
1021 return priority;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001022}
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001023
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001024/// NotInGraph - Helper function object for FilterNotInGraph.
1025struct NotInGraph {
1026private:
1027 const llvm::StringSet<>& ToolsInGraph_;
1028
1029public:
1030 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
1031 : ToolsInGraph_(ToolsInGraph)
1032 {}
1033
1034 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
1035 return (ToolsInGraph_.count(x->Name) == 0);
1036 }
1037};
1038
1039/// FilterNotInGraph - Filter out from ToolDescs all Tools not
1040/// mentioned in the compilation graph definition.
1041void FilterNotInGraph (const RecordVector& EdgeVector,
1042 ToolDescriptions& ToolDescs) {
1043
1044 // List all tools mentioned in the graph.
1045 llvm::StringSet<> ToolsInGraph;
1046
1047 for (RecordVector::const_iterator B = EdgeVector.begin(),
1048 E = EdgeVector.end(); B != E; ++B) {
1049
1050 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001051 const std::string& NodeA = Edge->getValueAsString("a");
1052 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001053
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001054 if (NodeA != "root")
1055 ToolsInGraph.insert(NodeA);
1056 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001057 }
1058
1059 // Filter ToolPropertiesList.
1060 ToolDescriptions::iterator new_end =
1061 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
1062 NotInGraph(ToolsInGraph));
1063 ToolDescs.erase(new_end, ToolDescs.end());
1064}
1065
1066/// FillInToolToLang - Fills in two tables that map tool names to
1067/// (input, output) languages. Helper function used by TypecheckGraph().
1068void FillInToolToLang (const ToolDescriptions& ToolDescs,
1069 StringMap<StringSet<> >& ToolToInLang,
1070 StringMap<std::string>& ToolToOutLang) {
1071 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1072 E = ToolDescs.end(); B != E; ++B) {
1073 const ToolDescription& D = *(*B);
1074 for (StrVector::const_iterator B = D.InLanguage.begin(),
1075 E = D.InLanguage.end(); B != E; ++B)
1076 ToolToInLang[D.Name].insert(*B);
1077 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001078 }
1079}
1080
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001081/// TypecheckGraph - Check that names for output and input languages
1082/// on all edges do match. This doesn't do much when the information
1083/// about the whole graph is not available (i.e. when compiling most
1084/// plugins).
1085void TypecheckGraph (const RecordVector& EdgeVector,
1086 const ToolDescriptions& ToolDescs) {
1087 StringMap<StringSet<> > ToolToInLang;
1088 StringMap<std::string> ToolToOutLang;
1089
1090 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
1091 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
1092 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
1093
1094 for (RecordVector::const_iterator B = EdgeVector.begin(),
1095 E = EdgeVector.end(); B != E; ++B) {
1096 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001097 const std::string& NodeA = Edge->getValueAsString("a");
1098 const std::string& NodeB = Edge->getValueAsString("b");
1099 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
1100 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001101
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001102 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001103 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001104 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001105 + ": output->input language mismatch";
1106 }
1107
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001108 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001109 throw "Edges back to the root are not allowed!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001110 }
1111}
1112
1113/// WalkCase - Walks the 'case' expression DAG and invokes
1114/// TestCallback on every test, and StatementCallback on every
1115/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001116/// combinators (that is, they are passed directly to TestCallback).
1117/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1118/// IndentLevel, bool FirstTest)'.
1119/// StatementCallback must have type 'void StatementCallback(const Init*,
1120/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001121template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001122void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1123 unsigned IndentLevel = 0)
1124{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001125 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001126
1127 // Error checks.
1128 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001129 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001130
1131 if (d.getNumArgs() < 2)
1132 throw "There should be at least one clause in the 'case' expression:\n"
1133 + d.getAsString();
1134
1135 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001136 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001137 const unsigned numArgs = d.getNumArgs();
1138 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001139 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1140 B != E; ++B) {
1141 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001142
1143 if (!even)
1144 {
1145 // Handle test.
1146 const DagInit& Test = InitPtrToDag(arg);
1147
1148 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001149 throw "The 'default' clause should be the last in the "
1150 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001151 if (i == numArgs)
1152 throw "Case construct handler: no corresponding action "
1153 "found for the test " + Test.getAsString() + '!';
1154
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001155 TestCallback(Test, IndentLevel, (i == 1));
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001156 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001157 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001158 {
1159 if (dynamic_cast<DagInit*>(arg)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001160 && GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001161 // Nested 'case'.
1162 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1163 }
1164
1165 // Handle statement.
1166 StatementCallback(arg, IndentLevel);
1167 }
1168
1169 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001170 even = !even;
1171 }
1172}
1173
1174/// ExtractOptionNames - A helper function object used by
1175/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1176class ExtractOptionNames {
1177 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001178
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001179 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001180 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001181 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001182 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001183 ActionName == "forward_value" ||
1184 ActionName == "forward_transformed_value" ||
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001185 ActionName == "switch_on" || ActionName == "any_switch_on" ||
1186 ActionName == "parameter_equals" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001187 ActionName == "element_in_list" || ActionName == "not_empty" ||
1188 ActionName == "empty") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001189 CheckNumberOfArguments(Stmt, 1);
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001190
1191 Init* Arg = Stmt.getArg(0);
1192 if (typeid(*Arg) == typeid(StringInit)) {
1193 const std::string& Name = InitPtrToString(Arg);
1194 OptionNames_.insert(Name);
1195 }
1196 else {
1197 // It's a list.
1198 const ListInit& List = InitPtrToList(Arg);
1199 for (ListInit::const_iterator B = List.begin(), E = List.end();
1200 B != E; ++B) {
1201 const std::string& Name = InitPtrToString(*B);
1202 OptionNames_.insert(Name);
1203 }
1204 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001205 }
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001206 else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001207 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001208 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001209 }
1210 }
1211 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001212
1213public:
1214 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1215 {}
1216
1217 void operator()(const Init* Statement) {
1218 if (typeid(*Statement) == typeid(ListInit)) {
1219 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1220 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1221 B != E; ++B)
1222 this->processDag(*B);
1223 }
1224 else {
1225 this->processDag(Statement);
1226 }
1227 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001228
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001229 void operator()(const DagInit& Test, unsigned, bool) {
1230 this->operator()(&Test);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001231 }
1232 void operator()(const Init* Statement, unsigned) {
1233 this->operator()(Statement);
1234 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001235};
1236
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001237/// CheckForSuperfluousOptions - Check that there are no side
1238/// effect-free options (specified only in the OptionList). Otherwise,
1239/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001240void CheckForSuperfluousOptions (const RecordVector& Edges,
1241 const ToolDescriptions& ToolDescs,
1242 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001243 llvm::StringSet<> nonSuperfluousOptions;
1244
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001245 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001246 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001247 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1248 E = ToolDescs.end(); B != E; ++B) {
1249 const ToolDescription& TD = *(*B);
1250 ExtractOptionNames Callback(nonSuperfluousOptions);
1251 if (TD.Actions)
1252 WalkCase(TD.Actions, Callback, Callback);
1253 }
1254
1255 // Add all options mentioned in the 'case' clauses of the
1256 // OptionalEdges of the compilation graph to the set of
1257 // non-superfluous options.
1258 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1259 B != E; ++B) {
1260 const Record* Edge = *B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001261 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001262
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001263 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001264 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001265 }
1266
1267 // Check that all options in OptDescs belong to the set of
1268 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001269 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001270 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001271 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001272 if (!nonSuperfluousOptions.count(Val.Name)
1273 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001274 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001275 "Probable cause: this option is specified only in the OptionList.\n";
1276 }
1277}
1278
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001279/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1280bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1281 if (TestName == "single_input_file") {
1282 O << "InputFilenames.size() == 1";
1283 return true;
1284 }
1285 else if (TestName == "multiple_input_files") {
1286 O << "InputFilenames.size() > 1";
1287 return true;
1288 }
1289
1290 return false;
1291}
1292
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001293/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1294template <typename F>
1295void EmitListTest(const ListInit& L, const char* LogicOp,
1296 F Callback, raw_ostream& O)
1297{
1298 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1299 // of Dags...
1300 bool isFirst = true;
1301 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1302 if (isFirst)
1303 isFirst = false;
1304 else
Mikhail Glushenkovb7935e02010-01-01 04:40:54 +00001305 O << ' ' << LogicOp << ' ';
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001306 Callback(InitPtrToString(*B), O);
1307 }
1308}
1309
1310// Callbacks for use with EmitListTest.
1311
1312class EmitSwitchOn {
1313 const OptionDescriptions& OptDescs_;
1314public:
1315 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1316 {}
1317
1318 void operator()(const std::string& OptName, raw_ostream& O) const {
1319 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1320 O << OptDesc.GenVariableName();
1321 }
1322};
1323
1324class EmitEmptyTest {
1325 bool EmitNegate_;
1326 const OptionDescriptions& OptDescs_;
1327public:
1328 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1329 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1330 {}
1331
1332 void operator()(const std::string& OptName, raw_ostream& O) const {
1333 const char* Neg = (EmitNegate_ ? "!" : "");
1334 if (OptName == "o") {
1335 O << Neg << "OutputFilename.empty()";
1336 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001337 else if (OptName == "save-temps") {
1338 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1339 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001340 else {
1341 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1342 O << Neg << OptDesc.GenVariableName() << ".empty()";
1343 }
1344 }
1345};
1346
1347
1348/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1349bool EmitCaseTest1ArgList(const std::string& TestName,
1350 const DagInit& d,
1351 const OptionDescriptions& OptDescs,
1352 raw_ostream& O) {
Mikhail Glushenkov3a481e32010-01-01 03:50:51 +00001353 const ListInit& L = InitPtrToList(d.getArg(0));
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001354
1355 if (TestName == "any_switch_on") {
1356 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1357 return true;
1358 }
1359 else if (TestName == "switch_on") {
1360 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1361 return true;
1362 }
1363 else if (TestName == "any_not_empty") {
1364 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1365 return true;
1366 }
1367 else if (TestName == "any_empty") {
1368 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1369 return true;
1370 }
1371 else if (TestName == "not_empty") {
1372 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1373 return true;
1374 }
1375 else if (TestName == "empty") {
1376 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1377 return true;
1378 }
1379
1380 return false;
1381}
1382
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001383/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1384bool EmitCaseTest1ArgStr(const std::string& TestName,
1385 const DagInit& d,
1386 const OptionDescriptions& OptDescs,
1387 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001388 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001389
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001390 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001391 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001392 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001393 }
1394 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001395 O << "InLangs.count(\"" << OptName << "\") != 0";
1396 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001397 }
1398 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001399 // This works only for single-argument Tool::GenerateAction. Join
1400 // tools can process several files in different languages simultaneously.
1401
1402 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001403 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001404 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001405 }
1406 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001407 bool EmitNegate = (TestName == "not_empty");
1408 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001409 return true;
1410 }
1411
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001412 return false;
1413}
1414
1415/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1416bool EmitCaseTest1Arg(const std::string& TestName,
1417 const DagInit& d,
1418 const OptionDescriptions& OptDescs,
1419 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001420 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001421 if (typeid(*d.getArg(0)) == typeid(ListInit))
1422 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1423 else
1424 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1425}
1426
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001427/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001428bool EmitCaseTest2Args(const std::string& TestName,
1429 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001430 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001431 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001432 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001433 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001434 const std::string& OptName = InitPtrToString(d.getArg(0));
1435 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001436
1437 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001438 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001439 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1440 return true;
1441 }
1442 else if (TestName == "element_in_list") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001443 const OptionDescription& OptDesc = OptDescs.FindList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001444 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001445 O << "std::find(" << VarName << ".begin(),\n";
1446 O.indent(IndentLevel + Indent1)
1447 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001448 << OptArg << "\") != " << VarName << ".end()";
1449 return true;
1450 }
1451
1452 return false;
1453}
1454
1455// Forward declaration.
1456// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
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 Glushenkovb5ccfbf2008-05-30 06:10:19 +00001460
1461/// EmitLogicalOperationTest - Helper function used by
1462/// EmitCaseConstructHandler.
1463void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001464 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001465 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001466 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001467 O << '(';
1468 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001469 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001470 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001471 if (j != NumArgs - 1) {
1472 O << ")\n";
1473 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1474 }
1475 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001476 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001477 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001478 }
1479}
1480
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001481void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001482 const OptionDescriptions& OptDescs, raw_ostream& O)
1483{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001484 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001485 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1486 O << "! (";
1487 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1488 O << ")";
1489}
1490
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001491/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001492void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001493 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001494 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001495 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001496
1497 if (TestName == "and")
1498 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1499 else if (TestName == "or")
1500 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001501 else if (TestName == "not")
1502 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001503 else if (EmitCaseTest0Args(TestName, O))
1504 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001505 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1506 return;
1507 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1508 return;
1509 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001510 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001511}
1512
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001513
1514/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1515class EmitCaseTestCallback {
1516 bool EmitElseIf_;
1517 const OptionDescriptions& OptDescs_;
1518 raw_ostream& O_;
1519public:
1520
1521 EmitCaseTestCallback(bool EmitElseIf,
1522 const OptionDescriptions& OptDescs, raw_ostream& O)
1523 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1524 {}
1525
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001526 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001527 {
1528 if (GetOperatorName(Test) == "default") {
1529 O_.indent(IndentLevel) << "else {\n";
1530 }
1531 else {
1532 O_.indent(IndentLevel)
1533 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001534 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001535 O_ << ") {\n";
1536 }
1537 }
1538};
1539
1540/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1541template <typename F>
1542class EmitCaseStatementCallback {
1543 F Callback_;
1544 raw_ostream& O_;
1545public:
1546
1547 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1548 : Callback_(Callback), O_(O)
1549 {}
1550
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001551 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001552
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001553 // Ignore nested 'case' DAG.
1554 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001555 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case")) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001556 if (typeid(*Statement) == typeid(ListInit)) {
1557 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1558 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1559 B != E; ++B)
1560 Callback_(*B, (IndentLevel + Indent1), O_);
1561 }
1562 else {
1563 Callback_(Statement, (IndentLevel + Indent1), O_);
1564 }
1565 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001566 O_.indent(IndentLevel) << "}\n";
1567 }
1568
1569};
1570
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001571/// EmitCaseConstructHandler - Emit code that handles the 'case'
1572/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001573/// clause. Implemented on top of WalkCase.
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00001574/// Callback's type is void F(const Init* Statement, unsigned IndentLevel,
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001575/// raw_ostream& O).
1576/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001577/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1578/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001579template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001580void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001581 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001582 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001583 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001584 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1585 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001586}
1587
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001588/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001589/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1590/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001591void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001592 const char* Delimiters = " \t\n\v\f\r";
1593 enum TokenizerState
1594 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1595 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001596
1597 if (CmdLine.empty())
1598 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001599 Out.push_back("");
1600
1601 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1602 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001603
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001604 for (; B != E; ++B) {
1605 char cur_ch = CmdLine[B];
1606
1607 switch (cur_st) {
1608 case Normal:
1609 if (cur_ch == '$') {
1610 cur_st = SpecialCommand;
1611 break;
1612 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001613 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001614 // Skip whitespace
1615 B = CmdLine.find_first_not_of(Delimiters, B);
1616 if (B == std::string::npos) {
1617 B = E-1;
1618 continue;
1619 }
1620 --B;
1621 Out.push_back("");
1622 continue;
1623 }
1624 break;
1625
1626
1627 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001628 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001629 cur_st = Normal;
1630 Out.push_back("");
1631 continue;
1632 }
1633 if (cur_ch == '(') {
1634 Out.push_back("");
1635 cur_st = InsideSpecialCommand;
1636 continue;
1637 }
1638 break;
1639
1640 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001641 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001642 continue;
1643 }
1644 if (cur_ch == '\'') {
1645 cur_st = InsideQuotationMarks;
1646 Out.push_back("");
1647 continue;
1648 }
1649 if (cur_ch == ')') {
1650 cur_st = Normal;
1651 Out.push_back("");
1652 }
1653 if (cur_ch == ',') {
1654 continue;
1655 }
1656
1657 break;
1658
1659 case InsideQuotationMarks:
1660 if (cur_ch == '\'') {
1661 cur_st = InsideSpecialCommand;
1662 continue;
1663 }
1664 break;
1665 }
1666
1667 Out.back().push_back(cur_ch);
1668 }
1669}
1670
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001671/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1672/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1673/// SubstituteSpecialCommands().
1674StrVector::const_iterator
1675SubstituteCall (StrVector::const_iterator Pos,
1676 StrVector::const_iterator End,
1677 bool IsJoin, raw_ostream& O)
1678{
1679 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001680 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001681 const std::string& CmdName = *Pos;
1682
1683 if (CmdName == ")")
1684 throw "$CALL invocation: empty argument list!";
1685
1686 O << "hooks::";
1687 O << CmdName << "(";
1688
1689
1690 bool firstIteration = true;
1691 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001692 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001693 const std::string& Arg = *Pos;
1694 assert(Arg.size() != 0);
1695
1696 if (Arg[0] == ')')
1697 break;
1698
1699 if (firstIteration)
1700 firstIteration = false;
1701 else
1702 O << ", ";
1703
1704 if (Arg == "$INFILE") {
1705 if (IsJoin)
1706 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1707 else
1708 O << "inFile.c_str()";
1709 }
1710 else {
1711 O << '"' << Arg << '"';
1712 }
1713 }
1714
1715 O << ')';
1716
1717 return Pos;
1718}
1719
1720/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1721/// function used by SubstituteSpecialCommands().
1722StrVector::const_iterator
1723SubstituteEnv (StrVector::const_iterator Pos,
1724 StrVector::const_iterator End, raw_ostream& O)
1725{
1726 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001727 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001728 const std::string& EnvName = *Pos;
1729
1730 if (EnvName == ")")
1731 throw "$ENV invocation: empty argument list!";
1732
1733 O << "checkCString(std::getenv(\"";
1734 O << EnvName;
1735 O << "\"))";
1736
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001737 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001738
1739 return Pos;
1740}
1741
1742/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1743/// handler code. Helper function used by EmitCmdLineVecFill().
1744StrVector::const_iterator
1745SubstituteSpecialCommands (StrVector::const_iterator Pos,
1746 StrVector::const_iterator End,
1747 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001748{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001749
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001750 const std::string& cmd = *Pos;
1751
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001752 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001753 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001754 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001755 }
1756 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001757 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001758 }
1759 else {
1760 throw "Unknown special command: " + cmd;
1761 }
1762
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001763 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001764 const std::string& Leftover = *Pos;
1765 assert(Leftover.at(0) == ')');
1766 if (Leftover.size() != 1)
1767 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001768
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001769 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001770}
1771
1772/// EmitCmdLineVecFill - Emit code that fills in the command line
1773/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001774void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001775 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001776 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001777 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001778 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001779
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001780 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001781 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001782
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001783 StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001784
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001785 // Emit the command itself.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001786 assert(!StrVec[0].empty());
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001787 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001788 if (StrVec[0][0] == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001789 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1790 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001791 }
1792 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001793 O << '"' << StrVec[0] << '"';
1794 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001795 }
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001796 O << ";\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001797
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001798 // Go through the command arguments.
1799 assert(B <= E);
1800 for (; B != E; ++B) {
1801 const std::string& cmd = *B;
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001802
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001803 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001804 O.indent(IndentLevel);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001805
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001806 if (cmd.at(0) == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001807 O << "vec.push_back(std::make_pair(0, ";
1808 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1809 O << "));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001810 }
1811 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001812 O << "vec.push_back(std::make_pair(0, \"" << cmd << "\"));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001813 }
1814 }
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001815
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001816}
1817
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001818/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1819/// implement EmitActionHandler. Emits code for
1820/// handling the (forward) and (forward_as) option properties.
1821void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001822 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001823 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001824 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001825 const std::string& Name = NewName.empty()
1826 ? ("-" + D.Name)
1827 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001828 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001829
1830 switch (D.Type) {
1831 case OptionType::Switch:
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001832 O.indent(IndentLevel)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001833 << "vec.push_back(std::make_pair(" << D.GenVariableName()
1834 << ".getPosition(), \"" << Name << "\"));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001835 break;
1836 case OptionType::Parameter:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001837 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1838 << D.GenVariableName()
1839 <<".getPosition(), \"" << Name;
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001840
1841 if (!D.isForwardNotSplit()) {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001842 O << "\"));\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001843 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1844 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001845 << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001846 }
1847 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001848 O << "=\" + " << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001849 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001850 break;
1851 case OptionType::Prefix:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001852 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1853 << D.GenVariableName() << ".getPosition(), \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001854 << Name << "\" + "
1855 << D.GenVariableName() << "));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001856 break;
1857 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001858 O.indent(IndentLevel)
1859 << "for (" << D.GenTypeDeclaration()
1860 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1861 O.indent(IndentLevel)
1862 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001863 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1864 << ".getPosition(B - " << D.GenVariableName()
1865 << ".begin());\n";
1866 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001867 << Name << "\" + " << "*B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001868 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001869
1870 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001871 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001872 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001873 }
1874
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001875 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001876 break;
1877 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001878 O.indent(IndentLevel)
1879 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1880 << D.GenVariableName() << ".begin(),\n";
1881 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1882 << ".end() ; B != E;) {\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001883 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1884 << ".getPosition(B - " << D.GenVariableName()
1885 << ".begin());\n";
1886 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001887 << Name << "\"));\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001888
1889 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001890 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001891 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001892 }
1893
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001894 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001895 break;
1896 case OptionType::Alias:
1897 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001898 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001899 }
1900}
1901
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001902/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1903/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001904struct ActionHandlingCallbackBase
1905{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001906
1907 void onErrorDag(const DagInit& d,
1908 unsigned IndentLevel, raw_ostream& O) const
1909 {
1910 O.indent(IndentLevel)
1911 << "throw std::runtime_error(\"" <<
1912 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1913 : "Unknown error!")
1914 << "\");\n";
1915 }
1916
1917 void onWarningDag(const DagInit& d,
1918 unsigned IndentLevel, raw_ostream& O) const
1919 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001920 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001921 O.indent(IndentLevel) << "llvm::errs() << \""
1922 << InitPtrToString(d.getArg(0)) << "\";\n";
1923 }
1924
1925};
1926
1927/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1928/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001929
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001930class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001931
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001932typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1933(const DagInit&, unsigned, raw_ostream&) const;
1934
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001935class EmitActionHandlersCallback :
1936 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001937 public HandlerTable<EmitActionHandlersCallbackHandler>
1938{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001939 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001940
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001941 const OptionDescriptions& OptDescs;
1942
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001943 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1944 /// onAppendCmd and onOutputSuffix.
1945 void EmitHookInvocation(const std::string& Str,
1946 const char* BlockOpen, const char* BlockClose,
1947 unsigned IndentLevel, raw_ostream& O) const
1948 {
1949 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001950 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001951
1952 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1953 B != E; ++B) {
1954 const std::string& cmd = *B;
1955
1956 O.indent(IndentLevel) << BlockOpen;
1957
1958 if (cmd.at(0) == '$')
1959 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1960 else
1961 O << '"' << cmd << '"';
1962
1963 O << BlockClose;
1964 }
1965 }
1966
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001967 void onAppendCmd (const DagInit& Dag,
1968 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001969 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001970 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001971 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001972 "vec.push_back(std::make_pair(65536, ", "));\n",
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001973 IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001974 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001975
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001976 void onForward (const DagInit& Dag,
1977 unsigned IndentLevel, raw_ostream& O) const
1978 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001979 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001980 const std::string& Name = InitPtrToString(Dag.getArg(0));
1981 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1982 IndentLevel, "", O);
1983 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001984
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001985 void onForwardAs (const DagInit& Dag,
1986 unsigned IndentLevel, raw_ostream& O) const
1987 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001988 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001989 const std::string& Name = InitPtrToString(Dag.getArg(0));
1990 const std::string& NewName = InitPtrToString(Dag.getArg(1));
1991 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1992 IndentLevel, NewName, O);
1993 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001994
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001995 void onForwardValue (const DagInit& Dag,
1996 unsigned IndentLevel, raw_ostream& O) const
1997 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001998 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001999 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00002000 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002001
2002 if (D.isParameter()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002003 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2004 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002005 << D.GenVariableName() << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002006 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00002007 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002008 O.indent(IndentLevel) << "for (cl::list<std::string>::iterator B = "
2009 << D.GenVariableName() << ".begin(), \n";
2010 O.indent(IndentLevel + Indent1) << " E = " << D.GenVariableName()
2011 << ".end(); B != E; ++B)\n";
2012 O.indent(IndentLevel) << "{\n";
2013 O.indent(IndentLevel + Indent1)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002014 << "unsigned pos = " << D.GenVariableName()
2015 << ".getPosition(B - " << D.GenVariableName()
2016 << ".begin());\n";
2017 O.indent(IndentLevel + Indent1)
2018 << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002019 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002020 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002021 }
2022
2023 void onForwardTransformedValue (const DagInit& Dag,
2024 unsigned IndentLevel, raw_ostream& O) const
2025 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002026 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002027 const std::string& Name = InitPtrToString(Dag.getArg(0));
2028 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00002029 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002030
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002031 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2032 << D.GenVariableName() << ".getPosition("
2033 << (D.isList() ? "0" : "") << "), "
2034 << "hooks::" << Hook << "(" << D.GenVariableName()
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002035 << (D.isParameter() ? ".c_str()" : "") << ")));\n";
2036 }
2037
2038 void onNoOutFile (const DagInit& Dag,
2039 unsigned IndentLevel, raw_ostream& O) const
2040 {
2041 CheckNumberOfArguments(Dag, 0);
2042 O.indent(IndentLevel) << "no_out_file = true;\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002043 }
2044
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002045 void onOutputSuffix (const DagInit& Dag,
2046 unsigned IndentLevel, raw_ostream& O) const
2047 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002048 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002049 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
2050 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002051 }
2052
2053 void onStopCompilation (const DagInit& Dag,
2054 unsigned IndentLevel, raw_ostream& O) const
2055 {
2056 O.indent(IndentLevel) << "stop_compilation = true;\n";
2057 }
2058
2059
2060 void onUnpackValues (const DagInit& Dag,
2061 unsigned IndentLevel, raw_ostream& O) const
2062 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002063 throw "'unpack_values' is deprecated. "
2064 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002065 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002066
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002067 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002068
2069 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
2070 : OptDescs(OD)
2071 {
2072 if (!staticMembersInitialized_) {
2073 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
2074 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
2075 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
2076 AddHandler("forward", &EmitActionHandlersCallback::onForward);
2077 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002078 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
2079 AddHandler("forward_transformed_value",
2080 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002081 AddHandler("no_out_file",
2082 &EmitActionHandlersCallback::onNoOutFile);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002083 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
2084 AddHandler("stop_compilation",
2085 &EmitActionHandlersCallback::onStopCompilation);
2086 AddHandler("unpack_values",
2087 &EmitActionHandlersCallback::onUnpackValues);
2088
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002089
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002090 staticMembersInitialized_ = true;
2091 }
2092 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002093
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002094 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002095 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002096 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002097 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002098 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002099};
2100
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002101void EmitGenerateActionMethodHeader(const ToolDescription& D,
2102 bool IsJoin, raw_ostream& O)
2103{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002104 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002105 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002106 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002107 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002108
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002109 O.indent(Indent2) << "bool HasChildren,\n";
2110 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2111 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2112 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2113 O.indent(Indent1) << "{\n";
2114 O.indent(Indent2) << "std::string cmd;\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002115 O.indent(Indent2) << "std::string out_file;\n";
2116 O.indent(Indent2) << "std::vector<std::pair<unsigned, std::string> > vec;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002117 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002118 O.indent(Indent2) << "bool no_out_file = false;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002119 O.indent(Indent2) << "const char* output_suffix = \""
2120 << D.OutputSuffix << "\";\n";
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002121}
2122
2123// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2124// Tool::GenerateAction() method.
2125void EmitGenerateActionMethod (const ToolDescription& D,
2126 const OptionDescriptions& OptDescs,
2127 bool IsJoin, raw_ostream& O) {
2128
2129 EmitGenerateActionMethodHeader(D, IsJoin, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002130
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002131 if (!D.CmdLine)
2132 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002133
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002134 // Process the 'command' property.
2135 O << '\n';
2136 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2137 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002138
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002139 // Process the 'actions' list of this tool.
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002140 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002141 EmitCaseConstructHandler(D.Actions, Indent2,
2142 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002143 false, OptDescs, O);
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002144 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002145
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002146 // Input file (s)
2147 if (!D.InFileOption.empty()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002148 O.indent(Indent2)
2149 << "vec.push_back(std::make_pair(InputFilenames.getPosition(0), \""
2150 << D.InFileOption << "\");\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002151 }
2152
2153 if (IsJoin) {
2154 O.indent(Indent2)
2155 << "for (PathVector::const_iterator B = inFiles.begin(),\n";
2156 O.indent(Indent3) << "E = inFiles.end(); B != E; ++B)\n";
2157 O.indent(Indent2) << "{\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002158 O.indent(Indent3) << "vec.push_back(std::make_pair("
2159 << "InputFilenames.getPosition(B - inFiles.begin()), "
2160 << "B->str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002161 O.indent(Indent2) << "}\n";
2162 }
2163 else {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002164 O.indent(Indent2) << "vec.push_back(std::make_pair("
2165 << "InputFilenames.getPosition(0), inFile.str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002166 }
2167
2168 // Output file
2169 O.indent(Indent2) << "if (!no_out_file) {\n";
2170 if (!D.OutFileOption.empty())
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002171 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002172 << D.OutFileOption << "\"));\n";
2173
2174 O.indent(Indent3) << "out_file = this->OutFilename("
2175 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
2176 O.indent(Indent4) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002177 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, out_file));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002178
2179 O.indent(Indent2) << "}\n\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002180
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002181 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002182 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002183 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002184 O.indent(Indent3) << "for (cl::list<std::string>::iterator B = "
2185 << SinkOptionName << ".begin(), E = " << SinkOptionName
2186 << ".end(); B != E; ++B)\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002187 O.indent(Indent4) << "vec.push_back(std::make_pair(" << SinkOptionName
2188 << ".getPosition(B - " << SinkOptionName
2189 << ".begin()), *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002190 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002191 }
2192
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002193 O.indent(Indent2) << "return Action(cmd, this->SortArgs(vec), "
2194 << "stop_compilation, out_file);\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002195 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002196}
2197
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002198/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2199/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002200void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2201 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002202 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002203 if (!ToolDesc.isJoin()) {
2204 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
2205 O.indent(Indent2) << "bool HasChildren,\n";
2206 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2207 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2208 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2209 O.indent(Indent1) << "{\n";
2210 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
2211 << " is not a Join tool!\");\n";
2212 O.indent(Indent1) << "}\n\n";
2213 }
2214 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002215 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002216 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002217
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002218 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002219}
2220
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002221/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2222/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002223void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002224 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2225 O.indent(Indent2) << "return InputLanguages_;\n";
2226 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002227
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002228 if (D.OutLanguage.empty())
2229 throw "Tool " + D.Name + " has no 'out_language' property!";
2230
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002231 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2232 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2233 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002234}
2235
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002236/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002237void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002238 O.indent(Indent1) << "const char* Name() const {\n";
2239 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2240 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002241}
2242
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002243/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2244/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002245void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002246 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002247 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002248 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002249 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002250 O.indent(Indent2) << "return false;\n";
2251 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002252}
2253
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002254/// EmitWorksOnEmptyCallback - Callback used by EmitWorksOnEmptyMethod in
2255/// conjunction with EmitCaseConstructHandler.
2256void EmitWorksOnEmptyCallback (const Init* Value,
2257 unsigned IndentLevel, raw_ostream& O) {
2258 CheckBooleanConstant(Value);
2259 O.indent(IndentLevel) << "return " << Value->getAsString() << ";\n";
2260}
2261
2262/// EmitWorksOnEmptyMethod - Emit the WorksOnEmpty() method for a given Tool
2263/// class.
2264void EmitWorksOnEmptyMethod (const ToolDescription& D,
2265 const OptionDescriptions& OptDescs,
2266 raw_ostream& O)
2267{
2268 O.indent(Indent1) << "bool WorksOnEmpty() const {\n";
2269 if (D.OnEmpty == 0)
2270 O.indent(Indent2) << "return false;\n";
2271 else
2272 EmitCaseConstructHandler(D.OnEmpty, Indent2, EmitWorksOnEmptyCallback,
2273 /*EmitElseIf = */ true, OptDescs, O);
2274 O.indent(Indent1) << "}\n\n";
2275}
2276
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002277/// EmitStaticMemberDefinitions - Emit static member definitions for a
2278/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002279void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002280 if (D.InLanguage.empty())
2281 throw "Tool " + D.Name + " has no 'in_language' property!";
2282
2283 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2284 for (StrVector::const_iterator B = D.InLanguage.begin(),
2285 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002286 O << '\"' << *B << "\", ";
2287 O << "0};\n\n";
2288}
2289
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002290/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002291void EmitToolClassDefinition (const ToolDescription& D,
2292 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002293 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002294 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002295 return;
2296
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002297 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002298 O << "class " << D.Name << " : public ";
2299 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002300 O << "JoinTool";
2301 else
2302 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002303
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002304 O << " {\nprivate:\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002305 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002306
2307 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002308 EmitNameMethod(D, O);
2309 EmitInOutLanguageMethods(D, O);
2310 EmitIsJoinMethod(D, O);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002311 EmitWorksOnEmptyMethod(D, OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002312 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002313
2314 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002315 O << "};\n";
2316
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002317 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002318
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002319}
2320
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002321/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002322/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002323void EmitOptionDefinitions (const OptionDescriptions& descs,
2324 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002325 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002326{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002327 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002328
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002329 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002330 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002331 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002332 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002333
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002334 if (val.Type == OptionType::Alias) {
2335 Aliases.push_back(val);
2336 continue;
2337 }
2338
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002339 if (val.isExtern())
2340 O << "extern ";
2341
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002342 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002343 << val.GenVariableName();
2344
2345 if (val.isExtern()) {
2346 O << ";\n";
2347 continue;
2348 }
2349
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002350 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002351
2352 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2353 O << ", cl::Prefix";
2354
2355 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002356 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002357 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002358 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002359 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002360 }
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002361
2362 if (val.isOptional())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002363 O << ", cl::Optional";
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002364
2365 if (val.isOneOrMore())
2366 O << ", cl::OneOrMore";
2367
2368 if (val.isZeroOrMore())
2369 O << ", cl::ZeroOrMore";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002370
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002371 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002372 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002373 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002374 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002375
2376 if (val.isCommaSeparated())
2377 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002378
2379 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002380 O << ", cl::multi_val(" << val.MultiVal << ')';
2381
2382 if (val.InitVal) {
2383 const std::string& str = val.InitVal->getAsString();
2384 O << ", cl::init(" << str << ')';
2385 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002386
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002387 if (!val.Help.empty())
2388 O << ", cl::desc(\"" << val.Help << "\")";
2389
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002390 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002391 }
2392
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002393 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002394 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002395 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002396 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002397
2398 O << val.GenTypeDeclaration() << ' '
2399 << val.GenVariableName()
2400 << "(\"" << val.Name << '\"';
2401
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002402 const OptionDescription& D = descs.FindOption(val.Help);
2403 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002404
2405 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2406 }
2407
2408 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002409 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002410 O << (HasExterns ? "extern cl" : "cl")
2411 << "::list<std::string> " << SinkOptionName
2412 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002413
2414 O << '\n';
2415}
2416
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002417/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002418/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002419
2420class EmitPreprocessOptionsCallback;
2421
2422typedef void
2423(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2424(const DagInit&, unsigned, raw_ostream&) const;
2425
2426class EmitPreprocessOptionsCallback :
2427 public ActionHandlingCallbackBase,
2428 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2429{
2430 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002431 typedef void
2432 (EmitPreprocessOptionsCallback::* HandlerImpl)
2433 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002434
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002435 const OptionDescriptions& OptDescs_;
2436
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002437 void onListOrDag(const DagInit& d, HandlerImpl h,
2438 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002439 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002440 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002441 const Init* I = d.getArg(0);
2442
2443 // If I is a list, apply h to each element.
2444 if (typeid(*I) == typeid(ListInit)) {
2445 const ListInit& L = *static_cast<const ListInit*>(I);
2446 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B)
2447 ((this)->*(h))(*B, IndentLevel, O);
2448 }
2449 // Otherwise, apply h to I.
2450 else {
2451 ((this)->*(h))(I, IndentLevel, O);
2452 }
2453 }
2454
2455 void onUnsetOptionImpl(const Init* I,
2456 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002457 {
2458 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002459 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002460
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002461 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002462 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2463 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002464 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002465 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2466 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002467 else if (OptDesc.isList()) {
2468 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2469 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002470 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002471 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002472 }
2473 }
2474
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002475 void onUnsetOption(const DagInit& d,
2476 unsigned IndentLevel, raw_ostream& O) const
2477 {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002478 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2479 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002480 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002481
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002482 void onSetOptionImpl(const DagInit& d,
2483 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002484 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002485 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002486 const Init* Value = d.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002487 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2488
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002489 if (OptDesc.isList()) {
2490 const ListInit& List = InitPtrToList(Value);
2491
2492 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2493 for (ListInit::const_iterator B = List.begin(), E = List.end();
2494 B != E; ++B) {
2495 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".push_back(\""
2496 << InitPtrToString(*B) << "\");\n";
2497 }
2498 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002499 else if (OptDesc.isSwitch()) {
2500 CheckBooleanConstant(Value);
2501 O.indent(IndentLevel) << OptDesc.GenVariableName()
2502 << " = " << Value->getAsString() << ";\n";
2503 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002504 else if (OptDesc.isParameter()) {
2505 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002506 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002507 << " = \"" << Str << "\";\n";
2508 }
2509 else {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002510 throw "Can't apply 'set_option' to alias option -" + OptName + " !";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002511 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002512 }
2513
2514 void onSetSwitch(const Init* I,
2515 unsigned IndentLevel, raw_ostream& O) const {
2516 const std::string& OptName = InitPtrToString(I);
2517 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2518
2519 if (OptDesc.isSwitch())
2520 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2521 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002522 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002523 }
2524
2525 void onSetOption(const DagInit& d,
2526 unsigned IndentLevel, raw_ostream& O) const
2527 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002528 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002529
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002530 // Two arguments: (set_option "parameter", VALUE), where VALUE can be a
2531 // boolean, a string or a string list.
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002532 if (d.getNumArgs() > 1)
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002533 this->onSetOptionImpl(d, IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002534 // One argument: (set_option "switch")
2535 // or (set_option ["switch1", "switch2", ...])
2536 else
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002537 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2538 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002539 }
2540
2541public:
2542
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002543 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002544 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002545 {
2546 if (!staticMembersInitialized_) {
2547 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2548 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2549 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002550 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002551
2552 staticMembersInitialized_ = true;
2553 }
2554 }
2555
2556 void operator()(const Init* I,
2557 unsigned IndentLevel, raw_ostream& O) const
2558 {
2559 InvokeDagInitHandler(this, I, IndentLevel, O);
2560 }
2561
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002562};
2563
2564/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2565void EmitPreprocessOptions (const RecordKeeper& Records,
2566 const OptionDescriptions& OptDecs, raw_ostream& O)
2567{
2568 O << "void PreprocessOptionsLocal() {\n";
2569
2570 const RecordVector& OptionPreprocessors =
2571 Records.getAllDerivedDefinitions("OptionPreprocessor");
2572
2573 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2574 E = OptionPreprocessors.end(); B!=E; ++B) {
2575 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002576 EmitCaseConstructHandler(Case, Indent1,
2577 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002578 false, OptDecs, O);
2579 }
2580
2581 O << "}\n\n";
2582}
2583
2584/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002585void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002586{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002587 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002588
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002589 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002590 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002591
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002592 // It is allowed for a plugin to have no language map.
2593 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002594
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002595 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2596 if (!LangsToSuffixesList)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00002597 throw "Error in the language map definition!";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002598
2599 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002600 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002601
2602 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2603 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2604
2605 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002606 O.indent(Indent1) << "langMap[\""
2607 << InitPtrToString(Suffixes->getElement(i))
2608 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002609 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002610 }
2611
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002612 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002613}
2614
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002615/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2616/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002617void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002618 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002619 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002620 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002621
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002622 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002623 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002624 }
2625 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002626 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002627 }
2628 else if (OpName == "error") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002629 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002630 O.indent(IndentLevel) << "throw std::runtime_error(\""
2631 << InitPtrToString(d.getArg(0))
2632 << "\");\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002633 return;
2634 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002635 else {
2636 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002637 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002638 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002639
2640 if (d.getNumArgs() > 0)
2641 O << InitPtrToInt(d.getArg(0)) << ";\n";
2642 else
2643 O << "2;\n";
2644
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002645}
2646
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002647/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002648void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002649 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002650 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002651
2652 // Class constructor.
2653 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002654 << "public:\n";
2655 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2656 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002657
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002658 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002659 O.indent(Indent1)
2660 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2661 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002662
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002663 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002664 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002665
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002666 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002667 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002668}
2669
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002670/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002671void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002672 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002673 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002674 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002675 for (RecordVector::const_iterator B = EdgeVector.begin(),
2676 E = EdgeVector.end(); B != E; ++B) {
2677 const Record* Edge = *B;
2678 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002679 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002680
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002681 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002682 EmitEdgeClass(i, NodeB, &Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002683 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002684 }
2685}
2686
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002687/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002688/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002689void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002690 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002691 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002692{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002693 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002694
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002695 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2696 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002697 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002698
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002699 O << '\n';
2700
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002701 // Insert edges.
2702
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002703 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002704 for (RecordVector::const_iterator B = EdgeVector.begin(),
2705 E = EdgeVector.end(); B != E; ++B) {
2706 const Record* Edge = *B;
2707 const std::string& NodeA = Edge->getValueAsString("a");
2708 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002709 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002710
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002711 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002712
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002713 if (IsDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002714 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002715 else
2716 O << "new Edge" << i << "()";
2717
2718 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002719 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002720 }
2721
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002722 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002723}
2724
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002725/// HookInfo - Information about the hook type and number of arguments.
2726struct HookInfo {
2727
2728 // A hook can either have a single parameter of type std::vector<std::string>,
2729 // or NumArgs parameters of type const char*.
2730 enum HookType { ListHook, ArgHook };
2731
2732 HookType Type;
2733 unsigned NumArgs;
2734
2735 HookInfo() : Type(ArgHook), NumArgs(1)
2736 {}
2737
2738 HookInfo(HookType T) : Type(T), NumArgs(1)
2739 {}
2740
2741 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2742 {}
2743};
2744
2745typedef llvm::StringMap<HookInfo> HookInfoMap;
2746
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002747/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002748/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002749/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002750class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002751 HookInfoMap& HookNames_;
2752 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002753public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002754 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2755 : HookNames_(HookNames), OptDescs_(OptDescs)
2756 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002757
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002758 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002759 const std::string& Name = GetOperatorName(Dag);
2760
2761 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002762 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002763 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2764 const std::string& HookName = InitPtrToString(Dag.getArg(1));
2765 const OptionDescription& D = OptDescs_.FindOption(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002766
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002767 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2768 : HookInfo::ArgHook);
2769 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002770 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002771 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002772 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2773 }
2774 }
2775
2776 void onCmdLine(const std::string& Cmd) {
2777 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002778 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002779
2780 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2781 B != E; ++B) {
2782 const std::string& cmd = *B;
2783
2784 if (cmd == "$CALL") {
2785 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002786 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002787 const std::string& HookName = *B;
2788
2789 if (HookName.at(0) == ')')
2790 throw "$CALL invoked with no arguments!";
2791
2792 while (++B != E && B->at(0) != ')') {
2793 ++NumArgs;
2794 }
2795
2796 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2797
2798 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2799 H->second.Type != HookInfo::ArgHook)
2800 throw "Overloading of hooks is not allowed. Overloaded hook: "
2801 + HookName;
2802 else
2803 HookNames_[HookName] = HookInfo(NumArgs);
2804 }
2805 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002806 }
2807
2808 void operator()(const Init* Arg) {
2809
2810 // We're invoked on an action (either a dag or a dag list).
2811 if (typeid(*Arg) == typeid(DagInit)) {
2812 const DagInit& Dag = InitPtrToDag(Arg);
2813 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002814 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002815 }
2816 else if (typeid(*Arg) == typeid(ListInit)) {
2817 const ListInit& List = InitPtrToList(Arg);
2818 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2819 ++B) {
2820 const DagInit& Dag = InitPtrToDag(*B);
2821 this->onAction(Dag);
2822 }
2823 return;
2824 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002825
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002826 // We're invoked on a command line.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002827 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002828 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002829
2830 void operator()(const DagInit* Test, unsigned, bool) {
2831 this->operator()(Test);
2832 }
2833 void operator()(const Init* Statement, unsigned) {
2834 this->operator()(Statement);
2835 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002836};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002837
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002838/// FillInHookNames - Actually extract the hook names from all command
2839/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002840void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002841 const OptionDescriptions& OptDescs,
2842 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002843{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002844 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002845 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2846 E = ToolDescs.end(); B != E; ++B) {
2847 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002848
2849 // Look for 'forward_transformed_value' in 'actions'.
2850 if (D.Actions)
2851 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2852
2853 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002854 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002855 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002856 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002857 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002858 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002859 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002860 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002861 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002862 }
2863}
2864
2865/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2866/// property records and emit hook function declaration for each
2867/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002868void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2869 const OptionDescriptions& OptDescs, raw_ostream& O) {
2870 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002871
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002872 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002873 if (HookNames.empty())
2874 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002875
2876 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002877 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002878 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002879 const char* HookName = B->first();
2880 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002881
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002882 O.indent(Indent1) << "std::string " << HookName << "(";
2883
2884 if (Info.Type == HookInfo::ArgHook) {
2885 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2886 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2887 }
2888 }
2889 else {
2890 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002891 }
2892
2893 O <<");\n";
2894 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002895 O << "}\n\n";
2896}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002897
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002898/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002899void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002900 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2901 O.indent(Indent1) << "int Priority() const { return "
2902 << Priority << "; }\n\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002903 O.indent(Indent1) << "void PreprocessOptions() const\n";
2904 O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002905 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2906 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2907 O.indent(Indent1)
2908 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2909 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2910 << "};\n\n"
2911 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002912}
2913
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002914/// EmitIncludes - Emit necessary #include directives and some
2915/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002916void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002917 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2918 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002919 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002920 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2921 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002922
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002923 << "#include \"llvm/Support/CommandLine.h\"\n"
2924 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002925
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002926 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002927 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002928 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002929 << "#include <stdexcept>\n\n"
2930
2931 << "using namespace llvm;\n"
2932 << "using namespace llvmc;\n\n"
2933
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002934 << "extern cl::opt<std::string> OutputFilename;\n\n"
2935
2936 << "inline const char* checkCString(const char* s)\n"
2937 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002938}
2939
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002940
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002941/// PluginData - Holds all information about a plugin.
2942struct PluginData {
2943 OptionDescriptions OptDescs;
2944 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002945 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002946 ToolDescriptions ToolDescs;
2947 RecordVector Edges;
2948 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002949};
2950
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002951/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002952/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002953bool HasSink(const ToolDescriptions& ToolDescs) {
2954 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2955 E = ToolDescs.end(); B != E; ++B)
2956 if ((*B)->isSink())
2957 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002958
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002959 return false;
2960}
2961
2962/// HasExterns - Go through the list of option descriptions and check
2963/// if there are any external options.
2964bool HasExterns(const OptionDescriptions& OptDescs) {
2965 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2966 E = OptDescs.end(); B != E; ++B)
2967 if (B->second.isExtern())
2968 return true;
2969
2970 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002971}
2972
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002973/// CollectPluginData - Collect tool and option properties,
2974/// compilation graph edges and plugin priority from the parse tree.
2975void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2976 // Collect option properties.
2977 const RecordVector& OptionLists =
2978 Records.getAllDerivedDefinitions("OptionList");
2979 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2980 Data.OptDescs);
2981
2982 // Collect tool properties.
2983 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2984 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2985 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002986 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002987
2988 // Collect compilation graph edges.
2989 const RecordVector& CompilationGraphs =
2990 Records.getAllDerivedDefinitions("CompilationGraph");
2991 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2992 Data.Edges);
2993
2994 // Calculate the priority of this plugin.
2995 const RecordVector& Priorities =
2996 Records.getAllDerivedDefinitions("PluginPriority");
2997 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002998}
2999
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003000/// CheckPluginData - Perform some sanity checks on the collected data.
3001void CheckPluginData(PluginData& Data) {
3002 // Filter out all tools not mentioned in the compilation graph.
3003 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003004
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003005 // Typecheck the compilation graph.
3006 TypecheckGraph(Data.Edges, Data.ToolDescs);
3007
3008 // Check that there are no options without side effects (specified
3009 // only in the OptionList).
3010 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003011}
3012
Daniel Dunbar1a551802009-07-03 00:10:29 +00003013void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003014 // Emit file header.
3015 EmitIncludes(O);
3016
3017 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00003018 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003019
3020 // Emit hook declarations.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003021 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003022
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003023 O << "namespace {\n\n";
3024
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00003025 // Emit PreprocessOptionsLocal() function.
3026 EmitPreprocessOptions(Records, Data.OptDescs, O);
3027
3028 // Emit PopulateLanguageMapLocal() function
3029 // (language map maps from file extensions to language names).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003030 EmitPopulateLanguageMap(Records, O);
3031
3032 // Emit Tool classes.
3033 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
3034 E = Data.ToolDescs.end(); B!=E; ++B)
3035 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
3036
3037 // Emit Edge# classes.
3038 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
3039
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00003040 // Emit PopulateCompilationGraphLocal() function.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003041 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
3042
3043 // Emit code for plugin registration.
3044 EmitRegisterPlugin(Data.Priority, O);
3045
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003046 O << "} // End anonymous namespace.\n\n";
3047
3048 // Force linkage magic.
3049 O << "namespace llvmc {\n";
3050 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
3051 O << "}\n";
3052
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003053 // EOF
3054}
3055
3056
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003057// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00003058}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003059
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003060/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00003061void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003062 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003063 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003064
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003065 CollectPluginData(Records, Data);
3066 CheckPluginData(Data);
3067
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00003068 this->EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003069 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003070
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003071 } catch (std::exception& Error) {
3072 throw Error.what() + std::string(" - usually this means a syntax error.");
3073 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003074}