mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-17 03:36:37 +00:00

Fortran's syntax is ambiguous for some assignment statements (to array elements or to the targets of pointers returned by functions) that appear as the first executable statements in a subprogram or BLOCK construct. Is A(I)=X a statement function definition at the end of the specification part, or ar array element assignment statement, or an assignment to a pointer returned by a function named A? Since f18 builds a parse tree for the entire source file before beginning any semantic analysis, we can't tell which is which until after name resolution, at which point the symbol table has been built. So we have to walk the parse tree and rewrite some misparsed statement function definitions that really were assignment statements. There's a bug in that code, though, due to the fact that the implementation used state in the parse tree walker to hold a list of misparsed statement function definitions extracted from one specification part to be reinserted at the beginning of the next execution part that is visited; it didn't work for misparsed cases BLOCK constructs. Their parse tree nodes encapsulate a parser::Block, not an instance of the wrapper class parser::ExecutionPart. So misparsed statement functions in BLOCK constructs were being rewritten into assignment statement that were inserted at the beginning of the executable part of the following subprogram, if and wherever one happened to occur. This led to crashes in lowering and much astonishment. A simple fix would have been to adjust the rewriting code to always insert the list at the next visited parser::Block, since parser::ExecutionPart is just a wrapper around Block anyway; but this patch goes further to do the "right thing", which is a restructuring of the rewrite that avoids the use of state and any assumptions about parse tree walking visitation order. Fixes https://github.com/llvm/llvm-project/issues/112549.
51 lines
798 B
Fortran
51 lines
798 B
Fortran
!RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s
|
|
!Test rewriting of misparsed statement function definitions
|
|
!into array element assignment statements.
|
|
|
|
program main
|
|
real sf(1)
|
|
integer :: j = 1
|
|
!CHECK: sf(int(j,kind=8))=1._4
|
|
sf(j) = 1.
|
|
end
|
|
|
|
function func
|
|
real sf(1)
|
|
integer :: j = 1
|
|
!CHECK: sf(int(j,kind=8))=2._4
|
|
sf(j) = 2.
|
|
func = 0.
|
|
end
|
|
|
|
subroutine subr
|
|
real sf(1)
|
|
integer :: j = 1
|
|
!CHECK: sf(int(j,kind=8))=3._4
|
|
sf(j) = 3.
|
|
end
|
|
|
|
module m
|
|
interface
|
|
module subroutine smp
|
|
end
|
|
end interface
|
|
end
|
|
submodule(m) sm
|
|
contains
|
|
module procedure smp
|
|
real sf(1)
|
|
integer :: j = 1
|
|
!CHECK: sf(int(j,kind=8))=4._4
|
|
sf(j) = 4.
|
|
end
|
|
end
|
|
|
|
subroutine block
|
|
block
|
|
real sf(1)
|
|
integer :: j = 1
|
|
!CHECK: sf(int(j,kind=8))=5._4
|
|
sf(j) = 5.
|
|
end block
|
|
end
|