Refactoring, no functionality change.
[oota-llvm.git] / utils / TableGen / LLVMCConfigurationEmitter.cpp
index 2800678f8c5bbe95b9a96af79a58a62d7ee539a8..be3382c29d5cbd27aba7acd4323fb221b7336cf5 100644 (file)
@@ -15,8 +15,6 @@
 #include "Record.h"
 
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
-#include "llvm/ADT/SmallVector.h"
-#include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringSet.h"
 #include <algorithm>
@@ -40,24 +38,30 @@ typedef std::vector<std::string> StrVector;
 /// Constants
 
 // Indentation.
-unsigned TabWidth = 4;
-unsigned Indent1  = TabWidth*1;
-unsigned Indent2  = TabWidth*2;
-unsigned Indent3  = TabWidth*3;
+const unsigned TabWidth = 4;
+const unsigned Indent1  = TabWidth*1;
+const unsigned Indent2  = TabWidth*2;
+const unsigned Indent3  = TabWidth*3;
 
 // Default help string.
-const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
+const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
 
 // Name for the "sink" option.
-const char * SinkOptionName = "AutoGeneratedSinkOption";
+const char * const SinkOptionName = "AutoGeneratedSinkOption";
 
 //===----------------------------------------------------------------------===//
 /// Helper functions
 
 /// Id - An 'identity' function object.
 struct Id {
-  template<typename T>
-  void operator()(const T&) const {
+  template<typename T0>
+  void operator()(const T0&) const {
+  }
+  template<typename T0, typename T1>
+  void operator()(const T0&, const T1&) const {
+  }
+  template<typename T0, typename T1, typename T2>
+  void operator()(const T0&, const T1&, const T2&) const {
   }
 };
 
@@ -81,16 +85,20 @@ const DagInit& InitPtrToDag(const Init* ptr) {
   return val;
 }
 
+const std::string GetOperatorName(const DagInit& D) {
+  return D.getOperator()->getAsString();
+}
+
 // checkNumberOfArguments - Ensure that the number of args in d is
 // greater than or equal to min_arguments, otherwise throw an exception.
-void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
-  if (!d || d->getNumArgs() < min_arguments)
-    throw d->getOperator()->getAsString() + ": too few arguments!";
+void checkNumberOfArguments (const DagInit& d, unsigned minArgs) {
+  if (d.getNumArgs() < minArgs)
+    throw GetOperatorName(d) + ": too few arguments!";
 }
 
 // isDagEmpty - is this DAG marked with an empty marker?
-bool isDagEmpty (const DagInit* d) {
-  return d->getOperator()->getAsString() == "empty_dag_marker";
+bool isDagEmpty (const DagInit& d) {
+  return GetOperatorName(d) == "empty_dag_marker";
 }
 
 // EscapeVariableName - Escape commas and other symbols not allowed
@@ -132,6 +140,18 @@ void checkedIncrement(I& P, I E, S ErrorString) {
     throw ErrorString;
 }
 
+// apply is needed because C++'s syntax doesn't let us construct a function
+// object and call it in the same statement.
+template<typename F, typename T0>
+void apply(F Fun, T0& Arg0) {
+  return Fun(Arg0);
+}
+
+template<typename F, typename T0, typename T1>
+void apply(F Fun, T0& Arg0, T1& Arg1) {
+  return Fun(Arg0, Arg1);
+}
+
 //===----------------------------------------------------------------------===//
 /// Back-end specific code
 
@@ -143,6 +163,10 @@ namespace OptionType {
   enum OptionType { Alias, Switch, Parameter, ParameterList,
                     Prefix, PrefixList};
 
+  bool IsAlias(OptionType t) {
+    return (t == Alias);
+  }
+
   bool IsList (OptionType t) {
     return (t == ParameterList || t == PrefixList);
   }
@@ -177,7 +201,8 @@ OptionType::OptionType stringToOptionType(const std::string& T) {
 namespace OptionDescriptionFlags {
   enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
                                 ReallyHidden = 0x4, Extern = 0x8,
-                                OneOrMore = 0x10, ZeroOrOne = 0x20 };
+                                OneOrMore = 0x10, Optional = 0x20,
+                                CommaSeparated = 0x40 };
 }
 
 /// OptionDescription - Represents data contained in a single
@@ -213,6 +238,9 @@ struct OptionDescription {
 
   bool isMultiVal() const;
 
+  bool isCommaSeparated() const;
+  void setCommaSeparated();
+
   bool isExtern() const;
   void setExtern();
 
@@ -222,8 +250,8 @@ struct OptionDescription {
   bool isOneOrMore() const;
   void setOneOrMore();
 
-  bool isZeroOrOne() const;
-  void setZeroOrOne();
+  bool isOptional() const;
+  void setOptional();
 
   bool isHidden() const;
   void setHidden();
@@ -231,12 +259,12 @@ struct OptionDescription {
   bool isReallyHidden() const;
   void setReallyHidden();
 
-  bool isParameter() const
-  { return OptionType::IsParameter(this->Type); }
-
   bool isSwitch() const
   { return OptionType::IsSwitch(this->Type); }
 
+  bool isParameter() const
+  { return OptionType::IsParameter(this->Type); }
+
   bool isList() const
   { return OptionType::IsList(this->Type); }
 
@@ -258,13 +286,20 @@ void OptionDescription::Merge (const OptionDescription& other)
 }
 
 bool OptionDescription::isAlias() const {
-  return Type == OptionType::Alias;
+  return OptionType::IsAlias(this->Type);
 }
 
 bool OptionDescription::isMultiVal() const {
   return MultiVal > 1;
 }
 
+bool OptionDescription::isCommaSeparated() const {
+  return Flags & OptionDescriptionFlags::CommaSeparated;
+}
+void OptionDescription::setCommaSeparated() {
+  Flags |= OptionDescriptionFlags::CommaSeparated;
+}
+
 bool OptionDescription::isExtern() const {
   return Flags & OptionDescriptionFlags::Extern;
 }
@@ -286,11 +321,11 @@ void OptionDescription::setOneOrMore() {
   Flags |= OptionDescriptionFlags::OneOrMore;
 }
 
-bool OptionDescription::isZeroOrOne() const {
-  return Flags & OptionDescriptionFlags::ZeroOrOne;
+bool OptionDescription::isOptional() const {
+  return Flags & OptionDescriptionFlags::Optional;
 }
-void OptionDescription::setZeroOrOne() {
-  Flags |= OptionDescriptionFlags::ZeroOrOne;
+void OptionDescription::setOptional() {
+  Flags |= OptionDescriptionFlags::Optional;
 }
 
 bool OptionDescription::isHidden() const {
@@ -352,6 +387,14 @@ public:
   /// FindOption - exception-throwing wrapper for find().
   const OptionDescription& FindOption(const std::string& OptName) const;
 
+  // Wrappers for FindOption that throw an exception in case the option has a
+  // wrong type.
+  const OptionDescription& FindSwitch(const std::string& OptName) const;
+  const OptionDescription& FindParameter(const std::string& OptName) const;
+  const OptionDescription& FindList(const std::string& OptName) const;
+  const OptionDescription&
+  FindListOrParameter(const std::string& OptName) const;
+
   /// insertDescription - Insert new OptionDescription into
   /// OptionDescriptions list
   void InsertDescription (const OptionDescription& o);
@@ -363,8 +406,7 @@ public:
 };
 
 const OptionDescription&
-OptionDescriptions::FindOption(const std::string& OptName) const
-{
+OptionDescriptions::FindOption(const std::string& OptName) const {
   const_iterator I = Descriptions.find(OptName);
   if (I != Descriptions.end())
     return I->second;
@@ -372,8 +414,40 @@ OptionDescriptions::FindOption(const std::string& OptName) const
     throw OptName + ": no such option!";
 }
 
-void OptionDescriptions::InsertDescription (const OptionDescription& o)
-{
+const OptionDescription&
+OptionDescriptions::FindSwitch(const std::string& OptName) const {
+  const OptionDescription& OptDesc = this->FindOption(OptName);
+  if (!OptDesc.isSwitch())
+    throw OptName + ": incorrect option type - should be a switch!";
+  return OptDesc;
+}
+
+const OptionDescription&
+OptionDescriptions::FindList(const std::string& OptName) const {
+  const OptionDescription& OptDesc = this->FindOption(OptName);
+  if (!OptDesc.isList())
+    throw OptName + ": incorrect option type - should be a list!";
+  return OptDesc;
+}
+
+const OptionDescription&
+OptionDescriptions::FindParameter(const std::string& OptName) const {
+  const OptionDescription& OptDesc = this->FindOption(OptName);
+  if (!OptDesc.isParameter())
+    throw OptName + ": incorrect option type - should be a parameter!";
+  return OptDesc;
+}
+
+const OptionDescription&
+OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
+  const OptionDescription& OptDesc = this->FindOption(OptName);
+  if (!OptDesc.isList() && !OptDesc.isParameter())
+    throw OptName
+      + ": incorrect option type - should be a list or parameter!";
+  return OptDesc;
+}
+
+void OptionDescriptions::InsertDescription (const OptionDescription& o) {
   container_type::iterator I = Descriptions.find(o.Name);
   if (I != Descriptions.end()) {
     OptionDescription& D = I->second;
@@ -386,54 +460,83 @@ void OptionDescriptions::InsertDescription (const OptionDescription& o)
 
 /// HandlerTable - A base class for function objects implemented as
 /// 'tables of handlers'.
-template <class T>
+template <typename Handler>
 class HandlerTable {
 protected:
   // Implementation details.
 
-  /// Handler -
-  typedef void (T::* Handler) (const DagInit*);
   /// HandlerMap - A map from property names to property handlers
   typedef StringMap<Handler> HandlerMap;
 
   static HandlerMap Handlers_;
   static bool staticMembersInitialized_;
 
-  T* childPtr;
 public:
 
-  HandlerTable(T* cp) : childPtr(cp)
-  {}
-
-  /// operator() - Just forwards to the corresponding property
-  /// handler.
-  void operator() (Init* i) {
-    const DagInit& property = InitPtrToDag(i);
-    const std::string& property_name = property.getOperator()->getAsString();
-    typename HandlerMap::iterator method = Handlers_.find(property_name);
+  Handler GetHandler (const std::string& HandlerName) const {
+    typename HandlerMap::iterator method = Handlers_.find(HandlerName);
 
     if (method != Handlers_.end()) {
       Handler h = method->second;
-      (childPtr->*h)(&property);
+      return h;
     }
     else {
-      throw "No handler found for property " + property_name + "!";
+      throw "No handler found for property " + HandlerName + "!";
     }
   }
 
-  void AddHandler(const char* Property, Handler Handl) {
-    Handlers_[Property] = Handl;
+  void AddHandler(const char* Property, Handler H) {
+    Handlers_[Property] = H;
   }
+
 };
 
-template <class T> typename HandlerTable<T>::HandlerMap
-HandlerTable<T>::Handlers_;
-template <class T> bool HandlerTable<T>::staticMembersInitialized_ = false;
+template <class Handler, class FunctionObject>
+Handler GetHandler(FunctionObject* Obj, const DagInit& Dag) {
+  const std::string& HandlerName = GetOperatorName(Dag);
+  return Obj->GetHandler(HandlerName);
+}
+
+template <class FunctionObject>
+void InvokeDagInitHandler(FunctionObject* Obj, Init* I) {
+  typedef void (FunctionObject::*Handler) (const DagInit&);
+
+  const DagInit& Dag = InitPtrToDag(I);
+  Handler h = GetHandler<Handler>(Obj, Dag);
+
+  ((Obj)->*(h))(Dag);
+}
+
+template <class FunctionObject>
+void InvokeDagInitHandler(const FunctionObject* const Obj,
+                          const Init* I, unsigned IndentLevel, raw_ostream& O)
+{
+  typedef void (FunctionObject::*Handler)
+    (const DagInit&, unsigned IndentLevel, raw_ostream& O) const;
+
+  const DagInit& Dag = InitPtrToDag(I);
+  Handler h = GetHandler<Handler>(Obj, Dag);
+
+  ((Obj)->*(h))(Dag, IndentLevel, O);
+}
+
+
+template <typename H>
+typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
+
+template <typename H>
+bool HandlerTable<H>::staticMembersInitialized_ = false;
 
 
 /// CollectOptionProperties - Function object for iterating over an
 /// option property list.
-class CollectOptionProperties : public HandlerTable<CollectOptionProperties> {
+class CollectOptionProperties;
+typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
+(const DagInit&);
+
+class CollectOptionProperties
+: public HandlerTable<CollectOptionPropertiesHandler>
+{
 private:
 
   /// optDescs_ - OptionDescriptions table. This is where the
@@ -443,7 +546,7 @@ private:
 public:
 
   explicit CollectOptionProperties(OptionDescription& OD)
-    : HandlerTable<CollectOptionProperties>(this), optDesc_(OD)
+    : optDesc_(OD)
   {
     if (!staticMembersInitialized_) {
       AddHandler("extern", &CollectOptionProperties::onExtern);
@@ -454,90 +557,103 @@ public:
       AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
       AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
       AddHandler("required", &CollectOptionProperties::onRequired);
-      AddHandler("zero_or_one", &CollectOptionProperties::onZeroOrOne);
+      AddHandler("optional", &CollectOptionProperties::onOptional);
+      AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
 
       staticMembersInitialized_ = true;
     }
   }
 
+  /// operator() - Just forwards to the corresponding property
+  /// handler.
+  void operator() (Init* I) {
+    InvokeDagInitHandler(this, I);
+  }
+
 private:
 
   /// Option property handlers --
   /// Methods that handle option properties such as (help) or (hidden).
 
-  void onExtern (const DagInit* d) {
+  void onExtern (const DagInit& d) {
     checkNumberOfArguments(d, 0);
     optDesc_.setExtern();
   }
 
-  void onHelp (const DagInit* d) {
+  void onHelp (const DagInit& d) {
     checkNumberOfArguments(d, 1);
-    optDesc_.Help = InitPtrToString(d->getArg(0));
+    optDesc_.Help = InitPtrToString(d.getArg(0));
   }
 
-  void onHidden (const DagInit* d) {
+  void onHidden (const DagInit& d) {
     checkNumberOfArguments(d, 0);
     optDesc_.setHidden();
   }
 
-  void onReallyHidden (const DagInit* d) {
+  void onReallyHidden (const DagInit& d) {
     checkNumberOfArguments(d, 0);
     optDesc_.setReallyHidden();
   }
 
-  void onRequired (const DagInit* d) {
+  void onCommaSeparated (const DagInit& d) {
+    checkNumberOfArguments(d, 0);
+    if (!optDesc_.isList())
+      throw "'comma_separated' is valid only on list options!";
+    optDesc_.setCommaSeparated();
+  }
+
+  void onRequired (const DagInit& d) {
     checkNumberOfArguments(d, 0);
-    if (optDesc_.isOneOrMore())
-      throw std::string("An option can't have both (required) "
-                        "and (one_or_more) properties!");
+    if (optDesc_.isOneOrMore() || optDesc_.isOptional())
+      throw "Only one of (required), (optional) or "
+        "(one_or_more) properties is allowed!";
     optDesc_.setRequired();
   }
 
-  void onInit (const DagInit* d) {
+  void onInit (const DagInit& d) {
     checkNumberOfArguments(d, 1);
-    Init* i = d->getArg(0);
+    Init* i = d.getArg(0);
     const std::string& str = i->getAsString();
 
     bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
     correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
 
     if (!correct)
-      throw std::string("Incorrect usage of the 'init' option property!");
+      throw "Incorrect usage of the 'init' option property!";
 
     optDesc_.InitVal = i;
   }
 
-  void onOneOrMore (const DagInit* d) {
+  void onOneOrMore (const DagInit& d) {
     checkNumberOfArguments(d, 0);
-    if (optDesc_.isRequired() || optDesc_.isZeroOrOne())
-      throw std::string("Only one of (required), (zero_or_one) or "
-                        "(one_or_more) properties is allowed!");
+    if (optDesc_.isRequired() || optDesc_.isOptional())
+      throw "Only one of (required), (optional) or "
+        "(one_or_more) properties is allowed!";
     if (!OptionType::IsList(optDesc_.Type))
       llvm::errs() << "Warning: specifying the 'one_or_more' property "
         "on a non-list option will have no effect.\n";
     optDesc_.setOneOrMore();
   }
 
-  void onZeroOrOne (const DagInit* d) {
+  void onOptional (const DagInit& d) {
     checkNumberOfArguments(d, 0);
     if (optDesc_.isRequired() || optDesc_.isOneOrMore())
-      throw std::string("Only one of (required), (zero_or_one) or "
-                        "(one_or_more) properties is allowed!");
+      throw "Only one of (required), (optional) or "
+        "(one_or_more) properties is allowed!";
     if (!OptionType::IsList(optDesc_.Type))
-      llvm::errs() << "Warning: specifying the 'zero_or_one' property"
+      llvm::errs() << "Warning: specifying the 'optional' property"
         "on a non-list option will have no effect.\n";
-    optDesc_.setZeroOrOne();
+    optDesc_.setOptional();
   }
 
-  void onMultiVal (const DagInit* d) {
+  void onMultiVal (const DagInit& d) {
     checkNumberOfArguments(d, 1);
-    int val = InitPtrToInt(d->getArg(0));
+    int val = InitPtrToInt(d.getArg(0));
     if (val < 2)
-      throw std::string("Error in the 'multi_val' property: "
-                        "the value must be greater than 1!");
+      throw "Error in the 'multi_val' property: "
+        "the value must be greater than 1!";
     if (!OptionType::IsList(optDesc_.Type))
-      throw std::string("The multi_val property is valid only "
-                        "on list options!");
+      throw "The multi_val property is valid only on list options!";
     optDesc_.MultiVal = val;
   }
 
@@ -555,23 +671,23 @@ public:
 
   void operator()(const Init* i) {
     const DagInit& d = InitPtrToDag(i);
-    checkNumberOfArguments(&d, 1);
+    checkNumberOfArguments(d, 1);
 
     const OptionType::OptionType Type =
-      stringToOptionType(d.getOperator()->getAsString());
+      stringToOptionType(GetOperatorName(d));
     const std::string& Name = InitPtrToString(d.getArg(0));
 
     OptionDescription OD(Type, Name);
 
     if (!OD.isExtern())
-      checkNumberOfArguments(&d, 2);
+      checkNumberOfArguments(d, 2);
 
     if (OD.isAlias()) {
       // Aliases store the aliased option name in the 'Help' field.
       OD.Help = InitPtrToString(d.getArg(1));
     }
     else if (!OD.isExtern()) {
-      processOptionProperties(&d, OD);
+      processOptionProperties(d, OD);
     }
     OptDescs_.InsertDescription(OD);
   }
@@ -579,12 +695,12 @@ public:
 private:
   /// processOptionProperties - Go through the list of option
   /// properties and call a corresponding handler for each.
-  static void processOptionProperties (const DagInit* d, OptionDescription& o) {
+  static void processOptionProperties (const DagInit& d, OptionDescription& o) {
     checkNumberOfArguments(d, 2);
-    DagInit::const_arg_iterator B = d->arg_begin();
+    DagInit::const_arg_iterator B = d.arg_begin();
     // Skip the first argument: it's always the option name.
     ++B;
-    std::for_each(B, d->arg_end(), CollectOptionProperties(o));
+    std::for_each(B, d.arg_end(), CollectOptionProperties(o));
   }
 
 };
@@ -642,7 +758,13 @@ typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
 
 /// CollectToolProperties - Function object for iterating over a list of
 /// tool property records.
-class CollectToolProperties : public HandlerTable<CollectToolProperties> {
+
+class CollectToolProperties;
+typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
+(const DagInit&);
+
+class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
+{
 private:
 
   /// toolDesc_ - Properties of the current Tool. This is where the
@@ -652,7 +774,7 @@ private:
 public:
 
   explicit CollectToolProperties (ToolDescription& d)
-    : HandlerTable<CollectToolProperties>(this) , toolDesc_(d)
+    : toolDesc_(d)
   {
     if (!staticMembersInitialized_) {
 
@@ -668,30 +790,33 @@ public:
     }
   }
 
+  void operator() (Init* I) {
+    InvokeDagInitHandler(this, I);
+  }
+
 private:
 
   /// Property handlers --
   /// Functions that extract information about tool properties from
   /// DAG representation.
 
-  void onActions (const DagInit* d) {
+  void onActions (const DagInit& d) {
     checkNumberOfArguments(d, 1);
-    Init* Case = d->getArg(0);
+    Init* Case = d.getArg(0);
     if (typeid(*Case) != typeid(DagInit) ||
-        static_cast<DagInit*>(Case)->getOperator()->getAsString() != "case")
-      throw
-        std::string("The argument to (actions) should be a 'case' construct!");
+        GetOperatorName(static_cast<DagInit&>(*Case)) != "case")
+      throw "The argument to (actions) should be a 'case' construct!";
     toolDesc_.Actions = Case;
   }
 
-  void onCmdLine (const DagInit* d) {
+  void onCmdLine (const DagInit& d) {
     checkNumberOfArguments(d, 1);
-    toolDesc_.CmdLine = d->getArg(0);
+    toolDesc_.CmdLine = d.getArg(0);
   }
 
-  void onInLanguage (const DagInit* d) {
+  void onInLanguage (const DagInit& d) {
     checkNumberOfArguments(d, 1);
-    Init* arg = d->getArg(0);
+    Init* arg = d.getArg(0);
 
     // Find out the argument's type.
     if (typeid(*arg) == typeid(StringInit)) {
@@ -716,22 +841,22 @@ private:
     }
   }
 
-  void onJoin (const DagInit* d) {
+  void onJoin (const DagInit& d) {
     checkNumberOfArguments(d, 0);
     toolDesc_.setJoin();
   }
 
-  void onOutLanguage (const DagInit* d) {
+  void onOutLanguage (const DagInit& d) {
     checkNumberOfArguments(d, 1);
-    toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
+    toolDesc_.OutLanguage = InitPtrToString(d.getArg(0));
   }
 
-  void onOutputSuffix (const DagInit* d) {
+  void onOutputSuffix (const DagInit& d) {
     checkNumberOfArguments(d, 1);
-    toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
+    toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
   }
 
-  void onSink (const DagInit* d) {
+  void onSink (const DagInit& d) {
     checkNumberOfArguments(d, 0);
     toolDesc_.setSink();
   }
@@ -775,11 +900,17 @@ void FillInEdgeVector(RecordVector::const_iterator B,
 /// CalculatePriority - Calculate the priority of this plugin.
 int CalculatePriority(RecordVector::const_iterator B,
                       RecordVector::const_iterator E) {
-  int total = 0;
-  for (; B!=E; ++B) {
-    total += static_cast<int>((*B)->getValueAsInt("priority"));
+  int priority = 0;
+
+  if (B != E) {
+    priority  = static_cast<int>((*B)->getValueAsInt("priority"));
+
+    if (++B != E)
+      throw "More than one 'PluginPriority' instance found: "
+        "most probably an error!";
   }
-  return total;
+
+  return priority;
 }
 
 /// NotInGraph - Helper function object for FilterNotInGraph.
@@ -867,29 +998,67 @@ void TypecheckGraph (const RecordVector& EdgeVector,
     }
 
     if (NodeB == "root")
-      throw std::string("Edges back to the root are not allowed!");
+      throw "Edges back to the root are not allowed!";
   }
 }
 
 /// WalkCase - Walks the 'case' expression DAG and invokes
 /// TestCallback on every test, and StatementCallback on every
 /// statement. Handles 'case' nesting, but not the 'and' and 'or'
-/// combinators.
-// TODO: Re-implement EmitCaseConstructHandler on top of this function?
+/// combinators (that is, they are passed directly to TestCallback).
+/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
+/// IndentLevel, bool FirstTest)'.
+/// StatementCallback must have type 'void StatementCallback(const Init*,
+/// unsigned IndentLevel)'.
 template <typename F1, typename F2>
-void WalkCase(Init* Case, F1 TestCallback, F2 StatementCallback) {
+void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
+              unsigned IndentLevel = 0)
+{
   const DagInit& d = InitPtrToDag(Case);
+
+  // Error checks.
+  if (GetOperatorName(d) != "case")
+    throw "WalkCase should be invoked only on 'case' expressions!";
+
+  if (d.getNumArgs() < 2)
+    throw "There should be at least one clause in the 'case' expression:\n"
+      + d.getAsString();
+
+  // Main loop.
   bool even = false;
+  const unsigned numArgs = d.getNumArgs();
+  unsigned i = 1;
   for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
        B != E; ++B) {
     Init* arg = *B;
-    if (even && dynamic_cast<DagInit*>(arg)
-        && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case")
-      WalkCase(arg, TestCallback, StatementCallback);
-    else if (!even)
-      TestCallback(arg);
+
+    if (!even)
+    {
+      // Handle test.
+      const DagInit& Test = InitPtrToDag(arg);
+
+      if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
+        throw "The 'default' clause should be the last in the "
+          "'case' construct!";
+      if (i == numArgs)
+        throw "Case construct handler: no corresponding action "
+          "found for the test " + Test.getAsString() + '!';
+
+      TestCallback(Test, IndentLevel, (i == 1));
+    }
     else
-      StatementCallback(arg);
+    {
+      if (dynamic_cast<DagInit*>(arg)
+          && GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
+        // Nested 'case'.
+        WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
+      }
+
+      // Handle statement.
+      StatementCallback(arg, IndentLevel);
+    }
+
+    ++i;
     even = !even;
   }
 }
@@ -901,12 +1070,14 @@ class ExtractOptionNames {
 
   void processDag(const Init* Statement) {
     const DagInit& Stmt = InitPtrToDag(Statement);
-    const std::string& ActionName = Stmt.getOperator()->getAsString();
+    const std::string& ActionName = GetOperatorName(Stmt);
     if (ActionName == "forward" || ActionName == "forward_as" ||
-        ActionName == "unpack_values" || ActionName == "switch_on" ||
-        ActionName == "parameter_equals" || ActionName == "element_in_list" ||
-        ActionName == "not_empty" || ActionName == "empty") {
-      checkNumberOfArguments(&Stmt, 1);
+        ActionName == "forward_value" ||
+        ActionName == "forward_transformed_value" ||
+        ActionName == "switch_on" || ActionName == "parameter_equals" ||
+        ActionName == "element_in_list" || ActionName == "not_empty" ||
+        ActionName == "empty") {
+      checkNumberOfArguments(Stmt, 1);
       const std::string& Name = InitPtrToString(Stmt.getArg(0));
       OptionNames_.insert(Name);
     }
@@ -932,6 +1103,13 @@ public:
       this->processDag(Statement);
     }
   }
+
+  void operator()(const DagInit& Test, unsigned, bool) {
+    this->operator()(&Test);
+  }
+  void operator()(const Init* Statement, unsigned) {
+    this->operator()(Statement);
+  }
 };
 
 /// CheckForSuperfluousOptions - Check that there are no side
@@ -958,10 +1136,10 @@ void CheckForSuperfluousOptions (const RecordVector& Edges,
   for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
        B != E; ++B) {
     const Record* Edge = *B;
-    DagInit* Weight = Edge->getValueAsDag("weight");
+    DagInit& Weight = *Edge->getValueAsDag("weight");
 
     if (!isDagEmpty(Weight))
-      WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
+      WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
   }
 
   // Check that all options in OptDescs belong to the set of
@@ -976,72 +1154,171 @@ void CheckForSuperfluousOptions (const RecordVector& Edges,
   }
 }
 
-/// EmitCaseTest1Arg - Helper function used by
-/// EmitCaseConstructHandler.
-bool EmitCaseTest1Arg(const std::string& TestName,
-                      const DagInit& d,
-                      const OptionDescriptions& OptDescs,
-                      raw_ostream& O) {
-  checkNumberOfArguments(&d, 1);
+/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
+bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
+  if (TestName == "single_input_file") {
+    O << "InputFilenames.size() == 1";
+    return true;
+  }
+  else if (TestName == "multiple_input_files") {
+    O << "InputFilenames.size() > 1";
+    return true;
+  }
+
+  return false;
+}
+
+/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
+template <typename F>
+void EmitListTest(const ListInit& L, const char* LogicOp,
+                  F Callback, raw_ostream& O)
+{
+  // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
+  // of Dags...
+  bool isFirst = true;
+  for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
+    if (isFirst)
+      isFirst = false;
+    else
+      O << " || ";
+    Callback(InitPtrToString(*B), O);
+  }
+}
+
+// Callbacks for use with EmitListTest.
+
+class EmitSwitchOn {
+  const OptionDescriptions& OptDescs_;
+public:
+  EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
+  {}
+
+  void operator()(const std::string& OptName, raw_ostream& O) const {
+    const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
+    O << OptDesc.GenVariableName();
+  }
+};
+
+class EmitEmptyTest {
+  bool EmitNegate_;
+  const OptionDescriptions& OptDescs_;
+public:
+  EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
+    : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
+  {}
+
+  void operator()(const std::string& OptName, raw_ostream& O) const {
+    const char* Neg = (EmitNegate_ ? "!" : "");
+    if (OptName == "o") {
+      O << Neg << "OutputFilename.empty()";
+    }
+    else if (OptName == "save-temps") {
+      O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
+    }
+    else {
+      const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
+      O << Neg << OptDesc.GenVariableName() << ".empty()";
+    }
+  }
+};
+
+
+/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
+bool EmitCaseTest1ArgList(const std::string& TestName,
+                          const DagInit& d,
+                          const OptionDescriptions& OptDescs,
+                          raw_ostream& O) {
+  const ListInit& L = *static_cast<ListInit*>(d.getArg(0));
+
+  if (TestName == "any_switch_on") {
+    EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
+    return true;
+  }
+  else if (TestName == "switch_on") {
+    EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
+    return true;
+  }
+  else if (TestName == "any_not_empty") {
+    EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
+    return true;
+  }
+  else if (TestName == "any_empty") {
+    EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
+    return true;
+  }
+  else if (TestName == "not_empty") {
+    EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
+    return true;
+  }
+  else if (TestName == "empty") {
+    EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
+    return true;
+  }
+
+  return false;
+}
+
+/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
+bool EmitCaseTest1ArgStr(const std::string& TestName,
+                         const DagInit& d,
+                         const OptionDescriptions& OptDescs,
+                         raw_ostream& O) {
   const std::string& OptName = InitPtrToString(d.getArg(0));
 
   if (TestName == "switch_on") {
-    const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
-    if (!OptDesc.isSwitch())
-      throw OptName + ": incorrect option type - should be a switch!";
-    O << OptDesc.GenVariableName();
+    apply(EmitSwitchOn(OptDescs), OptName, O);
     return true;
-  } else if (TestName == "input_languages_contain") {
+  }
+  else if (TestName == "input_languages_contain") {
     O << "InLangs.count(\"" << OptName << "\") != 0";
     return true;
-  } else if (TestName == "in_language") {
+  }
+  else if (TestName == "in_language") {
     // This works only for single-argument Tool::GenerateAction. Join
     // tools can process several files in different languages simultaneously.
 
     // TODO: make this work with Edge::Weight (if possible).
     O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
     return true;
-  } else if (TestName == "not_empty" || TestName == "empty") {
-    const char* Test = (TestName == "empty") ? "" : "!";
-
-    if (OptName == "o") {
-      O << Test << "OutputFilename.empty()";
-      return true;
-    }
-    else {
-      const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
-      if (OptDesc.isSwitch())
-        throw OptName
-          + ": incorrect option type - should be a list or parameter!";
-      O << Test << OptDesc.GenVariableName() << ".empty()";
-      return true;
-    }
+  }
+  else if (TestName == "not_empty" || TestName == "empty") {
+    bool EmitNegate = (TestName == "not_empty");
+    apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
+    return true;
   }
 
   return false;
 }
 
-/// EmitCaseTest2Args - Helper function used by
-/// EmitCaseConstructHandler.
+/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
+bool EmitCaseTest1Arg(const std::string& TestName,
+                      const DagInit& d,
+                      const OptionDescriptions& OptDescs,
+                      raw_ostream& O) {
+  checkNumberOfArguments(d, 1);
+  if (typeid(*d.getArg(0)) == typeid(ListInit))
+    return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
+  else
+    return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
+}
+
+/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
 bool EmitCaseTest2Args(const std::string& TestName,
                        const DagInit& d,
                        unsigned IndentLevel,
                        const OptionDescriptions& OptDescs,
                        raw_ostream& O) {
-  checkNumberOfArguments(&d, 2);
+  checkNumberOfArguments(d, 2);
   const std::string& OptName = InitPtrToString(d.getArg(0));
   const std::string& OptArg = InitPtrToString(d.getArg(1));
-  const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
 
   if (TestName == "parameter_equals") {
-    if (!OptDesc.isParameter())
-      throw OptName + ": incorrect option type - should be a parameter!";
+    const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
     O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
     return true;
   }
   else if (TestName == "element_in_list") {
-    if (!OptDesc.isList())
-      throw OptName + ": incorrect option type - should be a list!";
+    const OptionDescription& OptDesc = OptDescs.FindList(OptName);
     const std::string& VarName = OptDesc.GenVariableName();
     O << "std::find(" << VarName << ".begin(),\n";
     O.indent(IndentLevel + Indent1)
@@ -1082,7 +1359,7 @@ void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
 void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
                     const OptionDescriptions& OptDescs, raw_ostream& O)
 {
-  checkNumberOfArguments(&d, 1);
+  checkNumberOfArguments(d, 1);
   const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
   O << "! (";
   EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
@@ -1093,7 +1370,7 @@ void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
 void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
                   const OptionDescriptions& OptDescs,
                   raw_ostream& O) {
-  const std::string& TestName = d.getOperator()->getAsString();
+  const std::string& TestName = GetOperatorName(d);
 
   if (TestName == "and")
     EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
@@ -1101,6 +1378,8 @@ void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
     EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
   else if (TestName == "not")
     EmitLogicalNot(d, IndentLevel, OptDescs, O);
+  else if (EmitCaseTest0Args(TestName, O))
+    return;
   else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
     return;
   else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
@@ -1109,65 +1388,85 @@ void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
     throw TestName + ": unknown edge property!";
 }
 
-// Emit code that handles the 'case' construct.
-// Takes a function object that should emit code for every case clause.
-// Callback's type is
-// void F(Init* Statement, unsigned IndentLevel, raw_ostream& O).
-template <typename F>
-void EmitCaseConstructHandler(const Init* Dag, unsigned IndentLevel,
-                              F Callback, bool EmitElseIf,
-                              const OptionDescriptions& OptDescs,
-                              raw_ostream& O) {
-  const DagInit* d = &InitPtrToDag(Dag);
-  if (d->getOperator()->getAsString() != "case")
-    throw std::string("EmitCaseConstructHandler should be invoked"
-                      " only on 'case' expressions!");
 
-  unsigned numArgs = d->getNumArgs();
-  if (d->getNumArgs() < 2)
-    throw "There should be at least one clause in the 'case' expression:\n"
-      + d->getAsString();
+/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
+class EmitCaseTestCallback {
+  bool EmitElseIf_;
+  const OptionDescriptions& OptDescs_;
+  raw_ostream& O_;
+public:
 
-  for (unsigned i = 0; i != numArgs; ++i) {
-    const DagInit& Test = InitPtrToDag(d->getArg(i));
+  EmitCaseTestCallback(bool EmitElseIf,
+                       const OptionDescriptions& OptDescs, raw_ostream& O)
+    : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
+  {}
 
-    // Emit the test.
-    if (Test.getOperator()->getAsString() == "default") {
-      if (i+2 != numArgs)
-        throw std::string("The 'default' clause should be the last in the"
-                          "'case' construct!");
-      O.indent(IndentLevel) << "else {\n";
+  void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
+  {
+    if (GetOperatorName(Test) == "default") {
+      O_.indent(IndentLevel) << "else {\n";
     }
     else {
-      O.indent(IndentLevel) << ((i != 0 && EmitElseIf) ? "else if (" : "if (");
-      EmitCaseTest(Test, IndentLevel, OptDescs, O);
-      O << ") {\n";
+      O_.indent(IndentLevel)
+        << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
+      EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
+      O_ << ") {\n";
     }
+  }
+};
 
-    // Emit the corresponding statement.
-    ++i;
-    if (i == numArgs)
-      throw "Case construct handler: no corresponding action "
-        "found for the test " + Test.getAsString() + '!';
-
-    Init* arg = d->getArg(i);
-    const DagInit* nd = dynamic_cast<DagInit*>(arg);
-    if (nd && (nd->getOperator()->getAsString() == "case")) {
-      // Handle the nested 'case'.
-      EmitCaseConstructHandler(nd, (IndentLevel + Indent1),
-                               Callback, EmitElseIf, OptDescs, O);
-    }
-    else {
-      Callback(arg, (IndentLevel + Indent1), O);
+/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
+template <typename F>
+class EmitCaseStatementCallback {
+  F Callback_;
+  raw_ostream& O_;
+public:
+
+  EmitCaseStatementCallback(F Callback, raw_ostream& O)
+    : Callback_(Callback), O_(O)
+  {}
+
+  void operator() (const Init* Statement, unsigned IndentLevel) {
+
+    // Ignore nested 'case' DAG.
+    if (!(dynamic_cast<const DagInit*>(Statement) &&
+          GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case")) {
+      if (typeid(*Statement) == typeid(ListInit)) {
+        const ListInit& DagList = *static_cast<const ListInit*>(Statement);
+        for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
+             B != E; ++B)
+          Callback_(*B, (IndentLevel + Indent1), O_);
+      }
+      else {
+        Callback_(Statement, (IndentLevel + Indent1), O_);
+      }
     }
-    O.indent(IndentLevel) << "}\n";
+    O_.indent(IndentLevel) << "}\n";
   }
+
+};
+
+/// EmitCaseConstructHandler - Emit code that handles the 'case'
+/// construct. Takes a function object that should emit code for every case
+/// clause. Implemented on top of WalkCase.
+/// Callback's type is void F(Init* Statement, unsigned IndentLevel,
+/// raw_ostream& O).
+/// EmitElseIf parameter controls the type of condition that is emitted ('if
+/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..)  {..}
+/// .. else {..}').
+template <typename F>
+void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
+                              F Callback, bool EmitElseIf,
+                              const OptionDescriptions& OptDescs,
+                              raw_ostream& O) {
+  WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
+           EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
 }
 
-/// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to
-/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path"] .
-/// Helper function used by EmitCmdLineVecFill and.
-void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
+/// TokenizeCmdLine - converts from
+/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
+/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
+void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
   const char* Delimiters = " \t\n\v\f\r";
   enum TokenizerState
   { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
@@ -1247,62 +1546,99 @@ void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
   }
 }
 
-/// SubstituteSpecialCommands - Perform string substitution for $CALL
-/// and $ENV. Helper function used by EmitCmdLineVecFill().
-StrVector::const_iterator SubstituteSpecialCommands
-(StrVector::const_iterator Pos, StrVector::const_iterator End, raw_ostream& O)
+/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
+/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
+/// SubstituteSpecialCommands().
+StrVector::const_iterator
+SubstituteCall (StrVector::const_iterator Pos,
+                StrVector::const_iterator End,
+                bool IsJoin, raw_ostream& O)
 {
+  const char* errorMessage = "Syntax error in $CALL invocation!";
+  checkedIncrement(Pos, End, errorMessage);
+  const std::string& CmdName = *Pos;
 
-  const std::string& cmd = *Pos;
-
-  if (cmd == "$CALL") {
-    checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
-    const std::string& CmdName = *Pos;
+  if (CmdName == ")")
+    throw "$CALL invocation: empty argument list!";
 
-    if (CmdName == ")")
-      throw std::string("$CALL invocation: empty argument list!");
+  O << "hooks::";
+  O << CmdName << "(";
 
-    O << "hooks::";
-    O << CmdName << "(";
 
+  bool firstIteration = true;
+  while (true) {
+    checkedIncrement(Pos, End, errorMessage);
+    const std::string& Arg = *Pos;
+    assert(Arg.size() != 0);
 
-    bool firstIteration = true;
-    while (true) {
-      checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
-      const std::string& Arg = *Pos;
-      assert(Arg.size() != 0);
+    if (Arg[0] == ')')
+      break;
 
-      if (Arg[0] == ')')
-        break;
+    if (firstIteration)
+      firstIteration = false;
+    else
+      O << ", ";
 
-      if (firstIteration)
-        firstIteration = false;
+    if (Arg == "$INFILE") {
+      if (IsJoin)
+        throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
       else
-        O << ", ";
-
+        O << "inFile.c_str()";
+    }
+    else {
       O << '"' << Arg << '"';
     }
+  }
 
-    O << ')';
+  O << ')';
 
-  }
-  else if (cmd == "$ENV") {
-    checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
-    const std::string& EnvName = *Pos;
+  return Pos;
+}
+
+/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
+/// function used by SubstituteSpecialCommands().
+StrVector::const_iterator
+SubstituteEnv (StrVector::const_iterator Pos,
+               StrVector::const_iterator End, raw_ostream& O)
+{
+  const char* errorMessage = "Syntax error in $ENV invocation!";
+  checkedIncrement(Pos, End, errorMessage);
+  const std::string& EnvName = *Pos;
+
+  if (EnvName == ")")
+    throw "$ENV invocation: empty argument list!";
 
-    if (EnvName == ")")
-      throw "$ENV invocation: empty argument list!";
+  O << "checkCString(std::getenv(\"";
+  O << EnvName;
+  O << "\"))";
 
-    O << "checkCString(std::getenv(\"";
-    O << EnvName;
-    O << "\"))";
+  checkedIncrement(Pos, End, errorMessage);
 
-    checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
+  return Pos;
+}
+
+/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
+/// handler code. Helper function used by EmitCmdLineVecFill().
+StrVector::const_iterator
+SubstituteSpecialCommands (StrVector::const_iterator Pos,
+                           StrVector::const_iterator End,
+                           bool IsJoin, raw_ostream& O)
+{
+
+  const std::string& cmd = *Pos;
+
+  // Perform substitution.
+  if (cmd == "$CALL") {
+    Pos = SubstituteCall(Pos, End, IsJoin, O);
+  }
+  else if (cmd == "$ENV") {
+    Pos = SubstituteEnv(Pos, End, O);
   }
   else {
     throw "Unknown special command: " + cmd;
   }
 
+  // Handle '$CMD(ARG)/additional/text'.
   const std::string& Leftover = *Pos;
   assert(Leftover.at(0) == ')');
   if (Leftover.size() != 1)
@@ -1317,7 +1653,7 @@ void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
                         bool IsJoin, unsigned IndentLevel,
                         raw_ostream& O) {
   StrVector StrVec;
-  TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
+  TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
 
   if (StrVec.empty())
     throw "Tool '" + ToolName + "' has empty command line!";
@@ -1337,12 +1673,15 @@ void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
     ++I;
   }
 
+  bool hasINFILE = false;
+
   for (; I != E; ++I) {
     const std::string& cmd = *I;
     assert(!cmd.empty());
     O.indent(IndentLevel);
     if (cmd.at(0) == '$') {
       if (cmd == "$INFILE") {
+        hasINFILE = true;
         if (IsJoin) {
           O << "for (PathVector::const_iterator B = inFiles.begin()"
             << ", E = inFiles.end();\n";
@@ -1354,11 +1693,12 @@ void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
         }
       }
       else if (cmd == "$OUTFILE") {
-        O << "vec.push_back(out_file);\n";
+        O << "vec.push_back(\"\");\n";
+        O.indent(IndentLevel) << "out_file_index = vec.size()-1;\n";
       }
       else {
         O << "vec.push_back(";
-        I = SubstituteSpecialCommands(I, E, O);
+        I = SubstituteSpecialCommands(I, E, IsJoin, O);
         O << ");\n";
       }
     }
@@ -1366,10 +1706,12 @@ void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
       O << "vec.push_back(\"" << cmd << "\");\n";
     }
   }
-  O.indent(IndentLevel) << "cmd = ";
+  if (!hasINFILE)
+    throw "Tool '" + ToolName + "' doesn't take any input!";
 
+  O.indent(IndentLevel) << "cmd = ";
   if (StrVec[0][0] == '$')
-    SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), O);
+    SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), IsJoin, O);
   else
     O << '"' << StrVec[0] << '"';
   O << ";\n";
@@ -1449,113 +1791,241 @@ void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
     break;
   case OptionType::Alias:
   default:
-    throw std::string("Aliases are not allowed in tool option descriptions!");
+    throw "Aliases are not allowed in tool option descriptions!";
   }
 }
 
-/// EmitActionHandler - Emit code that handles actions. Used by
-/// EmitGenerateActionMethod() as an argument to
-/// EmitCaseConstructHandler().
-class EmitActionHandler {
+/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
+/// EmitPreprocessOptionsCallback.
+struct ActionHandlingCallbackBase
+{
+
+  void onErrorDag(const DagInit& d,
+                  unsigned IndentLevel, raw_ostream& O) const
+  {
+    O.indent(IndentLevel)
+      << "throw std::runtime_error(\"" <<
+      (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
+       : "Unknown error!")
+      << "\");\n";
+  }
+
+  void onWarningDag(const DagInit& d,
+                    unsigned IndentLevel, raw_ostream& O) const
+  {
+    checkNumberOfArguments(d, 1);
+    O.indent(IndentLevel) << "llvm::errs() << \""
+                          << InitPtrToString(d.getArg(0)) << "\";\n";
+  }
+
+};
+
+/// EmitActionHandlersCallback - Emit code that handles actions. Used by
+/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
+
+class EmitActionHandlersCallback;
+
+typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
+(const DagInit&, unsigned, raw_ostream&) const;
+
+class EmitActionHandlersCallback :
+  public ActionHandlingCallbackBase,
+  public HandlerTable<EmitActionHandlersCallbackHandler>
+{
+  typedef EmitActionHandlersCallbackHandler Handler;
+
   const OptionDescriptions& OptDescs;
 
-  void processActionDag(const Init* Statement, unsigned IndentLevel,
-                        raw_ostream& O) const
+  /// EmitHookInvocation - Common code for hook invocation from actions. Used by
+  /// onAppendCmd and onOutputSuffix.
+  void EmitHookInvocation(const std::string& Str,
+                          const char* BlockOpen, const char* BlockClose,
+                          unsigned IndentLevel, raw_ostream& O) const
   {
-    const DagInit& Dag = InitPtrToDag(Statement);
-    const std::string& ActionName = Dag.getOperator()->getAsString();
+    StrVector Out;
+    TokenizeCmdLine(Str, Out);
+
+    for (StrVector::const_iterator B = Out.begin(), E = Out.end();
+         B != E; ++B) {
+      const std::string& cmd = *B;
 
-    if (ActionName == "append_cmd") {
-      checkNumberOfArguments(&Dag, 1);
-      const std::string& Cmd = InitPtrToString(Dag.getArg(0));
-      StrVector Out;
-      llvm::SplitString(Cmd, Out);
+      O.indent(IndentLevel) << BlockOpen;
 
-      for (StrVector::const_iterator B = Out.begin(), E = Out.end();
-           B != E; ++B)
-        O.indent(IndentLevel) << "vec.push_back(\"" << *B << "\");\n";
-    }
-    else if (ActionName == "error") {
-      O.indent(IndentLevel) << "throw std::runtime_error(\"" <<
-        (Dag.getNumArgs() >= 1 ? InitPtrToString(Dag.getArg(0))
-         : "Unknown error!")
-        << "\");\n";
-    }
-    else if (ActionName == "forward") {
-      checkNumberOfArguments(&Dag, 1);
-      const std::string& Name = InitPtrToString(Dag.getArg(0));
-      EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
-                                            IndentLevel, "", O);
-    }
-    else if (ActionName == "forward_as") {
-      checkNumberOfArguments(&Dag, 2);
-      const std::string& Name = InitPtrToString(Dag.getArg(0));
-      const std::string& NewName = InitPtrToString(Dag.getArg(1));
-      EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
-                                            IndentLevel, NewName, O);
-    }
-    else if (ActionName == "output_suffix") {
-      checkNumberOfArguments(&Dag, 1);
-      const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
-      O.indent(IndentLevel) << "output_suffix = \"" << OutSuf << "\";\n";
-    }
-    else if (ActionName == "stop_compilation") {
-      O.indent(IndentLevel) << "stop_compilation = true;\n";
+      if (cmd.at(0) == '$')
+        B = SubstituteSpecialCommands(B, E,  /* IsJoin = */ true, O);
+      else
+        O << '"' << cmd << '"';
+
+      O << BlockClose;
     }
-    else if (ActionName == "unpack_values") {
-      checkNumberOfArguments(&Dag, 1);
-      const std::string& Name = InitPtrToString(Dag.getArg(0));
-      const OptionDescription& D = OptDescs.FindOption(Name);
-
-      if (D.isMultiVal())
-        throw std::string("Can't use unpack_values with multi-valued options!");
-
-      if (D.isList()) {
-        O.indent(IndentLevel)
-          << "for (" << D.GenTypeDeclaration()
-          << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
-        O.indent(IndentLevel)
-          << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n";
-        O.indent(IndentLevel + Indent1)
-          << "llvm::SplitString(*B, vec, \",\");\n";
-      }
-      else if (D.isParameter()){
-        O.indent(IndentLevel) << "llvm::SplitString("
-                              << D.GenVariableName() << ", vec, \",\");\n";
-      }
-      else {
-        throw "Option '" + D.Name +
-          "': switches can't have the 'unpack_values' property!";
-      }
+  }
+
+  void onAppendCmd (const DagInit& Dag,
+                    unsigned IndentLevel, raw_ostream& O) const
+  {
+    checkNumberOfArguments(Dag, 1);
+    this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
+                             "vec.push_back(", ");\n", IndentLevel, O);
+  }
+
+  void onForward (const DagInit& Dag,
+                  unsigned IndentLevel, raw_ostream& O) const
+  {
+    checkNumberOfArguments(Dag, 1);
+    const std::string& Name = InitPtrToString(Dag.getArg(0));
+    EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
+                                          IndentLevel, "", O);
+  }
+
+  void onForwardAs (const DagInit& Dag,
+                    unsigned IndentLevel, raw_ostream& O) const
+  {
+    checkNumberOfArguments(Dag, 2);
+    const std::string& Name = InitPtrToString(Dag.getArg(0));
+    const std::string& NewName = InitPtrToString(Dag.getArg(1));
+    EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
+                                          IndentLevel, NewName, O);
+  }
+
+  void onForwardValue (const DagInit& Dag,
+                       unsigned IndentLevel, raw_ostream& O) const
+  {
+    checkNumberOfArguments(Dag, 1);
+    const std::string& Name = InitPtrToString(Dag.getArg(0));
+    const OptionDescription& D = OptDescs.FindListOrParameter(Name);
+
+    if (D.isParameter()) {
+      O.indent(IndentLevel) << "vec.push_back("
+                            << D.GenVariableName() << ");\n";
     }
     else {
-      throw "Unknown action name: " + ActionName + "!";
+      O.indent(IndentLevel) << "std::copy(" << D.GenVariableName()
+                            << ".begin(), " << D.GenVariableName()
+                            << ".end(), std::back_inserter(vec));\n";
     }
   }
+
+  void onForwardTransformedValue (const DagInit& Dag,
+                                  unsigned IndentLevel, raw_ostream& O) const
+  {
+    checkNumberOfArguments(Dag, 2);
+    const std::string& Name = InitPtrToString(Dag.getArg(0));
+    const std::string& Hook = InitPtrToString(Dag.getArg(1));
+    const OptionDescription& D = OptDescs.FindListOrParameter(Name);
+
+    O.indent(IndentLevel) << "vec.push_back(" << "hooks::"
+                          << Hook << "(" << D.GenVariableName()
+                          << (D.isParameter() ? ".c_str()" : "") << "));\n";
+  }
+
+  void onOutputSuffix (const DagInit& Dag,
+                       unsigned IndentLevel, raw_ostream& O) const
+  {
+    checkNumberOfArguments(Dag, 1);
+    this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
+                             "output_suffix = ", ";\n", IndentLevel, O);
+  }
+
+  void onStopCompilation (const DagInit& Dag,
+                          unsigned IndentLevel, raw_ostream& O) const
+  {
+    O.indent(IndentLevel) << "stop_compilation = true;\n";
+  }
+
+
+  void onUnpackValues (const DagInit& Dag,
+                       unsigned IndentLevel, raw_ostream& O) const
+  {
+    throw "'unpack_values' is deprecated. "
+      "Use 'comma_separated' + 'forward_value' instead!";
+  }
+
  public:
-  EmitActionHandler(const OptionDescriptions& OD)
-    : OptDescs(OD) {}
 
-  void operator()(const Init* Statement, unsigned IndentLevel,
-                  raw_ostream& O) const
+  explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
+    : OptDescs(OD)
   {
-    if (typeid(*Statement) == typeid(ListInit)) {
-      const ListInit& DagList = *static_cast<const ListInit*>(Statement);
-      for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
-           B != E; ++B)
-        this->processActionDag(*B, IndentLevel, O);
-    }
-    else {
-      this->processActionDag(Statement, IndentLevel, O);
+    if (!staticMembersInitialized_) {
+      AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
+      AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
+      AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
+      AddHandler("forward", &EmitActionHandlersCallback::onForward);
+      AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
+      AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
+      AddHandler("forward_transformed_value",
+                 &EmitActionHandlersCallback::onForwardTransformedValue);
+      AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
+      AddHandler("stop_compilation",
+                 &EmitActionHandlersCallback::onStopCompilation);
+      AddHandler("unpack_values",
+                 &EmitActionHandlersCallback::onUnpackValues);
+
+      staticMembersInitialized_ = true;
     }
   }
+
+  void operator()(const Init* I,
+                  unsigned IndentLevel, raw_ostream& O) const
+  {
+    InvokeDagInitHandler(this, I, IndentLevel, O);
+  }
 };
 
-// EmitGenerateActionMethod - Emit one of two versions of the
-// Tool::GenerateAction() method.
-void EmitGenerateActionMethod (const ToolDescription& D,
-                               const OptionDescriptions& OptDescs,
-                               bool IsJoin, raw_ostream& O) {
+bool IsOutFileIndexCheckRequiredStr (const Init* CmdLine) {
+  StrVector StrVec;
+  TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
+
+  for (StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
+       I != E; ++I) {
+    if (*I == "$OUTFILE")
+      return false;
+  }
+
+  return true;
+}
+
+class IsOutFileIndexCheckRequiredStrCallback {
+  bool* ret_;
+
+public:
+  IsOutFileIndexCheckRequiredStrCallback(bool* ret) : ret_(ret)
+  {}
+
+  void operator()(const Init* CmdLine) {
+    // Ignore nested 'case' DAG.
+    if (typeid(*CmdLine) == typeid(DagInit))
+      return;
+
+    if (IsOutFileIndexCheckRequiredStr(CmdLine))
+      *ret_ = true;
+  }
+
+  void operator()(const DagInit* Test, unsigned, bool) {
+    this->operator()(Test);
+  }
+  void operator()(const Init* Statement, unsigned) {
+    this->operator()(Statement);
+  }
+};
+
+bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
+  bool ret = false;
+  WalkCase(CmdLine, Id(), IsOutFileIndexCheckRequiredStrCallback(&ret));
+  return ret;
+}
+
+/// IsOutFileIndexCheckRequired - Should we emit an "out_file_index != -1" check
+/// in EmitGenerateActionMethod() ?
+bool IsOutFileIndexCheckRequired (Init* CmdLine) {
+  if (typeid(*CmdLine) == typeid(StringInit))
+    return IsOutFileIndexCheckRequiredStr(CmdLine);
+  else
+    return IsOutFileIndexCheckRequiredCase(CmdLine);
+}
+
+void EmitGenerateActionMethodHeader(const ToolDescription& D,
+                                    bool IsJoin, raw_ostream& O)
+{
   if (IsJoin)
     O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
   else
@@ -1571,28 +2041,49 @@ void EmitGenerateActionMethod (const ToolDescription& D,
   O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
   O.indent(Indent2) << "const char* output_suffix = \""
                     << D.OutputSuffix << "\";\n";
-  O.indent(Indent2) << "std::string out_file;\n\n";
+}
 
-  // For every understood option, emit handling code.
-  if (D.Actions)
-    EmitCaseConstructHandler(D.Actions, Indent2, EmitActionHandler(OptDescs),
-                             false, OptDescs, O);
+// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
+// Tool::GenerateAction() method.
+void EmitGenerateActionMethod (const ToolDescription& D,
+                               const OptionDescriptions& OptDescs,
+                               bool IsJoin, raw_ostream& O) {
 
-  O << '\n';
-  O.indent(Indent2)
-    << "out_file = OutFilename(" << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
-  O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
+  EmitGenerateActionMethodHeader(D, IsJoin, O);
 
-  // cmd_line is either a string or a 'case' construct.
   if (!D.CmdLine)
     throw "Tool " + D.Name + " has no cmd_line property!";
-  else if (typeid(*D.CmdLine) == typeid(StringInit))
+
+  bool IndexCheckRequired = IsOutFileIndexCheckRequired(D.CmdLine);
+  O.indent(Indent2) << "int out_file_index"
+                    << (IndexCheckRequired ? " = -1" : "")
+                    << ";\n\n";
+
+  // Process the cmd_line property.
+  if (typeid(*D.CmdLine) == typeid(StringInit))
     EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
   else
     EmitCaseConstructHandler(D.CmdLine, Indent2,
                              EmitCmdLineVecFillCallback(IsJoin, D.Name),
                              true, OptDescs, O);
 
+  // For every understood option, emit handling code.
+  if (D.Actions)
+    EmitCaseConstructHandler(D.Actions, Indent2,
+                             EmitActionHandlersCallback(OptDescs),
+                             false, OptDescs, O);
+
+  O << '\n';
+  O.indent(Indent2)
+    << "std::string out_file = OutFilename("
+    << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
+  O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
+
+  if (IndexCheckRequired)
+    O.indent(Indent2) << "if (out_file_index != -1)\n";
+  O.indent(IndexCheckRequired ? Indent3 : Indent2)
+    << "vec[out_file_index] = out_file;\n";
+
   // Handle the Sink property.
   if (D.isSink()) {
     O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
@@ -1689,7 +2180,7 @@ void EmitToolClassDefinition (const ToolDescription& D,
   else
     O << "Tool";
 
-  O << "{\nprivate:\n";
+  O << " {\nprivate:\n";
   O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
 
   O << "public:\n";
@@ -1748,16 +2239,17 @@ void EmitOptionDefinitions (const OptionDescriptions& descs,
     else if (val.isOneOrMore() && val.isList()) {
         O << ", cl::OneOrMore";
     }
-    else if (val.isZeroOrOne() && val.isList()) {
-        O << ", cl::ZeroOrOne";
+    else if (val.isOptional() && val.isList()) {
+        O << ", cl::Optional";
     }
 
-    if (val.isReallyHidden()) {
+    if (val.isReallyHidden())
       O << ", cl::ReallyHidden";
-    }
-    else if (val.isHidden()) {
+    else if (val.isHidden())
       O << ", cl::Hidden";
-    }
+
+    if (val.isCommaSeparated())
+      O << ", cl::CommaSeparated";
 
     if (val.MultiVal > 1)
       O << ", cl::multi_val(" << val.MultiVal << ')';
@@ -1797,10 +2289,110 @@ void EmitOptionDefinitions (const OptionDescriptions& descs,
   O << '\n';
 }
 
-/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
+/// EmitPreprocessOptionsCallback - Helper function passed to
+/// EmitCaseConstructHandler() by EmitPreprocessOptions().
+
+class EmitPreprocessOptionsCallback;
+
+typedef void
+(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
+(const DagInit&, unsigned, raw_ostream&) const;
+
+class EmitPreprocessOptionsCallback :
+  public ActionHandlingCallbackBase,
+  public HandlerTable<EmitPreprocessOptionsCallbackHandler>
+{
+  typedef EmitPreprocessOptionsCallbackHandler Handler;
+
+  const OptionDescriptions& OptDescs_;
+
+  void onUnsetOptionStr(const Init* I,
+                        unsigned IndentLevel, raw_ostream& O) const
+  {
+    const std::string& OptName = InitPtrToString(I);
+    const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
+
+    if (OptDesc.isSwitch()) {
+      O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
+    }
+    else if (OptDesc.isParameter()) {
+      O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
+    }
+    else if (OptDesc.isList()) {
+      O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
+    }
+    else {
+      throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
+    }
+  }
+
+  void onUnsetOptionList(const ListInit& L,
+                         unsigned IndentLevel, raw_ostream& O) const
+  {
+    for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B)
+      this->onUnsetOptionStr(*B, IndentLevel, O);
+  }
+
+  void onUnsetOption(const DagInit& d,
+                     unsigned IndentLevel, raw_ostream& O) const
+  {
+    checkNumberOfArguments(d, 1);
+    Init* I = d.getArg(0);
+
+    if (typeid(*I) == typeid(ListInit)) {
+      const ListInit& L = *static_cast<const ListInit*>(I);
+      this->onUnsetOptionList(L, IndentLevel, O);
+    }
+    else {
+      this->onUnsetOptionStr(I, IndentLevel, O);
+    }
+  }
+
+public:
+
+  EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
+  : OptDescs_(OptDescs)
+  {
+    if (!staticMembersInitialized_) {
+      AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
+      AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
+      AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
+
+      staticMembersInitialized_ = true;
+    }
+  }
+
+  void operator()(const Init* I,
+                  unsigned IndentLevel, raw_ostream& O) const
+  {
+    InvokeDagInitHandler(this, I, IndentLevel, O);
+  }
+
+};
+
+/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
+void EmitPreprocessOptions (const RecordKeeper& Records,
+                            const OptionDescriptions& OptDecs, raw_ostream& O)
+{
+  O << "void PreprocessOptionsLocal() {\n";
+
+  const RecordVector& OptionPreprocessors =
+    Records.getAllDerivedDefinitions("OptionPreprocessor");
+
+  for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
+         E = OptionPreprocessors.end(); B!=E; ++B) {
+    DagInit* Case = (*B)->getValueAsDag("preprocessor");
+    EmitCaseConstructHandler(Case, Indent1,
+                             EmitPreprocessOptionsCallback(OptDecs),
+                             false, OptDecs, O);
+  }
+
+  O << "}\n\n";
+}
+
+/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
 void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
 {
-  // Generate code
   O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
 
   // Get the relevant field out of RecordKeeper
@@ -1811,7 +2403,7 @@ void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
 
     ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
     if (!LangsToSuffixesList)
-      throw std::string("Error in the language map definition!");
+      throw "Error in the language map definition!";
 
     for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
       const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
@@ -1834,7 +2426,7 @@ void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
 void IncDecWeight (const Init* i, unsigned IndentLevel,
                    raw_ostream& O) {
   const DagInit& d = InitPtrToDag(i);
-  const std::string& OpName = d.getOperator()->getAsString();
+  const std::string& OpName = GetOperatorName(d);
 
   if (OpName == "inc_weight") {
     O.indent(IndentLevel) << "ret += ";
@@ -1843,17 +2435,16 @@ void IncDecWeight (const Init* i, unsigned IndentLevel,
     O.indent(IndentLevel) << "ret -= ";
   }
   else if (OpName == "error") {
-    O.indent(IndentLevel)
-      << "throw std::runtime_error(\"" <<
-      (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
-       : "Unknown error!")
-      << "\");\n";
+    checkNumberOfArguments(d, 1);
+    O.indent(IndentLevel) << "throw std::runtime_error(\""
+                          << InitPtrToString(d.getArg(0))
+                          << "\");\n";
     return;
   }
-
-  else
-    throw "Unknown operator in edge properties list: " + OpName + '!' +
+  else {
+    throw "Unknown operator in edge properties list: '" + OpName + "'!"
       "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
+  }
 
   if (d.getNumArgs() > 0)
     O << InitPtrToInt(d.getArg(0)) << ";\n";
@@ -1894,15 +2485,15 @@ void EmitEdgeClasses (const RecordVector& EdgeVector,
          E = EdgeVector.end(); B != E; ++B) {
     const Record* Edge = *B;
     const std::string& NodeB = Edge->getValueAsString("b");
-    DagInit* Weight = Edge->getValueAsDag("weight");
+    DagInit& Weight = *Edge->getValueAsDag("weight");
 
     if (!isDagEmpty(Weight))
-      EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
+      EmitEdgeClass(i, NodeB, &Weight, OptDescs, O);
     ++i;
   }
 }
 
-/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
+/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
 /// function.
 void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
                                    const ToolDescriptions& ToolDescs,
@@ -1924,7 +2515,7 @@ void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
     const Record* Edge = *B;
     const std::string& NodeA = Edge->getValueAsString("a");
     const std::string& NodeB = Edge->getValueAsString("b");
-    DagInit* Weight = Edge->getValueAsDag("weight");
+    DagInit& Weight = *Edge->getValueAsDag("weight");
 
     O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
 
@@ -1940,18 +2531,61 @@ void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
   O << "}\n\n";
 }
 
+/// HookInfo - Information about the hook type and number of arguments.
+struct HookInfo {
+
+  // A hook can either have a single parameter of type std::vector<std::string>,
+  // or NumArgs parameters of type const char*.
+  enum HookType { ListHook, ArgHook };
+
+  HookType Type;
+  unsigned NumArgs;
+
+  HookInfo() : Type(ArgHook), NumArgs(1)
+  {}
+
+  HookInfo(HookType T) : Type(T), NumArgs(1)
+  {}
+
+  HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
+  {}
+};
+
+typedef llvm::StringMap<HookInfo> HookInfoMap;
+
 /// ExtractHookNames - Extract the hook names from all instances of
-/// $CALL(HookName) in the provided command line string. Helper
+/// $CALL(HookName) in the provided command line string/action. Helper
 /// function used by FillInHookNames().
 class ExtractHookNames {
-  llvm::StringMap<unsigned>& HookNames_;
+  HookInfoMap& HookNames_;
+  const OptionDescriptions& OptDescs_;
 public:
-  ExtractHookNames(llvm::StringMap<unsigned>& HookNames)
-  : HookNames_(HookNames) {}
+  ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
+    : HookNames_(HookNames), OptDescs_(OptDescs)
+  {}
 
-  void operator()(const Init* CmdLine) {
+  void onAction (const DagInit& Dag) {
+    const std::string& Name = GetOperatorName(Dag);
+
+    if (Name == "forward_transformed_value") {
+      checkNumberOfArguments(Dag, 2);
+      const std::string& OptName = InitPtrToString(Dag.getArg(0));
+      const std::string& HookName = InitPtrToString(Dag.getArg(1));
+      const OptionDescription& D = OptDescs_.FindOption(OptName);
+
+      HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
+                                      : HookInfo::ArgHook);
+    }
+    else if (Name == "append_cmd" || Name == "output_suffix") {
+      checkNumberOfArguments(Dag, 1);
+      this->onCmdLine(InitPtrToString(Dag.getArg(0)));
+    }
+  }
+
+  void onCmdLine(const std::string& Cmd) {
     StrVector cmds;
-    TokenizeCmdline(InitPtrToString(CmdLine), cmds);
+    TokenizeCmdLine(Cmd, cmds);
+
     for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
          B != E; ++B) {
       const std::string& cmd = *B;
@@ -1961,7 +2595,6 @@ public:
         checkedIncrement(B, E, "Syntax error in $CALL invocation!");
         const std::string& HookName = *B;
 
-
         if (HookName.at(0) == ')')
           throw "$CALL invoked with no arguments!";
 
@@ -1969,56 +2602,101 @@ public:
           ++NumArgs;
         }
 
-        StringMap<unsigned>::const_iterator H = HookNames_.find(HookName);
+        HookInfoMap::const_iterator H = HookNames_.find(HookName);
 
-        if (H != HookNames_.end() && H->second != NumArgs)
+        if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
+            H->second.Type != HookInfo::ArgHook)
           throw "Overloading of hooks is not allowed. Overloaded hook: "
             + HookName;
         else
-          HookNames_[HookName] = NumArgs;
+          HookNames_[HookName] = HookInfo(NumArgs);
+      }
+    }
+  }
+
+  void operator()(const Init* Arg) {
 
+    // We're invoked on an action (either a dag or a dag list).
+    if (typeid(*Arg) == typeid(DagInit)) {
+      const DagInit& Dag = InitPtrToDag(Arg);
+      this->onAction(Dag);
+      return;
+    }
+    else if (typeid(*Arg) == typeid(ListInit)) {
+      const ListInit& List = InitPtrToList(Arg);
+      for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
+           ++B) {
+        const DagInit& Dag = InitPtrToDag(*B);
+        this->onAction(Dag);
       }
+      return;
     }
+
+    // We're invoked on a command line.
+    this->onCmdLine(InitPtrToString(Arg));
+  }
+
+  void operator()(const DagInit* Test, unsigned, bool) {
+    this->operator()(Test);
+  }
+  void operator()(const Init* Statement, unsigned) {
+    this->operator()(Statement);
   }
 };
 
 /// FillInHookNames - Actually extract the hook names from all command
 /// line strings. Helper function used by EmitHookDeclarations().
 void FillInHookNames(const ToolDescriptions& ToolDescs,
-                     llvm::StringMap<unsigned>& HookNames)
+                     const OptionDescriptions& OptDescs,
+                     HookInfoMap& HookNames)
 {
-  // For all command lines:
+  // For all tool descriptions:
   for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
          E = ToolDescs.end(); B != E; ++B) {
     const ToolDescription& D = *(*B);
+
+    // Look for 'forward_transformed_value' in 'actions'.
+    if (D.Actions)
+      WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
+
+    // Look for hook invocations in 'cmd_line'.
     if (!D.CmdLine)
       continue;
     if (dynamic_cast<StringInit*>(D.CmdLine))
       // This is a string.
-      ExtractHookNames(HookNames).operator()(D.CmdLine);
+      ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
     else
       // This is a 'case' construct.
-      WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames));
+      WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
   }
 }
 
 /// EmitHookDeclarations - Parse CmdLine fields of all the tool
 /// property records and emit hook function declaration for each
 /// instance of $CALL(HookName).
-void EmitHookDeclarations(const ToolDescriptions& ToolDescs, raw_ostream& O) {
-  llvm::StringMap<unsigned> HookNames;
+void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
+                          const OptionDescriptions& OptDescs, raw_ostream& O) {
+  HookInfoMap HookNames;
 
-  FillInHookNames(ToolDescs, HookNames);
+  FillInHookNames(ToolDescs, OptDescs, HookNames);
   if (HookNames.empty())
     return;
 
   O << "namespace hooks {\n";
-  for (StringMap<unsigned>::const_iterator B = HookNames.begin(),
+  for (HookInfoMap::const_iterator B = HookNames.begin(),
          E = HookNames.end(); B != E; ++B) {
-    O.indent(Indent1) << "std::string " << B->first() << "(";
+    const char* HookName = B->first();
+    const HookInfo& Info = B->second;
 
-    for (unsigned i = 0, j = B->second; i < j; ++i) {
-      O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
+    O.indent(Indent1) << "std::string " << HookName << "(";
+
+    if (Info.Type == HookInfo::ArgHook) {
+      for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
+        O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
+      }
+    }
+    else {
+      O << "const std::vector<std::string>& Arg";
     }
 
     O <<");\n";
@@ -2031,6 +2709,8 @@ void EmitRegisterPlugin(int Priority, raw_ostream& O) {
   O << "struct Plugin : public llvmc::BasePlugin {\n\n";
   O.indent(Indent1) << "int Priority() const { return "
                     << Priority << "; }\n\n";
+  O.indent(Indent1) << "void PreprocessOptions() const\n";
+  O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
   O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
   O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
   O.indent(Indent1)
@@ -2043,15 +2723,18 @@ void EmitRegisterPlugin(int Priority, raw_ostream& O) {
 /// EmitIncludes - Emit necessary #include directives and some
 /// additional declarations.
 void EmitIncludes(raw_ostream& O) {
-  O << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
+  O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
+    << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
     << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
     << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
     << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
 
-    << "#include \"llvm/ADT/StringExtras.h\"\n"
-    << "#include \"llvm/Support/CommandLine.h\"\n\n"
+    << "#include \"llvm/Support/CommandLine.h\"\n"
+    << "#include \"llvm/Support/raw_ostream.h\"\n\n"
 
+    << "#include <algorithm>\n"
     << "#include <cstdlib>\n"
+    << "#include <iterator>\n"
     << "#include <stdexcept>\n\n"
 
     << "using namespace llvm;\n"
@@ -2145,12 +2828,15 @@ void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
   EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
 
   // Emit hook declarations.
-  EmitHookDeclarations(Data.ToolDescs, O);
+  EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
 
   O << "namespace {\n\n";
 
-  // Emit PopulateLanguageMap() function
-  // (a language map maps from file extensions to language names).
+  // Emit PreprocessOptionsLocal() function.
+  EmitPreprocessOptions(Records, Data.OptDescs, O);
+
+  // Emit PopulateLanguageMapLocal() function
+  // (language map maps from file extensions to language names).
   EmitPopulateLanguageMap(Records, O);
 
   // Emit Tool classes.
@@ -2161,7 +2847,7 @@ void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
   // Emit Edge# classes.
   EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
 
-  // Emit PopulateCompilationGraph() function.
+  // Emit PopulateCompilationGraphLocal() function.
   EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
 
   // Emit code for plugin registration.