A small refactoring (extract method) + some comment fixes.
[oota-llvm.git] / utils / TableGen / LLVMCConfigurationEmitter.cpp
index 5a7bf67c0a0f07766fac0c0c80162e4b8959d9cb..7330dd3fc592737fa5d6d4fb5fb6e8e37390bc21 100644 (file)
@@ -38,16 +38,16 @@ typedef std::vector<std::string> StrVector;
 //===----------------------------------------------------------------------===//
 /// Constants
 
-// Indentation strings
+// Indentation strings.
 const char * Indent1 = "    ";
 const char * Indent2 = "        ";
 const char * Indent3 = "            ";
 const char * Indent4 = "                ";
 
-// Default help string
+// Default help string.
 const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
 
-// Name for the "sink" option
+// Name for the "sink" option.
 const char * SinkOptionName = "AutoGeneratedSinkOption";
 
 //===----------------------------------------------------------------------===//
@@ -69,8 +69,8 @@ const DagInit& InitPtrToDagInitRef(Init* ptr) {
 }
 
 
-// Ensure that the number of args in d is <= min_arguments,
-// throw exception otherwise
+// checkNumberOfArguments - Ensure that the number of args in d is
+// less than or equal to min_arguments, otherwise throw an exception .
 void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
   if (d->getNumArgs() < min_arguments)
     throw "Property " + d->getOperator()->getAsString()
