293 Commits

Author SHA1 Message Date
Peter Klausler
73b83be1c9
[flang] Add warning and documentation for extension (#179708)
When a pointer component has no default initialization in the derived
type definition and does not appear explicitly as a component in a
structure constructor, we assume that it is meant to be NULL() as an
extension. But there's no warning or documentation for this extension;
add them.

Fixes https://github.com/llvm/llvm-project/issues/179580.
2026-02-06 09:32:16 -08:00
Peter Klausler
9d0043a35f
[flang] Refactor "unused"/"used without definition" warnings (#179539)
I'm emitting a false warning "used without definition" warning for
variable cited in an inquiry intrinsic (e.g. LBOUND), and failing to
emit an "unused local" warning for allocatables that only appear in
ALLOCATE statements.

This patch makes the analysis more context-aware by rewriting it in
terms of typed expression traversal, and moves the analysis part into
Evaluate/check-expression.cpp from Semantics/expression.cpp.
2026-02-06 09:29:50 -08:00
Peter Klausler
6577aa54e8
[flang] Fix proc ptr default initializers in structure constructors (#178897)
The default initializers for procedure pointer components are not being
used for unspecified components in structure constructors.

Fixes https://github.com/llvm/llvm-project/issues/178813.
2026-01-31 14:48:32 -08:00
Peter Klausler
55ee00e15f
[flang][CUDA] Allow constant to match device actual in specific procedure (#178658)
When scanning the specific procedures of a generic interface for a match
for a set of actual arguments, accept a constant actual argument as a
match for a dummy argument with the DEVICE attribute.
2026-01-31 14:47:49 -08:00
Peter Klausler
20103eeb3a
[flang] Fix exposed "free" instances of ac-implied-do indices (#178516)
Tweak the implementations of IsConstantExpr, IsInitialDataTarget, and
related utilities so that "free" instances of array constructor implied
DO indices are not treated as constant expressions when the surrounding
context (if any) doesn't contain their bounds. This fixes a current bug
in which a "free" implied DO index in a structure constructor got
wrapped up an a Constant<SomeDerived>, which led to a crash in lowering.
2026-01-31 14:47:29 -08:00
Katherine Rasmussen
5136b04dd2
[flang] Remove AmbiguousStructureConstructor warnings (#178088)
Remove `AmbiguousStructureConstructor` warnings. This removes harmless
warnings about standards-conforming generic function references that
are never ambiguous with structure constructors.
2026-01-28 10:32:41 -08:00
Peter Klausler
d69ad211b5
[flang] Missing function-vs-subroutine checks on bindings (#177699)
Derived type procedure bindings aren't always subject to the checks of
the function-vs-subroutine classification flags on their symbol table
entries in name resolution, and we're missing some cases that aren't
caught later in expression semantics, which assumed that name resolution
would have already handled them. So check function-vs-subroutine errors
on bindings in expression semantics entirely.

Fixes https://github.com/llvm/llvm-project/issues/177601.
2026-01-27 08:23:23 -08:00
Krzysztof Parzyszek
d542fac6b1
[flang] Add traits to more AST nodes (#175578)
Follow-up to PR175211.

There are still a few AST nodes that don't have any of the standard
traits (Wrapper/Tuple/etc). Because of that they require special
handling in the parse tree visitor.

Convert a subset of these nodes to the typical format, and remove the
special cases from the parse tree visitor.

The members of these nodes were frequently used, so instead of
extracting them by hand each time use helper member functions to access
them.
2026-01-20 09:57:35 -06:00
Krzysztof Parzyszek
b8fec8ebc6
[flang] Add traits to more AST nodes (#175566)
Follow-up to PR175211.

There are still a few AST nodes that don't have any of the standard
traits (Wrapper/Tuple/etc). Because of that they require special
handling in the parse tree visitor.

Convert a subset of these nodes to the typical format, and remove the
special cases from the parse tree visitor.
2026-01-20 08:00:00 -06:00
Peter Klausler
455de5543c
[flang] Fix non-portable TYPE(*) usage in generic (#176235)
Fortran allows a scalar actual argument of any type to correspond with a
TYPE(*) dummy argument that is an assumed-size array. This usage isn't
portable, and it didn't work with a generic procedure with this
compiler, only specific procedures. It affected at least one API in
OpenMPI.

Fix generic resolution to allow for this case, add a distinguishability
test to detect generic interfaces that have ambiguous specific
procedures due to it, and add an optional portability warning (off by
default).
2026-01-19 11:00:26 -08:00
Krzysztof Parzyszek
568b8e4873
[flang] Add traits to several AST nodes (#175065)
There are quite a few AST nodes that don't have any of the standard
traits (Wrapper/Tuple/etc). Because of that they require special
handling in the parse tree visitor.

Convert a subset of these nodes to the typical format, and remove the
special cases from the parse tree visitor.
2026-01-08 18:26:48 -06:00
TMJ
fcec9ced47
[flang] Do not error on constant nonzero UB in substring of ZLA (#174511)
A substring reference where the lower bound is higher than the upper
bound is defined in 9.4.1 to be zero-length.

Thus, a reference to a substring of a CHARACTER*(0) string such as

    string(foo():2)

cannot be a compile-time error since we do not know the return value of
foo().

We also should not error if the lbound > ubound at compile time.
2026-01-08 13:28:48 -05:00
Leandro Lupori
fa53d9285a
[flang] Check for errors when analyzing array constructors (#173092)
Errors in array constructor values result in the array having
less elements than it should, which can cause other errors that
will confuse the user. Avoid this by not returning an expression
on errors.

Fixes #127425
2026-01-08 11:33:21 -03:00
Peter Klausler
420437e33f
[flang] Emit warnings, not errors, for bad subscripts in dead code (#174040)
When semantics is checking expressions in known dead branches of IF
constructs, errors should not be fatal; emit warnings instead, and allow
them to be disabled.

Fixes https://github.com/llvm/llvm-project/issues/171844.
2025-12-31 13:29:29 -08:00
Peter Klausler
1a60e53b2d
[flang] Catch misuse of assumed-rank dummy argument (#173786)
Assumed-rank dummy arguments can't be parenthesized.

Fixes https://github.com/llvm/llvm-project/issues/173593.
2025-12-31 12:43:09 -08:00
Peter Klausler
5fc77c1666
[flang] Add new warnings for unused & undefined locals (#173504)
Add a requested warning for completely unused local variables. The
implementation runs a scan over typed expressions during the existing
expression semantics pass to detect variable uses, and a routine at the
end of semantics to take a pass over the symbol tables to find unused
locals.

The new infrastructure needed to detect variable uses, and the existing
infrastructure that detects potential variable definitions, then makes
it easy to detect variables that are used without any possible
initialization or definition, so I did that too.

The warning for unused locals is off by default -- they might indicate a
misspelling (that IMPLICIT NONE would have caught), but seem otherwise
generally benign. The warning for uses of completely uninitialized and
undefined variables, however, is enabled by default, since that's likely
to indicate a program bug that should be investigated.

This patch touches a lot of files lightly. Many of these files are tests
that would have produced needless warning noise; one new test was added.

Fixes https://github.com/llvm/llvm-project/issues/173276.
2025-12-31 12:42:06 -08:00
Peter Klausler
9c3955afd8
[flang] Use instantiated PDT for structure constructor in default init (#167409)
A structure constructor used in (or as) the default component
initializer for a PDT derived type component needs to traverse the scope
of the right PDT instantiation.

Fixes https://github.com/llvm/llvm-project/issues/167337 and fixes
https://github.com/llvm/llvm-project/issues/167573.
2025-11-14 08:23:24 -08:00
Jean-Didier PAILLEUX
87fb7b002d
[flang] Adding NOTIFY specifier in image selector and add notify type checks (#148810)
This PR adds support for the NOTIFY specifier in the image selector as
described in the 2023 standard, and add checks for the NOTIFY_TYPE type.
2025-11-05 16:35:25 +01:00
Peter Klausler
5354681c6d
[flang] Better folding warning due to hidden conversion (#165430)
When folding intrinsic function calls for types like REAL(2) that don't
have host math library support, we convert them to a type that has
greater range and precision, call a host math library routine that does
exist, and convert the result back to the original result type. The
folding of this second conversion can elicit floating-point warnings
(usually overflow) that are somewhat unclear to the user. Add support
for adding contextual information to these warnings.
2025-10-31 10:26:49 -07:00
Peter Klausler
e34d603f18
[flang] More information on generic resolution failures (#164738)
When a generic procedure reference does not match any of its specific
procedures, run through them and emit the errors for each attempted
match, so that the user has more information to resolve the problem by
adjusting the actual arguments.
2025-10-24 14:14:59 -05:00
Peter Klausler
a6181dc84b
[flang] Refine checks for NULL() in expressions (#163655)
Fix a false positive "NULL can't be an operand here" error message
arising in a defined generic interface for an intrinsic operator (==)
with multiple spellings.

Fixes https://github.com/llvm/llvm-project/issues/163255.
2025-10-16 12:20:53 -07:00
Peter Klausler
e0bffe1ff9
[flang][NFC] Use parser::Unwrap(Ref) more (#162918)
Replace more parse tree references to "thing" and "value()" with usage
of the parser::Unwrap<> template function.
Add parser::UnwrapRef<> as an alias for DEREF(Unwrap<>()).
2025-10-16 12:20:27 -07:00
Peter Klausler
e2ee91ed34
[flang] Clean up some optional<bool> usage (#161925)
Audit the use of std::optional<bool> as a tri-state logical value in
flang, correct a couple cases that need ".value_or()", add some explicit
".has_value()" calls, and document the possible pitfalls.
2025-10-10 10:09:02 -07:00
Peter Klausler
8b930895c4
[flang] Don't misinterpret valid component value for ancestor type (#161910)
As a common language extension, this compiler accepts a structure
constructor whose first value has no keyword and whose type matches an
ancestral type as if the constructor had had a keyword whose name was
the ancestral type. For example, given
  TYPE PARENT; REAL X; END TYPE
  TYPE, EXTENDS(PARENT) :: CHILD; END TYPE
we accept the nonconforming constructor "child(parent(1.))" as if it had
been the conforming "child(1.)" or "child(parent=parent(1.))".

The detection of this case needs to be constrained a bit to avoid a
false positive misinterpretation of conforming code in the case where
the actual first component of the derived type is a POINTER or
ALLOCATABLE whose type and rank would allow it to correspond with the
keywordless first value in the component value list.

Fixes https://github.com/llvm/llvm-project/issues/161887.
2025-10-10 10:08:39 -07:00
Peter Klausler
cbe3b72c4c
[flang][CUDA] Downgrade error to warning (#161570)
The compiler currently emit an error about the lack of an explicit
procedure interface when an external procedure that is called via an
implicit interface is known to have an dummy argument with a CUDA data
attribute, even when the corresponding actual argument does have a CUDA
data attribute. This behavior is inconsistent with what happens when
such a call is to an external in another source file and its definition
is not visible -- the compiler silently accepts an actual argument with
a CUDA data attribute across the implicit interface.

Harmonize this situation so that an actual argument with a CUDA data
attribute in a reference to a procedure with an implicit interface
elicits a usage warning encouraging the use of explicit interfaces. Only
when the procedure's definition is visible, and incompatible, will an
error message appear.
2025-10-03 10:48:13 -07:00
Peter Klausler
ed5e6b8701
[flang] Catch calls to impure intrinsics from PURE subprograms (#160947)
The code in expression semantics that catches a call to an impure
procedure in a PURE context misses calls to impure intrinsics, since
their designators have a SpecificIntrinsic rather than a Symbol. Replace
the current check with a new one that uses the characteristics of the
called procedure, which works for both intrinsic and non-intrinsic
cases.

Testing this change revealed that an explicit INTRINSIC statement wasn't
doing the right thing for extension "dual" intrinsics that can be called
as either a function or as a subroutine; the use of an INTRINSIC
statement would disallow its use as a subroutine. I've fixed that here
as well.

Fixes https://github.com/llvm/llvm-project/issues/157124.
2025-09-30 10:36:52 -07:00
Peter Klausler
35b141f6c9
[flang] Translate +x to (x), not x (#157513)
In expression semantics, don't completely delete the unary plus
operator, but instead translate it into parentheses. The distinction
matters in contexts where the bounds of x are significant or when x must
not be misinterpreted as a variable.

Fixes https://github.com/llvm/llvm-project/issues/157379.
2025-09-10 20:35:35 +00:00
Peter Klausler
b1e109064c
[flang] Downgrade an error to a warning for specific circumstances (#155675)
We emit an error on the component name in the structure constructor
"__builtin_c_ptr(__address=0)", which is the value of "c_ptr_null()",
because the component name "__address" is PRIVATE to an intrinsic
module. The error is specifically omitted, however, when the name
appears in a module file, since it's what we emit for "c_ptr_null()" in
initializers.

This patch carves out another exception -- downgrading the error to a
warning -- for the case of a PRIVATE component name in a structure
constructor from an intrinsic module when the structure constructor
appears in a module. This case arises when module files are being
reprocessed as Fortran source in order to convert them to hermetic
module files.
2025-08-29 07:49:33 -07:00
Peter Klausler
85b8b69140
[flang] Support UNSIGNED ** (#154601)
GNU Fortran added support for UNSIGNED ** UNSIGNED power operations; we
should do the same for portability. This actually simplifies semantics a
bit, since I had to go out of my way to exclude Power as a supported
operation for UNSIGNED.
2025-08-29 07:48:24 -07:00
Eugene Epshteyn
4b6a4aa522
[flang] Consolidate copy-in/copy-out determination in evaluate framework (#151408)
New implementation of `MayNeedCopy()` is used to consolidate
copy-in/copy-out checks.

`IsAssumedShape()` and `IsAssumedRank()` were simplified and are both
now in `Fortran::semantics` workspace.

`preparePresentUserCallActualArgument()` in lowering was modified to use
`MayNeedCopyInOut()`

Fixes https://github.com/llvm/llvm-project/issues/138471
2025-08-26 18:40:13 -04:00
Peter Klausler
442ae603c5
[flang] Warn about inexact real literal implicit widening pitfall (#152799)
When a REAL or COMPLEX literal appears without an explicit kind suffix
or a kind-determining exponent letter, and the conversion of that
literal from decimal to binary is inexact, emit a warning if that
constant is later implicitly widened to a more precise kind, since it
will have a different value than was probably intended.

Values that convert exactly from decimal to default real, e.g. 1.0 and
0.125, do not elicit this warning.

There are many contexts in which Fortran implicitly converts constants.
This patch covers name constant values, variable and component
initialization, constants in expressions, structure constructor
components, and array constructors.

For example, "real(8) :: tenth = 0.1" is a common Fortran bug that's
hard to find, and is one that often trips up even experienced Fortran
programmers. Unlike C and C++, the literal constant 0.1 is *not* double
precision by default, and it does not have the same value as 0.1d0 or
0.1_8 do when it is converted from decimal to real(4) and then to
real(8).
2025-08-13 14:36:13 -07:00
Peter Klausler
b01ab5318e
[flang][CUDA] Apply intrinsic operator overrides (#151018)
Fortran's intrinsic numeric and relational operators can be overridden
with explicit interfaces so long as one or more of the dummy arguments
have the DEVICE attribute. Semantics already allows this without
complaint, but fails to replace the operations with the defined specific
procedure calls when analyzing expressions.
2025-07-30 11:41:40 -07:00
Peter Klausler
fc7f9d795d
[flang] Better error message for ambiguous ASSIGNMENT(=) (#148720)
When a type-bound generic ASSIGNMENT(=) procedure is ambiguous for a
particular reference, say so, rather than claiming that no specific
procedure matched the types and ranks of the LHS and RHS.

Fixes https://github.com/llvm/llvm-project/issues/148675.
2025-07-16 09:09:29 -07:00
Peter Klausler
4dceb25dd1
[flang] Don't create bogus tokens from token pasting (##) (#147596)
When blank tokens arise from macro replacement in token sequences with
token pasting (##), the preprocessor is producing some bogus tokens
(e.g., "name(") that can lead to subtle bugs later when macro names are
not recognized as such.

The fix is to not paste tokens together when the result would not be a
valid Fortran or C token in the preprocessing context.
2025-07-14 11:11:43 -07:00
Peter Klausler
f521338024
[flang] Correct defined assignment case (#142020)
When a generic ASSIGNMENT(=) has elemental and non-elemental specific
procedures that match the actual arguments, the non-elemental procedure
must take precedence. We get this right for generics defined with
interface blocks, but the type-bound case fails if the non-elemental
specific takes a non-default PASS argument.

Fixes https://github.com/llvm/llvm-project/issues/141807.
2025-06-04 09:22:28 -07:00
Peter Klausler
ff8288442d
[flang] Fix crash in error recovery (#140768)
When a TYPE(*) dummy argument is erroneously used as a component value
in a structure constructor, semantics crashes if the structure
constructor had been initially parsed as a potential function reference.
Clean out stale typed expressions when reanalyzing the reconstructed
parse subtree to ensure that errors are caught the next time around.
2025-05-28 13:59:48 -07:00
Peter Klausler
9457616527
[flang] Pad Hollerith actual arguments (#139782)
For more compatible legacy behavior on old tests, extend Hollerith
actual arguments on the right with trailing blanks out to a multiple of
8 bytes. Fixes Fujitsu test 0343_0069.
2025-05-15 11:25:26 -07:00
Peter Klausler
936481fdf5
[flang] Use LHS type for RHS BOZ on assignment (#139626)
F'2023 allows the right-hand side of an assignment to an integer or real
scalar to be a BOZ literal constant; this has already been supported in
some compilers. The type of the left-hand side variable is used to
convert the value of the BOZ.
2025-05-13 07:49:20 -07:00
Peter Klausler
53f0367ab0
[flang] Fix spurious error on defined assignment in PURE (#139186)
An assignment to a whole polymorphic object in a PURE subprogram that is
implemented by means of a defined assignment procedure shouldn't be
subjected to the same definability checks as it would be for an
intrinsic assignment (which would also require it to be allocatable).

Fixes https://github.com/llvm/llvm-project/issues/139129.
2025-05-13 07:48:54 -07:00
Peter Klausler
e75fda107d
[flang] Acknowledge non-enforcement of C7108 (#139169)
Fortran 2023 constraint C7108 prohibits the use of a structure
constructor in a way that is ambiguous with a generic function reference
(intrinsic or user-defined). Sadly, no Fortran compiler implements this
constraint, and the common portable interpretation seems to be the
generic resolution, not the structure constructor.

Restructure the processing of structure constructors in expression
analysis so that it can be driven both from the parse tree as well as
from generic resolution, and then use it to detect ambigous structure
constructor / generic function cases, so that a portability warning can
be issued. And document this as a new intentional violation of the
standard in Extensions.md.

Fixes https://github.com/llvm/llvm-project/issues/138807.
2025-05-13 07:48:30 -07:00
Peter Klausler
9f8ff4b77d
[flang] Revamp evaluate::CoarrayRef (#136628)
Bring the typed expression representation of a coindexed reference up to
F'2023, which removed some restrictions that had allowed the current
representation to suffice for older revisions of the language. This new
representation is somewhat more simple -- it uses a DataRef as its base,
so any subscripts in a part-ref can be represented as an ArrayRef there.

Update the code that creates the CoarrayRef, and add more checking to
it, as well as actually capturing any STAT=, TEAM=, & TEAM_NUMBER=
specifiers that might appear. Enforce the constraint that the part-ref
must have subscripts if it is an array. (And update a pile of
copied-and-pasted test code that lacked such subscripts.)
2025-05-12 12:02:15 -07:00
Peter Klausler
262b3f7615
[flang] Remove runtime dependence on C++ support for types (#134164)
Fortran::runtime::Descriptor::BytesFor() only works for Fortran
intrinsic types for which a C++ type counterpart exists, so it crashes
on some types that are legitimate Fortran types like REAL(2). Move some
logic from Evaluate into a new header in flang/Common, then use it to
avoid this needless dependence on C++.
2025-04-04 08:42:38 -07:00
Peter Klausler
3bc8aa7823
[flang] Catch whole assumed-size array as RHS (#132819)
The right-hand side expression of an intrinsic assignment statement may
not be the name of an assumed-size array dummy argument.
2025-03-26 12:09:57 -07:00
Peter Klausler
6b9716b7f4
[flang] Catch bad usage case of whole assumed-size array (#132052)
Whole assumed-size arrays are generally not allowed outside specific
contexts, where expression analysis notes that they can appear. But
contexts can nest, and in the case of an actual argument that turns out
to be an array constructor, the permission to use a whole assumed-size
array must be rescinded.

Fixes https://github.com/llvm/llvm-project/issues/131909.
2025-03-19 12:02:34 -07:00
Peter Klausler
7f7d7d552b
[flang] Use local name for structure constructor (#132047)
When reinterpreting an ambiguously parsed function reference as a
structure constructor, use the original symbol of the type in the
representation of the derived type spec of the structure constructor,
not its ultimate resolution. The distinction turns out to matter when
generating module files containing derived type constants as
initializers when the derived types' names have undergone USE
association renaming.

Fixes https://github.com/llvm/llvm-project/issues/131579.
2025-03-19 12:02:03 -07:00
Peter Klausler
587f997db7
[flang] Catch C15104(4) violations when coindexing is present (#130677)
The value of a structure constructor component can't have a pointer
ultimate component if it is a coindexed designator.
2025-03-19 11:58:59 -07:00
Peter Klausler
af62a6f59a
[flang] Enforce F'2023 constraints C917 & C918 (#129962)
These are constraints that preclude the need to obtain type information
from descriptors on other images, essentially. When designating a
polymorphic component, its base may not be coindexed; nor shall a
coindexed designator have a type with a polymorphic potential subobject
component.
2025-03-10 16:48:37 -07:00
Peter Klausler
bbc27fbb1c
[flang] Refine checks on assignments to coarrays (#129966)
F'2023 10.2.1.2 paragraph 2 imposes some requirements on the left-hand
sides of assignments when they have coindices, and one was not checked
while another was inaccurately checked. In short, intrinsic assignment
to a coindexed object can't change its type, and neither can it affect
allocatable components.
2025-03-10 13:19:39 -07:00
Peter Klausler
d53079055e
[flang] Catch coindexed procedure pointer/binding references (#129931)
A procedure designator cannot be coindexed, except for cases in which
the coindexing doesn't matter (i.e. a binding that can't be overridden).
2025-03-10 13:18:07 -07:00
Peter Klausler
53c3a2c69a
[flang] Static checking for empty coarrays (#129610)
A coarray must not have a zero extent on a codimension; that would yield
an empty coarray. When cobounds are constants, verify them.
2025-03-10 13:16:31 -07:00