blob: 767e5b7124921eed3be720e44520965ced9b4331 [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"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000020#include <algorithm>
21#include <cassert>
22#include <functional>
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +000023#include <stdexcept>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000024#include <string>
Chris Lattner32a9e7a2008-06-04 04:46:14 +000025#include <typeinfo>
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +000026
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000027using namespace llvm;
28
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000029namespace {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000030
31//===----------------------------------------------------------------------===//
32/// Typedefs
33
34typedef std::vector<Record*> RecordVector;
35typedef std::vector<std::string> StrVector;
36
37//===----------------------------------------------------------------------===//
38/// Constants
39
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +000040// Indentation.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000041const unsigned TabWidth = 4;
42const unsigned Indent1 = TabWidth*1;
43const unsigned Indent2 = TabWidth*2;
44const unsigned Indent3 = TabWidth*3;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000045
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000046// Default help string.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000047const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000048
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000049// Name for the "sink" option.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000050const char * const SinkOptionName = "AutoGeneratedSinkOption";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000051
52//===----------------------------------------------------------------------===//
53/// Helper functions
54
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000055/// Id - An 'identity' function object.
56struct Id {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000057 template<typename T0>
58 void operator()(const T0&) const {
59 }
60 template<typename T0, typename T1>
61 void operator()(const T0&, const T1&) const {
62 }
63 template<typename T0, typename T1, typename T2>
64 void operator()(const T0&, const T1&, const T2&) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000065 }
66};
67
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000068int InitPtrToInt(const Init* ptr) {
69 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000070 return val.getValue();
71}
72
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000073const std::string& InitPtrToString(const Init* ptr) {
74 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
75 return val.getValue();
76}
77
78const ListInit& InitPtrToList(const Init* ptr) {
79 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
80 return val;
81}
82
83const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000084 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000085 return val;
86}
87
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000088const std::string GetOperatorName(const DagInit* D) {
89 return D->getOperator()->getAsString();
90}
91
92const std::string GetOperatorName(const DagInit& D) {
93 return GetOperatorName(&D);
94}
95
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000096// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +000097// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +000098void checkNumberOfArguments (const DagInit* d, unsigned minArgs) {
99 if (!d || d->getNumArgs() < minArgs)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000100 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000101}
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +0000102void checkNumberOfArguments (const DagInit& d, unsigned minArgs) {
103 checkNumberOfArguments(&d, minArgs);
104}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000105
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000106// isDagEmpty - is this DAG marked with an empty marker?
107bool isDagEmpty (const DagInit* d) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000108 return GetOperatorName(d) == "empty_dag_marker";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000109}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000110
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000111// EscapeVariableName - Escape commas and other symbols not allowed
112// in the C++ variable names. Makes it possible to use options named
113// like "Wa," (useful for prefix options).
114std::string EscapeVariableName(const std::string& Var) {
115 std::string ret;
116 for (unsigned i = 0; i != Var.size(); ++i) {
117 char cur_char = Var[i];
118 if (cur_char == ',') {
119 ret += "_comma_";
120 }
121 else if (cur_char == '+') {
122 ret += "_plus_";
123 }
124 else if (cur_char == '-') {
125 ret += "_dash_";
126 }
127 else {
128 ret.push_back(cur_char);
129 }
130 }
131 return ret;
132}
133
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000134/// oneOf - Does the input string contain this character?
135bool oneOf(const char* lst, char c) {
136 while (*lst) {
137 if (*lst++ == c)
138 return true;
139 }
140 return false;
141}
142
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000143template <class I, class S>
144void checkedIncrement(I& P, I E, S ErrorString) {
145 ++P;
146 if (P == E)
147 throw ErrorString;
148}
149
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000150// apply is needed because C++'s syntax doesn't let us construct a function
151// object and call it in the same statement.
152template<typename F, typename T0>
153void apply(F Fun, T0& Arg0) {
154 return Fun(Arg0);
155}
156
157template<typename F, typename T0, typename T1>
158void apply(F Fun, T0& Arg0, T1& Arg1) {
159 return Fun(Arg0, Arg1);
160}
161
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000162//===----------------------------------------------------------------------===//
163/// Back-end specific code
164
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000165
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000166/// OptionType - One of six different option types. See the
167/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000168namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000169
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000170 enum OptionType { Alias, Switch, Parameter, ParameterList,
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000171 Prefix, PrefixList};
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000172
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000173 bool IsAlias(OptionType t) {
174 return (t == Alias);
175 }
176
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000177 bool IsList (OptionType t) {
178 return (t == ParameterList || t == PrefixList);
179 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000180
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000181 bool IsSwitch (OptionType t) {
182 return (t == Switch);
183 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000184
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000185 bool IsParameter (OptionType t) {
186 return (t == Parameter || t == Prefix);
187 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000188
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000189}
190
191OptionType::OptionType stringToOptionType(const std::string& T) {
192 if (T == "alias_option")
193 return OptionType::Alias;
194 else if (T == "switch_option")
195 return OptionType::Switch;
196 else if (T == "parameter_option")
197 return OptionType::Parameter;
198 else if (T == "parameter_list_option")
199 return OptionType::ParameterList;
200 else if (T == "prefix_option")
201 return OptionType::Prefix;
202 else if (T == "prefix_list_option")
203 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000204 else
205 throw "Unknown option type: " + T + '!';
206}
207
208namespace OptionDescriptionFlags {
209 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000210 ReallyHidden = 0x4, Extern = 0x8,
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000211 OneOrMore = 0x10, Optional = 0x20,
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000212 CommaSeparated = 0x40 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000213}
214
215/// OptionDescription - Represents data contained in a single
216/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000217struct OptionDescription {
218 OptionType::OptionType Type;
219 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000220 unsigned Flags;
221 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000222 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000223 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000224
225 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000226 const std::string& n = "",
227 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000228 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000229 {}
230
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000231 /// GenTypeDeclaration - Returns the C++ variable type of this
232 /// option.
233 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000234
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000235 /// GenVariableName - Returns the variable name used in the
236 /// generated C++ code.
237 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000238
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000239 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000240 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000241
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000242 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000243
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000244 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000245
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000246 bool isMultiVal() const;
247
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000248 bool isCommaSeparated() const;
249 void setCommaSeparated();
250
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000251 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000252 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000253
254 bool isRequired() const;
255 void setRequired();
256
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000257 bool isOneOrMore() const;
258 void setOneOrMore();
259
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000260 bool isOptional() const;
261 void setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000262
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000263 bool isHidden() const;
264 void setHidden();
265
266 bool isReallyHidden() const;
267 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000268
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000269 bool isSwitch() const
270 { return OptionType::IsSwitch(this->Type); }
271
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000272 bool isParameter() const
273 { return OptionType::IsParameter(this->Type); }
274
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000275 bool isList() const
276 { return OptionType::IsList(this->Type); }
277
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000278};
279
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000280void OptionDescription::Merge (const OptionDescription& other)
281{
282 if (other.Type != Type)
283 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000284
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000285 if (Help == other.Help || Help == DefaultHelpString)
286 Help = other.Help;
287 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000288 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000289 " defined for option " + Name + "\n";
290 }
291
292 Flags |= other.Flags;
293}
294
295bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000296 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000297}
298
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000299bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000300 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000301}
302
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000303bool OptionDescription::isCommaSeparated() const {
304 return Flags & OptionDescriptionFlags::CommaSeparated;
305}
306void OptionDescription::setCommaSeparated() {
307 Flags |= OptionDescriptionFlags::CommaSeparated;
308}
309
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000310bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000311 return Flags & OptionDescriptionFlags::Extern;
312}
313void OptionDescription::setExtern() {
314 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000315}
316
317bool OptionDescription::isRequired() const {
318 return Flags & OptionDescriptionFlags::Required;
319}
320void OptionDescription::setRequired() {
321 Flags |= OptionDescriptionFlags::Required;
322}
323
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000324bool OptionDescription::isOneOrMore() const {
325 return Flags & OptionDescriptionFlags::OneOrMore;
326}
327void OptionDescription::setOneOrMore() {
328 Flags |= OptionDescriptionFlags::OneOrMore;
329}
330
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000331bool OptionDescription::isOptional() const {
332 return Flags & OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000333}
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000334void OptionDescription::setOptional() {
335 Flags |= OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000336}
337
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000338bool OptionDescription::isHidden() const {
339 return Flags & OptionDescriptionFlags::Hidden;
340}
341void OptionDescription::setHidden() {
342 Flags |= OptionDescriptionFlags::Hidden;
343}
344
345bool OptionDescription::isReallyHidden() const {
346 return Flags & OptionDescriptionFlags::ReallyHidden;
347}
348void OptionDescription::setReallyHidden() {
349 Flags |= OptionDescriptionFlags::ReallyHidden;
350}
351
352const char* OptionDescription::GenTypeDeclaration() const {
353 switch (Type) {
354 case OptionType::Alias:
355 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000356 case OptionType::PrefixList:
357 case OptionType::ParameterList:
358 return "cl::list<std::string>";
359 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000360 return "cl::opt<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000361 case OptionType::Parameter:
362 case OptionType::Prefix:
363 default:
364 return "cl::opt<std::string>";
365 }
366}
367
368std::string OptionDescription::GenVariableName() const {
369 const std::string& EscapedName = EscapeVariableName(Name);
370 switch (Type) {
371 case OptionType::Alias:
372 return "AutoGeneratedAlias_" + EscapedName;
373 case OptionType::PrefixList:
374 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000375 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000376 case OptionType::Switch:
377 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000378 case OptionType::Prefix:
379 case OptionType::Parameter:
380 default:
381 return "AutoGeneratedParameter_" + EscapedName;
382 }
383}
384
385/// OptionDescriptions - An OptionDescription array plus some helper
386/// functions.
387class OptionDescriptions {
388 typedef StringMap<OptionDescription> container_type;
389
390 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000391 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000392
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000393public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000394 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000395 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000396
397 // Wrappers for FindOption that throw an exception in case the option has a
398 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000399 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000400 const OptionDescription& FindParameter(const std::string& OptName) const;
401 const OptionDescription& FindList(const std::string& OptName) const;
402 const OptionDescription&
403 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000404
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000405 /// insertDescription - Insert new OptionDescription into
406 /// OptionDescriptions list
407 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000408
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000409 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000410 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000411 const_iterator begin() const { return Descriptions.begin(); }
412 const_iterator end() const { return Descriptions.end(); }
413};
414
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000415const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000416OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000417 const_iterator I = Descriptions.find(OptName);
418 if (I != Descriptions.end())
419 return I->second;
420 else
421 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000422}
423
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000424const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000425OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000426 const OptionDescription& OptDesc = this->FindOption(OptName);
427 if (!OptDesc.isSwitch())
428 throw OptName + ": incorrect option type - should be a switch!";
429 return OptDesc;
430}
431
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000432const OptionDescription&
433OptionDescriptions::FindList(const std::string& OptName) const {
434 const OptionDescription& OptDesc = this->FindOption(OptName);
435 if (!OptDesc.isList())
436 throw OptName + ": incorrect option type - should be a list!";
437 return OptDesc;
438}
439
440const OptionDescription&
441OptionDescriptions::FindParameter(const std::string& OptName) const {
442 const OptionDescription& OptDesc = this->FindOption(OptName);
443 if (!OptDesc.isParameter())
444 throw OptName + ": incorrect option type - should be a parameter!";
445 return OptDesc;
446}
447
448const OptionDescription&
449OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
450 const OptionDescription& OptDesc = this->FindOption(OptName);
451 if (!OptDesc.isList() && !OptDesc.isParameter())
452 throw OptName
453 + ": incorrect option type - should be a list or parameter!";
454 return OptDesc;
455}
456
457void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000458 container_type::iterator I = Descriptions.find(o.Name);
459 if (I != Descriptions.end()) {
460 OptionDescription& D = I->second;
461 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000462 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000463 else {
464 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000465 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000466}
467
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000468/// HandlerTable - A base class for function objects implemented as
469/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000470template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000471class HandlerTable {
472protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000473 // Implementation details.
474
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000475 /// HandlerMap - A map from property names to property handlers
476 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000477
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000478 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000479 static bool staticMembersInitialized_;
480
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000481public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000482
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000483 Handler GetHandler (const std::string& HandlerName) const {
484 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000485
486 if (method != Handlers_.end()) {
487 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000488 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000489 }
490 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000491 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000492 }
493 }
494
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000495 void AddHandler(const char* Property, Handler H) {
496 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000497 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000498
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000499};
500
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000501template <class FunctionObject>
502void InvokeDagInitHandler(FunctionObject* Obj, Init* i) {
503 typedef void (FunctionObject::*Handler) (const DagInit*);
504
505 const DagInit& property = InitPtrToDag(i);
506 const std::string& property_name = GetOperatorName(property);
507 Handler h = Obj->GetHandler(property_name);
508
509 ((Obj)->*(h))(&property);
510}
511
512template <typename H>
513typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
514
515template <typename H>
516bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000517
518
519/// CollectOptionProperties - Function object for iterating over an
520/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000521class CollectOptionProperties;
522typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
523(const DagInit*);
524
525class CollectOptionProperties
526: public HandlerTable<CollectOptionPropertiesHandler>
527{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000528private:
529
530 /// optDescs_ - OptionDescriptions table. This is where the
531 /// information is stored.
532 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000533
534public:
535
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000536 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000537 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000538 {
539 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000540 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000541 AddHandler("help", &CollectOptionProperties::onHelp);
542 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000543 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000544 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
545 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000546 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
547 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000548 AddHandler("optional", &CollectOptionProperties::onOptional);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000549 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000550
551 staticMembersInitialized_ = true;
552 }
553 }
554
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000555 /// operator() - Just forwards to the corresponding property
556 /// handler.
557 void operator() (Init* i) {
558 InvokeDagInitHandler(this, i);
559 }
560
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000561private:
562
563 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000564 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000565
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000566 void onExtern (const DagInit* d) {
567 checkNumberOfArguments(d, 0);
568 optDesc_.setExtern();
569 }
570
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000571 void onHelp (const DagInit* d) {
572 checkNumberOfArguments(d, 1);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000573 optDesc_.Help = InitPtrToString(d->getArg(0));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000574 }
575
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000576 void onHidden (const DagInit* d) {
577 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000578 optDesc_.setHidden();
579 }
580
581 void onReallyHidden (const DagInit* d) {
582 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000583 optDesc_.setReallyHidden();
584 }
585
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000586 void onCommaSeparated (const DagInit* d) {
587 checkNumberOfArguments(d, 0);
588 if (!optDesc_.isList())
589 throw "'comma_separated' is valid only on list options!";
590 optDesc_.setCommaSeparated();
591 }
592
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000593 void onRequired (const DagInit* d) {
594 checkNumberOfArguments(d, 0);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000595 if (optDesc_.isOneOrMore() || optDesc_.isOptional())
596 throw "Only one of (required), (optional) or "
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000597 "(one_or_more) properties is allowed!";
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000598 optDesc_.setRequired();
599 }
600
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000601 void onInit (const DagInit* d) {
602 checkNumberOfArguments(d, 1);
603 Init* i = d->getArg(0);
604 const std::string& str = i->getAsString();
605
606 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
607 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
608
609 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000610 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000611
612 optDesc_.InitVal = i;
613 }
614
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000615 void onOneOrMore (const DagInit* d) {
616 checkNumberOfArguments(d, 0);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000617 if (optDesc_.isRequired() || optDesc_.isOptional())
618 throw "Only one of (required), (optional) or "
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000619 "(one_or_more) properties is allowed!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000620 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000621 llvm::errs() << "Warning: specifying the 'one_or_more' property "
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000622 "on a non-list option will have no effect.\n";
623 optDesc_.setOneOrMore();
624 }
625
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000626 void onOptional (const DagInit* d) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000627 checkNumberOfArguments(d, 0);
628 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000629 throw "Only one of (required), (optional) or "
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000630 "(one_or_more) properties is allowed!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000631 if (!OptionType::IsList(optDesc_.Type))
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000632 llvm::errs() << "Warning: specifying the 'optional' property"
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000633 "on a non-list option will have no effect.\n";
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000634 optDesc_.setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000635 }
636
637 void onMultiVal (const DagInit* d) {
638 checkNumberOfArguments(d, 1);
639 int val = InitPtrToInt(d->getArg(0));
640 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000641 throw "Error in the 'multi_val' property: "
642 "the value must be greater than 1!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000643 if (!OptionType::IsList(optDesc_.Type))
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000644 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000645 optDesc_.MultiVal = val;
646 }
647
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000648};
649
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000650/// AddOption - A function object that is applied to every option
651/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000652class AddOption {
653private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000654 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000655
656public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000657 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000658 {}
659
660 void operator()(const Init* i) {
661 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000662 checkNumberOfArguments(&d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000663
664 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000665 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000666 const std::string& Name = InitPtrToString(d.getArg(0));
667
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000668 OptionDescription OD(Type, Name);
669
670 if (!OD.isExtern())
671 checkNumberOfArguments(&d, 2);
672
673 if (OD.isAlias()) {
674 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000675 OD.Help = InitPtrToString(d.getArg(1));
676 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000677 else if (!OD.isExtern()) {
678 processOptionProperties(&d, OD);
679 }
680 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000681 }
682
683private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000684 /// processOptionProperties - Go through the list of option
685 /// properties and call a corresponding handler for each.
686 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
687 checkNumberOfArguments(d, 2);
688 DagInit::const_arg_iterator B = d->arg_begin();
689 // Skip the first argument: it's always the option name.
690 ++B;
691 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000692 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000693
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000694};
695
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000696/// CollectOptionDescriptions - Collects option properties from all
697/// OptionLists.
698void CollectOptionDescriptions (RecordVector::const_iterator B,
699 RecordVector::const_iterator E,
700 OptionDescriptions& OptDescs)
701{
702 // For every OptionList:
703 for (; B!=E; ++B) {
704 RecordVector::value_type T = *B;
705 // Throws an exception if the value does not exist.
706 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000707
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000708 // For every option description in this list:
709 // collect the information and
710 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
711 }
712}
713
714// Tool information record
715
716namespace ToolFlags {
717 enum ToolFlags { Join = 0x1, Sink = 0x2 };
718}
719
720struct ToolDescription : public RefCountedBase<ToolDescription> {
721 std::string Name;
722 Init* CmdLine;
723 Init* Actions;
724 StrVector InLanguage;
725 std::string OutLanguage;
726 std::string OutputSuffix;
727 unsigned Flags;
728
729 // Various boolean properties
730 void setSink() { Flags |= ToolFlags::Sink; }
731 bool isSink() const { return Flags & ToolFlags::Sink; }
732 void setJoin() { Flags |= ToolFlags::Join; }
733 bool isJoin() const { return Flags & ToolFlags::Join; }
734
735 // Default ctor here is needed because StringMap can only store
736 // DefaultConstructible objects
737 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
738 ToolDescription (const std::string& n)
739 : Name(n), CmdLine(0), Actions(0), Flags(0)
740 {}
741};
742
743/// ToolDescriptions - A list of Tool information records.
744typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
745
746
747/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000748/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000749
750class CollectToolProperties;
751typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
752(const DagInit*);
753
754class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
755{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000756private:
757
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000758 /// toolDesc_ - Properties of the current Tool. This is where the
759 /// information is stored.
760 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000761
762public:
763
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000764 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000765 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000766 {
767 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000768
769 AddHandler("actions", &CollectToolProperties::onActions);
770 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
771 AddHandler("in_language", &CollectToolProperties::onInLanguage);
772 AddHandler("join", &CollectToolProperties::onJoin);
773 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
774 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
775 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000776
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000777 staticMembersInitialized_ = true;
778 }
779 }
780
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000781 void operator() (Init* i) {
782 InvokeDagInitHandler(this, i);
783 }
784
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000785private:
786
787 /// Property handlers --
788 /// Functions that extract information about tool properties from
789 /// DAG representation.
790
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000791 void onActions (const DagInit* d) {
792 checkNumberOfArguments(d, 1);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000793 Init* Case = d->getArg(0);
794 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000795 GetOperatorName(static_cast<DagInit*>(Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000796 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000797 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000798 }
799
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000800 void onCmdLine (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000801 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000802 toolDesc_.CmdLine = d->getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000803 }
804
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000805 void onInLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000806 checkNumberOfArguments(d, 1);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000807 Init* arg = d->getArg(0);
808
809 // Find out the argument's type.
810 if (typeid(*arg) == typeid(StringInit)) {
811 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000812 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000813 }
814 else {
815 // It's a list.
816 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000817 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000818
819 // Copy strings to the output vector.
820 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
821 B != E; ++B) {
822 out.push_back(InitPtrToString(*B));
823 }
824
825 // Remove duplicates.
826 std::sort(out.begin(), out.end());
827 StrVector::iterator newE = std::unique(out.begin(), out.end());
828 out.erase(newE, out.end());
829 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000830 }
831
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000832 void onJoin (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000833 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000834 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000835 }
836
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000837 void onOutLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000838 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000839 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000840 }
841
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000842 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000843 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000844 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000845 }
846
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000847 void onSink (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000848 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000849 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000850 }
851
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000852};
853
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000854/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000855/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000856/// CollectToolProperties function object).
857void CollectToolDescriptions (RecordVector::const_iterator B,
858 RecordVector::const_iterator E,
859 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000860{
861 // Iterate over a properties list of every Tool definition
862 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000863 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000864 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000865 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000866
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000867 IntrusiveRefCntPtr<ToolDescription>
868 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000869
870 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000871 CollectToolProperties(*ToolDesc));
872 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000873 }
874}
875
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000876/// FillInEdgeVector - Merge all compilation graph definitions into
877/// one single edge list.
878void FillInEdgeVector(RecordVector::const_iterator B,
879 RecordVector::const_iterator E, RecordVector& Out) {
880 for (; B != E; ++B) {
881 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000882
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000883 for (unsigned i = 0; i < edges->size(); ++i)
884 Out.push_back(edges->getElementAsRecord(i));
885 }
886}
887
888/// CalculatePriority - Calculate the priority of this plugin.
889int CalculatePriority(RecordVector::const_iterator B,
890 RecordVector::const_iterator E) {
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000891 int priority = 0;
892
893 if (B != E) {
894 priority = static_cast<int>((*B)->getValueAsInt("priority"));
895
896 if (++B != E)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000897 throw "More than one 'PluginPriority' instance found: "
898 "most probably an error!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000899 }
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000900
901 return priority;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000902}
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000903
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000904/// NotInGraph - Helper function object for FilterNotInGraph.
905struct NotInGraph {
906private:
907 const llvm::StringSet<>& ToolsInGraph_;
908
909public:
910 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
911 : ToolsInGraph_(ToolsInGraph)
912 {}
913
914 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
915 return (ToolsInGraph_.count(x->Name) == 0);
916 }
917};
918
919/// FilterNotInGraph - Filter out from ToolDescs all Tools not
920/// mentioned in the compilation graph definition.
921void FilterNotInGraph (const RecordVector& EdgeVector,
922 ToolDescriptions& ToolDescs) {
923
924 // List all tools mentioned in the graph.
925 llvm::StringSet<> ToolsInGraph;
926
927 for (RecordVector::const_iterator B = EdgeVector.begin(),
928 E = EdgeVector.end(); B != E; ++B) {
929
930 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000931 const std::string& NodeA = Edge->getValueAsString("a");
932 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000933
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000934 if (NodeA != "root")
935 ToolsInGraph.insert(NodeA);
936 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000937 }
938
939 // Filter ToolPropertiesList.
940 ToolDescriptions::iterator new_end =
941 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
942 NotInGraph(ToolsInGraph));
943 ToolDescs.erase(new_end, ToolDescs.end());
944}
945
946/// FillInToolToLang - Fills in two tables that map tool names to
947/// (input, output) languages. Helper function used by TypecheckGraph().
948void FillInToolToLang (const ToolDescriptions& ToolDescs,
949 StringMap<StringSet<> >& ToolToInLang,
950 StringMap<std::string>& ToolToOutLang) {
951 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
952 E = ToolDescs.end(); B != E; ++B) {
953 const ToolDescription& D = *(*B);
954 for (StrVector::const_iterator B = D.InLanguage.begin(),
955 E = D.InLanguage.end(); B != E; ++B)
956 ToolToInLang[D.Name].insert(*B);
957 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000958 }
959}
960
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000961/// TypecheckGraph - Check that names for output and input languages
962/// on all edges do match. This doesn't do much when the information
963/// about the whole graph is not available (i.e. when compiling most
964/// plugins).
965void TypecheckGraph (const RecordVector& EdgeVector,
966 const ToolDescriptions& ToolDescs) {
967 StringMap<StringSet<> > ToolToInLang;
968 StringMap<std::string> ToolToOutLang;
969
970 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
971 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
972 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
973
974 for (RecordVector::const_iterator B = EdgeVector.begin(),
975 E = EdgeVector.end(); B != E; ++B) {
976 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000977 const std::string& NodeA = Edge->getValueAsString("a");
978 const std::string& NodeB = Edge->getValueAsString("b");
979 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
980 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000981
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000982 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000983 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000984 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000985 + ": output->input language mismatch";
986 }
987
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000988 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000989 throw "Edges back to the root are not allowed!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000990 }
991}
992
993/// WalkCase - Walks the 'case' expression DAG and invokes
994/// TestCallback on every test, and StatementCallback on every
995/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000996/// combinators (that is, they are passed directly to TestCallback).
997/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
998/// IndentLevel, bool FirstTest)'.
999/// StatementCallback must have type 'void StatementCallback(const Init*,
1000/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001001template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001002void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1003 unsigned IndentLevel = 0)
1004{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001005 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001006
1007 // Error checks.
1008 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001009 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001010
1011 if (d.getNumArgs() < 2)
1012 throw "There should be at least one clause in the 'case' expression:\n"
1013 + d.getAsString();
1014
1015 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001016 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001017 const unsigned numArgs = d.getNumArgs();
1018 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001019 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1020 B != E; ++B) {
1021 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001022
1023 if (!even)
1024 {
1025 // Handle test.
1026 const DagInit& Test = InitPtrToDag(arg);
1027
1028 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001029 throw "The 'default' clause should be the last in the "
1030 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001031 if (i == numArgs)
1032 throw "Case construct handler: no corresponding action "
1033 "found for the test " + Test.getAsString() + '!';
1034
1035 TestCallback(&Test, IndentLevel, (i == 1));
1036 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001037 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001038 {
1039 if (dynamic_cast<DagInit*>(arg)
1040 && GetOperatorName(static_cast<DagInit*>(arg)) == "case") {
1041 // Nested 'case'.
1042 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1043 }
1044
1045 // Handle statement.
1046 StatementCallback(arg, IndentLevel);
1047 }
1048
1049 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001050 even = !even;
1051 }
1052}
1053
1054/// ExtractOptionNames - A helper function object used by
1055/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1056class ExtractOptionNames {
1057 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001058
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001059 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001060 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001061 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001062 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001063 ActionName == "forward_value" ||
1064 ActionName == "forward_transformed_value" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001065 ActionName == "switch_on" || ActionName == "parameter_equals" ||
1066 ActionName == "element_in_list" || ActionName == "not_empty" ||
1067 ActionName == "empty") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001068 checkNumberOfArguments(&Stmt, 1);
1069 const std::string& Name = InitPtrToString(Stmt.getArg(0));
1070 OptionNames_.insert(Name);
1071 }
1072 else if (ActionName == "and" || ActionName == "or") {
1073 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001074 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001075 }
1076 }
1077 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001078
1079public:
1080 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1081 {}
1082
1083 void operator()(const Init* Statement) {
1084 if (typeid(*Statement) == typeid(ListInit)) {
1085 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1086 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1087 B != E; ++B)
1088 this->processDag(*B);
1089 }
1090 else {
1091 this->processDag(Statement);
1092 }
1093 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001094
1095 void operator()(const DagInit* Test, unsigned, bool) {
1096 this->operator()(Test);
1097 }
1098 void operator()(const Init* Statement, unsigned) {
1099 this->operator()(Statement);
1100 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001101};
1102
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001103/// CheckForSuperfluousOptions - Check that there are no side
1104/// effect-free options (specified only in the OptionList). Otherwise,
1105/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001106void CheckForSuperfluousOptions (const RecordVector& Edges,
1107 const ToolDescriptions& ToolDescs,
1108 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001109 llvm::StringSet<> nonSuperfluousOptions;
1110
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001111 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001112 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001113 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1114 E = ToolDescs.end(); B != E; ++B) {
1115 const ToolDescription& TD = *(*B);
1116 ExtractOptionNames Callback(nonSuperfluousOptions);
1117 if (TD.Actions)
1118 WalkCase(TD.Actions, Callback, Callback);
1119 }
1120
1121 // Add all options mentioned in the 'case' clauses of the
1122 // OptionalEdges of the compilation graph to the set of
1123 // non-superfluous options.
1124 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1125 B != E; ++B) {
1126 const Record* Edge = *B;
1127 DagInit* Weight = Edge->getValueAsDag("weight");
1128
1129 if (!isDagEmpty(Weight))
1130 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001131 }
1132
1133 // Check that all options in OptDescs belong to the set of
1134 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001135 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001136 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001137 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001138 if (!nonSuperfluousOptions.count(Val.Name)
1139 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001140 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001141 "Probable cause: this option is specified only in the OptionList.\n";
1142 }
1143}
1144
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001145/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1146bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1147 if (TestName == "single_input_file") {
1148 O << "InputFilenames.size() == 1";
1149 return true;
1150 }
1151 else if (TestName == "multiple_input_files") {
1152 O << "InputFilenames.size() > 1";
1153 return true;
1154 }
1155
1156 return false;
1157}
1158
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001159/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1160template <typename F>
1161void EmitListTest(const ListInit& L, const char* LogicOp,
1162 F Callback, raw_ostream& O)
1163{
1164 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1165 // of Dags...
1166 bool isFirst = true;
1167 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1168 if (isFirst)
1169 isFirst = false;
1170 else
1171 O << " || ";
1172 Callback(InitPtrToString(*B), O);
1173 }
1174}
1175
1176// Callbacks for use with EmitListTest.
1177
1178class EmitSwitchOn {
1179 const OptionDescriptions& OptDescs_;
1180public:
1181 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1182 {}
1183
1184 void operator()(const std::string& OptName, raw_ostream& O) const {
1185 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1186 O << OptDesc.GenVariableName();
1187 }
1188};
1189
1190class EmitEmptyTest {
1191 bool EmitNegate_;
1192 const OptionDescriptions& OptDescs_;
1193public:
1194 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1195 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1196 {}
1197
1198 void operator()(const std::string& OptName, raw_ostream& O) const {
1199 const char* Neg = (EmitNegate_ ? "!" : "");
1200 if (OptName == "o") {
1201 O << Neg << "OutputFilename.empty()";
1202 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001203 else if (OptName == "save-temps") {
1204 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1205 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001206 else {
1207 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1208 O << Neg << OptDesc.GenVariableName() << ".empty()";
1209 }
1210 }
1211};
1212
1213
1214/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1215bool EmitCaseTest1ArgList(const std::string& TestName,
1216 const DagInit& d,
1217 const OptionDescriptions& OptDescs,
1218 raw_ostream& O) {
1219 const ListInit& L = *static_cast<ListInit*>(d.getArg(0));
1220
1221 if (TestName == "any_switch_on") {
1222 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1223 return true;
1224 }
1225 else if (TestName == "switch_on") {
1226 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1227 return true;
1228 }
1229 else if (TestName == "any_not_empty") {
1230 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1231 return true;
1232 }
1233 else if (TestName == "any_empty") {
1234 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1235 return true;
1236 }
1237 else if (TestName == "not_empty") {
1238 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1239 return true;
1240 }
1241 else if (TestName == "empty") {
1242 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1243 return true;
1244 }
1245
1246 return false;
1247}
1248
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001249/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1250bool EmitCaseTest1ArgStr(const std::string& TestName,
1251 const DagInit& d,
1252 const OptionDescriptions& OptDescs,
1253 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001254 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001255
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001256 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001257 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001258 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001259 }
1260 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001261 O << "InLangs.count(\"" << OptName << "\") != 0";
1262 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001263 }
1264 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001265 // This works only for single-argument Tool::GenerateAction. Join
1266 // tools can process several files in different languages simultaneously.
1267
1268 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001269 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001270 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001271 }
1272 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001273 bool EmitNegate = (TestName == "not_empty");
1274 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001275 return true;
1276 }
1277
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001278 return false;
1279}
1280
1281/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1282bool EmitCaseTest1Arg(const std::string& TestName,
1283 const DagInit& d,
1284 const OptionDescriptions& OptDescs,
1285 raw_ostream& O) {
1286 checkNumberOfArguments(&d, 1);
1287 if (typeid(*d.getArg(0)) == typeid(ListInit))
1288 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1289 else
1290 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1291}
1292
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001293/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001294bool EmitCaseTest2Args(const std::string& TestName,
1295 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001296 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001297 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001298 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001299 checkNumberOfArguments(&d, 2);
1300 const std::string& OptName = InitPtrToString(d.getArg(0));
1301 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001302
1303 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001304 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001305 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1306 return true;
1307 }
1308 else if (TestName == "element_in_list") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001309 const OptionDescription& OptDesc = OptDescs.FindList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001310 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001311 O << "std::find(" << VarName << ".begin(),\n";
1312 O.indent(IndentLevel + Indent1)
1313 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001314 << OptArg << "\") != " << VarName << ".end()";
1315 return true;
1316 }
1317
1318 return false;
1319}
1320
1321// Forward declaration.
1322// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001323void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001324 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001325 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001326
1327/// EmitLogicalOperationTest - Helper function used by
1328/// EmitCaseConstructHandler.
1329void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001330 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001331 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001332 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001333 O << '(';
1334 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001335 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001336 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001337 if (j != NumArgs - 1) {
1338 O << ")\n";
1339 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1340 }
1341 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001342 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001343 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001344 }
1345}
1346
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001347void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001348 const OptionDescriptions& OptDescs, raw_ostream& O)
1349{
1350 checkNumberOfArguments(&d, 1);
1351 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1352 O << "! (";
1353 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1354 O << ")";
1355}
1356
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001357/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001358void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001359 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001360 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001361 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001362
1363 if (TestName == "and")
1364 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1365 else if (TestName == "or")
1366 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001367 else if (TestName == "not")
1368 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001369 else if (EmitCaseTest0Args(TestName, O))
1370 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001371 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1372 return;
1373 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1374 return;
1375 else
1376 throw TestName + ": unknown edge property!";
1377}
1378
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001379
1380/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1381class EmitCaseTestCallback {
1382 bool EmitElseIf_;
1383 const OptionDescriptions& OptDescs_;
1384 raw_ostream& O_;
1385public:
1386
1387 EmitCaseTestCallback(bool EmitElseIf,
1388 const OptionDescriptions& OptDescs, raw_ostream& O)
1389 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1390 {}
1391
1392 void operator()(const DagInit* Test, unsigned IndentLevel, bool FirstTest)
1393 {
1394 if (GetOperatorName(Test) == "default") {
1395 O_.indent(IndentLevel) << "else {\n";
1396 }
1397 else {
1398 O_.indent(IndentLevel)
1399 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
1400 EmitCaseTest(*Test, IndentLevel, OptDescs_, O_);
1401 O_ << ") {\n";
1402 }
1403 }
1404};
1405
1406/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1407template <typename F>
1408class EmitCaseStatementCallback {
1409 F Callback_;
1410 raw_ostream& O_;
1411public:
1412
1413 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1414 : Callback_(Callback), O_(O)
1415 {}
1416
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001417 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001418
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001419 // Ignore nested 'case' DAG.
1420 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001421 GetOperatorName(static_cast<const DagInit*>(Statement)) == "case")) {
1422 if (typeid(*Statement) == typeid(ListInit)) {
1423 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1424 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1425 B != E; ++B)
1426 Callback_(*B, (IndentLevel + Indent1), O_);
1427 }
1428 else {
1429 Callback_(Statement, (IndentLevel + Indent1), O_);
1430 }
1431 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001432 O_.indent(IndentLevel) << "}\n";
1433 }
1434
1435};
1436
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001437/// EmitCaseConstructHandler - Emit code that handles the 'case'
1438/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001439/// clause. Implemented on top of WalkCase.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001440/// Callback's type is void F(Init* Statement, unsigned IndentLevel,
1441/// raw_ostream& O).
1442/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001443/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1444/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001445template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001446void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001447 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001448 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001449 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001450 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1451 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001452}
1453
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001454/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001455/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1456/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001457void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001458 const char* Delimiters = " \t\n\v\f\r";
1459 enum TokenizerState
1460 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1461 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001462
1463 if (CmdLine.empty())
1464 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001465 Out.push_back("");
1466
1467 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1468 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001469
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001470 for (; B != E; ++B) {
1471 char cur_ch = CmdLine[B];
1472
1473 switch (cur_st) {
1474 case Normal:
1475 if (cur_ch == '$') {
1476 cur_st = SpecialCommand;
1477 break;
1478 }
1479 if (oneOf(Delimiters, cur_ch)) {
1480 // Skip whitespace
1481 B = CmdLine.find_first_not_of(Delimiters, B);
1482 if (B == std::string::npos) {
1483 B = E-1;
1484 continue;
1485 }
1486 --B;
1487 Out.push_back("");
1488 continue;
1489 }
1490 break;
1491
1492
1493 case SpecialCommand:
1494 if (oneOf(Delimiters, cur_ch)) {
1495 cur_st = Normal;
1496 Out.push_back("");
1497 continue;
1498 }
1499 if (cur_ch == '(') {
1500 Out.push_back("");
1501 cur_st = InsideSpecialCommand;
1502 continue;
1503 }
1504 break;
1505
1506 case InsideSpecialCommand:
1507 if (oneOf(Delimiters, cur_ch)) {
1508 continue;
1509 }
1510 if (cur_ch == '\'') {
1511 cur_st = InsideQuotationMarks;
1512 Out.push_back("");
1513 continue;
1514 }
1515 if (cur_ch == ')') {
1516 cur_st = Normal;
1517 Out.push_back("");
1518 }
1519 if (cur_ch == ',') {
1520 continue;
1521 }
1522
1523 break;
1524
1525 case InsideQuotationMarks:
1526 if (cur_ch == '\'') {
1527 cur_st = InsideSpecialCommand;
1528 continue;
1529 }
1530 break;
1531 }
1532
1533 Out.back().push_back(cur_ch);
1534 }
1535}
1536
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001537/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1538/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1539/// SubstituteSpecialCommands().
1540StrVector::const_iterator
1541SubstituteCall (StrVector::const_iterator Pos,
1542 StrVector::const_iterator End,
1543 bool IsJoin, raw_ostream& O)
1544{
1545 const char* errorMessage = "Syntax error in $CALL invocation!";
1546 checkedIncrement(Pos, End, errorMessage);
1547 const std::string& CmdName = *Pos;
1548
1549 if (CmdName == ")")
1550 throw "$CALL invocation: empty argument list!";
1551
1552 O << "hooks::";
1553 O << CmdName << "(";
1554
1555
1556 bool firstIteration = true;
1557 while (true) {
1558 checkedIncrement(Pos, End, errorMessage);
1559 const std::string& Arg = *Pos;
1560 assert(Arg.size() != 0);
1561
1562 if (Arg[0] == ')')
1563 break;
1564
1565 if (firstIteration)
1566 firstIteration = false;
1567 else
1568 O << ", ";
1569
1570 if (Arg == "$INFILE") {
1571 if (IsJoin)
1572 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1573 else
1574 O << "inFile.c_str()";
1575 }
1576 else {
1577 O << '"' << Arg << '"';
1578 }
1579 }
1580
1581 O << ')';
1582
1583 return Pos;
1584}
1585
1586/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1587/// function used by SubstituteSpecialCommands().
1588StrVector::const_iterator
1589SubstituteEnv (StrVector::const_iterator Pos,
1590 StrVector::const_iterator End, raw_ostream& O)
1591{
1592 const char* errorMessage = "Syntax error in $ENV invocation!";
1593 checkedIncrement(Pos, End, errorMessage);
1594 const std::string& EnvName = *Pos;
1595
1596 if (EnvName == ")")
1597 throw "$ENV invocation: empty argument list!";
1598
1599 O << "checkCString(std::getenv(\"";
1600 O << EnvName;
1601 O << "\"))";
1602
1603 checkedIncrement(Pos, End, errorMessage);
1604
1605 return Pos;
1606}
1607
1608/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1609/// handler code. Helper function used by EmitCmdLineVecFill().
1610StrVector::const_iterator
1611SubstituteSpecialCommands (StrVector::const_iterator Pos,
1612 StrVector::const_iterator End,
1613 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001614{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001615
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001616 const std::string& cmd = *Pos;
1617
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001618 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001619 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001620 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001621 }
1622 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001623 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001624 }
1625 else {
1626 throw "Unknown special command: " + cmd;
1627 }
1628
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001629 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001630 const std::string& Leftover = *Pos;
1631 assert(Leftover.at(0) == ')');
1632 if (Leftover.size() != 1)
1633 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001634
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001635 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001636}
1637
1638/// EmitCmdLineVecFill - Emit code that fills in the command line
1639/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001640void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001641 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001642 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001643 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001644 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001645
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001646 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001647 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001648
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001649 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1650
1651 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001652 assert(!StrVec[0].empty());
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001653 if (StrVec[0][0] == '$') {
1654 while (I != E && (*I)[0] != ')' )
1655 ++I;
1656
1657 // Skip the ')' symbol.
1658 ++I;
1659 }
1660 else {
1661 ++I;
1662 }
1663
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001664 bool hasINFILE = false;
1665
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001666 for (; I != E; ++I) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001667 const std::string& cmd = *I;
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001668 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001669 O.indent(IndentLevel);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001670 if (cmd.at(0) == '$') {
1671 if (cmd == "$INFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001672 hasINFILE = true;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001673 if (IsJoin) {
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001674 O << "for (PathVector::const_iterator B = inFiles.begin()"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001675 << ", E = inFiles.end();\n";
1676 O.indent(IndentLevel) << "B != E; ++B)\n";
1677 O.indent(IndentLevel + Indent1) << "vec.push_back(B->str());\n";
1678 }
1679 else {
Chris Lattner74382b72009-08-23 22:45:37 +00001680 O << "vec.push_back(inFile.str());\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001681 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001682 }
1683 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001684 O << "vec.push_back(\"\");\n";
1685 O.indent(IndentLevel) << "out_file_index = vec.size()-1;\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001686 }
1687 else {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001688 O << "vec.push_back(";
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001689 I = SubstituteSpecialCommands(I, E, IsJoin, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001690 O << ");\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001691 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001692 }
1693 else {
1694 O << "vec.push_back(\"" << cmd << "\");\n";
1695 }
1696 }
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001697 if (!hasINFILE)
1698 throw "Tool '" + ToolName + "' doesn't take any input!";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001699
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001700 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001701 if (StrVec[0][0] == '$')
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001702 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001703 else
1704 O << '"' << StrVec[0] << '"';
1705 O << ";\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001706}
1707
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001708/// EmitCmdLineVecFillCallback - A function object wrapper around
1709/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1710/// argument to EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001711class EmitCmdLineVecFillCallback {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001712 bool IsJoin;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001713 const std::string& ToolName;
1714 public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001715 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1716 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001717
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001718 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001719 raw_ostream& O) const
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001720 {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001721 EmitCmdLineVecFill(Statement, ToolName, IsJoin, IndentLevel, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001722 }
1723};
1724
1725/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1726/// implement EmitActionHandler. Emits code for
1727/// handling the (forward) and (forward_as) option properties.
1728void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001729 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001730 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001731 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001732 const std::string& Name = NewName.empty()
1733 ? ("-" + D.Name)
1734 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001735 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001736
1737 switch (D.Type) {
1738 case OptionType::Switch:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001739 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001740 break;
1741 case OptionType::Parameter:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001742 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
1743 O.indent(IndentLevel) << "vec.push_back(" << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001744 break;
1745 case OptionType::Prefix:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001746 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\" + "
1747 << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001748 break;
1749 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001750 O.indent(IndentLevel)
1751 << "for (" << D.GenTypeDeclaration()
1752 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1753 O.indent(IndentLevel)
1754 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1755 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\" + " << "*B);\n";
1756 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001757
1758 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001759 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1760 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001761 }
1762
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001763 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001764 break;
1765 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001766 O.indent(IndentLevel)
1767 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1768 << D.GenVariableName() << ".begin(),\n";
1769 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1770 << ".end() ; B != E;) {\n";
1771 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001772
1773 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001774 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1775 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001776 }
1777
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001778 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001779 break;
1780 case OptionType::Alias:
1781 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001782 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001783 }
1784}
1785
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001786/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1787/// EmitPreprocessOptionsCallback.
1788struct ActionHandlingCallbackBase {
1789
1790 void onErrorDag(const DagInit& d,
1791 unsigned IndentLevel, raw_ostream& O) const
1792 {
1793 O.indent(IndentLevel)
1794 << "throw std::runtime_error(\"" <<
1795 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1796 : "Unknown error!")
1797 << "\");\n";
1798 }
1799
1800 void onWarningDag(const DagInit& d,
1801 unsigned IndentLevel, raw_ostream& O) const
1802 {
1803 checkNumberOfArguments(&d, 1);
1804 O.indent(IndentLevel) << "llvm::errs() << \""
1805 << InitPtrToString(d.getArg(0)) << "\";\n";
1806 }
1807
1808};
1809
1810/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1811/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001812class EmitActionHandlersCallback;
1813typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1814(const DagInit&, unsigned, raw_ostream&) const;
1815
1816class EmitActionHandlersCallback
1817: public ActionHandlingCallbackBase,
1818 public HandlerTable<EmitActionHandlersCallbackHandler>
1819{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001820 const OptionDescriptions& OptDescs;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001821 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001822
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001823 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1824 /// onAppendCmd and onOutputSuffix.
1825 void EmitHookInvocation(const std::string& Str,
1826 const char* BlockOpen, const char* BlockClose,
1827 unsigned IndentLevel, raw_ostream& O) const
1828 {
1829 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001830 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001831
1832 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1833 B != E; ++B) {
1834 const std::string& cmd = *B;
1835
1836 O.indent(IndentLevel) << BlockOpen;
1837
1838 if (cmd.at(0) == '$')
1839 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
1840 else
1841 O << '"' << cmd << '"';
1842
1843 O << BlockClose;
1844 }
1845 }
1846
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001847 void onAppendCmd (const DagInit& Dag,
1848 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001849 {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001850 checkNumberOfArguments(&Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001851 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
1852 "vec.push_back(", ");\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001853 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001854
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001855 void onForward (const DagInit& Dag,
1856 unsigned IndentLevel, raw_ostream& O) const
1857 {
1858 checkNumberOfArguments(&Dag, 1);
1859 const std::string& Name = InitPtrToString(Dag.getArg(0));
1860 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1861 IndentLevel, "", O);
1862 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001863
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001864 void onForwardAs (const DagInit& Dag,
1865 unsigned IndentLevel, raw_ostream& O) const
1866 {
1867 checkNumberOfArguments(&Dag, 2);
1868 const std::string& Name = InitPtrToString(Dag.getArg(0));
1869 const std::string& NewName = InitPtrToString(Dag.getArg(1));
1870 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1871 IndentLevel, NewName, O);
1872 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001873
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001874 void onForwardValue (const DagInit& Dag,
1875 unsigned IndentLevel, raw_ostream& O) const
1876 {
1877 checkNumberOfArguments(&Dag, 1);
1878 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001879 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001880
1881 if (D.isParameter()) {
1882 O.indent(IndentLevel) << "vec.push_back("
1883 << D.GenVariableName() << ");\n";
1884 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001885 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001886 O.indent(IndentLevel) << "std::copy(" << D.GenVariableName()
1887 << ".begin(), " << D.GenVariableName()
1888 << ".end(), std::back_inserter(vec));\n";
1889 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001890 }
1891
1892 void onForwardTransformedValue (const DagInit& Dag,
1893 unsigned IndentLevel, raw_ostream& O) const
1894 {
1895 checkNumberOfArguments(&Dag, 2);
1896 const std::string& Name = InitPtrToString(Dag.getArg(0));
1897 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001898 const OptionDescription& D = OptDescs.FindListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001899
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00001900 O.indent(IndentLevel) << "vec.push_back(" << "hooks::"
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00001901 << Hook << "(" << D.GenVariableName()
1902 << (D.isParameter() ? ".c_str()" : "") << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001903 }
1904
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001905 void onOutputSuffix (const DagInit& Dag,
1906 unsigned IndentLevel, raw_ostream& O) const
1907 {
1908 checkNumberOfArguments(&Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001909 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
1910 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001911 }
1912
1913 void onStopCompilation (const DagInit& Dag,
1914 unsigned IndentLevel, raw_ostream& O) const
1915 {
1916 O.indent(IndentLevel) << "stop_compilation = true;\n";
1917 }
1918
1919
1920 void onUnpackValues (const DagInit& Dag,
1921 unsigned IndentLevel, raw_ostream& O) const
1922 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001923 throw "'unpack_values' is deprecated. "
1924 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001925 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001926
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001927 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001928
1929 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
1930 : OptDescs(OD)
1931 {
1932 if (!staticMembersInitialized_) {
1933 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
1934 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
1935 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
1936 AddHandler("forward", &EmitActionHandlersCallback::onForward);
1937 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001938 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
1939 AddHandler("forward_transformed_value",
1940 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001941 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
1942 AddHandler("stop_compilation",
1943 &EmitActionHandlersCallback::onStopCompilation);
1944 AddHandler("unpack_values",
1945 &EmitActionHandlersCallback::onUnpackValues);
1946
1947 staticMembersInitialized_ = true;
1948 }
1949 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001950
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001951 void operator()(const Init* Statement,
1952 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001953 {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001954 const DagInit& Dag = InitPtrToDag(Statement);
1955 const std::string& ActionName = GetOperatorName(Dag);
1956 Handler h = GetHandler(ActionName);
1957
1958 ((this)->*(h))(Dag, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001959 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001960};
1961
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001962bool IsOutFileIndexCheckRequiredStr (const Init* CmdLine) {
1963 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001964 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001965
1966 for (StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1967 I != E; ++I) {
1968 if (*I == "$OUTFILE")
1969 return false;
1970 }
1971
1972 return true;
1973}
1974
1975class IsOutFileIndexCheckRequiredStrCallback {
1976 bool* ret_;
1977
1978public:
1979 IsOutFileIndexCheckRequiredStrCallback(bool* ret) : ret_(ret)
1980 {}
1981
1982 void operator()(const Init* CmdLine) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001983 // Ignore nested 'case' DAG.
1984 if (typeid(*CmdLine) == typeid(DagInit))
1985 return;
1986
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001987 if (IsOutFileIndexCheckRequiredStr(CmdLine))
1988 *ret_ = true;
1989 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001990
1991 void operator()(const DagInit* Test, unsigned, bool) {
1992 this->operator()(Test);
1993 }
1994 void operator()(const Init* Statement, unsigned) {
1995 this->operator()(Statement);
1996 }
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001997};
1998
1999bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
2000 bool ret = false;
2001 WalkCase(CmdLine, Id(), IsOutFileIndexCheckRequiredStrCallback(&ret));
2002 return ret;
2003}
2004
2005/// IsOutFileIndexCheckRequired - Should we emit an "out_file_index != -1" check
2006/// in EmitGenerateActionMethod() ?
2007bool IsOutFileIndexCheckRequired (Init* CmdLine) {
2008 if (typeid(*CmdLine) == typeid(StringInit))
2009 return IsOutFileIndexCheckRequiredStr(CmdLine);
2010 else
2011 return IsOutFileIndexCheckRequiredCase(CmdLine);
2012}
2013
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002014void EmitGenerateActionMethodHeader(const ToolDescription& D,
2015 bool IsJoin, raw_ostream& O)
2016{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002017 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002018 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002019 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002020 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002021
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002022 O.indent(Indent2) << "bool HasChildren,\n";
2023 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2024 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2025 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2026 O.indent(Indent1) << "{\n";
2027 O.indent(Indent2) << "std::string cmd;\n";
2028 O.indent(Indent2) << "std::vector<std::string> vec;\n";
2029 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2030 O.indent(Indent2) << "const char* output_suffix = \""
2031 << D.OutputSuffix << "\";\n";
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002032}
2033
2034// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2035// Tool::GenerateAction() method.
2036void EmitGenerateActionMethod (const ToolDescription& D,
2037 const OptionDescriptions& OptDescs,
2038 bool IsJoin, raw_ostream& O) {
2039
2040 EmitGenerateActionMethodHeader(D, IsJoin, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002041
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002042 if (!D.CmdLine)
2043 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002044
2045 bool IndexCheckRequired = IsOutFileIndexCheckRequired(D.CmdLine);
2046 O.indent(Indent2) << "int out_file_index"
2047 << (IndexCheckRequired ? " = -1" : "")
2048 << ";\n\n";
2049
2050 // Process the cmd_line property.
2051 if (typeid(*D.CmdLine) == typeid(StringInit))
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002052 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2053 else
2054 EmitCaseConstructHandler(D.CmdLine, Indent2,
2055 EmitCmdLineVecFillCallback(IsJoin, D.Name),
2056 true, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002057
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002058 // For every understood option, emit handling code.
2059 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002060 EmitCaseConstructHandler(D.Actions, Indent2,
2061 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002062 false, OptDescs, O);
2063
2064 O << '\n';
2065 O.indent(Indent2)
2066 << "std::string out_file = OutFilename("
2067 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
2068 O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002069
2070 if (IndexCheckRequired)
2071 O.indent(Indent2) << "if (out_file_index != -1)\n";
2072 O.indent(IndexCheckRequired ? Indent3 : Indent2)
2073 << "vec[out_file_index] = out_file;\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002074
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002075 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002076 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002077 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
2078 O.indent(Indent3) << "vec.insert(vec.end(), "
2079 << SinkOptionName << ".begin(), " << SinkOptionName
2080 << ".end());\n";
2081 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002082 }
2083
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002084 O.indent(Indent2) << "return Action(cmd, vec, stop_compilation, out_file);\n";
2085 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002086}
2087
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002088/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2089/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002090void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2091 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002092 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002093 if (!ToolDesc.isJoin()) {
2094 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
2095 O.indent(Indent2) << "bool HasChildren,\n";
2096 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2097 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2098 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2099 O.indent(Indent1) << "{\n";
2100 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
2101 << " is not a Join tool!\");\n";
2102 O.indent(Indent1) << "}\n\n";
2103 }
2104 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002105 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002106 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002107
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002108 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002109}
2110
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002111/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2112/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002113void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002114 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2115 O.indent(Indent2) << "return InputLanguages_;\n";
2116 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002117
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002118 if (D.OutLanguage.empty())
2119 throw "Tool " + D.Name + " has no 'out_language' property!";
2120
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002121 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2122 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2123 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002124}
2125
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002126/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002127void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002128 O.indent(Indent1) << "const char* Name() const {\n";
2129 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2130 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002131}
2132
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002133/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2134/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002135void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002136 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002137 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002138 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002139 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002140 O.indent(Indent2) << "return false;\n";
2141 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002142}
2143
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002144/// EmitStaticMemberDefinitions - Emit static member definitions for a
2145/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002146void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002147 if (D.InLanguage.empty())
2148 throw "Tool " + D.Name + " has no 'in_language' property!";
2149
2150 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2151 for (StrVector::const_iterator B = D.InLanguage.begin(),
2152 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002153 O << '\"' << *B << "\", ";
2154 O << "0};\n\n";
2155}
2156
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002157/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002158void EmitToolClassDefinition (const ToolDescription& D,
2159 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002160 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002161 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002162 return;
2163
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002164 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002165 O << "class " << D.Name << " : public ";
2166 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002167 O << "JoinTool";
2168 else
2169 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002170
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002171 O << " {\nprivate:\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002172 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002173
2174 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002175 EmitNameMethod(D, O);
2176 EmitInOutLanguageMethods(D, O);
2177 EmitIsJoinMethod(D, O);
2178 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002179
2180 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002181 O << "};\n";
2182
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002183 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002184
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002185}
2186
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002187/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002188/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002189void EmitOptionDefinitions (const OptionDescriptions& descs,
2190 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002191 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002192{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002193 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002194
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002195 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002196 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002197 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002198 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002199
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002200 if (val.Type == OptionType::Alias) {
2201 Aliases.push_back(val);
2202 continue;
2203 }
2204
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002205 if (val.isExtern())
2206 O << "extern ";
2207
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002208 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002209 << val.GenVariableName();
2210
2211 if (val.isExtern()) {
2212 O << ";\n";
2213 continue;
2214 }
2215
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002216 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002217
2218 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2219 O << ", cl::Prefix";
2220
2221 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002222 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002223 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002224 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002225 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002226 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002227 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002228 O << ", cl::OneOrMore";
2229 }
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002230 else if (val.isOptional() && val.isList()) {
2231 O << ", cl::Optional";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002232 }
2233
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002234 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002235 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002236 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002237 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002238
2239 if (val.isCommaSeparated())
2240 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002241
2242 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002243 O << ", cl::multi_val(" << val.MultiVal << ')';
2244
2245 if (val.InitVal) {
2246 const std::string& str = val.InitVal->getAsString();
2247 O << ", cl::init(" << str << ')';
2248 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002249
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002250 if (!val.Help.empty())
2251 O << ", cl::desc(\"" << val.Help << "\")";
2252
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002253 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002254 }
2255
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002256 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002257 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002258 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002259 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002260
2261 O << val.GenTypeDeclaration() << ' '
2262 << val.GenVariableName()
2263 << "(\"" << val.Name << '\"';
2264
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002265 const OptionDescription& D = descs.FindOption(val.Help);
2266 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002267
2268 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2269 }
2270
2271 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002272 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002273 O << (HasExterns ? "extern cl" : "cl")
2274 << "::list<std::string> " << SinkOptionName
2275 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002276
2277 O << '\n';
2278}
2279
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002280/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002281/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002282class EmitPreprocessOptionsCallback : ActionHandlingCallbackBase {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002283 const OptionDescriptions& OptDescs_;
2284
2285 void onUnsetOption(Init* i, unsigned IndentLevel, raw_ostream& O) {
2286 const std::string& OptName = InitPtrToString(i);
2287 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002288
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002289 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002290 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2291 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002292 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002293 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2294 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002295 else if (OptDesc.isList()) {
2296 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2297 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002298 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002299 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002300 }
2301 }
2302
2303 void processDag(const Init* I, unsigned IndentLevel, raw_ostream& O)
2304 {
2305 const DagInit& d = InitPtrToDag(I);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002306 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002307
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002308 if (OpName == "warning") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002309 this->onWarningDag(d, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002310 }
2311 else if (OpName == "error") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002312 this->onWarningDag(d, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002313 }
2314 else if (OpName == "unset_option") {
2315 checkNumberOfArguments(&d, 1);
2316 Init* I = d.getArg(0);
2317 if (typeid(*I) == typeid(ListInit)) {
2318 const ListInit& DagList = *static_cast<const ListInit*>(I);
2319 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
2320 B != E; ++B)
2321 this->onUnsetOption(*B, IndentLevel, O);
2322 }
2323 else {
2324 this->onUnsetOption(I, IndentLevel, O);
2325 }
2326 }
2327 else {
2328 throw "Unknown operator in the option preprocessor: '" + OpName + "'!"
2329 "\nOnly 'warning', 'error' and 'unset_option' are allowed.";
2330 }
2331 }
2332
2333public:
2334
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002335 void operator()(const Init* I, unsigned IndentLevel, raw_ostream& O) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002336 this->processDag(I, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002337 }
2338
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002339 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002340 : OptDescs_(OptDescs)
2341 {}
2342};
2343
2344/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2345void EmitPreprocessOptions (const RecordKeeper& Records,
2346 const OptionDescriptions& OptDecs, raw_ostream& O)
2347{
2348 O << "void PreprocessOptionsLocal() {\n";
2349
2350 const RecordVector& OptionPreprocessors =
2351 Records.getAllDerivedDefinitions("OptionPreprocessor");
2352
2353 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2354 E = OptionPreprocessors.end(); B!=E; ++B) {
2355 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002356 EmitCaseConstructHandler(Case, Indent1,
2357 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002358 false, OptDecs, O);
2359 }
2360
2361 O << "}\n\n";
2362}
2363
2364/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002365void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002366{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002367 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002368
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002369 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002370 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002371
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002372 // It is allowed for a plugin to have no language map.
2373 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002374
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002375 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2376 if (!LangsToSuffixesList)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00002377 throw "Error in the language map definition!";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002378
2379 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002380 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002381
2382 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2383 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2384
2385 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002386 O.indent(Indent1) << "langMap[\""
2387 << InitPtrToString(Suffixes->getElement(i))
2388 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002389 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002390 }
2391
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002392 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002393}
2394
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002395/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2396/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002397void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002398 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002399 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002400 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002401
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002402 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002403 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002404 }
2405 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002406 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002407 }
2408 else if (OpName == "error") {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002409 checkNumberOfArguments(&d, 1);
2410 O.indent(IndentLevel) << "throw std::runtime_error(\""
2411 << InitPtrToString(d.getArg(0))
2412 << "\");\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002413 return;
2414 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002415 else {
2416 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002417 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002418 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002419
2420 if (d.getNumArgs() > 0)
2421 O << InitPtrToInt(d.getArg(0)) << ";\n";
2422 else
2423 O << "2;\n";
2424
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002425}
2426
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002427/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002428void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002429 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002430 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002431
2432 // Class constructor.
2433 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002434 << "public:\n";
2435 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2436 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002437
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002438 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002439 O.indent(Indent1)
2440 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2441 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002442
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002443 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002444 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002445
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002446 O.indent(Indent2) << "return ret;\n";
2447 O.indent(Indent1) << "};\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002448}
2449
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002450/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002451void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002452 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002453 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002454 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002455 for (RecordVector::const_iterator B = EdgeVector.begin(),
2456 E = EdgeVector.end(); B != E; ++B) {
2457 const Record* Edge = *B;
2458 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002459 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002460
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002461 if (!isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002462 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002463 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002464 }
2465}
2466
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002467/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002468/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002469void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002470 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002471 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002472{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002473 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002474
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002475 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2476 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002477 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002478
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002479 O << '\n';
2480
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002481 // Insert edges.
2482
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002483 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002484 for (RecordVector::const_iterator B = EdgeVector.begin(),
2485 E = EdgeVector.end(); B != E; ++B) {
2486 const Record* Edge = *B;
2487 const std::string& NodeA = Edge->getValueAsString("a");
2488 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002489 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002490
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002491 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002492
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002493 if (isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002494 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002495 else
2496 O << "new Edge" << i << "()";
2497
2498 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002499 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002500 }
2501
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002502 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002503}
2504
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002505/// HookInfo - Information about the hook type and number of arguments.
2506struct HookInfo {
2507
2508 // A hook can either have a single parameter of type std::vector<std::string>,
2509 // or NumArgs parameters of type const char*.
2510 enum HookType { ListHook, ArgHook };
2511
2512 HookType Type;
2513 unsigned NumArgs;
2514
2515 HookInfo() : Type(ArgHook), NumArgs(1)
2516 {}
2517
2518 HookInfo(HookType T) : Type(T), NumArgs(1)
2519 {}
2520
2521 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2522 {}
2523};
2524
2525typedef llvm::StringMap<HookInfo> HookInfoMap;
2526
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002527/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002528/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002529/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002530class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002531 HookInfoMap& HookNames_;
2532 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002533public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002534 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2535 : HookNames_(HookNames), OptDescs_(OptDescs)
2536 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002537
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002538 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002539 const std::string& Name = GetOperatorName(Dag);
2540
2541 if (Name == "forward_transformed_value") {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002542 checkNumberOfArguments(Dag, 2);
2543 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2544 const std::string& HookName = InitPtrToString(Dag.getArg(1));
2545 const OptionDescription& D = OptDescs_.FindOption(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002546
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002547 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2548 : HookInfo::ArgHook);
2549 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002550 else if (Name == "append_cmd" || Name == "output_suffix") {
2551 checkNumberOfArguments(Dag, 1);
2552 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2553 }
2554 }
2555
2556 void onCmdLine(const std::string& Cmd) {
2557 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002558 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002559
2560 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2561 B != E; ++B) {
2562 const std::string& cmd = *B;
2563
2564 if (cmd == "$CALL") {
2565 unsigned NumArgs = 0;
2566 checkedIncrement(B, E, "Syntax error in $CALL invocation!");
2567 const std::string& HookName = *B;
2568
2569 if (HookName.at(0) == ')')
2570 throw "$CALL invoked with no arguments!";
2571
2572 while (++B != E && B->at(0) != ')') {
2573 ++NumArgs;
2574 }
2575
2576 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2577
2578 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2579 H->second.Type != HookInfo::ArgHook)
2580 throw "Overloading of hooks is not allowed. Overloaded hook: "
2581 + HookName;
2582 else
2583 HookNames_[HookName] = HookInfo(NumArgs);
2584 }
2585 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002586 }
2587
2588 void operator()(const Init* Arg) {
2589
2590 // We're invoked on an action (either a dag or a dag list).
2591 if (typeid(*Arg) == typeid(DagInit)) {
2592 const DagInit& Dag = InitPtrToDag(Arg);
2593 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002594 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002595 }
2596 else if (typeid(*Arg) == typeid(ListInit)) {
2597 const ListInit& List = InitPtrToList(Arg);
2598 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2599 ++B) {
2600 const DagInit& Dag = InitPtrToDag(*B);
2601 this->onAction(Dag);
2602 }
2603 return;
2604 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002605
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002606 // We're invoked on a command line.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002607 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002608 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002609
2610 void operator()(const DagInit* Test, unsigned, bool) {
2611 this->operator()(Test);
2612 }
2613 void operator()(const Init* Statement, unsigned) {
2614 this->operator()(Statement);
2615 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002616};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002617
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002618/// FillInHookNames - Actually extract the hook names from all command
2619/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002620void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002621 const OptionDescriptions& OptDescs,
2622 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002623{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002624 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002625 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2626 E = ToolDescs.end(); B != E; ++B) {
2627 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002628
2629 // Look for 'forward_transformed_value' in 'actions'.
2630 if (D.Actions)
2631 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2632
2633 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002634 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002635 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002636 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002637 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002638 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002639 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002640 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002641 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002642 }
2643}
2644
2645/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2646/// property records and emit hook function declaration for each
2647/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002648void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2649 const OptionDescriptions& OptDescs, raw_ostream& O) {
2650 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002651
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002652 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002653 if (HookNames.empty())
2654 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002655
2656 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002657 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002658 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002659 const char* HookName = B->first();
2660 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002661
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002662 O.indent(Indent1) << "std::string " << HookName << "(";
2663
2664 if (Info.Type == HookInfo::ArgHook) {
2665 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2666 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2667 }
2668 }
2669 else {
2670 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002671 }
2672
2673 O <<");\n";
2674 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002675 O << "}\n\n";
2676}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002677
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002678/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002679void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002680 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2681 O.indent(Indent1) << "int Priority() const { return "
2682 << Priority << "; }\n\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002683 O.indent(Indent1) << "void PreprocessOptions() const\n";
2684 O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002685 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2686 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2687 O.indent(Indent1)
2688 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2689 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2690 << "};\n\n"
2691 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002692}
2693
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002694/// EmitIncludes - Emit necessary #include directives and some
2695/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002696void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002697 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2698 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002699 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002700 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2701 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002702
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002703 << "#include \"llvm/Support/CommandLine.h\"\n"
2704 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002705
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002706 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002707 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002708 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002709 << "#include <stdexcept>\n\n"
2710
2711 << "using namespace llvm;\n"
2712 << "using namespace llvmc;\n\n"
2713
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002714 << "extern cl::opt<std::string> OutputFilename;\n\n"
2715
2716 << "inline const char* checkCString(const char* s)\n"
2717 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002718}
2719
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002720
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002721/// PluginData - Holds all information about a plugin.
2722struct PluginData {
2723 OptionDescriptions OptDescs;
2724 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002725 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002726 ToolDescriptions ToolDescs;
2727 RecordVector Edges;
2728 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002729};
2730
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002731/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002732/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002733bool HasSink(const ToolDescriptions& ToolDescs) {
2734 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2735 E = ToolDescs.end(); B != E; ++B)
2736 if ((*B)->isSink())
2737 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002738
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002739 return false;
2740}
2741
2742/// HasExterns - Go through the list of option descriptions and check
2743/// if there are any external options.
2744bool HasExterns(const OptionDescriptions& OptDescs) {
2745 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2746 E = OptDescs.end(); B != E; ++B)
2747 if (B->second.isExtern())
2748 return true;
2749
2750 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002751}
2752
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002753/// CollectPluginData - Collect tool and option properties,
2754/// compilation graph edges and plugin priority from the parse tree.
2755void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2756 // Collect option properties.
2757 const RecordVector& OptionLists =
2758 Records.getAllDerivedDefinitions("OptionList");
2759 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2760 Data.OptDescs);
2761
2762 // Collect tool properties.
2763 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2764 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2765 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002766 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002767
2768 // Collect compilation graph edges.
2769 const RecordVector& CompilationGraphs =
2770 Records.getAllDerivedDefinitions("CompilationGraph");
2771 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2772 Data.Edges);
2773
2774 // Calculate the priority of this plugin.
2775 const RecordVector& Priorities =
2776 Records.getAllDerivedDefinitions("PluginPriority");
2777 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002778}
2779
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002780/// CheckPluginData - Perform some sanity checks on the collected data.
2781void CheckPluginData(PluginData& Data) {
2782 // Filter out all tools not mentioned in the compilation graph.
2783 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002784
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002785 // Typecheck the compilation graph.
2786 TypecheckGraph(Data.Edges, Data.ToolDescs);
2787
2788 // Check that there are no options without side effects (specified
2789 // only in the OptionList).
2790 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2791
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002792}
2793
Daniel Dunbar1a551802009-07-03 00:10:29 +00002794void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002795 // Emit file header.
2796 EmitIncludes(O);
2797
2798 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002799 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002800
2801 // Emit hook declarations.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002802 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002803
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002804 O << "namespace {\n\n";
2805
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002806 // Emit PreprocessOptionsLocal() function.
2807 EmitPreprocessOptions(Records, Data.OptDescs, O);
2808
2809 // Emit PopulateLanguageMapLocal() function
2810 // (language map maps from file extensions to language names).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002811 EmitPopulateLanguageMap(Records, O);
2812
2813 // Emit Tool classes.
2814 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2815 E = Data.ToolDescs.end(); B!=E; ++B)
2816 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2817
2818 // Emit Edge# classes.
2819 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2820
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002821 // Emit PopulateCompilationGraphLocal() function.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002822 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2823
2824 // Emit code for plugin registration.
2825 EmitRegisterPlugin(Data.Priority, O);
2826
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002827 O << "} // End anonymous namespace.\n\n";
2828
2829 // Force linkage magic.
2830 O << "namespace llvmc {\n";
2831 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2832 O << "}\n";
2833
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002834 // EOF
2835}
2836
2837
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002838// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00002839}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002840
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002841/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002842void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002843 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002844 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002845
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002846 CollectPluginData(Records, Data);
2847 CheckPluginData(Data);
2848
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +00002849 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002850 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002851
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002852 } catch (std::exception& Error) {
2853 throw Error.what() + std::string(" - usually this means a syntax error.");
2854 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002855}