blob: 6927b01bd40e7ec81543ef7fe0cfdd73edb7119f [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
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00001097/// on all edges do match.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001098void TypecheckGraph (const RecordVector& EdgeVector,
1099 const ToolDescriptions& ToolDescs) {
1100 StringMap<StringSet<> > ToolToInLang;
1101 StringMap<std::string> ToolToOutLang;
1102
1103 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
1104 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
1105 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
1106
1107 for (RecordVector::const_iterator B = EdgeVector.begin(),
1108 E = EdgeVector.end(); B != E; ++B) {
1109 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001110 const std::string& NodeA = Edge->getValueAsString("a");
1111 const std::string& NodeB = Edge->getValueAsString("b");
1112 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
1113 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001114
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001115 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001116 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001117 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001118 + ": output->input language mismatch";
1119 }
1120
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001121 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001122 throw "Edges back to the root are not allowed!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001123 }
1124}
1125
1126/// WalkCase - Walks the 'case' expression DAG and invokes
1127/// TestCallback on every test, and StatementCallback on every
1128/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001129/// combinators (that is, they are passed directly to TestCallback).
1130/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1131/// IndentLevel, bool FirstTest)'.
1132/// StatementCallback must have type 'void StatementCallback(const Init*,
1133/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001134template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001135void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1136 unsigned IndentLevel = 0)
1137{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001138 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001139
1140 // Error checks.
1141 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001142 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001143
1144 if (d.getNumArgs() < 2)
1145 throw "There should be at least one clause in the 'case' expression:\n"
1146 + d.getAsString();
1147
1148 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001149 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001150 const unsigned numArgs = d.getNumArgs();
1151 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001152 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1153 B != E; ++B) {
1154 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001155
1156 if (!even)
1157 {
1158 // Handle test.
1159 const DagInit& Test = InitPtrToDag(arg);
1160
1161 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001162 throw "The 'default' clause should be the last in the "
1163 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001164 if (i == numArgs)
1165 throw "Case construct handler: no corresponding action "
1166 "found for the test " + Test.getAsString() + '!';
1167
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001168 TestCallback(Test, IndentLevel, (i == 1));
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001169 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001170 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001171 {
1172 if (dynamic_cast<DagInit*>(arg)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001173 && GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001174 // Nested 'case'.
1175 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1176 }
1177
1178 // Handle statement.
1179 StatementCallback(arg, IndentLevel);
1180 }
1181
1182 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001183 even = !even;
1184 }
1185}
1186
1187/// ExtractOptionNames - A helper function object used by
1188/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1189class ExtractOptionNames {
1190 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001191
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001192 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001193 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001194 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001195 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001196 ActionName == "forward_value" ||
1197 ActionName == "forward_transformed_value" ||
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001198 ActionName == "switch_on" || ActionName == "any_switch_on" ||
1199 ActionName == "parameter_equals" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001200 ActionName == "element_in_list" || ActionName == "not_empty" ||
1201 ActionName == "empty") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001202 CheckNumberOfArguments(Stmt, 1);
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001203
1204 Init* Arg = Stmt.getArg(0);
1205 if (typeid(*Arg) == typeid(StringInit)) {
1206 const std::string& Name = InitPtrToString(Arg);
1207 OptionNames_.insert(Name);
1208 }
1209 else {
1210 // It's a list.
1211 const ListInit& List = InitPtrToList(Arg);
1212 for (ListInit::const_iterator B = List.begin(), E = List.end();
1213 B != E; ++B) {
1214 const std::string& Name = InitPtrToString(*B);
1215 OptionNames_.insert(Name);
1216 }
1217 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001218 }
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001219 else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001220 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001221 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001222 }
1223 }
1224 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001225
1226public:
1227 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1228 {}
1229
1230 void operator()(const Init* Statement) {
1231 if (typeid(*Statement) == typeid(ListInit)) {
1232 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1233 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1234 B != E; ++B)
1235 this->processDag(*B);
1236 }
1237 else {
1238 this->processDag(Statement);
1239 }
1240 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001241
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001242 void operator()(const DagInit& Test, unsigned, bool) {
1243 this->operator()(&Test);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001244 }
1245 void operator()(const Init* Statement, unsigned) {
1246 this->operator()(Statement);
1247 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001248};
1249
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001250/// CheckForSuperfluousOptions - Check that there are no side
1251/// effect-free options (specified only in the OptionList). Otherwise,
1252/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001253void CheckForSuperfluousOptions (const RecordVector& Edges,
1254 const ToolDescriptions& ToolDescs,
1255 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001256 llvm::StringSet<> nonSuperfluousOptions;
1257
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001258 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001259 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001260 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1261 E = ToolDescs.end(); B != E; ++B) {
1262 const ToolDescription& TD = *(*B);
1263 ExtractOptionNames Callback(nonSuperfluousOptions);
1264 if (TD.Actions)
1265 WalkCase(TD.Actions, Callback, Callback);
1266 }
1267
1268 // Add all options mentioned in the 'case' clauses of the
1269 // OptionalEdges of the compilation graph to the set of
1270 // non-superfluous options.
1271 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1272 B != E; ++B) {
1273 const Record* Edge = *B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001274 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001275
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001276 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001277 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001278 }
1279
1280 // Check that all options in OptDescs belong to the set of
1281 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001282 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001283 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001284 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001285 if (!nonSuperfluousOptions.count(Val.Name)
1286 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001287 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001288 "Probable cause: this option is specified only in the OptionList.\n";
1289 }
1290}
1291
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001292/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1293bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1294 if (TestName == "single_input_file") {
1295 O << "InputFilenames.size() == 1";
1296 return true;
1297 }
1298 else if (TestName == "multiple_input_files") {
1299 O << "InputFilenames.size() > 1";
1300 return true;
1301 }
1302
1303 return false;
1304}
1305
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001306/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1307template <typename F>
1308void EmitListTest(const ListInit& L, const char* LogicOp,
1309 F Callback, raw_ostream& O)
1310{
1311 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1312 // of Dags...
1313 bool isFirst = true;
1314 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1315 if (isFirst)
1316 isFirst = false;
1317 else
Mikhail Glushenkovb7935e02010-01-01 04:40:54 +00001318 O << ' ' << LogicOp << ' ';
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001319 Callback(InitPtrToString(*B), O);
1320 }
1321}
1322
1323// Callbacks for use with EmitListTest.
1324
1325class EmitSwitchOn {
1326 const OptionDescriptions& OptDescs_;
1327public:
1328 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1329 {}
1330
1331 void operator()(const std::string& OptName, raw_ostream& O) const {
1332 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1333 O << OptDesc.GenVariableName();
1334 }
1335};
1336
1337class EmitEmptyTest {
1338 bool EmitNegate_;
1339 const OptionDescriptions& OptDescs_;
1340public:
1341 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1342 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1343 {}
1344
1345 void operator()(const std::string& OptName, raw_ostream& O) const {
1346 const char* Neg = (EmitNegate_ ? "!" : "");
1347 if (OptName == "o") {
1348 O << Neg << "OutputFilename.empty()";
1349 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001350 else if (OptName == "save-temps") {
1351 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1352 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001353 else {
1354 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1355 O << Neg << OptDesc.GenVariableName() << ".empty()";
1356 }
1357 }
1358};
1359
1360
1361/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1362bool EmitCaseTest1ArgList(const std::string& TestName,
1363 const DagInit& d,
1364 const OptionDescriptions& OptDescs,
1365 raw_ostream& O) {
Mikhail Glushenkov3a481e32010-01-01 03:50:51 +00001366 const ListInit& L = InitPtrToList(d.getArg(0));
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001367
1368 if (TestName == "any_switch_on") {
1369 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1370 return true;
1371 }
1372 else if (TestName == "switch_on") {
1373 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1374 return true;
1375 }
1376 else if (TestName == "any_not_empty") {
1377 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1378 return true;
1379 }
1380 else if (TestName == "any_empty") {
1381 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1382 return true;
1383 }
1384 else if (TestName == "not_empty") {
1385 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1386 return true;
1387 }
1388 else if (TestName == "empty") {
1389 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1390 return true;
1391 }
1392
1393 return false;
1394}
1395
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001396/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1397bool EmitCaseTest1ArgStr(const std::string& TestName,
1398 const DagInit& d,
1399 const OptionDescriptions& OptDescs,
1400 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001401 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001402
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001403 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001404 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001405 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001406 }
1407 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001408 O << "InLangs.count(\"" << OptName << "\") != 0";
1409 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001410 }
1411 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001412 // This works only for single-argument Tool::GenerateAction. Join
1413 // tools can process several files in different languages simultaneously.
1414
1415 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001416 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001417 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001418 }
1419 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001420 bool EmitNegate = (TestName == "not_empty");
1421 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001422 return true;
1423 }
1424
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001425 return false;
1426}
1427
1428/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1429bool EmitCaseTest1Arg(const std::string& TestName,
1430 const DagInit& d,
1431 const OptionDescriptions& OptDescs,
1432 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001433 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001434 if (typeid(*d.getArg(0)) == typeid(ListInit))
1435 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1436 else
1437 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1438}
1439
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001440/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001441bool EmitCaseTest2Args(const std::string& TestName,
1442 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001443 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001444 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001445 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001446 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001447 const std::string& OptName = InitPtrToString(d.getArg(0));
1448 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001449
1450 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001451 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001452 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1453 return true;
1454 }
1455 else if (TestName == "element_in_list") {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00001456 const OptionDescription& OptDesc = OptDescs.FindParameterList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001457 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001458 O << "std::find(" << VarName << ".begin(),\n";
1459 O.indent(IndentLevel + Indent1)
1460 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001461 << OptArg << "\") != " << VarName << ".end()";
1462 return true;
1463 }
1464
1465 return false;
1466}
1467
1468// Forward declaration.
1469// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001470void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001471 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001472 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001473
1474/// EmitLogicalOperationTest - Helper function used by
1475/// EmitCaseConstructHandler.
1476void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001477 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001478 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001479 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001480 O << '(';
1481 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001482 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001483 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001484 if (j != NumArgs - 1) {
1485 O << ")\n";
1486 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1487 }
1488 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001489 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001490 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001491 }
1492}
1493
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001494void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001495 const OptionDescriptions& OptDescs, raw_ostream& O)
1496{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001497 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001498 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1499 O << "! (";
1500 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1501 O << ")";
1502}
1503
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001504/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001505void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001506 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001507 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001508 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001509
1510 if (TestName == "and")
1511 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1512 else if (TestName == "or")
1513 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001514 else if (TestName == "not")
1515 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001516 else if (EmitCaseTest0Args(TestName, O))
1517 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001518 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1519 return;
1520 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1521 return;
1522 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001523 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001524}
1525
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001526
1527/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1528class EmitCaseTestCallback {
1529 bool EmitElseIf_;
1530 const OptionDescriptions& OptDescs_;
1531 raw_ostream& O_;
1532public:
1533
1534 EmitCaseTestCallback(bool EmitElseIf,
1535 const OptionDescriptions& OptDescs, raw_ostream& O)
1536 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1537 {}
1538
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001539 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001540 {
1541 if (GetOperatorName(Test) == "default") {
1542 O_.indent(IndentLevel) << "else {\n";
1543 }
1544 else {
1545 O_.indent(IndentLevel)
1546 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001547 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001548 O_ << ") {\n";
1549 }
1550 }
1551};
1552
1553/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1554template <typename F>
1555class EmitCaseStatementCallback {
1556 F Callback_;
1557 raw_ostream& O_;
1558public:
1559
1560 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1561 : Callback_(Callback), O_(O)
1562 {}
1563
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001564 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001565
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001566 // Ignore nested 'case' DAG.
1567 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001568 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case")) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001569 if (typeid(*Statement) == typeid(ListInit)) {
1570 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1571 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1572 B != E; ++B)
1573 Callback_(*B, (IndentLevel + Indent1), O_);
1574 }
1575 else {
1576 Callback_(Statement, (IndentLevel + Indent1), O_);
1577 }
1578 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001579 O_.indent(IndentLevel) << "}\n";
1580 }
1581
1582};
1583
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001584/// EmitCaseConstructHandler - Emit code that handles the 'case'
1585/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001586/// clause. Implemented on top of WalkCase.
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00001587/// Callback's type is void F(const Init* Statement, unsigned IndentLevel,
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001588/// raw_ostream& O).
1589/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001590/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1591/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001592template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001593void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001594 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001595 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001596 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001597 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1598 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001599}
1600
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001601/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001602/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1603/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001604void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001605 const char* Delimiters = " \t\n\v\f\r";
1606 enum TokenizerState
1607 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1608 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001609
1610 if (CmdLine.empty())
1611 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001612 Out.push_back("");
1613
1614 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1615 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001616
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001617 for (; B != E; ++B) {
1618 char cur_ch = CmdLine[B];
1619
1620 switch (cur_st) {
1621 case Normal:
1622 if (cur_ch == '$') {
1623 cur_st = SpecialCommand;
1624 break;
1625 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001626 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001627 // Skip whitespace
1628 B = CmdLine.find_first_not_of(Delimiters, B);
1629 if (B == std::string::npos) {
1630 B = E-1;
1631 continue;
1632 }
1633 --B;
1634 Out.push_back("");
1635 continue;
1636 }
1637 break;
1638
1639
1640 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001641 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001642 cur_st = Normal;
1643 Out.push_back("");
1644 continue;
1645 }
1646 if (cur_ch == '(') {
1647 Out.push_back("");
1648 cur_st = InsideSpecialCommand;
1649 continue;
1650 }
1651 break;
1652
1653 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001654 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001655 continue;
1656 }
1657 if (cur_ch == '\'') {
1658 cur_st = InsideQuotationMarks;
1659 Out.push_back("");
1660 continue;
1661 }
1662 if (cur_ch == ')') {
1663 cur_st = Normal;
1664 Out.push_back("");
1665 }
1666 if (cur_ch == ',') {
1667 continue;
1668 }
1669
1670 break;
1671
1672 case InsideQuotationMarks:
1673 if (cur_ch == '\'') {
1674 cur_st = InsideSpecialCommand;
1675 continue;
1676 }
1677 break;
1678 }
1679
1680 Out.back().push_back(cur_ch);
1681 }
1682}
1683
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001684/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1685/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1686/// SubstituteSpecialCommands().
1687StrVector::const_iterator
1688SubstituteCall (StrVector::const_iterator Pos,
1689 StrVector::const_iterator End,
1690 bool IsJoin, raw_ostream& O)
1691{
1692 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001693 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001694 const std::string& CmdName = *Pos;
1695
1696 if (CmdName == ")")
1697 throw "$CALL invocation: empty argument list!";
1698
1699 O << "hooks::";
1700 O << CmdName << "(";
1701
1702
1703 bool firstIteration = true;
1704 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001705 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001706 const std::string& Arg = *Pos;
1707 assert(Arg.size() != 0);
1708
1709 if (Arg[0] == ')')
1710 break;
1711
1712 if (firstIteration)
1713 firstIteration = false;
1714 else
1715 O << ", ";
1716
1717 if (Arg == "$INFILE") {
1718 if (IsJoin)
1719 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1720 else
1721 O << "inFile.c_str()";
1722 }
1723 else {
1724 O << '"' << Arg << '"';
1725 }
1726 }
1727
1728 O << ')';
1729
1730 return Pos;
1731}
1732
1733/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1734/// function used by SubstituteSpecialCommands().
1735StrVector::const_iterator
1736SubstituteEnv (StrVector::const_iterator Pos,
1737 StrVector::const_iterator End, raw_ostream& O)
1738{
1739 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001740 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001741 const std::string& EnvName = *Pos;
1742
1743 if (EnvName == ")")
1744 throw "$ENV invocation: empty argument list!";
1745
1746 O << "checkCString(std::getenv(\"";
1747 O << EnvName;
1748 O << "\"))";
1749
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001750 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001751
1752 return Pos;
1753}
1754
1755/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1756/// handler code. Helper function used by EmitCmdLineVecFill().
1757StrVector::const_iterator
1758SubstituteSpecialCommands (StrVector::const_iterator Pos,
1759 StrVector::const_iterator End,
1760 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001761{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001762
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001763 const std::string& cmd = *Pos;
1764
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001765 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001766 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001767 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001768 }
1769 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001770 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001771 }
1772 else {
1773 throw "Unknown special command: " + cmd;
1774 }
1775
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001776 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001777 const std::string& Leftover = *Pos;
1778 assert(Leftover.at(0) == ')');
1779 if (Leftover.size() != 1)
1780 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001781
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001782 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001783}
1784
1785/// EmitCmdLineVecFill - Emit code that fills in the command line
1786/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001787void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001788 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001789 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001790 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001791 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001792
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001793 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001794 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001795
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001796 StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001797
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001798 // Emit the command itself.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001799 assert(!StrVec[0].empty());
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001800 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001801 if (StrVec[0][0] == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001802 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1803 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001804 }
1805 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001806 O << '"' << StrVec[0] << '"';
1807 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001808 }
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001809 O << ";\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001810
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001811 // Go through the command arguments.
1812 assert(B <= E);
1813 for (; B != E; ++B) {
1814 const std::string& cmd = *B;
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001815
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001816 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001817 O.indent(IndentLevel);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001818
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001819 if (cmd.at(0) == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001820 O << "vec.push_back(std::make_pair(0, ";
1821 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1822 O << "));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001823 }
1824 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001825 O << "vec.push_back(std::make_pair(0, \"" << cmd << "\"));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001826 }
1827 }
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001828
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001829}
1830
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001831/// EmitForEachListElementCycleHeader - Emit common code for iterating through
1832/// all elements of a list. Helper function used by
1833/// EmitForwardOptionPropertyHandlingCode.
1834void EmitForEachListElementCycleHeader (const OptionDescription& D,
1835 unsigned IndentLevel,
1836 raw_ostream& O) {
1837 unsigned IndentLevel1 = IndentLevel + Indent1;
1838
1839 O.indent(IndentLevel)
1840 << "for (" << D.GenTypeDeclaration()
1841 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1842 O.indent(IndentLevel)
1843 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1844 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1845 << ".getPosition(B - " << D.GenVariableName()
1846 << ".begin());\n";
1847}
1848
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001849/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1850/// implement EmitActionHandler. Emits code for
1851/// handling the (forward) and (forward_as) option properties.
1852void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001853 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001854 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001855 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001856 const std::string& Name = NewName.empty()
1857 ? ("-" + D.Name)
1858 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001859 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001860
1861 switch (D.Type) {
1862 case OptionType::Switch:
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001863 O.indent(IndentLevel)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001864 << "vec.push_back(std::make_pair(" << D.GenVariableName()
1865 << ".getPosition(), \"" << Name << "\"));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001866 break;
1867 case OptionType::Parameter:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001868 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1869 << D.GenVariableName()
1870 <<".getPosition(), \"" << Name;
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001871
1872 if (!D.isForwardNotSplit()) {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001873 O << "\"));\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001874 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1875 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001876 << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001877 }
1878 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001879 O << "=\" + " << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001880 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001881 break;
1882 case OptionType::Prefix:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001883 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1884 << D.GenVariableName() << ".getPosition(), \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001885 << Name << "\" + "
1886 << D.GenVariableName() << "));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001887 break;
1888 case OptionType::PrefixList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001889 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001890 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001891 << Name << "\" + " << "*B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001892 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001893
1894 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001895 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001896 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001897 }
1898
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001899 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001900 break;
1901 case OptionType::ParameterList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001902 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001903 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001904 << Name << "\"));\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001905
1906 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001907 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001908 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001909 }
1910
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001911 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001912 break;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001913 case OptionType::SwitchList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001914 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001915 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
1916 << Name << "\"));\n";
1917 O.indent(IndentLevel1) << "++B;\n";
1918 O.indent(IndentLevel) << "}\n";
1919 break;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001920 case OptionType::Alias:
1921 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001922 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001923 }
1924}
1925
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001926/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1927/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001928struct ActionHandlingCallbackBase
1929{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001930
1931 void onErrorDag(const DagInit& d,
1932 unsigned IndentLevel, raw_ostream& O) const
1933 {
1934 O.indent(IndentLevel)
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00001935 << "PrintError(\""
1936 << (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0)) : "Unknown error!")
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001937 << "\");\n";
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +00001938 O.indent(IndentLevel) << "return 1;\n";
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001939 }
1940
1941 void onWarningDag(const DagInit& d,
1942 unsigned IndentLevel, raw_ostream& O) const
1943 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001944 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001945 O.indent(IndentLevel) << "llvm::errs() << \""
1946 << InitPtrToString(d.getArg(0)) << "\";\n";
1947 }
1948
1949};
1950
1951/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1952/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001953class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001954
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001955typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1956(const DagInit&, unsigned, raw_ostream&) const;
1957
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001958class EmitActionHandlersCallback :
1959 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001960 public HandlerTable<EmitActionHandlersCallbackHandler>
1961{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001962 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001963
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001964 const OptionDescriptions& OptDescs;
1965
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001966 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1967 /// onAppendCmd and onOutputSuffix.
1968 void EmitHookInvocation(const std::string& Str,
1969 const char* BlockOpen, const char* BlockClose,
1970 unsigned IndentLevel, raw_ostream& O) const
1971 {
1972 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001973 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001974
1975 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1976 B != E; ++B) {
1977 const std::string& cmd = *B;
1978
1979 O.indent(IndentLevel) << BlockOpen;
1980
1981 if (cmd.at(0) == '$')
1982 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1983 else
1984 O << '"' << cmd << '"';
1985
1986 O << BlockClose;
1987 }
1988 }
1989
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001990 void onAppendCmd (const DagInit& Dag,
1991 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001992 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001993 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001994 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001995 "vec.push_back(std::make_pair(65536, ", "));\n",
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001996 IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001997 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001998
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001999 void onForward (const DagInit& Dag,
2000 unsigned IndentLevel, raw_ostream& O) const
2001 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002002 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002003 const std::string& Name = InitPtrToString(Dag.getArg(0));
2004 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2005 IndentLevel, "", O);
2006 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002007
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002008 void onForwardAs (const DagInit& Dag,
2009 unsigned IndentLevel, raw_ostream& O) const
2010 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002011 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002012 const std::string& Name = InitPtrToString(Dag.getArg(0));
2013 const std::string& NewName = InitPtrToString(Dag.getArg(1));
2014 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2015 IndentLevel, NewName, O);
2016 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002017
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002018 void onForwardValue (const DagInit& Dag,
2019 unsigned IndentLevel, raw_ostream& O) const
2020 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002021 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002022 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002023 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002024
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002025 if (D.isSwitchList()) {
2026 throw std::runtime_error
2027 ("forward_value is not allowed with switch_list");
2028 }
2029
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002030 if (D.isParameter()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002031 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2032 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002033 << D.GenVariableName() << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002034 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00002035 else {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002036 O.indent(IndentLevel) << "for (" << D.GenTypeDeclaration()
2037 << "::iterator B = " << D.GenVariableName()
2038 << ".begin(), \n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002039 O.indent(IndentLevel + Indent1) << " E = " << D.GenVariableName()
2040 << ".end(); B != E; ++B)\n";
2041 O.indent(IndentLevel) << "{\n";
2042 O.indent(IndentLevel + Indent1)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002043 << "unsigned pos = " << D.GenVariableName()
2044 << ".getPosition(B - " << D.GenVariableName()
2045 << ".begin());\n";
2046 O.indent(IndentLevel + Indent1)
2047 << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002048 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002049 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002050 }
2051
2052 void onForwardTransformedValue (const DagInit& Dag,
2053 unsigned IndentLevel, raw_ostream& O) const
2054 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002055 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002056 const std::string& Name = InitPtrToString(Dag.getArg(0));
2057 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002058 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002059
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002060 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2061 << D.GenVariableName() << ".getPosition("
2062 << (D.isList() ? "0" : "") << "), "
2063 << "hooks::" << Hook << "(" << D.GenVariableName()
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002064 << (D.isParameter() ? ".c_str()" : "") << ")));\n";
2065 }
2066
2067 void onNoOutFile (const DagInit& Dag,
2068 unsigned IndentLevel, raw_ostream& O) const
2069 {
2070 CheckNumberOfArguments(Dag, 0);
2071 O.indent(IndentLevel) << "no_out_file = true;\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002072 }
2073
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002074 void onOutputSuffix (const DagInit& Dag,
2075 unsigned IndentLevel, raw_ostream& O) const
2076 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002077 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002078 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
2079 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002080 }
2081
2082 void onStopCompilation (const DagInit& Dag,
2083 unsigned IndentLevel, raw_ostream& O) const
2084 {
2085 O.indent(IndentLevel) << "stop_compilation = true;\n";
2086 }
2087
2088
2089 void onUnpackValues (const DagInit& Dag,
2090 unsigned IndentLevel, raw_ostream& O) const
2091 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002092 throw "'unpack_values' is deprecated. "
2093 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002094 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002095
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002096 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002097
2098 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
2099 : OptDescs(OD)
2100 {
2101 if (!staticMembersInitialized_) {
2102 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
2103 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
2104 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
2105 AddHandler("forward", &EmitActionHandlersCallback::onForward);
2106 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002107 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
2108 AddHandler("forward_transformed_value",
2109 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002110 AddHandler("no_out_file",
2111 &EmitActionHandlersCallback::onNoOutFile);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002112 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
2113 AddHandler("stop_compilation",
2114 &EmitActionHandlersCallback::onStopCompilation);
2115 AddHandler("unpack_values",
2116 &EmitActionHandlersCallback::onUnpackValues);
2117
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002118
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002119 staticMembersInitialized_ = true;
2120 }
2121 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002122
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002123 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002124 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002125 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002126 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002127 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002128};
2129
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002130void EmitGenerateActionMethodHeader(const ToolDescription& D,
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002131 bool IsJoin, bool Naked,
2132 raw_ostream& O)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002133{
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002134 O.indent(Indent1) << "int GenerateAction(Action& Out,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002135
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002136 if (IsJoin)
2137 O.indent(Indent2) << "const PathVector& inFiles,\n";
2138 else
2139 O.indent(Indent2) << "const sys::Path& inFile,\n";
2140
2141 O.indent(Indent2) << "const bool HasChildren,\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002142 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2143 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2144 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2145 O.indent(Indent1) << "{\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002146
2147 if (!Naked) {
2148 O.indent(Indent2) << "std::string cmd;\n";
2149 O.indent(Indent2) << "std::string out_file;\n";
2150 O.indent(Indent2)
2151 << "std::vector<std::pair<unsigned, std::string> > vec;\n";
2152 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2153 O.indent(Indent2) << "bool no_out_file = false;\n";
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002154 O.indent(Indent2) << "std::string output_suffix(\""
2155 << D.OutputSuffix << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002156 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002157}
2158
2159// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2160// Tool::GenerateAction() method.
2161void EmitGenerateActionMethod (const ToolDescription& D,
2162 const OptionDescriptions& OptDescs,
2163 bool IsJoin, raw_ostream& O) {
2164
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002165 EmitGenerateActionMethodHeader(D, IsJoin, /* Naked = */ false, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002166
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002167 if (!D.CmdLine)
2168 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002169
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002170 // Process the 'command' property.
2171 O << '\n';
2172 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2173 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002174
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002175 // Process the 'actions' list of this tool.
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002176 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002177 EmitCaseConstructHandler(D.Actions, Indent2,
2178 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002179 false, OptDescs, O);
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002180 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002181
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002182 // Input file (s)
2183 if (!D.InFileOption.empty()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002184 O.indent(Indent2)
2185 << "vec.push_back(std::make_pair(InputFilenames.getPosition(0), \""
2186 << D.InFileOption << "\");\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002187 }
2188
2189 if (IsJoin) {
2190 O.indent(Indent2)
2191 << "for (PathVector::const_iterator B = inFiles.begin(),\n";
2192 O.indent(Indent3) << "E = inFiles.end(); B != E; ++B)\n";
2193 O.indent(Indent2) << "{\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002194 O.indent(Indent3) << "vec.push_back(std::make_pair("
2195 << "InputFilenames.getPosition(B - inFiles.begin()), "
2196 << "B->str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002197 O.indent(Indent2) << "}\n";
2198 }
2199 else {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002200 O.indent(Indent2) << "vec.push_back(std::make_pair("
2201 << "InputFilenames.getPosition(0), inFile.str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002202 }
2203
2204 // Output file
2205 O.indent(Indent2) << "if (!no_out_file) {\n";
2206 if (!D.OutFileOption.empty())
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002207 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002208 << D.OutFileOption << "\"));\n";
2209
2210 O.indent(Indent3) << "out_file = this->OutFilename("
2211 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002212 O.indent(Indent4) <<
2213 "TempDir, stop_compilation, output_suffix.c_str()).str();\n\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002214 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, out_file));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002215
2216 O.indent(Indent2) << "}\n\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002217
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002218 // Handle the Sink property.
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002219 std::string SinkOption("autogenerated::");
2220 SinkOption += SinkOptionName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002221 if (D.isSink()) {
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002222 O.indent(Indent2) << "if (!" << SinkOption << ".empty()) {\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002223 O.indent(Indent3) << "for (cl::list<std::string>::iterator B = "
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002224 << SinkOption << ".begin(), E = " << SinkOption
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002225 << ".end(); B != E; ++B)\n";
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002226 O.indent(Indent4) << "vec.push_back(std::make_pair(" << SinkOption
2227 << ".getPosition(B - " << SinkOption
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002228 << ".begin()), *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002229 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002230 }
2231
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002232 O.indent(Indent2) << "Out.Construct(cmd, this->SortArgs(vec), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002233 << "stop_compilation, out_file);\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002234 O.indent(Indent2) << "return 0;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002235 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002236}
2237
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002238/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2239/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002240void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2241 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002242 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002243 if (!ToolDesc.isJoin()) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002244 EmitGenerateActionMethodHeader(ToolDesc, /* IsJoin = */ true,
2245 /* Naked = */ true, O);
2246 O.indent(Indent2) << "PrintError(\"" << ToolDesc.Name
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002247 << " is not a Join tool!\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002248 O.indent(Indent2) << "return -1;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002249 O.indent(Indent1) << "}\n\n";
2250 }
2251 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002252 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002253 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002254
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002255 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002256}
2257
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002258/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2259/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002260void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002261 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2262 O.indent(Indent2) << "return InputLanguages_;\n";
2263 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002264
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002265 if (D.OutLanguage.empty())
2266 throw "Tool " + D.Name + " has no 'out_language' property!";
2267
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002268 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2269 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2270 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002271}
2272
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002273/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002274void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002275 O.indent(Indent1) << "const char* Name() const {\n";
2276 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2277 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002278}
2279
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002280/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2281/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002282void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002283 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002284 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002285 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002286 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002287 O.indent(Indent2) << "return false;\n";
2288 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002289}
2290
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002291/// EmitWorksOnEmptyCallback - Callback used by EmitWorksOnEmptyMethod in
2292/// conjunction with EmitCaseConstructHandler.
2293void EmitWorksOnEmptyCallback (const Init* Value,
2294 unsigned IndentLevel, raw_ostream& O) {
2295 CheckBooleanConstant(Value);
2296 O.indent(IndentLevel) << "return " << Value->getAsString() << ";\n";
2297}
2298
2299/// EmitWorksOnEmptyMethod - Emit the WorksOnEmpty() method for a given Tool
2300/// class.
2301void EmitWorksOnEmptyMethod (const ToolDescription& D,
2302 const OptionDescriptions& OptDescs,
2303 raw_ostream& O)
2304{
2305 O.indent(Indent1) << "bool WorksOnEmpty() const {\n";
2306 if (D.OnEmpty == 0)
2307 O.indent(Indent2) << "return false;\n";
2308 else
2309 EmitCaseConstructHandler(D.OnEmpty, Indent2, EmitWorksOnEmptyCallback,
2310 /*EmitElseIf = */ true, OptDescs, O);
2311 O.indent(Indent1) << "}\n\n";
2312}
2313
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002314/// EmitStaticMemberDefinitions - Emit static member definitions for a
2315/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002316void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002317 if (D.InLanguage.empty())
2318 throw "Tool " + D.Name + " has no 'in_language' property!";
2319
2320 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2321 for (StrVector::const_iterator B = D.InLanguage.begin(),
2322 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002323 O << '\"' << *B << "\", ";
2324 O << "0};\n\n";
2325}
2326
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002327/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002328void EmitToolClassDefinition (const ToolDescription& D,
2329 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002330 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002331 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002332 return;
2333
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002334 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002335 O << "class " << D.Name << " : public ";
2336 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002337 O << "JoinTool";
2338 else
2339 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002340
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002341 O << " {\nprivate:\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002342 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002343
2344 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002345 EmitNameMethod(D, O);
2346 EmitInOutLanguageMethods(D, O);
2347 EmitIsJoinMethod(D, O);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002348 EmitWorksOnEmptyMethod(D, OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002349 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002350
2351 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002352 O << "};\n";
2353
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002354 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002355
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002356}
2357
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002358/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002359/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002360void EmitOptionDefinitions (const OptionDescriptions& descs,
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002361 bool HasSink, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002362{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002363 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002364
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002365 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002366 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002367 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002368 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002369
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002370 if (val.Type == OptionType::Alias) {
2371 Aliases.push_back(val);
2372 continue;
2373 }
2374
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002375 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002376 << val.GenPlainVariableName();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002377
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002378 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002379
2380 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2381 O << ", cl::Prefix";
2382
2383 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002384 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002385 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002386 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002387 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002388 }
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002389
2390 if (val.isOptional())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002391 O << ", cl::Optional";
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002392
2393 if (val.isOneOrMore())
2394 O << ", cl::OneOrMore";
2395
2396 if (val.isZeroOrMore())
2397 O << ", cl::ZeroOrMore";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002398
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002399 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002400 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002401 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002402 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002403
2404 if (val.isCommaSeparated())
2405 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002406
2407 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002408 O << ", cl::multi_val(" << val.MultiVal << ')';
2409
2410 if (val.InitVal) {
2411 const std::string& str = val.InitVal->getAsString();
2412 O << ", cl::init(" << str << ')';
2413 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002414
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002415 if (!val.Help.empty())
2416 O << ", cl::desc(\"" << val.Help << "\")";
2417
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002418 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002419 }
2420
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002421 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002422 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002423 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002424 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002425
2426 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov297514d2010-08-20 18:16:26 +00002427 << val.GenPlainVariableName()
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002428 << "(\"" << val.Name << '\"';
2429
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002430 const OptionDescription& D = descs.FindOption(val.Help);
2431 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002432
2433 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2434 }
2435
2436 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002437 if (HasSink)
Mikhail Glushenkov7a574542010-08-20 11:24:51 +00002438 O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002439
2440 O << '\n';
2441}
2442
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002443/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002444/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002445
2446class EmitPreprocessOptionsCallback;
2447
2448typedef void
2449(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2450(const DagInit&, unsigned, raw_ostream&) const;
2451
2452class EmitPreprocessOptionsCallback :
2453 public ActionHandlingCallbackBase,
2454 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2455{
2456 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002457 typedef void
2458 (EmitPreprocessOptionsCallback::* HandlerImpl)
2459 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002460
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002461 const OptionDescriptions& OptDescs_;
2462
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002463 void onListOrDag(const DagInit& d, HandlerImpl h,
2464 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002465 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002466 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002467 const Init* I = d.getArg(0);
2468
2469 // If I is a list, apply h to each element.
2470 if (typeid(*I) == typeid(ListInit)) {
2471 const ListInit& L = *static_cast<const ListInit*>(I);
2472 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B)
2473 ((this)->*(h))(*B, IndentLevel, O);
2474 }
2475 // Otherwise, apply h to I.
2476 else {
2477 ((this)->*(h))(I, IndentLevel, O);
2478 }
2479 }
2480
2481 void onUnsetOptionImpl(const Init* I,
2482 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002483 {
2484 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002485 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002486
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002487 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002488 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2489 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002490 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002491 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2492 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002493 else if (OptDesc.isList()) {
2494 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2495 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002496 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002497 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002498 }
2499 }
2500
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002501 void onUnsetOption(const DagInit& d,
2502 unsigned IndentLevel, raw_ostream& O) const
2503 {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002504 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2505 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002506 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002507
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002508 void onSetOptionImpl(const DagInit& d,
2509 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002510 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002511 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002512 const Init* Value = d.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002513 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2514
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002515 if (OptDesc.isList()) {
2516 const ListInit& List = InitPtrToList(Value);
2517
2518 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2519 for (ListInit::const_iterator B = List.begin(), E = List.end();
2520 B != E; ++B) {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002521 const Init* CurElem = *B;
2522 if (OptDesc.isSwitchList())
2523 CheckBooleanConstant(CurElem);
2524
2525 O.indent(IndentLevel)
2526 << OptDesc.GenVariableName() << ".push_back(\""
2527 << (OptDesc.isSwitchList() ? CurElem->getAsString()
2528 : InitPtrToString(CurElem))
2529 << "\");\n";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002530 }
2531 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002532 else if (OptDesc.isSwitch()) {
2533 CheckBooleanConstant(Value);
2534 O.indent(IndentLevel) << OptDesc.GenVariableName()
2535 << " = " << Value->getAsString() << ";\n";
2536 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002537 else if (OptDesc.isParameter()) {
2538 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002539 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002540 << " = \"" << Str << "\";\n";
2541 }
2542 else {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002543 throw "Can't apply 'set_option' to alias option -" + OptName + " !";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002544 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002545 }
2546
2547 void onSetSwitch(const Init* I,
2548 unsigned IndentLevel, raw_ostream& O) const {
2549 const std::string& OptName = InitPtrToString(I);
2550 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2551
2552 if (OptDesc.isSwitch())
2553 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2554 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002555 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002556 }
2557
2558 void onSetOption(const DagInit& d,
2559 unsigned IndentLevel, raw_ostream& O) const
2560 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002561 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002562
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002563 // Two arguments: (set_option "parameter", VALUE), where VALUE can be a
2564 // boolean, a string or a string list.
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002565 if (d.getNumArgs() > 1)
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002566 this->onSetOptionImpl(d, IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002567 // One argument: (set_option "switch")
2568 // or (set_option ["switch1", "switch2", ...])
2569 else
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002570 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2571 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002572 }
2573
2574public:
2575
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002576 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002577 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002578 {
2579 if (!staticMembersInitialized_) {
2580 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2581 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2582 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002583 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002584
2585 staticMembersInitialized_ = true;
2586 }
2587 }
2588
2589 void operator()(const Init* I,
2590 unsigned IndentLevel, raw_ostream& O) const
2591 {
2592 InvokeDagInitHandler(this, I, IndentLevel, O);
2593 }
2594
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002595};
2596
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002597/// EmitPreprocessOptions - Emit the PreprocessOptions() function.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002598void EmitPreprocessOptions (const RecordKeeper& Records,
2599 const OptionDescriptions& OptDecs, raw_ostream& O)
2600{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002601 O << "int PreprocessOptions () {\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002602
2603 const RecordVector& OptionPreprocessors =
2604 Records.getAllDerivedDefinitions("OptionPreprocessor");
2605
2606 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2607 E = OptionPreprocessors.end(); B!=E; ++B) {
2608 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002609 EmitCaseConstructHandler(Case, Indent1,
2610 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002611 false, OptDecs, O);
2612 }
2613
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002614 O << '\n';
2615 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002616 O << "}\n\n";
2617}
2618
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002619/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002620void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002621{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002622 O << "int PopulateLanguageMap (LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002623
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002624 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002625 // TODO: change this to getAllDerivedDefinitions.
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002626 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002627
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002628 if (LangMapRecord) {
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002629 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2630 if (!LangsToSuffixesList)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00002631 throw "Error in the language map definition!";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002632
2633 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002634 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002635
2636 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2637 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2638
2639 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002640 O.indent(Indent1) << "langMap[\""
2641 << InitPtrToString(Suffixes->getElement(i))
2642 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002643 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002644 }
2645
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002646 O << '\n';
2647 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002648 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002649}
2650
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002651/// EmitEdgePropertyHandlerCallback - Emits code that handles edge
2652/// properties. Helper function passed to EmitCaseConstructHandler() by
2653/// EmitEdgeClass().
2654void EmitEdgePropertyHandlerCallback (const Init* i, unsigned IndentLevel,
2655 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002656 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002657 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002658
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002659 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002660 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002661 }
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002662 else if (OpName == "error") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002663 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002664 O.indent(IndentLevel) << "PrintError(\""
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002665 << InitPtrToString(d.getArg(0))
2666 << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002667 O.indent(IndentLevel) << "return -1;\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002668 return;
2669 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002670 else {
2671 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002672 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002673 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002674
2675 if (d.getNumArgs() > 0)
2676 O << InitPtrToInt(d.getArg(0)) << ";\n";
2677 else
2678 O << "2;\n";
2679
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002680}
2681
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002682/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002683void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002684 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002685 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002686
2687 // Class constructor.
2688 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002689 << "public:\n";
2690 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2691 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002692
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002693 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002694 O.indent(Indent1)
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002695 << "int Weight(const InputLanguagesSet& InLangs) const {\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002696 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002697
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002698 // Handle the 'case' construct.
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +00002699 EmitCaseConstructHandler(Case, Indent2, EmitEdgePropertyHandlerCallback,
2700 false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002701
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002702 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002703 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002704}
2705
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002706/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002707void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002708 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002709 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002710 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002711 for (RecordVector::const_iterator B = EdgeVector.begin(),
2712 E = EdgeVector.end(); B != E; ++B) {
2713 const Record* Edge = *B;
2714 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002715 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002716
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002717 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002718 EmitEdgeClass(i, NodeB, &Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002719 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002720 }
2721}
2722
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002723/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph() function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002724void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002725 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002726 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002727{
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002728 O << "int PopulateCompilationGraph (CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002729
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002730 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2731 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002732 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002733
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002734 O << '\n';
2735
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002736 // Insert edges.
2737
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002738 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002739 for (RecordVector::const_iterator B = EdgeVector.begin(),
2740 E = EdgeVector.end(); B != E; ++B) {
2741 const Record* Edge = *B;
2742 const std::string& NodeA = Edge->getValueAsString("a");
2743 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002744 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002745
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002746 O.indent(Indent1) << "if (int ret = G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002747
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002748 if (IsDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002749 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002750 else
2751 O << "new Edge" << i << "()";
2752
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002753 O << "))\n";
2754 O.indent(Indent2) << "return ret;\n";
2755
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002756 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002757 }
2758
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002759 O << '\n';
2760 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002761 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002762}
2763
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002764/// HookInfo - Information about the hook type and number of arguments.
2765struct HookInfo {
2766
2767 // A hook can either have a single parameter of type std::vector<std::string>,
2768 // or NumArgs parameters of type const char*.
2769 enum HookType { ListHook, ArgHook };
2770
2771 HookType Type;
2772 unsigned NumArgs;
2773
2774 HookInfo() : Type(ArgHook), NumArgs(1)
2775 {}
2776
2777 HookInfo(HookType T) : Type(T), NumArgs(1)
2778 {}
2779
2780 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2781 {}
2782};
2783
2784typedef llvm::StringMap<HookInfo> HookInfoMap;
2785
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002786/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002787/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002788/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002789class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002790 HookInfoMap& HookNames_;
2791 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002792public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002793 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2794 : HookNames_(HookNames), OptDescs_(OptDescs)
2795 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002796
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002797 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002798 const std::string& Name = GetOperatorName(Dag);
2799
2800 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002801 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002802 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2803 const std::string& HookName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002804 const OptionDescription& D =
2805 OptDescs_.FindParameterListOrParameter(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002806
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002807 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2808 : HookInfo::ArgHook);
2809 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002810 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002811 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002812 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2813 }
2814 }
2815
2816 void onCmdLine(const std::string& Cmd) {
2817 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002818 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002819
2820 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2821 B != E; ++B) {
2822 const std::string& cmd = *B;
2823
2824 if (cmd == "$CALL") {
2825 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002826 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002827 const std::string& HookName = *B;
2828
2829 if (HookName.at(0) == ')')
2830 throw "$CALL invoked with no arguments!";
2831
2832 while (++B != E && B->at(0) != ')') {
2833 ++NumArgs;
2834 }
2835
2836 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2837
2838 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2839 H->second.Type != HookInfo::ArgHook)
2840 throw "Overloading of hooks is not allowed. Overloaded hook: "
2841 + HookName;
2842 else
2843 HookNames_[HookName] = HookInfo(NumArgs);
2844 }
2845 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002846 }
2847
2848 void operator()(const Init* Arg) {
2849
2850 // We're invoked on an action (either a dag or a dag list).
2851 if (typeid(*Arg) == typeid(DagInit)) {
2852 const DagInit& Dag = InitPtrToDag(Arg);
2853 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002854 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002855 }
2856 else if (typeid(*Arg) == typeid(ListInit)) {
2857 const ListInit& List = InitPtrToList(Arg);
2858 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2859 ++B) {
2860 const DagInit& Dag = InitPtrToDag(*B);
2861 this->onAction(Dag);
2862 }
2863 return;
2864 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002865
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002866 // We're invoked on a command line.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002867 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002868 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002869
2870 void operator()(const DagInit* Test, unsigned, bool) {
2871 this->operator()(Test);
2872 }
2873 void operator()(const Init* Statement, unsigned) {
2874 this->operator()(Statement);
2875 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002876};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002877
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002878/// FillInHookNames - Actually extract the hook names from all command
2879/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002880void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002881 const OptionDescriptions& OptDescs,
2882 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002883{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002884 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002885 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2886 E = ToolDescs.end(); B != E; ++B) {
2887 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002888
2889 // Look for 'forward_transformed_value' in 'actions'.
2890 if (D.Actions)
2891 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2892
2893 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002894 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002895 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002896 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002897 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002898 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002899 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002900 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002901 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002902 }
2903}
2904
2905/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2906/// property records and emit hook function declaration for each
2907/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002908void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2909 const OptionDescriptions& OptDescs, raw_ostream& O) {
2910 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002911
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002912 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002913 if (HookNames.empty())
2914 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002915
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002916 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002917 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002918 const char* HookName = B->first();
2919 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002920
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002921 O.indent(Indent1) << "std::string " << HookName << "(";
2922
2923 if (Info.Type == HookInfo::ArgHook) {
2924 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2925 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2926 }
2927 }
2928 else {
2929 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002930 }
2931
2932 O <<");\n";
2933 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002934}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002935
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002936/// EmitIncludes - Emit necessary #include directives and some
2937/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002938void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002939 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2940 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002941 << "#include \"llvm/CompilerDriver/Error.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002942 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002943
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002944 << "#include \"llvm/Support/CommandLine.h\"\n"
2945 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002946
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002947 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002948 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002949 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002950 << "#include <stdexcept>\n\n"
2951
2952 << "using namespace llvm;\n"
2953 << "using namespace llvmc;\n\n"
2954
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002955 << "inline const char* checkCString(const char* s)\n"
2956 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002957}
2958
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002959
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00002960/// DriverData - Holds all information about the driver.
2961struct DriverData {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002962 OptionDescriptions OptDescs;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002963 ToolDescriptions ToolDescs;
2964 RecordVector Edges;
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00002965 bool HasSink;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002966};
2967
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002968/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002969/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002970bool HasSink(const ToolDescriptions& ToolDescs) {
2971 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2972 E = ToolDescs.end(); B != E; ++B)
2973 if ((*B)->isSink())
2974 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002975
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002976 return false;
2977}
2978
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00002979/// CollectDriverData - Collect compilation graph edges, tool properties and
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00002980/// option properties from the parse tree.
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00002981void CollectDriverData (const RecordKeeper& Records, DriverData& Data) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002982 // Collect option properties.
2983 const RecordVector& OptionLists =
2984 Records.getAllDerivedDefinitions("OptionList");
2985 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2986 Data.OptDescs);
2987
2988 // Collect tool properties.
2989 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2990 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2991 Data.HasSink = HasSink(Data.ToolDescs);
2992
2993 // Collect compilation graph edges.
2994 const RecordVector& CompilationGraphs =
2995 Records.getAllDerivedDefinitions("CompilationGraph");
2996 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2997 Data.Edges);
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002998}
2999
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003000/// CheckDriverData - Perform some sanity checks on the collected data.
3001void CheckDriverData(DriverData& Data) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003002 // Filter out all tools not mentioned in the compilation graph.
3003 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003004
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003005 // Typecheck the compilation graph.
3006 TypecheckGraph(Data.Edges, Data.ToolDescs);
3007
3008 // Check that there are no options without side effects (specified
3009 // only in the OptionList).
3010 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003011}
3012
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003013void EmitDriverCode(const DriverData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003014 // Emit file header.
3015 EmitIncludes(O);
3016
3017 // Emit global option registration code.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003018 O << "namespace llvmc {\n"
3019 << "namespace autogenerated {\n\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003020 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003021 O << "} // End namespace autogenerated.\n"
3022 << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003023
3024 // Emit hook declarations.
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003025 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003026 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003027 O << "} // End namespace hooks.\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003028
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003029 O << "namespace {\n\n";
Mikhail Glushenkov03b6d4e2010-08-20 11:24:44 +00003030 O << "using namespace llvmc::autogenerated;\n\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003031
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003032 // Emit Tool classes.
3033 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
3034 E = Data.ToolDescs.end(); B!=E; ++B)
3035 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
3036
3037 // Emit Edge# classes.
3038 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
3039
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003040 O << "} // End anonymous namespace.\n\n";
3041
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003042 O << "namespace llvmc {\n";
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +00003043 O << "namespace autogenerated {\n\n";
3044
3045 // Emit PreprocessOptions() function.
3046 EmitPreprocessOptions(Records, Data.OptDescs, O);
3047
3048 // Emit PopulateLanguageMap() function
3049 // (language map maps from file extensions to language names).
3050 EmitPopulateLanguageMap(Records, O);
3051
3052 // Emit PopulateCompilationGraph() function.
3053 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
3054
3055 O << "} // End namespace autogenerated.\n";
3056 O << "} // End namespace llvmc.\n\n";
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003057
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003058 // EOF
3059}
3060
3061
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003062// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00003063}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003064
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003065/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00003066void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003067 try {
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003068 DriverData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003069
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003070 CollectDriverData(Records, Data);
3071 CheckDriverData(Data);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003072
Mikhail Glushenkovc712edc2010-08-23 19:24:00 +00003073 this->EmitSourceFileHeader("llvmc-based driver: auto-generated code", O);
3074 EmitDriverCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003075
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003076 } catch (std::exception& Error) {
3077 throw Error.what() + std::string(" - usually this means a syntax error.");
3078 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003079}