blob: 309983aeebf29bf58cf3e3ed6ddb166541db4416 [file] [log] [blame]
Mikhail Glushenkovfb37f392008-05-30 06:20:54 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000010// This tablegen backend is responsible for emitting LLVMC configuration code.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkovecbdcf22008-05-06 18:09:29 +000014#include "LLVMCConfigurationEmitter.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000015#include "Record.h"
16
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/StringMap.h"
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000021#include "llvm/ADT/StringSet.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000022#include <algorithm>
23#include <cassert>
24#include <functional>
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +000025#include <stdexcept>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000026#include <string>
Chris Lattner32a9e7a2008-06-04 04:46:14 +000027#include <typeinfo>
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +000028
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000029using namespace llvm;
30
Mikhail Glushenkov895820d2008-05-06 18:12:03 +000031namespace {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000032
33//===----------------------------------------------------------------------===//
34/// Typedefs
35
36typedef std::vector<Record*> RecordVector;
37typedef std::vector<std::string> StrVector;
38
39//===----------------------------------------------------------------------===//
40/// Constants
41
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +000042// Indentation.
43unsigned TabWidth = 4;
Mikhail Glushenkov9d7a2dc2009-09-28 01:15:44 +000044unsigned Indent1 = TabWidth*1;
45unsigned Indent2 = TabWidth*2;
46unsigned Indent3 = TabWidth*3;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000047
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000048// Default help string.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000049const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
50
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000051// Name for the "sink" option.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000052const char * SinkOptionName = "AutoGeneratedSinkOption";
53
54//===----------------------------------------------------------------------===//
55/// Helper functions
56
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000057/// Id - An 'identity' function object.
58struct Id {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000059 template<typename T0>
60 void operator()(const T0&) const {
61 }
62 template<typename T0, typename T1>
63 void operator()(const T0&, const T1&) const {
64 }
65 template<typename T0, typename T1, typename T2>
66 void operator()(const T0&, const T1&, const T2&) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000067 }
68};
69
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000070int InitPtrToInt(const Init* ptr) {
71 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000072 return val.getValue();
73}
74
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000075const std::string& InitPtrToString(const Init* ptr) {
76 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
77 return val.getValue();
78}
79
80const ListInit& InitPtrToList(const Init* ptr) {
81 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
82 return val;
83}
84
85const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000086 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000087 return val;
88}
89
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000090const std::string GetOperatorName(const DagInit* D) {
91 return D->getOperator()->getAsString();
92}
93
94const std::string GetOperatorName(const DagInit& D) {
95 return GetOperatorName(&D);
96}
97
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000098// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +000099// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000100void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000101 if (!d || d->getNumArgs() < min_arguments)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000102 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000103}
104
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000105// isDagEmpty - is this DAG marked with an empty marker?
106bool isDagEmpty (const DagInit* d) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000107 return GetOperatorName(d) == "empty_dag_marker";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000108}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000109
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000110// EscapeVariableName - Escape commas and other symbols not allowed
111// in the C++ variable names. Makes it possible to use options named
112// like "Wa," (useful for prefix options).
113std::string EscapeVariableName(const std::string& Var) {
114 std::string ret;
115 for (unsigned i = 0; i != Var.size(); ++i) {
116 char cur_char = Var[i];
117 if (cur_char == ',') {
118 ret += "_comma_";
119 }
120 else if (cur_char == '+') {
121 ret += "_plus_";
122 }
123 else if (cur_char == '-') {
124 ret += "_dash_";
125 }
126 else {
127 ret.push_back(cur_char);
128 }
129 }
130 return ret;
131}
132
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000133/// oneOf - Does the input string contain this character?
134bool oneOf(const char* lst, char c) {
135 while (*lst) {
136 if (*lst++ == c)
137 return true;
138 }
139 return false;
140}
141
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000142template <class I, class S>
143void checkedIncrement(I& P, I E, S ErrorString) {
144 ++P;
145 if (P == E)
146 throw ErrorString;
147}
148
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000149//===----------------------------------------------------------------------===//
150/// Back-end specific code
151
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000152
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000153/// OptionType - One of six different option types. See the
154/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000155namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000156
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000157 enum OptionType { Alias, Switch, Parameter, ParameterList,
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000158 Prefix, PrefixList};
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000159
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000160 bool IsList (OptionType t) {
161 return (t == ParameterList || t == PrefixList);
162 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000163
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000164 bool IsSwitch (OptionType t) {
165 return (t == Switch);
166 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000167
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000168 bool IsParameter (OptionType t) {
169 return (t == Parameter || t == Prefix);
170 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000171
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000172}
173
174OptionType::OptionType stringToOptionType(const std::string& T) {
175 if (T == "alias_option")
176 return OptionType::Alias;
177 else if (T == "switch_option")
178 return OptionType::Switch;
179 else if (T == "parameter_option")
180 return OptionType::Parameter;
181 else if (T == "parameter_list_option")
182 return OptionType::ParameterList;
183 else if (T == "prefix_option")
184 return OptionType::Prefix;
185 else if (T == "prefix_list_option")
186 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000187 else
188 throw "Unknown option type: " + T + '!';
189}
190
191namespace OptionDescriptionFlags {
192 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000193 ReallyHidden = 0x4, Extern = 0x8,
194 OneOrMore = 0x10, ZeroOrOne = 0x20 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000195}
196
197/// OptionDescription - Represents data contained in a single
198/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000199struct OptionDescription {
200 OptionType::OptionType Type;
201 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000202 unsigned Flags;
203 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000204 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000205 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000206
207 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000208 const std::string& n = "",
209 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000210 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000211 {}
212
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000213 /// GenTypeDeclaration - Returns the C++ variable type of this
214 /// option.
215 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000216
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000217 /// GenVariableName - Returns the variable name used in the
218 /// generated C++ code.
219 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000220
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000221 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000222 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000223
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000224 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000225
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000226 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000227
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000228 bool isMultiVal() const;
229
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000230 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000231 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000232
233 bool isRequired() const;
234 void setRequired();
235
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000236 bool isOneOrMore() const;
237 void setOneOrMore();
238
239 bool isZeroOrOne() const;
240 void setZeroOrOne();
241
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000242 bool isHidden() const;
243 void setHidden();
244
245 bool isReallyHidden() const;
246 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000247
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000248 bool isParameter() const
249 { return OptionType::IsParameter(this->Type); }
250
251 bool isSwitch() const
252 { return OptionType::IsSwitch(this->Type); }
253
254 bool isList() const
255 { return OptionType::IsList(this->Type); }
256
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000257};
258
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000259void OptionDescription::Merge (const OptionDescription& other)
260{
261 if (other.Type != Type)
262 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000263
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000264 if (Help == other.Help || Help == DefaultHelpString)
265 Help = other.Help;
266 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000267 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000268 " defined for option " + Name + "\n";
269 }
270
271 Flags |= other.Flags;
272}
273
274bool OptionDescription::isAlias() const {
275 return Type == OptionType::Alias;
276}
277
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000278bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000279 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000280}
281
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000282bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000283 return Flags & OptionDescriptionFlags::Extern;
284}
285void OptionDescription::setExtern() {
286 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000287}
288
289bool OptionDescription::isRequired() const {
290 return Flags & OptionDescriptionFlags::Required;
291}
292void OptionDescription::setRequired() {
293 Flags |= OptionDescriptionFlags::Required;
294}
295
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000296bool OptionDescription::isOneOrMore() const {
297 return Flags & OptionDescriptionFlags::OneOrMore;
298}
299void OptionDescription::setOneOrMore() {
300 Flags |= OptionDescriptionFlags::OneOrMore;
301}
302
303bool OptionDescription::isZeroOrOne() const {
304 return Flags & OptionDescriptionFlags::ZeroOrOne;
305}
306void OptionDescription::setZeroOrOne() {
307 Flags |= OptionDescriptionFlags::ZeroOrOne;
308}
309
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000310bool OptionDescription::isHidden() const {
311 return Flags & OptionDescriptionFlags::Hidden;
312}
313void OptionDescription::setHidden() {
314 Flags |= OptionDescriptionFlags::Hidden;
315}
316
317bool OptionDescription::isReallyHidden() const {
318 return Flags & OptionDescriptionFlags::ReallyHidden;
319}
320void OptionDescription::setReallyHidden() {
321 Flags |= OptionDescriptionFlags::ReallyHidden;
322}
323
324const char* OptionDescription::GenTypeDeclaration() const {
325 switch (Type) {
326 case OptionType::Alias:
327 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000328 case OptionType::PrefixList:
329 case OptionType::ParameterList:
330 return "cl::list<std::string>";
331 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000332 return "cl::opt<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000333 case OptionType::Parameter:
334 case OptionType::Prefix:
335 default:
336 return "cl::opt<std::string>";
337 }
338}
339
340std::string OptionDescription::GenVariableName() const {
341 const std::string& EscapedName = EscapeVariableName(Name);
342 switch (Type) {
343 case OptionType::Alias:
344 return "AutoGeneratedAlias_" + EscapedName;
345 case OptionType::PrefixList:
346 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000347 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000348 case OptionType::Switch:
349 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000350 case OptionType::Prefix:
351 case OptionType::Parameter:
352 default:
353 return "AutoGeneratedParameter_" + EscapedName;
354 }
355}
356
357/// OptionDescriptions - An OptionDescription array plus some helper
358/// functions.
359class OptionDescriptions {
360 typedef StringMap<OptionDescription> container_type;
361
362 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000363 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000364
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000365public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000366 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000367 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000368 /// FindSwitch - wrapper for FindOption that throws in case the option is not
369 /// a switch.
370 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000371
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000372 /// insertDescription - Insert new OptionDescription into
373 /// OptionDescriptions list
374 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000375
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000376 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000377 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000378 const_iterator begin() const { return Descriptions.begin(); }
379 const_iterator end() const { return Descriptions.end(); }
380};
381
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000382const OptionDescription&
383OptionDescriptions::FindOption(const std::string& OptName) const
384{
385 const_iterator I = Descriptions.find(OptName);
386 if (I != Descriptions.end())
387 return I->second;
388 else
389 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000390}
391
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000392const OptionDescription&
393OptionDescriptions::FindSwitch(const std::string& OptName) const
394{
395 const OptionDescription& OptDesc = this->FindOption(OptName);
396 if (!OptDesc.isSwitch())
397 throw OptName + ": incorrect option type - should be a switch!";
398 return OptDesc;
399}
400
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000401void OptionDescriptions::InsertDescription (const OptionDescription& o)
402{
403 container_type::iterator I = Descriptions.find(o.Name);
404 if (I != Descriptions.end()) {
405 OptionDescription& D = I->second;
406 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000407 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000408 else {
409 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000410 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000411}
412
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000413/// HandlerTable - A base class for function objects implemented as
414/// 'tables of handlers'.
415template <class T>
416class HandlerTable {
417protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000418 // Implementation details.
419
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000420 /// Handler -
421 typedef void (T::* Handler) (const DagInit*);
422 /// HandlerMap - A map from property names to property handlers
423 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000424
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000425 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000426 static bool staticMembersInitialized_;
427
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000428 T* childPtr;
429public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000430
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000431 HandlerTable(T* cp) : childPtr(cp)
432 {}
433
434 /// operator() - Just forwards to the corresponding property
435 /// handler.
436 void operator() (Init* i) {
437 const DagInit& property = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000438 const std::string& property_name = GetOperatorName(property);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000439 typename HandlerMap::iterator method = Handlers_.find(property_name);
440
441 if (method != Handlers_.end()) {
442 Handler h = method->second;
443 (childPtr->*h)(&property);
444 }
445 else {
446 throw "No handler found for property " + property_name + "!";
447 }
448 }
449
450 void AddHandler(const char* Property, Handler Handl) {
451 Handlers_[Property] = Handl;
452 }
453};
454
455template <class T> typename HandlerTable<T>::HandlerMap
456HandlerTable<T>::Handlers_;
457template <class T> bool HandlerTable<T>::staticMembersInitialized_ = false;
458
459
460/// CollectOptionProperties - Function object for iterating over an
461/// option property list.
462class CollectOptionProperties : public HandlerTable<CollectOptionProperties> {
463private:
464
465 /// optDescs_ - OptionDescriptions table. This is where the
466 /// information is stored.
467 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000468
469public:
470
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000471 explicit CollectOptionProperties(OptionDescription& OD)
472 : HandlerTable<CollectOptionProperties>(this), optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000473 {
474 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000475 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000476 AddHandler("help", &CollectOptionProperties::onHelp);
477 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000478 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000479 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
480 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000481 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
482 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000483 AddHandler("zero_or_one", &CollectOptionProperties::onZeroOrOne);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000484
485 staticMembersInitialized_ = true;
486 }
487 }
488
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000489private:
490
491 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000492 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000493
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000494 void onExtern (const DagInit* d) {
495 checkNumberOfArguments(d, 0);
496 optDesc_.setExtern();
497 }
498
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000499 void onHelp (const DagInit* d) {
500 checkNumberOfArguments(d, 1);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000501 optDesc_.Help = InitPtrToString(d->getArg(0));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000502 }
503
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000504 void onHidden (const DagInit* d) {
505 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000506 optDesc_.setHidden();
507 }
508
509 void onReallyHidden (const DagInit* d) {
510 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000511 optDesc_.setReallyHidden();
512 }
513
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000514 void onRequired (const DagInit* d) {
515 checkNumberOfArguments(d, 0);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000516 if (optDesc_.isOneOrMore())
517 throw std::string("An option can't have both (required) "
518 "and (one_or_more) properties!");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000519 optDesc_.setRequired();
520 }
521
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000522 void onInit (const DagInit* d) {
523 checkNumberOfArguments(d, 1);
524 Init* i = d->getArg(0);
525 const std::string& str = i->getAsString();
526
527 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
528 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
529
530 if (!correct)
531 throw std::string("Incorrect usage of the 'init' option property!");
532
533 optDesc_.InitVal = i;
534 }
535
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000536 void onOneOrMore (const DagInit* d) {
537 checkNumberOfArguments(d, 0);
538 if (optDesc_.isRequired() || optDesc_.isZeroOrOne())
539 throw std::string("Only one of (required), (zero_or_one) or "
540 "(one_or_more) properties is allowed!");
541 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000542 llvm::errs() << "Warning: specifying the 'one_or_more' property "
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000543 "on a non-list option will have no effect.\n";
544 optDesc_.setOneOrMore();
545 }
546
547 void onZeroOrOne (const DagInit* d) {
548 checkNumberOfArguments(d, 0);
549 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
550 throw std::string("Only one of (required), (zero_or_one) or "
551 "(one_or_more) properties is allowed!");
552 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000553 llvm::errs() << "Warning: specifying the 'zero_or_one' property"
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000554 "on a non-list option will have no effect.\n";
555 optDesc_.setZeroOrOne();
556 }
557
558 void onMultiVal (const DagInit* d) {
559 checkNumberOfArguments(d, 1);
560 int val = InitPtrToInt(d->getArg(0));
561 if (val < 2)
562 throw std::string("Error in the 'multi_val' property: "
563 "the value must be greater than 1!");
564 if (!OptionType::IsList(optDesc_.Type))
565 throw std::string("The multi_val property is valid only "
566 "on list options!");
567 optDesc_.MultiVal = val;
568 }
569
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000570};
571
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000572/// AddOption - A function object that is applied to every option
573/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000574class AddOption {
575private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000576 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000577
578public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000579 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000580 {}
581
582 void operator()(const Init* i) {
583 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000584 checkNumberOfArguments(&d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000585
586 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000587 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000588 const std::string& Name = InitPtrToString(d.getArg(0));
589
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000590 OptionDescription OD(Type, Name);
591
592 if (!OD.isExtern())
593 checkNumberOfArguments(&d, 2);
594
595 if (OD.isAlias()) {
596 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000597 OD.Help = InitPtrToString(d.getArg(1));
598 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000599 else if (!OD.isExtern()) {
600 processOptionProperties(&d, OD);
601 }
602 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000603 }
604
605private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000606 /// processOptionProperties - Go through the list of option
607 /// properties and call a corresponding handler for each.
608 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
609 checkNumberOfArguments(d, 2);
610 DagInit::const_arg_iterator B = d->arg_begin();
611 // Skip the first argument: it's always the option name.
612 ++B;
613 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000614 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000615
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000616};
617
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000618/// CollectOptionDescriptions - Collects option properties from all
619/// OptionLists.
620void CollectOptionDescriptions (RecordVector::const_iterator B,
621 RecordVector::const_iterator E,
622 OptionDescriptions& OptDescs)
623{
624 // For every OptionList:
625 for (; B!=E; ++B) {
626 RecordVector::value_type T = *B;
627 // Throws an exception if the value does not exist.
628 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000629
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000630 // For every option description in this list:
631 // collect the information and
632 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
633 }
634}
635
636// Tool information record
637
638namespace ToolFlags {
639 enum ToolFlags { Join = 0x1, Sink = 0x2 };
640}
641
642struct ToolDescription : public RefCountedBase<ToolDescription> {
643 std::string Name;
644 Init* CmdLine;
645 Init* Actions;
646 StrVector InLanguage;
647 std::string OutLanguage;
648 std::string OutputSuffix;
649 unsigned Flags;
650
651 // Various boolean properties
652 void setSink() { Flags |= ToolFlags::Sink; }
653 bool isSink() const { return Flags & ToolFlags::Sink; }
654 void setJoin() { Flags |= ToolFlags::Join; }
655 bool isJoin() const { return Flags & ToolFlags::Join; }
656
657 // Default ctor here is needed because StringMap can only store
658 // DefaultConstructible objects
659 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
660 ToolDescription (const std::string& n)
661 : Name(n), CmdLine(0), Actions(0), Flags(0)
662 {}
663};
664
665/// ToolDescriptions - A list of Tool information records.
666typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
667
668
669/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000670/// tool property records.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000671class CollectToolProperties : public HandlerTable<CollectToolProperties> {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000672private:
673
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000674 /// toolDesc_ - Properties of the current Tool. This is where the
675 /// information is stored.
676 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000677
678public:
679
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000680 explicit CollectToolProperties (ToolDescription& d)
681 : HandlerTable<CollectToolProperties>(this) , toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000682 {
683 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000684
685 AddHandler("actions", &CollectToolProperties::onActions);
686 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
687 AddHandler("in_language", &CollectToolProperties::onInLanguage);
688 AddHandler("join", &CollectToolProperties::onJoin);
689 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
690 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
691 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000692
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000693 staticMembersInitialized_ = true;
694 }
695 }
696
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000697private:
698
699 /// Property handlers --
700 /// Functions that extract information about tool properties from
701 /// DAG representation.
702
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000703 void onActions (const DagInit* d) {
704 checkNumberOfArguments(d, 1);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000705 Init* Case = d->getArg(0);
706 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000707 GetOperatorName(static_cast<DagInit*>(Case)) != "case")
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000708 throw
709 std::string("The argument to (actions) should be a 'case' construct!");
710 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000711 }
712
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000713 void onCmdLine (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000714 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000715 toolDesc_.CmdLine = d->getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000716 }
717
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000718 void onInLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000719 checkNumberOfArguments(d, 1);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000720 Init* arg = d->getArg(0);
721
722 // Find out the argument's type.
723 if (typeid(*arg) == typeid(StringInit)) {
724 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000725 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000726 }
727 else {
728 // It's a list.
729 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000730 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000731
732 // Copy strings to the output vector.
733 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
734 B != E; ++B) {
735 out.push_back(InitPtrToString(*B));
736 }
737
738 // Remove duplicates.
739 std::sort(out.begin(), out.end());
740 StrVector::iterator newE = std::unique(out.begin(), out.end());
741 out.erase(newE, out.end());
742 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000743 }
744
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000745 void onJoin (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000746 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000747 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000748 }
749
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000750 void onOutLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000751 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000752 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000753 }
754
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000755 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000756 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000757 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000758 }
759
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000760 void onSink (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000761 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000762 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000763 }
764
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000765};
766
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000767/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000768/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000769/// CollectToolProperties function object).
770void CollectToolDescriptions (RecordVector::const_iterator B,
771 RecordVector::const_iterator E,
772 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000773{
774 // Iterate over a properties list of every Tool definition
775 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000776 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000777 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000778 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000779
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000780 IntrusiveRefCntPtr<ToolDescription>
781 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000782
783 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000784 CollectToolProperties(*ToolDesc));
785 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000786 }
787}
788
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000789/// FillInEdgeVector - Merge all compilation graph definitions into
790/// one single edge list.
791void FillInEdgeVector(RecordVector::const_iterator B,
792 RecordVector::const_iterator E, RecordVector& Out) {
793 for (; B != E; ++B) {
794 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000795
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000796 for (unsigned i = 0; i < edges->size(); ++i)
797 Out.push_back(edges->getElementAsRecord(i));
798 }
799}
800
801/// CalculatePriority - Calculate the priority of this plugin.
802int CalculatePriority(RecordVector::const_iterator B,
803 RecordVector::const_iterator E) {
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000804 int priority = 0;
805
806 if (B != E) {
807 priority = static_cast<int>((*B)->getValueAsInt("priority"));
808
809 if (++B != E)
810 throw std::string("More than one 'PluginPriority' instance found: "
811 "most probably an error!");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000812 }
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000813
814 return priority;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000815}
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000816
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000817/// NotInGraph - Helper function object for FilterNotInGraph.
818struct NotInGraph {
819private:
820 const llvm::StringSet<>& ToolsInGraph_;
821
822public:
823 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
824 : ToolsInGraph_(ToolsInGraph)
825 {}
826
827 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
828 return (ToolsInGraph_.count(x->Name) == 0);
829 }
830};
831
832/// FilterNotInGraph - Filter out from ToolDescs all Tools not
833/// mentioned in the compilation graph definition.
834void FilterNotInGraph (const RecordVector& EdgeVector,
835 ToolDescriptions& ToolDescs) {
836
837 // List all tools mentioned in the graph.
838 llvm::StringSet<> ToolsInGraph;
839
840 for (RecordVector::const_iterator B = EdgeVector.begin(),
841 E = EdgeVector.end(); B != E; ++B) {
842
843 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000844 const std::string& NodeA = Edge->getValueAsString("a");
845 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000846
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000847 if (NodeA != "root")
848 ToolsInGraph.insert(NodeA);
849 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000850 }
851
852 // Filter ToolPropertiesList.
853 ToolDescriptions::iterator new_end =
854 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
855 NotInGraph(ToolsInGraph));
856 ToolDescs.erase(new_end, ToolDescs.end());
857}
858
859/// FillInToolToLang - Fills in two tables that map tool names to
860/// (input, output) languages. Helper function used by TypecheckGraph().
861void FillInToolToLang (const ToolDescriptions& ToolDescs,
862 StringMap<StringSet<> >& ToolToInLang,
863 StringMap<std::string>& ToolToOutLang) {
864 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
865 E = ToolDescs.end(); B != E; ++B) {
866 const ToolDescription& D = *(*B);
867 for (StrVector::const_iterator B = D.InLanguage.begin(),
868 E = D.InLanguage.end(); B != E; ++B)
869 ToolToInLang[D.Name].insert(*B);
870 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000871 }
872}
873
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000874/// TypecheckGraph - Check that names for output and input languages
875/// on all edges do match. This doesn't do much when the information
876/// about the whole graph is not available (i.e. when compiling most
877/// plugins).
878void TypecheckGraph (const RecordVector& EdgeVector,
879 const ToolDescriptions& ToolDescs) {
880 StringMap<StringSet<> > ToolToInLang;
881 StringMap<std::string> ToolToOutLang;
882
883 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
884 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
885 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
886
887 for (RecordVector::const_iterator B = EdgeVector.begin(),
888 E = EdgeVector.end(); B != E; ++B) {
889 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000890 const std::string& NodeA = Edge->getValueAsString("a");
891 const std::string& NodeB = Edge->getValueAsString("b");
892 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
893 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000894
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000895 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000896 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000897 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000898 + ": output->input language mismatch";
899 }
900
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000901 if (NodeB == "root")
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000902 throw std::string("Edges back to the root are not allowed!");
903 }
904}
905
906/// WalkCase - Walks the 'case' expression DAG and invokes
907/// TestCallback on every test, and StatementCallback on every
908/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000909/// combinators (that is, they are passed directly to TestCallback).
910/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
911/// IndentLevel, bool FirstTest)'.
912/// StatementCallback must have type 'void StatementCallback(const Init*,
913/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000914template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000915void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
916 unsigned IndentLevel = 0)
917{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000918 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000919
920 // Error checks.
921 if (GetOperatorName(d) != "case")
922 throw std::string("WalkCase should be invoked only on 'case' expressions!");
923
924 if (d.getNumArgs() < 2)
925 throw "There should be at least one clause in the 'case' expression:\n"
926 + d.getAsString();
927
928 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000929 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000930 const unsigned numArgs = d.getNumArgs();
931 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000932 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
933 B != E; ++B) {
934 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000935
936 if (!even)
937 {
938 // Handle test.
939 const DagInit& Test = InitPtrToDag(arg);
940
941 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
942 throw std::string("The 'default' clause should be the last in the"
943 "'case' construct!");
944 if (i == numArgs)
945 throw "Case construct handler: no corresponding action "
946 "found for the test " + Test.getAsString() + '!';
947
948 TestCallback(&Test, IndentLevel, (i == 1));
949 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000950 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000951 {
952 if (dynamic_cast<DagInit*>(arg)
953 && GetOperatorName(static_cast<DagInit*>(arg)) == "case") {
954 // Nested 'case'.
955 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
956 }
957
958 // Handle statement.
959 StatementCallback(arg, IndentLevel);
960 }
961
962 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000963 even = !even;
964 }
965}
966
967/// ExtractOptionNames - A helper function object used by
968/// CheckForSuperfluousOptions() to walk the 'case' DAG.
969class ExtractOptionNames {
970 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000971
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000972 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000973 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000974 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000975 if (ActionName == "forward" || ActionName == "forward_as" ||
976 ActionName == "unpack_values" || ActionName == "switch_on" ||
977 ActionName == "parameter_equals" || ActionName == "element_in_list" ||
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +0000978 ActionName == "not_empty" || ActionName == "empty") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000979 checkNumberOfArguments(&Stmt, 1);
980 const std::string& Name = InitPtrToString(Stmt.getArg(0));
981 OptionNames_.insert(Name);
982 }
983 else if (ActionName == "and" || ActionName == "or") {
984 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000985 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000986 }
987 }
988 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000989
990public:
991 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
992 {}
993
994 void operator()(const Init* Statement) {
995 if (typeid(*Statement) == typeid(ListInit)) {
996 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
997 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
998 B != E; ++B)
999 this->processDag(*B);
1000 }
1001 else {
1002 this->processDag(Statement);
1003 }
1004 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001005
1006 void operator()(const DagInit* Test, unsigned, bool) {
1007 this->operator()(Test);
1008 }
1009 void operator()(const Init* Statement, unsigned) {
1010 this->operator()(Statement);
1011 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001012};
1013
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001014/// CheckForSuperfluousOptions - Check that there are no side
1015/// effect-free options (specified only in the OptionList). Otherwise,
1016/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001017void CheckForSuperfluousOptions (const RecordVector& Edges,
1018 const ToolDescriptions& ToolDescs,
1019 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001020 llvm::StringSet<> nonSuperfluousOptions;
1021
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001022 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001023 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001024 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1025 E = ToolDescs.end(); B != E; ++B) {
1026 const ToolDescription& TD = *(*B);
1027 ExtractOptionNames Callback(nonSuperfluousOptions);
1028 if (TD.Actions)
1029 WalkCase(TD.Actions, Callback, Callback);
1030 }
1031
1032 // Add all options mentioned in the 'case' clauses of the
1033 // OptionalEdges of the compilation graph to the set of
1034 // non-superfluous options.
1035 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1036 B != E; ++B) {
1037 const Record* Edge = *B;
1038 DagInit* Weight = Edge->getValueAsDag("weight");
1039
1040 if (!isDagEmpty(Weight))
1041 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001042 }
1043
1044 // Check that all options in OptDescs belong to the set of
1045 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001046 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001047 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001048 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001049 if (!nonSuperfluousOptions.count(Val.Name)
1050 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001051 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001052 "Probable cause: this option is specified only in the OptionList.\n";
1053 }
1054}
1055
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001056/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1057bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1058 if (TestName == "single_input_file") {
1059 O << "InputFilenames.size() == 1";
1060 return true;
1061 }
1062 else if (TestName == "multiple_input_files") {
1063 O << "InputFilenames.size() > 1";
1064 return true;
1065 }
1066
1067 return false;
1068}
1069
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001070/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1071bool EmitCaseTest1ArgStr(const std::string& TestName,
1072 const DagInit& d,
1073 const OptionDescriptions& OptDescs,
1074 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001075 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001076
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001077 if (TestName == "switch_on") {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001078 const OptionDescription& OptDesc = OptDescs.FindSwitch(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001079 O << OptDesc.GenVariableName();
1080 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001081 }
1082 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001083 O << "InLangs.count(\"" << OptName << "\") != 0";
1084 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001085 }
1086 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001087 // This works only for single-argument Tool::GenerateAction. Join
1088 // tools can process several files in different languages simultaneously.
1089
1090 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001091 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001092 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001093 }
1094 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001095 const char* Test = (TestName == "empty") ? "" : "!";
1096
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001097 if (OptName == "o") {
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001098 O << Test << "OutputFilename.empty()";
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001099 return true;
1100 }
1101 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001102 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001103 if (OptDesc.isSwitch())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001104 throw OptName
1105 + ": incorrect option type - should be a list or parameter!";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001106 O << Test << OptDesc.GenVariableName() << ".empty()";
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001107 return true;
1108 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001109 }
1110
1111 return false;
1112}
1113
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001114/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1115bool EmitCaseTest1ArgList(const std::string& TestName,
1116 const DagInit& d,
1117 const OptionDescriptions& OptDescs,
1118 raw_ostream& O) {
1119 const ListInit& L = *static_cast<ListInit*>(d.getArg(0));
1120
1121 if (TestName == "any_switch_on") {
1122 bool isFirst = true;
1123
1124 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1125 const std::string& OptName = InitPtrToString(*B);
1126 const OptionDescription& OptDesc = OptDescs.FindSwitch(OptName);
1127
1128 if (isFirst)
1129 isFirst = false;
1130 else
1131 O << " || ";
1132 O << OptDesc.GenVariableName();
1133 }
1134
1135 return true;
1136 }
1137
1138 // TODO: implement any_not_empty, any_empty, switch_on [..], empty [..]
1139
1140 return false;
1141}
1142
1143/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1144bool EmitCaseTest1Arg(const std::string& TestName,
1145 const DagInit& d,
1146 const OptionDescriptions& OptDescs,
1147 raw_ostream& O) {
1148 checkNumberOfArguments(&d, 1);
1149 if (typeid(*d.getArg(0)) == typeid(ListInit))
1150 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1151 else
1152 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1153}
1154
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001155/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001156bool EmitCaseTest2Args(const std::string& TestName,
1157 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001158 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001159 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001160 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001161 checkNumberOfArguments(&d, 2);
1162 const std::string& OptName = InitPtrToString(d.getArg(0));
1163 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001164 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001165
1166 if (TestName == "parameter_equals") {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001167 if (!OptDesc.isParameter())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001168 throw OptName + ": incorrect option type - should be a parameter!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001169 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1170 return true;
1171 }
1172 else if (TestName == "element_in_list") {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001173 if (!OptDesc.isList())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001174 throw OptName + ": incorrect option type - should be a list!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001175 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001176 O << "std::find(" << VarName << ".begin(),\n";
1177 O.indent(IndentLevel + Indent1)
1178 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001179 << OptArg << "\") != " << VarName << ".end()";
1180 return true;
1181 }
1182
1183 return false;
1184}
1185
1186// Forward declaration.
1187// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001188void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001189 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001190 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001191
1192/// EmitLogicalOperationTest - Helper function used by
1193/// EmitCaseConstructHandler.
1194void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001195 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001196 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001197 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001198 O << '(';
1199 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001200 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001201 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001202 if (j != NumArgs - 1) {
1203 O << ")\n";
1204 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1205 }
1206 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001207 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001208 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001209 }
1210}
1211
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001212void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001213 const OptionDescriptions& OptDescs, raw_ostream& O)
1214{
1215 checkNumberOfArguments(&d, 1);
1216 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1217 O << "! (";
1218 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1219 O << ")";
1220}
1221
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001222/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001223void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001224 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001225 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001226 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001227
1228 if (TestName == "and")
1229 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1230 else if (TestName == "or")
1231 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001232 else if (TestName == "not")
1233 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001234 else if (EmitCaseTest0Args(TestName, O))
1235 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001236 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1237 return;
1238 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1239 return;
1240 else
1241 throw TestName + ": unknown edge property!";
1242}
1243
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001244
1245/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1246class EmitCaseTestCallback {
1247 bool EmitElseIf_;
1248 const OptionDescriptions& OptDescs_;
1249 raw_ostream& O_;
1250public:
1251
1252 EmitCaseTestCallback(bool EmitElseIf,
1253 const OptionDescriptions& OptDescs, raw_ostream& O)
1254 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1255 {}
1256
1257 void operator()(const DagInit* Test, unsigned IndentLevel, bool FirstTest)
1258 {
1259 if (GetOperatorName(Test) == "default") {
1260 O_.indent(IndentLevel) << "else {\n";
1261 }
1262 else {
1263 O_.indent(IndentLevel)
1264 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
1265 EmitCaseTest(*Test, IndentLevel, OptDescs_, O_);
1266 O_ << ") {\n";
1267 }
1268 }
1269};
1270
1271/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1272template <typename F>
1273class EmitCaseStatementCallback {
1274 F Callback_;
1275 raw_ostream& O_;
1276public:
1277
1278 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1279 : Callback_(Callback), O_(O)
1280 {}
1281
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001282 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001283
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001284 // Ignore nested 'case' DAG.
1285 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001286 GetOperatorName(static_cast<const DagInit*>(Statement)) == "case")) {
1287 if (typeid(*Statement) == typeid(ListInit)) {
1288 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1289 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1290 B != E; ++B)
1291 Callback_(*B, (IndentLevel + Indent1), O_);
1292 }
1293 else {
1294 Callback_(Statement, (IndentLevel + Indent1), O_);
1295 }
1296 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001297 O_.indent(IndentLevel) << "}\n";
1298 }
1299
1300};
1301
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001302/// EmitCaseConstructHandler - Emit code that handles the 'case'
1303/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001304/// clause. Implemented on top of WalkCase.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001305/// Callback's type is void F(Init* Statement, unsigned IndentLevel,
1306/// raw_ostream& O).
1307/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001308/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1309/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001310template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001311void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001312 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001313 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001314 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001315 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1316 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001317}
1318
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001319/// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to
1320/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path"] .
1321/// Helper function used by EmitCmdLineVecFill and.
1322void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
1323 const char* Delimiters = " \t\n\v\f\r";
1324 enum TokenizerState
1325 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1326 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001327
1328 if (CmdLine.empty())
1329 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001330 Out.push_back("");
1331
1332 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1333 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001334
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001335 for (; B != E; ++B) {
1336 char cur_ch = CmdLine[B];
1337
1338 switch (cur_st) {
1339 case Normal:
1340 if (cur_ch == '$') {
1341 cur_st = SpecialCommand;
1342 break;
1343 }
1344 if (oneOf(Delimiters, cur_ch)) {
1345 // Skip whitespace
1346 B = CmdLine.find_first_not_of(Delimiters, B);
1347 if (B == std::string::npos) {
1348 B = E-1;
1349 continue;
1350 }
1351 --B;
1352 Out.push_back("");
1353 continue;
1354 }
1355 break;
1356
1357
1358 case SpecialCommand:
1359 if (oneOf(Delimiters, cur_ch)) {
1360 cur_st = Normal;
1361 Out.push_back("");
1362 continue;
1363 }
1364 if (cur_ch == '(') {
1365 Out.push_back("");
1366 cur_st = InsideSpecialCommand;
1367 continue;
1368 }
1369 break;
1370
1371 case InsideSpecialCommand:
1372 if (oneOf(Delimiters, cur_ch)) {
1373 continue;
1374 }
1375 if (cur_ch == '\'') {
1376 cur_st = InsideQuotationMarks;
1377 Out.push_back("");
1378 continue;
1379 }
1380 if (cur_ch == ')') {
1381 cur_st = Normal;
1382 Out.push_back("");
1383 }
1384 if (cur_ch == ',') {
1385 continue;
1386 }
1387
1388 break;
1389
1390 case InsideQuotationMarks:
1391 if (cur_ch == '\'') {
1392 cur_st = InsideSpecialCommand;
1393 continue;
1394 }
1395 break;
1396 }
1397
1398 Out.back().push_back(cur_ch);
1399 }
1400}
1401
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001402/// SubstituteSpecialCommands - Perform string substitution for $CALL
1403/// and $ENV. Helper function used by EmitCmdLineVecFill().
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001404StrVector::const_iterator SubstituteSpecialCommands
Daniel Dunbar1a551802009-07-03 00:10:29 +00001405(StrVector::const_iterator Pos, StrVector::const_iterator End, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001406{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001407
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001408 const std::string& cmd = *Pos;
1409
1410 if (cmd == "$CALL") {
1411 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1412 const std::string& CmdName = *Pos;
1413
1414 if (CmdName == ")")
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001415 throw std::string("$CALL invocation: empty argument list!");
1416
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001417 O << "hooks::";
1418 O << CmdName << "(";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001419
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001420
1421 bool firstIteration = true;
1422 while (true) {
1423 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1424 const std::string& Arg = *Pos;
1425 assert(Arg.size() != 0);
1426
1427 if (Arg[0] == ')')
1428 break;
1429
1430 if (firstIteration)
1431 firstIteration = false;
1432 else
1433 O << ", ";
1434
1435 O << '"' << Arg << '"';
1436 }
1437
1438 O << ')';
1439
1440 }
1441 else if (cmd == "$ENV") {
1442 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
1443 const std::string& EnvName = *Pos;
1444
1445 if (EnvName == ")")
1446 throw "$ENV invocation: empty argument list!";
1447
1448 O << "checkCString(std::getenv(\"";
1449 O << EnvName;
1450 O << "\"))";
1451
1452 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001453 }
1454 else {
1455 throw "Unknown special command: " + cmd;
1456 }
1457
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001458 const std::string& Leftover = *Pos;
1459 assert(Leftover.at(0) == ')');
1460 if (Leftover.size() != 1)
1461 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001462
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001463 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001464}
1465
1466/// EmitCmdLineVecFill - Emit code that fills in the command line
1467/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001468void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001469 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001470 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001471 StrVector StrVec;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001472 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1473
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001474 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001475 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001476
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001477 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1478
1479 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001480 assert(!StrVec[0].empty());
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001481 if (StrVec[0][0] == '$') {
1482 while (I != E && (*I)[0] != ')' )
1483 ++I;
1484
1485 // Skip the ')' symbol.
1486 ++I;
1487 }
1488 else {
1489 ++I;
1490 }
1491
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001492 bool hasINFILE = false;
1493
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001494 for (; I != E; ++I) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001495 const std::string& cmd = *I;
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001496 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001497 O.indent(IndentLevel);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001498 if (cmd.at(0) == '$') {
1499 if (cmd == "$INFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001500 hasINFILE = true;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001501 if (IsJoin) {
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001502 O << "for (PathVector::const_iterator B = inFiles.begin()"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001503 << ", E = inFiles.end();\n";
1504 O.indent(IndentLevel) << "B != E; ++B)\n";
1505 O.indent(IndentLevel + Indent1) << "vec.push_back(B->str());\n";
1506 }
1507 else {
Chris Lattner74382b72009-08-23 22:45:37 +00001508 O << "vec.push_back(inFile.str());\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001509 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001510 }
1511 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001512 O << "vec.push_back(\"\");\n";
1513 O.indent(IndentLevel) << "out_file_index = vec.size()-1;\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001514 }
1515 else {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001516 O << "vec.push_back(";
1517 I = SubstituteSpecialCommands(I, E, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001518 O << ");\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001519 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001520 }
1521 else {
1522 O << "vec.push_back(\"" << cmd << "\");\n";
1523 }
1524 }
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001525 if (!hasINFILE)
1526 throw "Tool '" + ToolName + "' doesn't take any input!";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001527
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001528 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001529 if (StrVec[0][0] == '$')
1530 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), O);
1531 else
1532 O << '"' << StrVec[0] << '"';
1533 O << ";\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001534}
1535
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001536/// EmitCmdLineVecFillCallback - A function object wrapper around
1537/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1538/// argument to EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001539class EmitCmdLineVecFillCallback {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001540 bool IsJoin;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001541 const std::string& ToolName;
1542 public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001543 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1544 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001545
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001546 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001547 raw_ostream& O) const
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001548 {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001549 EmitCmdLineVecFill(Statement, ToolName, IsJoin, IndentLevel, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001550 }
1551};
1552
1553/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1554/// implement EmitActionHandler. Emits code for
1555/// handling the (forward) and (forward_as) option properties.
1556void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001557 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001558 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001559 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001560 const std::string& Name = NewName.empty()
1561 ? ("-" + D.Name)
1562 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001563 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001564
1565 switch (D.Type) {
1566 case OptionType::Switch:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001567 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001568 break;
1569 case OptionType::Parameter:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001570 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
1571 O.indent(IndentLevel) << "vec.push_back(" << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001572 break;
1573 case OptionType::Prefix:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001574 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\" + "
1575 << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001576 break;
1577 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001578 O.indent(IndentLevel)
1579 << "for (" << D.GenTypeDeclaration()
1580 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1581 O.indent(IndentLevel)
1582 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1583 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\" + " << "*B);\n";
1584 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001585
1586 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001587 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1588 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001589 }
1590
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001591 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001592 break;
1593 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001594 O.indent(IndentLevel)
1595 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1596 << D.GenVariableName() << ".begin(),\n";
1597 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1598 << ".end() ; B != E;) {\n";
1599 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001600
1601 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001602 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1603 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001604 }
1605
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001606 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001607 break;
1608 case OptionType::Alias:
1609 default:
1610 throw std::string("Aliases are not allowed in tool option descriptions!");
1611 }
1612}
1613
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001614/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1615/// EmitPreprocessOptionsCallback.
1616struct ActionHandlingCallbackBase {
1617
1618 void onErrorDag(const DagInit& d,
1619 unsigned IndentLevel, raw_ostream& O) const
1620 {
1621 O.indent(IndentLevel)
1622 << "throw std::runtime_error(\"" <<
1623 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1624 : "Unknown error!")
1625 << "\");\n";
1626 }
1627
1628 void onWarningDag(const DagInit& d,
1629 unsigned IndentLevel, raw_ostream& O) const
1630 {
1631 checkNumberOfArguments(&d, 1);
1632 O.indent(IndentLevel) << "llvm::errs() << \""
1633 << InitPtrToString(d.getArg(0)) << "\";\n";
1634 }
1635
1636};
1637
1638/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1639/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
1640class EmitActionHandlersCallback : ActionHandlingCallbackBase {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001641 const OptionDescriptions& OptDescs;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001642
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001643 void processActionDag(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001644 raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001645 {
1646 const DagInit& Dag = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001647 const std::string& ActionName = GetOperatorName(Dag);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001648
1649 if (ActionName == "append_cmd") {
1650 checkNumberOfArguments(&Dag, 1);
1651 const std::string& Cmd = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001652 StrVector Out;
1653 llvm::SplitString(Cmd, Out);
1654
1655 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1656 B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001657 O.indent(IndentLevel) << "vec.push_back(\"" << *B << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001658 }
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001659 else if (ActionName == "error") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001660 this->onErrorDag(Dag, IndentLevel, O);
1661 }
1662 else if (ActionName == "warning") {
1663 this->onWarningDag(Dag, IndentLevel, O);
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001664 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001665 else if (ActionName == "forward") {
1666 checkNumberOfArguments(&Dag, 1);
1667 const std::string& Name = InitPtrToString(Dag.getArg(0));
1668 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1669 IndentLevel, "", O);
1670 }
1671 else if (ActionName == "forward_as") {
1672 checkNumberOfArguments(&Dag, 2);
1673 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkove89331b2009-05-06 01:41:19 +00001674 const std::string& NewName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001675 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1676 IndentLevel, NewName, O);
1677 }
1678 else if (ActionName == "output_suffix") {
1679 checkNumberOfArguments(&Dag, 1);
1680 const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001681 O.indent(IndentLevel) << "output_suffix = \"" << OutSuf << "\";\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001682 }
1683 else if (ActionName == "stop_compilation") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001684 O.indent(IndentLevel) << "stop_compilation = true;\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001685 }
1686 else if (ActionName == "unpack_values") {
1687 checkNumberOfArguments(&Dag, 1);
1688 const std::string& Name = InitPtrToString(Dag.getArg(0));
1689 const OptionDescription& D = OptDescs.FindOption(Name);
1690
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001691 if (D.isMultiVal())
1692 throw std::string("Can't use unpack_values with multi-valued options!");
1693
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001694 if (D.isList()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001695 O.indent(IndentLevel)
1696 << "for (" << D.GenTypeDeclaration()
1697 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1698 O.indent(IndentLevel)
1699 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n";
1700 O.indent(IndentLevel + Indent1)
1701 << "llvm::SplitString(*B, vec, \",\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001702 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001703 else if (D.isParameter()){
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001704 O.indent(IndentLevel) << "llvm::SplitString("
1705 << D.GenVariableName() << ", vec, \",\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001706 }
1707 else {
1708 throw "Option '" + D.Name +
1709 "': switches can't have the 'unpack_values' property!";
1710 }
1711 }
1712 else {
1713 throw "Unknown action name: " + ActionName + "!";
1714 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001715 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001716 public:
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001717 EmitActionHandlersCallback(const OptionDescriptions& OD)
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001718 : OptDescs(OD) {}
1719
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001720 void operator()(const Init* Statement,
1721 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001722 {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001723 this->processActionDag(Statement, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001724 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001725};
1726
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001727bool IsOutFileIndexCheckRequiredStr (const Init* CmdLine) {
1728 StrVector StrVec;
1729 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1730
1731 for (StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1732 I != E; ++I) {
1733 if (*I == "$OUTFILE")
1734 return false;
1735 }
1736
1737 return true;
1738}
1739
1740class IsOutFileIndexCheckRequiredStrCallback {
1741 bool* ret_;
1742
1743public:
1744 IsOutFileIndexCheckRequiredStrCallback(bool* ret) : ret_(ret)
1745 {}
1746
1747 void operator()(const Init* CmdLine) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001748 // Ignore nested 'case' DAG.
1749 if (typeid(*CmdLine) == typeid(DagInit))
1750 return;
1751
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001752 if (IsOutFileIndexCheckRequiredStr(CmdLine))
1753 *ret_ = true;
1754 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001755
1756 void operator()(const DagInit* Test, unsigned, bool) {
1757 this->operator()(Test);
1758 }
1759 void operator()(const Init* Statement, unsigned) {
1760 this->operator()(Statement);
1761 }
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001762};
1763
1764bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
1765 bool ret = false;
1766 WalkCase(CmdLine, Id(), IsOutFileIndexCheckRequiredStrCallback(&ret));
1767 return ret;
1768}
1769
1770/// IsOutFileIndexCheckRequired - Should we emit an "out_file_index != -1" check
1771/// in EmitGenerateActionMethod() ?
1772bool IsOutFileIndexCheckRequired (Init* CmdLine) {
1773 if (typeid(*CmdLine) == typeid(StringInit))
1774 return IsOutFileIndexCheckRequiredStr(CmdLine);
1775 else
1776 return IsOutFileIndexCheckRequiredCase(CmdLine);
1777}
1778
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001779// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001780// Tool::GenerateAction() method.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001781void EmitGenerateActionMethod (const ToolDescription& D,
1782 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001783 bool IsJoin, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001784 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001785 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001786 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001787 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001788
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001789 O.indent(Indent2) << "bool HasChildren,\n";
1790 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1791 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1792 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1793 O.indent(Indent1) << "{\n";
1794 O.indent(Indent2) << "std::string cmd;\n";
1795 O.indent(Indent2) << "std::vector<std::string> vec;\n";
1796 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
1797 O.indent(Indent2) << "const char* output_suffix = \""
1798 << D.OutputSuffix << "\";\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001799
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001800 if (!D.CmdLine)
1801 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001802
1803 bool IndexCheckRequired = IsOutFileIndexCheckRequired(D.CmdLine);
1804 O.indent(Indent2) << "int out_file_index"
1805 << (IndexCheckRequired ? " = -1" : "")
1806 << ";\n\n";
1807
1808 // Process the cmd_line property.
1809 if (typeid(*D.CmdLine) == typeid(StringInit))
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001810 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
1811 else
1812 EmitCaseConstructHandler(D.CmdLine, Indent2,
1813 EmitCmdLineVecFillCallback(IsJoin, D.Name),
1814 true, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001815
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001816 // For every understood option, emit handling code.
1817 if (D.Actions)
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001818 EmitCaseConstructHandler(D.Actions, Indent2, EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001819 false, OptDescs, O);
1820
1821 O << '\n';
1822 O.indent(Indent2)
1823 << "std::string out_file = OutFilename("
1824 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
1825 O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001826
1827 if (IndexCheckRequired)
1828 O.indent(Indent2) << "if (out_file_index != -1)\n";
1829 O.indent(IndexCheckRequired ? Indent3 : Indent2)
1830 << "vec[out_file_index] = out_file;\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001831
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001832 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001833 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001834 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
1835 O.indent(Indent3) << "vec.insert(vec.end(), "
1836 << SinkOptionName << ".begin(), " << SinkOptionName
1837 << ".end());\n";
1838 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001839 }
1840
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001841 O.indent(Indent2) << "return Action(cmd, vec, stop_compilation, out_file);\n";
1842 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001843}
1844
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001845/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1846/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001847void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
1848 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001849 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001850 if (!ToolDesc.isJoin()) {
1851 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
1852 O.indent(Indent2) << "bool HasChildren,\n";
1853 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1854 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1855 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1856 O.indent(Indent1) << "{\n";
1857 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
1858 << " is not a Join tool!\");\n";
1859 O.indent(Indent1) << "}\n\n";
1860 }
1861 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001862 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001863 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001864
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001865 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001866}
1867
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001868/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1869/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001870void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001871 O.indent(Indent1) << "const char** InputLanguages() const {\n";
1872 O.indent(Indent2) << "return InputLanguages_;\n";
1873 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001874
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001875 if (D.OutLanguage.empty())
1876 throw "Tool " + D.Name + " has no 'out_language' property!";
1877
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001878 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
1879 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
1880 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001881}
1882
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001883/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001884void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001885 O.indent(Indent1) << "const char* Name() const {\n";
1886 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
1887 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001888}
1889
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001890/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1891/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001892void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001893 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001894 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001895 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001896 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001897 O.indent(Indent2) << "return false;\n";
1898 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001899}
1900
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001901/// EmitStaticMemberDefinitions - Emit static member definitions for a
1902/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001903void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001904 if (D.InLanguage.empty())
1905 throw "Tool " + D.Name + " has no 'in_language' property!";
1906
1907 O << "const char* " << D.Name << "::InputLanguages_[] = {";
1908 for (StrVector::const_iterator B = D.InLanguage.begin(),
1909 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001910 O << '\"' << *B << "\", ";
1911 O << "0};\n\n";
1912}
1913
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001914/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001915void EmitToolClassDefinition (const ToolDescription& D,
1916 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001917 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001918 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00001919 return;
1920
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001921 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001922 O << "class " << D.Name << " : public ";
1923 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00001924 O << "JoinTool";
1925 else
1926 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001927
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001928 O << "{\nprivate:\n";
1929 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001930
1931 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001932 EmitNameMethod(D, O);
1933 EmitInOutLanguageMethods(D, O);
1934 EmitIsJoinMethod(D, O);
1935 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001936
1937 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001938 O << "};\n";
1939
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001940 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001941
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001942}
1943
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00001944/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001945/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00001946void EmitOptionDefinitions (const OptionDescriptions& descs,
1947 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001948 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001949{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001950 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001951
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00001952 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001953 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001954 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001955 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001956
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001957 if (val.Type == OptionType::Alias) {
1958 Aliases.push_back(val);
1959 continue;
1960 }
1961
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001962 if (val.isExtern())
1963 O << "extern ";
1964
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001965 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001966 << val.GenVariableName();
1967
1968 if (val.isExtern()) {
1969 O << ";\n";
1970 continue;
1971 }
1972
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00001973 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001974
1975 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
1976 O << ", cl::Prefix";
1977
1978 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001979 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001980 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001981 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001982 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001983 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001984 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001985 O << ", cl::OneOrMore";
1986 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001987 else if (val.isZeroOrOne() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001988 O << ", cl::ZeroOrOne";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001989 }
1990
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001991 if (val.isReallyHidden()) {
1992 O << ", cl::ReallyHidden";
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00001993 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001994 else if (val.isHidden()) {
1995 O << ", cl::Hidden";
1996 }
1997
1998 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00001999 O << ", cl::multi_val(" << val.MultiVal << ')';
2000
2001 if (val.InitVal) {
2002 const std::string& str = val.InitVal->getAsString();
2003 O << ", cl::init(" << str << ')';
2004 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002005
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002006 if (!val.Help.empty())
2007 O << ", cl::desc(\"" << val.Help << "\")";
2008
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002009 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002010 }
2011
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002012 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002013 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002014 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002015 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002016
2017 O << val.GenTypeDeclaration() << ' '
2018 << val.GenVariableName()
2019 << "(\"" << val.Name << '\"';
2020
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002021 const OptionDescription& D = descs.FindOption(val.Help);
2022 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002023
2024 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2025 }
2026
2027 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002028 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002029 O << (HasExterns ? "extern cl" : "cl")
2030 << "::list<std::string> " << SinkOptionName
2031 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002032
2033 O << '\n';
2034}
2035
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002036/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002037/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002038class EmitPreprocessOptionsCallback : ActionHandlingCallbackBase {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002039 const OptionDescriptions& OptDescs_;
2040
2041 void onUnsetOption(Init* i, unsigned IndentLevel, raw_ostream& O) {
2042 const std::string& OptName = InitPtrToString(i);
2043 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2044 const OptionType::OptionType OptType = OptDesc.Type;
2045
2046 if (OptType == OptionType::Switch) {
2047 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2048 }
2049 else if (OptType == OptionType::Parameter
2050 || OptType == OptionType::Prefix) {
2051 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2052 }
2053 else {
2054 throw std::string("'unset_option' can only be applied to "
2055 "switches or parameter/prefix options.");
2056 }
2057 }
2058
2059 void processDag(const Init* I, unsigned IndentLevel, raw_ostream& O)
2060 {
2061 const DagInit& d = InitPtrToDag(I);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002062 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002063
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002064 if (OpName == "warning") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002065 this->onWarningDag(d, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002066 }
2067 else if (OpName == "error") {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002068 this->onWarningDag(d, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002069 }
2070 else if (OpName == "unset_option") {
2071 checkNumberOfArguments(&d, 1);
2072 Init* I = d.getArg(0);
2073 if (typeid(*I) == typeid(ListInit)) {
2074 const ListInit& DagList = *static_cast<const ListInit*>(I);
2075 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
2076 B != E; ++B)
2077 this->onUnsetOption(*B, IndentLevel, O);
2078 }
2079 else {
2080 this->onUnsetOption(I, IndentLevel, O);
2081 }
2082 }
2083 else {
2084 throw "Unknown operator in the option preprocessor: '" + OpName + "'!"
2085 "\nOnly 'warning', 'error' and 'unset_option' are allowed.";
2086 }
2087 }
2088
2089public:
2090
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002091 void operator()(const Init* I, unsigned IndentLevel, raw_ostream& O) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002092 this->processDag(I, IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002093 }
2094
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002095 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002096 : OptDescs_(OptDescs)
2097 {}
2098};
2099
2100/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2101void EmitPreprocessOptions (const RecordKeeper& Records,
2102 const OptionDescriptions& OptDecs, raw_ostream& O)
2103{
2104 O << "void PreprocessOptionsLocal() {\n";
2105
2106 const RecordVector& OptionPreprocessors =
2107 Records.getAllDerivedDefinitions("OptionPreprocessor");
2108
2109 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2110 E = OptionPreprocessors.end(); B!=E; ++B) {
2111 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002112 EmitCaseConstructHandler(Case, Indent1,
2113 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002114 false, OptDecs, O);
2115 }
2116
2117 O << "}\n\n";
2118}
2119
2120/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002121void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002122{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002123 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002124
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002125 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002126 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002127
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002128 // It is allowed for a plugin to have no language map.
2129 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002130
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002131 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2132 if (!LangsToSuffixesList)
2133 throw std::string("Error in the language map definition!");
2134
2135 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002136 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002137
2138 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2139 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2140
2141 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002142 O.indent(Indent1) << "langMap[\""
2143 << InitPtrToString(Suffixes->getElement(i))
2144 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002145 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002146 }
2147
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002148 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002149}
2150
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002151/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2152/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002153void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002154 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002155 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002156 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002157
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002158 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002159 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002160 }
2161 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002162 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002163 }
2164 else if (OpName == "error") {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002165 checkNumberOfArguments(&d, 1);
2166 O.indent(IndentLevel) << "throw std::runtime_error(\""
2167 << InitPtrToString(d.getArg(0))
2168 << "\");\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002169 return;
2170 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002171 else {
2172 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002173 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002174 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002175
2176 if (d.getNumArgs() > 0)
2177 O << InitPtrToInt(d.getArg(0)) << ";\n";
2178 else
2179 O << "2;\n";
2180
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002181}
2182
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002183/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002184void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002185 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002186 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002187
2188 // Class constructor.
2189 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002190 << "public:\n";
2191 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2192 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002193
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002194 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002195 O.indent(Indent1)
2196 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2197 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002198
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002199 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002200 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002201
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002202 O.indent(Indent2) << "return ret;\n";
2203 O.indent(Indent1) << "};\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002204}
2205
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002206/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002207void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002208 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002209 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002210 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002211 for (RecordVector::const_iterator B = EdgeVector.begin(),
2212 E = EdgeVector.end(); B != E; ++B) {
2213 const Record* Edge = *B;
2214 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002215 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002216
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002217 if (!isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002218 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002219 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002220 }
2221}
2222
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002223/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002224/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002225void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002226 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002227 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002228{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002229 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002230
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002231 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2232 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002233 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002234
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002235 O << '\n';
2236
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002237 // Insert edges.
2238
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002239 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002240 for (RecordVector::const_iterator B = EdgeVector.begin(),
2241 E = EdgeVector.end(); B != E; ++B) {
2242 const Record* Edge = *B;
2243 const std::string& NodeA = Edge->getValueAsString("a");
2244 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002245 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002246
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002247 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002248
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002249 if (isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002250 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002251 else
2252 O << "new Edge" << i << "()";
2253
2254 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002255 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002256 }
2257
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002258 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002259}
2260
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002261/// ExtractHookNames - Extract the hook names from all instances of
2262/// $CALL(HookName) in the provided command line string. Helper
2263/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002264class ExtractHookNames {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002265 llvm::StringMap<unsigned>& HookNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002266public:
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002267 ExtractHookNames(llvm::StringMap<unsigned>& HookNames)
2268 : HookNames_(HookNames) {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002269
2270 void operator()(const Init* CmdLine) {
2271 StrVector cmds;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002272
2273 // Ignore nested 'case' DAG.
2274 if (typeid(*CmdLine) == typeid(DagInit))
2275 return;
2276
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002277 TokenizeCmdline(InitPtrToString(CmdLine), cmds);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002278 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2279 B != E; ++B) {
2280 const std::string& cmd = *B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002281
2282 if (cmd == "$CALL") {
2283 unsigned NumArgs = 0;
2284 checkedIncrement(B, E, "Syntax error in $CALL invocation!");
2285 const std::string& HookName = *B;
2286
2287
2288 if (HookName.at(0) == ')')
2289 throw "$CALL invoked with no arguments!";
2290
2291 while (++B != E && B->at(0) != ')') {
2292 ++NumArgs;
2293 }
2294
2295 StringMap<unsigned>::const_iterator H = HookNames_.find(HookName);
2296
2297 if (H != HookNames_.end() && H->second != NumArgs)
2298 throw "Overloading of hooks is not allowed. Overloaded hook: "
2299 + HookName;
2300 else
2301 HookNames_[HookName] = NumArgs;
2302
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002303 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002304 }
2305 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002306
2307 void operator()(const DagInit* Test, unsigned, bool) {
2308 this->operator()(Test);
2309 }
2310 void operator()(const Init* Statement, unsigned) {
2311 this->operator()(Statement);
2312 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002313};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002314
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002315/// FillInHookNames - Actually extract the hook names from all command
2316/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002317void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002318 llvm::StringMap<unsigned>& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002319{
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002320 // For all command lines:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002321 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2322 E = ToolDescs.end(); B != E; ++B) {
2323 const ToolDescription& D = *(*B);
2324 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002325 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002326 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002327 // This is a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002328 ExtractHookNames(HookNames).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002329 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002330 // This is a 'case' construct.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002331 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002332 }
2333}
2334
2335/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2336/// property records and emit hook function declaration for each
2337/// instance of $CALL(HookName).
Daniel Dunbar1a551802009-07-03 00:10:29 +00002338void EmitHookDeclarations(const ToolDescriptions& ToolDescs, raw_ostream& O) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002339 llvm::StringMap<unsigned> HookNames;
2340
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002341 FillInHookNames(ToolDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002342 if (HookNames.empty())
2343 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002344
2345 O << "namespace hooks {\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002346 for (StringMap<unsigned>::const_iterator B = HookNames.begin(),
2347 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002348 O.indent(Indent1) << "std::string " << B->first() << "(";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002349
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002350 for (unsigned i = 0, j = B->second; i < j; ++i) {
2351 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2352 }
2353
2354 O <<");\n";
2355 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002356 O << "}\n\n";
2357}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002358
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002359/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002360void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002361 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2362 O.indent(Indent1) << "int Priority() const { return "
2363 << Priority << "; }\n\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002364 O.indent(Indent1) << "void PreprocessOptions() const\n";
2365 O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002366 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2367 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2368 O.indent(Indent1)
2369 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2370 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2371 << "};\n\n"
2372 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002373}
2374
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002375/// EmitIncludes - Emit necessary #include directives and some
2376/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002377void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002378 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2379 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002380 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002381 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2382 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002383
2384 << "#include \"llvm/ADT/StringExtras.h\"\n"
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002385 << "#include \"llvm/Support/CommandLine.h\"\n"
2386 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002387
2388 << "#include <cstdlib>\n"
2389 << "#include <stdexcept>\n\n"
2390
2391 << "using namespace llvm;\n"
2392 << "using namespace llvmc;\n\n"
2393
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002394 << "extern cl::opt<std::string> OutputFilename;\n\n"
2395
2396 << "inline const char* checkCString(const char* s)\n"
2397 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002398}
2399
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002400
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002401/// PluginData - Holds all information about a plugin.
2402struct PluginData {
2403 OptionDescriptions OptDescs;
2404 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002405 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002406 ToolDescriptions ToolDescs;
2407 RecordVector Edges;
2408 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002409};
2410
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002411/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002412/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002413bool HasSink(const ToolDescriptions& ToolDescs) {
2414 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2415 E = ToolDescs.end(); B != E; ++B)
2416 if ((*B)->isSink())
2417 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002418
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002419 return false;
2420}
2421
2422/// HasExterns - Go through the list of option descriptions and check
2423/// if there are any external options.
2424bool HasExterns(const OptionDescriptions& OptDescs) {
2425 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2426 E = OptDescs.end(); B != E; ++B)
2427 if (B->second.isExtern())
2428 return true;
2429
2430 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002431}
2432
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002433/// CollectPluginData - Collect tool and option properties,
2434/// compilation graph edges and plugin priority from the parse tree.
2435void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2436 // Collect option properties.
2437 const RecordVector& OptionLists =
2438 Records.getAllDerivedDefinitions("OptionList");
2439 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2440 Data.OptDescs);
2441
2442 // Collect tool properties.
2443 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2444 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2445 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002446 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002447
2448 // Collect compilation graph edges.
2449 const RecordVector& CompilationGraphs =
2450 Records.getAllDerivedDefinitions("CompilationGraph");
2451 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2452 Data.Edges);
2453
2454 // Calculate the priority of this plugin.
2455 const RecordVector& Priorities =
2456 Records.getAllDerivedDefinitions("PluginPriority");
2457 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002458}
2459
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002460/// CheckPluginData - Perform some sanity checks on the collected data.
2461void CheckPluginData(PluginData& Data) {
2462 // Filter out all tools not mentioned in the compilation graph.
2463 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002464
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002465 // Typecheck the compilation graph.
2466 TypecheckGraph(Data.Edges, Data.ToolDescs);
2467
2468 // Check that there are no options without side effects (specified
2469 // only in the OptionList).
2470 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2471
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002472}
2473
Daniel Dunbar1a551802009-07-03 00:10:29 +00002474void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002475 // Emit file header.
2476 EmitIncludes(O);
2477
2478 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002479 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002480
2481 // Emit hook declarations.
2482 EmitHookDeclarations(Data.ToolDescs, O);
2483
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002484 O << "namespace {\n\n";
2485
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002486 // Emit PreprocessOptionsLocal() function.
2487 EmitPreprocessOptions(Records, Data.OptDescs, O);
2488
2489 // Emit PopulateLanguageMapLocal() function
2490 // (language map maps from file extensions to language names).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002491 EmitPopulateLanguageMap(Records, O);
2492
2493 // Emit Tool classes.
2494 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2495 E = Data.ToolDescs.end(); B!=E; ++B)
2496 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2497
2498 // Emit Edge# classes.
2499 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2500
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002501 // Emit PopulateCompilationGraphLocal() function.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002502 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2503
2504 // Emit code for plugin registration.
2505 EmitRegisterPlugin(Data.Priority, O);
2506
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002507 O << "} // End anonymous namespace.\n\n";
2508
2509 // Force linkage magic.
2510 O << "namespace llvmc {\n";
2511 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2512 O << "}\n";
2513
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002514 // EOF
2515}
2516
2517
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002518// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00002519}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002520
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002521/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002522void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002523 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002524 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002525
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002526 CollectPluginData(Records, Data);
2527 CheckPluginData(Data);
2528
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +00002529 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002530 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002531
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002532 } catch (std::exception& Error) {
2533 throw Error.what() + std::string(" - usually this means a syntax error.");
2534 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002535}