blob: 7ec389144406ce218799d437ffec35c91bb55cf7 [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"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/StringMap.h"
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000021#include "llvm/ADT/StringSet.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000022#include <algorithm>
23#include <cassert>
24#include <functional>
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +000025#include <stdexcept>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000026#include <string>
Chris Lattner32a9e7a2008-06-04 04:46:14 +000027#include <typeinfo>
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +000028
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000029using namespace llvm;
30
Mikhail Glushenkov895820d2008-05-06 18:12:03 +000031namespace {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000032
33//===----------------------------------------------------------------------===//
34/// Typedefs
35
36typedef std::vector<Record*> RecordVector;
37typedef std::vector<std::string> StrVector;
38
39//===----------------------------------------------------------------------===//
40/// Constants
41
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +000042// Indentation.
43unsigned TabWidth = 4;
Mikhail Glushenkov9d7a2dc2009-09-28 01:15:44 +000044unsigned Indent1 = TabWidth*1;
45unsigned Indent2 = TabWidth*2;
46unsigned Indent3 = TabWidth*3;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000047
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000048// Default help string.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000049const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
50
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000051// Name for the "sink" option.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000052const char * SinkOptionName = "AutoGeneratedSinkOption";
53
54//===----------------------------------------------------------------------===//
55/// Helper functions
56
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000057/// Id - An 'identity' function object.
58struct Id {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000059 template<typename T0>
60 void operator()(const T0&) const {
61 }
62 template<typename T0, typename T1>
63 void operator()(const T0&, const T1&) const {
64 }
65 template<typename T0, typename T1, typename T2>
66 void operator()(const T0&, const T1&, const T2&) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000067 }
68};
69
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000070int InitPtrToInt(const Init* ptr) {
71 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000072 return val.getValue();
73}
74
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000075const std::string& InitPtrToString(const Init* ptr) {
76 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
77 return val.getValue();
78}
79
80const ListInit& InitPtrToList(const Init* ptr) {
81 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
82 return val;
83}
84
85const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000086 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000087 return val;
88}
89
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000090const std::string GetOperatorName(const DagInit* D) {
91 return D->getOperator()->getAsString();
92}
93
94const std::string GetOperatorName(const DagInit& D) {
95 return GetOperatorName(&D);
96}
97
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000098// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +000099// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000100void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000101 if (!d || d->getNumArgs() < min_arguments)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000102 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000103}
104
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000105// isDagEmpty - is this DAG marked with an empty marker?
106bool isDagEmpty (const DagInit* d) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000107 return GetOperatorName(d) == "empty_dag_marker";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000108}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000109
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000110// EscapeVariableName - Escape commas and other symbols not allowed
111// in the C++ variable names. Makes it possible to use options named
112// like "Wa," (useful for prefix options).
113std::string EscapeVariableName(const std::string& Var) {
114 std::string ret;
115 for (unsigned i = 0; i != Var.size(); ++i) {
116 char cur_char = Var[i];
117 if (cur_char == ',') {
118 ret += "_comma_";
119 }
120 else if (cur_char == '+') {
121 ret += "_plus_";
122 }
123 else if (cur_char == '-') {
124 ret += "_dash_";
125 }
126 else {
127 ret.push_back(cur_char);
128 }
129 }
130 return ret;
131}
132
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000133/// oneOf - Does the input string contain this character?
134bool oneOf(const char* lst, char c) {
135 while (*lst) {
136 if (*lst++ == c)
137 return true;
138 }
139 return false;
140}
141
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000142template <class I, class S>
143void checkedIncrement(I& P, I E, S ErrorString) {
144 ++P;
145 if (P == E)
146 throw ErrorString;
147}
148
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000149// apply is needed because C++'s syntax doesn't let us construct a function
150// object and call it in the same statement.
151template<typename F, typename T0>
152void apply(F Fun, T0& Arg0) {
153 return Fun(Arg0);
154}
155
156template<typename F, typename T0, typename T1>
157void apply(F Fun, T0& Arg0, T1& Arg1) {
158 return Fun(Arg0, Arg1);
159}
160
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000161//===----------------------------------------------------------------------===//
162/// Back-end specific code
163
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000164
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000165/// OptionType - One of six different option types. See the
166/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000167namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000168
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000169 enum OptionType { Alias, Switch, Parameter, ParameterList,
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000170 Prefix, PrefixList};
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000171
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000172 bool IsAlias(OptionType t) {
173 return (t == Alias);
174 }
175
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000176 bool IsList (OptionType t) {
177 return (t == ParameterList || t == PrefixList);
178 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000179
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000180 bool IsSwitch (OptionType t) {
181 return (t == Switch);
182 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000183
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000184 bool IsParameter (OptionType t) {
185 return (t == Parameter || t == Prefix);
186 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000187
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000188}
189
190OptionType::OptionType stringToOptionType(const std::string& T) {
191 if (T == "alias_option")
192 return OptionType::Alias;
193 else if (T == "switch_option")
194 return OptionType::Switch;
195 else if (T == "parameter_option")
196 return OptionType::Parameter;
197 else if (T == "parameter_list_option")
198 return OptionType::ParameterList;
199 else if (T == "prefix_option")
200 return OptionType::Prefix;
201 else if (T == "prefix_list_option")
202 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000203 else
204 throw "Unknown option type: " + T + '!';
205}
206
207namespace OptionDescriptionFlags {
208 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000209 ReallyHidden = 0x4, Extern = 0x8,
210 OneOrMore = 0x10, ZeroOrOne = 0x20 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000211}
212
213/// OptionDescription - Represents data contained in a single
214/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000215struct OptionDescription {
216 OptionType::OptionType Type;
217 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000218 unsigned Flags;
219 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000220 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000221 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000222
223 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000224 const std::string& n = "",
225 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000226 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000227 {}
228
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000229 /// GenTypeDeclaration - Returns the C++ variable type of this
230 /// option.
231 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000232
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000233 /// GenVariableName - Returns the variable name used in the
234 /// generated C++ code.
235 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000236
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000237 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000238 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000239
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000240 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000241
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000242 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000243
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000244 bool isMultiVal() const;
245
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000246 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000247 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000248
249 bool isRequired() const;
250 void setRequired();
251
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000252 bool isOneOrMore() const;
253 void setOneOrMore();
254
255 bool isZeroOrOne() const;
256 void setZeroOrOne();
257
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000258 bool isHidden() const;
259 void setHidden();
260
261 bool isReallyHidden() const;
262 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000263
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000264 bool isSwitch() const
265 { return OptionType::IsSwitch(this->Type); }
266
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000267 bool isParameter() const
268 { return OptionType::IsParameter(this->Type); }
269
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000270 bool isList() const
271 { return OptionType::IsList(this->Type); }
272
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000273};
274
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000275void OptionDescription::Merge (const OptionDescription& other)
276{
277 if (other.Type != Type)
278 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000279
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000280 if (Help == other.Help || Help == DefaultHelpString)
281 Help = other.Help;
282 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000283 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000284 " defined for option " + Name + "\n";
285 }
286
287 Flags |= other.Flags;
288}
289
290bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000291 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000292}
293
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000294bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000295 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000296}
297
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000298bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000299 return Flags & OptionDescriptionFlags::Extern;
300}
301void OptionDescription::setExtern() {
302 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000303}
304
305bool OptionDescription::isRequired() const {
306 return Flags & OptionDescriptionFlags::Required;
307}
308void OptionDescription::setRequired() {
309 Flags |= OptionDescriptionFlags::Required;
310}
311
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000312bool OptionDescription::isOneOrMore() const {
313 return Flags & OptionDescriptionFlags::OneOrMore;
314}
315void OptionDescription::setOneOrMore() {
316 Flags |= OptionDescriptionFlags::OneOrMore;
317}
318
319bool OptionDescription::isZeroOrOne() const {
320 return Flags & OptionDescriptionFlags::ZeroOrOne;
321}
322void OptionDescription::setZeroOrOne() {
323 Flags |= OptionDescriptionFlags::ZeroOrOne;
324}
325
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000326bool OptionDescription::isHidden() const {
327 return Flags & OptionDescriptionFlags::Hidden;
328}
329void OptionDescription::setHidden() {
330 Flags |= OptionDescriptionFlags::Hidden;
331}
332
333bool OptionDescription::isReallyHidden() const {
334 return Flags & OptionDescriptionFlags::ReallyHidden;
335}
336void OptionDescription::setReallyHidden() {
337 Flags |= OptionDescriptionFlags::ReallyHidden;
338}
339
340const char* OptionDescription::GenTypeDeclaration() const {
341 switch (Type) {
342 case OptionType::Alias:
343 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000344 case OptionType::PrefixList:
345 case OptionType::ParameterList:
346 return "cl::list<std::string>";
347 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000348 return "cl::opt<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000349 case OptionType::Parameter:
350 case OptionType::Prefix:
351 default:
352 return "cl::opt<std::string>";
353 }
354}
355
356std::string OptionDescription::GenVariableName() const {
357 const std::string& EscapedName = EscapeVariableName(Name);
358 switch (Type) {
359 case OptionType::Alias:
360 return "AutoGeneratedAlias_" + EscapedName;
361 case OptionType::PrefixList:
362 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000363 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000364 case OptionType::Switch:
365 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000366 case OptionType::Prefix:
367 case OptionType::Parameter:
368 default:
369 return "AutoGeneratedParameter_" + EscapedName;
370 }
371}
372
373/// OptionDescriptions - An OptionDescription array plus some helper
374/// functions.
375class OptionDescriptions {
376 typedef StringMap<OptionDescription> container_type;
377
378 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000379 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000380
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000381public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000382 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000383 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000384
385 // Wrappers for FindOption that throw an exception in case the option has a
386 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000387 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000388 const OptionDescription& FindParameter(const std::string& OptName) const;
389 const OptionDescription& FindList(const std::string& OptName) const;
390 const OptionDescription&
391 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000392
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000393 /// insertDescription - Insert new OptionDescription into
394 /// OptionDescriptions list
395 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000396
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000397 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000398 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000399 const_iterator begin() const { return Descriptions.begin(); }
400 const_iterator end() const { return Descriptions.end(); }
401};
402
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000403const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000404OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000405 const_iterator I = Descriptions.find(OptName);
406 if (I != Descriptions.end())
407 return I->second;
408 else
409 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000410}
411
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000412const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000413OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000414 const OptionDescription& OptDesc = this->FindOption(OptName);
415 if (!OptDesc.isSwitch())
416 throw OptName + ": incorrect option type - should be a switch!";
417 return OptDesc;
418}
419
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000420const OptionDescription&
421OptionDescriptions::FindList(const std::string& OptName) const {
422 const OptionDescription& OptDesc = this->FindOption(OptName);
423 if (!OptDesc.isList())
424 throw OptName + ": incorrect option type - should be a list!";
425 return OptDesc;
426}
427
428const OptionDescription&
429OptionDescriptions::FindParameter(const std::string& OptName) const {
430 const OptionDescription& OptDesc = this->FindOption(OptName);
431 if (!OptDesc.isParameter())
432 throw OptName + ": incorrect option type - should be a parameter!";
433 return OptDesc;
434}
435
436const OptionDescription&
437OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
438 const OptionDescription& OptDesc = this->FindOption(OptName);
439 if (!OptDesc.isList() && !OptDesc.isParameter())
440 throw OptName
441 + ": incorrect option type - should be a list or parameter!";
442 return OptDesc;
443}
444
445void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000446 container_type::iterator I = Descriptions.find(o.Name);
447 if (I != Descriptions.end()) {
448 OptionDescription& D = I->second;
449 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000450 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000451 else {
452 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000453 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000454}
455
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000456/// HandlerTable - A base class for function objects implemented as
457/// 'tables of handlers'.
458template <class T>
459class HandlerTable {
460protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000461 // Implementation details.
462
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000463 /// Handler -
464 typedef void (T::* Handler) (const DagInit*);
465 /// HandlerMap - A map from property names to property handlers
466 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000467
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000468 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000469 static bool staticMembersInitialized_;
470
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000471 T* childPtr;
472public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000473
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000474 HandlerTable(T* cp) : childPtr(cp)
475 {}
476
477 /// operator() - Just forwards to the corresponding property
478 /// handler.
479 void operator() (Init* i) {
480 const DagInit& property = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000481 const std::string& property_name = GetOperatorName(property);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000482 typename HandlerMap::iterator method = Handlers_.find(property_name);
483
484 if (method != Handlers_.end()) {
485 Handler h = method->second;
486 (childPtr->*h)(&property);
487 }
488 else {
489 throw "No handler found for property " + property_name + "!";
490 }
491 }
492
493 void AddHandler(const char* Property, Handler Handl) {
494 Handlers_[Property] = Handl;
495 }
496};
497
498template <class T> typename HandlerTable<T>::HandlerMap
499HandlerTable<T>::Handlers_;
500template <class T> bool HandlerTable<T>::staticMembersInitialized_ = false;
501
502
503/// CollectOptionProperties - Function object for iterating over an
504/// option property list.
505class CollectOptionProperties : public HandlerTable<CollectOptionProperties> {
506private:
507
508 /// optDescs_ - OptionDescriptions table. This is where the
509 /// information is stored.
510 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000511
512public:
513
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000514 explicit CollectOptionProperties(OptionDescription& OD)
515 : HandlerTable<CollectOptionProperties>(this), optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000516 {
517 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000518 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000519 AddHandler("help", &CollectOptionProperties::onHelp);
520 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000521 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000522 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
523 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000524 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
525 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000526 AddHandler("zero_or_one", &CollectOptionProperties::onZeroOrOne);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000527
528 staticMembersInitialized_ = true;
529 }
530 }
531
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000532private:
533
534 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000535 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000536
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000537 void onExtern (const DagInit* d) {
538 checkNumberOfArguments(d, 0);
539 optDesc_.setExtern();
540 }
541
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000542 void onHelp (const DagInit* d) {
543 checkNumberOfArguments(d, 1);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000544 optDesc_.Help = InitPtrToString(d->getArg(0));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000545 }
546
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000547 void onHidden (const DagInit* d) {
548 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000549 optDesc_.setHidden();
550 }
551
552 void onReallyHidden (const DagInit* d) {
553 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000554 optDesc_.setReallyHidden();
555 }
556
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000557 void onRequired (const DagInit* d) {
558 checkNumberOfArguments(d, 0);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000559 if (optDesc_.isOneOrMore())
560 throw std::string("An option can't have both (required) "
561 "and (one_or_more) properties!");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000562 optDesc_.setRequired();
563 }
564
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000565 void onInit (const DagInit* d) {
566 checkNumberOfArguments(d, 1);
567 Init* i = d->getArg(0);
568 const std::string& str = i->getAsString();
569
570 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
571 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
572
573 if (!correct)
574 throw std::string("Incorrect usage of the 'init' option property!");
575
576 optDesc_.InitVal = i;
577 }
578
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000579 void onOneOrMore (const DagInit* d) {
580 checkNumberOfArguments(d, 0);
581 if (optDesc_.isRequired() || optDesc_.isZeroOrOne())
582 throw std::string("Only one of (required), (zero_or_one) or "
583 "(one_or_more) properties is allowed!");
584 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000585 llvm::errs() << "Warning: specifying the 'one_or_more' property "
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000586 "on a non-list option will have no effect.\n";
587 optDesc_.setOneOrMore();
588 }
589
590 void onZeroOrOne (const DagInit* d) {
591 checkNumberOfArguments(d, 0);
592 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
593 throw std::string("Only one of (required), (zero_or_one) or "
594 "(one_or_more) properties is allowed!");
595 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000596 llvm::errs() << "Warning: specifying the 'zero_or_one' property"
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000597 "on a non-list option will have no effect.\n";
598 optDesc_.setZeroOrOne();
599 }
600
601 void onMultiVal (const DagInit* d) {
602 checkNumberOfArguments(d, 1);
603 int val = InitPtrToInt(d->getArg(0));
604 if (val < 2)
605 throw std::string("Error in the 'multi_val' property: "
606 "the value must be greater than 1!");
607 if (!OptionType::IsList(optDesc_.Type))
608 throw std::string("The multi_val property is valid only "
609 "on list options!");
610 optDesc_.MultiVal = val;
611 }
612
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000613};
614
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000615/// AddOption - A function object that is applied to every option
616/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000617class AddOption {
618private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000619 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000620
621public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000622 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000623 {}
624
625 void operator()(const Init* i) {
626 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000627 checkNumberOfArguments(&d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000628
629 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000630 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000631 const std::string& Name = InitPtrToString(d.getArg(0));
632
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000633 OptionDescription OD(Type, Name);
634
635 if (!OD.isExtern())
636 checkNumberOfArguments(&d, 2);
637
638 if (OD.isAlias()) {
639 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000640 OD.Help = InitPtrToString(d.getArg(1));
641 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000642 else if (!OD.isExtern()) {
643 processOptionProperties(&d, OD);
644 }
645 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000646 }
647
648private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000649 /// processOptionProperties - Go through the list of option
650 /// properties and call a corresponding handler for each.
651 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
652 checkNumberOfArguments(d, 2);
653 DagInit::const_arg_iterator B = d->arg_begin();
654 // Skip the first argument: it's always the option name.
655 ++B;
656 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000657 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000658
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000659};
660
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000661/// CollectOptionDescriptions - Collects option properties from all
662/// OptionLists.
663void CollectOptionDescriptions (RecordVector::const_iterator B,
664 RecordVector::const_iterator E,
665 OptionDescriptions& OptDescs)
666{
667 // For every OptionList:
668 for (; B!=E; ++B) {
669 RecordVector::value_type T = *B;
670 // Throws an exception if the value does not exist.
671 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000672
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000673 // For every option description in this list:
674 // collect the information and
675 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
676 }
677}
678
679// Tool information record
680
681namespace ToolFlags {
682 enum ToolFlags { Join = 0x1, Sink = 0x2 };
683}
684
685struct ToolDescription : public RefCountedBase<ToolDescription> {
686 std::string Name;
687 Init* CmdLine;
688 Init* Actions;
689 StrVector InLanguage;
690 std::string OutLanguage;
691 std::string OutputSuffix;
692 unsigned Flags;
693
694 // Various boolean properties
695 void setSink() { Flags |= ToolFlags::Sink; }
696 bool isSink() const { return Flags & ToolFlags::Sink; }
697 void setJoin() { Flags |= ToolFlags::Join; }
698 bool isJoin() const { return Flags & ToolFlags::Join; }
699
700 // Default ctor here is needed because StringMap can only store
701 // DefaultConstructible objects
702 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
703 ToolDescription (const std::string& n)
704 : Name(n), CmdLine(0), Actions(0), Flags(0)
705 {}
706};
707
708/// ToolDescriptions - A list of Tool information records.
709typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
710
711
712/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000713/// tool property records.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000714class CollectToolProperties : public HandlerTable<CollectToolProperties> {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000715private:
716
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000717 /// toolDesc_ - Properties of the current Tool. This is where the
718 /// information is stored.
719 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000720
721public:
722
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000723 explicit CollectToolProperties (ToolDescription& d)
724 : HandlerTable<CollectToolProperties>(this) , toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000725 {
726 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000727
728 AddHandler("actions", &CollectToolProperties::onActions);
729 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
730 AddHandler("in_language", &CollectToolProperties::onInLanguage);
731 AddHandler("join", &CollectToolProperties::onJoin);
732 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
733 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
734 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000735
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000736 staticMembersInitialized_ = true;
737 }
738 }
739
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000740private:
741
742 /// Property handlers --
743 /// Functions that extract information about tool properties from
744 /// DAG representation.
745
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000746 void onActions (const DagInit* d) {
747 checkNumberOfArguments(d, 1);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000748 Init* Case = d->getArg(0);
749 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000750 GetOperatorName(static_cast<DagInit*>(Case)) != "case")
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000751 throw
752 std::string("The argument to (actions) should be a 'case' construct!");
753 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000754 }
755
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000756 void onCmdLine (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000757 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000758 toolDesc_.CmdLine = d->getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000759 }
760
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000761 void onInLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000762 checkNumberOfArguments(d, 1);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000763 Init* arg = d->getArg(0);
764
765 // Find out the argument's type.
766 if (typeid(*arg) == typeid(StringInit)) {
767 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000768 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000769 }
770 else {
771 // It's a list.
772 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000773 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000774
775 // Copy strings to the output vector.
776 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
777 B != E; ++B) {
778 out.push_back(InitPtrToString(*B));
779 }
780
781 // Remove duplicates.
782 std::sort(out.begin(), out.end());
783 StrVector::iterator newE = std::unique(out.begin(), out.end());
784 out.erase(newE, out.end());
785 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000786 }
787
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000788 void onJoin (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000789 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000790 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000791 }
792
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000793 void onOutLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000794 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000795 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000796 }
797
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000798 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000799 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000800 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000801 }
802
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000803 void onSink (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000804 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000805 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000806 }
807
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000808};
809
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000810/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000811/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000812/// CollectToolProperties function object).
813void CollectToolDescriptions (RecordVector::const_iterator B,
814 RecordVector::const_iterator E,
815 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000816{
817 // Iterate over a properties list of every Tool definition
818 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000819 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000820 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000821 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000822
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000823 IntrusiveRefCntPtr<ToolDescription>
824 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000825
826 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000827 CollectToolProperties(*ToolDesc));
828 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000829 }
830}
831
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000832/// FillInEdgeVector - Merge all compilation graph definitions into
833/// one single edge list.
834void FillInEdgeVector(RecordVector::const_iterator B,
835 RecordVector::const_iterator E, RecordVector& Out) {
836 for (; B != E; ++B) {
837 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000838
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000839 for (unsigned i = 0; i < edges->size(); ++i)
840 Out.push_back(edges->getElementAsRecord(i));
841 }
842}
843
844/// CalculatePriority - Calculate the priority of this plugin.
845int CalculatePriority(RecordVector::const_iterator B,
846 RecordVector::const_iterator E) {
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000847 int priority = 0;
848
849 if (B != E) {
850 priority = static_cast<int>((*B)->getValueAsInt("priority"));
851
852 if (++B != E)
853 throw std::string("More than one 'PluginPriority' instance found: "
854 "most probably an error!");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000855 }
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000856
857 return priority;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000858}
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000859
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000860/// NotInGraph - Helper function object for FilterNotInGraph.
861struct NotInGraph {
862private:
863 const llvm::StringSet<>& ToolsInGraph_;
864
865public:
866 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
867 : ToolsInGraph_(ToolsInGraph)
868 {}
869
870 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
871 return (ToolsInGraph_.count(x->Name) == 0);
872 }
873};
874
875/// FilterNotInGraph - Filter out from ToolDescs all Tools not
876/// mentioned in the compilation graph definition.
877void FilterNotInGraph (const RecordVector& EdgeVector,
878 ToolDescriptions& ToolDescs) {
879
880 // List all tools mentioned in the graph.
881 llvm::StringSet<> ToolsInGraph;
882
883 for (RecordVector::const_iterator B = EdgeVector.begin(),
884 E = EdgeVector.end(); B != E; ++B) {
885
886 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000887 const std::string& NodeA = Edge->getValueAsString("a");
888 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000889
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000890 if (NodeA != "root")
891 ToolsInGraph.insert(NodeA);
892 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000893 }
894
895 // Filter ToolPropertiesList.
896 ToolDescriptions::iterator new_end =
897 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
898 NotInGraph(ToolsInGraph));
899 ToolDescs.erase(new_end, ToolDescs.end());
900}
901
902/// FillInToolToLang - Fills in two tables that map tool names to
903/// (input, output) languages. Helper function used by TypecheckGraph().
904void FillInToolToLang (const ToolDescriptions& ToolDescs,
905 StringMap<StringSet<> >& ToolToInLang,
906 StringMap<std::string>& ToolToOutLang) {
907 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
908 E = ToolDescs.end(); B != E; ++B) {
909 const ToolDescription& D = *(*B);
910 for (StrVector::const_iterator B = D.InLanguage.begin(),
911 E = D.InLanguage.end(); B != E; ++B)
912 ToolToInLang[D.Name].insert(*B);
913 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000914 }
915}
916
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000917/// TypecheckGraph - Check that names for output and input languages
918/// on all edges do match. This doesn't do much when the information
919/// about the whole graph is not available (i.e. when compiling most
920/// plugins).
921void TypecheckGraph (const RecordVector& EdgeVector,
922 const ToolDescriptions& ToolDescs) {
923 StringMap<StringSet<> > ToolToInLang;
924 StringMap<std::string> ToolToOutLang;
925
926 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
927 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
928 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
929
930 for (RecordVector::const_iterator B = EdgeVector.begin(),
931 E = EdgeVector.end(); B != E; ++B) {
932 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000933 const std::string& NodeA = Edge->getValueAsString("a");
934 const std::string& NodeB = Edge->getValueAsString("b");
935 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
936 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000937
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000938 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000939 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000940 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000941 + ": output->input language mismatch";
942 }
943
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000944 if (NodeB == "root")
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000945 throw std::string("Edges back to the root are not allowed!");
946 }
947}
948
949/// WalkCase - Walks the 'case' expression DAG and invokes
950/// TestCallback on every test, and StatementCallback on every
951/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000952/// combinators (that is, they are passed directly to TestCallback).
953/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
954/// IndentLevel, bool FirstTest)'.
955/// StatementCallback must have type 'void StatementCallback(const Init*,
956/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000957template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000958void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
959 unsigned IndentLevel = 0)
960{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000961 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000962
963 // Error checks.
964 if (GetOperatorName(d) != "case")
965 throw std::string("WalkCase should be invoked only on 'case' expressions!");
966
967 if (d.getNumArgs() < 2)
968 throw "There should be at least one clause in the 'case' expression:\n"
969 + d.getAsString();
970
971 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000972 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000973 const unsigned numArgs = d.getNumArgs();
974 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000975 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
976 B != E; ++B) {
977 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000978
979 if (!even)
980 {
981 // Handle test.
982 const DagInit& Test = InitPtrToDag(arg);
983
984 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
985 throw std::string("The 'default' clause should be the last in the"
986 "'case' construct!");
987 if (i == numArgs)
988 throw "Case construct handler: no corresponding action "
989 "found for the test " + Test.getAsString() + '!';
990
991 TestCallback(&Test, IndentLevel, (i == 1));
992 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000993 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000994 {
995 if (dynamic_cast<DagInit*>(arg)
996 && GetOperatorName(static_cast<DagInit*>(arg)) == "case") {
997 // Nested 'case'.
998 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
999 }
1000
1001 // Handle statement.
1002 StatementCallback(arg, IndentLevel);
1003 }
1004
1005 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001006 even = !even;
1007 }
1008}
1009
1010/// ExtractOptionNames - A helper function object used by
1011/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1012class ExtractOptionNames {
1013 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001014
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001015 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001016 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001017 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001018 if (ActionName == "forward" || ActionName == "forward_as" ||
1019 ActionName == "unpack_values" || ActionName == "switch_on" ||
1020 ActionName == "parameter_equals" || ActionName == "element_in_list" ||
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001021 ActionName == "not_empty" || ActionName == "empty") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001022 checkNumberOfArguments(&Stmt, 1);
1023 const std::string& Name = InitPtrToString(Stmt.getArg(0));
1024 OptionNames_.insert(Name);
1025 }
1026 else if (ActionName == "and" || ActionName == "or") {
1027 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001028 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001029 }
1030 }
1031 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001032
1033public:
1034 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1035 {}
1036
1037 void operator()(const Init* Statement) {
1038 if (typeid(*Statement) == typeid(ListInit)) {
1039 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1040 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1041 B != E; ++B)
1042 this->processDag(*B);
1043 }
1044 else {
1045 this->processDag(Statement);
1046 }
1047 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001048
1049 void operator()(const DagInit* Test, unsigned, bool) {
1050 this->operator()(Test);
1051 }
1052 void operator()(const Init* Statement, unsigned) {
1053 this->operator()(Statement);
1054 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001055};
1056
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001057/// CheckForSuperfluousOptions - Check that there are no side
1058/// effect-free options (specified only in the OptionList). Otherwise,
1059/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001060void CheckForSuperfluousOptions (const RecordVector& Edges,
1061 const ToolDescriptions& ToolDescs,
1062 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001063 llvm::StringSet<> nonSuperfluousOptions;
1064
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001065 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001066 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001067 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1068 E = ToolDescs.end(); B != E; ++B) {
1069 const ToolDescription& TD = *(*B);
1070 ExtractOptionNames Callback(nonSuperfluousOptions);
1071 if (TD.Actions)
1072 WalkCase(TD.Actions, Callback, Callback);
1073 }
1074
1075 // Add all options mentioned in the 'case' clauses of the
1076 // OptionalEdges of the compilation graph to the set of
1077 // non-superfluous options.
1078 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1079 B != E; ++B) {
1080 const Record* Edge = *B;
1081 DagInit* Weight = Edge->getValueAsDag("weight");
1082
1083 if (!isDagEmpty(Weight))
1084 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001085 }
1086
1087 // Check that all options in OptDescs belong to the set of
1088 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001089 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001090 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001091 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001092 if (!nonSuperfluousOptions.count(Val.Name)
1093 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001094 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001095 "Probable cause: this option is specified only in the OptionList.\n";
1096 }
1097}
1098
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001099/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1100bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1101 if (TestName == "single_input_file") {
1102 O << "InputFilenames.size() == 1";
1103 return true;
1104 }
1105 else if (TestName == "multiple_input_files") {
1106 O << "InputFilenames.size() > 1";
1107 return true;
1108 }
1109
1110 return false;
1111}
1112
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001113/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1114template <typename F>
1115void EmitListTest(const ListInit& L, const char* LogicOp,
1116 F Callback, raw_ostream& O)
1117{
1118 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1119 // of Dags...
1120 bool isFirst = true;
1121 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1122 if (isFirst)
1123 isFirst = false;
1124 else
1125 O << " || ";
1126 Callback(InitPtrToString(*B), O);
1127 }
1128}
1129
1130// Callbacks for use with EmitListTest.
1131
1132class EmitSwitchOn {
1133 const OptionDescriptions& OptDescs_;
1134public:
1135 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1136 {}
1137
1138 void operator()(const std::string& OptName, raw_ostream& O) const {
1139 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1140 O << OptDesc.GenVariableName();
1141 }
1142};
1143
1144class EmitEmptyTest {
1145 bool EmitNegate_;
1146 const OptionDescriptions& OptDescs_;
1147public:
1148 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1149 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1150 {}
1151
1152 void operator()(const std::string& OptName, raw_ostream& O) const {
1153 const char* Neg = (EmitNegate_ ? "!" : "");
1154 if (OptName == "o") {
1155 O << Neg << "OutputFilename.empty()";
1156 }
1157 else {
1158 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1159 O << Neg << OptDesc.GenVariableName() << ".empty()";
1160 }
1161 }
1162};
1163
1164
1165/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1166bool EmitCaseTest1ArgList(const std::string& TestName,
1167 const DagInit& d,
1168 const OptionDescriptions& OptDescs,
1169 raw_ostream& O) {
1170 const ListInit& L = *static_cast<ListInit*>(d.getArg(0));
1171
1172 if (TestName == "any_switch_on") {
1173 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1174 return true;
1175 }
1176 else if (TestName == "switch_on") {
1177 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1178 return true;
1179 }
1180 else if (TestName == "any_not_empty") {
1181 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1182 return true;
1183 }
1184 else if (TestName == "any_empty") {
1185 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1186 return true;
1187 }
1188 else if (TestName == "not_empty") {
1189 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1190 return true;
1191 }
1192 else if (TestName == "empty") {
1193 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1194 return true;
1195 }
1196
1197 return false;
1198}
1199
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001200/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1201bool EmitCaseTest1ArgStr(const std::string& TestName,
1202 const DagInit& d,
1203 const OptionDescriptions& OptDescs,
1204 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001205 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001206
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001207 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001208 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001209 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001210 }
1211 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001212 O << "InLangs.count(\"" << OptName << "\") != 0";
1213 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001214 }
1215 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001216 // This works only for single-argument Tool::GenerateAction. Join
1217 // tools can process several files in different languages simultaneously.
1218
1219 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001220 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001221 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001222 }
1223 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001224 bool EmitNegate = (TestName == "not_empty");
1225 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001226 return true;
1227 }
1228
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001229 return false;
1230}
1231
1232/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1233bool EmitCaseTest1Arg(const std::string& TestName,
1234 const DagInit& d,
1235 const OptionDescriptions& OptDescs,
1236 raw_ostream& O) {
1237 checkNumberOfArguments(&d, 1);
1238 if (typeid(*d.getArg(0)) == typeid(ListInit))
1239 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1240 else
1241 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1242}
1243
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001244/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001245bool EmitCaseTest2Args(const std::string& TestName,
1246 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001247 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001248 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001249 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001250 checkNumberOfArguments(&d, 2);
1251 const std::string& OptName = InitPtrToString(d.getArg(0));
1252 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001253
1254 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001255 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001256 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1257 return true;
1258 }
1259 else if (TestName == "element_in_list") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001260 const OptionDescription& OptDesc = OptDescs.FindList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001261 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001262 O << "std::find(" << VarName << ".begin(),\n";
1263 O.indent(IndentLevel + Indent1)
1264 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001265 << OptArg << "\") != " << VarName << ".end()";
1266 return true;
1267 }
1268
1269 return false;
1270}
1271
1272// Forward declaration.
1273// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001274void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001275 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001276 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001277
1278/// EmitLogicalOperationTest - Helper function used by
1279/// EmitCaseConstructHandler.
1280void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001281 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001282 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001283 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001284 O << '(';
1285 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001286 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001287 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001288 if (j != NumArgs - 1) {
1289 O << ")\n";
1290 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1291 }
1292 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001293 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001294 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001295 }
1296}
1297
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001298void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001299 const OptionDescriptions& OptDescs, raw_ostream& O)
1300{
1301 checkNumberOfArguments(&d, 1);
1302 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1303 O << "! (";
1304 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1305 O << ")";
1306}
1307
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001308/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001309void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001310 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001311 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001312 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001313
1314 if (TestName == "and")
1315 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1316 else if (TestName == "or")
1317 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001318 else if (TestName == "not")
1319 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001320 else if (EmitCaseTest0Args(TestName, O))
1321 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001322 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1323 return;
1324 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1325 return;
1326 else
1327 throw TestName + ": unknown edge property!";
1328}
1329
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001330
1331/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1332class EmitCaseTestCallback {
1333 bool EmitElseIf_;
1334 const OptionDescriptions& OptDescs_;
1335 raw_ostream& O_;
1336public:
1337
1338 EmitCaseTestCallback(bool EmitElseIf,
1339 const OptionDescriptions& OptDescs, raw_ostream& O)
1340 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1341 {}
1342
1343 void operator()(const DagInit* Test, unsigned IndentLevel, bool FirstTest)
1344 {
1345 if (GetOperatorName(Test) == "default") {
1346 O_.indent(IndentLevel) << "else {\n";
1347 }
1348 else {
1349 O_.indent(IndentLevel)
1350 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
1351 EmitCaseTest(*Test, IndentLevel, OptDescs_, O_);
1352 O_ << ") {\n";
1353 }
1354 }
1355};
1356
1357/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1358template <typename F>
1359class EmitCaseStatementCallback {
1360 F Callback_;
1361 raw_ostream& O_;
1362public:
1363
1364 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1365 : Callback_(Callback), O_(O)
1366 {}
1367
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001368 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001369
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001370 // Ignore nested 'case' DAG.
1371 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001372 GetOperatorName(static_cast<const DagInit*>(Statement)) == "case")) {
1373 if (typeid(*Statement) == typeid(ListInit)) {
1374 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1375 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1376 B != E; ++B)
1377 Callback_(*B, (IndentLevel + Indent1), O_);
1378 }
1379 else {
1380 Callback_(Statement, (IndentLevel + Indent1), O_);
1381 }
1382 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001383 O_.indent(IndentLevel) << "}\n";
1384 }
1385
1386};
1387
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001388/// EmitCaseConstructHandler - Emit code that handles the 'case'
1389/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001390/// clause. Implemented on top of WalkCase.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001391/// Callback's type is void F(Init* Statement, unsigned IndentLevel,
1392/// raw_ostream& O).
1393/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001394/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1395/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001396template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001397void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001398 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001399 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001400 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001401 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1402 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001403}
1404
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001405/// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to
1406/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path"] .
1407/// Helper function used by EmitCmdLineVecFill and.
1408void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
1409 const char* Delimiters = " \t\n\v\f\r";
1410 enum TokenizerState
1411 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1412 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001413
1414 if (CmdLine.empty())
1415 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001416 Out.push_back("");
1417
1418 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1419 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001420
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001421 for (; B != E; ++B) {
1422 char cur_ch = CmdLine[B];
1423
1424 switch (cur_st) {
1425 case Normal:
1426 if (cur_ch == '$') {
1427 cur_st = SpecialCommand;
1428 break;
1429 }
1430 if (oneOf(Delimiters, cur_ch)) {
1431 // Skip whitespace
1432 B = CmdLine.find_first_not_of(Delimiters, B);
1433 if (B == std::string::npos) {
1434 B = E-1;
1435 continue;
1436 }
1437 --B;
1438 Out.push_back("");
1439 continue;
1440 }
1441 break;
1442
1443
1444 case SpecialCommand:
1445 if (oneOf(Delimiters, cur_ch)) {
1446 cur_st = Normal;
1447 Out.push_back("");
1448 continue;
1449 }
1450 if (cur_ch == '(') {
1451 Out.push_back("");
1452 cur_st = InsideSpecialCommand;
1453 continue;
1454 }
1455 break;
1456
1457 case InsideSpecialCommand:
1458 if (oneOf(Delimiters, cur_ch)) {
1459 continue;
1460 }
1461 if (cur_ch == '\'') {
1462 cur_st = InsideQuotationMarks;
1463 Out.push_back("");
1464 continue;
1465 }
1466 if (cur_ch == ')') {
1467 cur_st = Normal;
1468 Out.push_back("");
1469 }
1470 if (cur_ch == ',') {
1471 continue;
1472 }
1473
1474 break;
1475
1476 case InsideQuotationMarks:
1477 if (cur_ch == '\'') {
1478 cur_st = InsideSpecialCommand;
1479 continue;
1480 }
1481 break;
1482 }
1483
1484 Out.back().push_back(cur_ch);
1485 }
1486}
1487
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001488/// SubstituteSpecialCommands - Perform string substitution for $CALL
1489/// and $ENV. Helper function used by EmitCmdLineVecFill().
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001490StrVector::const_iterator SubstituteSpecialCommands
Daniel Dunbar1a551802009-07-03 00:10:29 +00001491(StrVector::const_iterator Pos, StrVector::const_iterator End, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001492{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001493
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001494 const std::string& cmd = *Pos;
1495
1496 if (cmd == "$CALL") {
1497 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1498 const std::string& CmdName = *Pos;
1499
1500 if (CmdName == ")")
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001501 throw std::string("$CALL invocation: empty argument list!");
1502
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001503 O << "hooks::";
1504 O << CmdName << "(";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001505
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001506
1507 bool firstIteration = true;
1508 while (true) {
1509 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1510 const std::string& Arg = *Pos;
1511 assert(Arg.size() != 0);
1512
1513 if (Arg[0] == ')')
1514 break;
1515
1516 if (firstIteration)
1517 firstIteration = false;
1518 else
1519 O << ", ";
1520
1521 O << '"' << Arg << '"';
1522 }
1523
1524 O << ')';
1525
1526 }
1527 else if (cmd == "$ENV") {
1528 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
1529 const std::string& EnvName = *Pos;
1530
1531 if (EnvName == ")")
1532 throw "$ENV invocation: empty argument list!";
1533
1534 O << "checkCString(std::getenv(\"";
1535 O << EnvName;
1536 O << "\"))";
1537
1538 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001539 }
1540 else {
1541 throw "Unknown special command: " + cmd;
1542 }
1543
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001544 const std::string& Leftover = *Pos;
1545 assert(Leftover.at(0) == ')');
1546 if (Leftover.size() != 1)
1547 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001548
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001549 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001550}
1551
1552/// EmitCmdLineVecFill - Emit code that fills in the command line
1553/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001554void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001555 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001556 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001557 StrVector StrVec;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001558 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1559
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001560 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001561 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001562
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001563 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1564
1565 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001566 assert(!StrVec[0].empty());
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001567 if (StrVec[0][0] == '$') {
1568 while (I != E && (*I)[0] != ')' )
1569 ++I;
1570
1571 // Skip the ')' symbol.
1572 ++I;
1573 }
1574 else {
1575 ++I;
1576 }
1577
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001578 bool hasINFILE = false;
1579
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001580 for (; I != E; ++I) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001581 const std::string& cmd = *I;
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001582 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001583 O.indent(IndentLevel);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001584 if (cmd.at(0) == '$') {
1585 if (cmd == "$INFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001586 hasINFILE = true;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001587 if (IsJoin) {
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001588 O << "for (PathVector::const_iterator B = inFiles.begin()"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001589 << ", E = inFiles.end();\n";
1590 O.indent(IndentLevel) << "B != E; ++B)\n";
1591 O.indent(IndentLevel + Indent1) << "vec.push_back(B->str());\n";
1592 }
1593 else {
Chris Lattner74382b72009-08-23 22:45:37 +00001594 O << "vec.push_back(inFile.str());\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001595 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001596 }
1597 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001598 O << "vec.push_back(\"\");\n";
1599 O.indent(IndentLevel) << "out_file_index = vec.size()-1;\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001600 }
1601 else {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001602 O << "vec.push_back(";
1603 I = SubstituteSpecialCommands(I, E, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001604 O << ");\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001605 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001606 }
1607 else {
1608 O << "vec.push_back(\"" << cmd << "\");\n";
1609 }
1610 }
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001611 if (!hasINFILE)
1612 throw "Tool '" + ToolName + "' doesn't take any input!";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001613
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001614 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001615 if (StrVec[0][0] == '$')
1616 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), O);
1617 else
1618 O << '"' << StrVec[0] << '"';
1619 O << ";\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001620}
1621
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001622/// EmitCmdLineVecFillCallback - A function object wrapper around
1623/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1624/// argument to EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001625class EmitCmdLineVecFillCallback {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001626 bool IsJoin;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001627 const std::string& ToolName;
1628 public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001629 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1630 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001631
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001632 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001633 raw_ostream& O) const
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001634 {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001635 EmitCmdLineVecFill(Statement, ToolName, IsJoin, IndentLevel, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001636 }
1637};
1638
1639/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1640/// implement EmitActionHandler. Emits code for
1641/// handling the (forward) and (forward_as) option properties.
1642void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001643 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001644 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001645 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001646 const std::string& Name = NewName.empty()
1647 ? ("-" + D.Name)
1648 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001649 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001650
1651 switch (D.Type) {
1652 case OptionType::Switch:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001653 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001654 break;
1655 case OptionType::Parameter:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001656 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
1657 O.indent(IndentLevel) << "vec.push_back(" << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001658 break;
1659 case OptionType::Prefix:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001660 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\" + "
1661 << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001662 break;
1663 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001664 O.indent(IndentLevel)
1665 << "for (" << D.GenTypeDeclaration()
1666 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1667 O.indent(IndentLevel)
1668 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1669 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\" + " << "*B);\n";
1670 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001671
1672 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001673 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1674 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001675 }
1676
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001677 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001678 break;
1679 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001680 O.indent(IndentLevel)
1681 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1682 << D.GenVariableName() << ".begin(),\n";
1683 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1684 << ".end() ; B != E;) {\n";
1685 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001686
1687 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001688 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1689 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001690 }
1691
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001692 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001693 break;
1694 case OptionType::Alias:
1695 default:
1696 throw std::string("Aliases are not allowed in tool option descriptions!");
1697 }
1698}
1699
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001700/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1701/// EmitPreprocessOptionsCallback.
1702struct ActionHandlingCallbackBase {
1703
1704 void onErrorDag(const DagInit& d,
1705 unsigned IndentLevel, raw_ostream& O) const
1706 {
1707 O.indent(IndentLevel)
1708 << "throw std::runtime_error(\"" <<
1709 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1710 : "Unknown error!")
1711 << "\");\n";
1712 }
1713
1714 void onWarningDag(const DagInit& d,
1715 unsigned IndentLevel, raw_ostream& O) const
1716 {
1717 checkNumberOfArguments(&d, 1);
1718 O.indent(IndentLevel) << "llvm::errs() << \""
1719 << InitPtrToString(d.getArg(0)) << "\";\n";
1720 }
1721
1722};
1723
1724/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1725/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
1726class EmitActionHandlersCallback : ActionHandlingCallbackBase {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001727 const OptionDescriptions& OptDescs;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001728
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001729 void processActionDag(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001730 raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001731 {
1732 const DagInit& Dag = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001733 const std::string& ActionName = GetOperatorName(Dag);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001734
1735 if (ActionName == "append_cmd") {
1736 checkNumberOfArguments(&Dag, 1);
1737 const std::string& Cmd = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001738 StrVector Out;
1739 llvm::SplitString(Cmd, Out);
1740
1741 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1742 B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001743 O.indent(IndentLevel) << "vec.push_back(\"" << *B << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001744 }
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001745 else if (ActionName == "error") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001746 this->onErrorDag(Dag, IndentLevel, O);
1747 }
1748 else if (ActionName == "warning") {
1749 this->onWarningDag(Dag, IndentLevel, O);
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001750 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001751 else if (ActionName == "forward") {
1752 checkNumberOfArguments(&Dag, 1);
1753 const std::string& Name = InitPtrToString(Dag.getArg(0));
1754 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1755 IndentLevel, "", O);
1756 }
1757 else if (ActionName == "forward_as") {
1758 checkNumberOfArguments(&Dag, 2);
1759 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkove89331b2009-05-06 01:41:19 +00001760 const std::string& NewName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001761 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1762 IndentLevel, NewName, O);
1763 }
1764 else if (ActionName == "output_suffix") {
1765 checkNumberOfArguments(&Dag, 1);
1766 const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001767 O.indent(IndentLevel) << "output_suffix = \"" << OutSuf << "\";\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001768 }
1769 else if (ActionName == "stop_compilation") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001770 O.indent(IndentLevel) << "stop_compilation = true;\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001771 }
1772 else if (ActionName == "unpack_values") {
1773 checkNumberOfArguments(&Dag, 1);
1774 const std::string& Name = InitPtrToString(Dag.getArg(0));
1775 const OptionDescription& D = OptDescs.FindOption(Name);
1776
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001777 if (D.isMultiVal())
1778 throw std::string("Can't use unpack_values with multi-valued options!");
1779
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001780 if (D.isList()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001781 O.indent(IndentLevel)
1782 << "for (" << D.GenTypeDeclaration()
1783 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1784 O.indent(IndentLevel)
1785 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n";
1786 O.indent(IndentLevel + Indent1)
1787 << "llvm::SplitString(*B, vec, \",\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001788 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001789 else if (D.isParameter()){
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001790 O.indent(IndentLevel) << "llvm::SplitString("
1791 << D.GenVariableName() << ", vec, \",\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001792 }
1793 else {
1794 throw "Option '" + D.Name +
1795 "': switches can't have the 'unpack_values' property!";
1796 }
1797 }
1798 else {
1799 throw "Unknown action name: " + ActionName + "!";
1800 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001801 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001802 public:
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001803 EmitActionHandlersCallback(const OptionDescriptions& OD)
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001804 : OptDescs(OD) {}
1805
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001806 void operator()(const Init* Statement,
1807 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001808 {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001809 this->processActionDag(Statement, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001810 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001811};
1812
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001813bool IsOutFileIndexCheckRequiredStr (const Init* CmdLine) {
1814 StrVector StrVec;
1815 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1816
1817 for (StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1818 I != E; ++I) {
1819 if (*I == "$OUTFILE")
1820 return false;
1821 }
1822
1823 return true;
1824}
1825
1826class IsOutFileIndexCheckRequiredStrCallback {
1827 bool* ret_;
1828
1829public:
1830 IsOutFileIndexCheckRequiredStrCallback(bool* ret) : ret_(ret)
1831 {}
1832
1833 void operator()(const Init* CmdLine) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001834 // Ignore nested 'case' DAG.
1835 if (typeid(*CmdLine) == typeid(DagInit))
1836 return;
1837
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001838 if (IsOutFileIndexCheckRequiredStr(CmdLine))
1839 *ret_ = true;
1840 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001841
1842 void operator()(const DagInit* Test, unsigned, bool) {
1843 this->operator()(Test);
1844 }
1845 void operator()(const Init* Statement, unsigned) {
1846 this->operator()(Statement);
1847 }
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001848};
1849
1850bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
1851 bool ret = false;
1852 WalkCase(CmdLine, Id(), IsOutFileIndexCheckRequiredStrCallback(&ret));
1853 return ret;
1854}
1855
1856/// IsOutFileIndexCheckRequired - Should we emit an "out_file_index != -1" check
1857/// in EmitGenerateActionMethod() ?
1858bool IsOutFileIndexCheckRequired (Init* CmdLine) {
1859 if (typeid(*CmdLine) == typeid(StringInit))
1860 return IsOutFileIndexCheckRequiredStr(CmdLine);
1861 else
1862 return IsOutFileIndexCheckRequiredCase(CmdLine);
1863}
1864
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001865// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001866// Tool::GenerateAction() method.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001867void EmitGenerateActionMethod (const ToolDescription& D,
1868 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001869 bool IsJoin, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001870 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001871 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001872 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001873 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001874
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001875 O.indent(Indent2) << "bool HasChildren,\n";
1876 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1877 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1878 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1879 O.indent(Indent1) << "{\n";
1880 O.indent(Indent2) << "std::string cmd;\n";
1881 O.indent(Indent2) << "std::vector<std::string> vec;\n";
1882 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
1883 O.indent(Indent2) << "const char* output_suffix = \""
1884 << D.OutputSuffix << "\";\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001885
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001886 if (!D.CmdLine)
1887 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001888
1889 bool IndexCheckRequired = IsOutFileIndexCheckRequired(D.CmdLine);
1890 O.indent(Indent2) << "int out_file_index"
1891 << (IndexCheckRequired ? " = -1" : "")
1892 << ";\n\n";
1893
1894 // Process the cmd_line property.
1895 if (typeid(*D.CmdLine) == typeid(StringInit))
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001896 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
1897 else
1898 EmitCaseConstructHandler(D.CmdLine, Indent2,
1899 EmitCmdLineVecFillCallback(IsJoin, D.Name),
1900 true, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001901
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001902 // For every understood option, emit handling code.
1903 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00001904 EmitCaseConstructHandler(D.Actions, Indent2,
1905 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001906 false, OptDescs, O);
1907
1908 O << '\n';
1909 O.indent(Indent2)
1910 << "std::string out_file = OutFilename("
1911 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
1912 O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001913
1914 if (IndexCheckRequired)
1915 O.indent(Indent2) << "if (out_file_index != -1)\n";
1916 O.indent(IndexCheckRequired ? Indent3 : Indent2)
1917 << "vec[out_file_index] = out_file;\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001918
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001919 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001920 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001921 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
1922 O.indent(Indent3) << "vec.insert(vec.end(), "
1923 << SinkOptionName << ".begin(), " << SinkOptionName
1924 << ".end());\n";
1925 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001926 }
1927
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001928 O.indent(Indent2) << "return Action(cmd, vec, stop_compilation, out_file);\n";
1929 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001930}
1931
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001932/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1933/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001934void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
1935 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001936 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001937 if (!ToolDesc.isJoin()) {
1938 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
1939 O.indent(Indent2) << "bool HasChildren,\n";
1940 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1941 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1942 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1943 O.indent(Indent1) << "{\n";
1944 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
1945 << " is not a Join tool!\");\n";
1946 O.indent(Indent1) << "}\n\n";
1947 }
1948 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001949 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001950 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001951
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001952 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001953}
1954
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001955/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1956/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001957void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001958 O.indent(Indent1) << "const char** InputLanguages() const {\n";
1959 O.indent(Indent2) << "return InputLanguages_;\n";
1960 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001961
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001962 if (D.OutLanguage.empty())
1963 throw "Tool " + D.Name + " has no 'out_language' property!";
1964
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001965 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
1966 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
1967 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001968}
1969
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001970/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001971void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001972 O.indent(Indent1) << "const char* Name() const {\n";
1973 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
1974 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001975}
1976
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001977/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1978/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001979void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001980 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001981 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001982 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001983 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001984 O.indent(Indent2) << "return false;\n";
1985 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001986}
1987
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001988/// EmitStaticMemberDefinitions - Emit static member definitions for a
1989/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001990void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001991 if (D.InLanguage.empty())
1992 throw "Tool " + D.Name + " has no 'in_language' property!";
1993
1994 O << "const char* " << D.Name << "::InputLanguages_[] = {";
1995 for (StrVector::const_iterator B = D.InLanguage.begin(),
1996 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001997 O << '\"' << *B << "\", ";
1998 O << "0};\n\n";
1999}
2000
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002001/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002002void EmitToolClassDefinition (const ToolDescription& D,
2003 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002004 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002005 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002006 return;
2007
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002008 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002009 O << "class " << D.Name << " : public ";
2010 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002011 O << "JoinTool";
2012 else
2013 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002014
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002015 O << "{\nprivate:\n";
2016 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002017
2018 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002019 EmitNameMethod(D, O);
2020 EmitInOutLanguageMethods(D, O);
2021 EmitIsJoinMethod(D, O);
2022 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002023
2024 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002025 O << "};\n";
2026
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002027 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002028
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002029}
2030
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002031/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002032/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002033void EmitOptionDefinitions (const OptionDescriptions& descs,
2034 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002035 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002036{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002037 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002038
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002039 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002040 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002041 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002042 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002043
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002044 if (val.Type == OptionType::Alias) {
2045 Aliases.push_back(val);
2046 continue;
2047 }
2048
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002049 if (val.isExtern())
2050 O << "extern ";
2051
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002052 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002053 << val.GenVariableName();
2054
2055 if (val.isExtern()) {
2056 O << ";\n";
2057 continue;
2058 }
2059
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002060 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002061
2062 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2063 O << ", cl::Prefix";
2064
2065 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002066 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002067 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002068 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002069 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002070 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002071 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002072 O << ", cl::OneOrMore";
2073 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002074 else if (val.isZeroOrOne() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002075 O << ", cl::ZeroOrOne";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002076 }
2077
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002078 if (val.isReallyHidden()) {
2079 O << ", cl::ReallyHidden";
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002080 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002081 else if (val.isHidden()) {
2082 O << ", cl::Hidden";
2083 }
2084
2085 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002086 O << ", cl::multi_val(" << val.MultiVal << ')';
2087
2088 if (val.InitVal) {
2089 const std::string& str = val.InitVal->getAsString();
2090 O << ", cl::init(" << str << ')';
2091 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002092
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002093 if (!val.Help.empty())
2094 O << ", cl::desc(\"" << val.Help << "\")";
2095
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002096 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002097 }
2098
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002099 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002100 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002101 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002102 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002103
2104 O << val.GenTypeDeclaration() << ' '
2105 << val.GenVariableName()
2106 << "(\"" << val.Name << '\"';
2107
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002108 const OptionDescription& D = descs.FindOption(val.Help);
2109 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002110
2111 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2112 }
2113
2114 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002115 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002116 O << (HasExterns ? "extern cl" : "cl")
2117 << "::list<std::string> " << SinkOptionName
2118 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002119
2120 O << '\n';
2121}
2122
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002123/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002124/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002125class EmitPreprocessOptionsCallback : ActionHandlingCallbackBase {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002126 const OptionDescriptions& OptDescs_;
2127
2128 void onUnsetOption(Init* i, unsigned IndentLevel, raw_ostream& O) {
2129 const std::string& OptName = InitPtrToString(i);
2130 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002131
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002132 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002133 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2134 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002135 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002136 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2137 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002138 else if (OptDesc.isList()) {
2139 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2140 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002141 else {
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002142 throw "Can't apply 'unset_option' to alias option '" + OptName + "'";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002143 }
2144 }
2145
2146 void processDag(const Init* I, unsigned IndentLevel, raw_ostream& O)
2147 {
2148 const DagInit& d = InitPtrToDag(I);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002149 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002150
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002151 if (OpName == "warning") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002152 this->onWarningDag(d, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002153 }
2154 else if (OpName == "error") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002155 this->onWarningDag(d, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002156 }
2157 else if (OpName == "unset_option") {
2158 checkNumberOfArguments(&d, 1);
2159 Init* I = d.getArg(0);
2160 if (typeid(*I) == typeid(ListInit)) {
2161 const ListInit& DagList = *static_cast<const ListInit*>(I);
2162 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
2163 B != E; ++B)
2164 this->onUnsetOption(*B, IndentLevel, O);
2165 }
2166 else {
2167 this->onUnsetOption(I, IndentLevel, O);
2168 }
2169 }
2170 else {
2171 throw "Unknown operator in the option preprocessor: '" + OpName + "'!"
2172 "\nOnly 'warning', 'error' and 'unset_option' are allowed.";
2173 }
2174 }
2175
2176public:
2177
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002178 void operator()(const Init* I, unsigned IndentLevel, raw_ostream& O) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002179 this->processDag(I, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002180 }
2181
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002182 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002183 : OptDescs_(OptDescs)
2184 {}
2185};
2186
2187/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2188void EmitPreprocessOptions (const RecordKeeper& Records,
2189 const OptionDescriptions& OptDecs, raw_ostream& O)
2190{
2191 O << "void PreprocessOptionsLocal() {\n";
2192
2193 const RecordVector& OptionPreprocessors =
2194 Records.getAllDerivedDefinitions("OptionPreprocessor");
2195
2196 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2197 E = OptionPreprocessors.end(); B!=E; ++B) {
2198 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002199 EmitCaseConstructHandler(Case, Indent1,
2200 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002201 false, OptDecs, O);
2202 }
2203
2204 O << "}\n\n";
2205}
2206
2207/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002208void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002209{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002210 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002211
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002212 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002213 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002214
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002215 // It is allowed for a plugin to have no language map.
2216 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002217
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002218 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2219 if (!LangsToSuffixesList)
2220 throw std::string("Error in the language map definition!");
2221
2222 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002223 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002224
2225 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2226 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2227
2228 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002229 O.indent(Indent1) << "langMap[\""
2230 << InitPtrToString(Suffixes->getElement(i))
2231 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002232 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002233 }
2234
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002235 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002236}
2237
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002238/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2239/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002240void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002241 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002242 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002243 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002244
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002245 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002246 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002247 }
2248 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002249 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002250 }
2251 else if (OpName == "error") {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002252 checkNumberOfArguments(&d, 1);
2253 O.indent(IndentLevel) << "throw std::runtime_error(\""
2254 << InitPtrToString(d.getArg(0))
2255 << "\");\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002256 return;
2257 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002258 else {
2259 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002260 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002261 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002262
2263 if (d.getNumArgs() > 0)
2264 O << InitPtrToInt(d.getArg(0)) << ";\n";
2265 else
2266 O << "2;\n";
2267
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002268}
2269
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002270/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002271void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002272 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002273 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002274
2275 // Class constructor.
2276 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002277 << "public:\n";
2278 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2279 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002280
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002281 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002282 O.indent(Indent1)
2283 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2284 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002285
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002286 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002287 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002288
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002289 O.indent(Indent2) << "return ret;\n";
2290 O.indent(Indent1) << "};\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002291}
2292
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002293/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002294void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002295 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002296 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002297 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002298 for (RecordVector::const_iterator B = EdgeVector.begin(),
2299 E = EdgeVector.end(); B != E; ++B) {
2300 const Record* Edge = *B;
2301 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002302 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002303
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002304 if (!isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002305 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002306 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002307 }
2308}
2309
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002310/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002311/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002312void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002313 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002314 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002315{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002316 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002317
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002318 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2319 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002320 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002321
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002322 O << '\n';
2323
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002324 // Insert edges.
2325
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002326 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002327 for (RecordVector::const_iterator B = EdgeVector.begin(),
2328 E = EdgeVector.end(); B != E; ++B) {
2329 const Record* Edge = *B;
2330 const std::string& NodeA = Edge->getValueAsString("a");
2331 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002332 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002333
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002334 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002335
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002336 if (isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002337 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002338 else
2339 O << "new Edge" << i << "()";
2340
2341 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002342 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002343 }
2344
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002345 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002346}
2347
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002348/// ExtractHookNames - Extract the hook names from all instances of
2349/// $CALL(HookName) in the provided command line string. Helper
2350/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002351class ExtractHookNames {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002352 llvm::StringMap<unsigned>& HookNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002353public:
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002354 ExtractHookNames(llvm::StringMap<unsigned>& HookNames)
2355 : HookNames_(HookNames) {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002356
2357 void operator()(const Init* CmdLine) {
2358 StrVector cmds;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002359
2360 // Ignore nested 'case' DAG.
2361 if (typeid(*CmdLine) == typeid(DagInit))
2362 return;
2363
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002364 TokenizeCmdline(InitPtrToString(CmdLine), cmds);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002365 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2366 B != E; ++B) {
2367 const std::string& cmd = *B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002368
2369 if (cmd == "$CALL") {
2370 unsigned NumArgs = 0;
2371 checkedIncrement(B, E, "Syntax error in $CALL invocation!");
2372 const std::string& HookName = *B;
2373
2374
2375 if (HookName.at(0) == ')')
2376 throw "$CALL invoked with no arguments!";
2377
2378 while (++B != E && B->at(0) != ')') {
2379 ++NumArgs;
2380 }
2381
2382 StringMap<unsigned>::const_iterator H = HookNames_.find(HookName);
2383
2384 if (H != HookNames_.end() && H->second != NumArgs)
2385 throw "Overloading of hooks is not allowed. Overloaded hook: "
2386 + HookName;
2387 else
2388 HookNames_[HookName] = NumArgs;
2389
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002390 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002391 }
2392 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002393
2394 void operator()(const DagInit* Test, unsigned, bool) {
2395 this->operator()(Test);
2396 }
2397 void operator()(const Init* Statement, unsigned) {
2398 this->operator()(Statement);
2399 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002400};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002401
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002402/// FillInHookNames - Actually extract the hook names from all command
2403/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002404void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002405 llvm::StringMap<unsigned>& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002406{
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002407 // For all command lines:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002408 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2409 E = ToolDescs.end(); B != E; ++B) {
2410 const ToolDescription& D = *(*B);
2411 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002412 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002413 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002414 // This is a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002415 ExtractHookNames(HookNames).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002416 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002417 // This is a 'case' construct.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002418 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002419 }
2420}
2421
2422/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2423/// property records and emit hook function declaration for each
2424/// instance of $CALL(HookName).
Daniel Dunbar1a551802009-07-03 00:10:29 +00002425void EmitHookDeclarations(const ToolDescriptions& ToolDescs, raw_ostream& O) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002426 llvm::StringMap<unsigned> HookNames;
2427
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002428 FillInHookNames(ToolDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002429 if (HookNames.empty())
2430 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002431
2432 O << "namespace hooks {\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002433 for (StringMap<unsigned>::const_iterator B = HookNames.begin(),
2434 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002435 O.indent(Indent1) << "std::string " << B->first() << "(";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002436
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002437 for (unsigned i = 0, j = B->second; i < j; ++i) {
2438 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2439 }
2440
2441 O <<");\n";
2442 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002443 O << "}\n\n";
2444}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002445
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002446/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002447void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002448 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2449 O.indent(Indent1) << "int Priority() const { return "
2450 << Priority << "; }\n\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002451 O.indent(Indent1) << "void PreprocessOptions() const\n";
2452 O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002453 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2454 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2455 O.indent(Indent1)
2456 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2457 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2458 << "};\n\n"
2459 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002460}
2461
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002462/// EmitIncludes - Emit necessary #include directives and some
2463/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002464void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002465 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2466 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002467 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002468 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2469 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002470
2471 << "#include \"llvm/ADT/StringExtras.h\"\n"
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002472 << "#include \"llvm/Support/CommandLine.h\"\n"
2473 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002474
2475 << "#include <cstdlib>\n"
2476 << "#include <stdexcept>\n\n"
2477
2478 << "using namespace llvm;\n"
2479 << "using namespace llvmc;\n\n"
2480
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002481 << "extern cl::opt<std::string> OutputFilename;\n\n"
2482
2483 << "inline const char* checkCString(const char* s)\n"
2484 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002485}
2486
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002487
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002488/// PluginData - Holds all information about a plugin.
2489struct PluginData {
2490 OptionDescriptions OptDescs;
2491 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002492 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002493 ToolDescriptions ToolDescs;
2494 RecordVector Edges;
2495 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002496};
2497
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002498/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002499/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002500bool HasSink(const ToolDescriptions& ToolDescs) {
2501 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2502 E = ToolDescs.end(); B != E; ++B)
2503 if ((*B)->isSink())
2504 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002505
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002506 return false;
2507}
2508
2509/// HasExterns - Go through the list of option descriptions and check
2510/// if there are any external options.
2511bool HasExterns(const OptionDescriptions& OptDescs) {
2512 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2513 E = OptDescs.end(); B != E; ++B)
2514 if (B->second.isExtern())
2515 return true;
2516
2517 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002518}
2519
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002520/// CollectPluginData - Collect tool and option properties,
2521/// compilation graph edges and plugin priority from the parse tree.
2522void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2523 // Collect option properties.
2524 const RecordVector& OptionLists =
2525 Records.getAllDerivedDefinitions("OptionList");
2526 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2527 Data.OptDescs);
2528
2529 // Collect tool properties.
2530 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2531 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2532 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002533 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002534
2535 // Collect compilation graph edges.
2536 const RecordVector& CompilationGraphs =
2537 Records.getAllDerivedDefinitions("CompilationGraph");
2538 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2539 Data.Edges);
2540
2541 // Calculate the priority of this plugin.
2542 const RecordVector& Priorities =
2543 Records.getAllDerivedDefinitions("PluginPriority");
2544 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002545}
2546
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002547/// CheckPluginData - Perform some sanity checks on the collected data.
2548void CheckPluginData(PluginData& Data) {
2549 // Filter out all tools not mentioned in the compilation graph.
2550 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002551
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002552 // Typecheck the compilation graph.
2553 TypecheckGraph(Data.Edges, Data.ToolDescs);
2554
2555 // Check that there are no options without side effects (specified
2556 // only in the OptionList).
2557 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2558
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002559}
2560
Daniel Dunbar1a551802009-07-03 00:10:29 +00002561void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002562 // Emit file header.
2563 EmitIncludes(O);
2564
2565 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002566 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002567
2568 // Emit hook declarations.
2569 EmitHookDeclarations(Data.ToolDescs, O);
2570
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002571 O << "namespace {\n\n";
2572
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002573 // Emit PreprocessOptionsLocal() function.
2574 EmitPreprocessOptions(Records, Data.OptDescs, O);
2575
2576 // Emit PopulateLanguageMapLocal() function
2577 // (language map maps from file extensions to language names).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002578 EmitPopulateLanguageMap(Records, O);
2579
2580 // Emit Tool classes.
2581 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2582 E = Data.ToolDescs.end(); B!=E; ++B)
2583 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2584
2585 // Emit Edge# classes.
2586 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2587
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002588 // Emit PopulateCompilationGraphLocal() function.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002589 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2590
2591 // Emit code for plugin registration.
2592 EmitRegisterPlugin(Data.Priority, O);
2593
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002594 O << "} // End anonymous namespace.\n\n";
2595
2596 // Force linkage magic.
2597 O << "namespace llvmc {\n";
2598 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2599 O << "}\n";
2600
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002601 // EOF
2602}
2603
2604
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002605// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00002606}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002607
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002608/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002609void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002610 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002611 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002612
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002613 CollectPluginData(Records, Data);
2614 CheckPluginData(Data);
2615
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +00002616 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002617 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002618
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002619 } catch (std::exception& Error) {
2620 throw Error.what() + std::string(" - usually this means a syntax error.");
2621 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002622}