[flang] Don't reference non-invariant symbols in shape expressions
authorPeter Klausler <pklausler@nvidia.com>
Mon, 1 Nov 2021 23:24:01 +0000 (16:24 -0700)
committerPeter Klausler <pklausler@nvidia.com>
Mon, 8 Nov 2021 20:48:47 +0000 (12:48 -0800)
commitca47447952f1f8b0de11aac75b45f83f88579b80
tree1d4119254a8f6b82c81c11c5695d7ed26a355c06
parentfae440974a690535a048aeefd53579cfb6a1b459
[flang] Don't reference non-invariant symbols in shape expressions

When an array's shape involves references to symbols that are not
invariant in a scope -- the classic example being a dummy array
with an explicit shape involving other dummy arguments -- the
compiler was creating shape expressions that referenced those
symbols.  This might be valid if those symbols are somehow
captured and copied at each entry point to a subprogram, and
the copies referenced in the shapes instead, but that's not
the case.

This patch introduces a new expression predicate IsScopeInvariantExpr(),
which defines a class of expressions that contains constant expressions
(in the sense that the standard uses that term) as well as references
to items that may be safely accessed in a context-free way throughout
their scopes.   This includes dummy arguments that are INTENT(IN)
and not VALUE, descriptor inquiries into descriptors that cannot
change, and bare LEN type parameters within the definitions of
derived types.  The new predicate is then used in shape analysis
to winnow out results that would have otherwise been contextual.

Differential Revision: https://reviews.llvm.org/D113309
flang/include/flang/Evaluate/check-expression.h
flang/lib/Evaluate/check-expression.cpp
flang/lib/Evaluate/shape.cpp
flang/test/Semantics/modfile33.f90
flang/test/Semantics/offsets01.f90