blob: 26e9fa4a9dff3a4fb595c9e31e8f1c054bcf04ec [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 Glushenkov7a574542010-08-20 11:24:51 +000052const char * const SinkOptionName = "SinkOption";
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 Glushenkovfc97aeb2010-07-19 03:16:25 +0000191 enum OptionType { Alias, Switch, SwitchList,
192 Parameter, ParameterList, 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) {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000199 return (t == SwitchList || t == ParameterList || t == PrefixList);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000200 }
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 Glushenkovfc97aeb2010-07-19 03:16:25 +0000206 bool IsSwitchList (OptionType t) {
207 return (t == SwitchList);
208 }
209
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000210 bool IsParameter (OptionType t) {
211 return (t == Parameter || t == Prefix);
212 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000213
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000214}
215
216OptionType::OptionType stringToOptionType(const std::string& T) {
217 if (T == "alias_option")
218 return OptionType::Alias;
219 else if (T == "switch_option")
220 return OptionType::Switch;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000221 else if (T == "switch_list_option")
222 return OptionType::SwitchList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000223 else if (T == "parameter_option")
224 return OptionType::Parameter;
225 else if (T == "parameter_list_option")
226 return OptionType::ParameterList;
227 else if (T == "prefix_option")
228 return OptionType::Prefix;
229 else if (T == "prefix_list_option")
230 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000231 else
232 throw "Unknown option type: " + T + '!';
233}
234
235namespace OptionDescriptionFlags {
236 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000237 ReallyHidden = 0x4, OneOrMore = 0x8,
238 Optional = 0x10, CommaSeparated = 0x20,
239 ForwardNotSplit = 0x40, ZeroOrMore = 0x80 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000240}
241
242/// OptionDescription - Represents data contained in a single
243/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000244struct OptionDescription {
245 OptionType::OptionType Type;
246 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000247 unsigned Flags;
248 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000249 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000250 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000251
252 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000253 const std::string& n = "",
254 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000255 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000256 {}
257
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000258 /// GenTypeDeclaration - Returns the C++ variable type of this
259 /// option.
260 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000261
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000262 /// GenVariableName - Returns the variable name used in the
263 /// generated C++ code.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000264 std::string GenVariableName() const
265 { return "autogenerated::" + GenOptionType() + EscapeVariableName(Name); }
266
267 /// GenPlainVariableName - Returns the variable name without the namespace
268 /// prefix.
269 std::string GenPlainVariableName() const
270 { return GenOptionType() + EscapeVariableName(Name); }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000271
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000272 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000273 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000274
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000275 /// CheckConsistency - Check that the flags are consistent.
276 void CheckConsistency() const;
277
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000278 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000279
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000280 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000281
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000282 bool isMultiVal() const;
283
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000284 bool isCommaSeparated() const;
285 void setCommaSeparated();
286
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000287 bool isForwardNotSplit() const;
288 void setForwardNotSplit();
289
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000290 bool isRequired() const;
291 void setRequired();
292
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000293 bool isOneOrMore() const;
294 void setOneOrMore();
295
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000296 bool isZeroOrMore() const;
297 void setZeroOrMore();
298
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000299 bool isOptional() const;
300 void setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000301
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000302 bool isHidden() const;
303 void setHidden();
304
305 bool isReallyHidden() const;
306 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000307
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000308 bool isSwitch() const
309 { return OptionType::IsSwitch(this->Type); }
310
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000311 bool isSwitchList() const
312 { return OptionType::IsSwitchList(this->Type); }
313
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000314 bool isParameter() const
315 { return OptionType::IsParameter(this->Type); }
316
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000317 bool isList() const
318 { return OptionType::IsList(this->Type); }
319
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000320 bool isParameterList() const
321 { return (OptionType::IsList(this->Type)
322 && !OptionType::IsSwitchList(this->Type)); }
323
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000324private:
325
326 // GenOptionType - Helper function used by GenVariableName().
327 std::string GenOptionType() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000328};
329
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000330void OptionDescription::CheckConsistency() const {
331 unsigned i = 0;
332
333 i += this->isRequired();
334 i += this->isOptional();
335 i += this->isOneOrMore();
336 i += this->isZeroOrMore();
337
338 if (i > 1) {
339 throw "Only one of (required), (optional), (one_or_more) or "
340 "(zero_or_more) properties is allowed!";
341 }
342}
343
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000344void OptionDescription::Merge (const OptionDescription& other)
345{
346 if (other.Type != Type)
347 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000348
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000349 if (Help == other.Help || Help == DefaultHelpString)
350 Help = other.Help;
351 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000352 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000353 " defined for option " + Name + "\n";
354 }
355
356 Flags |= other.Flags;
357}
358
359bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000360 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000361}
362
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000363bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000364 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000365}
366
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000367bool OptionDescription::isCommaSeparated() const {
368 return Flags & OptionDescriptionFlags::CommaSeparated;
369}
370void OptionDescription::setCommaSeparated() {
371 Flags |= OptionDescriptionFlags::CommaSeparated;
372}
373
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000374bool OptionDescription::isForwardNotSplit() const {
375 return Flags & OptionDescriptionFlags::ForwardNotSplit;
376}
377void OptionDescription::setForwardNotSplit() {
378 Flags |= OptionDescriptionFlags::ForwardNotSplit;
379}
380
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000381bool OptionDescription::isRequired() const {
382 return Flags & OptionDescriptionFlags::Required;
383}
384void OptionDescription::setRequired() {
385 Flags |= OptionDescriptionFlags::Required;
386}
387
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000388bool OptionDescription::isOneOrMore() const {
389 return Flags & OptionDescriptionFlags::OneOrMore;
390}
391void OptionDescription::setOneOrMore() {
392 Flags |= OptionDescriptionFlags::OneOrMore;
393}
394
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000395bool OptionDescription::isZeroOrMore() const {
396 return Flags & OptionDescriptionFlags::ZeroOrMore;
397}
398void OptionDescription::setZeroOrMore() {
399 Flags |= OptionDescriptionFlags::ZeroOrMore;
400}
401
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000402bool OptionDescription::isOptional() const {
403 return Flags & OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000404}
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000405void OptionDescription::setOptional() {
406 Flags |= OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000407}
408
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000409bool OptionDescription::isHidden() const {
410 return Flags & OptionDescriptionFlags::Hidden;
411}
412void OptionDescription::setHidden() {
413 Flags |= OptionDescriptionFlags::Hidden;
414}
415
416bool OptionDescription::isReallyHidden() const {
417 return Flags & OptionDescriptionFlags::ReallyHidden;
418}
419void OptionDescription::setReallyHidden() {
420 Flags |= OptionDescriptionFlags::ReallyHidden;
421}
422
423const char* OptionDescription::GenTypeDeclaration() const {
424 switch (Type) {
425 case OptionType::Alias:
426 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000427 case OptionType::PrefixList:
428 case OptionType::ParameterList:
429 return "cl::list<std::string>";
430 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000431 return "cl::opt<bool>";
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000432 case OptionType::SwitchList:
433 return "cl::list<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000434 case OptionType::Parameter:
435 case OptionType::Prefix:
436 default:
437 return "cl::opt<std::string>";
438 }
439}
440
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000441std::string OptionDescription::GenOptionType() const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000442 switch (Type) {
443 case OptionType::Alias:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000444 return "Alias_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000445 case OptionType::PrefixList:
446 case OptionType::ParameterList:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000447 return "List_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000448 case OptionType::Switch:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000449 return "Switch_";
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000450 case OptionType::SwitchList:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000451 return "SwitchList_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000452 case OptionType::Prefix:
453 case OptionType::Parameter:
454 default:
Mikhail Glushenkov7a574542010-08-20 11:24:51 +0000455 return "Parameter_";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000456 }
457}
458
459/// OptionDescriptions - An OptionDescription array plus some helper
460/// functions.
461class OptionDescriptions {
462 typedef StringMap<OptionDescription> container_type;
463
464 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000465 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000466
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000467public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000468 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000469 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000470
471 // Wrappers for FindOption that throw an exception in case the option has a
472 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000473 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000474 const OptionDescription& FindParameter(const std::string& OptName) const;
475 const OptionDescription& FindList(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000476 const OptionDescription& FindParameterList(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000477 const OptionDescription&
478 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000479 const OptionDescription&
480 FindParameterListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000481
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000482 /// insertDescription - Insert new OptionDescription into
483 /// OptionDescriptions list
484 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000485
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000486 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000487 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000488 const_iterator begin() const { return Descriptions.begin(); }
489 const_iterator end() const { return Descriptions.end(); }
490};
491
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000492const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000493OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000494 const_iterator I = Descriptions.find(OptName);
495 if (I != Descriptions.end())
496 return I->second;
497 else
498 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000499}
500
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000501const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000502OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000503 const OptionDescription& OptDesc = this->FindOption(OptName);
504 if (!OptDesc.isSwitch())
505 throw OptName + ": incorrect option type - should be a switch!";
506 return OptDesc;
507}
508
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000509const OptionDescription&
510OptionDescriptions::FindList(const std::string& OptName) const {
511 const OptionDescription& OptDesc = this->FindOption(OptName);
512 if (!OptDesc.isList())
513 throw OptName + ": incorrect option type - should be a list!";
514 return OptDesc;
515}
516
517const OptionDescription&
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000518OptionDescriptions::FindParameterList(const std::string& OptName) const {
519 const OptionDescription& OptDesc = this->FindOption(OptName);
520 if (!OptDesc.isList() || OptDesc.isSwitchList())
521 throw OptName + ": incorrect option type - should be a parameter list!";
522 return OptDesc;
523}
524
525const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000526OptionDescriptions::FindParameter(const std::string& OptName) const {
527 const OptionDescription& OptDesc = this->FindOption(OptName);
528 if (!OptDesc.isParameter())
529 throw OptName + ": incorrect option type - should be a parameter!";
530 return OptDesc;
531}
532
533const OptionDescription&
534OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
535 const OptionDescription& OptDesc = this->FindOption(OptName);
536 if (!OptDesc.isList() && !OptDesc.isParameter())
537 throw OptName
538 + ": incorrect option type - should be a list or parameter!";
539 return OptDesc;
540}
541
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000542const OptionDescription&
543OptionDescriptions::FindParameterListOrParameter
544(const std::string& OptName) const {
545 const OptionDescription& OptDesc = this->FindOption(OptName);
546 if ((!OptDesc.isList() && !OptDesc.isParameter()) || OptDesc.isSwitchList())
547 throw OptName
548 + ": incorrect option type - should be a parameter list or parameter!";
549 return OptDesc;
550}
551
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000552void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000553 container_type::iterator I = Descriptions.find(o.Name);
554 if (I != Descriptions.end()) {
555 OptionDescription& D = I->second;
556 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000557 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000558 else {
559 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000560 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000561}
562
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000563/// HandlerTable - A base class for function objects implemented as
564/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000565template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000566class HandlerTable {
567protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000568 // Implementation details.
569
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000570 /// HandlerMap - A map from property names to property handlers
571 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000572
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000573 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000574 static bool staticMembersInitialized_;
575
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000576public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000577
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000578 Handler GetHandler (const std::string& HandlerName) const {
579 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000580
581 if (method != Handlers_.end()) {
582 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000583 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000584 }
585 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000586 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000587 }
588 }
589
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000590 void AddHandler(const char* Property, Handler H) {
591 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000592 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000593
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000594};
595
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000596template <class Handler, class FunctionObject>
597Handler GetHandler(FunctionObject* Obj, const DagInit& Dag) {
598 const std::string& HandlerName = GetOperatorName(Dag);
599 return Obj->GetHandler(HandlerName);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000600}
601
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000602template <class FunctionObject>
603void InvokeDagInitHandler(FunctionObject* Obj, Init* I) {
604 typedef void (FunctionObject::*Handler) (const DagInit&);
605
606 const DagInit& Dag = InitPtrToDag(I);
607 Handler h = GetHandler<Handler>(Obj, Dag);
608
609 ((Obj)->*(h))(Dag);
610}
611
612template <class FunctionObject>
613void InvokeDagInitHandler(const FunctionObject* const Obj,
614 const Init* I, unsigned IndentLevel, raw_ostream& O)
615{
616 typedef void (FunctionObject::*Handler)
617 (const DagInit&, unsigned IndentLevel, raw_ostream& O) const;
618
619 const DagInit& Dag = InitPtrToDag(I);
620 Handler h = GetHandler<Handler>(Obj, Dag);
621
622 ((Obj)->*(h))(Dag, IndentLevel, O);
623}
624
625
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000626template <typename H>
627typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
628
629template <typename H>
630bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000631
632
633/// CollectOptionProperties - Function object for iterating over an
634/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000635class CollectOptionProperties;
636typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000637(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000638
639class CollectOptionProperties
640: public HandlerTable<CollectOptionPropertiesHandler>
641{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000642private:
643
644 /// optDescs_ - OptionDescriptions table. This is where the
645 /// information is stored.
646 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000647
648public:
649
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000650 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000651 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000652 {
653 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000654 AddHandler("help", &CollectOptionProperties::onHelp);
655 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000656 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000657 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
658 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000659 AddHandler("zero_or_more", &CollectOptionProperties::onZeroOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000660 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
661 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000662 AddHandler("optional", &CollectOptionProperties::onOptional);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000663 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000664 AddHandler("forward_not_split",
665 &CollectOptionProperties::onForwardNotSplit);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000666
667 staticMembersInitialized_ = true;
668 }
669 }
670
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000671 /// operator() - Just forwards to the corresponding property
672 /// handler.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000673 void operator() (Init* I) {
674 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000675 }
676
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000677private:
678
679 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000680 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000681
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000682 void onHelp (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000683 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000684 optDesc_.Help = EscapeQuotes(InitPtrToString(d.getArg(0)));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000685 }
686
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000687 void onHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000688 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000689 optDesc_.setHidden();
690 }
691
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000692 void onReallyHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000693 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000694 optDesc_.setReallyHidden();
695 }
696
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000697 void onCommaSeparated (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000698 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000699 if (!optDesc_.isParameterList())
700 throw "'comma_separated' is valid only on parameter list options!";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000701 optDesc_.setCommaSeparated();
702 }
703
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000704 void onForwardNotSplit (const DagInit& d) {
705 CheckNumberOfArguments(d, 0);
706 if (!optDesc_.isParameter())
707 throw "'forward_not_split' is valid only for parameter options!";
708 optDesc_.setForwardNotSplit();
709 }
710
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000711 void onRequired (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000712 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000713
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000714 optDesc_.setRequired();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000715 optDesc_.CheckConsistency();
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000716 }
717
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000718 void onInit (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000719 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000720 Init* i = d.getArg(0);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000721 const std::string& str = i->getAsString();
722
723 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
724 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
725
726 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000727 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000728
729 optDesc_.InitVal = i;
730 }
731
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000732 void onOneOrMore (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000733 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000734
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000735 optDesc_.setOneOrMore();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000736 optDesc_.CheckConsistency();
737 }
738
739 void onZeroOrMore (const DagInit& d) {
740 CheckNumberOfArguments(d, 0);
741
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000742 if (optDesc_.isList())
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000743 llvm::errs() << "Warning: specifying the 'zero_or_more' property "
744 "on a list option has no effect.\n";
745
746 optDesc_.setZeroOrMore();
747 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000748 }
749
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000750 void onOptional (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000751 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000752
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000753 if (!optDesc_.isList())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000754 llvm::errs() << "Warning: specifying the 'optional' property"
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000755 "on a non-list option has no effect.\n";
756
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000757 optDesc_.setOptional();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000758 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000759 }
760
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000761 void onMultiVal (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000762 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000763 int val = InitPtrToInt(d.getArg(0));
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000764 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000765 throw "Error in the 'multi_val' property: "
766 "the value must be greater than 1!";
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000767 if (!optDesc_.isParameterList())
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000768 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000769 optDesc_.MultiVal = val;
770 }
771
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000772};
773
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000774/// AddOption - A function object that is applied to every option
775/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000776class AddOption {
777private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000778 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000779
780public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000781 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000782 {}
783
784 void operator()(const Init* i) {
785 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000786 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000787
788 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000789 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000790 const std::string& Name = InitPtrToString(d.getArg(0));
791
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000792 OptionDescription OD(Type, Name);
793
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000794
795 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000796
797 if (OD.isAlias()) {
798 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000799 OD.Help = InitPtrToString(d.getArg(1));
800 }
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000801 else {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000802 processOptionProperties(d, OD);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000803 }
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000804
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000805 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000806 }
807
808private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000809 /// processOptionProperties - Go through the list of option
810 /// properties and call a corresponding handler for each.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000811 static void processOptionProperties (const DagInit& d, OptionDescription& o) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000812 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000813 DagInit::const_arg_iterator B = d.arg_begin();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000814 // Skip the first argument: it's always the option name.
815 ++B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000816 std::for_each(B, d.arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000817 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000818
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000819};
820
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000821/// CollectOptionDescriptions - Collects option properties from all
822/// OptionLists.
823void CollectOptionDescriptions (RecordVector::const_iterator B,
824 RecordVector::const_iterator E,
825 OptionDescriptions& OptDescs)
826{
827 // For every OptionList:
828 for (; B!=E; ++B) {
829 RecordVector::value_type T = *B;
830 // Throws an exception if the value does not exist.
831 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000832
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000833 // For every option description in this list:
834 // collect the information and
835 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
836 }
837}
838
839// Tool information record
840
841namespace ToolFlags {
842 enum ToolFlags { Join = 0x1, Sink = 0x2 };
843}
844
845struct ToolDescription : public RefCountedBase<ToolDescription> {
846 std::string Name;
847 Init* CmdLine;
848 Init* Actions;
849 StrVector InLanguage;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000850 std::string InFileOption;
851 std::string OutFileOption;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000852 std::string OutLanguage;
853 std::string OutputSuffix;
854 unsigned Flags;
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000855 const Init* OnEmpty;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000856
857 // Various boolean properties
858 void setSink() { Flags |= ToolFlags::Sink; }
859 bool isSink() const { return Flags & ToolFlags::Sink; }
860 void setJoin() { Flags |= ToolFlags::Join; }
861 bool isJoin() const { return Flags & ToolFlags::Join; }
862
863 // Default ctor here is needed because StringMap can only store
864 // DefaultConstructible objects
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000865 ToolDescription ()
Mikhail Glushenkovc4301292010-02-23 09:05:01 +0000866 : CmdLine(0), Actions(0), OutFileOption("-o"),
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000867 Flags(0), OnEmpty(0)
868 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000869 ToolDescription (const std::string& n)
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000870 : Name(n), CmdLine(0), Actions(0), OutFileOption("-o"),
871 Flags(0), OnEmpty(0)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000872 {}
873};
874
875/// ToolDescriptions - A list of Tool information records.
876typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
877
878
879/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000880/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000881
882class CollectToolProperties;
883typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000884(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000885
886class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
887{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000888private:
889
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000890 /// toolDesc_ - Properties of the current Tool. This is where the
891 /// information is stored.
892 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000893
894public:
895
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000896 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000897 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000898 {
899 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000900
901 AddHandler("actions", &CollectToolProperties::onActions);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000902 AddHandler("command", &CollectToolProperties::onCommand);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000903 AddHandler("in_language", &CollectToolProperties::onInLanguage);
904 AddHandler("join", &CollectToolProperties::onJoin);
905 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000906
907 AddHandler("out_file_option", &CollectToolProperties::onOutFileOption);
908 AddHandler("in_file_option", &CollectToolProperties::onInFileOption);
909
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000910 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
911 AddHandler("sink", &CollectToolProperties::onSink);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000912 AddHandler("works_on_empty", &CollectToolProperties::onWorksOnEmpty);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000913
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000914 staticMembersInitialized_ = true;
915 }
916 }
917
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000918 void operator() (Init* I) {
919 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000920 }
921
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000922private:
923
924 /// Property handlers --
925 /// Functions that extract information about tool properties from
926 /// DAG representation.
927
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000928 void onActions (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000929 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000930 Init* Case = d.getArg(0);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000931 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000932 GetOperatorName(static_cast<DagInit&>(*Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000933 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000934 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000935 }
936
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000937 void onCommand (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000938 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000939 toolDesc_.CmdLine = d.getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000940 }
941
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000942 void onInLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000943 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000944 Init* arg = d.getArg(0);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000945
946 // Find out the argument's type.
947 if (typeid(*arg) == typeid(StringInit)) {
948 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000949 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000950 }
951 else {
952 // It's a list.
953 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000954 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000955
956 // Copy strings to the output vector.
957 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
958 B != E; ++B) {
959 out.push_back(InitPtrToString(*B));
960 }
961
962 // Remove duplicates.
963 std::sort(out.begin(), out.end());
964 StrVector::iterator newE = std::unique(out.begin(), out.end());
965 out.erase(newE, out.end());
966 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000967 }
968
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000969 void onJoin (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000970 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000971 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000972 }
973
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000974 void onOutLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000975 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000976 toolDesc_.OutLanguage = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000977 }
978
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000979 void onOutFileOption (const DagInit& d) {
980 CheckNumberOfArguments(d, 1);
981 toolDesc_.OutFileOption = InitPtrToString(d.getArg(0));
982 }
983
984 void onInFileOption (const DagInit& d) {
985 CheckNumberOfArguments(d, 1);
986 toolDesc_.InFileOption = InitPtrToString(d.getArg(0));
987 }
988
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000989 void onOutputSuffix (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000990 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000991 toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000992 }
993
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000994 void onSink (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000995 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000996 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000997 }
998
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000999 void onWorksOnEmpty (const DagInit& d) {
1000 toolDesc_.OnEmpty = d.getArg(0);
1001 }
1002
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001003};
1004
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001005/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001006/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001007/// CollectToolProperties function object).
1008void CollectToolDescriptions (RecordVector::const_iterator B,
1009 RecordVector::const_iterator E,
1010 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001011{
1012 // Iterate over a properties list of every Tool definition
1013 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00001014 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001015 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001016 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001017
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001018 IntrusiveRefCntPtr<ToolDescription>
1019 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001020
1021 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001022 CollectToolProperties(*ToolDesc));
1023 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001024 }
1025}
1026
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001027/// FillInEdgeVector - Merge all compilation graph definitions into
1028/// one single edge list.
1029void FillInEdgeVector(RecordVector::const_iterator B,
1030 RecordVector::const_iterator E, RecordVector& Out) {
1031 for (; B != E; ++B) {
1032 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +00001033
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001034 for (unsigned i = 0; i < edges->size(); ++i)
1035 Out.push_back(edges->getElementAsRecord(i));
1036 }
1037}
1038
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001039/// NotInGraph - Helper function object for FilterNotInGraph.
1040struct NotInGraph {
1041private:
1042 const llvm::StringSet<>& ToolsInGraph_;
1043
1044public:
1045 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
1046 : ToolsInGraph_(ToolsInGraph)
1047 {}
1048
1049 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
1050 return (ToolsInGraph_.count(x->Name) == 0);
1051 }
1052};
1053
1054/// FilterNotInGraph - Filter out from ToolDescs all Tools not
1055/// mentioned in the compilation graph definition.
1056void FilterNotInGraph (const RecordVector& EdgeVector,
1057 ToolDescriptions& ToolDescs) {
1058
1059 // List all tools mentioned in the graph.
1060 llvm::StringSet<> ToolsInGraph;
1061
1062 for (RecordVector::const_iterator B = EdgeVector.begin(),
1063 E = EdgeVector.end(); B != E; ++B) {
1064
1065 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001066 const std::string& NodeA = Edge->getValueAsString("a");
1067 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001068
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001069 if (NodeA != "root")
1070 ToolsInGraph.insert(NodeA);
1071 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001072 }
1073
1074 // Filter ToolPropertiesList.
1075 ToolDescriptions::iterator new_end =
1076 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
1077 NotInGraph(ToolsInGraph));
1078 ToolDescs.erase(new_end, ToolDescs.end());
1079}
1080
1081/// FillInToolToLang - Fills in two tables that map tool names to
1082/// (input, output) languages. Helper function used by TypecheckGraph().
1083void FillInToolToLang (const ToolDescriptions& ToolDescs,
1084 StringMap<StringSet<> >& ToolToInLang,
1085 StringMap<std::string>& ToolToOutLang) {
1086 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1087 E = ToolDescs.end(); B != E; ++B) {
1088 const ToolDescription& D = *(*B);
1089 for (StrVector::const_iterator B = D.InLanguage.begin(),
1090 E = D.InLanguage.end(); B != E; ++B)
1091 ToolToInLang[D.Name].insert(*B);
1092 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001093 }
1094}
1095
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001096/// TypecheckGraph - Check that names for output and input languages
1097/// on all edges do match. This doesn't do much when the information
1098/// about the whole graph is not available (i.e. when compiling most
1099/// plugins).
1100void TypecheckGraph (const RecordVector& EdgeVector,
1101 const ToolDescriptions& ToolDescs) {
1102 StringMap<StringSet<> > ToolToInLang;
1103 StringMap<std::string> ToolToOutLang;
1104
1105 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
1106 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
1107 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
1108
1109 for (RecordVector::const_iterator B = EdgeVector.begin(),
1110 E = EdgeVector.end(); B != E; ++B) {
1111 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001112 const std::string& NodeA = Edge->getValueAsString("a");
1113 const std::string& NodeB = Edge->getValueAsString("b");
1114 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
1115 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001116
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001117 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001118 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001119 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001120 + ": output->input language mismatch";
1121 }
1122
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001123 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001124 throw "Edges back to the root are not allowed!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001125 }
1126}
1127
1128/// WalkCase - Walks the 'case' expression DAG and invokes
1129/// TestCallback on every test, and StatementCallback on every
1130/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001131/// combinators (that is, they are passed directly to TestCallback).
1132/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1133/// IndentLevel, bool FirstTest)'.
1134/// StatementCallback must have type 'void StatementCallback(const Init*,
1135/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001136template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001137void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1138 unsigned IndentLevel = 0)
1139{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001140 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001141
1142 // Error checks.
1143 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001144 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001145
1146 if (d.getNumArgs() < 2)
1147 throw "There should be at least one clause in the 'case' expression:\n"
1148 + d.getAsString();
1149
1150 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001151 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001152 const unsigned numArgs = d.getNumArgs();
1153 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001154 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1155 B != E; ++B) {
1156 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001157
1158 if (!even)
1159 {
1160 // Handle test.
1161 const DagInit& Test = InitPtrToDag(arg);
1162
1163 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001164 throw "The 'default' clause should be the last in the "
1165 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001166 if (i == numArgs)
1167 throw "Case construct handler: no corresponding action "
1168 "found for the test " + Test.getAsString() + '!';
1169
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001170 TestCallback(Test, IndentLevel, (i == 1));
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001171 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001172 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001173 {
1174 if (dynamic_cast<DagInit*>(arg)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001175 && GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001176 // Nested 'case'.
1177 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1178 }
1179
1180 // Handle statement.
1181 StatementCallback(arg, IndentLevel);
1182 }
1183
1184 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001185 even = !even;
1186 }
1187}
1188
1189/// ExtractOptionNames - A helper function object used by
1190/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1191class ExtractOptionNames {
1192 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001193
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001194 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001195 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001196 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001197 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001198 ActionName == "forward_value" ||
1199 ActionName == "forward_transformed_value" ||
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001200 ActionName == "switch_on" || ActionName == "any_switch_on" ||
1201 ActionName == "parameter_equals" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001202 ActionName == "element_in_list" || ActionName == "not_empty" ||
1203 ActionName == "empty") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001204 CheckNumberOfArguments(Stmt, 1);
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001205
1206 Init* Arg = Stmt.getArg(0);
1207 if (typeid(*Arg) == typeid(StringInit)) {
1208 const std::string& Name = InitPtrToString(Arg);
1209 OptionNames_.insert(Name);
1210 }
1211 else {
1212 // It's a list.
1213 const ListInit& List = InitPtrToList(Arg);
1214 for (ListInit::const_iterator B = List.begin(), E = List.end();
1215 B != E; ++B) {
1216 const std::string& Name = InitPtrToString(*B);
1217 OptionNames_.insert(Name);
1218 }
1219 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001220 }
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001221 else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001222 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001223 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001224 }
1225 }
1226 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001227
1228public:
1229 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1230 {}
1231
1232 void operator()(const Init* Statement) {
1233 if (typeid(*Statement) == typeid(ListInit)) {
1234 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1235 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1236 B != E; ++B)
1237 this->processDag(*B);
1238 }
1239 else {
1240 this->processDag(Statement);
1241 }
1242 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001243
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001244 void operator()(const DagInit& Test, unsigned, bool) {
1245 this->operator()(&Test);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001246 }
1247 void operator()(const Init* Statement, unsigned) {
1248 this->operator()(Statement);
1249 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001250};
1251
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001252/// CheckForSuperfluousOptions - Check that there are no side
1253/// effect-free options (specified only in the OptionList). Otherwise,
1254/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001255void CheckForSuperfluousOptions (const RecordVector& Edges,
1256 const ToolDescriptions& ToolDescs,
1257 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001258 llvm::StringSet<> nonSuperfluousOptions;
1259
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001260 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001261 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001262 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1263 E = ToolDescs.end(); B != E; ++B) {
1264 const ToolDescription& TD = *(*B);
1265 ExtractOptionNames Callback(nonSuperfluousOptions);
1266 if (TD.Actions)
1267 WalkCase(TD.Actions, Callback, Callback);
1268 }
1269
1270 // Add all options mentioned in the 'case' clauses of the
1271 // OptionalEdges of the compilation graph to the set of
1272 // non-superfluous options.
1273 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1274 B != E; ++B) {
1275 const Record* Edge = *B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001276 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001277
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001278 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001279 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001280 }
1281
1282 // Check that all options in OptDescs belong to the set of
1283 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001284 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001285 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001286 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001287 if (!nonSuperfluousOptions.count(Val.Name)
1288 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001289 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001290 "Probable cause: this option is specified only in the OptionList.\n";
1291 }
1292}
1293
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001294/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1295bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1296 if (TestName == "single_input_file") {
1297 O << "InputFilenames.size() == 1";
1298 return true;
1299 }
1300 else if (TestName == "multiple_input_files") {
1301 O << "InputFilenames.size() > 1";
1302 return true;
1303 }
1304
1305 return false;
1306}
1307
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001308/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1309template <typename F>
1310void EmitListTest(const ListInit& L, const char* LogicOp,
1311 F Callback, raw_ostream& O)
1312{
1313 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1314 // of Dags...
1315 bool isFirst = true;
1316 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1317 if (isFirst)
1318 isFirst = false;
1319 else
Mikhail Glushenkovb7935e02010-01-01 04:40:54 +00001320 O << ' ' << LogicOp << ' ';
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001321 Callback(InitPtrToString(*B), O);
1322 }
1323}
1324
1325// Callbacks for use with EmitListTest.
1326
1327class EmitSwitchOn {
1328 const OptionDescriptions& OptDescs_;
1329public:
1330 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1331 {}
1332
1333 void operator()(const std::string& OptName, raw_ostream& O) const {
1334 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1335 O << OptDesc.GenVariableName();
1336 }
1337};
1338
1339class EmitEmptyTest {
1340 bool EmitNegate_;
1341 const OptionDescriptions& OptDescs_;
1342public:
1343 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1344 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1345 {}
1346
1347 void operator()(const std::string& OptName, raw_ostream& O) const {
1348 const char* Neg = (EmitNegate_ ? "!" : "");
1349 if (OptName == "o") {
1350 O << Neg << "OutputFilename.empty()";
1351 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001352 else if (OptName == "save-temps") {
1353 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1354 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001355 else {
1356 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1357 O << Neg << OptDesc.GenVariableName() << ".empty()";
1358 }
1359 }
1360};
1361
1362
1363/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1364bool EmitCaseTest1ArgList(const std::string& TestName,
1365 const DagInit& d,
1366 const OptionDescriptions& OptDescs,
1367 raw_ostream& O) {
Mikhail Glushenkov3a481e32010-01-01 03:50:51 +00001368 const ListInit& L = InitPtrToList(d.getArg(0));
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001369
1370 if (TestName == "any_switch_on") {
1371 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1372 return true;
1373 }
1374 else if (TestName == "switch_on") {
1375 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1376 return true;
1377 }
1378 else if (TestName == "any_not_empty") {
1379 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1380 return true;
1381 }
1382 else if (TestName == "any_empty") {
1383 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1384 return true;
1385 }
1386 else if (TestName == "not_empty") {
1387 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1388 return true;
1389 }
1390 else if (TestName == "empty") {
1391 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1392 return true;
1393 }
1394
1395 return false;
1396}
1397
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001398/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1399bool EmitCaseTest1ArgStr(const std::string& TestName,
1400 const DagInit& d,
1401 const OptionDescriptions& OptDescs,
1402 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001403 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001404
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001405 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001406 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001407 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001408 }
1409 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001410 O << "InLangs.count(\"" << OptName << "\") != 0";
1411 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001412 }
1413 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001414 // This works only for single-argument Tool::GenerateAction. Join
1415 // tools can process several files in different languages simultaneously.
1416
1417 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001418 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001419 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001420 }
1421 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001422 bool EmitNegate = (TestName == "not_empty");
1423 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001424 return true;
1425 }
1426
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001427 return false;
1428}
1429
1430/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1431bool EmitCaseTest1Arg(const std::string& TestName,
1432 const DagInit& d,
1433 const OptionDescriptions& OptDescs,
1434 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001435 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001436 if (typeid(*d.getArg(0)) == typeid(ListInit))
1437 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1438 else
1439 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1440}
1441
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001442/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001443bool EmitCaseTest2Args(const std::string& TestName,
1444 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001445 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001446 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001447 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001448 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001449 const std::string& OptName = InitPtrToString(d.getArg(0));
1450 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001451
1452 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001453 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001454 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1455 return true;
1456 }
1457 else if (TestName == "element_in_list") {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00001458 const OptionDescription& OptDesc = OptDescs.FindParameterList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001459 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001460 O << "std::find(" << VarName << ".begin(),\n";
1461 O.indent(IndentLevel + Indent1)
1462 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001463 << OptArg << "\") != " << VarName << ".end()";
1464 return true;
1465 }
1466
1467 return false;
1468}
1469
1470// Forward declaration.
1471// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001472void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001473 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001474 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001475
1476/// EmitLogicalOperationTest - Helper function used by
1477/// EmitCaseConstructHandler.
1478void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001479 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001480 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001481 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001482 O << '(';
1483 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001484 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001485 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001486 if (j != NumArgs - 1) {
1487 O << ")\n";
1488 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1489 }
1490 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001491 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001492 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001493 }
1494}
1495
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001496void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001497 const OptionDescriptions& OptDescs, raw_ostream& O)
1498{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001499 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001500 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1501 O << "! (";
1502 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1503 O << ")";
1504}
1505
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001506/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001507void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001508 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001509 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001510 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001511
1512 if (TestName == "and")
1513 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1514 else if (TestName == "or")
1515 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001516 else if (TestName == "not")
1517 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001518 else if (EmitCaseTest0Args(TestName, O))
1519 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001520 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1521 return;
1522 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1523 return;
1524 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001525 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001526}
1527
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001528
1529/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1530class EmitCaseTestCallback {
1531 bool EmitElseIf_;
1532 const OptionDescriptions& OptDescs_;
1533 raw_ostream& O_;
1534public:
1535
1536 EmitCaseTestCallback(bool EmitElseIf,
1537 const OptionDescriptions& OptDescs, raw_ostream& O)
1538 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1539 {}
1540
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001541 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001542 {
1543 if (GetOperatorName(Test) == "default") {
1544 O_.indent(IndentLevel) << "else {\n";
1545 }
1546 else {
1547 O_.indent(IndentLevel)
1548 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001549 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001550 O_ << ") {\n";
1551 }
1552 }
1553};
1554
1555/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1556template <typename F>
1557class EmitCaseStatementCallback {
1558 F Callback_;
1559 raw_ostream& O_;
1560public:
1561
1562 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1563 : Callback_(Callback), O_(O)
1564 {}
1565
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001566 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001567
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001568 // Ignore nested 'case' DAG.
1569 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001570 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case")) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001571 if (typeid(*Statement) == typeid(ListInit)) {
1572 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1573 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1574 B != E; ++B)
1575 Callback_(*B, (IndentLevel + Indent1), O_);
1576 }
1577 else {
1578 Callback_(Statement, (IndentLevel + Indent1), O_);
1579 }
1580 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001581 O_.indent(IndentLevel) << "}\n";
1582 }
1583
1584};
1585
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001586/// EmitCaseConstructHandler - Emit code that handles the 'case'
1587/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001588/// clause. Implemented on top of WalkCase.
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00001589/// Callback's type is void F(const Init* Statement, unsigned IndentLevel,
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001590/// raw_ostream& O).
1591/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001592/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1593/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001594template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001595void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001596 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001597 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001598 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001599 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1600 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001601}
1602
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001603/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001604/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1605/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001606void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001607 const char* Delimiters = " \t\n\v\f\r";
1608 enum TokenizerState
1609 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1610 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001611
1612 if (CmdLine.empty())
1613 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001614 Out.push_back("");
1615
1616 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1617 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001618
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001619 for (; B != E; ++B) {
1620 char cur_ch = CmdLine[B];
1621
1622 switch (cur_st) {
1623 case Normal:
1624 if (cur_ch == '$') {
1625 cur_st = SpecialCommand;
1626 break;
1627 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001628 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001629 // Skip whitespace
1630 B = CmdLine.find_first_not_of(Delimiters, B);
1631 if (B == std::string::npos) {
1632 B = E-1;
1633 continue;
1634 }
1635 --B;
1636 Out.push_back("");
1637 continue;
1638 }
1639 break;
1640
1641
1642 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001643 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001644 cur_st = Normal;
1645 Out.push_back("");
1646 continue;
1647 }
1648 if (cur_ch == '(') {
1649 Out.push_back("");
1650 cur_st = InsideSpecialCommand;
1651 continue;
1652 }
1653 break;
1654
1655 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001656 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001657 continue;
1658 }
1659 if (cur_ch == '\'') {
1660 cur_st = InsideQuotationMarks;
1661 Out.push_back("");
1662 continue;
1663 }
1664 if (cur_ch == ')') {
1665 cur_st = Normal;
1666 Out.push_back("");
1667 }
1668 if (cur_ch == ',') {
1669 continue;
1670 }
1671
1672 break;
1673
1674 case InsideQuotationMarks:
1675 if (cur_ch == '\'') {
1676 cur_st = InsideSpecialCommand;
1677 continue;
1678 }
1679 break;
1680 }
1681
1682 Out.back().push_back(cur_ch);
1683 }
1684}
1685
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001686/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1687/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1688/// SubstituteSpecialCommands().
1689StrVector::const_iterator
1690SubstituteCall (StrVector::const_iterator Pos,
1691 StrVector::const_iterator End,
1692 bool IsJoin, raw_ostream& O)
1693{
1694 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001695 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001696 const std::string& CmdName = *Pos;
1697
1698 if (CmdName == ")")
1699 throw "$CALL invocation: empty argument list!";
1700
1701 O << "hooks::";
1702 O << CmdName << "(";
1703
1704
1705 bool firstIteration = true;
1706 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001707 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001708 const std::string& Arg = *Pos;
1709 assert(Arg.size() != 0);
1710
1711 if (Arg[0] == ')')
1712 break;
1713
1714 if (firstIteration)
1715 firstIteration = false;
1716 else
1717 O << ", ";
1718
1719 if (Arg == "$INFILE") {
1720 if (IsJoin)
1721 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1722 else
1723 O << "inFile.c_str()";
1724 }
1725 else {
1726 O << '"' << Arg << '"';
1727 }
1728 }
1729
1730 O << ')';
1731
1732 return Pos;
1733}
1734
1735/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1736/// function used by SubstituteSpecialCommands().
1737StrVector::const_iterator
1738SubstituteEnv (StrVector::const_iterator Pos,
1739 StrVector::const_iterator End, raw_ostream& O)
1740{
1741 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001742 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001743 const std::string& EnvName = *Pos;
1744
1745 if (EnvName == ")")
1746 throw "$ENV invocation: empty argument list!";
1747
1748 O << "checkCString(std::getenv(\"";
1749 O << EnvName;
1750 O << "\"))";
1751
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001752 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001753
1754 return Pos;
1755}
1756
1757/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1758/// handler code. Helper function used by EmitCmdLineVecFill().
1759StrVector::const_iterator
1760SubstituteSpecialCommands (StrVector::const_iterator Pos,
1761 StrVector::const_iterator End,
1762 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001763{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001764
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001765 const std::string& cmd = *Pos;
1766
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001767 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001768 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001769 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001770 }
1771 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001772 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001773 }
1774 else {
1775 throw "Unknown special command: " + cmd;
1776 }
1777
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001778 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001779 const std::string& Leftover = *Pos;
1780 assert(Leftover.at(0) == ')');
1781 if (Leftover.size() != 1)
1782 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001783
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001784 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001785}
1786
1787/// EmitCmdLineVecFill - Emit code that fills in the command line
1788/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001789void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001790 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001791 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001792 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001793 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001794
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001795 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001796 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001797
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001798 StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001799
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001800 // Emit the command itself.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001801 assert(!StrVec[0].empty());
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001802 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001803 if (StrVec[0][0] == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001804 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1805 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001806 }
1807 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001808 O << '"' << StrVec[0] << '"';
1809 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001810 }
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001811 O << ";\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001812
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001813 // Go through the command arguments.
1814 assert(B <= E);
1815 for (; B != E; ++B) {
1816 const std::string& cmd = *B;
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001817
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001818 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001819 O.indent(IndentLevel);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001820
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001821 if (cmd.at(0) == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001822 O << "vec.push_back(std::make_pair(0, ";
1823 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1824 O << "));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001825 }
1826 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001827 O << "vec.push_back(std::make_pair(0, \"" << cmd << "\"));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001828 }
1829 }
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001830
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001831}
1832
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001833/// EmitForEachListElementCycleHeader - Emit common code for iterating through
1834/// all elements of a list. Helper function used by
1835/// EmitForwardOptionPropertyHandlingCode.
1836void EmitForEachListElementCycleHeader (const OptionDescription& D,
1837 unsigned IndentLevel,
1838 raw_ostream& O) {
1839 unsigned IndentLevel1 = IndentLevel + Indent1;
1840
1841 O.indent(IndentLevel)
1842 << "for (" << D.GenTypeDeclaration()
1843 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1844 O.indent(IndentLevel)
1845 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1846 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1847 << ".getPosition(B - " << D.GenVariableName()
1848 << ".begin());\n";
1849}
1850
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001851/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1852/// implement EmitActionHandler. Emits code for
1853/// handling the (forward) and (forward_as) option properties.
1854void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001855 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001856 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001857 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001858 const std::string& Name = NewName.empty()
1859 ? ("-" + D.Name)
1860 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001861 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001862
1863 switch (D.Type) {
1864 case OptionType::Switch:
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001865 O.indent(IndentLevel)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001866 << "vec.push_back(std::make_pair(" << D.GenVariableName()
1867 << ".getPosition(), \"" << Name << "\"));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001868 break;
1869 case OptionType::Parameter:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001870 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1871 << D.GenVariableName()
1872 <<".getPosition(), \"" << Name;
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001873
1874 if (!D.isForwardNotSplit()) {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001875 O << "\"));\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001876 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1877 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001878 << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001879 }
1880 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001881 O << "=\" + " << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001882 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001883 break;
1884 case OptionType::Prefix:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001885 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1886 << D.GenVariableName() << ".getPosition(), \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001887 << Name << "\" + "
1888 << D.GenVariableName() << "));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001889 break;
1890 case OptionType::PrefixList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001891 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001892 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001893 << Name << "\" + " << "*B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001894 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001895
1896 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001897 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001898 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001899 }
1900
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001901 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001902 break;
1903 case OptionType::ParameterList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001904 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001905 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001906 << Name << "\"));\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001907
1908 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001909 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001910 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001911 }
1912
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001913 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001914 break;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001915 case OptionType::SwitchList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001916 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001917 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
1918 << Name << "\"));\n";
1919 O.indent(IndentLevel1) << "++B;\n";
1920 O.indent(IndentLevel) << "}\n";
1921 break;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001922 case OptionType::Alias:
1923 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001924 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001925 }
1926}
1927
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001928/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1929/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001930struct ActionHandlingCallbackBase
1931{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001932
1933 void onErrorDag(const DagInit& d,
1934 unsigned IndentLevel, raw_ostream& O) const
1935 {
1936 O.indent(IndentLevel)
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00001937 << "PrintError(\""
1938 << (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0)) : "Unknown error!")
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001939 << "\");\n";
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +00001940 O.indent(IndentLevel) << "return 1;\n";
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001941 }
1942
1943 void onWarningDag(const DagInit& d,
1944 unsigned IndentLevel, raw_ostream& O) const
1945 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001946 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001947 O.indent(IndentLevel) << "llvm::errs() << \""
1948 << InitPtrToString(d.getArg(0)) << "\";\n";
1949 }
1950
1951};
1952
1953/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1954/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001955
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001956class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001957
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001958typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1959(const DagInit&, unsigned, raw_ostream&) const;
1960
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001961class EmitActionHandlersCallback :
1962 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001963 public HandlerTable<EmitActionHandlersCallbackHandler>
1964{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001965 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001966
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001967 const OptionDescriptions& OptDescs;
1968
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001969 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1970 /// onAppendCmd and onOutputSuffix.
1971 void EmitHookInvocation(const std::string& Str,
1972 const char* BlockOpen, const char* BlockClose,
1973 unsigned IndentLevel, raw_ostream& O) const
1974 {
1975 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001976 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001977
1978 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1979 B != E; ++B) {
1980 const std::string& cmd = *B;
1981
1982 O.indent(IndentLevel) << BlockOpen;
1983
1984 if (cmd.at(0) == '$')
1985 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1986 else
1987 O << '"' << cmd << '"';
1988
1989 O << BlockClose;
1990 }
1991 }
1992
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001993 void onAppendCmd (const DagInit& Dag,
1994 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001995 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001996 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001997 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001998 "vec.push_back(std::make_pair(65536, ", "));\n",
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001999 IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002000 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00002001
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002002 void onForward (const DagInit& Dag,
2003 unsigned IndentLevel, raw_ostream& O) const
2004 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002005 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002006 const std::string& Name = InitPtrToString(Dag.getArg(0));
2007 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2008 IndentLevel, "", O);
2009 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002010
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002011 void onForwardAs (const DagInit& Dag,
2012 unsigned IndentLevel, raw_ostream& O) const
2013 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002014 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002015 const std::string& Name = InitPtrToString(Dag.getArg(0));
2016 const std::string& NewName = InitPtrToString(Dag.getArg(1));
2017 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2018 IndentLevel, NewName, O);
2019 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002020
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002021 void onForwardValue (const DagInit& Dag,
2022 unsigned IndentLevel, raw_ostream& O) const
2023 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002024 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002025 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002026 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002027
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002028 if (D.isSwitchList()) {
2029 throw std::runtime_error
2030 ("forward_value is not allowed with switch_list");
2031 }
2032
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002033 if (D.isParameter()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002034 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2035 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002036 << D.GenVariableName() << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002037 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00002038 else {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002039 O.indent(IndentLevel) << "for (" << D.GenTypeDeclaration()
2040 << "::iterator B = " << D.GenVariableName()
2041 << ".begin(), \n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002042 O.indent(IndentLevel + Indent1) << " E = " << D.GenVariableName()
2043 << ".end(); B != E; ++B)\n";
2044 O.indent(IndentLevel) << "{\n";
2045 O.indent(IndentLevel + Indent1)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002046 << "unsigned pos = " << D.GenVariableName()
2047 << ".getPosition(B - " << D.GenVariableName()
2048 << ".begin());\n";
2049 O.indent(IndentLevel + Indent1)
2050 << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002051 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002052 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002053 }
2054
2055 void onForwardTransformedValue (const DagInit& Dag,
2056 unsigned IndentLevel, raw_ostream& O) const
2057 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002058 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002059 const std::string& Name = InitPtrToString(Dag.getArg(0));
2060 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002061 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002062
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002063 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2064 << D.GenVariableName() << ".getPosition("
2065 << (D.isList() ? "0" : "") << "), "
2066 << "hooks::" << Hook << "(" << D.GenVariableName()
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002067 << (D.isParameter() ? ".c_str()" : "") << ")));\n";
2068 }
2069
2070 void onNoOutFile (const DagInit& Dag,
2071 unsigned IndentLevel, raw_ostream& O) const
2072 {
2073 CheckNumberOfArguments(Dag, 0);
2074 O.indent(IndentLevel) << "no_out_file = true;\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002075 }
2076
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002077 void onOutputSuffix (const DagInit& Dag,
2078 unsigned IndentLevel, raw_ostream& O) const
2079 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002080 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002081 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
2082 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002083 }
2084
2085 void onStopCompilation (const DagInit& Dag,
2086 unsigned IndentLevel, raw_ostream& O) const
2087 {
2088 O.indent(IndentLevel) << "stop_compilation = true;\n";
2089 }
2090
2091
2092 void onUnpackValues (const DagInit& Dag,
2093 unsigned IndentLevel, raw_ostream& O) const
2094 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002095 throw "'unpack_values' is deprecated. "
2096 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002097 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002098
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002099 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002100
2101 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
2102 : OptDescs(OD)
2103 {
2104 if (!staticMembersInitialized_) {
2105 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
2106 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
2107 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
2108 AddHandler("forward", &EmitActionHandlersCallback::onForward);
2109 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002110 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
2111 AddHandler("forward_transformed_value",
2112 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002113 AddHandler("no_out_file",
2114 &EmitActionHandlersCallback::onNoOutFile);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002115 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
2116 AddHandler("stop_compilation",
2117 &EmitActionHandlersCallback::onStopCompilation);
2118 AddHandler("unpack_values",
2119 &EmitActionHandlersCallback::onUnpackValues);
2120
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002121
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002122 staticMembersInitialized_ = true;
2123 }
2124 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002125
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002126 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002127 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002128 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002129 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002130 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002131};
2132
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002133void EmitGenerateActionMethodHeader(const ToolDescription& D,
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002134 bool IsJoin, bool Naked,
2135 raw_ostream& O)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002136{
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002137 O.indent(Indent1) << "int GenerateAction(Action& Out,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002138
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002139 if (IsJoin)
2140 O.indent(Indent2) << "const PathVector& inFiles,\n";
2141 else
2142 O.indent(Indent2) << "const sys::Path& inFile,\n";
2143
2144 O.indent(Indent2) << "const bool HasChildren,\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002145 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2146 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2147 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2148 O.indent(Indent1) << "{\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002149
2150 if (!Naked) {
2151 O.indent(Indent2) << "std::string cmd;\n";
2152 O.indent(Indent2) << "std::string out_file;\n";
2153 O.indent(Indent2)
2154 << "std::vector<std::pair<unsigned, std::string> > vec;\n";
2155 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2156 O.indent(Indent2) << "bool no_out_file = false;\n";
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002157 O.indent(Indent2) << "std::string output_suffix(\""
2158 << D.OutputSuffix << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002159 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002160}
2161
2162// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2163// Tool::GenerateAction() method.
2164void EmitGenerateActionMethod (const ToolDescription& D,
2165 const OptionDescriptions& OptDescs,
2166 bool IsJoin, raw_ostream& O) {
2167
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002168 EmitGenerateActionMethodHeader(D, IsJoin, /* Naked = */ false, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002169
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002170 if (!D.CmdLine)
2171 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002172
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002173 // Process the 'command' property.
2174 O << '\n';
2175 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2176 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002177
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002178 // Process the 'actions' list of this tool.
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002179 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002180 EmitCaseConstructHandler(D.Actions, Indent2,
2181 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002182 false, OptDescs, O);
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002183 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002184
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002185 // Input file (s)
2186 if (!D.InFileOption.empty()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002187 O.indent(Indent2)
2188 << "vec.push_back(std::make_pair(InputFilenames.getPosition(0), \""
2189 << D.InFileOption << "\");\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002190 }
2191
2192 if (IsJoin) {
2193 O.indent(Indent2)
2194 << "for (PathVector::const_iterator B = inFiles.begin(),\n";
2195 O.indent(Indent3) << "E = inFiles.end(); B != E; ++B)\n";
2196 O.indent(Indent2) << "{\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002197 O.indent(Indent3) << "vec.push_back(std::make_pair("
2198 << "InputFilenames.getPosition(B - inFiles.begin()), "
2199 << "B->str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002200 O.indent(Indent2) << "}\n";
2201 }
2202 else {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002203 O.indent(Indent2) << "vec.push_back(std::make_pair("
2204 << "InputFilenames.getPosition(0), inFile.str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002205 }
2206
2207 // Output file
2208 O.indent(Indent2) << "if (!no_out_file) {\n";
2209 if (!D.OutFileOption.empty())
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002210 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002211 << D.OutFileOption << "\"));\n";
2212
2213 O.indent(Indent3) << "out_file = this->OutFilename("
2214 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002215 O.indent(Indent4) <<
2216 "TempDir, stop_compilation, output_suffix.c_str()).str();\n\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002217 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, out_file));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002218
2219 O.indent(Indent2) << "}\n\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002220
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002221 // Handle the Sink property.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002222 std::string SinkOption("autogenerated::");
2223 SinkOption += SinkOptionName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002224 if (D.isSink()) {
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002225 O.indent(Indent2) << "if (!" << SinkOption << ".empty()) {\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002226 O.indent(Indent3) << "for (cl::list<std::string>::iterator B = "
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002227 << SinkOption << ".begin(), E = " << SinkOption
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002228 << ".end(); B != E; ++B)\n";
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002229 O.indent(Indent4) << "vec.push_back(std::make_pair(" << SinkOption
2230 << ".getPosition(B - " << SinkOption
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002231 << ".begin()), *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002232 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002233 }
2234
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002235 O.indent(Indent2) << "Out.Construct(cmd, this->SortArgs(vec), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002236 << "stop_compilation, out_file);\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002237 O.indent(Indent2) << "return 0;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002238 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002239}
2240
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002241/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2242/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002243void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2244 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002245 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002246 if (!ToolDesc.isJoin()) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002247 EmitGenerateActionMethodHeader(ToolDesc, /* IsJoin = */ true,
2248 /* Naked = */ true, O);
2249 O.indent(Indent2) << "PrintError(\"" << ToolDesc.Name
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002250 << " is not a Join tool!\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002251 O.indent(Indent2) << "return -1;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002252 O.indent(Indent1) << "}\n\n";
2253 }
2254 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002255 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002256 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002257
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002258 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002259}
2260
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002261/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2262/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002263void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002264 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2265 O.indent(Indent2) << "return InputLanguages_;\n";
2266 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002267
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002268 if (D.OutLanguage.empty())
2269 throw "Tool " + D.Name + " has no 'out_language' property!";
2270
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002271 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2272 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2273 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002274}
2275
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002276/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002277void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002278 O.indent(Indent1) << "const char* Name() const {\n";
2279 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2280 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002281}
2282
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002283/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2284/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002285void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002286 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002287 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002288 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002289 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002290 O.indent(Indent2) << "return false;\n";
2291 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002292}
2293
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002294/// EmitWorksOnEmptyCallback - Callback used by EmitWorksOnEmptyMethod in
2295/// conjunction with EmitCaseConstructHandler.
2296void EmitWorksOnEmptyCallback (const Init* Value,
2297 unsigned IndentLevel, raw_ostream& O) {
2298 CheckBooleanConstant(Value);
2299 O.indent(IndentLevel) << "return " << Value->getAsString() << ";\n";
2300}
2301
2302/// EmitWorksOnEmptyMethod - Emit the WorksOnEmpty() method for a given Tool
2303/// class.
2304void EmitWorksOnEmptyMethod (const ToolDescription& D,
2305 const OptionDescriptions& OptDescs,
2306 raw_ostream& O)
2307{
2308 O.indent(Indent1) << "bool WorksOnEmpty() const {\n";
2309 if (D.OnEmpty == 0)
2310 O.indent(Indent2) << "return false;\n";
2311 else
2312 EmitCaseConstructHandler(D.OnEmpty, Indent2, EmitWorksOnEmptyCallback,
2313 /*EmitElseIf = */ true, OptDescs, O);
2314 O.indent(Indent1) << "}\n\n";
2315}
2316
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002317/// EmitStaticMemberDefinitions - Emit static member definitions for a
2318/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002319void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002320 if (D.InLanguage.empty())
2321 throw "Tool " + D.Name + " has no 'in_language' property!";
2322
2323 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2324 for (StrVector::const_iterator B = D.InLanguage.begin(),
2325 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002326 O << '\"' << *B << "\", ";
2327 O << "0};\n\n";
2328}
2329
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002330/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002331void EmitToolClassDefinition (const ToolDescription& D,
2332 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002333 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002334 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002335 return;
2336
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002337 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002338 O << "class " << D.Name << " : public ";
2339 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002340 O << "JoinTool";
2341 else
2342 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002343
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002344 O << " {\nprivate:\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002345 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002346
2347 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002348 EmitNameMethod(D, O);
2349 EmitInOutLanguageMethods(D, O);
2350 EmitIsJoinMethod(D, O);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002351 EmitWorksOnEmptyMethod(D, OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002352 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002353
2354 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002355 O << "};\n";
2356
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002357 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002358
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002359}
2360
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002361/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002362/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002363void EmitOptionDefinitions (const OptionDescriptions& descs,
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002364 bool HasSink, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002365{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002366 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002367
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002368 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002369 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002370 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002371 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002372
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002373 if (val.Type == OptionType::Alias) {
2374 Aliases.push_back(val);
2375 continue;
2376 }
2377
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002378 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002379 << val.GenPlainVariableName();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002380
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002381 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002382
2383 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2384 O << ", cl::Prefix";
2385
2386 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002387 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002388 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002389 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002390 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002391 }
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002392
2393 if (val.isOptional())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002394 O << ", cl::Optional";
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002395
2396 if (val.isOneOrMore())
2397 O << ", cl::OneOrMore";
2398
2399 if (val.isZeroOrMore())
2400 O << ", cl::ZeroOrMore";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002401
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002402 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002403 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002404 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002405 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002406
2407 if (val.isCommaSeparated())
2408 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002409
2410 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002411 O << ", cl::multi_val(" << val.MultiVal << ')';
2412
2413 if (val.InitVal) {
2414 const std::string& str = val.InitVal->getAsString();
2415 O << ", cl::init(" << str << ')';
2416 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002417
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002418 if (!val.Help.empty())
2419 O << ", cl::desc(\"" << val.Help << "\")";
2420
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002421 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002422 }
2423
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002424 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002425 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002426 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002427 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002428
2429 O << val.GenTypeDeclaration() << ' '
2430 << val.GenVariableName()
2431 << "(\"" << val.Name << '\"';
2432
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002433 const OptionDescription& D = descs.FindOption(val.Help);
2434 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002435
2436 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2437 }
2438
2439 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002440 if (HasSink)
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002441 O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002442
2443 O << '\n';
2444}
2445
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002446/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002447/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002448
2449class EmitPreprocessOptionsCallback;
2450
2451typedef void
2452(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2453(const DagInit&, unsigned, raw_ostream&) const;
2454
2455class EmitPreprocessOptionsCallback :
2456 public ActionHandlingCallbackBase,
2457 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2458{
2459 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002460 typedef void
2461 (EmitPreprocessOptionsCallback::* HandlerImpl)
2462 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002463
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002464 const OptionDescriptions& OptDescs_;
2465
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002466 void onListOrDag(const DagInit& d, HandlerImpl h,
2467 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002468 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002469 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002470 const Init* I = d.getArg(0);
2471
2472 // If I is a list, apply h to each element.
2473 if (typeid(*I) == typeid(ListInit)) {
2474 const ListInit& L = *static_cast<const ListInit*>(I);
2475 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B)
2476 ((this)->*(h))(*B, IndentLevel, O);
2477 }
2478 // Otherwise, apply h to I.
2479 else {
2480 ((this)->*(h))(I, IndentLevel, O);
2481 }
2482 }
2483
2484 void onUnsetOptionImpl(const Init* I,
2485 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002486 {
2487 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002488 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002489
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002490 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002491 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2492 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002493 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002494 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2495 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002496 else if (OptDesc.isList()) {
2497 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2498 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002499 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002500 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002501 }
2502 }
2503
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002504 void onUnsetOption(const DagInit& d,
2505 unsigned IndentLevel, raw_ostream& O) const
2506 {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002507 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2508 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002509 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002510
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002511 void onSetOptionImpl(const DagInit& d,
2512 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002513 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002514 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002515 const Init* Value = d.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002516 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2517
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002518 if (OptDesc.isList()) {
2519 const ListInit& List = InitPtrToList(Value);
2520
2521 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2522 for (ListInit::const_iterator B = List.begin(), E = List.end();
2523 B != E; ++B) {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002524 const Init* CurElem = *B;
2525 if (OptDesc.isSwitchList())
2526 CheckBooleanConstant(CurElem);
2527
2528 O.indent(IndentLevel)
2529 << OptDesc.GenVariableName() << ".push_back(\""
2530 << (OptDesc.isSwitchList() ? CurElem->getAsString()
2531 : InitPtrToString(CurElem))
2532 << "\");\n";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002533 }
2534 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002535 else if (OptDesc.isSwitch()) {
2536 CheckBooleanConstant(Value);
2537 O.indent(IndentLevel) << OptDesc.GenVariableName()
2538 << " = " << Value->getAsString() << ";\n";
2539 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002540 else if (OptDesc.isParameter()) {
2541 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002542 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002543 << " = \"" << Str << "\";\n";
2544 }
2545 else {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002546 throw "Can't apply 'set_option' to alias option -" + OptName + " !";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002547 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002548 }
2549
2550 void onSetSwitch(const Init* I,
2551 unsigned IndentLevel, raw_ostream& O) const {
2552 const std::string& OptName = InitPtrToString(I);
2553 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2554
2555 if (OptDesc.isSwitch())
2556 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2557 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002558 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002559 }
2560
2561 void onSetOption(const DagInit& d,
2562 unsigned IndentLevel, raw_ostream& O) const
2563 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002564 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002565
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002566 // Two arguments: (set_option "parameter", VALUE), where VALUE can be a
2567 // boolean, a string or a string list.
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002568 if (d.getNumArgs() > 1)
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002569 this->onSetOptionImpl(d, IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002570 // One argument: (set_option "switch")
2571 // or (set_option ["switch1", "switch2", ...])
2572 else
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002573 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2574 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002575 }
2576
2577public:
2578
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002579 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002580 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002581 {
2582 if (!staticMembersInitialized_) {
2583 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2584 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2585 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002586 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002587
2588 staticMembersInitialized_ = true;
2589 }
2590 }
2591
2592 void operator()(const Init* I,
2593 unsigned IndentLevel, raw_ostream& O) const
2594 {
2595 InvokeDagInitHandler(this, I, IndentLevel, O);
2596 }
2597
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002598};
2599
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002600/// EmitPreprocessOptions - Emit the PreprocessOptions() function.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002601void EmitPreprocessOptions (const RecordKeeper& Records,
2602 const OptionDescriptions& OptDecs, raw_ostream& O)
2603{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002604 O << "int PreprocessOptions () {\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002605
2606 const RecordVector& OptionPreprocessors =
2607 Records.getAllDerivedDefinitions("OptionPreprocessor");
2608
2609 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2610 E = OptionPreprocessors.end(); B!=E; ++B) {
2611 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002612 EmitCaseConstructHandler(Case, Indent1,
2613 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002614 false, OptDecs, O);
2615 }
2616
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002617 O << '\n';
2618 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002619 O << "}\n\n";
2620}
2621
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002622/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002623void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002624{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002625 O << "int PopulateLanguageMap (LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002626
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002627 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002628 // TODO: change this to getAllDerivedDefinitions.
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002629 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002630
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002631 // It is allowed for a plugin to have no language map.
2632 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002633
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002634 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2635 if (!LangsToSuffixesList)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00002636 throw "Error in the language map definition!";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002637
2638 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002639 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002640
2641 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2642 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2643
2644 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002645 O.indent(Indent1) << "langMap[\""
2646 << InitPtrToString(Suffixes->getElement(i))
2647 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002648 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002649 }
2650
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002651 O << '\n';
2652 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002653 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002654}
2655
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002656/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2657/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002658void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002659 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002660 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002661 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002662
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002663 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002664 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002665 }
2666 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002667 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002668 }
2669 else if (OpName == "error") {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002670 // TODO: fix this
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002671 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002672 O.indent(IndentLevel) << "PrintError(\""
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002673 << InitPtrToString(d.getArg(0))
2674 << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002675 O.indent(IndentLevel) << "return -1;\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002676 return;
2677 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002678 else {
2679 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002680 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002681 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002682
2683 if (d.getNumArgs() > 0)
2684 O << InitPtrToInt(d.getArg(0)) << ";\n";
2685 else
2686 O << "2;\n";
2687
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002688}
2689
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002690/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002691void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002692 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002693 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002694
2695 // Class constructor.
2696 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002697 << "public:\n";
2698 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2699 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002700
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002701 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002702 O.indent(Indent1)
2703 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2704 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002705
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002706 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002707 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002708
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002709 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002710 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002711}
2712
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002713/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002714void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002715 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002716 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002717 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002718 for (RecordVector::const_iterator B = EdgeVector.begin(),
2719 E = EdgeVector.end(); B != E; ++B) {
2720 const Record* Edge = *B;
2721 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002722 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002723
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002724 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002725 EmitEdgeClass(i, NodeB, &Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002726 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002727 }
2728}
2729
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002730/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph() function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002731void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002732 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002733 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002734{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002735 O << "int PopulateCompilationGraph (CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002736
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002737 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2738 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002739 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002740
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002741 O << '\n';
2742
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002743 // Insert edges.
2744
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002745 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002746 for (RecordVector::const_iterator B = EdgeVector.begin(),
2747 E = EdgeVector.end(); B != E; ++B) {
2748 const Record* Edge = *B;
2749 const std::string& NodeA = Edge->getValueAsString("a");
2750 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002751 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002752
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002753 O.indent(Indent1) << "if (int ret = G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002754
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002755 if (IsDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002756 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002757 else
2758 O << "new Edge" << i << "()";
2759
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002760 O << "))\n";
2761 O.indent(Indent2) << "return ret;\n";
2762
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002763 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002764 }
2765
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002766 O << '\n';
2767 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002768 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002769}
2770
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002771/// HookInfo - Information about the hook type and number of arguments.
2772struct HookInfo {
2773
2774 // A hook can either have a single parameter of type std::vector<std::string>,
2775 // or NumArgs parameters of type const char*.
2776 enum HookType { ListHook, ArgHook };
2777
2778 HookType Type;
2779 unsigned NumArgs;
2780
2781 HookInfo() : Type(ArgHook), NumArgs(1)
2782 {}
2783
2784 HookInfo(HookType T) : Type(T), NumArgs(1)
2785 {}
2786
2787 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2788 {}
2789};
2790
2791typedef llvm::StringMap<HookInfo> HookInfoMap;
2792
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002793/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002794/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002795/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002796class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002797 HookInfoMap& HookNames_;
2798 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002799public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002800 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2801 : HookNames_(HookNames), OptDescs_(OptDescs)
2802 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002803
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002804 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002805 const std::string& Name = GetOperatorName(Dag);
2806
2807 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002808 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002809 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2810 const std::string& HookName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002811 const OptionDescription& D =
2812 OptDescs_.FindParameterListOrParameter(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002813
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002814 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2815 : HookInfo::ArgHook);
2816 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002817 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002818 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002819 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2820 }
2821 }
2822
2823 void onCmdLine(const std::string& Cmd) {
2824 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002825 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002826
2827 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2828 B != E; ++B) {
2829 const std::string& cmd = *B;
2830
2831 if (cmd == "$CALL") {
2832 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002833 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002834 const std::string& HookName = *B;
2835
2836 if (HookName.at(0) == ')')
2837 throw "$CALL invoked with no arguments!";
2838
2839 while (++B != E && B->at(0) != ')') {
2840 ++NumArgs;
2841 }
2842
2843 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2844
2845 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2846 H->second.Type != HookInfo::ArgHook)
2847 throw "Overloading of hooks is not allowed. Overloaded hook: "
2848 + HookName;
2849 else
2850 HookNames_[HookName] = HookInfo(NumArgs);
2851 }
2852 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002853 }
2854
2855 void operator()(const Init* Arg) {
2856
2857 // We're invoked on an action (either a dag or a dag list).
2858 if (typeid(*Arg) == typeid(DagInit)) {
2859 const DagInit& Dag = InitPtrToDag(Arg);
2860 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002861 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002862 }
2863 else if (typeid(*Arg) == typeid(ListInit)) {
2864 const ListInit& List = InitPtrToList(Arg);
2865 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2866 ++B) {
2867 const DagInit& Dag = InitPtrToDag(*B);
2868 this->onAction(Dag);
2869 }
2870 return;
2871 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002872
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002873 // We're invoked on a command line.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002874 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002875 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002876
2877 void operator()(const DagInit* Test, unsigned, bool) {
2878 this->operator()(Test);
2879 }
2880 void operator()(const Init* Statement, unsigned) {
2881 this->operator()(Statement);
2882 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002883};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002884
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002885/// FillInHookNames - Actually extract the hook names from all command
2886/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002887void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002888 const OptionDescriptions& OptDescs,
2889 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002890{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002891 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002892 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2893 E = ToolDescs.end(); B != E; ++B) {
2894 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002895
2896 // Look for 'forward_transformed_value' in 'actions'.
2897 if (D.Actions)
2898 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2899
2900 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002901 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002902 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002903 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002904 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002905 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002906 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002907 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002908 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002909 }
2910}
2911
2912/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2913/// property records and emit hook function declaration for each
2914/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002915void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2916 const OptionDescriptions& OptDescs, raw_ostream& O) {
2917 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002918
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002919 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002920 if (HookNames.empty())
2921 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002922
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002923 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002924 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002925 const char* HookName = B->first();
2926 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002927
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002928 O.indent(Indent1) << "std::string " << HookName << "(";
2929
2930 if (Info.Type == HookInfo::ArgHook) {
2931 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2932 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2933 }
2934 }
2935 else {
2936 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002937 }
2938
2939 O <<");\n";
2940 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002941}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002942
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002943/// EmitIncludes - Emit necessary #include directives and some
2944/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002945void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002946 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2947 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002948 << "#include \"llvm/CompilerDriver/Error.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002949 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002950
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002951 << "#include \"llvm/Support/CommandLine.h\"\n"
2952 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002953
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002954 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002955 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002956 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002957 << "#include <stdexcept>\n\n"
2958
2959 << "using namespace llvm;\n"
2960 << "using namespace llvmc;\n\n"
2961
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002962 << "inline const char* checkCString(const char* s)\n"
2963 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002964}
2965
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002966
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002967/// PluginData - Holds all information about a plugin.
2968struct PluginData {
2969 OptionDescriptions OptDescs;
2970 bool HasSink;
2971 ToolDescriptions ToolDescs;
2972 RecordVector Edges;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002973};
2974
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002975/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002976/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002977bool HasSink(const ToolDescriptions& ToolDescs) {
2978 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2979 E = ToolDescs.end(); B != E; ++B)
2980 if ((*B)->isSink())
2981 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002982
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002983 return false;
2984}
2985
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002986/// CollectPluginData - Collect compilation graph edges, tool properties and
2987/// option properties from the parse tree.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002988void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2989 // Collect option properties.
2990 const RecordVector& OptionLists =
2991 Records.getAllDerivedDefinitions("OptionList");
2992 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2993 Data.OptDescs);
2994
2995 // Collect tool properties.
2996 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2997 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2998 Data.HasSink = HasSink(Data.ToolDescs);
2999
3000 // Collect compilation graph edges.
3001 const RecordVector& CompilationGraphs =
3002 Records.getAllDerivedDefinitions("CompilationGraph");
3003 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
3004 Data.Edges);
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00003005}
3006
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003007/// CheckPluginData - Perform some sanity checks on the collected data.
3008void CheckPluginData(PluginData& Data) {
3009 // Filter out all tools not mentioned in the compilation graph.
3010 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003011
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003012 // Typecheck the compilation graph.
3013 TypecheckGraph(Data.Edges, Data.ToolDescs);
3014
3015 // Check that there are no options without side effects (specified
3016 // only in the OptionList).
3017 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003018}
3019
Daniel Dunbar1a551802009-07-03 00:10:29 +00003020void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003021 // Emit file header.
3022 EmitIncludes(O);
3023
3024 // Emit global option registration code.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003025 O << "namespace llvmc {\n"
3026 << "namespace autogenerated {\n\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003027 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003028 O << "} // End namespace autogenerated.\n"
3029 << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003030
3031 // Emit hook declarations.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003032 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003033 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003034 O << "} // End namespace hooks.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003035
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003036 O << "namespace {\n\n";
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003037 O << "using namespace llvmc::autogenerated;\n\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003038
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003039 // Emit Tool classes.
3040 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
3041 E = Data.ToolDescs.end(); B!=E; ++B)
3042 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
3043
3044 // Emit Edge# classes.
3045 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
3046
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003047 O << "} // End anonymous namespace.\n\n";
3048
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003049 O << "namespace llvmc {\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003050 O << "namespace autogenerated {\n\n";
3051
3052 // Emit PreprocessOptions() function.
3053 EmitPreprocessOptions(Records, Data.OptDescs, O);
3054
3055 // Emit PopulateLanguageMap() function
3056 // (language map maps from file extensions to language names).
3057 EmitPopulateLanguageMap(Records, O);
3058
3059 // Emit PopulateCompilationGraph() function.
3060 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
3061
3062 O << "} // End namespace autogenerated.\n";
3063 O << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003064
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003065 // EOF
3066}
3067
3068
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003069// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00003070}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003071
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003072/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00003073void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003074 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003075 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003076
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003077 CollectPluginData(Records, Data);
3078 CheckPluginData(Data);
3079
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00003080 this->EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003081 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003082
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003083 } catch (std::exception& Error) {
3084 throw Error.what() + std::string(" - usually this means a syntax error.");
3085 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003086}