[flang-commits] [flang] 7454acd - [flang] Fix implicit declarations in statement functions

Tim Keith via flang-commits flang-commits at lists.llvm.org
Sun Jul 26 12:14:04 PDT 2020


Author: Tim Keith
Date: 2020-07-26T12:13:39-07:00
New Revision: 7454acdf3b7d064ebbf6b8027296f42f504b285a

URL: https://github.com/llvm/llvm-project/commit/7454acdf3b7d064ebbf6b8027296f42f504b285a
DIFF: https://github.com/llvm/llvm-project/commit/7454acdf3b7d064ebbf6b8027296f42f504b285a.diff

LOG: [flang] Fix implicit declarations in statement functions

If a symbol (that is not a dummy argument) is implicitly declared inside
a statement function, don't create it in the statement function's scope.
Instead, treat statement functions like blocks when finding the inclusive
scope and create the symbol there.

Add a new flag, StmtFunction, to symbols that represent statement functions.

Differential Revision: https://reviews.llvm.org/D84588

Added: 
    

Modified: 
    flang/include/flang/Semantics/scope.h
    flang/include/flang/Semantics/symbol.h
    flang/lib/Semantics/resolve-names.cpp
    flang/lib/Semantics/scope.cpp
    flang/test/Semantics/symbol16.f90

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Semantics/scope.h b/flang/include/flang/Semantics/scope.h
index a67a00888927..5ebe5f32eb67 100644
--- a/flang/include/flang/Semantics/scope.h
+++ b/flang/include/flang/Semantics/scope.h
@@ -87,6 +87,7 @@ class Scope {
   bool IsModule() const; // only module, not submodule
   bool IsSubmodule() const;
   bool IsDerivedType() const { return kind_ == Kind::DerivedType; }
+  bool IsStmtFunction() const;
   bool IsParameterizedDerivedType() const;
   Symbol *symbol() { return symbol_; }
   const Symbol *symbol() const { return symbol_; }

diff  --git a/flang/include/flang/Semantics/symbol.h b/flang/include/flang/Semantics/symbol.h
index 227f951aef5f..a1fd1baef78d 100644
--- a/flang/include/flang/Semantics/symbol.h
+++ b/flang/include/flang/Semantics/symbol.h
@@ -482,6 +482,7 @@ class Symbol {
       Error, // an error has been reported on this symbol
       Function, // symbol is a function
       Subroutine, // symbol is a subroutine
+      StmtFunction, // symbol is a statement function (Function is set too)
       Implicit, // symbol is implicitly typed
       ModFile, // symbol came from .mod file
       ParentComp, // symbol is the "parent component" of an extended type

diff  --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 7189b4848265..8ea2e8ed9a60 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -2015,7 +2015,8 @@ void ScopeHandler::Say2(const parser::Name &name, MessageFixedText &&msg1,
 
 Scope &ScopeHandler::InclusiveScope() {
   for (auto *scope{&currScope()};; scope = &scope->parent()) {
-    if (scope->kind() != Scope::Kind::Block && !scope->IsDerivedType()) {
+    if (scope->kind() != Scope::Kind::Block && !scope->IsDerivedType() &&
+        !scope->IsStmtFunction()) {
       return *scope;
     }
   }
@@ -2692,6 +2693,7 @@ bool SubprogramVisitor::HandleStmtFunction(const parser::StmtFunctionStmt &x) {
     return true;
   }
   auto &symbol{PushSubprogramScope(name, Symbol::Flag::Function)};
+  symbol.set(Symbol::Flag::StmtFunction);
   EraseSymbol(symbol); // removes symbol added by PushSubprogramScope
   auto &details{symbol.get<SubprogramDetails>()};
   for (const auto &dummyName : std::get<std::list<parser::Name>>(x.t)) {

diff  --git a/flang/lib/Semantics/scope.cpp b/flang/lib/Semantics/scope.cpp
index 02637ba2add7..a2a9e1dbe9e7 100644
--- a/flang/lib/Semantics/scope.cpp
+++ b/flang/lib/Semantics/scope.cpp
@@ -333,6 +333,10 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Scope &scope) {
   return os;
 }
 
+bool Scope::IsStmtFunction() const {
+  return symbol_ && symbol_->test(Symbol::Flag::StmtFunction);
+}
+
 bool Scope::IsParameterizedDerivedType() const {
   if (!IsDerivedType()) {
     return false;

diff  --git a/flang/test/Semantics/symbol16.f90 b/flang/test/Semantics/symbol16.f90
index ce47134fc337..4fa6f2b9c0ea 100644
--- a/flang/test/Semantics/symbol16.f90
+++ b/flang/test/Semantics/symbol16.f90
@@ -3,7 +3,7 @@
 
 !DEF: /p1 MainProgram
 program p1
- !DEF: /p1/f (Function) Subprogram INTEGER(4)
+ !DEF: /p1/f (Function, StmtFunction) Subprogram INTEGER(4)
  !DEF: /p1/i ObjectEntity INTEGER(4)
  !DEF: /p1/j ObjectEntity INTEGER(4)
  integer f, i, j
@@ -15,3 +15,13 @@ program p1
  !REF: /p1/f
  j = f(2)
 end program
+
+!DEF: /p2 MainProgram
+program p2
+ !DEF: /p2/f (Function, StmtFunction) Subprogram REAL(4)
+ !DEF: /p2/f/x (Implicit) ObjectEntity REAL(4)
+ !DEF: /p2/y (Implicit) ObjectEntity REAL(4)
+ f(x) = y
+ !REF: /p2/y
+ y = 1.0
+end program


        


More information about the flang-commits mailing list