blob: a8eaa3a16a7b6efb6c2152e174e3625ed6a291bb [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 {
59 template<typename T>
60 void operator()(const T&) const {
61 }
62};
63
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000064int InitPtrToInt(const Init* ptr) {
65 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000066 return val.getValue();
67}
68
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000069const std::string& InitPtrToString(const Init* ptr) {
70 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
71 return val.getValue();
72}
73
74const ListInit& InitPtrToList(const Init* ptr) {
75 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
76 return val;
77}
78
79const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000080 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000081 return val;
82}
83
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000084// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +000085// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov581936a2008-05-06 17:22:03 +000086void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +000087 if (!d || d->getNumArgs() < min_arguments)
Mikhail Glushenkovb7970002009-07-07 16:07:36 +000088 throw d->getOperator()->getAsString() + ": too few arguments!";
Mikhail Glushenkov581936a2008-05-06 17:22:03 +000089}
90
Mikhail Glushenkove5557f42008-05-30 06:08:50 +000091// isDagEmpty - is this DAG marked with an empty marker?
92bool isDagEmpty (const DagInit* d) {
Mikhail Glushenkov2d04ec52009-09-10 16:22:02 +000093 return d->getOperator()->getAsString() == "empty_dag_marker";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +000094}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +000095
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000096// EscapeVariableName - Escape commas and other symbols not allowed
97// in the C++ variable names. Makes it possible to use options named
98// like "Wa," (useful for prefix options).
99std::string EscapeVariableName(const std::string& Var) {
100 std::string ret;
101 for (unsigned i = 0; i != Var.size(); ++i) {
102 char cur_char = Var[i];
103 if (cur_char == ',') {
104 ret += "_comma_";
105 }
106 else if (cur_char == '+') {
107 ret += "_plus_";
108 }
109 else if (cur_char == '-') {
110 ret += "_dash_";
111 }
112 else {
113 ret.push_back(cur_char);
114 }
115 }
116 return ret;
117}
118
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000119/// oneOf - Does the input string contain this character?
120bool oneOf(const char* lst, char c) {
121 while (*lst) {
122 if (*lst++ == c)
123 return true;
124 }
125 return false;
126}
127
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000128template <class I, class S>
129void checkedIncrement(I& P, I E, S ErrorString) {
130 ++P;
131 if (P == E)
132 throw ErrorString;
133}
134
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000135//===----------------------------------------------------------------------===//
136/// Back-end specific code
137
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000138
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000139/// OptionType - One of six different option types. See the
140/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000141namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000142
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000143 enum OptionType { Alias, Switch, Parameter, ParameterList,
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000144 Prefix, PrefixList};
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000145
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000146 bool IsList (OptionType t) {
147 return (t == ParameterList || t == PrefixList);
148 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000149
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000150 bool IsSwitch (OptionType t) {
151 return (t == Switch);
152 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000153
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000154 bool IsParameter (OptionType t) {
155 return (t == Parameter || t == Prefix);
156 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000157
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000158}
159
160OptionType::OptionType stringToOptionType(const std::string& T) {
161 if (T == "alias_option")
162 return OptionType::Alias;
163 else if (T == "switch_option")
164 return OptionType::Switch;
165 else if (T == "parameter_option")
166 return OptionType::Parameter;
167 else if (T == "parameter_list_option")
168 return OptionType::ParameterList;
169 else if (T == "prefix_option")
170 return OptionType::Prefix;
171 else if (T == "prefix_list_option")
172 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000173 else
174 throw "Unknown option type: " + T + '!';
175}
176
177namespace OptionDescriptionFlags {
178 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000179 ReallyHidden = 0x4, Extern = 0x8,
180 OneOrMore = 0x10, ZeroOrOne = 0x20 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000181}
182
183/// OptionDescription - Represents data contained in a single
184/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000185struct OptionDescription {
186 OptionType::OptionType Type;
187 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000188 unsigned Flags;
189 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000190 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000191 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000192
193 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000194 const std::string& n = "",
195 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000196 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000197 {}
198
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000199 /// GenTypeDeclaration - Returns the C++ variable type of this
200 /// option.
201 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000202
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000203 /// GenVariableName - Returns the variable name used in the
204 /// generated C++ code.
205 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000206
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000207 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000208 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000209
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000210 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000211
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000212 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000213
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000214 bool isMultiVal() const;
215
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000216 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000217 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000218
219 bool isRequired() const;
220 void setRequired();
221
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000222 bool isOneOrMore() const;
223 void setOneOrMore();
224
225 bool isZeroOrOne() const;
226 void setZeroOrOne();
227
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000228 bool isHidden() const;
229 void setHidden();
230
231 bool isReallyHidden() const;
232 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000233
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000234 bool isParameter() const
235 { return OptionType::IsParameter(this->Type); }
236
237 bool isSwitch() const
238 { return OptionType::IsSwitch(this->Type); }
239
240 bool isList() const
241 { return OptionType::IsList(this->Type); }
242
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000243};
244
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000245void OptionDescription::Merge (const OptionDescription& other)
246{
247 if (other.Type != Type)
248 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000249
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000250 if (Help == other.Help || Help == DefaultHelpString)
251 Help = other.Help;
252 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000253 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000254 " defined for option " + Name + "\n";
255 }
256
257 Flags |= other.Flags;
258}
259
260bool OptionDescription::isAlias() const {
261 return Type == OptionType::Alias;
262}
263
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000264bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000265 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000266}
267
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000268bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000269 return Flags & OptionDescriptionFlags::Extern;
270}
271void OptionDescription::setExtern() {
272 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000273}
274
275bool OptionDescription::isRequired() const {
276 return Flags & OptionDescriptionFlags::Required;
277}
278void OptionDescription::setRequired() {
279 Flags |= OptionDescriptionFlags::Required;
280}
281
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000282bool OptionDescription::isOneOrMore() const {
283 return Flags & OptionDescriptionFlags::OneOrMore;
284}
285void OptionDescription::setOneOrMore() {
286 Flags |= OptionDescriptionFlags::OneOrMore;
287}
288
289bool OptionDescription::isZeroOrOne() const {
290 return Flags & OptionDescriptionFlags::ZeroOrOne;
291}
292void OptionDescription::setZeroOrOne() {
293 Flags |= OptionDescriptionFlags::ZeroOrOne;
294}
295
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000296bool OptionDescription::isHidden() const {
297 return Flags & OptionDescriptionFlags::Hidden;
298}
299void OptionDescription::setHidden() {
300 Flags |= OptionDescriptionFlags::Hidden;
301}
302
303bool OptionDescription::isReallyHidden() const {
304 return Flags & OptionDescriptionFlags::ReallyHidden;
305}
306void OptionDescription::setReallyHidden() {
307 Flags |= OptionDescriptionFlags::ReallyHidden;
308}
309
310const char* OptionDescription::GenTypeDeclaration() const {
311 switch (Type) {
312 case OptionType::Alias:
313 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000314 case OptionType::PrefixList:
315 case OptionType::ParameterList:
316 return "cl::list<std::string>";
317 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000318 return "cl::opt<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000319 case OptionType::Parameter:
320 case OptionType::Prefix:
321 default:
322 return "cl::opt<std::string>";
323 }
324}
325
326std::string OptionDescription::GenVariableName() const {
327 const std::string& EscapedName = EscapeVariableName(Name);
328 switch (Type) {
329 case OptionType::Alias:
330 return "AutoGeneratedAlias_" + EscapedName;
331 case OptionType::PrefixList:
332 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000333 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000334 case OptionType::Switch:
335 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000336 case OptionType::Prefix:
337 case OptionType::Parameter:
338 default:
339 return "AutoGeneratedParameter_" + EscapedName;
340 }
341}
342
343/// OptionDescriptions - An OptionDescription array plus some helper
344/// functions.
345class OptionDescriptions {
346 typedef StringMap<OptionDescription> container_type;
347
348 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000349 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000350
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000351public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000352 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000353 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000354 /// FindSwitch - wrapper for FindOption that throws in case the option is not
355 /// a switch.
356 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000357
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000358 /// insertDescription - Insert new OptionDescription into
359 /// OptionDescriptions list
360 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000361
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000362 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000363 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000364 const_iterator begin() const { return Descriptions.begin(); }
365 const_iterator end() const { return Descriptions.end(); }
366};
367
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000368const OptionDescription&
369OptionDescriptions::FindOption(const std::string& OptName) const
370{
371 const_iterator I = Descriptions.find(OptName);
372 if (I != Descriptions.end())
373 return I->second;
374 else
375 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000376}
377
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000378const OptionDescription&
379OptionDescriptions::FindSwitch(const std::string& OptName) const
380{
381 const OptionDescription& OptDesc = this->FindOption(OptName);
382 if (!OptDesc.isSwitch())
383 throw OptName + ": incorrect option type - should be a switch!";
384 return OptDesc;
385}
386
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000387void OptionDescriptions::InsertDescription (const OptionDescription& o)
388{
389 container_type::iterator I = Descriptions.find(o.Name);
390 if (I != Descriptions.end()) {
391 OptionDescription& D = I->second;
392 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000393 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000394 else {
395 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000396 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000397}
398
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000399/// HandlerTable - A base class for function objects implemented as
400/// 'tables of handlers'.
401template <class T>
402class HandlerTable {
403protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000404 // Implementation details.
405
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000406 /// Handler -
407 typedef void (T::* Handler) (const DagInit*);
408 /// HandlerMap - A map from property names to property handlers
409 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000410
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000411 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000412 static bool staticMembersInitialized_;
413
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000414 T* childPtr;
415public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000416
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000417 HandlerTable(T* cp) : childPtr(cp)
418 {}
419
420 /// operator() - Just forwards to the corresponding property
421 /// handler.
422 void operator() (Init* i) {
423 const DagInit& property = InitPtrToDag(i);
424 const std::string& property_name = property.getOperator()->getAsString();
425 typename HandlerMap::iterator method = Handlers_.find(property_name);
426
427 if (method != Handlers_.end()) {
428 Handler h = method->second;
429 (childPtr->*h)(&property);
430 }
431 else {
432 throw "No handler found for property " + property_name + "!";
433 }
434 }
435
436 void AddHandler(const char* Property, Handler Handl) {
437 Handlers_[Property] = Handl;
438 }
439};
440
441template <class T> typename HandlerTable<T>::HandlerMap
442HandlerTable<T>::Handlers_;
443template <class T> bool HandlerTable<T>::staticMembersInitialized_ = false;
444
445
446/// CollectOptionProperties - Function object for iterating over an
447/// option property list.
448class CollectOptionProperties : public HandlerTable<CollectOptionProperties> {
449private:
450
451 /// optDescs_ - OptionDescriptions table. This is where the
452 /// information is stored.
453 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000454
455public:
456
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000457 explicit CollectOptionProperties(OptionDescription& OD)
458 : HandlerTable<CollectOptionProperties>(this), optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000459 {
460 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000461 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000462 AddHandler("help", &CollectOptionProperties::onHelp);
463 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000464 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000465 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
466 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000467 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
468 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000469 AddHandler("zero_or_one", &CollectOptionProperties::onZeroOrOne);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000470
471 staticMembersInitialized_ = true;
472 }
473 }
474
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000475private:
476
477 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000478 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000479
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000480 void onExtern (const DagInit* d) {
481 checkNumberOfArguments(d, 0);
482 optDesc_.setExtern();
483 }
484
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000485 void onHelp (const DagInit* d) {
486 checkNumberOfArguments(d, 1);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000487 optDesc_.Help = InitPtrToString(d->getArg(0));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000488 }
489
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000490 void onHidden (const DagInit* d) {
491 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000492 optDesc_.setHidden();
493 }
494
495 void onReallyHidden (const DagInit* d) {
496 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000497 optDesc_.setReallyHidden();
498 }
499
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000500 void onRequired (const DagInit* d) {
501 checkNumberOfArguments(d, 0);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000502 if (optDesc_.isOneOrMore())
503 throw std::string("An option can't have both (required) "
504 "and (one_or_more) properties!");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000505 optDesc_.setRequired();
506 }
507
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000508 void onInit (const DagInit* d) {
509 checkNumberOfArguments(d, 1);
510 Init* i = d->getArg(0);
511 const std::string& str = i->getAsString();
512
513 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
514 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
515
516 if (!correct)
517 throw std::string("Incorrect usage of the 'init' option property!");
518
519 optDesc_.InitVal = i;
520 }
521
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000522 void onOneOrMore (const DagInit* d) {
523 checkNumberOfArguments(d, 0);
524 if (optDesc_.isRequired() || optDesc_.isZeroOrOne())
525 throw std::string("Only one of (required), (zero_or_one) or "
526 "(one_or_more) properties is allowed!");
527 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000528 llvm::errs() << "Warning: specifying the 'one_or_more' property "
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000529 "on a non-list option will have no effect.\n";
530 optDesc_.setOneOrMore();
531 }
532
533 void onZeroOrOne (const DagInit* d) {
534 checkNumberOfArguments(d, 0);
535 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
536 throw std::string("Only one of (required), (zero_or_one) or "
537 "(one_or_more) properties is allowed!");
538 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000539 llvm::errs() << "Warning: specifying the 'zero_or_one' property"
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000540 "on a non-list option will have no effect.\n";
541 optDesc_.setZeroOrOne();
542 }
543
544 void onMultiVal (const DagInit* d) {
545 checkNumberOfArguments(d, 1);
546 int val = InitPtrToInt(d->getArg(0));
547 if (val < 2)
548 throw std::string("Error in the 'multi_val' property: "
549 "the value must be greater than 1!");
550 if (!OptionType::IsList(optDesc_.Type))
551 throw std::string("The multi_val property is valid only "
552 "on list options!");
553 optDesc_.MultiVal = val;
554 }
555
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000556};
557
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000558/// AddOption - A function object that is applied to every option
559/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000560class AddOption {
561private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000562 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000563
564public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000565 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000566 {}
567
568 void operator()(const Init* i) {
569 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000570 checkNumberOfArguments(&d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000571
572 const OptionType::OptionType Type =
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000573 stringToOptionType(d.getOperator()->getAsString());
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000574 const std::string& Name = InitPtrToString(d.getArg(0));
575
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000576 OptionDescription OD(Type, Name);
577
578 if (!OD.isExtern())
579 checkNumberOfArguments(&d, 2);
580
581 if (OD.isAlias()) {
582 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000583 OD.Help = InitPtrToString(d.getArg(1));
584 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000585 else if (!OD.isExtern()) {
586 processOptionProperties(&d, OD);
587 }
588 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000589 }
590
591private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000592 /// processOptionProperties - Go through the list of option
593 /// properties and call a corresponding handler for each.
594 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
595 checkNumberOfArguments(d, 2);
596 DagInit::const_arg_iterator B = d->arg_begin();
597 // Skip the first argument: it's always the option name.
598 ++B;
599 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000600 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000601
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000602};
603
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000604/// CollectOptionDescriptions - Collects option properties from all
605/// OptionLists.
606void CollectOptionDescriptions (RecordVector::const_iterator B,
607 RecordVector::const_iterator E,
608 OptionDescriptions& OptDescs)
609{
610 // For every OptionList:
611 for (; B!=E; ++B) {
612 RecordVector::value_type T = *B;
613 // Throws an exception if the value does not exist.
614 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000615
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000616 // For every option description in this list:
617 // collect the information and
618 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
619 }
620}
621
622// Tool information record
623
624namespace ToolFlags {
625 enum ToolFlags { Join = 0x1, Sink = 0x2 };
626}
627
628struct ToolDescription : public RefCountedBase<ToolDescription> {
629 std::string Name;
630 Init* CmdLine;
631 Init* Actions;
632 StrVector InLanguage;
633 std::string OutLanguage;
634 std::string OutputSuffix;
635 unsigned Flags;
636
637 // Various boolean properties
638 void setSink() { Flags |= ToolFlags::Sink; }
639 bool isSink() const { return Flags & ToolFlags::Sink; }
640 void setJoin() { Flags |= ToolFlags::Join; }
641 bool isJoin() const { return Flags & ToolFlags::Join; }
642
643 // Default ctor here is needed because StringMap can only store
644 // DefaultConstructible objects
645 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
646 ToolDescription (const std::string& n)
647 : Name(n), CmdLine(0), Actions(0), Flags(0)
648 {}
649};
650
651/// ToolDescriptions - A list of Tool information records.
652typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
653
654
655/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000656/// tool property records.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000657class CollectToolProperties : public HandlerTable<CollectToolProperties> {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000658private:
659
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000660 /// toolDesc_ - Properties of the current Tool. This is where the
661 /// information is stored.
662 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000663
664public:
665
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000666 explicit CollectToolProperties (ToolDescription& d)
667 : HandlerTable<CollectToolProperties>(this) , toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000668 {
669 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000670
671 AddHandler("actions", &CollectToolProperties::onActions);
672 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
673 AddHandler("in_language", &CollectToolProperties::onInLanguage);
674 AddHandler("join", &CollectToolProperties::onJoin);
675 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
676 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
677 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000678
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000679 staticMembersInitialized_ = true;
680 }
681 }
682
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000683private:
684
685 /// Property handlers --
686 /// Functions that extract information about tool properties from
687 /// DAG representation.
688
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000689 void onActions (const DagInit* d) {
690 checkNumberOfArguments(d, 1);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000691 Init* Case = d->getArg(0);
692 if (typeid(*Case) != typeid(DagInit) ||
693 static_cast<DagInit*>(Case)->getOperator()->getAsString() != "case")
694 throw
695 std::string("The argument to (actions) should be a 'case' construct!");
696 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000697 }
698
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000699 void onCmdLine (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000700 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000701 toolDesc_.CmdLine = d->getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000702 }
703
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000704 void onInLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000705 checkNumberOfArguments(d, 1);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000706 Init* arg = d->getArg(0);
707
708 // Find out the argument's type.
709 if (typeid(*arg) == typeid(StringInit)) {
710 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000711 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000712 }
713 else {
714 // It's a list.
715 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000716 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000717
718 // Copy strings to the output vector.
719 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
720 B != E; ++B) {
721 out.push_back(InitPtrToString(*B));
722 }
723
724 // Remove duplicates.
725 std::sort(out.begin(), out.end());
726 StrVector::iterator newE = std::unique(out.begin(), out.end());
727 out.erase(newE, out.end());
728 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000729 }
730
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000731 void onJoin (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000732 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000733 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000734 }
735
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000736 void onOutLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000737 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000738 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000739 }
740
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000741 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000742 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000743 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000744 }
745
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000746 void onSink (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000747 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000748 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000749 }
750
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000751};
752
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000753/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000754/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000755/// CollectToolProperties function object).
756void CollectToolDescriptions (RecordVector::const_iterator B,
757 RecordVector::const_iterator E,
758 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000759{
760 // Iterate over a properties list of every Tool definition
761 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000762 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000763 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000764 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000765
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000766 IntrusiveRefCntPtr<ToolDescription>
767 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000768
769 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000770 CollectToolProperties(*ToolDesc));
771 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000772 }
773}
774
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000775/// FillInEdgeVector - Merge all compilation graph definitions into
776/// one single edge list.
777void FillInEdgeVector(RecordVector::const_iterator B,
778 RecordVector::const_iterator E, RecordVector& Out) {
779 for (; B != E; ++B) {
780 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000781
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000782 for (unsigned i = 0; i < edges->size(); ++i)
783 Out.push_back(edges->getElementAsRecord(i));
784 }
785}
786
787/// CalculatePriority - Calculate the priority of this plugin.
788int CalculatePriority(RecordVector::const_iterator B,
789 RecordVector::const_iterator E) {
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000790 int priority = 0;
791
792 if (B != E) {
793 priority = static_cast<int>((*B)->getValueAsInt("priority"));
794
795 if (++B != E)
796 throw std::string("More than one 'PluginPriority' instance found: "
797 "most probably an error!");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000798 }
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000799
800 return priority;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000801}
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000802
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000803/// NotInGraph - Helper function object for FilterNotInGraph.
804struct NotInGraph {
805private:
806 const llvm::StringSet<>& ToolsInGraph_;
807
808public:
809 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
810 : ToolsInGraph_(ToolsInGraph)
811 {}
812
813 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
814 return (ToolsInGraph_.count(x->Name) == 0);
815 }
816};
817
818/// FilterNotInGraph - Filter out from ToolDescs all Tools not
819/// mentioned in the compilation graph definition.
820void FilterNotInGraph (const RecordVector& EdgeVector,
821 ToolDescriptions& ToolDescs) {
822
823 // List all tools mentioned in the graph.
824 llvm::StringSet<> ToolsInGraph;
825
826 for (RecordVector::const_iterator B = EdgeVector.begin(),
827 E = EdgeVector.end(); B != E; ++B) {
828
829 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000830 const std::string& NodeA = Edge->getValueAsString("a");
831 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000832
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000833 if (NodeA != "root")
834 ToolsInGraph.insert(NodeA);
835 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000836 }
837
838 // Filter ToolPropertiesList.
839 ToolDescriptions::iterator new_end =
840 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
841 NotInGraph(ToolsInGraph));
842 ToolDescs.erase(new_end, ToolDescs.end());
843}
844
845/// FillInToolToLang - Fills in two tables that map tool names to
846/// (input, output) languages. Helper function used by TypecheckGraph().
847void FillInToolToLang (const ToolDescriptions& ToolDescs,
848 StringMap<StringSet<> >& ToolToInLang,
849 StringMap<std::string>& ToolToOutLang) {
850 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
851 E = ToolDescs.end(); B != E; ++B) {
852 const ToolDescription& D = *(*B);
853 for (StrVector::const_iterator B = D.InLanguage.begin(),
854 E = D.InLanguage.end(); B != E; ++B)
855 ToolToInLang[D.Name].insert(*B);
856 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000857 }
858}
859
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000860/// TypecheckGraph - Check that names for output and input languages
861/// on all edges do match. This doesn't do much when the information
862/// about the whole graph is not available (i.e. when compiling most
863/// plugins).
864void TypecheckGraph (const RecordVector& EdgeVector,
865 const ToolDescriptions& ToolDescs) {
866 StringMap<StringSet<> > ToolToInLang;
867 StringMap<std::string> ToolToOutLang;
868
869 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
870 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
871 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
872
873 for (RecordVector::const_iterator B = EdgeVector.begin(),
874 E = EdgeVector.end(); B != E; ++B) {
875 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000876 const std::string& NodeA = Edge->getValueAsString("a");
877 const std::string& NodeB = Edge->getValueAsString("b");
878 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
879 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000880
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000881 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000882 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000883 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000884 + ": output->input language mismatch";
885 }
886
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000887 if (NodeB == "root")
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000888 throw std::string("Edges back to the root are not allowed!");
889 }
890}
891
892/// WalkCase - Walks the 'case' expression DAG and invokes
893/// TestCallback on every test, and StatementCallback on every
894/// statement. Handles 'case' nesting, but not the 'and' and 'or'
895/// combinators.
896// TODO: Re-implement EmitCaseConstructHandler on top of this function?
897template <typename F1, typename F2>
898void WalkCase(Init* Case, F1 TestCallback, F2 StatementCallback) {
899 const DagInit& d = InitPtrToDag(Case);
900 bool even = false;
901 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
902 B != E; ++B) {
903 Init* arg = *B;
904 if (even && dynamic_cast<DagInit*>(arg)
905 && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case")
906 WalkCase(arg, TestCallback, StatementCallback);
907 else if (!even)
908 TestCallback(arg);
909 else
910 StatementCallback(arg);
911 even = !even;
912 }
913}
914
915/// ExtractOptionNames - A helper function object used by
916/// CheckForSuperfluousOptions() to walk the 'case' DAG.
917class ExtractOptionNames {
918 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000919
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000920 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000921 const DagInit& Stmt = InitPtrToDag(Statement);
922 const std::string& ActionName = Stmt.getOperator()->getAsString();
923 if (ActionName == "forward" || ActionName == "forward_as" ||
924 ActionName == "unpack_values" || ActionName == "switch_on" ||
925 ActionName == "parameter_equals" || ActionName == "element_in_list" ||
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +0000926 ActionName == "not_empty" || ActionName == "empty") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000927 checkNumberOfArguments(&Stmt, 1);
928 const std::string& Name = InitPtrToString(Stmt.getArg(0));
929 OptionNames_.insert(Name);
930 }
931 else if (ActionName == "and" || ActionName == "or") {
932 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000933 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000934 }
935 }
936 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000937
938public:
939 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
940 {}
941
942 void operator()(const Init* Statement) {
943 if (typeid(*Statement) == typeid(ListInit)) {
944 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
945 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
946 B != E; ++B)
947 this->processDag(*B);
948 }
949 else {
950 this->processDag(Statement);
951 }
952 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000953};
954
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000955/// CheckForSuperfluousOptions - Check that there are no side
956/// effect-free options (specified only in the OptionList). Otherwise,
957/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000958void CheckForSuperfluousOptions (const RecordVector& Edges,
959 const ToolDescriptions& ToolDescs,
960 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000961 llvm::StringSet<> nonSuperfluousOptions;
962
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000963 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000964 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000965 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
966 E = ToolDescs.end(); B != E; ++B) {
967 const ToolDescription& TD = *(*B);
968 ExtractOptionNames Callback(nonSuperfluousOptions);
969 if (TD.Actions)
970 WalkCase(TD.Actions, Callback, Callback);
971 }
972
973 // Add all options mentioned in the 'case' clauses of the
974 // OptionalEdges of the compilation graph to the set of
975 // non-superfluous options.
976 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
977 B != E; ++B) {
978 const Record* Edge = *B;
979 DagInit* Weight = Edge->getValueAsDag("weight");
980
981 if (!isDagEmpty(Weight))
982 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000983 }
984
985 // Check that all options in OptDescs belong to the set of
986 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000987 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000988 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000989 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000990 if (!nonSuperfluousOptions.count(Val.Name)
991 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +0000992 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000993 "Probable cause: this option is specified only in the OptionList.\n";
994 }
995}
996
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +0000997/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
998bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
999 if (TestName == "single_input_file") {
1000 O << "InputFilenames.size() == 1";
1001 return true;
1002 }
1003 else if (TestName == "multiple_input_files") {
1004 O << "InputFilenames.size() > 1";
1005 return true;
1006 }
1007
1008 return false;
1009}
1010
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001011/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1012bool EmitCaseTest1ArgStr(const std::string& TestName,
1013 const DagInit& d,
1014 const OptionDescriptions& OptDescs,
1015 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001016 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001017
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001018 if (TestName == "switch_on") {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001019 const OptionDescription& OptDesc = OptDescs.FindSwitch(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001020 O << OptDesc.GenVariableName();
1021 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001022 }
1023 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001024 O << "InLangs.count(\"" << OptName << "\") != 0";
1025 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001026 }
1027 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001028 // This works only for single-argument Tool::GenerateAction. Join
1029 // tools can process several files in different languages simultaneously.
1030
1031 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001032 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001033 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001034 }
1035 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001036 const char* Test = (TestName == "empty") ? "" : "!";
1037
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001038 if (OptName == "o") {
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001039 O << Test << "OutputFilename.empty()";
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001040 return true;
1041 }
1042 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001043 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001044 if (OptDesc.isSwitch())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001045 throw OptName
1046 + ": incorrect option type - should be a list or parameter!";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001047 O << Test << OptDesc.GenVariableName() << ".empty()";
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001048 return true;
1049 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001050 }
1051
1052 return false;
1053}
1054
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001055/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1056bool EmitCaseTest1ArgList(const std::string& TestName,
1057 const DagInit& d,
1058 const OptionDescriptions& OptDescs,
1059 raw_ostream& O) {
1060 const ListInit& L = *static_cast<ListInit*>(d.getArg(0));
1061
1062 if (TestName == "any_switch_on") {
1063 bool isFirst = true;
1064
1065 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1066 const std::string& OptName = InitPtrToString(*B);
1067 const OptionDescription& OptDesc = OptDescs.FindSwitch(OptName);
1068
1069 if (isFirst)
1070 isFirst = false;
1071 else
1072 O << " || ";
1073 O << OptDesc.GenVariableName();
1074 }
1075
1076 return true;
1077 }
1078
1079 // TODO: implement any_not_empty, any_empty, switch_on [..], empty [..]
1080
1081 return false;
1082}
1083
1084/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1085bool EmitCaseTest1Arg(const std::string& TestName,
1086 const DagInit& d,
1087 const OptionDescriptions& OptDescs,
1088 raw_ostream& O) {
1089 checkNumberOfArguments(&d, 1);
1090 if (typeid(*d.getArg(0)) == typeid(ListInit))
1091 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1092 else
1093 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1094}
1095
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001096/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001097bool EmitCaseTest2Args(const std::string& TestName,
1098 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001099 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001100 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001101 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001102 checkNumberOfArguments(&d, 2);
1103 const std::string& OptName = InitPtrToString(d.getArg(0));
1104 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001105 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001106
1107 if (TestName == "parameter_equals") {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001108 if (!OptDesc.isParameter())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001109 throw OptName + ": incorrect option type - should be a parameter!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001110 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1111 return true;
1112 }
1113 else if (TestName == "element_in_list") {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001114 if (!OptDesc.isList())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001115 throw OptName + ": incorrect option type - should be a list!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001116 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001117 O << "std::find(" << VarName << ".begin(),\n";
1118 O.indent(IndentLevel + Indent1)
1119 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001120 << OptArg << "\") != " << VarName << ".end()";
1121 return true;
1122 }
1123
1124 return false;
1125}
1126
1127// Forward declaration.
1128// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001129void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001130 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001131 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001132
1133/// EmitLogicalOperationTest - Helper function used by
1134/// EmitCaseConstructHandler.
1135void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001136 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001137 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001138 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001139 O << '(';
1140 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001141 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001142 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001143 if (j != NumArgs - 1) {
1144 O << ")\n";
1145 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1146 }
1147 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001148 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001149 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001150 }
1151}
1152
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001153void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001154 const OptionDescriptions& OptDescs, raw_ostream& O)
1155{
1156 checkNumberOfArguments(&d, 1);
1157 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1158 O << "! (";
1159 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1160 O << ")";
1161}
1162
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001163/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001164void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001165 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001166 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001167 const std::string& TestName = d.getOperator()->getAsString();
1168
1169 if (TestName == "and")
1170 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1171 else if (TestName == "or")
1172 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001173 else if (TestName == "not")
1174 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001175 else if (EmitCaseTest0Args(TestName, O))
1176 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001177 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1178 return;
1179 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1180 return;
1181 else
1182 throw TestName + ": unknown edge property!";
1183}
1184
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001185/// EmitCaseConstructHandler - Emit code that handles the 'case'
1186/// construct. Takes a function object that should emit code for every case
1187/// clause.
1188/// Callback's type is void F(Init* Statement, unsigned IndentLevel,
1189/// raw_ostream& O).
1190/// EmitElseIf parameter controls the type of condition that is emitted ('if
1191/// (..) {...} else if (...) {} ... else {...}' vs. 'if (..) {...} if(...)
1192/// {...} ...').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001193template <typename F>
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001194void EmitCaseConstructHandler(const Init* Dag, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001195 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001196 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001197 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001198 const DagInit* d = &InitPtrToDag(Dag);
1199 if (d->getOperator()->getAsString() != "case")
1200 throw std::string("EmitCaseConstructHandler should be invoked"
1201 " only on 'case' expressions!");
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001202
Mikhail Glushenkovcb64f4b2008-05-30 06:15:47 +00001203 unsigned numArgs = d->getNumArgs();
1204 if (d->getNumArgs() < 2)
1205 throw "There should be at least one clause in the 'case' expression:\n"
1206 + d->getAsString();
1207
1208 for (unsigned i = 0; i != numArgs; ++i) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001209 const DagInit& Test = InitPtrToDag(d->getArg(i));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001210
Mikhail Glushenkovcb64f4b2008-05-30 06:15:47 +00001211 // Emit the test.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001212 if (Test.getOperator()->getAsString() == "default") {
1213 if (i+2 != numArgs)
1214 throw std::string("The 'default' clause should be the last in the"
1215 "'case' construct!");
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001216 O.indent(IndentLevel) << "else {\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001217 }
1218 else {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001219 O.indent(IndentLevel) << ((i != 0 && EmitElseIf) ? "else if (" : "if (");
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001220 EmitCaseTest(Test, IndentLevel, OptDescs, O);
1221 O << ") {\n";
1222 }
1223
Mikhail Glushenkovcb64f4b2008-05-30 06:15:47 +00001224 // Emit the corresponding statement.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001225 ++i;
1226 if (i == numArgs)
1227 throw "Case construct handler: no corresponding action "
1228 "found for the test " + Test.getAsString() + '!';
1229
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001230 Init* arg = d->getArg(i);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001231 const DagInit* nd = dynamic_cast<DagInit*>(arg);
1232 if (nd && (nd->getOperator()->getAsString() == "case")) {
1233 // Handle the nested 'case'.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001234 EmitCaseConstructHandler(nd, (IndentLevel + Indent1),
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001235 Callback, EmitElseIf, OptDescs, O);
1236 }
1237 else {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001238 Callback(arg, (IndentLevel + Indent1), O);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001239 }
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001240 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001241 }
1242}
1243
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001244/// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to
1245/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path"] .
1246/// Helper function used by EmitCmdLineVecFill and.
1247void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
1248 const char* Delimiters = " \t\n\v\f\r";
1249 enum TokenizerState
1250 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1251 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001252
1253 if (CmdLine.empty())
1254 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001255 Out.push_back("");
1256
1257 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1258 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001259
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001260 for (; B != E; ++B) {
1261 char cur_ch = CmdLine[B];
1262
1263 switch (cur_st) {
1264 case Normal:
1265 if (cur_ch == '$') {
1266 cur_st = SpecialCommand;
1267 break;
1268 }
1269 if (oneOf(Delimiters, cur_ch)) {
1270 // Skip whitespace
1271 B = CmdLine.find_first_not_of(Delimiters, B);
1272 if (B == std::string::npos) {
1273 B = E-1;
1274 continue;
1275 }
1276 --B;
1277 Out.push_back("");
1278 continue;
1279 }
1280 break;
1281
1282
1283 case SpecialCommand:
1284 if (oneOf(Delimiters, cur_ch)) {
1285 cur_st = Normal;
1286 Out.push_back("");
1287 continue;
1288 }
1289 if (cur_ch == '(') {
1290 Out.push_back("");
1291 cur_st = InsideSpecialCommand;
1292 continue;
1293 }
1294 break;
1295
1296 case InsideSpecialCommand:
1297 if (oneOf(Delimiters, cur_ch)) {
1298 continue;
1299 }
1300 if (cur_ch == '\'') {
1301 cur_st = InsideQuotationMarks;
1302 Out.push_back("");
1303 continue;
1304 }
1305 if (cur_ch == ')') {
1306 cur_st = Normal;
1307 Out.push_back("");
1308 }
1309 if (cur_ch == ',') {
1310 continue;
1311 }
1312
1313 break;
1314
1315 case InsideQuotationMarks:
1316 if (cur_ch == '\'') {
1317 cur_st = InsideSpecialCommand;
1318 continue;
1319 }
1320 break;
1321 }
1322
1323 Out.back().push_back(cur_ch);
1324 }
1325}
1326
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001327/// SubstituteSpecialCommands - Perform string substitution for $CALL
1328/// and $ENV. Helper function used by EmitCmdLineVecFill().
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001329StrVector::const_iterator SubstituteSpecialCommands
Daniel Dunbar1a551802009-07-03 00:10:29 +00001330(StrVector::const_iterator Pos, StrVector::const_iterator End, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001331{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001332
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001333 const std::string& cmd = *Pos;
1334
1335 if (cmd == "$CALL") {
1336 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1337 const std::string& CmdName = *Pos;
1338
1339 if (CmdName == ")")
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001340 throw std::string("$CALL invocation: empty argument list!");
1341
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001342 O << "hooks::";
1343 O << CmdName << "(";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001344
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001345
1346 bool firstIteration = true;
1347 while (true) {
1348 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1349 const std::string& Arg = *Pos;
1350 assert(Arg.size() != 0);
1351
1352 if (Arg[0] == ')')
1353 break;
1354
1355 if (firstIteration)
1356 firstIteration = false;
1357 else
1358 O << ", ";
1359
1360 O << '"' << Arg << '"';
1361 }
1362
1363 O << ')';
1364
1365 }
1366 else if (cmd == "$ENV") {
1367 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
1368 const std::string& EnvName = *Pos;
1369
1370 if (EnvName == ")")
1371 throw "$ENV invocation: empty argument list!";
1372
1373 O << "checkCString(std::getenv(\"";
1374 O << EnvName;
1375 O << "\"))";
1376
1377 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001378 }
1379 else {
1380 throw "Unknown special command: " + cmd;
1381 }
1382
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001383 const std::string& Leftover = *Pos;
1384 assert(Leftover.at(0) == ')');
1385 if (Leftover.size() != 1)
1386 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001387
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001388 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001389}
1390
1391/// EmitCmdLineVecFill - Emit code that fills in the command line
1392/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001393void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001394 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001395 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001396 StrVector StrVec;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001397 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1398
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001399 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001400 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001401
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001402 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1403
1404 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001405 assert(!StrVec[0].empty());
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001406 if (StrVec[0][0] == '$') {
1407 while (I != E && (*I)[0] != ')' )
1408 ++I;
1409
1410 // Skip the ')' symbol.
1411 ++I;
1412 }
1413 else {
1414 ++I;
1415 }
1416
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001417 bool hasINFILE = false;
1418
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001419 for (; I != E; ++I) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001420 const std::string& cmd = *I;
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001421 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001422 O.indent(IndentLevel);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001423 if (cmd.at(0) == '$') {
1424 if (cmd == "$INFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001425 hasINFILE = true;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001426 if (IsJoin) {
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001427 O << "for (PathVector::const_iterator B = inFiles.begin()"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001428 << ", E = inFiles.end();\n";
1429 O.indent(IndentLevel) << "B != E; ++B)\n";
1430 O.indent(IndentLevel + Indent1) << "vec.push_back(B->str());\n";
1431 }
1432 else {
Chris Lattner74382b72009-08-23 22:45:37 +00001433 O << "vec.push_back(inFile.str());\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001434 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001435 }
1436 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001437 O << "vec.push_back(\"\");\n";
1438 O.indent(IndentLevel) << "out_file_index = vec.size()-1;\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001439 }
1440 else {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001441 O << "vec.push_back(";
1442 I = SubstituteSpecialCommands(I, E, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001443 O << ");\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001444 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001445 }
1446 else {
1447 O << "vec.push_back(\"" << cmd << "\");\n";
1448 }
1449 }
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001450 if (!hasINFILE)
1451 throw "Tool '" + ToolName + "' doesn't take any input!";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001452
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001453 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001454 if (StrVec[0][0] == '$')
1455 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), O);
1456 else
1457 O << '"' << StrVec[0] << '"';
1458 O << ";\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001459}
1460
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001461/// EmitCmdLineVecFillCallback - A function object wrapper around
1462/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1463/// argument to EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001464class EmitCmdLineVecFillCallback {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001465 bool IsJoin;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001466 const std::string& ToolName;
1467 public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001468 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1469 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001470
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001471 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001472 raw_ostream& O) const
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001473 {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001474 EmitCmdLineVecFill(Statement, ToolName, IsJoin, IndentLevel, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001475 }
1476};
1477
1478/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1479/// implement EmitActionHandler. Emits code for
1480/// handling the (forward) and (forward_as) option properties.
1481void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001482 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001483 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001484 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001485 const std::string& Name = NewName.empty()
1486 ? ("-" + D.Name)
1487 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001488 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001489
1490 switch (D.Type) {
1491 case OptionType::Switch:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001492 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001493 break;
1494 case OptionType::Parameter:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001495 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
1496 O.indent(IndentLevel) << "vec.push_back(" << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001497 break;
1498 case OptionType::Prefix:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001499 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\" + "
1500 << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001501 break;
1502 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001503 O.indent(IndentLevel)
1504 << "for (" << D.GenTypeDeclaration()
1505 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1506 O.indent(IndentLevel)
1507 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1508 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\" + " << "*B);\n";
1509 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001510
1511 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001512 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1513 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001514 }
1515
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001516 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001517 break;
1518 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001519 O.indent(IndentLevel)
1520 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1521 << D.GenVariableName() << ".begin(),\n";
1522 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1523 << ".end() ; B != E;) {\n";
1524 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001525
1526 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001527 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1528 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001529 }
1530
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001531 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001532 break;
1533 case OptionType::Alias:
1534 default:
1535 throw std::string("Aliases are not allowed in tool option descriptions!");
1536 }
1537}
1538
1539/// EmitActionHandler - Emit code that handles actions. Used by
1540/// EmitGenerateActionMethod() as an argument to
1541/// EmitCaseConstructHandler().
1542class EmitActionHandler {
1543 const OptionDescriptions& OptDescs;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001544
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001545 void processActionDag(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001546 raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001547 {
1548 const DagInit& Dag = InitPtrToDag(Statement);
1549 const std::string& ActionName = Dag.getOperator()->getAsString();
1550
1551 if (ActionName == "append_cmd") {
1552 checkNumberOfArguments(&Dag, 1);
1553 const std::string& Cmd = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001554 StrVector Out;
1555 llvm::SplitString(Cmd, Out);
1556
1557 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1558 B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001559 O.indent(IndentLevel) << "vec.push_back(\"" << *B << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001560 }
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001561 else if (ActionName == "error") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001562 O.indent(IndentLevel) << "throw std::runtime_error(\"" <<
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001563 (Dag.getNumArgs() >= 1 ? InitPtrToString(Dag.getArg(0))
1564 : "Unknown error!")
1565 << "\");\n";
1566 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001567 else if (ActionName == "forward") {
1568 checkNumberOfArguments(&Dag, 1);
1569 const std::string& Name = InitPtrToString(Dag.getArg(0));
1570 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1571 IndentLevel, "", O);
1572 }
1573 else if (ActionName == "forward_as") {
1574 checkNumberOfArguments(&Dag, 2);
1575 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkove89331b2009-05-06 01:41:19 +00001576 const std::string& NewName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001577 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1578 IndentLevel, NewName, O);
1579 }
1580 else if (ActionName == "output_suffix") {
1581 checkNumberOfArguments(&Dag, 1);
1582 const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001583 O.indent(IndentLevel) << "output_suffix = \"" << OutSuf << "\";\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001584 }
1585 else if (ActionName == "stop_compilation") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001586 O.indent(IndentLevel) << "stop_compilation = true;\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001587 }
1588 else if (ActionName == "unpack_values") {
1589 checkNumberOfArguments(&Dag, 1);
1590 const std::string& Name = InitPtrToString(Dag.getArg(0));
1591 const OptionDescription& D = OptDescs.FindOption(Name);
1592
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001593 if (D.isMultiVal())
1594 throw std::string("Can't use unpack_values with multi-valued options!");
1595
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001596 if (D.isList()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001597 O.indent(IndentLevel)
1598 << "for (" << D.GenTypeDeclaration()
1599 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1600 O.indent(IndentLevel)
1601 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n";
1602 O.indent(IndentLevel + Indent1)
1603 << "llvm::SplitString(*B, vec, \",\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001604 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001605 else if (D.isParameter()){
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001606 O.indent(IndentLevel) << "llvm::SplitString("
1607 << D.GenVariableName() << ", vec, \",\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001608 }
1609 else {
1610 throw "Option '" + D.Name +
1611 "': switches can't have the 'unpack_values' property!";
1612 }
1613 }
1614 else {
1615 throw "Unknown action name: " + ActionName + "!";
1616 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001617 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001618 public:
1619 EmitActionHandler(const OptionDescriptions& OD)
1620 : OptDescs(OD) {}
1621
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001622 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001623 raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001624 {
1625 if (typeid(*Statement) == typeid(ListInit)) {
1626 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1627 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1628 B != E; ++B)
1629 this->processActionDag(*B, IndentLevel, O);
1630 }
1631 else {
1632 this->processActionDag(Statement, IndentLevel, O);
1633 }
1634 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001635};
1636
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001637bool IsOutFileIndexCheckRequiredStr (const Init* CmdLine) {
1638 StrVector StrVec;
1639 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1640
1641 for (StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1642 I != E; ++I) {
1643 if (*I == "$OUTFILE")
1644 return false;
1645 }
1646
1647 return true;
1648}
1649
1650class IsOutFileIndexCheckRequiredStrCallback {
1651 bool* ret_;
1652
1653public:
1654 IsOutFileIndexCheckRequiredStrCallback(bool* ret) : ret_(ret)
1655 {}
1656
1657 void operator()(const Init* CmdLine) {
1658 if (IsOutFileIndexCheckRequiredStr(CmdLine))
1659 *ret_ = true;
1660 }
1661};
1662
1663bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
1664 bool ret = false;
1665 WalkCase(CmdLine, Id(), IsOutFileIndexCheckRequiredStrCallback(&ret));
1666 return ret;
1667}
1668
1669/// IsOutFileIndexCheckRequired - Should we emit an "out_file_index != -1" check
1670/// in EmitGenerateActionMethod() ?
1671bool IsOutFileIndexCheckRequired (Init* CmdLine) {
1672 if (typeid(*CmdLine) == typeid(StringInit))
1673 return IsOutFileIndexCheckRequiredStr(CmdLine);
1674 else
1675 return IsOutFileIndexCheckRequiredCase(CmdLine);
1676}
1677
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001678// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001679// Tool::GenerateAction() method.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001680void EmitGenerateActionMethod (const ToolDescription& D,
1681 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001682 bool IsJoin, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001683 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001684 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001685 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001686 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001687
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001688 O.indent(Indent2) << "bool HasChildren,\n";
1689 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1690 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1691 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1692 O.indent(Indent1) << "{\n";
1693 O.indent(Indent2) << "std::string cmd;\n";
1694 O.indent(Indent2) << "std::vector<std::string> vec;\n";
1695 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
1696 O.indent(Indent2) << "const char* output_suffix = \""
1697 << D.OutputSuffix << "\";\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001698
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001699 if (!D.CmdLine)
1700 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001701
1702 bool IndexCheckRequired = IsOutFileIndexCheckRequired(D.CmdLine);
1703 O.indent(Indent2) << "int out_file_index"
1704 << (IndexCheckRequired ? " = -1" : "")
1705 << ";\n\n";
1706
1707 // Process the cmd_line property.
1708 if (typeid(*D.CmdLine) == typeid(StringInit))
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001709 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
1710 else
1711 EmitCaseConstructHandler(D.CmdLine, Indent2,
1712 EmitCmdLineVecFillCallback(IsJoin, D.Name),
1713 true, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001714
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001715 // For every understood option, emit handling code.
1716 if (D.Actions)
1717 EmitCaseConstructHandler(D.Actions, Indent2, EmitActionHandler(OptDescs),
1718 false, OptDescs, O);
1719
1720 O << '\n';
1721 O.indent(Indent2)
1722 << "std::string out_file = OutFilename("
1723 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
1724 O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001725
1726 if (IndexCheckRequired)
1727 O.indent(Indent2) << "if (out_file_index != -1)\n";
1728 O.indent(IndexCheckRequired ? Indent3 : Indent2)
1729 << "vec[out_file_index] = out_file;\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001730
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001731 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001732 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001733 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
1734 O.indent(Indent3) << "vec.insert(vec.end(), "
1735 << SinkOptionName << ".begin(), " << SinkOptionName
1736 << ".end());\n";
1737 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001738 }
1739
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001740 O.indent(Indent2) << "return Action(cmd, vec, stop_compilation, out_file);\n";
1741 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001742}
1743
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001744/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1745/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001746void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
1747 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001748 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001749 if (!ToolDesc.isJoin()) {
1750 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
1751 O.indent(Indent2) << "bool HasChildren,\n";
1752 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1753 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1754 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1755 O.indent(Indent1) << "{\n";
1756 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
1757 << " is not a Join tool!\");\n";
1758 O.indent(Indent1) << "}\n\n";
1759 }
1760 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001761 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001762 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001763
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001764 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001765}
1766
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001767/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1768/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001769void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001770 O.indent(Indent1) << "const char** InputLanguages() const {\n";
1771 O.indent(Indent2) << "return InputLanguages_;\n";
1772 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001773
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001774 if (D.OutLanguage.empty())
1775 throw "Tool " + D.Name + " has no 'out_language' property!";
1776
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001777 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
1778 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
1779 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001780}
1781
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001782/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001783void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001784 O.indent(Indent1) << "const char* Name() const {\n";
1785 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
1786 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001787}
1788
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001789/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1790/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001791void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001792 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001793 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001794 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001795 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001796 O.indent(Indent2) << "return false;\n";
1797 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001798}
1799
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001800/// EmitStaticMemberDefinitions - Emit static member definitions for a
1801/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001802void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001803 if (D.InLanguage.empty())
1804 throw "Tool " + D.Name + " has no 'in_language' property!";
1805
1806 O << "const char* " << D.Name << "::InputLanguages_[] = {";
1807 for (StrVector::const_iterator B = D.InLanguage.begin(),
1808 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001809 O << '\"' << *B << "\", ";
1810 O << "0};\n\n";
1811}
1812
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001813/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001814void EmitToolClassDefinition (const ToolDescription& D,
1815 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001816 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001817 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00001818 return;
1819
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001820 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001821 O << "class " << D.Name << " : public ";
1822 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00001823 O << "JoinTool";
1824 else
1825 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001826
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001827 O << "{\nprivate:\n";
1828 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001829
1830 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001831 EmitNameMethod(D, O);
1832 EmitInOutLanguageMethods(D, O);
1833 EmitIsJoinMethod(D, O);
1834 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001835
1836 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001837 O << "};\n";
1838
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001839 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001840
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001841}
1842
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00001843/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001844/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00001845void EmitOptionDefinitions (const OptionDescriptions& descs,
1846 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001847 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001848{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001849 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001850
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00001851 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001852 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001853 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001854 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001855
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001856 if (val.Type == OptionType::Alias) {
1857 Aliases.push_back(val);
1858 continue;
1859 }
1860
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001861 if (val.isExtern())
1862 O << "extern ";
1863
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001864 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001865 << val.GenVariableName();
1866
1867 if (val.isExtern()) {
1868 O << ";\n";
1869 continue;
1870 }
1871
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00001872 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001873
1874 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
1875 O << ", cl::Prefix";
1876
1877 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001878 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001879 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001880 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001881 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001882 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001883 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001884 O << ", cl::OneOrMore";
1885 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001886 else if (val.isZeroOrOne() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001887 O << ", cl::ZeroOrOne";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001888 }
1889
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001890 if (val.isReallyHidden()) {
1891 O << ", cl::ReallyHidden";
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00001892 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001893 else if (val.isHidden()) {
1894 O << ", cl::Hidden";
1895 }
1896
1897 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00001898 O << ", cl::multi_val(" << val.MultiVal << ')';
1899
1900 if (val.InitVal) {
1901 const std::string& str = val.InitVal->getAsString();
1902 O << ", cl::init(" << str << ')';
1903 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00001904
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001905 if (!val.Help.empty())
1906 O << ", cl::desc(\"" << val.Help << "\")";
1907
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00001908 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001909 }
1910
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001911 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001912 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001913 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001914 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001915
1916 O << val.GenTypeDeclaration() << ' '
1917 << val.GenVariableName()
1918 << "(\"" << val.Name << '\"';
1919
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001920 const OptionDescription& D = descs.FindOption(val.Help);
1921 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001922
1923 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
1924 }
1925
1926 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001927 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00001928 O << (HasExterns ? "extern cl" : "cl")
1929 << "::list<std::string> " << SinkOptionName
1930 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001931
1932 O << '\n';
1933}
1934
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001935/// PreprocessOptionsCallback - Helper function passed to
1936/// EmitCaseConstructHandler() by EmitPreprocessOptions().
1937class PreprocessOptionsCallback {
1938 const OptionDescriptions& OptDescs_;
1939
1940 void onUnsetOption(Init* i, unsigned IndentLevel, raw_ostream& O) {
1941 const std::string& OptName = InitPtrToString(i);
1942 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
1943 const OptionType::OptionType OptType = OptDesc.Type;
1944
1945 if (OptType == OptionType::Switch) {
1946 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
1947 }
1948 else if (OptType == OptionType::Parameter
1949 || OptType == OptionType::Prefix) {
1950 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
1951 }
1952 else {
1953 throw std::string("'unset_option' can only be applied to "
1954 "switches or parameter/prefix options.");
1955 }
1956 }
1957
1958 void processDag(const Init* I, unsigned IndentLevel, raw_ostream& O)
1959 {
1960 const DagInit& d = InitPtrToDag(I);
1961 const std::string& OpName = d.getOperator()->getAsString();
1962
1963 // TOFIX: there is some duplication between this function and
1964 // EmitActionHandler.
1965 if (OpName == "warning") {
1966 checkNumberOfArguments(&d, 1);
1967 O.indent(IndentLevel) << "llvm::errs() << \""
1968 << InitPtrToString(d.getArg(0)) << "\";\n";
1969 }
1970 else if (OpName == "error") {
1971 checkNumberOfArguments(&d, 1);
1972 O.indent(IndentLevel) << "throw std::runtime_error(\""
1973 << InitPtrToString(d.getArg(0))
1974 << "\");\n";
1975 }
1976 else if (OpName == "unset_option") {
1977 checkNumberOfArguments(&d, 1);
1978 Init* I = d.getArg(0);
1979 if (typeid(*I) == typeid(ListInit)) {
1980 const ListInit& DagList = *static_cast<const ListInit*>(I);
1981 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1982 B != E; ++B)
1983 this->onUnsetOption(*B, IndentLevel, O);
1984 }
1985 else {
1986 this->onUnsetOption(I, IndentLevel, O);
1987 }
1988 }
1989 else {
1990 throw "Unknown operator in the option preprocessor: '" + OpName + "'!"
1991 "\nOnly 'warning', 'error' and 'unset_option' are allowed.";
1992 }
1993 }
1994
1995public:
1996
1997 // TODO: Remove duplication.
1998 void operator()(const Init* I, unsigned IndentLevel, raw_ostream& O) {
1999 if (typeid(*I) == typeid(ListInit)) {
2000 const ListInit& DagList = *static_cast<const ListInit*>(I);
2001 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
2002 B != E; ++B)
2003 this->processDag(*B, IndentLevel, O);
2004 }
2005 else {
2006 this->processDag(I, IndentLevel, O);
2007 }
2008 }
2009
2010 PreprocessOptionsCallback(const OptionDescriptions& OptDescs)
2011 : OptDescs_(OptDescs)
2012 {}
2013};
2014
2015/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2016void EmitPreprocessOptions (const RecordKeeper& Records,
2017 const OptionDescriptions& OptDecs, raw_ostream& O)
2018{
2019 O << "void PreprocessOptionsLocal() {\n";
2020
2021 const RecordVector& OptionPreprocessors =
2022 Records.getAllDerivedDefinitions("OptionPreprocessor");
2023
2024 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2025 E = OptionPreprocessors.end(); B!=E; ++B) {
2026 DagInit* Case = (*B)->getValueAsDag("preprocessor");
2027 EmitCaseConstructHandler(Case, Indent1, PreprocessOptionsCallback(OptDecs),
2028 false, OptDecs, O);
2029 }
2030
2031 O << "}\n\n";
2032}
2033
2034/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002035void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002036{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002037 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002038
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002039 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002040 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002041
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002042 // It is allowed for a plugin to have no language map.
2043 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002044
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002045 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2046 if (!LangsToSuffixesList)
2047 throw std::string("Error in the language map definition!");
2048
2049 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002050 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002051
2052 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2053 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2054
2055 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002056 O.indent(Indent1) << "langMap[\""
2057 << InitPtrToString(Suffixes->getElement(i))
2058 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002059 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002060 }
2061
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002062 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002063}
2064
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002065/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2066/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002067void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002068 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002069 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002070 const std::string& OpName = d.getOperator()->getAsString();
2071
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002072 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002073 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002074 }
2075 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002076 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002077 }
2078 else if (OpName == "error") {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002079 checkNumberOfArguments(&d, 1);
2080 O.indent(IndentLevel) << "throw std::runtime_error(\""
2081 << InitPtrToString(d.getArg(0))
2082 << "\");\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002083 return;
2084 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002085 else {
2086 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002087 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002088 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002089
2090 if (d.getNumArgs() > 0)
2091 O << InitPtrToInt(d.getArg(0)) << ";\n";
2092 else
2093 O << "2;\n";
2094
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002095}
2096
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002097/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002098void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002099 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002100 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002101
2102 // Class constructor.
2103 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002104 << "public:\n";
2105 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2106 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002107
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002108 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002109 O.indent(Indent1)
2110 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2111 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002112
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002113 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002114 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002115
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002116 O.indent(Indent2) << "return ret;\n";
2117 O.indent(Indent1) << "};\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002118}
2119
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002120/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002121void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002122 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002123 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002124 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002125 for (RecordVector::const_iterator B = EdgeVector.begin(),
2126 E = EdgeVector.end(); B != E; ++B) {
2127 const Record* Edge = *B;
2128 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002129 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002130
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002131 if (!isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002132 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002133 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002134 }
2135}
2136
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002137/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002138/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002139void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002140 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002141 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002142{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002143 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002144
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002145 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2146 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002147 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002148
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002149 O << '\n';
2150
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002151 // Insert edges.
2152
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002153 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002154 for (RecordVector::const_iterator B = EdgeVector.begin(),
2155 E = EdgeVector.end(); B != E; ++B) {
2156 const Record* Edge = *B;
2157 const std::string& NodeA = Edge->getValueAsString("a");
2158 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002159 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002160
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002161 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002162
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002163 if (isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002164 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002165 else
2166 O << "new Edge" << i << "()";
2167
2168 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002169 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002170 }
2171
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002172 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002173}
2174
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002175/// ExtractHookNames - Extract the hook names from all instances of
2176/// $CALL(HookName) in the provided command line string. Helper
2177/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002178class ExtractHookNames {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002179 llvm::StringMap<unsigned>& HookNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002180public:
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002181 ExtractHookNames(llvm::StringMap<unsigned>& HookNames)
2182 : HookNames_(HookNames) {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002183
2184 void operator()(const Init* CmdLine) {
2185 StrVector cmds;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002186 TokenizeCmdline(InitPtrToString(CmdLine), cmds);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002187 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2188 B != E; ++B) {
2189 const std::string& cmd = *B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002190
2191 if (cmd == "$CALL") {
2192 unsigned NumArgs = 0;
2193 checkedIncrement(B, E, "Syntax error in $CALL invocation!");
2194 const std::string& HookName = *B;
2195
2196
2197 if (HookName.at(0) == ')')
2198 throw "$CALL invoked with no arguments!";
2199
2200 while (++B != E && B->at(0) != ')') {
2201 ++NumArgs;
2202 }
2203
2204 StringMap<unsigned>::const_iterator H = HookNames_.find(HookName);
2205
2206 if (H != HookNames_.end() && H->second != NumArgs)
2207 throw "Overloading of hooks is not allowed. Overloaded hook: "
2208 + HookName;
2209 else
2210 HookNames_[HookName] = NumArgs;
2211
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002212 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002213 }
2214 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002215};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002216
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002217/// FillInHookNames - Actually extract the hook names from all command
2218/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002219void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002220 llvm::StringMap<unsigned>& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002221{
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002222 // For all command lines:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002223 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2224 E = ToolDescs.end(); B != E; ++B) {
2225 const ToolDescription& D = *(*B);
2226 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002227 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002228 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002229 // This is a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002230 ExtractHookNames(HookNames).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002231 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002232 // This is a 'case' construct.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002233 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002234 }
2235}
2236
2237/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2238/// property records and emit hook function declaration for each
2239/// instance of $CALL(HookName).
Daniel Dunbar1a551802009-07-03 00:10:29 +00002240void EmitHookDeclarations(const ToolDescriptions& ToolDescs, raw_ostream& O) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002241 llvm::StringMap<unsigned> HookNames;
2242
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002243 FillInHookNames(ToolDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002244 if (HookNames.empty())
2245 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002246
2247 O << "namespace hooks {\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002248 for (StringMap<unsigned>::const_iterator B = HookNames.begin(),
2249 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002250 O.indent(Indent1) << "std::string " << B->first() << "(";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002251
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002252 for (unsigned i = 0, j = B->second; i < j; ++i) {
2253 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2254 }
2255
2256 O <<");\n";
2257 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002258 O << "}\n\n";
2259}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002260
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002261/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002262void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002263 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2264 O.indent(Indent1) << "int Priority() const { return "
2265 << Priority << "; }\n\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002266 O.indent(Indent1) << "void PreprocessOptions() const\n";
2267 O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002268 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2269 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2270 O.indent(Indent1)
2271 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2272 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2273 << "};\n\n"
2274 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002275}
2276
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002277/// EmitIncludes - Emit necessary #include directives and some
2278/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002279void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002280 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2281 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002282 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002283 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2284 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002285
2286 << "#include \"llvm/ADT/StringExtras.h\"\n"
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002287 << "#include \"llvm/Support/CommandLine.h\"\n"
2288 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002289
2290 << "#include <cstdlib>\n"
2291 << "#include <stdexcept>\n\n"
2292
2293 << "using namespace llvm;\n"
2294 << "using namespace llvmc;\n\n"
2295
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002296 << "extern cl::opt<std::string> OutputFilename;\n\n"
2297
2298 << "inline const char* checkCString(const char* s)\n"
2299 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002300}
2301
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002302
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002303/// PluginData - Holds all information about a plugin.
2304struct PluginData {
2305 OptionDescriptions OptDescs;
2306 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002307 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002308 ToolDescriptions ToolDescs;
2309 RecordVector Edges;
2310 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002311};
2312
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002313/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002314/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002315bool HasSink(const ToolDescriptions& ToolDescs) {
2316 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2317 E = ToolDescs.end(); B != E; ++B)
2318 if ((*B)->isSink())
2319 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002320
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002321 return false;
2322}
2323
2324/// HasExterns - Go through the list of option descriptions and check
2325/// if there are any external options.
2326bool HasExterns(const OptionDescriptions& OptDescs) {
2327 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2328 E = OptDescs.end(); B != E; ++B)
2329 if (B->second.isExtern())
2330 return true;
2331
2332 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002333}
2334
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002335/// CollectPluginData - Collect tool and option properties,
2336/// compilation graph edges and plugin priority from the parse tree.
2337void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2338 // Collect option properties.
2339 const RecordVector& OptionLists =
2340 Records.getAllDerivedDefinitions("OptionList");
2341 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2342 Data.OptDescs);
2343
2344 // Collect tool properties.
2345 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2346 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2347 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002348 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002349
2350 // Collect compilation graph edges.
2351 const RecordVector& CompilationGraphs =
2352 Records.getAllDerivedDefinitions("CompilationGraph");
2353 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2354 Data.Edges);
2355
2356 // Calculate the priority of this plugin.
2357 const RecordVector& Priorities =
2358 Records.getAllDerivedDefinitions("PluginPriority");
2359 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002360}
2361
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002362/// CheckPluginData - Perform some sanity checks on the collected data.
2363void CheckPluginData(PluginData& Data) {
2364 // Filter out all tools not mentioned in the compilation graph.
2365 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002366
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002367 // Typecheck the compilation graph.
2368 TypecheckGraph(Data.Edges, Data.ToolDescs);
2369
2370 // Check that there are no options without side effects (specified
2371 // only in the OptionList).
2372 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2373
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002374}
2375
Daniel Dunbar1a551802009-07-03 00:10:29 +00002376void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002377 // Emit file header.
2378 EmitIncludes(O);
2379
2380 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002381 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002382
2383 // Emit hook declarations.
2384 EmitHookDeclarations(Data.ToolDescs, O);
2385
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002386 O << "namespace {\n\n";
2387
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002388 // Emit PreprocessOptionsLocal() function.
2389 EmitPreprocessOptions(Records, Data.OptDescs, O);
2390
2391 // Emit PopulateLanguageMapLocal() function
2392 // (language map maps from file extensions to language names).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002393 EmitPopulateLanguageMap(Records, O);
2394
2395 // Emit Tool classes.
2396 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2397 E = Data.ToolDescs.end(); B!=E; ++B)
2398 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2399
2400 // Emit Edge# classes.
2401 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2402
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002403 // Emit PopulateCompilationGraphLocal() function.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002404 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2405
2406 // Emit code for plugin registration.
2407 EmitRegisterPlugin(Data.Priority, O);
2408
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002409 O << "} // End anonymous namespace.\n\n";
2410
2411 // Force linkage magic.
2412 O << "namespace llvmc {\n";
2413 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2414 O << "}\n";
2415
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002416 // EOF
2417}
2418
2419
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002420// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00002421}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002422
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002423/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002424void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002425 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002426 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002427
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002428 CollectPluginData(Records, Data);
2429 CheckPluginData(Data);
2430
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +00002431 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002432 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002433
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002434 } catch (std::exception& Error) {
2435 throw Error.what() + std::string(" - usually this means a syntax error.");
2436 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002437}