[flang] Write function result to .mod file after dummy args
authorTim Keith <tkeith@nvidia.com>
Mon, 12 Aug 2019 20:06:59 +0000 (13:06 -0700)
committerTim Keith <tkeith@nvidia.com>
Tue, 13 Aug 2019 16:15:39 +0000 (09:15 -0700)
The function result can depend on the declaration of the dummy
arguments so it should be written to the .mod file after them.

For example:
```
  function f(x)
    integer :: x(:)
    integer :: f(size(x))
  end
```

Original-commit: flang-compiler/f18@f6c8c58c24368ae9f209b2c03637531ed869adeb
Reviewed-on: https://github.com/flang-compiler/f18/pull/650
Tree-same-pre-rewrite: false

flang/lib/semantics/mod-file.cc
flang/test/semantics/CMakeLists.txt
flang/test/semantics/modfile04.f90
flang/test/semantics/modfile06.f90
flang/test/semantics/modfile07.f90
flang/test/semantics/modfile30.f90 [new file with mode: 0644]

index c9a8f16..114870d 100644 (file)
@@ -786,12 +786,12 @@ static std::string ModFilePath(const std::string &dir, const SourceName &name,
 void SubprogramSymbolCollector::Collect() {
   const auto &details{symbol_.get<SubprogramDetails>()};
   isInterface_ = details.isInterface();
-  if (details.isFunction()) {
-    DoSymbol(details.result());
-  }
   for (const Symbol *dummyArg : details.dummyArgs()) {
     DoSymbol(DEREF(dummyArg));
   }
+  if (details.isFunction()) {
+    DoSymbol(details.result());
+  }
   for (const auto &pair : scope_) {
     const Symbol *symbol{pair.second};
     if (const auto *useDetails{symbol->detailsIf<UseDetails>()}) {
index f79286f..c783d1a 100644 (file)
@@ -211,6 +211,7 @@ set(MODFILE_TESTS
   modfile27.f90
   modfile28.f90
   modfile29.f90
+  modfile30.f90
 )
 
 set(LABEL_TESTS
index bc59997..d4cb0fc 100644 (file)
@@ -65,8 +65,8 @@ end
 !real(4)::x
 !end
 !function f2(y)
-!real(4)::f2
 !complex(4)::y
+!real(4)::f2
 !end
 !end
 
@@ -75,12 +75,12 @@ end
 !contains
 !function f3(x)
 ! use m1,only:t
-! type(t)::f3
 ! type::t2(b)
 !  integer(4),kind::b=2_4
 !  integer(4)::y
 ! end type
 ! type(t2(b=2_4))::x
+! type(t)::f3
 !end
 !function f4() result(x)
 !complex(4)::x
index 99b20e9..45a0484 100644 (file)
@@ -1,4 +1,4 @@
-! Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
+! Copyright (c) 2018-2019, NVIDIA CORPORATION.  All rights reserved.
 !
 ! Licensed under the Apache License, Version 2.0 (the "License");
 ! you may not use this file except in compliance with the License.
@@ -28,8 +28,8 @@ end
 !module m
 ! interface
 !  function f(x)
-!   integer(4)::f
 !   real(4)::x
+!   integer(4)::f
 !  end
 ! end interface
 ! interface
index 9e336f2..dde8eff 100644 (file)
@@ -51,16 +51,16 @@ end
 ! end interface
 ! interface
 !  function s1(x,y)
-!   real(4)::s1
 !   real(4)::x
 !   real(4)::y
+!   real(4)::s1
 !  end
 ! end interface
 ! interface
 !  function s2(x,y)
-!   complex(4)::s2
 !   complex(4)::x
 !   complex(4)::y
+!   complex(4)::s2
 !  end
 ! end interface
 ! interface operator(+)
@@ -81,14 +81,14 @@ end
 ! end interface
 !contains
 ! function s3(x,y)
-!  logical(4)::s3
 !  logical(4)::x
 !  logical(4)::y
+!  logical(4)::s3
 ! end
 ! function s4(x,y)
-!  integer(4)::s4
 !  integer(4)::x
 !  integer(4)::y
+!  integer(4)::s4
 ! end
 !end
 
@@ -158,11 +158,11 @@ end
 !  subroutine s1(f)
 !   interface
 !    function f(x)
-!     real(4)::f
 !     interface
 !      subroutine x()
 !      end
 !     end interface
+!     real(4)::f
 !    end
 !   end interface
 !  end
@@ -194,8 +194,8 @@ end
 ! end interface
 ! interface
 !  function f(x)
-!   integer(4)::f
 !   real(4)::x
+!   integer(4)::f
 !  end
 ! end interface
 !end
@@ -231,8 +231,8 @@ end
 ! end interface
 ! interface
 !  function f(x)
-!   integer(4)::f
 !   real(4)::x
+!   integer(4)::f
 !  end
 ! end interface
 !end
diff --git a/flang/test/semantics/modfile30.f90 b/flang/test/semantics/modfile30.f90
new file mode 100644 (file)
index 0000000..cf6fd53
--- /dev/null
@@ -0,0 +1,41 @@
+! Copyright (c) 2019, NVIDIA CORPORATION.  All rights reserved.
+!
+! Licensed under the Apache License, Version 2.0 (the "License");
+! you may not use this file except in compliance with the License.
+! You may obtain a copy of the License at
+!
+!     http://www.apache.org/licenses/LICENSE-2.0
+!
+! Unless required by applicable law or agreed to in writing, software
+! distributed under the License is distributed on an "AS IS" BASIS,
+! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+! See the License for the specific language governing permissions and
+! limitations under the License.
+
+! Verify miscellaneous bugs
+
+! The function result must be declared after the dummy arguments
+module m1
+contains
+  function f1(x) result(y)
+    integer :: x(:)
+    integer :: y(size(x))
+  end
+  function f2(x)
+    integer :: x(:)
+    integer :: f2(size(x))
+  end
+end
+
+!Expect: m1.mod
+!module m1
+!contains
+! function f1(x) result(y)
+!  integer(4)::x(:)
+!  integer(4)::y(1_8:1_8*size(x,dim=1))
+! end
+! function f2(x)
+!  integer(4)::x(:)
+!  integer(4)::f2(1_8:1_8*size(x,dim=1))
+! end
+!end