blob: 133415bf73b57b5bb3bd19a0311e99fff75a7d54 [file] [log] [blame]
Mikhail Glushenkov2d3327f2008-05-30 06:20:54 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
Anton Korobeynikove9ffb5b2008-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 Glushenkov34307a92008-05-06 18:08:59 +000010// This tablegen backend is responsible for emitting LLVMC configuration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkov41405722008-05-06 18:09:29 +000014#include "LLVMCConfigurationEmitter.h"
Anton Korobeynikove9ffb5b2008-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 Glushenkov0e92d2f2008-05-30 06:18:16 +000021#include "llvm/ADT/StringSet.h"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000022#include "llvm/Support/Streams.h"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000023#include <algorithm>
24#include <cassert>
25#include <functional>
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +000026#include <stdexcept>
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000027#include <string>
Chris Lattner52aa6862008-06-04 04:46:14 +000028#include <typeinfo>
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000029using namespace llvm;
30
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +000031namespace {
Anton Korobeynikove9ffb5b2008-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 Glushenkovbe46ae12008-05-07 21:50:19 +000042// Indentation strings.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000043const char * Indent1 = " ";
44const char * Indent2 = " ";
45const char * Indent3 = " ";
46const char * Indent4 = " ";
47
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000048// Default help string.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000049const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
50
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000051// Name for the "sink" option.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000052const char * SinkOptionName = "AutoGeneratedSinkOption";
53
54//===----------------------------------------------------------------------===//
55/// Helper functions
56
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000057int InitPtrToInt(const Init* ptr) {
58 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000059 return val.getValue();
60}
61
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +000062const std::string& InitPtrToString(const Init* ptr) {
63 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
64 return val.getValue();
65}
66
67const ListInit& InitPtrToList(const Init* ptr) {
68 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
69 return val;
70}
71
72const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000073 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000074 return val;
75}
76
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000077// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000078// less than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000079void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
80 if (d->getNumArgs() < min_arguments)
81 throw "Property " + d->getOperator()->getAsString()
82 + " has too few arguments!";
83}
84
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000085// isDagEmpty - is this DAG marked with an empty marker?
86bool isDagEmpty (const DagInit* d) {
87 return d->getOperator()->getAsString() == "empty";
88}
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000089
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000090//===----------------------------------------------------------------------===//
91/// Back-end specific code
92
93// A command-line option can have one of the following types:
94//
Mikhail Glushenkovb623c322008-05-30 06:22:52 +000095// Alias - an alias for another option.
96//
97// Switch - a simple switch without arguments, e.g. -O2
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000098//
99// Parameter - an option that takes one(and only one) argument, e.g. -o file,
100// --output=file
101//
102// ParameterList - same as Parameter, but more than one occurence
103// of the option is allowed, e.g. -lm -lpthread
104//
105// Prefix - argument is everything after the prefix,
106// e.g. -Wa,-foo,-bar, -DNAME=VALUE
107//
108// PrefixList - same as Prefix, but more than one option occurence is
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000109// allowed.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000110
111namespace OptionType {
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000112 enum OptionType { Alias, Switch,
113 Parameter, ParameterList, Prefix, PrefixList};
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000114}
115
116bool IsListOptionType (OptionType::OptionType t) {
117 return (t == OptionType::ParameterList || t == OptionType::PrefixList);
118}
119
120// Code duplication here is necessary because one option can affect
121// several tools and those tools may have different actions associated
122// with this option. GlobalOptionDescriptions are used to generate
123// the option registration code, while ToolOptionDescriptions are used
124// to generate tool-specific code.
125
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000126/// OptionDescription - Base class for option descriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000127struct OptionDescription {
128 OptionType::OptionType Type;
129 std::string Name;
130
131 OptionDescription(OptionType::OptionType t = OptionType::Switch,
132 const std::string& n = "")
133 : Type(t), Name(n)
134 {}
135
136 const char* GenTypeDeclaration() const {
137 switch (Type) {
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000138 case OptionType::Alias:
139 return "cl::alias";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000140 case OptionType::PrefixList:
141 case OptionType::ParameterList:
142 return "cl::list<std::string>";
143 case OptionType::Switch:
144 return "cl::opt<bool>";
145 case OptionType::Parameter:
146 case OptionType::Prefix:
147 default:
148 return "cl::opt<std::string>";
149 }
150 }
151
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000152 // Escape commas and other symbols not allowed in the C++ variable
153 // names. Makes it possible to use options with names like "Wa,"
154 // (useful for prefix options).
155 std::string EscapeVariableName(const std::string& Var) const {
156 std::string ret;
157 for (unsigned i = 0; i != Var.size(); ++i) {
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000158 char cur_char = Var[i];
159 if (cur_char == ',') {
160 ret += "_comma_";
161 }
162 else if (cur_char == '+') {
163 ret += "_plus_";
164 }
165 else if (cur_char == ',') {
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000166 ret += "_comma_";
167 }
168 else {
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000169 ret.push_back(cur_char);
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000170 }
171 }
172 return ret;
173 }
174
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000175 std::string GenVariableName() const {
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000176 const std::string& EscapedName = EscapeVariableName(Name);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000177 switch (Type) {
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000178 case OptionType::Alias:
179 return "AutoGeneratedAlias" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000180 case OptionType::Switch:
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000181 return "AutoGeneratedSwitch" + EscapedName;
182 case OptionType::Prefix:
183 return "AutoGeneratedPrefix" + EscapedName;
184 case OptionType::PrefixList:
185 return "AutoGeneratedPrefixList" + EscapedName;
186 case OptionType::Parameter:
187 return "AutoGeneratedParameter" + EscapedName;
188 case OptionType::ParameterList:
189 default:
190 return "AutoGeneratedParameterList" + EscapedName;
191 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000192 }
193
194};
195
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000196// Global option description.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000197
198namespace GlobalOptionDescriptionFlags {
199 enum GlobalOptionDescriptionFlags { Required = 0x1 };
200}
201
202struct GlobalOptionDescription : public OptionDescription {
203 std::string Help;
204 unsigned Flags;
205
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000206 // We need to provide a default constructor because
207 // StringMap can only store DefaultConstructible objects.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000208 GlobalOptionDescription() : OptionDescription(), Flags(0)
209 {}
210
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000211 GlobalOptionDescription (OptionType::OptionType t, const std::string& n,
212 const std::string& h = DefaultHelpString)
213 : OptionDescription(t, n), Help(h), Flags(0)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000214 {}
215
216 bool isRequired() const {
217 return Flags & GlobalOptionDescriptionFlags::Required;
218 }
219 void setRequired() {
220 Flags |= GlobalOptionDescriptionFlags::Required;
221 }
222
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000223 /// Merge - Merge two option descriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000224 void Merge (const GlobalOptionDescription& other)
225 {
226 if (other.Type != Type)
227 throw "Conflicting definitions for the option " + Name + "!";
228
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000229 if (Help == DefaultHelpString)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000230 Help = other.Help;
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000231 else if (other.Help != DefaultHelpString) {
232 llvm::cerr << "Warning: more than one help string defined for option "
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000233 + Name + "\n";
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000234 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000235
236 Flags |= other.Flags;
237 }
238};
239
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000240/// GlobalOptionDescriptions - A GlobalOptionDescription array
241/// together with some flags affecting generation of option
242/// declarations.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000243struct GlobalOptionDescriptions {
244 typedef StringMap<GlobalOptionDescription> container_type;
245 typedef container_type::const_iterator const_iterator;
246
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000247 /// Descriptions - A list of GlobalOptionDescriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000248 container_type Descriptions;
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000249 /// HasSink - Should the emitter generate a "cl::sink" option?
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000250 bool HasSink;
251
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000252 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000253 const GlobalOptionDescription& FindOption(const std::string& OptName) const {
254 const_iterator I = Descriptions.find(OptName);
255 if (I != Descriptions.end())
256 return I->second;
257 else
258 throw OptName + ": no such option!";
259 }
260
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000261 /// insertDescription - Insert new GlobalOptionDescription into
262 /// GlobalOptionDescriptions list
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000263 void insertDescription (const GlobalOptionDescription& o)
264 {
265 container_type::iterator I = Descriptions.find(o.Name);
266 if (I != Descriptions.end()) {
267 GlobalOptionDescription& D = I->second;
268 D.Merge(o);
269 }
270 else {
271 Descriptions[o.Name] = o;
272 }
273 }
274
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000275 // Support for STL-style iteration
276 const_iterator begin() const { return Descriptions.begin(); }
277 const_iterator end() const { return Descriptions.end(); }
278};
279
280
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000281// Tool-local option description.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000282
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000283// Properties without arguments are implemented as flags.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000284namespace ToolOptionDescriptionFlags {
285 enum ToolOptionDescriptionFlags { StopCompilation = 0x1,
286 Forward = 0x2, UnpackValues = 0x4};
287}
288namespace OptionPropertyType {
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000289 enum OptionPropertyType { AppendCmd, ForwardAs, OutputSuffix };
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000290}
291
292typedef std::pair<OptionPropertyType::OptionPropertyType, std::string>
293OptionProperty;
294typedef SmallVector<OptionProperty, 4> OptionPropertyList;
295
296struct ToolOptionDescription : public OptionDescription {
297 unsigned Flags;
298 OptionPropertyList Props;
299
300 // StringMap can only store DefaultConstructible objects
Mikhail Glushenkov18cbe892008-03-27 09:53:47 +0000301 ToolOptionDescription() : OptionDescription(), Flags(0) {}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000302
303 ToolOptionDescription (OptionType::OptionType t, const std::string& n)
304 : OptionDescription(t, n)
305 {}
306
307 // Various boolean properties
308 bool isStopCompilation() const {
309 return Flags & ToolOptionDescriptionFlags::StopCompilation;
310 }
311 void setStopCompilation() {
312 Flags |= ToolOptionDescriptionFlags::StopCompilation;
313 }
314
315 bool isForward() const {
316 return Flags & ToolOptionDescriptionFlags::Forward;
317 }
318 void setForward() {
319 Flags |= ToolOptionDescriptionFlags::Forward;
320 }
321
322 bool isUnpackValues() const {
323 return Flags & ToolOptionDescriptionFlags::UnpackValues;
324 }
325 void setUnpackValues() {
326 Flags |= ToolOptionDescriptionFlags::UnpackValues;
327 }
328
329 void AddProperty (OptionPropertyType::OptionPropertyType t,
330 const std::string& val)
331 {
332 Props.push_back(std::make_pair(t, val));
333 }
334};
335
336typedef StringMap<ToolOptionDescription> ToolOptionDescriptions;
337
338// Tool information record
339
340namespace ToolFlags {
341 enum ToolFlags { Join = 0x1, Sink = 0x2 };
342}
343
344struct ToolProperties : public RefCountedBase<ToolProperties> {
345 std::string Name;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000346 Init* CmdLine;
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000347 StrVector InLanguage;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000348 std::string OutLanguage;
349 std::string OutputSuffix;
350 unsigned Flags;
351 ToolOptionDescriptions OptDescs;
352
353 // Various boolean properties
354 void setSink() { Flags |= ToolFlags::Sink; }
355 bool isSink() const { return Flags & ToolFlags::Sink; }
356 void setJoin() { Flags |= ToolFlags::Join; }
357 bool isJoin() const { return Flags & ToolFlags::Join; }
358
359 // Default ctor here is needed because StringMap can only store
360 // DefaultConstructible objects
Anton Korobeynikov246fced2008-06-01 16:22:49 +0000361 ToolProperties() : CmdLine(0), Flags(0) {}
362 ToolProperties (const std::string& n) : Name(n), CmdLine(0), Flags(0) {}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000363};
364
365
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000366/// ToolPropertiesList - A list of Tool information records
367/// IntrusiveRefCntPtrs are used here because StringMap has no copy
368/// constructor (and we want to avoid copying ToolProperties anyway).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000369typedef std::vector<IntrusiveRefCntPtr<ToolProperties> > ToolPropertiesList;
370
371
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000372/// CollectOptionProperties - Function object for iterating over a
373/// list (usually, a DAG) of option property records.
374class CollectOptionProperties {
375private:
376 // Implementation details.
377
378 /// OptionPropertyHandler - a function that extracts information
379 /// about a given option property from its DAG representation.
380 typedef void (CollectOptionProperties::* OptionPropertyHandler)
381 (const DagInit*);
382
383 /// OptionPropertyHandlerMap - A map from option property names to
384 /// option property handlers
385 typedef StringMap<OptionPropertyHandler> OptionPropertyHandlerMap;
386
387 static OptionPropertyHandlerMap optionPropertyHandlers_;
388 static bool staticMembersInitialized_;
389
390 /// This is where the information is stored
391
392 /// toolProps_ - Properties of the current Tool.
393 ToolProperties* toolProps_;
394 /// optDescs_ - OptionDescriptions table (used to register options
395 /// globally).
396 GlobalOptionDescription& optDesc_;
397
398public:
399
400 explicit CollectOptionProperties(ToolProperties* TP,
401 GlobalOptionDescription& OD)
402 : toolProps_(TP), optDesc_(OD)
403 {
404 if (!staticMembersInitialized_) {
405 optionPropertyHandlers_["append_cmd"] =
406 &CollectOptionProperties::onAppendCmd;
407 optionPropertyHandlers_["forward"] =
408 &CollectOptionProperties::onForward;
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000409 optionPropertyHandlers_["forward_as"] =
410 &CollectOptionProperties::onForwardAs;
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000411 optionPropertyHandlers_["help"] =
412 &CollectOptionProperties::onHelp;
413 optionPropertyHandlers_["output_suffix"] =
414 &CollectOptionProperties::onOutputSuffix;
415 optionPropertyHandlers_["required"] =
416 &CollectOptionProperties::onRequired;
417 optionPropertyHandlers_["stop_compilation"] =
418 &CollectOptionProperties::onStopCompilation;
419 optionPropertyHandlers_["unpack_values"] =
420 &CollectOptionProperties::onUnpackValues;
421
422 staticMembersInitialized_ = true;
423 }
424 }
425
426 /// operator() - Gets called for every option property; Just forwards
427 /// to the corresponding property handler.
428 void operator() (Init* i) {
429 const DagInit& option_property = InitPtrToDag(i);
430 const std::string& option_property_name
431 = option_property.getOperator()->getAsString();
432 OptionPropertyHandlerMap::iterator method
433 = optionPropertyHandlers_.find(option_property_name);
434
435 if (method != optionPropertyHandlers_.end()) {
436 OptionPropertyHandler h = method->second;
437 (this->*h)(&option_property);
438 }
439 else {
440 throw "Unknown option property: " + option_property_name + "!";
441 }
442 }
443
444private:
445
446 /// Option property handlers --
447 /// Methods that handle properties that are common for all types of
448 /// options (like append_cmd, stop_compilation)
449
450 void onAppendCmd (const DagInit* d) {
451 checkNumberOfArguments(d, 1);
452 checkToolProps(d);
453 const std::string& cmd = InitPtrToString(d->getArg(0));
454
455 toolProps_->OptDescs[optDesc_.Name].
456 AddProperty(OptionPropertyType::AppendCmd, cmd);
457 }
458
459 void onOutputSuffix (const DagInit* d) {
460 checkNumberOfArguments(d, 1);
461 checkToolProps(d);
462 const std::string& suf = InitPtrToString(d->getArg(0));
463
464 if (toolProps_->OptDescs[optDesc_.Name].Type != OptionType::Switch)
465 throw "Option " + optDesc_.Name
466 + " can't have 'output_suffix' property since it isn't a switch!";
467
468 toolProps_->OptDescs[optDesc_.Name].AddProperty
469 (OptionPropertyType::OutputSuffix, suf);
470 }
471
472 void onForward (const DagInit* d) {
473 checkNumberOfArguments(d, 0);
474 checkToolProps(d);
475 toolProps_->OptDescs[optDesc_.Name].setForward();
476 }
477
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000478 void onForwardAs (const DagInit* d) {
479 checkNumberOfArguments(d, 1);
480 checkToolProps(d);
481 const std::string& cmd = InitPtrToString(d->getArg(0));
482
483 toolProps_->OptDescs[optDesc_.Name].
484 AddProperty(OptionPropertyType::ForwardAs, cmd);
485 }
486
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000487 void onHelp (const DagInit* d) {
488 checkNumberOfArguments(d, 1);
489 const std::string& help_message = InitPtrToString(d->getArg(0));
490
491 optDesc_.Help = help_message;
492 }
493
494 void onRequired (const DagInit* d) {
495 checkNumberOfArguments(d, 0);
496 checkToolProps(d);
497 optDesc_.setRequired();
498 }
499
500 void onStopCompilation (const DagInit* d) {
501 checkNumberOfArguments(d, 0);
502 checkToolProps(d);
503 if (optDesc_.Type != OptionType::Switch)
504 throw std::string("Only options of type Switch can stop compilation!");
505 toolProps_->OptDescs[optDesc_.Name].setStopCompilation();
506 }
507
508 void onUnpackValues (const DagInit* d) {
509 checkNumberOfArguments(d, 0);
510 checkToolProps(d);
511 toolProps_->OptDescs[optDesc_.Name].setUnpackValues();
512 }
513
514 // Helper functions
515
516 /// checkToolProps - Throw an error if toolProps_ == 0.
517 void checkToolProps(const DagInit* d) {
518 if (!d)
519 throw "Option property " + d->getOperator()->getAsString()
520 + " can't be used in this context";
521 }
522
523};
524
525CollectOptionProperties::OptionPropertyHandlerMap
526CollectOptionProperties::optionPropertyHandlers_;
527
528bool CollectOptionProperties::staticMembersInitialized_ = false;
529
530
531/// processOptionProperties - Go through the list of option
532/// properties and call a corresponding handler for each.
533void processOptionProperties (const DagInit* d, ToolProperties* t,
534 GlobalOptionDescription& o) {
535 checkNumberOfArguments(d, 2);
536 DagInit::const_arg_iterator B = d->arg_begin();
537 // Skip the first argument: it's always the option name.
538 ++B;
539 std::for_each(B, d->arg_end(), CollectOptionProperties(t, o));
540}
541
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000542/// AddOption - A function object wrapper for
543/// processOptionProperties. Used by CollectProperties and
544/// CollectPropertiesFromOptionList.
545class AddOption {
546private:
547 GlobalOptionDescriptions& OptDescs_;
548 ToolProperties* ToolProps_;
549
550public:
551 explicit AddOption(GlobalOptionDescriptions& OD, ToolProperties* TP = 0)
552 : OptDescs_(OD), ToolProps_(TP)
553 {}
554
555 void operator()(const Init* i) {
556 const DagInit& d = InitPtrToDag(i);
557 checkNumberOfArguments(&d, 2);
558
559 const OptionType::OptionType Type =
560 getOptionType(d.getOperator()->getAsString());
561 const std::string& Name = InitPtrToString(d.getArg(0));
562
563 GlobalOptionDescription OD(Type, Name);
564 if (Type != OptionType::Alias) {
565 processOptionProperties(&d, ToolProps_, OD);
566 if (ToolProps_) {
567 ToolProps_->OptDescs[Name].Type = Type;
568 ToolProps_->OptDescs[Name].Name = Name;
569 }
570 }
571 else {
572 OD.Help = InitPtrToString(d.getArg(1));
573 }
574 OptDescs_.insertDescription(OD);
575 }
576
577private:
578 OptionType::OptionType getOptionType(const std::string& T) const {
579 if (T == "alias_option")
580 return OptionType::Alias;
581 else if (T == "switch_option")
582 return OptionType::Switch;
583 else if (T == "parameter_option")
584 return OptionType::Parameter;
585 else if (T == "parameter_list_option")
586 return OptionType::ParameterList;
587 else if (T == "prefix_option")
588 return OptionType::Prefix;
589 else if (T == "prefix_list_option")
590 return OptionType::PrefixList;
591 else
592 throw "Unknown option type: " + T + '!';
593 }
594};
595
596
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000597/// CollectProperties - Function object for iterating over a list of
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000598/// tool property records.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000599class CollectProperties {
600private:
601
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000602 // Implementation details
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000603
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000604 /// PropertyHandler - a function that extracts information
605 /// about a given tool property from its DAG representation
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000606 typedef void (CollectProperties::*PropertyHandler)(const DagInit*);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000607
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000608 /// PropertyHandlerMap - A map from property names to property
609 /// handlers.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000610 typedef StringMap<PropertyHandler> PropertyHandlerMap;
611
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000612 // Static maps from strings to CollectProperties methods("handlers")
613 static PropertyHandlerMap propertyHandlers_;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000614 static bool staticMembersInitialized_;
615
616
617 /// This is where the information is stored
618
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000619 /// toolProps_ - Properties of the current Tool.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000620 ToolProperties& toolProps_;
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000621 /// optDescs_ - OptionDescriptions table (used to register options
622 /// globally).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000623 GlobalOptionDescriptions& optDescs_;
624
625public:
626
627 explicit CollectProperties (ToolProperties& p, GlobalOptionDescriptions& d)
628 : toolProps_(p), optDescs_(d)
629 {
630 if (!staticMembersInitialized_) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000631 propertyHandlers_["cmd_line"] = &CollectProperties::onCmdLine;
632 propertyHandlers_["in_language"] = &CollectProperties::onInLanguage;
633 propertyHandlers_["join"] = &CollectProperties::onJoin;
634 propertyHandlers_["out_language"] = &CollectProperties::onOutLanguage;
635 propertyHandlers_["output_suffix"] = &CollectProperties::onOutputSuffix;
636 propertyHandlers_["parameter_option"]
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000637 = &CollectProperties::addOption;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000638 propertyHandlers_["parameter_list_option"] =
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000639 &CollectProperties::addOption;
640 propertyHandlers_["prefix_option"] = &CollectProperties::addOption;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000641 propertyHandlers_["prefix_list_option"] =
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000642 &CollectProperties::addOption;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000643 propertyHandlers_["sink"] = &CollectProperties::onSink;
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000644 propertyHandlers_["switch_option"] = &CollectProperties::addOption;
645 propertyHandlers_["alias_option"] = &CollectProperties::addOption;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000646
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000647 staticMembersInitialized_ = true;
648 }
649 }
650
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000651 /// operator() - Gets called for every tool property; Just forwards
652 /// to the corresponding property handler.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000653 void operator() (Init* i) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000654 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000655 const std::string& property_name = d.getOperator()->getAsString();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000656 PropertyHandlerMap::iterator method
657 = propertyHandlers_.find(property_name);
658
659 if (method != propertyHandlers_.end()) {
660 PropertyHandler h = method->second;
661 (this->*h)(&d);
662 }
663 else {
664 throw "Unknown tool property: " + property_name + "!";
665 }
666 }
667
668private:
669
670 /// Property handlers --
671 /// Functions that extract information about tool properties from
672 /// DAG representation.
673
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000674 void onCmdLine (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000675 checkNumberOfArguments(d, 1);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000676 toolProps_.CmdLine = d->getArg(0);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000677 }
678
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000679 void onInLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000680 checkNumberOfArguments(d, 1);
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000681 Init* arg = d->getArg(0);
682
683 // Find out the argument's type.
684 if (typeid(*arg) == typeid(StringInit)) {
685 // It's a string.
686 toolProps_.InLanguage.push_back(InitPtrToString(arg));
687 }
688 else {
689 // It's a list.
690 const ListInit& lst = InitPtrToList(arg);
691 StrVector& out = toolProps_.InLanguage;
692
693 // Copy strings to the output vector.
694 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
695 B != E; ++B) {
696 out.push_back(InitPtrToString(*B));
697 }
698
699 // Remove duplicates.
700 std::sort(out.begin(), out.end());
701 StrVector::iterator newE = std::unique(out.begin(), out.end());
702 out.erase(newE, out.end());
703 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000704 }
705
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000706 void onJoin (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000707 checkNumberOfArguments(d, 0);
708 toolProps_.setJoin();
709 }
710
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000711 void onOutLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000712 checkNumberOfArguments(d, 1);
713 toolProps_.OutLanguage = InitPtrToString(d->getArg(0));
714 }
715
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000716 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000717 checkNumberOfArguments(d, 1);
718 toolProps_.OutputSuffix = InitPtrToString(d->getArg(0));
719 }
720
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000721 void onSink (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000722 checkNumberOfArguments(d, 0);
723 optDescs_.HasSink = true;
724 toolProps_.setSink();
725 }
726
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000727 // Just forwards to the AddOption function object. Somewhat
728 // non-optimal, but avoids code duplication.
729 void addOption (const DagInit* d) {
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000730 checkNumberOfArguments(d, 2);
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000731 AddOption(optDescs_, &toolProps_)(d);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000732 }
733
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000734};
735
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000736// Defintions of static members of CollectProperties.
737CollectProperties::PropertyHandlerMap CollectProperties::propertyHandlers_;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000738bool CollectProperties::staticMembersInitialized_ = false;
739
740
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000741/// CollectToolProperties - Gather information about tool properties
742/// from the parsed TableGen data (basically a wrapper for the
743/// CollectProperties function object).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000744void CollectToolProperties (RecordVector::const_iterator B,
745 RecordVector::const_iterator E,
746 ToolPropertiesList& TPList,
747 GlobalOptionDescriptions& OptDescs)
748{
749 // Iterate over a properties list of every Tool definition
750 for (;B!=E;++B) {
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000751 Record* T = *B;
752 // Throws an exception if the value does not exist.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000753 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000754
755 IntrusiveRefCntPtr<ToolProperties>
756 ToolProps(new ToolProperties(T->getName()));
757
758 std::for_each(PropList->begin(), PropList->end(),
759 CollectProperties(*ToolProps, OptDescs));
760 TPList.push_back(ToolProps);
761 }
762}
763
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000764
765/// CollectPropertiesFromOptionList - Gather information about
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000766/// *global* option properties from the OptionList.
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000767void CollectPropertiesFromOptionList (RecordVector::const_iterator B,
768 RecordVector::const_iterator E,
769 GlobalOptionDescriptions& OptDescs)
770{
771 // Iterate over a properties list of every Tool definition
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000772 for (;B!=E;++B) {
773 RecordVector::value_type T = *B;
774 // Throws an exception if the value does not exist.
775 ListInit* PropList = T->getValueAsListInit("options");
776
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000777 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000778 }
779}
780
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000781/// CheckForSuperfluousOptions - Check that there are no side
782/// effect-free options (specified only in the OptionList). Otherwise,
783/// output a warning.
784void CheckForSuperfluousOptions (const ToolPropertiesList& TPList,
785 const GlobalOptionDescriptions& OptDescs) {
786 llvm::StringSet<> nonSuperfluousOptions;
787
788 // Add all options mentioned in the TPList to the set of
789 // non-superfluous options.
790 for (ToolPropertiesList::const_iterator B = TPList.begin(),
791 E = TPList.end(); B != E; ++B) {
792 const ToolProperties& TP = *(*B);
793 for (ToolOptionDescriptions::const_iterator B = TP.OptDescs.begin(),
794 E = TP.OptDescs.end(); B != E; ++B) {
795 nonSuperfluousOptions.insert(B->first());
796 }
797 }
798
799 // Check that all options in OptDescs belong to the set of
800 // non-superfluous options.
801 for (GlobalOptionDescriptions::const_iterator B = OptDescs.begin(),
802 E = OptDescs.end(); B != E; ++B) {
803 const GlobalOptionDescription& Val = B->second;
804 if (!nonSuperfluousOptions.count(Val.Name)
805 && Val.Type != OptionType::Alias)
806 cerr << "Warning: option '-" << Val.Name << "' has no effect! "
807 "Probable cause: this option is specified only in the OptionList.\n";
808 }
809}
810
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000811/// EmitCaseTest1Arg - Helper function used by
812/// EmitCaseConstructHandler.
813bool EmitCaseTest1Arg(const std::string& TestName,
814 const DagInit& d,
815 const GlobalOptionDescriptions& OptDescs,
816 std::ostream& O) {
817 checkNumberOfArguments(&d, 1);
818 const std::string& OptName = InitPtrToString(d.getArg(0));
819 if (TestName == "switch_on") {
820 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
821 if (OptDesc.Type != OptionType::Switch)
822 throw OptName + ": incorrect option type!";
823 O << OptDesc.GenVariableName();
824 return true;
825 } else if (TestName == "input_languages_contain") {
826 O << "InLangs.count(\"" << OptName << "\") != 0";
827 return true;
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000828 } else if (TestName == "in_language") {
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +0000829 // TODO: remove this restriction
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000830 // Works only for cmd_line!
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +0000831 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000832 return true;
833 } else if (TestName == "not_empty") {
Mikhail Glushenkovb4833872008-05-30 06:24:07 +0000834 if (OptName == "o") {
835 O << "!OutputFilename.empty()";
836 return true;
837 }
838 else {
839 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
840 if (OptDesc.Type == OptionType::Switch)
841 throw OptName + ": incorrect option type!";
842 O << '!' << OptDesc.GenVariableName() << ".empty()";
843 return true;
844 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000845 }
846
847 return false;
848}
849
850/// EmitCaseTest2Args - Helper function used by
851/// EmitCaseConstructHandler.
852bool EmitCaseTest2Args(const std::string& TestName,
853 const DagInit& d,
854 const char* IndentLevel,
855 const GlobalOptionDescriptions& OptDescs,
856 std::ostream& O) {
857 checkNumberOfArguments(&d, 2);
858 const std::string& OptName = InitPtrToString(d.getArg(0));
859 const std::string& OptArg = InitPtrToString(d.getArg(1));
860 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
861
862 if (TestName == "parameter_equals") {
863 if (OptDesc.Type != OptionType::Parameter
864 && OptDesc.Type != OptionType::Prefix)
865 throw OptName + ": incorrect option type!";
866 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
867 return true;
868 }
869 else if (TestName == "element_in_list") {
870 if (OptDesc.Type != OptionType::ParameterList
871 && OptDesc.Type != OptionType::PrefixList)
872 throw OptName + ": incorrect option type!";
873 const std::string& VarName = OptDesc.GenVariableName();
874 O << "std::find(" << VarName << ".begin(),\n"
875 << IndentLevel << Indent1 << VarName << ".end(), \""
876 << OptArg << "\") != " << VarName << ".end()";
877 return true;
878 }
879
880 return false;
881}
882
883// Forward declaration.
884// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
885void EmitCaseTest(const DagInit& d, const char* IndentLevel,
886 const GlobalOptionDescriptions& OptDescs,
887 std::ostream& O);
888
889/// EmitLogicalOperationTest - Helper function used by
890/// EmitCaseConstructHandler.
891void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
892 const char* IndentLevel,
893 const GlobalOptionDescriptions& OptDescs,
894 std::ostream& O) {
895 O << '(';
896 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000897 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000898 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
899 if (j != NumArgs - 1)
900 O << ")\n" << IndentLevel << Indent1 << ' ' << LogicOp << " (";
901 else
902 O << ')';
903 }
904}
905
906/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
907void EmitCaseTest(const DagInit& d, const char* IndentLevel,
908 const GlobalOptionDescriptions& OptDescs,
909 std::ostream& O) {
910 const std::string& TestName = d.getOperator()->getAsString();
911
912 if (TestName == "and")
913 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
914 else if (TestName == "or")
915 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
916 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
917 return;
918 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
919 return;
920 else
921 throw TestName + ": unknown edge property!";
922}
923
924// Emit code that handles the 'case' construct.
925// Takes a function object that should emit code for every case clause.
926// Callback's type is
927// void F(Init* Statement, const char* IndentLevel, std::ostream& O).
928template <typename F>
929void EmitCaseConstructHandler(const DagInit* d, const char* IndentLevel,
Mikhail Glushenkov1d95e9f2008-05-31 13:43:21 +0000930 F Callback, bool EmitElseIf,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000931 const GlobalOptionDescriptions& OptDescs,
932 std::ostream& O) {
933 assert(d->getOperator()->getAsString() == "case");
934
Mikhail Glushenkov31681512008-05-30 06:15:47 +0000935 unsigned numArgs = d->getNumArgs();
936 if (d->getNumArgs() < 2)
937 throw "There should be at least one clause in the 'case' expression:\n"
938 + d->getAsString();
939
940 for (unsigned i = 0; i != numArgs; ++i) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000941 const DagInit& Test = InitPtrToDag(d->getArg(i));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000942
Mikhail Glushenkov31681512008-05-30 06:15:47 +0000943 // Emit the test.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000944 if (Test.getOperator()->getAsString() == "default") {
945 if (i+2 != numArgs)
946 throw std::string("The 'default' clause should be the last in the"
947 "'case' construct!");
948 O << IndentLevel << "else {\n";
949 }
950 else {
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +0000951 O << IndentLevel << ((i != 0 && EmitElseIf) ? "else if (" : "if (");
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000952 EmitCaseTest(Test, IndentLevel, OptDescs, O);
953 O << ") {\n";
954 }
955
Mikhail Glushenkov31681512008-05-30 06:15:47 +0000956 // Emit the corresponding statement.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000957 ++i;
958 if (i == numArgs)
959 throw "Case construct handler: no corresponding action "
960 "found for the test " + Test.getAsString() + '!';
961
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +0000962 Init* arg = d->getArg(i);
963 if (dynamic_cast<DagInit*>(arg)
964 && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case") {
965 EmitCaseConstructHandler(static_cast<DagInit*>(arg),
966 (std::string(IndentLevel) + Indent1).c_str(),
967 Callback, EmitElseIf, OptDescs, O);
968 }
969 else {
970 Callback(arg, IndentLevel, O);
971 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000972 O << IndentLevel << "}\n";
973 }
974}
975
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000976/// EmitForwardOptionPropertyHandlingCode - Helper function used to
977/// implement EmitOptionPropertyHandlingCode(). Emits code for
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000978/// handling the (forward) and (forward_as) option properties.
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000979void EmitForwardOptionPropertyHandlingCode (const ToolOptionDescription& D,
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000980 const std::string& NewName,
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000981 std::ostream& O) {
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000982 const std::string& Name = NewName.empty()
983 ? ("-" + D.Name)
984 : NewName;
985
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000986 switch (D.Type) {
987 case OptionType::Switch:
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000988 O << Indent3 << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000989 break;
990 case OptionType::Parameter:
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000991 O << Indent3 << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000992 O << Indent3 << "vec.push_back(" << D.GenVariableName() << ");\n";
993 break;
994 case OptionType::Prefix:
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000995 O << Indent3 << "vec.push_back(\"" << Name << "\" + "
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000996 << D.GenVariableName() << ");\n";
997 break;
998 case OptionType::PrefixList:
999 O << Indent3 << "for (" << D.GenTypeDeclaration()
1000 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1001 << Indent3 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n"
Mikhail Glushenkov50084e82008-09-22 20:46:19 +00001002 << Indent4 << "vec.push_back(\"" << Name << "\" + "
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001003 << "*B);\n";
1004 break;
1005 case OptionType::ParameterList:
1006 O << Indent3 << "for (" << D.GenTypeDeclaration()
1007 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1008 << Indent3 << "E = " << D.GenVariableName()
1009 << ".end() ; B != E; ++B) {\n"
Mikhail Glushenkov50084e82008-09-22 20:46:19 +00001010 << Indent4 << "vec.push_back(\"" << Name << "\");\n"
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001011 << Indent4 << "vec.push_back(*B);\n"
1012 << Indent3 << "}\n";
1013 break;
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001014 case OptionType::Alias:
1015 default:
1016 throw std::string("Aliases are not allowed in tool option descriptions!");
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001017 }
1018}
1019
Mikhail Glushenkovea6ce492008-05-30 06:15:20 +00001020// ToolOptionHasInterestingProperties - A helper function used by
1021// EmitOptionPropertyHandlingCode() that tells us whether we should
1022// emit any property handling code at all.
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001023bool ToolOptionHasInterestingProperties(const ToolOptionDescription& D) {
Mikhail Glushenkovea6ce492008-05-30 06:15:20 +00001024 bool ret = false;
1025 for (OptionPropertyList::const_iterator B = D.Props.begin(),
1026 E = D.Props.end(); B != E; ++B) {
1027 const OptionProperty& OptProp = *B;
Mikhail Glushenkov50084e82008-09-22 20:46:19 +00001028 if (OptProp.first == OptionPropertyType::AppendCmd
1029 || OptProp.first == OptionPropertyType::ForwardAs)
Mikhail Glushenkovea6ce492008-05-30 06:15:20 +00001030 ret = true;
1031 }
1032 if (D.isForward() || D.isUnpackValues())
1033 ret = true;
1034 return ret;
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001035}
1036
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001037/// EmitOptionPropertyHandlingCode - Helper function used by
1038/// EmitGenerateActionMethod(). Emits code that handles option
1039/// properties.
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001040void EmitOptionPropertyHandlingCode (const ToolOptionDescription& D,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001041 std::ostream& O)
1042{
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001043 if (!ToolOptionHasInterestingProperties(D))
1044 return;
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001045 // Start of the if-clause.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001046 O << Indent2 << "if (";
1047 if (D.Type == OptionType::Switch)
1048 O << D.GenVariableName();
1049 else
1050 O << '!' << D.GenVariableName() << ".empty()";
1051
1052 O <<") {\n";
1053
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001054 // Handle option properties that take an argument.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001055 for (OptionPropertyList::const_iterator B = D.Props.begin(),
1056 E = D.Props.end(); B!=E; ++B) {
1057 const OptionProperty& val = *B;
1058
1059 switch (val.first) {
1060 // (append_cmd cmd) property
1061 case OptionPropertyType::AppendCmd:
1062 O << Indent3 << "vec.push_back(\"" << val.second << "\");\n";
1063 break;
Mikhail Glushenkov50084e82008-09-22 20:46:19 +00001064 // (forward_as) property
1065 case OptionPropertyType::ForwardAs:
1066 EmitForwardOptionPropertyHandlingCode(D, val.second, O);
1067 break;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001068 // Other properties with argument
1069 default:
1070 break;
1071 }
1072 }
1073
1074 // Handle flags
1075
1076 // (forward) property
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001077 if (D.isForward())
Mikhail Glushenkov50084e82008-09-22 20:46:19 +00001078 EmitForwardOptionPropertyHandlingCode(D, "", O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001079
1080 // (unpack_values) property
1081 if (D.isUnpackValues()) {
1082 if (IsListOptionType(D.Type)) {
1083 O << Indent3 << "for (" << D.GenTypeDeclaration()
1084 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1085 << Indent3 << "E = " << D.GenVariableName()
1086 << ".end(); B != E; ++B)\n"
Mikhail Glushenkov028f18e2008-05-06 18:13:45 +00001087 << Indent4 << "llvm::SplitString(*B, vec, \",\");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001088 }
1089 else if (D.Type == OptionType::Prefix || D.Type == OptionType::Parameter){
Mikhail Glushenkov028f18e2008-05-06 18:13:45 +00001090 O << Indent3 << "llvm::SplitString("
1091 << D.GenVariableName() << ", vec, \",\");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001092 }
1093 else {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001094 throw std::string("Switches can't have unpack_values property!");
1095 }
1096 }
1097
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001098 // End of the if-clause.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001099 O << Indent2 << "}\n";
1100}
1101
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001102/// SubstituteSpecialCommands - Perform string substitution for $CALL
1103/// and $ENV. Helper function used by EmitCmdLineVecFill().
1104std::string SubstituteSpecialCommands(const std::string& cmd) {
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001105 size_t cparen = cmd.find(")");
1106 std::string ret;
1107
1108 if (cmd.find("$CALL(") == 0) {
1109 if (cmd.size() == 6)
1110 throw std::string("$CALL invocation: empty argument list!");
1111
1112 ret += "hooks::";
1113 ret += std::string(cmd.begin() + 6, cmd.begin() + cparen);
1114 ret += "()";
1115 }
1116 else if (cmd.find("$ENV(") == 0) {
1117 if (cmd.size() == 5)
1118 throw std::string("$ENV invocation: empty argument list!");
1119
1120 ret += "std::getenv(\"";
1121 ret += std::string(cmd.begin() + 5, cmd.begin() + cparen);
1122 ret += "\")";
1123 }
1124 else {
1125 throw "Unknown special command: " + cmd;
1126 }
1127
1128 if (cmd.begin() + cparen + 1 != cmd.end()) {
1129 ret += " + std::string(\"";
1130 ret += (cmd.c_str() + cparen + 1);
1131 ret += "\")";
1132 }
1133
1134 return ret;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001135}
1136
1137/// EmitCmdLineVecFill - Emit code that fills in the command line
1138/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001139void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
1140 bool Version, const char* IndentLevel,
1141 std::ostream& O) {
1142 StrVector StrVec;
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001143 SplitString(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001144 if (StrVec.empty())
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001145 throw "Tool " + ToolName + " has empty command line!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001146
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001147 StrVector::const_iterator I = StrVec.begin();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001148 ++I;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001149 for (StrVector::const_iterator E = StrVec.end(); I != E; ++I) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001150 const std::string& cmd = *I;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001151 O << IndentLevel;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001152 if (cmd.at(0) == '$') {
1153 if (cmd == "$INFILE") {
1154 if (Version)
1155 O << "for (PathVector::const_iterator B = inFiles.begin()"
1156 << ", E = inFiles.end();\n"
1157 << IndentLevel << "B != E; ++B)\n"
1158 << IndentLevel << Indent1 << "vec.push_back(B->toString());\n";
1159 else
1160 O << "vec.push_back(inFile.toString());\n";
1161 }
1162 else if (cmd == "$OUTFILE") {
1163 O << "vec.push_back(outFile.toString());\n";
1164 }
1165 else {
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001166 O << "vec.push_back(" << SubstituteSpecialCommands(cmd);
1167 O << ");\n";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001168 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001169 }
1170 else {
1171 O << "vec.push_back(\"" << cmd << "\");\n";
1172 }
1173 }
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001174 O << IndentLevel << "cmd = "
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001175 << ((StrVec[0][0] == '$') ? SubstituteSpecialCommands(StrVec[0])
1176 : "\"" + StrVec[0] + "\"")
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001177 << ";\n";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001178}
1179
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001180/// EmitCmdLineVecFillCallback - A function object wrapper around
1181/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1182/// argument to EmitCaseConstructHandler().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001183class EmitCmdLineVecFillCallback {
1184 bool Version;
1185 const std::string& ToolName;
1186 public:
1187 EmitCmdLineVecFillCallback(bool Ver, const std::string& TN)
1188 : Version(Ver), ToolName(TN) {}
1189
1190 void operator()(const Init* Statement, const char* IndentLevel,
1191 std::ostream& O) const
1192 {
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001193 EmitCmdLineVecFill(Statement, ToolName, Version,
1194 (std::string(IndentLevel) + Indent1).c_str(), O);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001195 }
1196};
1197
1198// EmitGenerateActionMethod - Emit one of two versions of the
1199// Tool::GenerateAction() method.
1200void EmitGenerateActionMethod (const ToolProperties& P,
1201 const GlobalOptionDescriptions& OptDescs,
1202 bool Version, std::ostream& O) {
1203 if (Version)
1204 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n";
1205 else
1206 O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n";
1207
1208 O << Indent2 << "const sys::Path& outFile,\n"
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001209 << Indent2 << "const InputLanguagesSet& InLangs,\n"
1210 << Indent2 << "const LanguageMap& LangMap) const\n"
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001211 << Indent1 << "{\n"
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001212 << Indent2 << "const char* cmd;\n"
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001213 << Indent2 << "std::vector<std::string> vec;\n";
1214
1215 // cmd_line is either a string or a 'case' construct.
1216 if (typeid(*P.CmdLine) == typeid(StringInit))
1217 EmitCmdLineVecFill(P.CmdLine, P.Name, Version, Indent2, O);
1218 else
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001219 EmitCaseConstructHandler(&InitPtrToDag(P.CmdLine), Indent2,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001220 EmitCmdLineVecFillCallback(Version, P.Name),
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001221 true, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001222
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001223 // For every understood option, emit handling code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001224 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
1225 E = P.OptDescs.end(); B != E; ++B) {
1226 const ToolOptionDescription& val = B->second;
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001227 EmitOptionPropertyHandlingCode(val, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001228 }
1229
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001230 // Handle the Sink property.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001231 if (P.isSink()) {
1232 O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n"
1233 << Indent3 << "vec.insert(vec.end(), "
1234 << SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n"
1235 << Indent2 << "}\n";
1236 }
1237
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001238 O << Indent2 << "return Action(cmd, vec);\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001239 << Indent1 << "}\n\n";
1240}
1241
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001242/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1243/// a given Tool class.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001244void EmitGenerateActionMethods (const ToolProperties& P,
1245 const GlobalOptionDescriptions& OptDescs,
1246 std::ostream& O) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001247 if (!P.isJoin())
1248 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001249 << Indent2 << "const llvm::sys::Path& outFile,\n"
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001250 << Indent2 << "const InputLanguagesSet& InLangs,\n"
1251 << Indent2 << "const LanguageMap& LangMap) const\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001252 << Indent1 << "{\n"
1253 << Indent2 << "throw std::runtime_error(\"" << P.Name
1254 << " is not a Join tool!\");\n"
1255 << Indent1 << "}\n\n";
1256 else
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001257 EmitGenerateActionMethod(P, OptDescs, true, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001258
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001259 EmitGenerateActionMethod(P, OptDescs, false, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001260}
1261
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001262/// EmitIsLastMethod - Emit the IsLast() method for a given Tool
1263/// class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001264void EmitIsLastMethod (const ToolProperties& P, std::ostream& O) {
1265 O << Indent1 << "bool IsLast() const {\n"
1266 << Indent2 << "bool last = false;\n";
1267
1268 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
1269 E = P.OptDescs.end(); B != E; ++B) {
1270 const ToolOptionDescription& val = B->second;
1271
1272 if (val.isStopCompilation())
1273 O << Indent2
1274 << "if (" << val.GenVariableName()
1275 << ")\n" << Indent3 << "last = true;\n";
1276 }
1277
1278 O << Indent2 << "return last;\n"
1279 << Indent1 << "}\n\n";
1280}
1281
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001282/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1283/// methods for a given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001284void EmitInOutLanguageMethods (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001285 O << Indent1 << "const char** InputLanguages() const {\n"
1286 << Indent2 << "return InputLanguages_;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001287 << Indent1 << "}\n\n";
1288
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001289 O << Indent1 << "const char* OutputLanguage() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001290 << Indent2 << "return \"" << P.OutLanguage << "\";\n"
1291 << Indent1 << "}\n\n";
1292}
1293
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001294/// EmitOutputSuffixMethod - Emit the OutputSuffix() method for a
1295/// given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001296void EmitOutputSuffixMethod (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001297 O << Indent1 << "const char* OutputSuffix() const {\n"
Mikhail Glushenkovabab33b2008-05-30 06:13:02 +00001298 << Indent2 << "const char* ret = \"" << P.OutputSuffix << "\";\n";
1299
1300 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
1301 E = P.OptDescs.end(); B != E; ++B) {
1302 const ToolOptionDescription& OptDesc = B->second;
1303 for (OptionPropertyList::const_iterator B = OptDesc.Props.begin(),
1304 E = OptDesc.Props.end(); B != E; ++B) {
1305 const OptionProperty& OptProp = *B;
1306 if (OptProp.first == OptionPropertyType::OutputSuffix) {
1307 O << Indent2 << "if (" << OptDesc.GenVariableName() << ")\n"
1308 << Indent3 << "ret = \"" << OptProp.second << "\";\n";
1309 }
1310 }
1311 }
1312
1313 O << Indent2 << "return ret;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001314 << Indent1 << "}\n\n";
1315}
1316
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001317/// EmitNameMethod - Emit the Name() method for a given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001318void EmitNameMethod (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001319 O << Indent1 << "const char* Name() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001320 << Indent2 << "return \"" << P.Name << "\";\n"
1321 << Indent1 << "}\n\n";
1322}
1323
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001324/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1325/// class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001326void EmitIsJoinMethod (const ToolProperties& P, std::ostream& O) {
1327 O << Indent1 << "bool IsJoin() const {\n";
1328 if (P.isJoin())
1329 O << Indent2 << "return true;\n";
1330 else
1331 O << Indent2 << "return false;\n";
1332 O << Indent1 << "}\n\n";
1333}
1334
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001335/// EmitStaticMemberDefinitions - Emit static member definitions for a
1336/// given Tool class.
1337void EmitStaticMemberDefinitions(const ToolProperties& P, std::ostream& O) {
1338 O << "const char* " << P.Name << "::InputLanguages_[] = {";
1339 for (StrVector::const_iterator B = P.InLanguage.begin(),
1340 E = P.InLanguage.end(); B != E; ++B)
1341 O << '\"' << *B << "\", ";
1342 O << "0};\n\n";
1343}
1344
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001345/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001346void EmitToolClassDefinition (const ToolProperties& P,
1347 const GlobalOptionDescriptions& OptDescs,
1348 std::ostream& O) {
1349 if (P.Name == "root")
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001350 return;
1351
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001352 // Header
Mikhail Glushenkov121889c2008-05-06 17:26:53 +00001353 O << "class " << P.Name << " : public ";
1354 if (P.isJoin())
1355 O << "JoinTool";
1356 else
1357 O << "Tool";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001358
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001359 O << "{\nprivate:\n"
1360 << Indent1 << "static const char* InputLanguages_[];\n\n";
1361
1362 O << "public:\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001363 EmitNameMethod(P, O);
1364 EmitInOutLanguageMethods(P, O);
1365 EmitOutputSuffixMethod(P, O);
1366 EmitIsJoinMethod(P, O);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001367 EmitGenerateActionMethods(P, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001368 EmitIsLastMethod(P, O);
1369
1370 // Close class definition
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001371 O << "};\n";
1372
1373 EmitStaticMemberDefinitions(P, O);
1374
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001375}
1376
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001377/// EmitOptionDescriptions - Iterate over a list of option
1378/// descriptions and emit registration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001379void EmitOptionDescriptions (const GlobalOptionDescriptions& descs,
1380 std::ostream& O)
1381{
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001382 std::vector<GlobalOptionDescription> Aliases;
1383
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001384 // Emit static cl::Option variables.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001385 for (GlobalOptionDescriptions::const_iterator B = descs.begin(),
1386 E = descs.end(); B!=E; ++B) {
1387 const GlobalOptionDescription& val = B->second;
1388
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001389 if (val.Type == OptionType::Alias) {
1390 Aliases.push_back(val);
1391 continue;
1392 }
1393
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001394 O << val.GenTypeDeclaration() << ' '
1395 << val.GenVariableName()
1396 << "(\"" << val.Name << '\"';
1397
1398 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
1399 O << ", cl::Prefix";
1400
1401 if (val.isRequired()) {
1402 switch (val.Type) {
1403 case OptionType::PrefixList:
1404 case OptionType::ParameterList:
1405 O << ", cl::OneOrMore";
1406 break;
1407 default:
1408 O << ", cl::Required";
1409 }
1410 }
1411
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001412 if (!val.Help.empty())
1413 O << ", cl::desc(\"" << val.Help << "\")";
1414
1415 O << ");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001416 }
1417
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001418 // Emit the aliases (they should go after all the 'proper' options).
1419 for (std::vector<GlobalOptionDescription>::const_iterator
1420 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
1421 const GlobalOptionDescription& val = *B;
1422
1423 O << val.GenTypeDeclaration() << ' '
1424 << val.GenVariableName()
1425 << "(\"" << val.Name << '\"';
1426
1427 GlobalOptionDescriptions::container_type
1428 ::const_iterator F = descs.Descriptions.find(val.Help);
1429 if (F != descs.Descriptions.end())
1430 O << ", cl::aliasopt(" << F->second.GenVariableName() << ")";
1431 else
1432 throw val.Name + ": alias to an unknown option!";
1433
1434 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
1435 }
1436
1437 // Emit the sink option.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001438 if (descs.HasSink)
1439 O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
1440
1441 O << '\n';
1442}
1443
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001444/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001445void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O)
1446{
1447 // Get the relevant field out of RecordKeeper
1448 Record* LangMapRecord = Records.getDef("LanguageMap");
1449 if (!LangMapRecord)
1450 throw std::string("Language map definition not found!");
1451
1452 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
1453 if (!LangsToSuffixesList)
1454 throw std::string("Error in the language map definition!");
1455
1456 // Generate code
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001457 O << "void llvmc::PopulateLanguageMap(LanguageMap& langMap) {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001458
1459 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
1460 Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
1461
1462 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
1463 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
1464
1465 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001466 O << Indent1 << "langMap[\""
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001467 << InitPtrToString(Suffixes->getElement(i))
1468 << "\"] = \"" << Lang << "\";\n";
1469 }
1470
1471 O << "}\n\n";
1472}
1473
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001474/// FillInToolToLang - Fills in two tables that map tool names to
1475/// (input, output) languages. Used by the typechecker.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001476void FillInToolToLang (const ToolPropertiesList& TPList,
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001477 StringMap<StringSet<> >& ToolToInLang,
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001478 StringMap<std::string>& ToolToOutLang) {
1479 for (ToolPropertiesList::const_iterator B = TPList.begin(), E = TPList.end();
1480 B != E; ++B) {
1481 const ToolProperties& P = *(*B);
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001482 for (StrVector::const_iterator B = P.InLanguage.begin(),
1483 E = P.InLanguage.end(); B != E; ++B)
1484 ToolToInLang[P.Name].insert(*B);
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001485 ToolToOutLang[P.Name] = P.OutLanguage;
1486 }
1487}
1488
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001489/// TypecheckGraph - Check that names for output and input languages
1490/// on all edges do match.
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001491// TOFIX: It would be nice if this function also checked for cycles
1492// and multiple default edges in the graph (better error
1493// reporting). Unfortunately, it is awkward to do right now because
1494// our intermediate representation is not sufficiently
Mikhail Glushenkovc178ead2008-09-22 20:45:17 +00001495// sophisticated. Algorithms like these require a real graph instead of
1496// an AST.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001497void TypecheckGraph (Record* CompilationGraph,
1498 const ToolPropertiesList& TPList) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001499 StringMap<StringSet<> > ToolToInLang;
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001500 StringMap<std::string> ToolToOutLang;
1501
1502 FillInToolToLang(TPList, ToolToInLang, ToolToOutLang);
1503 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001504 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
1505 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001506
1507 for (unsigned i = 0; i < edges->size(); ++i) {
1508 Record* Edge = edges->getElementAsRecord(i);
1509 Record* A = Edge->getValueAsDef("a");
1510 Record* B = Edge->getValueAsDef("b");
1511 StringMap<std::string>::iterator IA = ToolToOutLang.find(A->getName());
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001512 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(B->getName());
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001513 if (IA == IAE)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001514 throw A->getName() + ": no such tool!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001515 if (IB == IBE)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001516 throw B->getName() + ": no such tool!";
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001517 if (A->getName() != "root" && IB->second.count(IA->second) == 0)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001518 throw "Edge " + A->getName() + "->" + B->getName()
1519 + ": output->input language mismatch";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001520 if (B->getName() == "root")
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001521 throw std::string("Edges back to the root are not allowed!");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001522 }
1523}
1524
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001525/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
1526/// by EmitEdgeClass().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001527void IncDecWeight (const Init* i, const char* IndentLevel,
1528 std::ostream& O) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001529 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001530 const std::string& OpName = d.getOperator()->getAsString();
1531
1532 if (OpName == "inc_weight")
1533 O << IndentLevel << Indent1 << "ret += ";
1534 else if (OpName == "dec_weight")
1535 O << IndentLevel << Indent1 << "ret -= ";
1536 else
1537 throw "Unknown operator in edge properties list: " + OpName + '!';
1538
1539 if (d.getNumArgs() > 0)
1540 O << InitPtrToInt(d.getArg(0)) << ";\n";
1541 else
1542 O << "2;\n";
1543
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001544}
1545
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001546/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001547void EmitEdgeClass (unsigned N, const std::string& Target,
1548 DagInit* Case, const GlobalOptionDescriptions& OptDescs,
1549 std::ostream& O) {
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001550
1551 // Class constructor.
1552 O << "class Edge" << N << ": public Edge {\n"
1553 << "public:\n"
1554 << Indent1 << "Edge" << N << "() : Edge(\"" << Target
1555 << "\") {}\n\n"
1556
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001557 // Function Weight().
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001558 << Indent1 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n"
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001559 << Indent2 << "unsigned ret = 0;\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001560
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001561 // Handle the 'case' construct.
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001562 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001563
1564 O << Indent2 << "return ret;\n"
1565 << Indent1 << "};\n\n};\n\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001566}
1567
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001568/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001569void EmitEdgeClasses (Record* CompilationGraph,
1570 const GlobalOptionDescriptions& OptDescs,
1571 std::ostream& O) {
1572 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
1573
1574 for (unsigned i = 0; i < edges->size(); ++i) {
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001575 Record* Edge = edges->getElementAsRecord(i);
1576 Record* B = Edge->getValueAsDef("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001577 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001578
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001579 if (isDagEmpty(Weight))
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001580 continue;
1581
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001582 EmitEdgeClass(i, B->getName(), Weight, OptDescs, O);
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001583 }
1584}
1585
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001586/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
1587/// function.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001588void EmitPopulateCompilationGraph (Record* CompilationGraph,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001589 std::ostream& O)
1590{
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001591 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001592
1593 // Generate code
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001594 O << "void llvmc::PopulateCompilationGraph(CompilationGraph& G) {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001595
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001596 // Insert vertices
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001597
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001598 RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
1599 if (Tools.empty())
1600 throw std::string("No tool definitions found!");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001601
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001602 for (RecordVector::iterator B = Tools.begin(), E = Tools.end(); B != E; ++B) {
1603 const std::string& Name = (*B)->getName();
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001604 if (Name != "root")
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001605 O << Indent1 << "G.insertNode(new "
1606 << Name << "());\n";
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001607 }
1608
1609 O << '\n';
1610
1611 // Insert edges
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001612 for (unsigned i = 0; i < edges->size(); ++i) {
1613 Record* Edge = edges->getElementAsRecord(i);
1614 Record* A = Edge->getValueAsDef("a");
1615 Record* B = Edge->getValueAsDef("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001616 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001617
1618 O << Indent1 << "G.insertEdge(\"" << A->getName() << "\", ";
1619
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001620 if (isDagEmpty(Weight))
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001621 O << "new SimpleEdge(\"" << B->getName() << "\")";
1622 else
1623 O << "new Edge" << i << "()";
1624
1625 O << ");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001626 }
1627
1628 O << "}\n\n";
1629}
1630
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001631/// ExtractHookNames - Extract the hook names from all instances of
1632/// $CALL(HookName) in the provided command line string. Helper
1633/// function used by FillInHookNames().
1634void ExtractHookNames(const Init* CmdLine, StrVector& HookNames) {
1635 StrVector cmds;
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001636 llvm::SplitString(InitPtrToString(CmdLine), cmds);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001637 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
1638 B != E; ++B) {
1639 const std::string& cmd = *B;
1640 if (cmd.find("$CALL(") == 0) {
1641 if (cmd.size() == 6)
1642 throw std::string("$CALL invocation: empty argument list!");
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001643 HookNames.push_back(std::string(cmd.begin() + 6,
1644 cmd.begin() + cmd.find(")")));
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001645 }
1646 }
1647}
1648
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001649/// ExtractHookNamesFromCaseConstruct - Extract hook names from the
1650/// 'case' expression, handle nesting. Helper function used by
1651/// FillInHookNames().
1652void ExtractHookNamesFromCaseConstruct(Init* Case, StrVector& HookNames) {
1653 const DagInit& d = InitPtrToDag(Case);
1654 bool even = false;
1655 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1656 B != E; ++B) {
1657 Init* arg = *B;
1658 if (even && dynamic_cast<DagInit*>(arg)
1659 && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case")
1660 ExtractHookNamesFromCaseConstruct(arg, HookNames);
1661 else if (even)
1662 ExtractHookNames(arg, HookNames);
1663 even = !even;
1664 }
1665}
1666
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001667/// FillInHookNames - Actually extract the hook names from all command
1668/// line strings. Helper function used by EmitHookDeclarations().
1669void FillInHookNames(const ToolPropertiesList& TPList,
1670 StrVector& HookNames) {
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001671 // For all command lines:
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001672 for (ToolPropertiesList::const_iterator B = TPList.begin(),
1673 E = TPList.end(); B != E; ++B) {
1674 const ToolProperties& P = *(*B);
1675 if (!P.CmdLine)
1676 continue;
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001677 if (dynamic_cast<StringInit*>(P.CmdLine))
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001678 // This is a string.
1679 ExtractHookNames(P.CmdLine, HookNames);
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001680 else
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001681 // This is a 'case' construct.
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001682 ExtractHookNamesFromCaseConstruct(P.CmdLine, HookNames);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001683 }
1684}
1685
1686/// EmitHookDeclarations - Parse CmdLine fields of all the tool
1687/// property records and emit hook function declaration for each
1688/// instance of $CALL(HookName).
1689void EmitHookDeclarations(const ToolPropertiesList& ToolProps,
1690 std::ostream& O) {
1691 StrVector HookNames;
1692 FillInHookNames(ToolProps, HookNames);
1693 if (HookNames.empty())
1694 return;
1695 std::sort(HookNames.begin(), HookNames.end());
1696 StrVector::const_iterator E = std::unique(HookNames.begin(), HookNames.end());
1697
1698 O << "namespace hooks {\n";
1699 for (StrVector::const_iterator B = HookNames.begin(); B != E; ++B)
1700 O << Indent1 << "std::string " << *B << "();\n";
1701
1702 O << "}\n\n";
1703}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001704
1705// End of anonymous namespace
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001706}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001707
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001708/// run - The back-end entry point.
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001709void LLVMCConfigurationEmitter::run (std::ostream &O) {
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00001710 try {
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001711
1712 // Emit file header.
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001713 EmitSourceFileHeader("LLVMC Configuration Library", O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001714
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001715 // Get a list of all defined Tools.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001716 RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
1717 if (Tools.empty())
1718 throw std::string("No tool definitions found!");
1719
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001720 // Gather information from the Tool description dags.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001721 ToolPropertiesList tool_props;
1722 GlobalOptionDescriptions opt_descs;
1723 CollectToolProperties(Tools.begin(), Tools.end(), tool_props, opt_descs);
1724
Mikhail Glushenkovd638e852008-05-30 06:26:08 +00001725 RecordVector OptionLists = Records.getAllDerivedDefinitions("OptionList");
1726 CollectPropertiesFromOptionList(OptionLists.begin(), OptionLists.end(),
1727 opt_descs);
1728
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +00001729 // Check that there are no options without side effects (specified
1730 // only in the OptionList).
1731 CheckForSuperfluousOptions(tool_props, opt_descs);
1732
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001733 // Emit global option registration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001734 EmitOptionDescriptions(opt_descs, O);
1735
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001736 // Emit hook declarations.
1737 EmitHookDeclarations(tool_props, O);
1738
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001739 // Emit PopulateLanguageMap() function
1740 // (a language map maps from file extensions to language names).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001741 EmitPopulateLanguageMap(Records, O);
1742
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001743 // Emit Tool classes.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001744 for (ToolPropertiesList::const_iterator B = tool_props.begin(),
1745 E = tool_props.end(); B!=E; ++B)
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001746 EmitToolClassDefinition(*(*B), opt_descs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001747
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001748 Record* CompilationGraphRecord = Records.getDef("CompilationGraph");
1749 if (!CompilationGraphRecord)
1750 throw std::string("Compilation graph description not found!");
1751
1752 // Typecheck the compilation graph.
1753 TypecheckGraph(CompilationGraphRecord, tool_props);
1754
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001755 // Emit Edge# classes.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001756 EmitEdgeClasses(CompilationGraphRecord, opt_descs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001757
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001758 // Emit PopulateCompilationGraph() function.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001759 EmitPopulateCompilationGraph(CompilationGraphRecord, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001760
1761 // EOF
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00001762 } catch (std::exception& Error) {
1763 throw Error.what() + std::string(" - usually this means a syntax error.");
1764 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001765}