[llvm-commits] [llvm] r51748 - /llvm/trunk/utils/TableGen/LLVMCConfigurationEmitter.cpp

Mikhail Glushenkov foldr at codedgers.com
Thu May 29 23:22:15 PDT 2008


Author: foldr
Date: Fri May 30 01:22:15 2008
New Revision: 51748

URL: http://llvm.org/viewvc/llvm-project?rev=51748&view=rev
Log:
Allow nesting of case expressions.

The following is now legal:

    (case (in_language "c"),
          (case (switch_on "E"), "gcc -x c -E $INFILE", (default), "gcc -x c $INFILE"),
          (default),
          "gcc $INFILE $OUTFILE")

Modified:
    llvm/trunk/utils/TableGen/LLVMCConfigurationEmitter.cpp

Modified: llvm/trunk/utils/TableGen/LLVMCConfigurationEmitter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/LLVMCConfigurationEmitter.cpp?rev=51748&r1=51747&r2=51748&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/LLVMCConfigurationEmitter.cpp (original)
+++ llvm/trunk/utils/TableGen/LLVMCConfigurationEmitter.cpp Fri May 30 01:22:15 2008
@@ -763,7 +763,7 @@
 // void F(Init* Statement, const char* IndentLevel, std::ostream& O).
 template <typename F>
 void EmitCaseConstructHandler(const DagInit* d, const char* IndentLevel,
-                              const F& Callback,
+                              const F& Callback, bool EmitElseIf,
                               const GlobalOptionDescriptions& OptDescs,
                               std::ostream& O) {
   assert(d->getOperator()->getAsString() == "case");
@@ -784,7 +784,7 @@
       O << IndentLevel << "else {\n";
     }
     else {
-      O << IndentLevel << "if (";
+      O << IndentLevel << ((i != 0 && EmitElseIf) ? "else if (" : "if (");
       EmitCaseTest(Test, IndentLevel, OptDescs, O);
       O << ") {\n";
     }
@@ -795,7 +795,16 @@
       throw "Case construct handler: no corresponding action "
         "found for the test " + Test.getAsString() + '!';
 
-    Callback(d->getArg(i), IndentLevel, O);
+    Init* arg = d->getArg(i);
+    if (dynamic_cast<DagInit*>(arg)
+        && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case") {
+      EmitCaseConstructHandler(static_cast<DagInit*>(arg),
+                               (std::string(IndentLevel) + Indent1).c_str(),
+                               Callback, EmitElseIf, OptDescs, O);
+    }
+    else {
+      Callback(arg, IndentLevel, O);
+    }
     O << IndentLevel << "}\n";
   }
 }
@@ -1031,7 +1040,7 @@
   else
     EmitCaseConstructHandler(&InitPtrToDag(P.CmdLine), Indent2,
                              EmitCmdLineVecFillCallback(Version, P.Name),
-                             OptDescs, O);
+                             true, OptDescs, O);
 
   // For every understood option, emit handling code.
   for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
@@ -1332,7 +1341,7 @@
     << Indent2 << "unsigned ret = 0;\n";
 
   // Handle the 'case' construct.
-  EmitCaseConstructHandler(Case, Indent2, IncDecWeight, OptDescs, O);
+  EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
 
   O << Indent2 << "return ret;\n"
     << Indent1 << "};\n\n};\n\n";
@@ -1420,30 +1429,40 @@
   }
 }
 
+/// ExtractHookNamesFromCaseConstruct - Extract hook names from the
+/// 'case' expression, handle nesting. Helper function used by
+/// FillInHookNames().
+void ExtractHookNamesFromCaseConstruct(Init* Case, StrVector& HookNames) {
+  const DagInit& d = InitPtrToDag(Case);
+  bool even = false;
+  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")
+      ExtractHookNamesFromCaseConstruct(arg, HookNames);
+    else if (even)
+      ExtractHookNames(arg, HookNames);
+    even = !even;
+  }
+}
+
 /// FillInHookNames - Actually extract the hook names from all command
 /// line strings. Helper function used by EmitHookDeclarations().
 void FillInHookNames(const ToolPropertiesList& TPList,
                      StrVector& HookNames) {
+  // For all command lines:
   for (ToolPropertiesList::const_iterator B = TPList.begin(),
          E = TPList.end(); B != E; ++B) {
     const ToolProperties& P = *(*B);
     if (!P.CmdLine)
       continue;
-    if (typeid(*P.CmdLine) == typeid(StringInit)) {
+    if (dynamic_cast<StringInit*>(P.CmdLine))
       // This is a string.
       ExtractHookNames(P.CmdLine, HookNames);
-    }
-    else {
+    else
       // This is a 'case' construct.
-      const DagInit& d = InitPtrToDag(P.CmdLine);
-      bool even = false;
-      for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
-           B != E; ++B) {
-        if (even)
-          ExtractHookNames(*B, HookNames);
-        even = !even;
-      }
-    }
+      ExtractHookNamesFromCaseConstruct(P.CmdLine, HookNames);
   }
 }
 





More information about the llvm-commits mailing list