blob: 8b55b81eae46dd9910c2f2b4f6a86248e1511690 [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
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
32//===----------------------------------------------------------------------===//
33/// Typedefs
34
35typedef std::vector<Record*> RecordVector;
36typedef std::vector<std::string> StrVector;
37
38//===----------------------------------------------------------------------===//
39/// Constants
40
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +000041// Indentation.
Chris Lattnerec8e1b72009-11-03 18:30:31 +000042static const unsigned TabWidth = 4;
43static const unsigned Indent1 = TabWidth*1;
44static const unsigned Indent2 = TabWidth*2;
45static const unsigned Indent3 = TabWidth*3;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000046
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000047// Default help string.
Chris Lattnerec8e1b72009-11-03 18:30:31 +000048static const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000049
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000050// Name for the "sink" option.
Chris Lattnerec8e1b72009-11-03 18:30:31 +000051static const char * const SinkOptionName = "AutoGeneratedSinkOption";
52
53namespace {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000054
55//===----------------------------------------------------------------------===//
56/// Helper functions
57
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000058/// Id - An 'identity' function object.
59struct Id {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000060 template<typename T0>
61 void operator()(const T0&) const {
62 }
63 template<typename T0, typename T1>
64 void operator()(const T0&, const T1&) const {
65 }
66 template<typename T0, typename T1, typename T2>
67 void operator()(const T0&, const T1&, const T2&) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000068 }
69};
70
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000071int InitPtrToInt(const Init* ptr) {
72 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000073 return val.getValue();
74}
75
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000076const std::string& InitPtrToString(const Init* ptr) {
77 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
78 return val.getValue();
79}
80
81const ListInit& InitPtrToList(const Init* ptr) {
82 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
83 return val;
84}
85
86const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000087 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000088 return val;
89}
90
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000091const std::string GetOperatorName(const DagInit* D) {
92 return D->getOperator()->getAsString();
93}
94
95const std::string GetOperatorName(const DagInit& D) {
96 return GetOperatorName(&D);
97}
98
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000099// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +0000100// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000101void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000102 if (!d || d->getNumArgs() < min_arguments)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000103 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000104}
105
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000106// isDagEmpty - is this DAG marked with an empty marker?
107bool isDagEmpty (const DagInit* d) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000108 return GetOperatorName(d) == "empty_dag_marker";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000109}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000110
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000111// EscapeVariableName - Escape commas and other symbols not allowed
112// in the C++ variable names. Makes it possible to use options named
113// like "Wa," (useful for prefix options).
114std::string EscapeVariableName(const std::string& Var) {
115 std::string ret;
116 for (unsigned i = 0; i != Var.size(); ++i) {
117 char cur_char = Var[i];
118 if (cur_char == ',') {
119 ret += "_comma_";
120 }
121 else if (cur_char == '+') {
122 ret += "_plus_";
123 }
124 else if (cur_char == '-') {
125 ret += "_dash_";
126 }
127 else {
128 ret.push_back(cur_char);
129 }
130 }
131 return ret;
132}
133
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000134/// oneOf - Does the input string contain this character?
135bool oneOf(const char* lst, char c) {
136 while (*lst) {
137 if (*lst++ == c)
138 return true;
139 }
140 return false;
141}
142
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000143template <class I, class S>
144void checkedIncrement(I& P, I E, S ErrorString) {
145 ++P;
146 if (P == E)
147 throw ErrorString;
148}
149
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000150// apply is needed because C++'s syntax doesn't let us construct a function
151// object and call it in the same statement.
152template<typename F, typename T0>
153void apply(F Fun, T0& Arg0) {
154 return Fun(Arg0);
155}
156
157template<typename F, typename T0, typename T1>
158void apply(F Fun, T0& Arg0, T1& Arg1) {
159 return Fun(Arg0, Arg1);
160}
161
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000162//===----------------------------------------------------------------------===//
163/// Back-end specific code
164
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000165
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000166/// OptionType - One of six different option types. See the
167/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000168namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000169
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000170 enum OptionType { Alias, Switch, Parameter, ParameterList,
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000171 Prefix, PrefixList};
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000172
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000173 bool IsAlias(OptionType t) {
174 return (t == Alias);
175 }
176
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000177 bool IsList (OptionType t) {
178 return (t == ParameterList || t == PrefixList);
179 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000180
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000181 bool IsSwitch (OptionType t) {
182 return (t == Switch);
183 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000184
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000185 bool IsParameter (OptionType t) {
186 return (t == Parameter || t == Prefix);
187 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000188
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000189}
190
191OptionType::OptionType stringToOptionType(const std::string& T) {
192 if (T == "alias_option")
193 return OptionType::Alias;
194 else if (T == "switch_option")
195 return OptionType::Switch;
196 else if (T == "parameter_option")
197 return OptionType::Parameter;
198 else if (T == "parameter_list_option")
199 return OptionType::ParameterList;
200 else if (T == "prefix_option")
201 return OptionType::Prefix;
202 else if (T == "prefix_list_option")
203 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000204 else
205 throw "Unknown option type: " + T + '!';
206}
207
208namespace OptionDescriptionFlags {
209 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000210 ReallyHidden = 0x4, Extern = 0x8,
211 OneOrMore = 0x10, ZeroOrOne = 0x20 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000212}
213
214/// OptionDescription - Represents data contained in a single
215/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000216struct OptionDescription {
217 OptionType::OptionType Type;
218 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000219 unsigned Flags;
220 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000221 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000222 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000223
224 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000225 const std::string& n = "",
226 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000227 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000228 {}
229
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000230 /// GenTypeDeclaration - Returns the C++ variable type of this
231 /// option.
232 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000233
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000234 /// GenVariableName - Returns the variable name used in the
235 /// generated C++ code.
236 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000237
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000238 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000239 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000240
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000241 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000242
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000243 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000244
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000245 bool isMultiVal() const;
246
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000247 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000248 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000249
250 bool isRequired() const;
251 void setRequired();
252
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000253 bool isOneOrMore() const;
254 void setOneOrMore();
255
256 bool isZeroOrOne() const;
257 void setZeroOrOne();
258
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000259 bool isHidden() const;
260 void setHidden();
261
262 bool isReallyHidden() const;
263 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000264
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000265 bool isSwitch() const
266 { return OptionType::IsSwitch(this->Type); }
267
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000268 bool isParameter() const
269 { return OptionType::IsParameter(this->Type); }
270
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000271 bool isList() const
272 { return OptionType::IsList(this->Type); }
273
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000274};
275
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000276void OptionDescription::Merge (const OptionDescription& other)
277{
278 if (other.Type != Type)
279 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000280
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000281 if (Help == other.Help || Help == DefaultHelpString)
282 Help = other.Help;
283 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000284 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000285 " defined for option " + Name + "\n";
286 }
287
288 Flags |= other.Flags;
289}
290
291bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000292 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000293}
294
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000295bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000296 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000297}
298
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000299bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000300 return Flags & OptionDescriptionFlags::Extern;
301}
302void OptionDescription::setExtern() {
303 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000304}
305
306bool OptionDescription::isRequired() const {
307 return Flags & OptionDescriptionFlags::Required;
308}
309void OptionDescription::setRequired() {
310 Flags |= OptionDescriptionFlags::Required;
311}
312
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000313bool OptionDescription::isOneOrMore() const {
314 return Flags & OptionDescriptionFlags::OneOrMore;
315}
316void OptionDescription::setOneOrMore() {
317 Flags |= OptionDescriptionFlags::OneOrMore;
318}
319
320bool OptionDescription::isZeroOrOne() const {
321 return Flags & OptionDescriptionFlags::ZeroOrOne;
322}
323void OptionDescription::setZeroOrOne() {
324 Flags |= OptionDescriptionFlags::ZeroOrOne;
325}
326
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000327bool OptionDescription::isHidden() const {
328 return Flags & OptionDescriptionFlags::Hidden;
329}
330void OptionDescription::setHidden() {
331 Flags |= OptionDescriptionFlags::Hidden;
332}
333
334bool OptionDescription::isReallyHidden() const {
335 return Flags & OptionDescriptionFlags::ReallyHidden;
336}
337void OptionDescription::setReallyHidden() {
338 Flags |= OptionDescriptionFlags::ReallyHidden;
339}
340
341const char* OptionDescription::GenTypeDeclaration() const {
342 switch (Type) {
343 case OptionType::Alias:
344 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000345 case OptionType::PrefixList:
346 case OptionType::ParameterList:
347 return "cl::list<std::string>";
348 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000349 return "cl::opt<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000350 case OptionType::Parameter:
351 case OptionType::Prefix:
352 default:
353 return "cl::opt<std::string>";
354 }
355}
356
357std::string OptionDescription::GenVariableName() const {
358 const std::string& EscapedName = EscapeVariableName(Name);
359 switch (Type) {
360 case OptionType::Alias:
361 return "AutoGeneratedAlias_" + EscapedName;
362 case OptionType::PrefixList:
363 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000364 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000365 case OptionType::Switch:
366 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000367 case OptionType::Prefix:
368 case OptionType::Parameter:
369 default:
370 return "AutoGeneratedParameter_" + EscapedName;
371 }
372}
373
374/// OptionDescriptions - An OptionDescription array plus some helper
375/// functions.
376class OptionDescriptions {
377 typedef StringMap<OptionDescription> container_type;
378
379 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000380 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000381
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000382public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000383 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000384 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000385
386 // Wrappers for FindOption that throw an exception in case the option has a
387 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000388 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000389 const OptionDescription& FindParameter(const std::string& OptName) const;
390 const OptionDescription& FindList(const std::string& OptName) const;
391 const OptionDescription&
392 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000393
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000394 /// insertDescription - Insert new OptionDescription into
395 /// OptionDescriptions list
396 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000397
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000398 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000399 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000400 const_iterator begin() const { return Descriptions.begin(); }
401 const_iterator end() const { return Descriptions.end(); }
402};
403
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000404const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000405OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000406 const_iterator I = Descriptions.find(OptName);
407 if (I != Descriptions.end())
408 return I->second;
409 else
410 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000411}
412
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000413const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000414OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000415 const OptionDescription& OptDesc = this->FindOption(OptName);
416 if (!OptDesc.isSwitch())
417 throw OptName + ": incorrect option type - should be a switch!";
418 return OptDesc;
419}
420
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000421const OptionDescription&
422OptionDescriptions::FindList(const std::string& OptName) const {
423 const OptionDescription& OptDesc = this->FindOption(OptName);
424 if (!OptDesc.isList())
425 throw OptName + ": incorrect option type - should be a list!";
426 return OptDesc;
427}
428
429const OptionDescription&
430OptionDescriptions::FindParameter(const std::string& OptName) const {
431 const OptionDescription& OptDesc = this->FindOption(OptName);
432 if (!OptDesc.isParameter())
433 throw OptName + ": incorrect option type - should be a parameter!";
434 return OptDesc;
435}
436
437const OptionDescription&
438OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
439 const OptionDescription& OptDesc = this->FindOption(OptName);
440 if (!OptDesc.isList() && !OptDesc.isParameter())
441 throw OptName
442 + ": incorrect option type - should be a list or parameter!";
443 return OptDesc;
444}
445
446void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000447 container_type::iterator I = Descriptions.find(o.Name);
448 if (I != Descriptions.end()) {
449 OptionDescription& D = I->second;
450 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000451 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000452 else {
453 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000454 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000455}
456
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000457/// HandlerTable - A base class for function objects implemented as
458/// 'tables of handlers'.
459template <class T>
460class HandlerTable {
461protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000462 // Implementation details.
463
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000464 /// Handler -
465 typedef void (T::* Handler) (const DagInit*);
466 /// HandlerMap - A map from property names to property handlers
467 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000468
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000469 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000470 static bool staticMembersInitialized_;
471
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000472 T* childPtr;
473public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000474
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000475 HandlerTable(T* cp) : childPtr(cp)
476 {}
477
478 /// operator() - Just forwards to the corresponding property
479 /// handler.
480 void operator() (Init* i) {
481 const DagInit& property = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000482 const std::string& property_name = GetOperatorName(property);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000483 typename HandlerMap::iterator method = Handlers_.find(property_name);
484
485 if (method != Handlers_.end()) {
486 Handler h = method->second;
487 (childPtr->*h)(&property);
488 }
489 else {
490 throw "No handler found for property " + property_name + "!";
491 }
492 }
493
494 void AddHandler(const char* Property, Handler Handl) {
495 Handlers_[Property] = Handl;
496 }
497};
498
499template <class T> typename HandlerTable<T>::HandlerMap
500HandlerTable<T>::Handlers_;
501template <class T> bool HandlerTable<T>::staticMembersInitialized_ = false;
502
503
504/// CollectOptionProperties - Function object for iterating over an
505/// option property list.
506class CollectOptionProperties : public HandlerTable<CollectOptionProperties> {
507private:
508
509 /// optDescs_ - OptionDescriptions table. This is where the
510 /// information is stored.
511 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000512
513public:
514
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000515 explicit CollectOptionProperties(OptionDescription& OD)
516 : HandlerTable<CollectOptionProperties>(this), optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000517 {
518 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000519 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000520 AddHandler("help", &CollectOptionProperties::onHelp);
521 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000522 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000523 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
524 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000525 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
526 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000527 AddHandler("zero_or_one", &CollectOptionProperties::onZeroOrOne);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000528
529 staticMembersInitialized_ = true;
530 }
531 }
532
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000533private:
534
535 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000536 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000537
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000538 void onExtern (const DagInit* d) {
539 checkNumberOfArguments(d, 0);
540 optDesc_.setExtern();
541 }
542
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000543 void onHelp (const DagInit* d) {
544 checkNumberOfArguments(d, 1);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000545 optDesc_.Help = InitPtrToString(d->getArg(0));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000546 }
547
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000548 void onHidden (const DagInit* d) {
549 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000550 optDesc_.setHidden();
551 }
552
553 void onReallyHidden (const DagInit* d) {
554 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000555 optDesc_.setReallyHidden();
556 }
557
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000558 void onRequired (const DagInit* d) {
559 checkNumberOfArguments(d, 0);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000560 if (optDesc_.isOneOrMore())
561 throw std::string("An option can't have both (required) "
562 "and (one_or_more) properties!");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000563 optDesc_.setRequired();
564 }
565
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000566 void onInit (const DagInit* d) {
567 checkNumberOfArguments(d, 1);
568 Init* i = d->getArg(0);
569 const std::string& str = i->getAsString();
570
571 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
572 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
573
574 if (!correct)
575 throw std::string("Incorrect usage of the 'init' option property!");
576
577 optDesc_.InitVal = i;
578 }
579
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000580 void onOneOrMore (const DagInit* d) {
581 checkNumberOfArguments(d, 0);
582 if (optDesc_.isRequired() || optDesc_.isZeroOrOne())
583 throw std::string("Only one of (required), (zero_or_one) or "
584 "(one_or_more) properties is allowed!");
585 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000586 llvm::errs() << "Warning: specifying the 'one_or_more' property "
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000587 "on a non-list option will have no effect.\n";
588 optDesc_.setOneOrMore();
589 }
590
591 void onZeroOrOne (const DagInit* d) {
592 checkNumberOfArguments(d, 0);
593 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
594 throw std::string("Only one of (required), (zero_or_one) or "
595 "(one_or_more) properties is allowed!");
596 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000597 llvm::errs() << "Warning: specifying the 'zero_or_one' property"
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000598 "on a non-list option will have no effect.\n";
599 optDesc_.setZeroOrOne();
600 }
601
602 void onMultiVal (const DagInit* d) {
603 checkNumberOfArguments(d, 1);
604 int val = InitPtrToInt(d->getArg(0));
605 if (val < 2)
606 throw std::string("Error in the 'multi_val' property: "
607 "the value must be greater than 1!");
608 if (!OptionType::IsList(optDesc_.Type))
609 throw std::string("The multi_val property is valid only "
610 "on list options!");
611 optDesc_.MultiVal = val;
612 }
613
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000614};
615
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000616/// AddOption - A function object that is applied to every option
617/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000618class AddOption {
619private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000620 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000621
622public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000623 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000624 {}
625
626 void operator()(const Init* i) {
627 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000628 checkNumberOfArguments(&d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000629
630 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000631 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000632 const std::string& Name = InitPtrToString(d.getArg(0));
633
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000634 OptionDescription OD(Type, Name);
635
636 if (!OD.isExtern())
637 checkNumberOfArguments(&d, 2);
638
639 if (OD.isAlias()) {
640 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000641 OD.Help = InitPtrToString(d.getArg(1));
642 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000643 else if (!OD.isExtern()) {
644 processOptionProperties(&d, OD);
645 }
646 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000647 }
648
649private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000650 /// processOptionProperties - Go through the list of option
651 /// properties and call a corresponding handler for each.
652 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
653 checkNumberOfArguments(d, 2);
654 DagInit::const_arg_iterator B = d->arg_begin();
655 // Skip the first argument: it's always the option name.
656 ++B;
657 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000658 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000659
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000660};
661
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000662/// CollectOptionDescriptions - Collects option properties from all
663/// OptionLists.
664void CollectOptionDescriptions (RecordVector::const_iterator B,
665 RecordVector::const_iterator E,
666 OptionDescriptions& OptDescs)
667{
668 // For every OptionList:
669 for (; B!=E; ++B) {
670 RecordVector::value_type T = *B;
671 // Throws an exception if the value does not exist.
672 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000673
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000674 // For every option description in this list:
675 // collect the information and
676 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
677 }
678}
679
680// Tool information record
681
682namespace ToolFlags {
683 enum ToolFlags { Join = 0x1, Sink = 0x2 };
684}
685
686struct ToolDescription : public RefCountedBase<ToolDescription> {
687 std::string Name;
688 Init* CmdLine;
689 Init* Actions;
690 StrVector InLanguage;
691 std::string OutLanguage;
692 std::string OutputSuffix;
693 unsigned Flags;
694
695 // Various boolean properties
696 void setSink() { Flags |= ToolFlags::Sink; }
697 bool isSink() const { return Flags & ToolFlags::Sink; }
698 void setJoin() { Flags |= ToolFlags::Join; }
699 bool isJoin() const { return Flags & ToolFlags::Join; }
700
701 // Default ctor here is needed because StringMap can only store
702 // DefaultConstructible objects
703 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
704 ToolDescription (const std::string& n)
705 : Name(n), CmdLine(0), Actions(0), Flags(0)
706 {}
707};
708
709/// ToolDescriptions - A list of Tool information records.
710typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
711
712
713/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000714/// tool property records.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000715class CollectToolProperties : public HandlerTable<CollectToolProperties> {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000716private:
717
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000718 /// toolDesc_ - Properties of the current Tool. This is where the
719 /// information is stored.
720 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000721
722public:
723
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000724 explicit CollectToolProperties (ToolDescription& d)
725 : HandlerTable<CollectToolProperties>(this) , toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000726 {
727 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000728
729 AddHandler("actions", &CollectToolProperties::onActions);
730 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
731 AddHandler("in_language", &CollectToolProperties::onInLanguage);
732 AddHandler("join", &CollectToolProperties::onJoin);
733 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
734 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
735 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000736
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000737 staticMembersInitialized_ = true;
738 }
739 }
740
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000741private:
742
743 /// Property handlers --
744 /// Functions that extract information about tool properties from
745 /// DAG representation.
746
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000747 void onActions (const DagInit* d) {
748 checkNumberOfArguments(d, 1);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000749 Init* Case = d->getArg(0);
750 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000751 GetOperatorName(static_cast<DagInit*>(Case)) != "case")
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000752 throw
753 std::string("The argument to (actions) should be a 'case' construct!");
754 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000755 }
756
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000757 void onCmdLine (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000758 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000759 toolDesc_.CmdLine = d->getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000760 }
761
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000762 void onInLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000763 checkNumberOfArguments(d, 1);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000764 Init* arg = d->getArg(0);
765
766 // Find out the argument's type.
767 if (typeid(*arg) == typeid(StringInit)) {
768 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000769 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000770 }
771 else {
772 // It's a list.
773 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000774 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000775
776 // Copy strings to the output vector.
777 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
778 B != E; ++B) {
779 out.push_back(InitPtrToString(*B));
780 }
781
782 // Remove duplicates.
783 std::sort(out.begin(), out.end());
784 StrVector::iterator newE = std::unique(out.begin(), out.end());
785 out.erase(newE, out.end());
786 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000787 }
788
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000789 void onJoin (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000790 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000791 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000792 }
793
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000794 void onOutLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000795 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000796 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000797 }
798
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000799 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000800 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000801 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000802 }
803
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000804 void onSink (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000805 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000806 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000807 }
808
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000809};
810
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000811/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000812/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000813/// CollectToolProperties function object).
814void CollectToolDescriptions (RecordVector::const_iterator B,
815 RecordVector::const_iterator E,
816 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000817{
818 // Iterate over a properties list of every Tool definition
819 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000820 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000821 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000822 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000823
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000824 IntrusiveRefCntPtr<ToolDescription>
825 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000826
827 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000828 CollectToolProperties(*ToolDesc));
829 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000830 }
831}
832
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000833/// FillInEdgeVector - Merge all compilation graph definitions into
834/// one single edge list.
835void FillInEdgeVector(RecordVector::const_iterator B,
836 RecordVector::const_iterator E, RecordVector& Out) {
837 for (; B != E; ++B) {
838 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000839
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000840 for (unsigned i = 0; i < edges->size(); ++i)
841 Out.push_back(edges->getElementAsRecord(i));
842 }
843}
844
845/// CalculatePriority - Calculate the priority of this plugin.
846int CalculatePriority(RecordVector::const_iterator B,
847 RecordVector::const_iterator E) {
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000848 int priority = 0;
849
850 if (B != E) {
851 priority = static_cast<int>((*B)->getValueAsInt("priority"));
852
853 if (++B != E)
854 throw std::string("More than one 'PluginPriority' instance found: "
855 "most probably an error!");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000856 }
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000857
858 return priority;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000859}
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000860
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000861/// NotInGraph - Helper function object for FilterNotInGraph.
862struct NotInGraph {
863private:
864 const llvm::StringSet<>& ToolsInGraph_;
865
866public:
867 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
868 : ToolsInGraph_(ToolsInGraph)
869 {}
870
871 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
872 return (ToolsInGraph_.count(x->Name) == 0);
873 }
874};
875
876/// FilterNotInGraph - Filter out from ToolDescs all Tools not
877/// mentioned in the compilation graph definition.
878void FilterNotInGraph (const RecordVector& EdgeVector,
879 ToolDescriptions& ToolDescs) {
880
881 // List all tools mentioned in the graph.
882 llvm::StringSet<> ToolsInGraph;
883
884 for (RecordVector::const_iterator B = EdgeVector.begin(),
885 E = EdgeVector.end(); B != E; ++B) {
886
887 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000888 const std::string& NodeA = Edge->getValueAsString("a");
889 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000890
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000891 if (NodeA != "root")
892 ToolsInGraph.insert(NodeA);
893 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000894 }
895
896 // Filter ToolPropertiesList.
897 ToolDescriptions::iterator new_end =
898 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
899 NotInGraph(ToolsInGraph));
900 ToolDescs.erase(new_end, ToolDescs.end());
901}
902
903/// FillInToolToLang - Fills in two tables that map tool names to
904/// (input, output) languages. Helper function used by TypecheckGraph().
905void FillInToolToLang (const ToolDescriptions& ToolDescs,
906 StringMap<StringSet<> >& ToolToInLang,
907 StringMap<std::string>& ToolToOutLang) {
908 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
909 E = ToolDescs.end(); B != E; ++B) {
910 const ToolDescription& D = *(*B);
911 for (StrVector::const_iterator B = D.InLanguage.begin(),
912 E = D.InLanguage.end(); B != E; ++B)
913 ToolToInLang[D.Name].insert(*B);
914 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000915 }
916}
917
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000918/// TypecheckGraph - Check that names for output and input languages
919/// on all edges do match. This doesn't do much when the information
920/// about the whole graph is not available (i.e. when compiling most
921/// plugins).
922void TypecheckGraph (const RecordVector& EdgeVector,
923 const ToolDescriptions& ToolDescs) {
924 StringMap<StringSet<> > ToolToInLang;
925 StringMap<std::string> ToolToOutLang;
926
927 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
928 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
929 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
930
931 for (RecordVector::const_iterator B = EdgeVector.begin(),
932 E = EdgeVector.end(); B != E; ++B) {
933 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000934 const std::string& NodeA = Edge->getValueAsString("a");
935 const std::string& NodeB = Edge->getValueAsString("b");
936 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
937 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000938
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000939 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000940 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000941 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000942 + ": output->input language mismatch";
943 }
944
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000945 if (NodeB == "root")
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000946 throw std::string("Edges back to the root are not allowed!");
947 }
948}
949
950/// WalkCase - Walks the 'case' expression DAG and invokes
951/// TestCallback on every test, and StatementCallback on every
952/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000953/// combinators (that is, they are passed directly to TestCallback).
954/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
955/// IndentLevel, bool FirstTest)'.
956/// StatementCallback must have type 'void StatementCallback(const Init*,
957/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000958template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000959void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
960 unsigned IndentLevel = 0)
961{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000962 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000963
964 // Error checks.
965 if (GetOperatorName(d) != "case")
966 throw std::string("WalkCase should be invoked only on 'case' expressions!");
967
968 if (d.getNumArgs() < 2)
969 throw "There should be at least one clause in the 'case' expression:\n"
970 + d.getAsString();
971
972 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000973 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000974 const unsigned numArgs = d.getNumArgs();
975 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000976 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
977 B != E; ++B) {
978 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000979
980 if (!even)
981 {
982 // Handle test.
983 const DagInit& Test = InitPtrToDag(arg);
984
985 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
986 throw std::string("The 'default' clause should be the last in the"
987 "'case' construct!");
988 if (i == numArgs)
989 throw "Case construct handler: no corresponding action "
990 "found for the test " + Test.getAsString() + '!';
991
992 TestCallback(&Test, IndentLevel, (i == 1));
993 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000994 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000995 {
996 if (dynamic_cast<DagInit*>(arg)
997 && GetOperatorName(static_cast<DagInit*>(arg)) == "case") {
998 // Nested 'case'.
999 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1000 }
1001
1002 // Handle statement.
1003 StatementCallback(arg, IndentLevel);
1004 }
1005
1006 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001007 even = !even;
1008 }
1009}
1010
1011/// ExtractOptionNames - A helper function object used by
1012/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1013class ExtractOptionNames {
1014 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001015
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001016 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001017 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001018 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001019 if (ActionName == "forward" || ActionName == "forward_as" ||
1020 ActionName == "unpack_values" || ActionName == "switch_on" ||
1021 ActionName == "parameter_equals" || ActionName == "element_in_list" ||
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001022 ActionName == "not_empty" || ActionName == "empty") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001023 checkNumberOfArguments(&Stmt, 1);
1024 const std::string& Name = InitPtrToString(Stmt.getArg(0));
1025 OptionNames_.insert(Name);
1026 }
1027 else if (ActionName == "and" || ActionName == "or") {
1028 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001029 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001030 }
1031 }
1032 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001033
1034public:
1035 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1036 {}
1037
1038 void operator()(const Init* Statement) {
1039 if (typeid(*Statement) == typeid(ListInit)) {
1040 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1041 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1042 B != E; ++B)
1043 this->processDag(*B);
1044 }
1045 else {
1046 this->processDag(Statement);
1047 }
1048 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001049
1050 void operator()(const DagInit* Test, unsigned, bool) {
1051 this->operator()(Test);
1052 }
1053 void operator()(const Init* Statement, unsigned) {
1054 this->operator()(Statement);
1055 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001056};
1057
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001058/// CheckForSuperfluousOptions - Check that there are no side
1059/// effect-free options (specified only in the OptionList). Otherwise,
1060/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001061void CheckForSuperfluousOptions (const RecordVector& Edges,
1062 const ToolDescriptions& ToolDescs,
1063 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001064 llvm::StringSet<> nonSuperfluousOptions;
1065
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001066 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001067 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001068 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1069 E = ToolDescs.end(); B != E; ++B) {
1070 const ToolDescription& TD = *(*B);
1071 ExtractOptionNames Callback(nonSuperfluousOptions);
1072 if (TD.Actions)
1073 WalkCase(TD.Actions, Callback, Callback);
1074 }
1075
1076 // Add all options mentioned in the 'case' clauses of the
1077 // OptionalEdges of the compilation graph to the set of
1078 // non-superfluous options.
1079 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1080 B != E; ++B) {
1081 const Record* Edge = *B;
1082 DagInit* Weight = Edge->getValueAsDag("weight");
1083
1084 if (!isDagEmpty(Weight))
1085 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001086 }
1087
1088 // Check that all options in OptDescs belong to the set of
1089 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001090 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001091 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001092 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001093 if (!nonSuperfluousOptions.count(Val.Name)
1094 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001095 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001096 "Probable cause: this option is specified only in the OptionList.\n";
1097 }
1098}
1099
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001100/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1101bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1102 if (TestName == "single_input_file") {
1103 O << "InputFilenames.size() == 1";
1104 return true;
1105 }
1106 else if (TestName == "multiple_input_files") {
1107 O << "InputFilenames.size() > 1";
1108 return true;
1109 }
1110
1111 return false;
1112}
1113
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001114/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1115template <typename F>
1116void EmitListTest(const ListInit& L, const char* LogicOp,
1117 F Callback, raw_ostream& O)
1118{
1119 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1120 // of Dags...
1121 bool isFirst = true;
1122 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1123 if (isFirst)
1124 isFirst = false;
1125 else
1126 O << " || ";
1127 Callback(InitPtrToString(*B), O);
1128 }
1129}
1130
1131// Callbacks for use with EmitListTest.
1132
1133class EmitSwitchOn {
1134 const OptionDescriptions& OptDescs_;
1135public:
1136 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1137 {}
1138
1139 void operator()(const std::string& OptName, raw_ostream& O) const {
1140 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1141 O << OptDesc.GenVariableName();
1142 }
1143};
1144
1145class EmitEmptyTest {
1146 bool EmitNegate_;
1147 const OptionDescriptions& OptDescs_;
1148public:
1149 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1150 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1151 {}
1152
1153 void operator()(const std::string& OptName, raw_ostream& O) const {
1154 const char* Neg = (EmitNegate_ ? "!" : "");
1155 if (OptName == "o") {
1156 O << Neg << "OutputFilename.empty()";
1157 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001158 else if (OptName == "save-temps") {
1159 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1160 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001161 else {
1162 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1163 O << Neg << OptDesc.GenVariableName() << ".empty()";
1164 }
1165 }
1166};
1167
1168
1169/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1170bool EmitCaseTest1ArgList(const std::string& TestName,
1171 const DagInit& d,
1172 const OptionDescriptions& OptDescs,
1173 raw_ostream& O) {
1174 const ListInit& L = *static_cast<ListInit*>(d.getArg(0));
1175
1176 if (TestName == "any_switch_on") {
1177 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1178 return true;
1179 }
1180 else if (TestName == "switch_on") {
1181 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1182 return true;
1183 }
1184 else if (TestName == "any_not_empty") {
1185 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1186 return true;
1187 }
1188 else if (TestName == "any_empty") {
1189 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1190 return true;
1191 }
1192 else if (TestName == "not_empty") {
1193 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1194 return true;
1195 }
1196 else if (TestName == "empty") {
1197 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1198 return true;
1199 }
1200
1201 return false;
1202}
1203
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001204/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1205bool EmitCaseTest1ArgStr(const std::string& TestName,
1206 const DagInit& d,
1207 const OptionDescriptions& OptDescs,
1208 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001209 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001210
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001211 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001212 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001213 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001214 }
1215 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001216 O << "InLangs.count(\"" << OptName << "\") != 0";
1217 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001218 }
1219 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001220 // This works only for single-argument Tool::GenerateAction. Join
1221 // tools can process several files in different languages simultaneously.
1222
1223 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001224 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001225 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001226 }
1227 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001228 bool EmitNegate = (TestName == "not_empty");
1229 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001230 return true;
1231 }
1232
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001233 return false;
1234}
1235
1236/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1237bool EmitCaseTest1Arg(const std::string& TestName,
1238 const DagInit& d,
1239 const OptionDescriptions& OptDescs,
1240 raw_ostream& O) {
1241 checkNumberOfArguments(&d, 1);
1242 if (typeid(*d.getArg(0)) == typeid(ListInit))
1243 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1244 else
1245 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1246}
1247
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001248/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001249bool EmitCaseTest2Args(const std::string& TestName,
1250 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001251 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001252 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001253 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001254 checkNumberOfArguments(&d, 2);
1255 const std::string& OptName = InitPtrToString(d.getArg(0));
1256 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001257
1258 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001259 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001260 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1261 return true;
1262 }
1263 else if (TestName == "element_in_list") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001264 const OptionDescription& OptDesc = OptDescs.FindList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001265 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001266 O << "std::find(" << VarName << ".begin(),\n";
1267 O.indent(IndentLevel + Indent1)
1268 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001269 << OptArg << "\") != " << VarName << ".end()";
1270 return true;
1271 }
1272
1273 return false;
1274}
1275
1276// Forward declaration.
1277// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001278void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001279 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001280 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001281
1282/// EmitLogicalOperationTest - Helper function used by
1283/// EmitCaseConstructHandler.
1284void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001285 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001286 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001287 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001288 O << '(';
1289 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001290 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001291 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001292 if (j != NumArgs - 1) {
1293 O << ")\n";
1294 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1295 }
1296 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001297 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001298 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001299 }
1300}
1301
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001302void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001303 const OptionDescriptions& OptDescs, raw_ostream& O)
1304{
1305 checkNumberOfArguments(&d, 1);
1306 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1307 O << "! (";
1308 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1309 O << ")";
1310}
1311
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001312/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001313void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001314 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001315 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001316 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001317
1318 if (TestName == "and")
1319 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1320 else if (TestName == "or")
1321 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001322 else if (TestName == "not")
1323 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001324 else if (EmitCaseTest0Args(TestName, O))
1325 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001326 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1327 return;
1328 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1329 return;
1330 else
1331 throw TestName + ": unknown edge property!";
1332}
1333
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001334
1335/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1336class EmitCaseTestCallback {
1337 bool EmitElseIf_;
1338 const OptionDescriptions& OptDescs_;
1339 raw_ostream& O_;
1340public:
1341
1342 EmitCaseTestCallback(bool EmitElseIf,
1343 const OptionDescriptions& OptDescs, raw_ostream& O)
1344 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1345 {}
1346
1347 void operator()(const DagInit* Test, unsigned IndentLevel, bool FirstTest)
1348 {
1349 if (GetOperatorName(Test) == "default") {
1350 O_.indent(IndentLevel) << "else {\n";
1351 }
1352 else {
1353 O_.indent(IndentLevel)
1354 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
1355 EmitCaseTest(*Test, IndentLevel, OptDescs_, O_);
1356 O_ << ") {\n";
1357 }
1358 }
1359};
1360
1361/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1362template <typename F>
1363class EmitCaseStatementCallback {
1364 F Callback_;
1365 raw_ostream& O_;
1366public:
1367
1368 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1369 : Callback_(Callback), O_(O)
1370 {}
1371
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001372 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001373
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001374 // Ignore nested 'case' DAG.
1375 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001376 GetOperatorName(static_cast<const DagInit*>(Statement)) == "case")) {
1377 if (typeid(*Statement) == typeid(ListInit)) {
1378 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1379 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1380 B != E; ++B)
1381 Callback_(*B, (IndentLevel + Indent1), O_);
1382 }
1383 else {
1384 Callback_(Statement, (IndentLevel + Indent1), O_);
1385 }
1386 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001387 O_.indent(IndentLevel) << "}\n";
1388 }
1389
1390};
1391
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001392/// EmitCaseConstructHandler - Emit code that handles the 'case'
1393/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001394/// clause. Implemented on top of WalkCase.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001395/// Callback's type is void F(Init* Statement, unsigned IndentLevel,
1396/// raw_ostream& O).
1397/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001398/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1399/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001400template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001401void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001402 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001403 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001404 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001405 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1406 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001407}
1408
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001409/// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to
1410/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path"] .
1411/// Helper function used by EmitCmdLineVecFill and.
1412void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
1413 const char* Delimiters = " \t\n\v\f\r";
1414 enum TokenizerState
1415 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1416 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001417
1418 if (CmdLine.empty())
1419 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001420 Out.push_back("");
1421
1422 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1423 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001424
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001425 for (; B != E; ++B) {
1426 char cur_ch = CmdLine[B];
1427
1428 switch (cur_st) {
1429 case Normal:
1430 if (cur_ch == '$') {
1431 cur_st = SpecialCommand;
1432 break;
1433 }
1434 if (oneOf(Delimiters, cur_ch)) {
1435 // Skip whitespace
1436 B = CmdLine.find_first_not_of(Delimiters, B);
1437 if (B == std::string::npos) {
1438 B = E-1;
1439 continue;
1440 }
1441 --B;
1442 Out.push_back("");
1443 continue;
1444 }
1445 break;
1446
1447
1448 case SpecialCommand:
1449 if (oneOf(Delimiters, cur_ch)) {
1450 cur_st = Normal;
1451 Out.push_back("");
1452 continue;
1453 }
1454 if (cur_ch == '(') {
1455 Out.push_back("");
1456 cur_st = InsideSpecialCommand;
1457 continue;
1458 }
1459 break;
1460
1461 case InsideSpecialCommand:
1462 if (oneOf(Delimiters, cur_ch)) {
1463 continue;
1464 }
1465 if (cur_ch == '\'') {
1466 cur_st = InsideQuotationMarks;
1467 Out.push_back("");
1468 continue;
1469 }
1470 if (cur_ch == ')') {
1471 cur_st = Normal;
1472 Out.push_back("");
1473 }
1474 if (cur_ch == ',') {
1475 continue;
1476 }
1477
1478 break;
1479
1480 case InsideQuotationMarks:
1481 if (cur_ch == '\'') {
1482 cur_st = InsideSpecialCommand;
1483 continue;
1484 }
1485 break;
1486 }
1487
1488 Out.back().push_back(cur_ch);
1489 }
1490}
1491
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001492/// SubstituteSpecialCommands - Perform string substitution for $CALL
1493/// and $ENV. Helper function used by EmitCmdLineVecFill().
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001494StrVector::const_iterator SubstituteSpecialCommands
Daniel Dunbar1a551802009-07-03 00:10:29 +00001495(StrVector::const_iterator Pos, StrVector::const_iterator End, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001496{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001497
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001498 const std::string& cmd = *Pos;
1499
1500 if (cmd == "$CALL") {
1501 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1502 const std::string& CmdName = *Pos;
1503
1504 if (CmdName == ")")
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001505 throw std::string("$CALL invocation: empty argument list!");
1506
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001507 O << "hooks::";
1508 O << CmdName << "(";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001509
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001510
1511 bool firstIteration = true;
1512 while (true) {
1513 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1514 const std::string& Arg = *Pos;
1515 assert(Arg.size() != 0);
1516
1517 if (Arg[0] == ')')
1518 break;
1519
1520 if (firstIteration)
1521 firstIteration = false;
1522 else
1523 O << ", ";
1524
1525 O << '"' << Arg << '"';
1526 }
1527
1528 O << ')';
1529
1530 }
1531 else if (cmd == "$ENV") {
1532 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
1533 const std::string& EnvName = *Pos;
1534
1535 if (EnvName == ")")
1536 throw "$ENV invocation: empty argument list!";
1537
1538 O << "checkCString(std::getenv(\"";
1539 O << EnvName;
1540 O << "\"))";
1541
1542 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001543 }
1544 else {
1545 throw "Unknown special command: " + cmd;
1546 }
1547
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001548 const std::string& Leftover = *Pos;
1549 assert(Leftover.at(0) == ')');
1550 if (Leftover.size() != 1)
1551 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001552
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001553 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001554}
1555
1556/// EmitCmdLineVecFill - Emit code that fills in the command line
1557/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001558void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001559 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001560 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001561 StrVector StrVec;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001562 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1563
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001564 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001565 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001566
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001567 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1568
1569 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001570 assert(!StrVec[0].empty());
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001571 if (StrVec[0][0] == '$') {
1572 while (I != E && (*I)[0] != ')' )
1573 ++I;
1574
1575 // Skip the ')' symbol.
1576 ++I;
1577 }
1578 else {
1579 ++I;
1580 }
1581
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001582 bool hasINFILE = false;
1583
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001584 for (; I != E; ++I) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001585 const std::string& cmd = *I;
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001586 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001587 O.indent(IndentLevel);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001588 if (cmd.at(0) == '$') {
1589 if (cmd == "$INFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001590 hasINFILE = true;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001591 if (IsJoin) {
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001592 O << "for (PathVector::const_iterator B = inFiles.begin()"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001593 << ", E = inFiles.end();\n";
1594 O.indent(IndentLevel) << "B != E; ++B)\n";
1595 O.indent(IndentLevel + Indent1) << "vec.push_back(B->str());\n";
1596 }
1597 else {
Chris Lattner74382b72009-08-23 22:45:37 +00001598 O << "vec.push_back(inFile.str());\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001599 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001600 }
1601 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001602 O << "vec.push_back(\"\");\n";
1603 O.indent(IndentLevel) << "out_file_index = vec.size()-1;\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001604 }
1605 else {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001606 O << "vec.push_back(";
1607 I = SubstituteSpecialCommands(I, E, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001608 O << ");\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001609 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001610 }
1611 else {
1612 O << "vec.push_back(\"" << cmd << "\");\n";
1613 }
1614 }
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001615 if (!hasINFILE)
1616 throw "Tool '" + ToolName + "' doesn't take any input!";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001617
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001618 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001619 if (StrVec[0][0] == '$')
1620 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), O);
1621 else
1622 O << '"' << StrVec[0] << '"';
1623 O << ";\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001624}
1625
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001626/// EmitCmdLineVecFillCallback - A function object wrapper around
1627/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1628/// argument to EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001629class EmitCmdLineVecFillCallback {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001630 bool IsJoin;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001631 const std::string& ToolName;
1632 public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001633 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1634 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001635
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001636 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001637 raw_ostream& O) const
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001638 {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001639 EmitCmdLineVecFill(Statement, ToolName, IsJoin, IndentLevel, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001640 }
1641};
1642
1643/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1644/// implement EmitActionHandler. Emits code for
1645/// handling the (forward) and (forward_as) option properties.
1646void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001647 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001648 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001649 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001650 const std::string& Name = NewName.empty()
1651 ? ("-" + D.Name)
1652 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001653 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001654
1655 switch (D.Type) {
1656 case OptionType::Switch:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001657 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001658 break;
1659 case OptionType::Parameter:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001660 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
1661 O.indent(IndentLevel) << "vec.push_back(" << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001662 break;
1663 case OptionType::Prefix:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001664 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\" + "
1665 << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001666 break;
1667 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001668 O.indent(IndentLevel)
1669 << "for (" << D.GenTypeDeclaration()
1670 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1671 O.indent(IndentLevel)
1672 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1673 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\" + " << "*B);\n";
1674 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001675
1676 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001677 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1678 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001679 }
1680
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001681 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001682 break;
1683 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001684 O.indent(IndentLevel)
1685 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1686 << D.GenVariableName() << ".begin(),\n";
1687 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1688 << ".end() ; B != E;) {\n";
1689 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001690
1691 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001692 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1693 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001694 }
1695
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001696 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001697 break;
1698 case OptionType::Alias:
1699 default:
1700 throw std::string("Aliases are not allowed in tool option descriptions!");
1701 }
1702}
1703
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001704/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1705/// EmitPreprocessOptionsCallback.
1706struct ActionHandlingCallbackBase {
1707
1708 void onErrorDag(const DagInit& d,
1709 unsigned IndentLevel, raw_ostream& O) const
1710 {
1711 O.indent(IndentLevel)
1712 << "throw std::runtime_error(\"" <<
1713 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1714 : "Unknown error!")
1715 << "\");\n";
1716 }
1717
1718 void onWarningDag(const DagInit& d,
1719 unsigned IndentLevel, raw_ostream& O) const
1720 {
1721 checkNumberOfArguments(&d, 1);
1722 O.indent(IndentLevel) << "llvm::errs() << \""
1723 << InitPtrToString(d.getArg(0)) << "\";\n";
1724 }
1725
1726};
1727
1728/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1729/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
1730class EmitActionHandlersCallback : ActionHandlingCallbackBase {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001731 const OptionDescriptions& OptDescs;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001732
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001733 void processActionDag(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001734 raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001735 {
1736 const DagInit& Dag = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001737 const std::string& ActionName = GetOperatorName(Dag);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001738
1739 if (ActionName == "append_cmd") {
1740 checkNumberOfArguments(&Dag, 1);
1741 const std::string& Cmd = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001742 StrVector Out;
1743 llvm::SplitString(Cmd, Out);
1744
1745 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1746 B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001747 O.indent(IndentLevel) << "vec.push_back(\"" << *B << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001748 }
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001749 else if (ActionName == "error") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001750 this->onErrorDag(Dag, IndentLevel, O);
1751 }
1752 else if (ActionName == "warning") {
1753 this->onWarningDag(Dag, IndentLevel, O);
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001754 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001755 else if (ActionName == "forward") {
1756 checkNumberOfArguments(&Dag, 1);
1757 const std::string& Name = InitPtrToString(Dag.getArg(0));
1758 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1759 IndentLevel, "", O);
1760 }
1761 else if (ActionName == "forward_as") {
1762 checkNumberOfArguments(&Dag, 2);
1763 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkove89331b2009-05-06 01:41:19 +00001764 const std::string& NewName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001765 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1766 IndentLevel, NewName, O);
1767 }
1768 else if (ActionName == "output_suffix") {
1769 checkNumberOfArguments(&Dag, 1);
1770 const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001771 O.indent(IndentLevel) << "output_suffix = \"" << OutSuf << "\";\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001772 }
1773 else if (ActionName == "stop_compilation") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001774 O.indent(IndentLevel) << "stop_compilation = true;\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001775 }
1776 else if (ActionName == "unpack_values") {
1777 checkNumberOfArguments(&Dag, 1);
1778 const std::string& Name = InitPtrToString(Dag.getArg(0));
1779 const OptionDescription& D = OptDescs.FindOption(Name);
1780
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001781 if (D.isMultiVal())
1782 throw std::string("Can't use unpack_values with multi-valued options!");
1783
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001784 if (D.isList()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001785 O.indent(IndentLevel)
1786 << "for (" << D.GenTypeDeclaration()
1787 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1788 O.indent(IndentLevel)
1789 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n";
1790 O.indent(IndentLevel + Indent1)
1791 << "llvm::SplitString(*B, vec, \",\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001792 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001793 else if (D.isParameter()){
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001794 O.indent(IndentLevel) << "llvm::SplitString("
1795 << D.GenVariableName() << ", vec, \",\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001796 }
1797 else {
1798 throw "Option '" + D.Name +
1799 "': switches can't have the 'unpack_values' property!";
1800 }
1801 }
1802 else {
1803 throw "Unknown action name: " + ActionName + "!";
1804 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001805 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001806 public:
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001807 EmitActionHandlersCallback(const OptionDescriptions& OD)
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001808 : OptDescs(OD) {}
1809
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001810 void operator()(const Init* Statement,
1811 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001812 {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001813 this->processActionDag(Statement, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001814 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001815};
1816
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001817bool IsOutFileIndexCheckRequiredStr (const Init* CmdLine) {
1818 StrVector StrVec;
1819 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1820
1821 for (StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1822 I != E; ++I) {
1823 if (*I == "$OUTFILE")
1824 return false;
1825 }
1826
1827 return true;
1828}
1829
1830class IsOutFileIndexCheckRequiredStrCallback {
1831 bool* ret_;
1832
1833public:
1834 IsOutFileIndexCheckRequiredStrCallback(bool* ret) : ret_(ret)
1835 {}
1836
1837 void operator()(const Init* CmdLine) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001838 // Ignore nested 'case' DAG.
1839 if (typeid(*CmdLine) == typeid(DagInit))
1840 return;
1841
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001842 if (IsOutFileIndexCheckRequiredStr(CmdLine))
1843 *ret_ = true;
1844 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001845
1846 void operator()(const DagInit* Test, unsigned, bool) {
1847 this->operator()(Test);
1848 }
1849 void operator()(const Init* Statement, unsigned) {
1850 this->operator()(Statement);
1851 }
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001852};
1853
1854bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
1855 bool ret = false;
1856 WalkCase(CmdLine, Id(), IsOutFileIndexCheckRequiredStrCallback(&ret));
1857 return ret;
1858}
1859
1860/// IsOutFileIndexCheckRequired - Should we emit an "out_file_index != -1" check
1861/// in EmitGenerateActionMethod() ?
1862bool IsOutFileIndexCheckRequired (Init* CmdLine) {
1863 if (typeid(*CmdLine) == typeid(StringInit))
1864 return IsOutFileIndexCheckRequiredStr(CmdLine);
1865 else
1866 return IsOutFileIndexCheckRequiredCase(CmdLine);
1867}
1868
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001869// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001870// Tool::GenerateAction() method.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001871void EmitGenerateActionMethod (const ToolDescription& D,
1872 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001873 bool IsJoin, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001874 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001875 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001876 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001877 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001878
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001879 O.indent(Indent2) << "bool HasChildren,\n";
1880 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1881 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1882 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1883 O.indent(Indent1) << "{\n";
1884 O.indent(Indent2) << "std::string cmd;\n";
1885 O.indent(Indent2) << "std::vector<std::string> vec;\n";
1886 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
1887 O.indent(Indent2) << "const char* output_suffix = \""
1888 << D.OutputSuffix << "\";\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001889
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001890 if (!D.CmdLine)
1891 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001892
1893 bool IndexCheckRequired = IsOutFileIndexCheckRequired(D.CmdLine);
1894 O.indent(Indent2) << "int out_file_index"
1895 << (IndexCheckRequired ? " = -1" : "")
1896 << ";\n\n";
1897
1898 // Process the cmd_line property.
1899 if (typeid(*D.CmdLine) == typeid(StringInit))
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001900 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
1901 else
1902 EmitCaseConstructHandler(D.CmdLine, Indent2,
1903 EmitCmdLineVecFillCallback(IsJoin, D.Name),
1904 true, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001905
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001906 // For every understood option, emit handling code.
1907 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00001908 EmitCaseConstructHandler(D.Actions, Indent2,
1909 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001910 false, OptDescs, O);
1911
1912 O << '\n';
1913 O.indent(Indent2)
1914 << "std::string out_file = OutFilename("
1915 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
1916 O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001917
1918 if (IndexCheckRequired)
1919 O.indent(Indent2) << "if (out_file_index != -1)\n";
1920 O.indent(IndexCheckRequired ? Indent3 : Indent2)
1921 << "vec[out_file_index] = out_file;\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001922
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001923 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001924 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001925 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
1926 O.indent(Indent3) << "vec.insert(vec.end(), "
1927 << SinkOptionName << ".begin(), " << SinkOptionName
1928 << ".end());\n";
1929 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001930 }
1931
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001932 O.indent(Indent2) << "return Action(cmd, vec, stop_compilation, out_file);\n";
1933 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001934}
1935
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001936/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1937/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001938void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
1939 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001940 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001941 if (!ToolDesc.isJoin()) {
1942 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
1943 O.indent(Indent2) << "bool HasChildren,\n";
1944 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1945 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1946 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1947 O.indent(Indent1) << "{\n";
1948 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
1949 << " is not a Join tool!\");\n";
1950 O.indent(Indent1) << "}\n\n";
1951 }
1952 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001953 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001954 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001955
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001956 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001957}
1958
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001959/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1960/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001961void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001962 O.indent(Indent1) << "const char** InputLanguages() const {\n";
1963 O.indent(Indent2) << "return InputLanguages_;\n";
1964 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001965
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001966 if (D.OutLanguage.empty())
1967 throw "Tool " + D.Name + " has no 'out_language' property!";
1968
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001969 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
1970 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
1971 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001972}
1973
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001974/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001975void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001976 O.indent(Indent1) << "const char* Name() const {\n";
1977 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
1978 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001979}
1980
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001981/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1982/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001983void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001984 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001985 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001986 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001987 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001988 O.indent(Indent2) << "return false;\n";
1989 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001990}
1991
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001992/// EmitStaticMemberDefinitions - Emit static member definitions for a
1993/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001994void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001995 if (D.InLanguage.empty())
1996 throw "Tool " + D.Name + " has no 'in_language' property!";
1997
1998 O << "const char* " << D.Name << "::InputLanguages_[] = {";
1999 for (StrVector::const_iterator B = D.InLanguage.begin(),
2000 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002001 O << '\"' << *B << "\", ";
2002 O << "0};\n\n";
2003}
2004
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002005/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002006void EmitToolClassDefinition (const ToolDescription& D,
2007 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002008 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002009 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002010 return;
2011
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002012 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002013 O << "class " << D.Name << " : public ";
2014 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002015 O << "JoinTool";
2016 else
2017 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002018
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002019 O << "{\nprivate:\n";
2020 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002021
2022 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002023 EmitNameMethod(D, O);
2024 EmitInOutLanguageMethods(D, O);
2025 EmitIsJoinMethod(D, O);
2026 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002027
2028 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002029 O << "};\n";
2030
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002031 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002032
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002033}
2034
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002035/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002036/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002037void EmitOptionDefinitions (const OptionDescriptions& descs,
2038 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002039 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002040{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002041 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002042
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002043 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002044 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002045 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002046 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002047
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002048 if (val.Type == OptionType::Alias) {
2049 Aliases.push_back(val);
2050 continue;
2051 }
2052
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002053 if (val.isExtern())
2054 O << "extern ";
2055
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002056 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002057 << val.GenVariableName();
2058
2059 if (val.isExtern()) {
2060 O << ";\n";
2061 continue;
2062 }
2063
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002064 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002065
2066 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2067 O << ", cl::Prefix";
2068
2069 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002070 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002071 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002072 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002073 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002074 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002075 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002076 O << ", cl::OneOrMore";
2077 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002078 else if (val.isZeroOrOne() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002079 O << ", cl::ZeroOrOne";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002080 }
2081
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002082 if (val.isReallyHidden()) {
2083 O << ", cl::ReallyHidden";
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002084 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002085 else if (val.isHidden()) {
2086 O << ", cl::Hidden";
2087 }
2088
2089 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002090 O << ", cl::multi_val(" << val.MultiVal << ')';
2091
2092 if (val.InitVal) {
2093 const std::string& str = val.InitVal->getAsString();
2094 O << ", cl::init(" << str << ')';
2095 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002096
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002097 if (!val.Help.empty())
2098 O << ", cl::desc(\"" << val.Help << "\")";
2099
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002100 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002101 }
2102
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002103 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002104 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002105 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002106 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002107
2108 O << val.GenTypeDeclaration() << ' '
2109 << val.GenVariableName()
2110 << "(\"" << val.Name << '\"';
2111
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002112 const OptionDescription& D = descs.FindOption(val.Help);
2113 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002114
2115 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2116 }
2117
2118 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002119 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002120 O << (HasExterns ? "extern cl" : "cl")
2121 << "::list<std::string> " << SinkOptionName
2122 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002123
2124 O << '\n';
2125}
2126
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002127/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002128/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002129class EmitPreprocessOptionsCallback : ActionHandlingCallbackBase {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002130 const OptionDescriptions& OptDescs_;
2131
2132 void onUnsetOption(Init* i, unsigned IndentLevel, raw_ostream& O) {
2133 const std::string& OptName = InitPtrToString(i);
2134 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002135
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002136 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002137 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2138 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002139 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002140 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2141 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002142 else if (OptDesc.isList()) {
2143 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2144 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002145 else {
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002146 throw "Can't apply 'unset_option' to alias option '" + OptName + "'";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002147 }
2148 }
2149
2150 void processDag(const Init* I, unsigned IndentLevel, raw_ostream& O)
2151 {
2152 const DagInit& d = InitPtrToDag(I);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002153 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002154
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002155 if (OpName == "warning") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002156 this->onWarningDag(d, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002157 }
2158 else if (OpName == "error") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002159 this->onWarningDag(d, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002160 }
2161 else if (OpName == "unset_option") {
2162 checkNumberOfArguments(&d, 1);
2163 Init* I = d.getArg(0);
2164 if (typeid(*I) == typeid(ListInit)) {
2165 const ListInit& DagList = *static_cast<const ListInit*>(I);
2166 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
2167 B != E; ++B)
2168 this->onUnsetOption(*B, IndentLevel, O);
2169 }
2170 else {
2171 this->onUnsetOption(I, IndentLevel, O);
2172 }
2173 }
2174 else {
2175 throw "Unknown operator in the option preprocessor: '" + OpName + "'!"
2176 "\nOnly 'warning', 'error' and 'unset_option' are allowed.";
2177 }
2178 }
2179
2180public:
2181
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002182 void operator()(const Init* I, unsigned IndentLevel, raw_ostream& O) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002183 this->processDag(I, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002184 }
2185
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002186 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002187 : OptDescs_(OptDescs)
2188 {}
2189};
2190
2191/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2192void EmitPreprocessOptions (const RecordKeeper& Records,
2193 const OptionDescriptions& OptDecs, raw_ostream& O)
2194{
2195 O << "void PreprocessOptionsLocal() {\n";
2196
2197 const RecordVector& OptionPreprocessors =
2198 Records.getAllDerivedDefinitions("OptionPreprocessor");
2199
2200 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2201 E = OptionPreprocessors.end(); B!=E; ++B) {
2202 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002203 EmitCaseConstructHandler(Case, Indent1,
2204 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002205 false, OptDecs, O);
2206 }
2207
2208 O << "}\n\n";
2209}
2210
2211/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002212void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002213{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002214 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002215
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002216 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002217 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002218
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002219 // It is allowed for a plugin to have no language map.
2220 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002221
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002222 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2223 if (!LangsToSuffixesList)
2224 throw std::string("Error in the language map definition!");
2225
2226 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002227 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002228
2229 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2230 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2231
2232 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002233 O.indent(Indent1) << "langMap[\""
2234 << InitPtrToString(Suffixes->getElement(i))
2235 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002236 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002237 }
2238
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002239 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002240}
2241
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002242/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2243/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002244void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002245 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002246 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002247 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002248
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002249 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002250 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002251 }
2252 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002253 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002254 }
2255 else if (OpName == "error") {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002256 checkNumberOfArguments(&d, 1);
2257 O.indent(IndentLevel) << "throw std::runtime_error(\""
2258 << InitPtrToString(d.getArg(0))
2259 << "\");\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002260 return;
2261 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002262 else {
2263 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002264 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002265 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002266
2267 if (d.getNumArgs() > 0)
2268 O << InitPtrToInt(d.getArg(0)) << ";\n";
2269 else
2270 O << "2;\n";
2271
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002272}
2273
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002274/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002275void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002276 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002277 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002278
2279 // Class constructor.
2280 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002281 << "public:\n";
2282 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2283 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002284
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002285 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002286 O.indent(Indent1)
2287 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2288 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002289
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002290 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002291 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002292
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002293 O.indent(Indent2) << "return ret;\n";
2294 O.indent(Indent1) << "};\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002295}
2296
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002297/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002298void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002299 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002300 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002301 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002302 for (RecordVector::const_iterator B = EdgeVector.begin(),
2303 E = EdgeVector.end(); B != E; ++B) {
2304 const Record* Edge = *B;
2305 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002306 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002307
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002308 if (!isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002309 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002310 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002311 }
2312}
2313
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002314/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002315/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002316void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002317 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002318 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002319{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002320 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002321
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002322 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2323 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002324 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002325
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002326 O << '\n';
2327
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002328 // Insert edges.
2329
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002330 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002331 for (RecordVector::const_iterator B = EdgeVector.begin(),
2332 E = EdgeVector.end(); B != E; ++B) {
2333 const Record* Edge = *B;
2334 const std::string& NodeA = Edge->getValueAsString("a");
2335 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002336 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002337
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002338 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002339
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002340 if (isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002341 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002342 else
2343 O << "new Edge" << i << "()";
2344
2345 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002346 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002347 }
2348
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002349 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002350}
2351
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002352/// ExtractHookNames - Extract the hook names from all instances of
2353/// $CALL(HookName) in the provided command line string. Helper
2354/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002355class ExtractHookNames {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002356 llvm::StringMap<unsigned>& HookNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002357public:
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002358 ExtractHookNames(llvm::StringMap<unsigned>& HookNames)
2359 : HookNames_(HookNames) {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002360
2361 void operator()(const Init* CmdLine) {
2362 StrVector cmds;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002363
2364 // Ignore nested 'case' DAG.
2365 if (typeid(*CmdLine) == typeid(DagInit))
2366 return;
2367
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002368 TokenizeCmdline(InitPtrToString(CmdLine), cmds);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002369 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2370 B != E; ++B) {
2371 const std::string& cmd = *B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002372
2373 if (cmd == "$CALL") {
2374 unsigned NumArgs = 0;
2375 checkedIncrement(B, E, "Syntax error in $CALL invocation!");
2376 const std::string& HookName = *B;
2377
2378
2379 if (HookName.at(0) == ')')
2380 throw "$CALL invoked with no arguments!";
2381
2382 while (++B != E && B->at(0) != ')') {
2383 ++NumArgs;
2384 }
2385
2386 StringMap<unsigned>::const_iterator H = HookNames_.find(HookName);
2387
2388 if (H != HookNames_.end() && H->second != NumArgs)
2389 throw "Overloading of hooks is not allowed. Overloaded hook: "
2390 + HookName;
2391 else
2392 HookNames_[HookName] = NumArgs;
2393
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002394 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002395 }
2396 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002397
2398 void operator()(const DagInit* Test, unsigned, bool) {
2399 this->operator()(Test);
2400 }
2401 void operator()(const Init* Statement, unsigned) {
2402 this->operator()(Statement);
2403 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002404};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002405
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002406/// FillInHookNames - Actually extract the hook names from all command
2407/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002408void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002409 llvm::StringMap<unsigned>& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002410{
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002411 // For all command lines:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002412 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2413 E = ToolDescs.end(); B != E; ++B) {
2414 const ToolDescription& D = *(*B);
2415 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002416 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002417 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002418 // This is a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002419 ExtractHookNames(HookNames).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002420 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002421 // This is a 'case' construct.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002422 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002423 }
2424}
2425
2426/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2427/// property records and emit hook function declaration for each
2428/// instance of $CALL(HookName).
Daniel Dunbar1a551802009-07-03 00:10:29 +00002429void EmitHookDeclarations(const ToolDescriptions& ToolDescs, raw_ostream& O) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002430 llvm::StringMap<unsigned> HookNames;
2431
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002432 FillInHookNames(ToolDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002433 if (HookNames.empty())
2434 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002435
2436 O << "namespace hooks {\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002437 for (StringMap<unsigned>::const_iterator B = HookNames.begin(),
2438 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002439 O.indent(Indent1) << "std::string " << B->first() << "(";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002440
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002441 for (unsigned i = 0, j = B->second; i < j; ++i) {
2442 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2443 }
2444
2445 O <<");\n";
2446 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002447 O << "}\n\n";
2448}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002449
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002450/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002451void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002452 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2453 O.indent(Indent1) << "int Priority() const { return "
2454 << Priority << "; }\n\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002455 O.indent(Indent1) << "void PreprocessOptions() const\n";
2456 O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002457 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2458 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2459 O.indent(Indent1)
2460 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2461 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2462 << "};\n\n"
2463 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002464}
2465
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002466/// EmitIncludes - Emit necessary #include directives and some
2467/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002468void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002469 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2470 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002471 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002472 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2473 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002474
2475 << "#include \"llvm/ADT/StringExtras.h\"\n"
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002476 << "#include \"llvm/Support/CommandLine.h\"\n"
2477 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002478
2479 << "#include <cstdlib>\n"
2480 << "#include <stdexcept>\n\n"
2481
2482 << "using namespace llvm;\n"
2483 << "using namespace llvmc;\n\n"
2484
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002485 << "extern cl::opt<std::string> OutputFilename;\n\n"
2486
2487 << "inline const char* checkCString(const char* s)\n"
2488 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002489}
2490
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002491
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002492/// PluginData - Holds all information about a plugin.
2493struct PluginData {
2494 OptionDescriptions OptDescs;
2495 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002496 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002497 ToolDescriptions ToolDescs;
2498 RecordVector Edges;
2499 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002500};
2501
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002502/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002503/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002504bool HasSink(const ToolDescriptions& ToolDescs) {
2505 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2506 E = ToolDescs.end(); B != E; ++B)
2507 if ((*B)->isSink())
2508 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002509
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002510 return false;
2511}
2512
2513/// HasExterns - Go through the list of option descriptions and check
2514/// if there are any external options.
2515bool HasExterns(const OptionDescriptions& OptDescs) {
2516 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2517 E = OptDescs.end(); B != E; ++B)
2518 if (B->second.isExtern())
2519 return true;
2520
2521 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002522}
2523
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002524/// CollectPluginData - Collect tool and option properties,
2525/// compilation graph edges and plugin priority from the parse tree.
2526void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2527 // Collect option properties.
2528 const RecordVector& OptionLists =
2529 Records.getAllDerivedDefinitions("OptionList");
2530 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2531 Data.OptDescs);
2532
2533 // Collect tool properties.
2534 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2535 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2536 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002537 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002538
2539 // Collect compilation graph edges.
2540 const RecordVector& CompilationGraphs =
2541 Records.getAllDerivedDefinitions("CompilationGraph");
2542 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2543 Data.Edges);
2544
2545 // Calculate the priority of this plugin.
2546 const RecordVector& Priorities =
2547 Records.getAllDerivedDefinitions("PluginPriority");
2548 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002549}
2550
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002551/// CheckPluginData - Perform some sanity checks on the collected data.
2552void CheckPluginData(PluginData& Data) {
2553 // Filter out all tools not mentioned in the compilation graph.
2554 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002555
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002556 // Typecheck the compilation graph.
2557 TypecheckGraph(Data.Edges, Data.ToolDescs);
2558
2559 // Check that there are no options without side effects (specified
2560 // only in the OptionList).
2561 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2562
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002563}
2564
Daniel Dunbar1a551802009-07-03 00:10:29 +00002565void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002566 // Emit file header.
2567 EmitIncludes(O);
2568
2569 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002570 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002571
2572 // Emit hook declarations.
2573 EmitHookDeclarations(Data.ToolDescs, O);
2574
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002575 O << "namespace {\n\n";
2576
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002577 // Emit PreprocessOptionsLocal() function.
2578 EmitPreprocessOptions(Records, Data.OptDescs, O);
2579
2580 // Emit PopulateLanguageMapLocal() function
2581 // (language map maps from file extensions to language names).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002582 EmitPopulateLanguageMap(Records, O);
2583
2584 // Emit Tool classes.
2585 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2586 E = Data.ToolDescs.end(); B!=E; ++B)
2587 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2588
2589 // Emit Edge# classes.
2590 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2591
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002592 // Emit PopulateCompilationGraphLocal() function.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002593 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2594
2595 // Emit code for plugin registration.
2596 EmitRegisterPlugin(Data.Priority, O);
2597
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002598 O << "} // End anonymous namespace.\n\n";
2599
2600 // Force linkage magic.
2601 O << "namespace llvmc {\n";
2602 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2603 O << "}\n";
2604
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002605 // EOF
2606}
2607
2608
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002609// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00002610}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002611
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002612/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002613void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002614 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002615 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002616
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002617 CollectPluginData(Records, Data);
2618 CheckPluginData(Data);
2619
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +00002620 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002621 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002622
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002623 } catch (std::exception& Error) {
2624 throw Error.what() + std::string(" - usually this means a syntax error.");
2625 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002626}