1 //===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open
6 // Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend is responsible for emitting LLVMC configuration code.
12 //===----------------------------------------------------------------------===//
14 #include "LLVMCConfigurationEmitter.h"
17 #include "llvm/ADT/IntrusiveRefCntPtr.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringSet.h"
33 //===----------------------------------------------------------------------===//
36 typedef std::vector<Record*> RecordVector;
37 typedef std::vector<std::string> StrVector;
39 //===----------------------------------------------------------------------===//
43 unsigned TabWidth = 4;
44 unsigned Indent1 = TabWidth*1;
45 unsigned Indent2 = TabWidth*2;
46 unsigned Indent3 = TabWidth*3;
48 // Default help string.
49 const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
51 // Name for the "sink" option.
52 const char * SinkOptionName = "AutoGeneratedSinkOption";
54 //===----------------------------------------------------------------------===//
57 /// Id - An 'identity' function object.
60 void operator()(const T0&) const {
62 template<typename T0, typename T1>
63 void operator()(const T0&, const T1&) const {
65 template<typename T0, typename T1, typename T2>
66 void operator()(const T0&, const T1&, const T2&) const {
70 int InitPtrToInt(const Init* ptr) {
71 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
72 return val.getValue();
75 const std::string& InitPtrToString(const Init* ptr) {
76 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
77 return val.getValue();
80 const ListInit& InitPtrToList(const Init* ptr) {
81 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
85 const DagInit& InitPtrToDag(const Init* ptr) {
86 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
90 const std::string GetOperatorName(const DagInit* D) {
91 return D->getOperator()->getAsString();
94 const std::string GetOperatorName(const DagInit& D) {
95 return GetOperatorName(&D);
98 // checkNumberOfArguments - Ensure that the number of args in d is
99 // greater than or equal to min_arguments, otherwise throw an exception.
100 void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
101 if (!d || d->getNumArgs() < min_arguments)
102 throw GetOperatorName(d) + ": too few arguments!";
105 // isDagEmpty - is this DAG marked with an empty marker?
106 bool isDagEmpty (const DagInit* d) {
107 return GetOperatorName(d) == "empty_dag_marker";
110 // EscapeVariableName - Escape commas and other symbols not allowed
111 // in the C++ variable names. Makes it possible to use options named
112 // like "Wa," (useful for prefix options).
113 std::string EscapeVariableName(const std::string& Var) {
115 for (unsigned i = 0; i != Var.size(); ++i) {
116 char cur_char = Var[i];
117 if (cur_char == ',') {
120 else if (cur_char == '+') {
123 else if (cur_char == '-') {
127 ret.push_back(cur_char);
133 /// oneOf - Does the input string contain this character?
134 bool oneOf(const char* lst, char c) {
142 template <class I, class S>
143 void checkedIncrement(I& P, I E, S ErrorString) {
149 // apply is needed because C++'s syntax doesn't let us construct a function
150 // object and call it in the same statement.
151 template<typename F, typename T0>
152 void apply(F Fun, T0& Arg0) {
156 template<typename F, typename T0, typename T1>
157 void apply(F Fun, T0& Arg0, T1& Arg1) {
158 return Fun(Arg0, Arg1);
161 //===----------------------------------------------------------------------===//
162 /// Back-end specific code
165 /// OptionType - One of six different option types. See the
166 /// documentation for detailed description of differences.
167 namespace OptionType {
169 enum OptionType { Alias, Switch, Parameter, ParameterList,
172 bool IsAlias(OptionType t) {
176 bool IsList (OptionType t) {
177 return (t == ParameterList || t == PrefixList);
180 bool IsSwitch (OptionType t) {
181 return (t == Switch);
184 bool IsParameter (OptionType t) {
185 return (t == Parameter || t == Prefix);
190 OptionType::OptionType stringToOptionType(const std::string& T) {
191 if (T == "alias_option")
192 return OptionType::Alias;
193 else if (T == "switch_option")
194 return OptionType::Switch;
195 else if (T == "parameter_option")
196 return OptionType::Parameter;
197 else if (T == "parameter_list_option")
198 return OptionType::ParameterList;
199 else if (T == "prefix_option")
200 return OptionType::Prefix;
201 else if (T == "prefix_list_option")
202 return OptionType::PrefixList;
204 throw "Unknown option type: " + T + '!';
207 namespace OptionDescriptionFlags {
208 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
209 ReallyHidden = 0x4, Extern = 0x8,
210 OneOrMore = 0x10, ZeroOrOne = 0x20 };
213 /// OptionDescription - Represents data contained in a single
214 /// OptionList entry.
215 struct OptionDescription {
216 OptionType::OptionType Type;
223 OptionDescription(OptionType::OptionType t = OptionType::Switch,
224 const std::string& n = "",
225 const std::string& h = DefaultHelpString)
226 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
229 /// GenTypeDeclaration - Returns the C++ variable type of this
231 const char* GenTypeDeclaration() const;
233 /// GenVariableName - Returns the variable name used in the
234 /// generated C++ code.
235 std::string GenVariableName() const;
237 /// Merge - Merge two option descriptions.
238 void Merge (const OptionDescription& other);
240 // Misc convenient getters/setters.
242 bool isAlias() const;
244 bool isMultiVal() const;
246 bool isExtern() const;
249 bool isRequired() const;
252 bool isOneOrMore() const;
255 bool isZeroOrOne() const;
258 bool isHidden() const;
261 bool isReallyHidden() const;
262 void setReallyHidden();
264 bool isSwitch() const
265 { return OptionType::IsSwitch(this->Type); }
267 bool isParameter() const
268 { return OptionType::IsParameter(this->Type); }
271 { return OptionType::IsList(this->Type); }
275 void OptionDescription::Merge (const OptionDescription& other)
277 if (other.Type != Type)
278 throw "Conflicting definitions for the option " + Name + "!";
280 if (Help == other.Help || Help == DefaultHelpString)
282 else if (other.Help != DefaultHelpString) {
283 llvm::errs() << "Warning: several different help strings"
284 " defined for option " + Name + "\n";
287 Flags |= other.Flags;
290 bool OptionDescription::isAlias() const {
291 return OptionType::IsAlias(this->Type);
294 bool OptionDescription::isMultiVal() const {
298 bool OptionDescription::isExtern() const {
299 return Flags & OptionDescriptionFlags::Extern;
301 void OptionDescription::setExtern() {
302 Flags |= OptionDescriptionFlags::Extern;
305 bool OptionDescription::isRequired() const {
306 return Flags & OptionDescriptionFlags::Required;
308 void OptionDescription::setRequired() {
309 Flags |= OptionDescriptionFlags::Required;
312 bool OptionDescription::isOneOrMore() const {
313 return Flags & OptionDescriptionFlags::OneOrMore;
315 void OptionDescription::setOneOrMore() {
316 Flags |= OptionDescriptionFlags::OneOrMore;
319 bool OptionDescription::isZeroOrOne() const {
320 return Flags & OptionDescriptionFlags::ZeroOrOne;
322 void OptionDescription::setZeroOrOne() {
323 Flags |= OptionDescriptionFlags::ZeroOrOne;
326 bool OptionDescription::isHidden() const {
327 return Flags & OptionDescriptionFlags::Hidden;
329 void OptionDescription::setHidden() {
330 Flags |= OptionDescriptionFlags::Hidden;
333 bool OptionDescription::isReallyHidden() const {
334 return Flags & OptionDescriptionFlags::ReallyHidden;
336 void OptionDescription::setReallyHidden() {
337 Flags |= OptionDescriptionFlags::ReallyHidden;
340 const char* OptionDescription::GenTypeDeclaration() const {
342 case OptionType::Alias:
344 case OptionType::PrefixList:
345 case OptionType::ParameterList:
346 return "cl::list<std::string>";
347 case OptionType::Switch:
348 return "cl::opt<bool>";
349 case OptionType::Parameter:
350 case OptionType::Prefix:
352 return "cl::opt<std::string>";
356 std::string OptionDescription::GenVariableName() const {
357 const std::string& EscapedName = EscapeVariableName(Name);
359 case OptionType::Alias:
360 return "AutoGeneratedAlias_" + EscapedName;
361 case OptionType::PrefixList:
362 case OptionType::ParameterList:
363 return "AutoGeneratedList_" + EscapedName;
364 case OptionType::Switch:
365 return "AutoGeneratedSwitch_" + EscapedName;
366 case OptionType::Prefix:
367 case OptionType::Parameter:
369 return "AutoGeneratedParameter_" + EscapedName;
373 /// OptionDescriptions - An OptionDescription array plus some helper
375 class OptionDescriptions {
376 typedef StringMap<OptionDescription> container_type;
378 /// Descriptions - A list of OptionDescriptions.
379 container_type Descriptions;
382 /// FindOption - exception-throwing wrapper for find().
383 const OptionDescription& FindOption(const std::string& OptName) const;
385 // Wrappers for FindOption that throw an exception in case the option has a
387 const OptionDescription& FindSwitch(const std::string& OptName) const;
388 const OptionDescription& FindParameter(const std::string& OptName) const;
389 const OptionDescription& FindList(const std::string& OptName) const;
390 const OptionDescription&
391 FindListOrParameter(const std::string& OptName) const;
393 /// insertDescription - Insert new OptionDescription into
394 /// OptionDescriptions list
395 void InsertDescription (const OptionDescription& o);
397 // Support for STL-style iteration
398 typedef container_type::const_iterator const_iterator;
399 const_iterator begin() const { return Descriptions.begin(); }
400 const_iterator end() const { return Descriptions.end(); }
403 const OptionDescription&
404 OptionDescriptions::FindOption(const std::string& OptName) const {
405 const_iterator I = Descriptions.find(OptName);
406 if (I != Descriptions.end())
409 throw OptName + ": no such option!";
412 const OptionDescription&
413 OptionDescriptions::FindSwitch(const std::string& OptName) const {
414 const OptionDescription& OptDesc = this->FindOption(OptName);
415 if (!OptDesc.isSwitch())
416 throw OptName + ": incorrect option type - should be a switch!";
420 const OptionDescription&
421 OptionDescriptions::FindList(const std::string& OptName) const {
422 const OptionDescription& OptDesc = this->FindOption(OptName);
423 if (!OptDesc.isList())
424 throw OptName + ": incorrect option type - should be a list!";
428 const OptionDescription&
429 OptionDescriptions::FindParameter(const std::string& OptName) const {
430 const OptionDescription& OptDesc = this->FindOption(OptName);
431 if (!OptDesc.isParameter())
432 throw OptName + ": incorrect option type - should be a parameter!";
436 const OptionDescription&
437 OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
438 const OptionDescription& OptDesc = this->FindOption(OptName);
439 if (!OptDesc.isList() && !OptDesc.isParameter())
441 + ": incorrect option type - should be a list or parameter!";
445 void OptionDescriptions::InsertDescription (const OptionDescription& o) {
446 container_type::iterator I = Descriptions.find(o.Name);
447 if (I != Descriptions.end()) {
448 OptionDescription& D = I->second;
452 Descriptions[o.Name] = o;
456 /// HandlerTable - A base class for function objects implemented as
457 /// 'tables of handlers'.
461 // Implementation details.
464 typedef void (T::* Handler) (const DagInit*);
465 /// HandlerMap - A map from property names to property handlers
466 typedef StringMap<Handler> HandlerMap;
468 static HandlerMap Handlers_;
469 static bool staticMembersInitialized_;
474 HandlerTable(T* cp) : childPtr(cp)
477 /// operator() - Just forwards to the corresponding property
479 void operator() (Init* i) {
480 const DagInit& property = InitPtrToDag(i);
481 const std::string& property_name = GetOperatorName(property);
482 typename HandlerMap::iterator method = Handlers_.find(property_name);
484 if (method != Handlers_.end()) {
485 Handler h = method->second;
486 (childPtr->*h)(&property);
489 throw "No handler found for property " + property_name + "!";
493 void AddHandler(const char* Property, Handler Handl) {
494 Handlers_[Property] = Handl;
498 template <class T> typename HandlerTable<T>::HandlerMap
499 HandlerTable<T>::Handlers_;
500 template <class T> bool HandlerTable<T>::staticMembersInitialized_ = false;
503 /// CollectOptionProperties - Function object for iterating over an
504 /// option property list.
505 class CollectOptionProperties : public HandlerTable<CollectOptionProperties> {
508 /// optDescs_ - OptionDescriptions table. This is where the
509 /// information is stored.
510 OptionDescription& optDesc_;
514 explicit CollectOptionProperties(OptionDescription& OD)
515 : HandlerTable<CollectOptionProperties>(this), optDesc_(OD)
517 if (!staticMembersInitialized_) {
518 AddHandler("extern", &CollectOptionProperties::onExtern);
519 AddHandler("help", &CollectOptionProperties::onHelp);
520 AddHandler("hidden", &CollectOptionProperties::onHidden);
521 AddHandler("init", &CollectOptionProperties::onInit);
522 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
523 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
524 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
525 AddHandler("required", &CollectOptionProperties::onRequired);
526 AddHandler("zero_or_one", &CollectOptionProperties::onZeroOrOne);
528 staticMembersInitialized_ = true;
534 /// Option property handlers --
535 /// Methods that handle option properties such as (help) or (hidden).
537 void onExtern (const DagInit* d) {
538 checkNumberOfArguments(d, 0);
539 optDesc_.setExtern();
542 void onHelp (const DagInit* d) {
543 checkNumberOfArguments(d, 1);
544 optDesc_.Help = InitPtrToString(d->getArg(0));
547 void onHidden (const DagInit* d) {
548 checkNumberOfArguments(d, 0);
549 optDesc_.setHidden();
552 void onReallyHidden (const DagInit* d) {
553 checkNumberOfArguments(d, 0);
554 optDesc_.setReallyHidden();
557 void onRequired (const DagInit* d) {
558 checkNumberOfArguments(d, 0);
559 if (optDesc_.isOneOrMore())
560 throw std::string("An option can't have both (required) "
561 "and (one_or_more) properties!");
562 optDesc_.setRequired();
565 void onInit (const DagInit* d) {
566 checkNumberOfArguments(d, 1);
567 Init* i = d->getArg(0);
568 const std::string& str = i->getAsString();
570 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
571 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
574 throw std::string("Incorrect usage of the 'init' option property!");
576 optDesc_.InitVal = i;
579 void onOneOrMore (const DagInit* d) {
580 checkNumberOfArguments(d, 0);
581 if (optDesc_.isRequired() || optDesc_.isZeroOrOne())
582 throw std::string("Only one of (required), (zero_or_one) or "
583 "(one_or_more) properties is allowed!");
584 if (!OptionType::IsList(optDesc_.Type))
585 llvm::errs() << "Warning: specifying the 'one_or_more' property "
586 "on a non-list option will have no effect.\n";
587 optDesc_.setOneOrMore();
590 void onZeroOrOne (const DagInit* d) {
591 checkNumberOfArguments(d, 0);
592 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
593 throw std::string("Only one of (required), (zero_or_one) or "
594 "(one_or_more) properties is allowed!");
595 if (!OptionType::IsList(optDesc_.Type))
596 llvm::errs() << "Warning: specifying the 'zero_or_one' property"
597 "on a non-list option will have no effect.\n";
598 optDesc_.setZeroOrOne();
601 void onMultiVal (const DagInit* d) {
602 checkNumberOfArguments(d, 1);
603 int val = InitPtrToInt(d->getArg(0));
605 throw std::string("Error in the 'multi_val' property: "
606 "the value must be greater than 1!");
607 if (!OptionType::IsList(optDesc_.Type))
608 throw std::string("The multi_val property is valid only "
610 optDesc_.MultiVal = val;
615 /// AddOption - A function object that is applied to every option
616 /// description. Used by CollectOptionDescriptions.
619 OptionDescriptions& OptDescs_;
622 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
625 void operator()(const Init* i) {
626 const DagInit& d = InitPtrToDag(i);
627 checkNumberOfArguments(&d, 1);
629 const OptionType::OptionType Type =
630 stringToOptionType(GetOperatorName(d));
631 const std::string& Name = InitPtrToString(d.getArg(0));
633 OptionDescription OD(Type, Name);
636 checkNumberOfArguments(&d, 2);
639 // Aliases store the aliased option name in the 'Help' field.
640 OD.Help = InitPtrToString(d.getArg(1));
642 else if (!OD.isExtern()) {
643 processOptionProperties(&d, OD);
645 OptDescs_.InsertDescription(OD);
649 /// processOptionProperties - Go through the list of option
650 /// properties and call a corresponding handler for each.
651 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
652 checkNumberOfArguments(d, 2);
653 DagInit::const_arg_iterator B = d->arg_begin();
654 // Skip the first argument: it's always the option name.
656 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
661 /// CollectOptionDescriptions - Collects option properties from all
663 void CollectOptionDescriptions (RecordVector::const_iterator B,
664 RecordVector::const_iterator E,
665 OptionDescriptions& OptDescs)
667 // For every OptionList:
669 RecordVector::value_type T = *B;
670 // Throws an exception if the value does not exist.
671 ListInit* PropList = T->getValueAsListInit("options");
673 // For every option description in this list:
674 // collect the information and
675 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
679 // Tool information record
681 namespace ToolFlags {
682 enum ToolFlags { Join = 0x1, Sink = 0x2 };
685 struct ToolDescription : public RefCountedBase<ToolDescription> {
689 StrVector InLanguage;
690 std::string OutLanguage;
691 std::string OutputSuffix;
694 // Various boolean properties
695 void setSink() { Flags |= ToolFlags::Sink; }
696 bool isSink() const { return Flags & ToolFlags::Sink; }
697 void setJoin() { Flags |= ToolFlags::Join; }
698 bool isJoin() const { return Flags & ToolFlags::Join; }
700 // Default ctor here is needed because StringMap can only store
701 // DefaultConstructible objects
702 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
703 ToolDescription (const std::string& n)
704 : Name(n), CmdLine(0), Actions(0), Flags(0)
708 /// ToolDescriptions - A list of Tool information records.
709 typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
712 /// CollectToolProperties - Function object for iterating over a list of
713 /// tool property records.
714 class CollectToolProperties : public HandlerTable<CollectToolProperties> {
717 /// toolDesc_ - Properties of the current Tool. This is where the
718 /// information is stored.
719 ToolDescription& toolDesc_;
723 explicit CollectToolProperties (ToolDescription& d)
724 : HandlerTable<CollectToolProperties>(this) , toolDesc_(d)
726 if (!staticMembersInitialized_) {
728 AddHandler("actions", &CollectToolProperties::onActions);
729 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
730 AddHandler("in_language", &CollectToolProperties::onInLanguage);
731 AddHandler("join", &CollectToolProperties::onJoin);
732 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
733 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
734 AddHandler("sink", &CollectToolProperties::onSink);
736 staticMembersInitialized_ = true;
742 /// Property handlers --
743 /// Functions that extract information about tool properties from
744 /// DAG representation.
746 void onActions (const DagInit* d) {
747 checkNumberOfArguments(d, 1);
748 Init* Case = d->getArg(0);
749 if (typeid(*Case) != typeid(DagInit) ||
750 GetOperatorName(static_cast<DagInit*>(Case)) != "case")
752 std::string("The argument to (actions) should be a 'case' construct!");
753 toolDesc_.Actions = Case;
756 void onCmdLine (const DagInit* d) {
757 checkNumberOfArguments(d, 1);
758 toolDesc_.CmdLine = d->getArg(0);
761 void onInLanguage (const DagInit* d) {
762 checkNumberOfArguments(d, 1);
763 Init* arg = d->getArg(0);
765 // Find out the argument's type.
766 if (typeid(*arg) == typeid(StringInit)) {
768 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
772 const ListInit& lst = InitPtrToList(arg);
773 StrVector& out = toolDesc_.InLanguage;
775 // Copy strings to the output vector.
776 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
778 out.push_back(InitPtrToString(*B));
781 // Remove duplicates.
782 std::sort(out.begin(), out.end());
783 StrVector::iterator newE = std::unique(out.begin(), out.end());
784 out.erase(newE, out.end());
788 void onJoin (const DagInit* d) {
789 checkNumberOfArguments(d, 0);
793 void onOutLanguage (const DagInit* d) {
794 checkNumberOfArguments(d, 1);
795 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
798 void onOutputSuffix (const DagInit* d) {
799 checkNumberOfArguments(d, 1);
800 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
803 void onSink (const DagInit* d) {
804 checkNumberOfArguments(d, 0);
810 /// CollectToolDescriptions - Gather information about tool properties
811 /// from the parsed TableGen data (basically a wrapper for the
812 /// CollectToolProperties function object).
813 void CollectToolDescriptions (RecordVector::const_iterator B,
814 RecordVector::const_iterator E,
815 ToolDescriptions& ToolDescs)
817 // Iterate over a properties list of every Tool definition
819 const Record* T = *B;
820 // Throws an exception if the value does not exist.
821 ListInit* PropList = T->getValueAsListInit("properties");
823 IntrusiveRefCntPtr<ToolDescription>
824 ToolDesc(new ToolDescription(T->getName()));
826 std::for_each(PropList->begin(), PropList->end(),
827 CollectToolProperties(*ToolDesc));
828 ToolDescs.push_back(ToolDesc);
832 /// FillInEdgeVector - Merge all compilation graph definitions into
833 /// one single edge list.
834 void FillInEdgeVector(RecordVector::const_iterator B,
835 RecordVector::const_iterator E, RecordVector& Out) {
836 for (; B != E; ++B) {
837 const ListInit* edges = (*B)->getValueAsListInit("edges");
839 for (unsigned i = 0; i < edges->size(); ++i)
840 Out.push_back(edges->getElementAsRecord(i));
844 /// CalculatePriority - Calculate the priority of this plugin.
845 int CalculatePriority(RecordVector::const_iterator B,
846 RecordVector::const_iterator E) {
850 priority = static_cast<int>((*B)->getValueAsInt("priority"));
853 throw std::string("More than one 'PluginPriority' instance found: "
854 "most probably an error!");
860 /// NotInGraph - Helper function object for FilterNotInGraph.
863 const llvm::StringSet<>& ToolsInGraph_;
866 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
867 : ToolsInGraph_(ToolsInGraph)
870 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
871 return (ToolsInGraph_.count(x->Name) == 0);
875 /// FilterNotInGraph - Filter out from ToolDescs all Tools not
876 /// mentioned in the compilation graph definition.
877 void FilterNotInGraph (const RecordVector& EdgeVector,
878 ToolDescriptions& ToolDescs) {
880 // List all tools mentioned in the graph.
881 llvm::StringSet<> ToolsInGraph;
883 for (RecordVector::const_iterator B = EdgeVector.begin(),
884 E = EdgeVector.end(); B != E; ++B) {
886 const Record* Edge = *B;
887 const std::string& NodeA = Edge->getValueAsString("a");
888 const std::string& NodeB = Edge->getValueAsString("b");
891 ToolsInGraph.insert(NodeA);
892 ToolsInGraph.insert(NodeB);
895 // Filter ToolPropertiesList.
896 ToolDescriptions::iterator new_end =
897 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
898 NotInGraph(ToolsInGraph));
899 ToolDescs.erase(new_end, ToolDescs.end());
902 /// FillInToolToLang - Fills in two tables that map tool names to
903 /// (input, output) languages. Helper function used by TypecheckGraph().
904 void FillInToolToLang (const ToolDescriptions& ToolDescs,
905 StringMap<StringSet<> >& ToolToInLang,
906 StringMap<std::string>& ToolToOutLang) {
907 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
908 E = ToolDescs.end(); B != E; ++B) {
909 const ToolDescription& D = *(*B);
910 for (StrVector::const_iterator B = D.InLanguage.begin(),
911 E = D.InLanguage.end(); B != E; ++B)
912 ToolToInLang[D.Name].insert(*B);
913 ToolToOutLang[D.Name] = D.OutLanguage;
917 /// TypecheckGraph - Check that names for output and input languages
918 /// on all edges do match. This doesn't do much when the information
919 /// about the whole graph is not available (i.e. when compiling most
921 void TypecheckGraph (const RecordVector& EdgeVector,
922 const ToolDescriptions& ToolDescs) {
923 StringMap<StringSet<> > ToolToInLang;
924 StringMap<std::string> ToolToOutLang;
926 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
927 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
928 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
930 for (RecordVector::const_iterator B = EdgeVector.begin(),
931 E = EdgeVector.end(); B != E; ++B) {
932 const Record* Edge = *B;
933 const std::string& NodeA = Edge->getValueAsString("a");
934 const std::string& NodeB = Edge->getValueAsString("b");
935 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
936 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
938 if (NodeA != "root") {
939 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
940 throw "Edge " + NodeA + "->" + NodeB
941 + ": output->input language mismatch";
945 throw std::string("Edges back to the root are not allowed!");
949 /// WalkCase - Walks the 'case' expression DAG and invokes
950 /// TestCallback on every test, and StatementCallback on every
951 /// statement. Handles 'case' nesting, but not the 'and' and 'or'
952 /// combinators (that is, they are passed directly to TestCallback).
953 /// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
954 /// IndentLevel, bool FirstTest)'.
955 /// StatementCallback must have type 'void StatementCallback(const Init*,
956 /// unsigned IndentLevel)'.
957 template <typename F1, typename F2>
958 void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
959 unsigned IndentLevel = 0)
961 const DagInit& d = InitPtrToDag(Case);
964 if (GetOperatorName(d) != "case")
965 throw std::string("WalkCase should be invoked only on 'case' expressions!");
967 if (d.getNumArgs() < 2)
968 throw "There should be at least one clause in the 'case' expression:\n"
973 const unsigned numArgs = d.getNumArgs();
975 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
982 const DagInit& Test = InitPtrToDag(arg);
984 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
985 throw std::string("The 'default' clause should be the last in the"
986 "'case' construct!");
988 throw "Case construct handler: no corresponding action "
989 "found for the test " + Test.getAsString() + '!';
991 TestCallback(&Test, IndentLevel, (i == 1));
995 if (dynamic_cast<DagInit*>(arg)
996 && GetOperatorName(static_cast<DagInit*>(arg)) == "case") {
998 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1001 // Handle statement.
1002 StatementCallback(arg, IndentLevel);
1010 /// ExtractOptionNames - A helper function object used by
1011 /// CheckForSuperfluousOptions() to walk the 'case' DAG.
1012 class ExtractOptionNames {
1013 llvm::StringSet<>& OptionNames_;
1015 void processDag(const Init* Statement) {
1016 const DagInit& Stmt = InitPtrToDag(Statement);
1017 const std::string& ActionName = GetOperatorName(Stmt);
1018 if (ActionName == "forward" || ActionName == "forward_as" ||
1019 ActionName == "unpack_values" || ActionName == "switch_on" ||
1020 ActionName == "parameter_equals" || ActionName == "element_in_list" ||
1021 ActionName == "not_empty" || ActionName == "empty") {
1022 checkNumberOfArguments(&Stmt, 1);
1023 const std::string& Name = InitPtrToString(Stmt.getArg(0));
1024 OptionNames_.insert(Name);
1026 else if (ActionName == "and" || ActionName == "or") {
1027 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
1028 this->processDag(Stmt.getArg(i));
1034 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1037 void operator()(const Init* Statement) {
1038 if (typeid(*Statement) == typeid(ListInit)) {
1039 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1040 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1042 this->processDag(*B);
1045 this->processDag(Statement);
1049 void operator()(const DagInit* Test, unsigned, bool) {
1050 this->operator()(Test);
1052 void operator()(const Init* Statement, unsigned) {
1053 this->operator()(Statement);
1057 /// CheckForSuperfluousOptions - Check that there are no side
1058 /// effect-free options (specified only in the OptionList). Otherwise,
1059 /// output a warning.
1060 void CheckForSuperfluousOptions (const RecordVector& Edges,
1061 const ToolDescriptions& ToolDescs,
1062 const OptionDescriptions& OptDescs) {
1063 llvm::StringSet<> nonSuperfluousOptions;
1065 // Add all options mentioned in the ToolDesc.Actions to the set of
1066 // non-superfluous options.
1067 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1068 E = ToolDescs.end(); B != E; ++B) {
1069 const ToolDescription& TD = *(*B);
1070 ExtractOptionNames Callback(nonSuperfluousOptions);
1072 WalkCase(TD.Actions, Callback, Callback);
1075 // Add all options mentioned in the 'case' clauses of the
1076 // OptionalEdges of the compilation graph to the set of
1077 // non-superfluous options.
1078 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1080 const Record* Edge = *B;
1081 DagInit* Weight = Edge->getValueAsDag("weight");
1083 if (!isDagEmpty(Weight))
1084 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
1087 // Check that all options in OptDescs belong to the set of
1088 // non-superfluous options.
1089 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
1090 E = OptDescs.end(); B != E; ++B) {
1091 const OptionDescription& Val = B->second;
1092 if (!nonSuperfluousOptions.count(Val.Name)
1093 && Val.Type != OptionType::Alias)
1094 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
1095 "Probable cause: this option is specified only in the OptionList.\n";
1099 /// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1100 bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1101 if (TestName == "single_input_file") {
1102 O << "InputFilenames.size() == 1";
1105 else if (TestName == "multiple_input_files") {
1106 O << "InputFilenames.size() > 1";
1113 /// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1114 template <typename F>
1115 void EmitListTest(const ListInit& L, const char* LogicOp,
1116 F Callback, raw_ostream& O)
1118 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1120 bool isFirst = true;
1121 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1126 Callback(InitPtrToString(*B), O);
1130 // Callbacks for use with EmitListTest.
1132 class EmitSwitchOn {
1133 const OptionDescriptions& OptDescs_;
1135 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1138 void operator()(const std::string& OptName, raw_ostream& O) const {
1139 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1140 O << OptDesc.GenVariableName();
1144 class EmitEmptyTest {
1146 const OptionDescriptions& OptDescs_;
1148 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1149 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1152 void operator()(const std::string& OptName, raw_ostream& O) const {
1153 const char* Neg = (EmitNegate_ ? "!" : "");
1154 if (OptName == "o") {
1155 O << Neg << "OutputFilename.empty()";
1158 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1159 O << Neg << OptDesc.GenVariableName() << ".empty()";
1165 /// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1166 bool EmitCaseTest1ArgList(const std::string& TestName,
1168 const OptionDescriptions& OptDescs,
1170 const ListInit& L = *static_cast<ListInit*>(d.getArg(0));
1172 if (TestName == "any_switch_on") {
1173 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1176 else if (TestName == "switch_on") {
1177 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1180 else if (TestName == "any_not_empty") {
1181 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1184 else if (TestName == "any_empty") {
1185 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1188 else if (TestName == "not_empty") {
1189 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1192 else if (TestName == "empty") {
1193 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1200 /// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1201 bool EmitCaseTest1ArgStr(const std::string& TestName,
1203 const OptionDescriptions& OptDescs,
1205 const std::string& OptName = InitPtrToString(d.getArg(0));
1207 if (TestName == "switch_on") {
1208 apply(EmitSwitchOn(OptDescs), OptName, O);
1211 else if (TestName == "input_languages_contain") {
1212 O << "InLangs.count(\"" << OptName << "\") != 0";
1215 else if (TestName == "in_language") {
1216 // This works only for single-argument Tool::GenerateAction. Join
1217 // tools can process several files in different languages simultaneously.
1219 // TODO: make this work with Edge::Weight (if possible).
1220 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
1223 else if (TestName == "not_empty" || TestName == "empty") {
1224 bool EmitNegate = (TestName == "not_empty");
1225 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
1232 /// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1233 bool EmitCaseTest1Arg(const std::string& TestName,
1235 const OptionDescriptions& OptDescs,
1237 checkNumberOfArguments(&d, 1);
1238 if (typeid(*d.getArg(0)) == typeid(ListInit))
1239 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1241 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1244 /// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
1245 bool EmitCaseTest2Args(const std::string& TestName,
1247 unsigned IndentLevel,
1248 const OptionDescriptions& OptDescs,
1250 checkNumberOfArguments(&d, 2);
1251 const std::string& OptName = InitPtrToString(d.getArg(0));
1252 const std::string& OptArg = InitPtrToString(d.getArg(1));
1254 if (TestName == "parameter_equals") {
1255 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
1256 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1259 else if (TestName == "element_in_list") {
1260 const OptionDescription& OptDesc = OptDescs.FindList(OptName);
1261 const std::string& VarName = OptDesc.GenVariableName();
1262 O << "std::find(" << VarName << ".begin(),\n";
1263 O.indent(IndentLevel + Indent1)
1264 << VarName << ".end(), \""
1265 << OptArg << "\") != " << VarName << ".end()";
1272 // Forward declaration.
1273 // EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
1274 void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
1275 const OptionDescriptions& OptDescs,
1278 /// EmitLogicalOperationTest - Helper function used by
1279 /// EmitCaseConstructHandler.
1280 void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
1281 unsigned IndentLevel,
1282 const OptionDescriptions& OptDescs,
1285 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
1286 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
1287 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1288 if (j != NumArgs - 1) {
1290 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1298 void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
1299 const OptionDescriptions& OptDescs, raw_ostream& O)
1301 checkNumberOfArguments(&d, 1);
1302 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1304 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1308 /// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
1309 void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
1310 const OptionDescriptions& OptDescs,
1312 const std::string& TestName = GetOperatorName(d);
1314 if (TestName == "and")
1315 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1316 else if (TestName == "or")
1317 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
1318 else if (TestName == "not")
1319 EmitLogicalNot(d, IndentLevel, OptDescs, O);
1320 else if (EmitCaseTest0Args(TestName, O))
1322 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1324 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1327 throw TestName + ": unknown edge property!";
1331 /// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1332 class EmitCaseTestCallback {
1334 const OptionDescriptions& OptDescs_;
1338 EmitCaseTestCallback(bool EmitElseIf,
1339 const OptionDescriptions& OptDescs, raw_ostream& O)
1340 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1343 void operator()(const DagInit* Test, unsigned IndentLevel, bool FirstTest)
1345 if (GetOperatorName(Test) == "default") {
1346 O_.indent(IndentLevel) << "else {\n";
1349 O_.indent(IndentLevel)
1350 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
1351 EmitCaseTest(*Test, IndentLevel, OptDescs_, O_);
1357 /// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1358 template <typename F>
1359 class EmitCaseStatementCallback {
1364 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1365 : Callback_(Callback), O_(O)
1368 void operator() (const Init* Statement, unsigned IndentLevel) {
1370 // Ignore nested 'case' DAG.
1371 if (!(dynamic_cast<const DagInit*>(Statement) &&
1372 GetOperatorName(static_cast<const DagInit*>(Statement)) == "case")) {
1373 if (typeid(*Statement) == typeid(ListInit)) {
1374 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1375 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1377 Callback_(*B, (IndentLevel + Indent1), O_);
1380 Callback_(Statement, (IndentLevel + Indent1), O_);
1383 O_.indent(IndentLevel) << "}\n";
1388 /// EmitCaseConstructHandler - Emit code that handles the 'case'
1389 /// construct. Takes a function object that should emit code for every case
1390 /// clause. Implemented on top of WalkCase.
1391 /// Callback's type is void F(Init* Statement, unsigned IndentLevel,
1392 /// raw_ostream& O).
1393 /// EmitElseIf parameter controls the type of condition that is emitted ('if
1394 /// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1396 template <typename F>
1397 void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
1398 F Callback, bool EmitElseIf,
1399 const OptionDescriptions& OptDescs,
1401 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1402 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
1405 /// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to
1406 /// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path"] .
1407 /// Helper function used by EmitCmdLineVecFill and.
1408 void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
1409 const char* Delimiters = " \t\n\v\f\r";
1411 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1414 if (CmdLine.empty())
1418 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1421 for (; B != E; ++B) {
1422 char cur_ch = CmdLine[B];
1426 if (cur_ch == '$') {
1427 cur_st = SpecialCommand;
1430 if (oneOf(Delimiters, cur_ch)) {
1432 B = CmdLine.find_first_not_of(Delimiters, B);
1433 if (B == std::string::npos) {
1444 case SpecialCommand:
1445 if (oneOf(Delimiters, cur_ch)) {
1450 if (cur_ch == '(') {
1452 cur_st = InsideSpecialCommand;
1457 case InsideSpecialCommand:
1458 if (oneOf(Delimiters, cur_ch)) {
1461 if (cur_ch == '\'') {
1462 cur_st = InsideQuotationMarks;
1466 if (cur_ch == ')') {
1470 if (cur_ch == ',') {
1476 case InsideQuotationMarks:
1477 if (cur_ch == '\'') {
1478 cur_st = InsideSpecialCommand;
1484 Out.back().push_back(cur_ch);
1488 /// SubstituteSpecialCommands - Perform string substitution for $CALL
1489 /// and $ENV. Helper function used by EmitCmdLineVecFill().
1490 StrVector::const_iterator SubstituteSpecialCommands
1491 (StrVector::const_iterator Pos, StrVector::const_iterator End, raw_ostream& O)
1494 const std::string& cmd = *Pos;
1496 if (cmd == "$CALL") {
1497 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1498 const std::string& CmdName = *Pos;
1501 throw std::string("$CALL invocation: empty argument list!");
1504 O << CmdName << "(";
1507 bool firstIteration = true;
1509 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1510 const std::string& Arg = *Pos;
1511 assert(Arg.size() != 0);
1517 firstIteration = false;
1521 O << '"' << Arg << '"';
1527 else if (cmd == "$ENV") {
1528 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
1529 const std::string& EnvName = *Pos;
1532 throw "$ENV invocation: empty argument list!";
1534 O << "checkCString(std::getenv(\"";
1538 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
1541 throw "Unknown special command: " + cmd;
1544 const std::string& Leftover = *Pos;
1545 assert(Leftover.at(0) == ')');
1546 if (Leftover.size() != 1)
1547 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
1552 /// EmitCmdLineVecFill - Emit code that fills in the command line
1553 /// vector. Helper function used by EmitGenerateActionMethod().
1554 void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
1555 bool IsJoin, unsigned IndentLevel,
1558 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1561 throw "Tool '" + ToolName + "' has empty command line!";
1563 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1565 // If there is a hook invocation on the place of the first command, skip it.
1566 assert(!StrVec[0].empty());
1567 if (StrVec[0][0] == '$') {
1568 while (I != E && (*I)[0] != ')' )
1571 // Skip the ')' symbol.
1578 bool hasINFILE = false;
1580 for (; I != E; ++I) {
1581 const std::string& cmd = *I;
1582 assert(!cmd.empty());
1583 O.indent(IndentLevel);
1584 if (cmd.at(0) == '$') {
1585 if (cmd == "$INFILE") {
1588 O << "for (PathVector::const_iterator B = inFiles.begin()"
1589 << ", E = inFiles.end();\n";
1590 O.indent(IndentLevel) << "B != E; ++B)\n";
1591 O.indent(IndentLevel + Indent1) << "vec.push_back(B->str());\n";
1594 O << "vec.push_back(inFile.str());\n";
1597 else if (cmd == "$OUTFILE") {
1598 O << "vec.push_back(\"\");\n";
1599 O.indent(IndentLevel) << "out_file_index = vec.size()-1;\n";
1602 O << "vec.push_back(";
1603 I = SubstituteSpecialCommands(I, E, O);
1608 O << "vec.push_back(\"" << cmd << "\");\n";
1612 throw "Tool '" + ToolName + "' doesn't take any input!";
1614 O.indent(IndentLevel) << "cmd = ";
1615 if (StrVec[0][0] == '$')
1616 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), O);
1618 O << '"' << StrVec[0] << '"';
1622 /// EmitCmdLineVecFillCallback - A function object wrapper around
1623 /// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1624 /// argument to EmitCaseConstructHandler().
1625 class EmitCmdLineVecFillCallback {
1627 const std::string& ToolName;
1629 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1630 : IsJoin(J), ToolName(TN) {}
1632 void operator()(const Init* Statement, unsigned IndentLevel,
1633 raw_ostream& O) const
1635 EmitCmdLineVecFill(Statement, ToolName, IsJoin, IndentLevel, O);
1639 /// EmitForwardOptionPropertyHandlingCode - Helper function used to
1640 /// implement EmitActionHandler. Emits code for
1641 /// handling the (forward) and (forward_as) option properties.
1642 void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
1643 unsigned IndentLevel,
1644 const std::string& NewName,
1646 const std::string& Name = NewName.empty()
1649 unsigned IndentLevel1 = IndentLevel + Indent1;
1652 case OptionType::Switch:
1653 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
1655 case OptionType::Parameter:
1656 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
1657 O.indent(IndentLevel) << "vec.push_back(" << D.GenVariableName() << ");\n";
1659 case OptionType::Prefix:
1660 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\" + "
1661 << D.GenVariableName() << ");\n";
1663 case OptionType::PrefixList:
1664 O.indent(IndentLevel)
1665 << "for (" << D.GenTypeDeclaration()
1666 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1667 O.indent(IndentLevel)
1668 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1669 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\" + " << "*B);\n";
1670 O.indent(IndentLevel1) << "++B;\n";
1672 for (int i = 1, j = D.MultiVal; i < j; ++i) {
1673 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1674 O.indent(IndentLevel1) << "++B;\n";
1677 O.indent(IndentLevel) << "}\n";
1679 case OptionType::ParameterList:
1680 O.indent(IndentLevel)
1681 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1682 << D.GenVariableName() << ".begin(),\n";
1683 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1684 << ".end() ; B != E;) {\n";
1685 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\");\n";
1687 for (int i = 0, j = D.MultiVal; i < j; ++i) {
1688 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1689 O.indent(IndentLevel1) << "++B;\n";
1692 O.indent(IndentLevel) << "}\n";
1694 case OptionType::Alias:
1696 throw std::string("Aliases are not allowed in tool option descriptions!");
1700 /// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1701 /// EmitPreprocessOptionsCallback.
1702 struct ActionHandlingCallbackBase {
1704 void onErrorDag(const DagInit& d,
1705 unsigned IndentLevel, raw_ostream& O) const
1707 O.indent(IndentLevel)
1708 << "throw std::runtime_error(\"" <<
1709 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1714 void onWarningDag(const DagInit& d,
1715 unsigned IndentLevel, raw_ostream& O) const
1717 checkNumberOfArguments(&d, 1);
1718 O.indent(IndentLevel) << "llvm::errs() << \""
1719 << InitPtrToString(d.getArg(0)) << "\";\n";
1724 /// EmitActionHandlersCallback - Emit code that handles actions. Used by
1725 /// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
1726 class EmitActionHandlersCallback : ActionHandlingCallbackBase {
1727 const OptionDescriptions& OptDescs;
1729 void processActionDag(const Init* Statement, unsigned IndentLevel,
1730 raw_ostream& O) const
1732 const DagInit& Dag = InitPtrToDag(Statement);
1733 const std::string& ActionName = GetOperatorName(Dag);
1735 if (ActionName == "append_cmd") {
1736 checkNumberOfArguments(&Dag, 1);
1737 const std::string& Cmd = InitPtrToString(Dag.getArg(0));
1739 llvm::SplitString(Cmd, Out);
1741 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1743 O.indent(IndentLevel) << "vec.push_back(\"" << *B << "\");\n";
1745 else if (ActionName == "error") {
1746 this->onErrorDag(Dag, IndentLevel, O);
1748 else if (ActionName == "warning") {
1749 this->onWarningDag(Dag, IndentLevel, O);
1751 else if (ActionName == "forward") {
1752 checkNumberOfArguments(&Dag, 1);
1753 const std::string& Name = InitPtrToString(Dag.getArg(0));
1754 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1755 IndentLevel, "", O);
1757 else if (ActionName == "forward_as") {
1758 checkNumberOfArguments(&Dag, 2);
1759 const std::string& Name = InitPtrToString(Dag.getArg(0));
1760 const std::string& NewName = InitPtrToString(Dag.getArg(1));
1761 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1762 IndentLevel, NewName, O);
1764 else if (ActionName == "output_suffix") {
1765 checkNumberOfArguments(&Dag, 1);
1766 const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
1767 O.indent(IndentLevel) << "output_suffix = \"" << OutSuf << "\";\n";
1769 else if (ActionName == "stop_compilation") {
1770 O.indent(IndentLevel) << "stop_compilation = true;\n";
1772 else if (ActionName == "unpack_values") {
1773 checkNumberOfArguments(&Dag, 1);
1774 const std::string& Name = InitPtrToString(Dag.getArg(0));
1775 const OptionDescription& D = OptDescs.FindOption(Name);
1778 throw std::string("Can't use unpack_values with multi-valued options!");
1781 O.indent(IndentLevel)
1782 << "for (" << D.GenTypeDeclaration()
1783 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1784 O.indent(IndentLevel)
1785 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n";
1786 O.indent(IndentLevel + Indent1)
1787 << "llvm::SplitString(*B, vec, \",\");\n";
1789 else if (D.isParameter()){
1790 O.indent(IndentLevel) << "llvm::SplitString("
1791 << D.GenVariableName() << ", vec, \",\");\n";
1794 throw "Option '" + D.Name +
1795 "': switches can't have the 'unpack_values' property!";
1799 throw "Unknown action name: " + ActionName + "!";
1803 EmitActionHandlersCallback(const OptionDescriptions& OD)
1806 void operator()(const Init* Statement,
1807 unsigned IndentLevel, raw_ostream& O) const
1809 this->processActionDag(Statement, IndentLevel, O);
1813 bool IsOutFileIndexCheckRequiredStr (const Init* CmdLine) {
1815 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1817 for (StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1819 if (*I == "$OUTFILE")
1826 class IsOutFileIndexCheckRequiredStrCallback {
1830 IsOutFileIndexCheckRequiredStrCallback(bool* ret) : ret_(ret)
1833 void operator()(const Init* CmdLine) {
1834 // Ignore nested 'case' DAG.
1835 if (typeid(*CmdLine) == typeid(DagInit))
1838 if (IsOutFileIndexCheckRequiredStr(CmdLine))
1842 void operator()(const DagInit* Test, unsigned, bool) {
1843 this->operator()(Test);
1845 void operator()(const Init* Statement, unsigned) {
1846 this->operator()(Statement);
1850 bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
1852 WalkCase(CmdLine, Id(), IsOutFileIndexCheckRequiredStrCallback(&ret));
1856 /// IsOutFileIndexCheckRequired - Should we emit an "out_file_index != -1" check
1857 /// in EmitGenerateActionMethod() ?
1858 bool IsOutFileIndexCheckRequired (Init* CmdLine) {
1859 if (typeid(*CmdLine) == typeid(StringInit))
1860 return IsOutFileIndexCheckRequiredStr(CmdLine);
1862 return IsOutFileIndexCheckRequiredCase(CmdLine);
1865 // EmitGenerateActionMethod - Emit either a normal or a "join" version of the
1866 // Tool::GenerateAction() method.
1867 void EmitGenerateActionMethod (const ToolDescription& D,
1868 const OptionDescriptions& OptDescs,
1869 bool IsJoin, raw_ostream& O) {
1871 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
1873 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
1875 O.indent(Indent2) << "bool HasChildren,\n";
1876 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1877 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1878 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1879 O.indent(Indent1) << "{\n";
1880 O.indent(Indent2) << "std::string cmd;\n";
1881 O.indent(Indent2) << "std::vector<std::string> vec;\n";
1882 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
1883 O.indent(Indent2) << "const char* output_suffix = \""
1884 << D.OutputSuffix << "\";\n";
1887 throw "Tool " + D.Name + " has no cmd_line property!";
1889 bool IndexCheckRequired = IsOutFileIndexCheckRequired(D.CmdLine);
1890 O.indent(Indent2) << "int out_file_index"
1891 << (IndexCheckRequired ? " = -1" : "")
1894 // Process the cmd_line property.
1895 if (typeid(*D.CmdLine) == typeid(StringInit))
1896 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
1898 EmitCaseConstructHandler(D.CmdLine, Indent2,
1899 EmitCmdLineVecFillCallback(IsJoin, D.Name),
1902 // For every understood option, emit handling code.
1904 EmitCaseConstructHandler(D.Actions, Indent2, EmitActionHandlersCallback(OptDescs),
1905 false, OptDescs, O);
1909 << "std::string out_file = OutFilename("
1910 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
1911 O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
1913 if (IndexCheckRequired)
1914 O.indent(Indent2) << "if (out_file_index != -1)\n";
1915 O.indent(IndexCheckRequired ? Indent3 : Indent2)
1916 << "vec[out_file_index] = out_file;\n";
1918 // Handle the Sink property.
1920 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
1921 O.indent(Indent3) << "vec.insert(vec.end(), "
1922 << SinkOptionName << ".begin(), " << SinkOptionName
1924 O.indent(Indent2) << "}\n";
1927 O.indent(Indent2) << "return Action(cmd, vec, stop_compilation, out_file);\n";
1928 O.indent(Indent1) << "}\n\n";
1931 /// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1932 /// a given Tool class.
1933 void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
1934 const OptionDescriptions& OptDescs,
1936 if (!ToolDesc.isJoin()) {
1937 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
1938 O.indent(Indent2) << "bool HasChildren,\n";
1939 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1940 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1941 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1942 O.indent(Indent1) << "{\n";
1943 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
1944 << " is not a Join tool!\");\n";
1945 O.indent(Indent1) << "}\n\n";
1948 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
1951 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
1954 /// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1955 /// methods for a given Tool class.
1956 void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
1957 O.indent(Indent1) << "const char** InputLanguages() const {\n";
1958 O.indent(Indent2) << "return InputLanguages_;\n";
1959 O.indent(Indent1) << "}\n\n";
1961 if (D.OutLanguage.empty())
1962 throw "Tool " + D.Name + " has no 'out_language' property!";
1964 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
1965 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
1966 O.indent(Indent1) << "}\n\n";
1969 /// EmitNameMethod - Emit the Name() method for a given Tool class.
1970 void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
1971 O.indent(Indent1) << "const char* Name() const {\n";
1972 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
1973 O.indent(Indent1) << "}\n\n";
1976 /// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1978 void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
1979 O.indent(Indent1) << "bool IsJoin() const {\n";
1981 O.indent(Indent2) << "return true;\n";
1983 O.indent(Indent2) << "return false;\n";
1984 O.indent(Indent1) << "}\n\n";
1987 /// EmitStaticMemberDefinitions - Emit static member definitions for a
1988 /// given Tool class.
1989 void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
1990 if (D.InLanguage.empty())
1991 throw "Tool " + D.Name + " has no 'in_language' property!";
1993 O << "const char* " << D.Name << "::InputLanguages_[] = {";
1994 for (StrVector::const_iterator B = D.InLanguage.begin(),
1995 E = D.InLanguage.end(); B != E; ++B)
1996 O << '\"' << *B << "\", ";
2000 /// EmitToolClassDefinition - Emit a Tool class definition.
2001 void EmitToolClassDefinition (const ToolDescription& D,
2002 const OptionDescriptions& OptDescs,
2004 if (D.Name == "root")
2008 O << "class " << D.Name << " : public ";
2014 O << "{\nprivate:\n";
2015 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
2018 EmitNameMethod(D, O);
2019 EmitInOutLanguageMethods(D, O);
2020 EmitIsJoinMethod(D, O);
2021 EmitGenerateActionMethods(D, OptDescs, O);
2023 // Close class definition
2026 EmitStaticMemberDefinitions(D, O);
2030 /// EmitOptionDefinitions - Iterate over a list of option descriptions
2031 /// and emit registration code.
2032 void EmitOptionDefinitions (const OptionDescriptions& descs,
2033 bool HasSink, bool HasExterns,
2036 std::vector<OptionDescription> Aliases;
2038 // Emit static cl::Option variables.
2039 for (OptionDescriptions::const_iterator B = descs.begin(),
2040 E = descs.end(); B!=E; ++B) {
2041 const OptionDescription& val = B->second;
2043 if (val.Type == OptionType::Alias) {
2044 Aliases.push_back(val);
2051 O << val.GenTypeDeclaration() << ' '
2052 << val.GenVariableName();
2054 if (val.isExtern()) {
2059 O << "(\"" << val.Name << "\"\n";
2061 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2062 O << ", cl::Prefix";
2064 if (val.isRequired()) {
2065 if (val.isList() && !val.isMultiVal())
2066 O << ", cl::OneOrMore";
2068 O << ", cl::Required";
2070 else if (val.isOneOrMore() && val.isList()) {
2071 O << ", cl::OneOrMore";
2073 else if (val.isZeroOrOne() && val.isList()) {
2074 O << ", cl::ZeroOrOne";
2077 if (val.isReallyHidden()) {
2078 O << ", cl::ReallyHidden";
2080 else if (val.isHidden()) {
2081 O << ", cl::Hidden";
2084 if (val.MultiVal > 1)
2085 O << ", cl::multi_val(" << val.MultiVal << ')';
2088 const std::string& str = val.InitVal->getAsString();
2089 O << ", cl::init(" << str << ')';
2092 if (!val.Help.empty())
2093 O << ", cl::desc(\"" << val.Help << "\")";
2098 // Emit the aliases (they should go after all the 'proper' options).
2099 for (std::vector<OptionDescription>::const_iterator
2100 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
2101 const OptionDescription& val = *B;
2103 O << val.GenTypeDeclaration() << ' '
2104 << val.GenVariableName()
2105 << "(\"" << val.Name << '\"';
2107 const OptionDescription& D = descs.FindOption(val.Help);
2108 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
2110 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2113 // Emit the sink option.
2115 O << (HasExterns ? "extern cl" : "cl")
2116 << "::list<std::string> " << SinkOptionName
2117 << (HasExterns ? ";\n" : "(cl::Sink);\n");
2122 /// EmitPreprocessOptionsCallback - Helper function passed to
2123 /// EmitCaseConstructHandler() by EmitPreprocessOptions().
2124 class EmitPreprocessOptionsCallback : ActionHandlingCallbackBase {
2125 const OptionDescriptions& OptDescs_;
2127 void onUnsetOption(Init* i, unsigned IndentLevel, raw_ostream& O) {
2128 const std::string& OptName = InitPtrToString(i);
2129 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2131 if (OptDesc.isSwitch()) {
2132 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2134 else if (OptDesc.isParameter()) {
2135 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2137 else if (OptDesc.isList()) {
2138 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2141 throw "Can't apply 'unset_option' to alias option '" + OptName + "'";
2145 void processDag(const Init* I, unsigned IndentLevel, raw_ostream& O)
2147 const DagInit& d = InitPtrToDag(I);
2148 const std::string& OpName = GetOperatorName(d);
2150 if (OpName == "warning") {
2151 this->onWarningDag(d, IndentLevel, O);
2153 else if (OpName == "error") {
2154 this->onWarningDag(d, IndentLevel, O);
2156 else if (OpName == "unset_option") {
2157 checkNumberOfArguments(&d, 1);
2158 Init* I = d.getArg(0);
2159 if (typeid(*I) == typeid(ListInit)) {
2160 const ListInit& DagList = *static_cast<const ListInit*>(I);
2161 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
2163 this->onUnsetOption(*B, IndentLevel, O);
2166 this->onUnsetOption(I, IndentLevel, O);
2170 throw "Unknown operator in the option preprocessor: '" + OpName + "'!"
2171 "\nOnly 'warning', 'error' and 'unset_option' are allowed.";
2177 void operator()(const Init* I, unsigned IndentLevel, raw_ostream& O) {
2178 this->processDag(I, IndentLevel, O);
2181 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
2182 : OptDescs_(OptDescs)
2186 /// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2187 void EmitPreprocessOptions (const RecordKeeper& Records,
2188 const OptionDescriptions& OptDecs, raw_ostream& O)
2190 O << "void PreprocessOptionsLocal() {\n";
2192 const RecordVector& OptionPreprocessors =
2193 Records.getAllDerivedDefinitions("OptionPreprocessor");
2195 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2196 E = OptionPreprocessors.end(); B!=E; ++B) {
2197 DagInit* Case = (*B)->getValueAsDag("preprocessor");
2198 EmitCaseConstructHandler(Case, Indent1,
2199 EmitPreprocessOptionsCallback(OptDecs),
2206 /// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
2207 void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
2209 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
2211 // Get the relevant field out of RecordKeeper
2212 const Record* LangMapRecord = Records.getDef("LanguageMap");
2214 // It is allowed for a plugin to have no language map.
2215 if (LangMapRecord) {
2217 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2218 if (!LangsToSuffixesList)
2219 throw std::string("Error in the language map definition!");
2221 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
2222 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
2224 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2225 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2227 for (unsigned i = 0; i < Suffixes->size(); ++i)
2228 O.indent(Indent1) << "langMap[\""
2229 << InitPtrToString(Suffixes->getElement(i))
2230 << "\"] = \"" << Lang << "\";\n";
2237 /// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2238 /// by EmitEdgeClass().
2239 void IncDecWeight (const Init* i, unsigned IndentLevel,
2241 const DagInit& d = InitPtrToDag(i);
2242 const std::string& OpName = GetOperatorName(d);
2244 if (OpName == "inc_weight") {
2245 O.indent(IndentLevel) << "ret += ";
2247 else if (OpName == "dec_weight") {
2248 O.indent(IndentLevel) << "ret -= ";
2250 else if (OpName == "error") {
2251 checkNumberOfArguments(&d, 1);
2252 O.indent(IndentLevel) << "throw std::runtime_error(\""
2253 << InitPtrToString(d.getArg(0))
2258 throw "Unknown operator in edge properties list: '" + OpName + "'!"
2259 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
2262 if (d.getNumArgs() > 0)
2263 O << InitPtrToInt(d.getArg(0)) << ";\n";
2269 /// EmitEdgeClass - Emit a single Edge# class.
2270 void EmitEdgeClass (unsigned N, const std::string& Target,
2271 DagInit* Case, const OptionDescriptions& OptDescs,
2274 // Class constructor.
2275 O << "class Edge" << N << ": public Edge {\n"
2277 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2280 // Function Weight().
2282 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2283 O.indent(Indent2) << "unsigned ret = 0;\n";
2285 // Handle the 'case' construct.
2286 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
2288 O.indent(Indent2) << "return ret;\n";
2289 O.indent(Indent1) << "};\n\n};\n\n";
2292 /// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
2293 void EmitEdgeClasses (const RecordVector& EdgeVector,
2294 const OptionDescriptions& OptDescs,
2297 for (RecordVector::const_iterator B = EdgeVector.begin(),
2298 E = EdgeVector.end(); B != E; ++B) {
2299 const Record* Edge = *B;
2300 const std::string& NodeB = Edge->getValueAsString("b");
2301 DagInit* Weight = Edge->getValueAsDag("weight");
2303 if (!isDagEmpty(Weight))
2304 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
2309 /// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
2311 void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
2312 const ToolDescriptions& ToolDescs,
2315 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
2317 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2318 E = ToolDescs.end(); B != E; ++B)
2319 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
2326 for (RecordVector::const_iterator B = EdgeVector.begin(),
2327 E = EdgeVector.end(); B != E; ++B) {
2328 const Record* Edge = *B;
2329 const std::string& NodeA = Edge->getValueAsString("a");
2330 const std::string& NodeB = Edge->getValueAsString("b");
2331 DagInit* Weight = Edge->getValueAsDag("weight");
2333 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
2335 if (isDagEmpty(Weight))
2336 O << "new SimpleEdge(\"" << NodeB << "\")";
2338 O << "new Edge" << i << "()";
2347 /// ExtractHookNames - Extract the hook names from all instances of
2348 /// $CALL(HookName) in the provided command line string. Helper
2349 /// function used by FillInHookNames().
2350 class ExtractHookNames {
2351 llvm::StringMap<unsigned>& HookNames_;
2353 ExtractHookNames(llvm::StringMap<unsigned>& HookNames)
2354 : HookNames_(HookNames) {}
2356 void operator()(const Init* CmdLine) {
2359 // Ignore nested 'case' DAG.
2360 if (typeid(*CmdLine) == typeid(DagInit))
2363 TokenizeCmdline(InitPtrToString(CmdLine), cmds);
2364 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2366 const std::string& cmd = *B;
2368 if (cmd == "$CALL") {
2369 unsigned NumArgs = 0;
2370 checkedIncrement(B, E, "Syntax error in $CALL invocation!");
2371 const std::string& HookName = *B;
2374 if (HookName.at(0) == ')')
2375 throw "$CALL invoked with no arguments!";
2377 while (++B != E && B->at(0) != ')') {
2381 StringMap<unsigned>::const_iterator H = HookNames_.find(HookName);
2383 if (H != HookNames_.end() && H->second != NumArgs)
2384 throw "Overloading of hooks is not allowed. Overloaded hook: "
2387 HookNames_[HookName] = NumArgs;
2393 void operator()(const DagInit* Test, unsigned, bool) {
2394 this->operator()(Test);
2396 void operator()(const Init* Statement, unsigned) {
2397 this->operator()(Statement);
2401 /// FillInHookNames - Actually extract the hook names from all command
2402 /// line strings. Helper function used by EmitHookDeclarations().
2403 void FillInHookNames(const ToolDescriptions& ToolDescs,
2404 llvm::StringMap<unsigned>& HookNames)
2406 // For all command lines:
2407 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2408 E = ToolDescs.end(); B != E; ++B) {
2409 const ToolDescription& D = *(*B);
2412 if (dynamic_cast<StringInit*>(D.CmdLine))
2413 // This is a string.
2414 ExtractHookNames(HookNames).operator()(D.CmdLine);
2416 // This is a 'case' construct.
2417 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames));
2421 /// EmitHookDeclarations - Parse CmdLine fields of all the tool
2422 /// property records and emit hook function declaration for each
2423 /// instance of $CALL(HookName).
2424 void EmitHookDeclarations(const ToolDescriptions& ToolDescs, raw_ostream& O) {
2425 llvm::StringMap<unsigned> HookNames;
2427 FillInHookNames(ToolDescs, HookNames);
2428 if (HookNames.empty())
2431 O << "namespace hooks {\n";
2432 for (StringMap<unsigned>::const_iterator B = HookNames.begin(),
2433 E = HookNames.end(); B != E; ++B) {
2434 O.indent(Indent1) << "std::string " << B->first() << "(";
2436 for (unsigned i = 0, j = B->second; i < j; ++i) {
2437 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2445 /// EmitRegisterPlugin - Emit code to register this plugin.
2446 void EmitRegisterPlugin(int Priority, raw_ostream& O) {
2447 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2448 O.indent(Indent1) << "int Priority() const { return "
2449 << Priority << "; }\n\n";
2450 O.indent(Indent1) << "void PreprocessOptions() const\n";
2451 O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
2452 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2453 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2455 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2456 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2458 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
2461 /// EmitIncludes - Emit necessary #include directives and some
2462 /// additional declarations.
2463 void EmitIncludes(raw_ostream& O) {
2464 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2465 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
2466 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
2467 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2468 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
2470 << "#include \"llvm/ADT/StringExtras.h\"\n"
2471 << "#include \"llvm/Support/CommandLine.h\"\n"
2472 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
2474 << "#include <cstdlib>\n"
2475 << "#include <stdexcept>\n\n"
2477 << "using namespace llvm;\n"
2478 << "using namespace llvmc;\n\n"
2480 << "extern cl::opt<std::string> OutputFilename;\n\n"
2482 << "inline const char* checkCString(const char* s)\n"
2483 << "{ return s == NULL ? \"\" : s; }\n\n";
2487 /// PluginData - Holds all information about a plugin.
2489 OptionDescriptions OptDescs;
2492 ToolDescriptions ToolDescs;
2497 /// HasSink - Go through the list of tool descriptions and check if
2498 /// there are any with the 'sink' property set.
2499 bool HasSink(const ToolDescriptions& ToolDescs) {
2500 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2501 E = ToolDescs.end(); B != E; ++B)
2508 /// HasExterns - Go through the list of option descriptions and check
2509 /// if there are any external options.
2510 bool HasExterns(const OptionDescriptions& OptDescs) {
2511 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2512 E = OptDescs.end(); B != E; ++B)
2513 if (B->second.isExtern())
2519 /// CollectPluginData - Collect tool and option properties,
2520 /// compilation graph edges and plugin priority from the parse tree.
2521 void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2522 // Collect option properties.
2523 const RecordVector& OptionLists =
2524 Records.getAllDerivedDefinitions("OptionList");
2525 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2528 // Collect tool properties.
2529 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2530 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2531 Data.HasSink = HasSink(Data.ToolDescs);
2532 Data.HasExterns = HasExterns(Data.OptDescs);
2534 // Collect compilation graph edges.
2535 const RecordVector& CompilationGraphs =
2536 Records.getAllDerivedDefinitions("CompilationGraph");
2537 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2540 // Calculate the priority of this plugin.
2541 const RecordVector& Priorities =
2542 Records.getAllDerivedDefinitions("PluginPriority");
2543 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
2546 /// CheckPluginData - Perform some sanity checks on the collected data.
2547 void CheckPluginData(PluginData& Data) {
2548 // Filter out all tools not mentioned in the compilation graph.
2549 FilterNotInGraph(Data.Edges, Data.ToolDescs);
2551 // Typecheck the compilation graph.
2552 TypecheckGraph(Data.Edges, Data.ToolDescs);
2554 // Check that there are no options without side effects (specified
2555 // only in the OptionList).
2556 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2560 void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
2561 // Emit file header.
2564 // Emit global option registration code.
2565 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
2567 // Emit hook declarations.
2568 EmitHookDeclarations(Data.ToolDescs, O);
2570 O << "namespace {\n\n";
2572 // Emit PreprocessOptionsLocal() function.
2573 EmitPreprocessOptions(Records, Data.OptDescs, O);
2575 // Emit PopulateLanguageMapLocal() function
2576 // (language map maps from file extensions to language names).
2577 EmitPopulateLanguageMap(Records, O);
2579 // Emit Tool classes.
2580 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2581 E = Data.ToolDescs.end(); B!=E; ++B)
2582 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2584 // Emit Edge# classes.
2585 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2587 // Emit PopulateCompilationGraphLocal() function.
2588 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2590 // Emit code for plugin registration.
2591 EmitRegisterPlugin(Data.Priority, O);
2593 O << "} // End anonymous namespace.\n\n";
2595 // Force linkage magic.
2596 O << "namespace llvmc {\n";
2597 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2604 // End of anonymous namespace
2607 /// run - The back-end entry point.
2608 void LLVMCConfigurationEmitter::run (raw_ostream &O) {
2612 CollectPluginData(Records, Data);
2613 CheckPluginData(Data);
2615 EmitSourceFileHeader("LLVMC Configuration Library", O);
2616 EmitPluginCode(Data, O);
2618 } catch (std::exception& Error) {
2619 throw Error.what() + std::string(" - usually this means a syntax error.");