[flang] Move ConvertToAssignment into class StmtFunctionStmt.
authorTim Keith <tkeith@nvidia.com>
Thu, 19 Apr 2018 14:07:47 +0000 (07:07 -0700)
committerTim Keith <tkeith@nvidia.com>
Thu, 19 Apr 2018 14:07:47 +0000 (07:07 -0700)
Original-commit: flang-compiler/f18@e3eaca38fd0c30765474c2a46ae4d6c36796af07
Reviewed-on: https://github.com/flang-compiler/f18/pull/60

flang/lib/parser/parse-tree.cc
flang/lib/parser/parse-tree.h
flang/lib/semantics/rewrite-parse-tree.cc

index 53037a7..8eaa82d 100644 (file)
@@ -46,5 +46,24 @@ DataRef::DataRef(std::list<PartRef> &&prl) : u{std::move(prl.front().name)} {
 Expr::Expr(Designator &&x) : u{Indirection<Designator>(std::move(x))} {}
 Expr::Expr(FunctionReference &&x)
   : u{Indirection<FunctionReference>(std::move(x))} {}
+
+// R1544 stmt-function-stmt
+// Convert this stmt-function-stmt to an array element assignment statement.
+Statement<ActionStmt> StmtFunctionStmt::ConvertToAssignment() {
+  auto &funcName = std::get<Name>(t);
+  auto &funcArgs = std::get<std::list<Name>>(t);
+  auto &funcExpr = std::get<Scalar<Expr>>(t).thing;
+  ArrayElement arrayElement{funcName, std::list<SectionSubscript>{}};
+  for (Name &arg : funcArgs) {
+    arrayElement.subscripts.push_back(SectionSubscript{
+        Scalar{Integer{Indirection{Expr{Indirection{Designator{arg}}}}}}});
+  }
+  auto &&variable = Variable{
+      Indirection{Designator{DataRef{Indirection{std::move(arrayElement)}}}}};
+  return Statement{std::nullopt,
+      ActionStmt{Indirection{
+          AssignmentStmt{std::move(variable), std::move(funcExpr)}}}};
+}
+
 }  // namespace parser
 }  // namespace Fortran
index bcf5177..d0a2643 100644 (file)
@@ -3114,6 +3114,7 @@ WRAPPER_CLASS(ReturnStmt, std::optional<ScalarIntExpr>);
 struct StmtFunctionStmt {
   TUPLE_CLASS_BOILERPLATE(StmtFunctionStmt);
   std::tuple<Name, std::list<Name>, Scalar<Expr>> t;
+  Statement<ActionStmt> ConvertToAssignment();
 };
 
 // Compiler directives
index 62d9ec7..6eb526f 100644 (file)
@@ -53,7 +53,10 @@ public:
   bool Pre(parser::ExecutionPart &x) {
     auto origFirst = x.v.begin();  // insert each elem before origFirst
     for (stmtFuncType &sf : stmtFuncsToConvert) {
-      x.v.insert(origFirst, ConvertToAssignment(sf));
+      auto &&stmt = sf.statement->ConvertToAssignment();
+      stmt.source = sf.source;
+      x.v.insert(origFirst,
+          parser::ExecutionPartConstruct{parser::ExecutableConstruct{stmt}});
     }
     stmtFuncsToConvert.clear();
     return true;
@@ -62,29 +65,6 @@ public:
 private:
   const symbolMap &symbols_;
   std::list<stmtFuncType> stmtFuncsToConvert;
-
-  // Convert a statement function statement to an ExecutionPartConstruct
-  // containing an array element assignment statement.
-  static parser::ExecutionPartConstruct ConvertToAssignment(stmtFuncType &x) {
-    parser::StmtFunctionStmt &sf{*x.statement};
-    auto &funcName = std::get<parser::Name>(sf.t);
-    auto &funcArgs = std::get<std::list<parser::Name>>(sf.t);
-    auto &funcExpr = std::get<parser::Scalar<parser::Expr>>(sf.t).thing;
-    parser::ArrayElement arrayElement{
-        funcName, std::list<parser::SectionSubscript>{}};
-    for (parser::Name &arg : funcArgs) {
-      arrayElement.subscripts.push_back(parser::SectionSubscript{
-          parser::Scalar{parser::Integer{parser::Indirection{
-              parser::Expr{parser::Indirection{parser::Designator{arg}}}}}}});
-    }
-    auto &&variable = parser::Variable{parser::Indirection{parser::Designator{
-        parser::DataRef{parser::Indirection{std::move(arrayElement)}}}}};
-    auto &&stmt = parser::Statement{std::nullopt,
-        parser::ActionStmt{parser::Indirection{
-            parser::AssignmentStmt{std::move(variable), std::move(funcExpr)}}}};
-    stmt.source = x.source;
-    return parser::ExecutionPartConstruct{parser::ExecutableConstruct{stmt}};
-  }
 };
 
 static void CollectSymbols(Scope &scope, symbolMap &symbols) {