@@ -111,8 +111,7 @@ bool IsListOptionType (OptionType::OptionType t) {
 // the option registration code, while ToolOptionDescriptions are used
 // to generate tool-specific code.
 
-// Base class for option descriptions
-
+/// OptionDescription - Base class for option descriptions.
 struct OptionDescription {
   OptionType::OptionType Type;
   std::string Name;
@@ -154,7 +153,7 @@ struct OptionDescription {
 
 };
 
-// Global option description
+// Global option description.
 
 namespace GlobalOptionDescriptionFlags {
   enum GlobalOptionDescriptionFlags { Required = 0x1 };
@@ -164,8 +163,8 @@ struct GlobalOptionDescription : public OptionDescription {
   std::string Help;
   unsigned Flags;
 
-  // We need t provide a default constructor since
-  // StringMap can only store DefaultConstructible objects
+  // We need to provide a default constructor because
+  // StringMap can only store DefaultConstructible objects.
   GlobalOptionDescription() : OptionDescription(), Flags(0)
   {}
 
@@ -180,7 +179,7 @@ struct GlobalOptionDescription : public OptionDescription {
     Flags |= GlobalOptionDescriptionFlags::Required;
   }
 
-  // Merge two option descriptions
+  /// Merge - Merge two option descriptions.
   void Merge (const GlobalOptionDescription& other)
   {
     if (other.Type != Type)
@@ -197,15 +196,16 @@ struct GlobalOptionDescription : public OptionDescription {
   }
 };
 
-// A GlobalOptionDescription array
-// + some flags affecting generation of option declarations
+/// GlobalOptionDescriptions - A GlobalOptionDescription array
+/// together with some flags affecting generation of option
+/// declarations.
 struct GlobalOptionDescriptions {
   typedef StringMap<GlobalOptionDescription> container_type;
   typedef container_type::const_iterator const_iterator;
 
-  // A list of GlobalOptionDescriptions
+  /// Descriptions - A list of GlobalOptionDescriptions.
   container_type Descriptions;
-  // Should the emitter generate a "cl::sink" option?
+  /// HasSink - Should the emitter generate a "cl::sink" option?
   bool HasSink;
 
   const GlobalOptionDescription& FindOption(const std::string& OptName) const {
@@ -222,9 +222,9 @@ struct GlobalOptionDescriptions {
 };
 
 
-// Tool-local option description
+// Tool-local option description.
 
-// Properties without arguments are implemented as flags
+// Properties without arguments are implemented as flags.
 namespace ToolOptionDescriptionFlags {
   enum ToolOptionDescriptionFlags { StopCompilation = 0x1,
                                     Forward = 0x2, UnpackValues = 0x4};
@@ -307,31 +307,34 @@ struct ToolProperties : public RefCountedBase<ToolProperties> {
 };
 
 
-// A list of Tool information records
-// IntrusiveRefCntPtrs are used because StringMap has no copy constructor
-// (and we want to avoid copying ToolProperties anyway)
+/// ToolPropertiesList - A list of Tool information records
+/// IntrusiveRefCntPtrs are used here because StringMap has no copy
+/// constructor (and we want to avoid copying ToolProperties anyway).
 typedef std::vector<IntrusiveRefCntPtr<ToolProperties> > ToolPropertiesList;
 
 
-// Function object for iterating over a list of tool property records
+/// CollectProperties - Function object for iterating over a list of
+/// tool property records.
 class CollectProperties {
 private:
 
   /// Implementation details
 
-  // "Property handler" - a function that extracts information
-  // about a given tool property from its DAG representation
+  /// PropertyHandler - a function that extracts information
+  /// about a given tool property from its DAG representation
   typedef void (CollectProperties::*PropertyHandler)(const DagInit*);
 
-  // Map from property names -> property handlers
+  /// PropertyHandlerMap - A map from property names to property
+  /// handlers.
   typedef StringMap<PropertyHandler> PropertyHandlerMap;
 
-  // "Option property handler" - a function that extracts information
-  // about a given option property from its DAG representation
+  /// OptionPropertyHandler - a function that extracts information
+  /// about a given option property from its DAG representation.
   typedef void (CollectProperties::* OptionPropertyHandler)
   (const DagInit*, GlobalOptionDescription &);
 
-  // Map from option property names -> option property handlers
+  /// OptionPropertyHandlerMap - A map from option property names to
+  /// option property handlers
   typedef StringMap<OptionPropertyHandler> OptionPropertyHandlerMap;
 
   // Static maps from strings to CollectProperties methods("handlers")
@@ -342,9 +345,10 @@ private:
 
   /// This is where the information is stored
 
-  // Current Tool properties
+  /// toolProps_ -  Properties of the current Tool.
   ToolProperties& toolProps_;
-  // OptionDescriptions table(used to register options globally)
+  /// optDescs_ - OptionDescriptions table (used to register options
+  /// globally).
   GlobalOptionDescriptions& optDescs_;
 
 public:
@@ -383,8 +387,8 @@ public:
     }
   }
 
-  // Gets called for every tool property;
-  // Just forwards to the corresponding property handler.
+  /// operator() - Gets called for every tool property; Just forwards
+  /// to the corresponding property handler.
   void operator() (Init* i) {
     const DagInit& d = InitPtrToDagInitRef(i);
     const std::string& property_name = d.getOperator()->getAsString();
@@ -525,12 +529,12 @@ private:
     }
   }
 
-  // Go through the list of option properties and call a corresponding
-  // handler for each.
-  //
-  // Parameters:
-  // name - option name
-  // d - option property list
+  /// processOptionProperties - Go through the list of option
+  /// properties and call a corresponding handler for each.
+  ///
+  /// Parameters:
+  /// name - option name
+  /// d - option property list
   void processOptionProperties (const DagInit* d, GlobalOptionDescription& o) {
     // First argument is option name
     checkNumberOfArguments(d, 2);
@@ -564,8 +568,9 @@ CollectProperties::optionPropertyHandlers_;
 bool CollectProperties::staticMembersInitialized_ = false;
 
 
-// Gather information from the parsed TableGen data
-// (Basically a wrapper for CollectProperties)
+/// CollectToolProperties - Gather information from the parsed
+/// TableGen data (basically a wrapper for the CollectProperties
+/// function object).
 void CollectToolProperties (RecordVector::const_iterator B,
                             RecordVector::const_iterator E,
                             ToolPropertiesList& TPList,
@@ -585,12 +590,50 @@ void CollectToolProperties (RecordVector::const_iterator B,
   }
 }
 
-// Used by EmitGenerateActionMethod
+/// EmitForwardOptionPropertyHandlingCode - Helper function used to
+/// implement EmitOptionPropertyHandlingCode(). Emits code for
+/// handling the (forward) option property.
+void EmitForwardOptionPropertyHandlingCode (const ToolOptionDescription& D,
+                                            std::ostream& O) {
+  switch (D.Type) {
+  case OptionType::Switch:
+    O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n";
+    break;
+  case OptionType::Parameter:
+    O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n";
+    O << Indent3 << "vec.push_back(" << D.GenVariableName() << ");\n";
+    break;
+  case OptionType::Prefix:
+    O << Indent3 << "vec.push_back(\"-" << D.Name << "\" + "
+      << D.GenVariableName() << ");\n";
+    break;
+  case OptionType::PrefixList:
+    O << Indent3 << "for (" << D.GenTypeDeclaration()
+      << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
+      << Indent3 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n"
+      << Indent4 << "vec.push_back(\"-" << D.Name << "\" + "
+      << "*B);\n";
+    break;
+  case OptionType::ParameterList:
+    O << Indent3 << "for (" << D.GenTypeDeclaration()
+      << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
+      << Indent3 << "E = " << D.GenVariableName()
+      << ".end() ; B != E; ++B) {\n"
+      << Indent4 << "vec.push_back(\"-" << D.Name << "\");\n"
+      << Indent4 << "vec.push_back(*B);\n"
+      << Indent3 << "}\n";
+    break;
+  }
+}
+
+/// EmitOptionPropertyHandlingCode - Helper function used by
+/// EmitGenerateActionMethod(). Emits code that handles option
+/// properties.
 void EmitOptionPropertyHandlingCode (const ToolProperties& P,
                                      const ToolOptionDescription& D,
                                      std::ostream& O)
 {
-  // if clause
+  // Start of the if-clause.
   O << Indent2 << "if (";
   if (D.Type == OptionType::Switch)
     O << D.GenVariableName();
@@ -599,7 +642,7 @@ void EmitOptionPropertyHandlingCode (const ToolProperties& P,
 
   O <<") {\n";
 
-  // Handle option properties that take an argument
+  // Handle option properties that take an argument.
   for (OptionPropertyList::const_iterator B = D.Props.begin(),
         E = D.Props.end(); B!=E; ++B) {
     const OptionProperty& val = *B;
@@ -618,37 +661,8 @@ void EmitOptionPropertyHandlingCode (const ToolProperties& P,
   // Handle flags
 
   // (forward) property
-  if (D.isForward()) {
-    switch (D.Type) {
-    case OptionType::Switch:
-      O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n";
-      break;
-    case OptionType::Parameter:
-      O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n";
-      O << Indent3 << "vec.push_back(" << D.GenVariableName() << ");\n";
-      break;
-    case OptionType::Prefix:
-      O << Indent3 << "vec.push_back(\"-" << D.Name << "\" + "
-        << D.GenVariableName() << ");\n";
-      break;
-    case OptionType::PrefixList:
-      O << Indent3 << "for (" << D.GenTypeDeclaration()
-        << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
-        << Indent3 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n"
-        << Indent4 << "vec.push_back(\"-" << D.Name << "\" + "
-        << "*B);\n";
-      break;
-    case OptionType::ParameterList:
-      O << Indent3 << "for (" << D.GenTypeDeclaration()
-        << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
-        << Indent3 << "E = " << D.GenVariableName()
-        << ".end() ; B != E; ++B) {\n"
-        << Indent4 << "vec.push_back(\"-" << D.Name << "\");\n"
-        << Indent4 << "vec.push_back(*B);\n"
-        << Indent3 << "}\n";
-      break;
-    }
-  }
+  if (D.isForward())
+    HandleForwardPropertyy(O);
 
   // (unpack_values) property
   if (D.isUnpackValues()) {
@@ -669,15 +683,15 @@ void EmitOptionPropertyHandlingCode (const ToolProperties& P,
     }
   }
 
-  // close if clause
+  // End of the if-clause.
   O << Indent2 << "}\n";
 }
 
-// Emite one of two versions of GenerateAction method
-void EmitGenerateActionMethod (const ToolProperties& P, int V, std::ostream& O)
+// EmitGenerateActionMethod - Emit one of two versions of the
+// Tool::GenerateAction() method.
+void EmitGenerateActionMethod (const ToolProperties& P, bool V, std::ostream& O)
 {
-  assert(V==1 || V==2);
-  if (V==1)
+  if (V)
     O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n";
   else
     O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n";
@@ -696,7 +710,7 @@ void EmitGenerateActionMethod (const ToolProperties& P, int V, std::ostream& O)
     const std::string& cmd = *I;
     O << Indent2;
     if (cmd == "$INFILE") {
-      if (V==1)
+      if (V)
         O << "for (PathVector::const_iterator B = inFiles.begin()"
           << ", E = inFiles.end();\n"
           << Indent2 << "B != E; ++B)\n"
@@ -712,14 +726,14 @@ void EmitGenerateActionMethod (const ToolProperties& P, int V, std::ostream& O)
     }
   }
 
-  // For every understood option, emit handling code
+  // For every understood option, emit handling code.
   for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
         E = P.OptDescs.end(); B != E; ++B) {
     const ToolOptionDescription& val = B->second;
     EmitOptionPropertyHandlingCode(P, val, O);
   }
 
-  // Handle Sink property
+  // Handle the Sink property.
   if (P.isSink()) {
     O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n"
       << Indent3 << "vec.insert(vec.end(), "
@@ -731,7 +745,8 @@ void EmitGenerateActionMethod (const ToolProperties& P, int V, std::ostream& O)
     << Indent1 << "}\n\n";
 }
 
-// Emit GenerateAction methods for Tool classes
+/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
+/// a given Tool class.
 void EmitGenerateActionMethods (const ToolProperties& P, std::ostream& O) {
 
   if (!P.isJoin())
@@ -747,7 +762,8 @@ void EmitGenerateActionMethods (const ToolProperties& P, std::ostream& O) {
   EmitGenerateActionMethod(P, 2, O);
 }
 
-// Emit IsLast() method for Tool classes
+/// EmitIsLastMethod - Emit the IsLast() method for a given Tool
+/// class.
 void EmitIsLastMethod (const ToolProperties& P, std::ostream& O) {
   O << Indent1 << "bool IsLast() const {\n"
     << Indent2 << "bool last = false;\n";
@@ -766,7 +782,8 @@ void EmitIsLastMethod (const ToolProperties& P, std::ostream& O) {
     << Indent1 <<  "}\n\n";
 }
 
-// Emit static [Input,Output]Language() methods for Tool classes
+/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
+/// methods for a given Tool class.
 void EmitInOutLanguageMethods (const ToolProperties& P, std::ostream& O) {
   O << Indent1 << "const char* InputLanguage() const {\n"
     << Indent2 << "return \"" << P.InLanguage << "\";\n"
@@ -777,21 +794,23 @@ void EmitInOutLanguageMethods (const ToolProperties& P, std::ostream& O) {
     << Indent1 << "}\n\n";
 }
 
-// Emit static [Input,Output]Language() methods for Tool classes
+/// EmitOutputSuffixMethod - Emit the OutputSuffix() method for a
+/// given Tool class.
 void EmitOutputSuffixMethod (const ToolProperties& P, std::ostream& O) {
   O << Indent1 << "const char* OutputSuffix() const {\n"
     << Indent2 << "return \"" << P.OutputSuffix << "\";\n"
     << Indent1 << "}\n\n";
 }
 
-// Emit static Name() method for Tool classes
+/// EmitNameMethod - Emit the Name() method for a given Tool class.
 void EmitNameMethod (const ToolProperties& P, std::ostream& O) {
   O << Indent1 << "const char* Name() const {\n"
     << Indent2 << "return \"" << P.Name << "\";\n"
     << Indent1 << "}\n\n";
 }
 
-// Emit static Name() method for Tool classes
+/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
+/// class.
 void EmitIsJoinMethod (const ToolProperties& P, std::ostream& O) {
   O << Indent1 << "bool IsJoin() const {\n";
   if (P.isJoin())
@@ -801,7 +820,7 @@ void EmitIsJoinMethod (const ToolProperties& P, std::ostream& O) {
   O << Indent1 << "}\n\n";
 }
 
-// Emit a Tool class definition
+/// EmitToolClassDefinition - Emit a Tool class definition.
 void EmitToolClassDefinition (const ToolProperties& P, std::ostream& O) {
 
   if(P.Name == "root")
@@ -826,7 +845,8 @@ void EmitToolClassDefinition (const ToolProperties& P, std::ostream& O) {
   O << "};\n\n";
 }
 
-// Iterate over a list of option descriptions and emit registration code
+/// EmitOptionDescriptions - Iterate over a list of option
+/// descriptions and emit registration code.
 void EmitOptionDescriptions (const GlobalOptionDescriptions& descs,
                              std::ostream& O)
 {
@@ -862,6 +882,7 @@ void EmitOptionDescriptions (const GlobalOptionDescriptions& descs,
   O << '\n';
 }
 
+/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
 void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O)
 {
   // Get the relevant field out of RecordKeeper
@@ -891,8 +912,8 @@ void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O)
   O << "}\n\n";
 }
 
-// Fills in two tables that map tool names to (input, output) languages.
-// Used by the typechecker.
+/// FillInToolToLang - Fills in two tables that map tool names to
+/// (input, output) languages.  Used by the typechecker.
 void FillInToolToLang (const ToolPropertiesList& TPList,
                        StringMap<std::string>& ToolToInLang,
                        StringMap<std::string>& ToolToOutLang) {
@@ -904,7 +925,8 @@ void FillInToolToLang (const ToolPropertiesList& TPList,
   }
 }
 
-// Check that all output and input language names match.
+/// TypecheckGraph - Check that names for output and input languages
+/// on all edges do match.
 // TOFIX: check for cycles.
 // TOFIX: check for multiple default edges.
 void TypecheckGraph (Record* CompilationGraph,
@@ -935,7 +957,8 @@ void TypecheckGraph (Record* CompilationGraph,
   }
 }
 
-// Helper function used by EmitEdgePropertyTest.
+/// EmitEdgePropertyTest1Arg - Helper function used by
+/// EmitEdgePropertyTest.
 bool EmitEdgePropertyTest1Arg(const std::string& PropName,
                               const DagInit& Prop,
                               const GlobalOptionDescriptions& OptDescs,
@@ -948,8 +971,7 @@ bool EmitEdgePropertyTest1Arg(const std::string& PropName,
       throw OptName + ": incorrect option type!";
     O << OptDesc.GenVariableName();
     return true;
-  }
-  else if (PropName == "if_input_languages_contain") {
+  } else if (PropName == "if_input_languages_contain") {
     O << "InLangs.count(\"" << OptName << "\") != 0";
     return true;
   }
@@ -957,7 +979,8 @@ bool EmitEdgePropertyTest1Arg(const std::string& PropName,
   return false;
 }
 
-// Helper function used by EmitEdgePropertyTest.
+/// EmitEdgePropertyTest2Args - Helper function used by
+/// EmitEdgePropertyTest.
 bool EmitEdgePropertyTest2Args(const std::string& PropName,
                                const DagInit& Prop,
                                const GlobalOptionDescriptions& OptDescs,
@@ -993,7 +1016,8 @@ void EmitEdgePropertyTest(const DagInit& Prop,
                           const GlobalOptionDescriptions& OptDescs,
                           std::ostream& O);
 
-// Helper function used by EmitEdgeClass.
+/// EmitLogicalOperationTest - Helper function used by
+/// EmitEdgePropertyTest.
 void EmitLogicalOperationTest(const DagInit& Prop, const char* LogicOp,
                               const GlobalOptionDescriptions& OptDescs,
                               std::ostream& O) {
@@ -1008,7 +1032,7 @@ void EmitLogicalOperationTest(const DagInit& Prop, const char* LogicOp,
   }
 }
 
-// Helper function used by EmitEdgeClass.
+/// EmitEdgePropertyTest - Helper function used by EmitEdgeClass.
 void EmitEdgePropertyTest(const DagInit& Prop,
                           const GlobalOptionDescriptions& OptDescs,
                           std::ostream& O) {
@@ -1026,7 +1050,7 @@ void EmitEdgePropertyTest(const DagInit& Prop,
     throw PropName + ": unknown edge property!";
 }
 
-// Emit a single Edge* class.
+/// EmitEdgeClass - Emit a single Edge# class.
 void EmitEdgeClass(unsigned N, const std::string& Target,
                    ListInit* Props, const GlobalOptionDescriptions& OptDescs,
                    std::ostream& O) {
@@ -1084,6 +1108,8 @@ void EmitEdgeClasses (Record* CompilationGraph,
   }
 }
 
+/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
+/// function.
 void EmitPopulateCompilationGraph (Record* CompilationGraph,
                                    std::ostream& O)
 {
@@ -1132,29 +1158,30 @@ void EmitPopulateCompilationGraph (Record* CompilationGraph,
 // End of anonymous namespace
 }
 
-// Back-end entry point
+/// run - The back-end entry point.
 void LLVMCConfigurationEmitter::run (std::ostream &O) {
-  // Emit file header
+
+  // Emit file header.
   EmitSourceFileHeader("LLVMC Configuration Library", O);
 
-  // Get a list of all defined Tools
+  // Get a list of all defined Tools.
   RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
   if (Tools.empty())
     throw std::string("No tool definitions found!");
 
-  // Gather information from the Tool descriptions
+  // Gather information from the Tool description dags.
   ToolPropertiesList tool_props;
   GlobalOptionDescriptions opt_descs;
   CollectToolProperties(Tools.begin(), Tools.end(), tool_props, opt_descs);
 
-  // Emit global option registration code
+  // Emit global option registration code.
   EmitOptionDescriptions(opt_descs, O);
 
-  // Emit PopulateLanguageMap function
-  // (a language map maps from file extensions to language names)
+  // Emit PopulateLanguageMap() function
+  // (a language map maps from file extensions to language names).
   EmitPopulateLanguageMap(Records, O);
 
-  // Emit Tool classes
+  // Emit Tool classes.
   for (ToolPropertiesList::const_iterator B = tool_props.begin(),
          E = tool_props.end(); B!=E; ++B)
     EmitToolClassDefinition(*(*B), O);
@@ -1166,10 +1193,10 @@ void LLVMCConfigurationEmitter::run (std::ostream &O) {
   // Typecheck the compilation graph.
   TypecheckGraph(CompilationGraphRecord, tool_props);
 
-  // Emit Edge* classes.
+  // Emit Edge# classes.
   EmitEdgeClasses(CompilationGraphRecord, opt_descs, O);
 
-  // Emit PopulateCompilationGraph function
+  // Emit PopulateCompilationGraph() function.
   EmitPopulateCompilationGraph(CompilationGraphRecord, O);
 
   // EOF