Skip to content

Conversation

klausler
Copy link
Contributor

Move a function that seems to be running into an MSVC problem from the source file where I created it to another one (tools.cpp) that is already known to be able to access the semantics::Scope type.

Move a function that seems to be running into an MSVC problem
from the source file where I created it to another one (tools.cpp)
that is already known to be able to access the semantics::Scope type.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Sep 30, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 30, 2025

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

Move a function that seems to be running into an MSVC problem from the source file where I created it to another one (tools.cpp) that is already known to be able to access the semantics::Scope type.


Full diff: https://github.com/llvm/llvm-project/pull/161426.diff

3 Files Affected:

  • (modified) flang/include/flang/Evaluate/tools.h (+3)
  • (modified) flang/lib/Evaluate/constant.cpp (-28)
  • (modified) flang/lib/Evaluate/tools.cpp (+27)
diff --git a/flang/include/flang/Evaluate/tools.h b/flang/include/flang/Evaluate/tools.h
index 5f2f199e778c7..f9d74db1df03b 100644
--- a/flang/include/flang/Evaluate/tools.h
+++ b/flang/include/flang/Evaluate/tools.h
@@ -1521,6 +1521,9 @@ bool IsVarSubexpressionOf(
 // it returns std::nullopt.
 std::optional<Expr<SomeType>> GetConvertInput(const Expr<SomeType> &x);
 
+// How many ancestors does have a derived type have?
+std::optional<int> DerivedTypeDepth(const semantics::Scope &);
+
 } // namespace Fortran::evaluate
 
 namespace Fortran::semantics {
diff --git a/flang/lib/Evaluate/constant.cpp b/flang/lib/Evaluate/constant.cpp
index 8923ab114c737..f57dd825a7a7c 100644
--- a/flang/lib/Evaluate/constant.cpp
+++ b/flang/lib/Evaluate/constant.cpp
@@ -10,7 +10,6 @@
 #include "flang/Evaluate/expression.h"
 #include "flang/Evaluate/shape.h"
 #include "flang/Evaluate/type.h"
-#include "flang/Semantics/scope.h"
 #include <string>
 
 namespace Fortran::evaluate {
@@ -390,33 +389,6 @@ std::size_t Constant<SomeDerived>::CopyFrom(const Constant<SomeDerived> &source,
   return Base::CopyFrom(source, count, resultSubscripts, dimOrder);
 }
 
-static std::optional<int> DerivedTypeDepth(const semantics::Scope &scope) {
-  if (scope.IsDerivedType()) {
-    for (auto iter{scope.cbegin()}; iter != scope.cend(); ++iter) {
-      const Symbol &symbol{*iter->second};
-      if (symbol.test(Symbol::Flag::ParentComp)) {
-        if (const semantics::DeclTypeSpec *type{symbol.GetType()}) {
-          if (const semantics::DerivedTypeSpec *derived{type->AsDerived()}) {
-            const semantics::Scope *parent{derived->scope()};
-            if (!parent) {
-              parent = derived->typeSymbol().scope();
-            }
-            if (parent) {
-              if (auto parentDepth{DerivedTypeDepth(*parent)}) {
-                return 1 + *parentDepth;
-              }
-            }
-          }
-        }
-        return std::nullopt; // error recovery
-      }
-    }
-    return 0;
-  } else {
-    return std::nullopt; // error recovery
-  }
-}
-
 bool ComponentCompare::operator()(SymbolRef x, SymbolRef y) const {
   if (&x->owner() != &y->owner()) {
     // Not components of the same derived type; put ancestors' components first.
diff --git a/flang/lib/Evaluate/tools.cpp b/flang/lib/Evaluate/tools.cpp
index 1f3cbbf6a0c36..6d0da63ead07a 100644
--- a/flang/lib/Evaluate/tools.cpp
+++ b/flang/lib/Evaluate/tools.cpp
@@ -1950,6 +1950,33 @@ bool IsVarSubexpressionOf(
   return VariableFinder{sub}(super);
 }
 
+std::optional<int> DerivedTypeDepth(const semantics::Scope &scope) {
+  if (scope.IsDerivedType()) {
+    for (auto iter{scope.cbegin()}; iter != scope.cend(); ++iter) {
+      const Symbol &symbol{*iter->second};
+      if (symbol.test(Symbol::Flag::ParentComp)) {
+        if (const semantics::DeclTypeSpec *type{symbol.GetType()}) {
+          if (const semantics::DerivedTypeSpec *derived{type->AsDerived()}) {
+            const semantics::Scope *parent{derived->scope()};
+            if (!parent) {
+              parent = derived->typeSymbol().scope();
+            }
+            if (parent) {
+              if (auto parentDepth{DerivedTypeDepth(*parent)}) {
+                return 1 + *parentDepth;
+              }
+            }
+          }
+        }
+        return std::nullopt; // error recovery
+      }
+    }
+    return 0;
+  } else {
+    return std::nullopt; // error recovery
+  }
+}
+
 } // namespace Fortran::evaluate
 
 namespace Fortran::semantics {

@klausler klausler merged commit e27e9ca into llvm:main Sep 30, 2025
12 checks passed
@klausler klausler deleted the fix-msvc2 branch September 30, 2025 20:04
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
Move a function that seems to be running into an MSVC problem from the
source file where I created it to another one (tools.cpp) that is
already known to be able to access the semantics::Scope type.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants