summaryrefslogtreecommitdiff
path: root/flang/lib/Semantics/expression.cpp
AgeCommit message (Collapse)Author
2025-11-14[flang] Use instantiated PDT for structure constructor in default init (#167409)Peter Klausler
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-05[flang] Adding NOTIFY specifier in image selector and add notify type checks ↵Jean-Didier PAILLEUX
(#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-10-31[flang] Better folding warning due to hidden conversion (#165430)Peter Klausler
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-24[flang] More information on generic resolution failures (#164738)Peter Klausler
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-16[flang] Refine checks for NULL() in expressions (#163655)Peter Klausler
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[flang][NFC] Use parser::Unwrap(Ref) more (#162918)Peter Klausler
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-10[flang] Clean up some optional<bool> usage (#161925)Peter Klausler
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[flang] Don't misinterpret valid component value for ancestor type (#161910)Peter Klausler
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-03[flang][CUDA] Downgrade error to warning (#161570)Peter Klausler
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-09-30[flang] Catch calls to impure intrinsics from PURE subprograms (#160947)Peter Klausler
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-10[flang] Translate +x to (x), not x (#157513)Peter Klausler
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-08-29[flang] Downgrade an error to a warning for specific circumstances (#155675)Peter Klausler
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[flang] Support UNSIGNED ** (#154601)Peter Klausler
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-26[flang] Consolidate copy-in/copy-out determination in evaluate framework ↵Eugene Epshteyn
(#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-13[flang] Warn about inexact real literal implicit widening pitfall (#152799)Peter Klausler
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-07-30[flang][CUDA] Apply intrinsic operator overrides (#151018)Peter Klausler
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-16[flang] Better error message for ambiguous ASSIGNMENT(=) (#148720)Peter Klausler
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-14[flang] Don't create bogus tokens from token pasting (##) (#147596)Peter Klausler
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-06-04[flang] Correct defined assignment case (#142020)Peter Klausler
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-05-28[flang] Fix crash in error recovery (#140768)Peter Klausler
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-15[flang] Pad Hollerith actual arguments (#139782)Peter Klausler
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-13[flang] Use LHS type for RHS BOZ on assignment (#139626)Peter Klausler
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[flang] Fix spurious error on defined assignment in PURE (#139186)Peter Klausler
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[flang] Acknowledge non-enforcement of C7108 (#139169)Peter Klausler
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-12[flang] Revamp evaluate::CoarrayRef (#136628)Peter Klausler
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-04-04[flang] Remove runtime dependence on C++ support for types (#134164)Peter Klausler
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-03-26[flang] Catch whole assumed-size array as RHS (#132819)Peter Klausler
The right-hand side expression of an intrinsic assignment statement may not be the name of an assumed-size array dummy argument.
2025-03-19[flang] Catch bad usage case of whole assumed-size array (#132052)Peter Klausler
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[flang] Use local name for structure constructor (#132047)Peter Klausler
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[flang] Catch C15104(4) violations when coindexing is present (#130677)Peter Klausler
The value of a structure constructor component can't have a pointer ultimate component if it is a coindexed designator.
2025-03-10[flang] Enforce F'2023 constraints C917 & C918 (#129962)Peter Klausler
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[flang] Refine checks on assignments to coarrays (#129966)Peter Klausler
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[flang] Catch coindexed procedure pointer/binding references (#129931)Peter Klausler
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[flang] Static checking for empty coarrays (#129610)Peter Klausler
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-03[flang] Further work on NULL(MOLD=allocatable) (#129345)Peter Klausler
Refine handling of NULL(...) in semantics to properly distinguish NULL(), NULL(objectPointer), NULL(procPointer), and NULL(allocatable) from each other in relevant contexts. Add IsNullAllocatable() and IsNullPointerOrAllocatable() utility functions. IsNullAllocatable() is true only for NULL(allocatable); it is false for a bare NULL(), which can be detected independently with IsBareNullPointer(). IsNullPointer() now returns false for NULL(allocatable). ALLOCATED(NULL(allocatable)) now works, and folds to .FALSE. These utilities were modified to accept const pointer arguments rather than const references; I usually prefer this style when the result should clearly be false for a null argument (in the C sense), and it helped me find all of their use sites in the code.
2025-02-27[flang] Catch usage of : and * lengths in array c'tors (#128974)Peter Klausler
The definition of an array constructor doesn't preclude the use of [character(:)::] or [character(*)::] directly, but there is language elsewhere in the standard that restricts their use to specific contexts, neither of which include explicitly typed array constructors. Fixes https://github.com/llvm/llvm-project/issues/128755.
2025-02-27[flang] Refine handling of SELECT TYPE associations in analyses (#128935)Peter Klausler
A few bits of semantic checking need a variant of the ResolveAssociations utility function that stops when hitting a construct entity for a type or class guard. This is necessary for cases like the bug below where the analysis is concerned with the type of the name in context, rather than its shape or storage or whatever. So add a flag to ResolveAssociations and GetAssociationRoot to make this happen, and use it at the appropriate call sites. Fixes https://github.com/llvm/llvm-project/issues/128608.
2025-02-06[Flang] Move non-common headers to FortranSupport (#124416)Michael Kruse
Move non-common files from FortranCommon to FortranSupport (analogous to LLVMSupport) such that * declarations and definitions that are only used by the Flang compiler, but not by the runtime, are moved to FortranSupport * declarations and definitions that are used by both ("common"), the compiler and the runtime, remain in FortranCommon * generic STL-like/ADT/utility classes and algorithms remain in FortranCommon This allows a for cleaner separation between compiler and runtime components, which are compiled differently. For instance, runtime sources must not use STL's `<optional>` which causes problems with CUDA support. Instead, the surrogate header `flang/Common/optional.h` must be used. This PR fixes this for `fast-int-sel.h`. Declarations in include/Runtime are also used by both, but are header-only. `ISO_Fortran_binding_wrapper.h`, a header used by compiler and runtime, is also moved into FortranCommon.
2025-01-31[flang] Make REAL/COMPLEX(10) a hard error for non-x86 targets (#124655)Peter Klausler
Currently the use of REAL/COMPLEX(KIND=10) as a type or literal constant suffix elicits an optional warning message only. This leads to compiler internal errors during lowering when these types appear in code being compiled to non-x86_64 targets. For better error messaging, make the use of these types a hard error in semantics instead when they are not supported by the target architecture.
2025-01-31[flang] Prefer non-elemental to elemental defined operator resolution (#124941)Peter Klausler
A non-elemental specific procedure must take precedence over an elemental specific procedure in defined operator generic resolution. Fixes https://github.com/llvm/llvm-project/issues/124777.
2025-01-08[flang] Make IsCoarray() more accurate (#121415)Peter Klausler
A designator without cosubscripts can have subscripts, component references, substrings, &c. and still have corank. The current IsCoarray() predicate only seems to work for whole variable/component references. This was breaking some cases of THIS_IMAGE().
2025-01-08[flang] Silence inappropriate error message (#120614)Peter Klausler
A recent patch added better compatibility checking for actual procedure arguments, but it has led to a few failures in the Fujitsu Fortran test suite in cases of NULL() actual arguments being associated with dummy procedure pointers. As is the case with dummy data pointers, these must always be accepted. Fixes Fujitsu Fortran test cases 0249_0023 through 0028 and 0387_0047.
2024-12-18[flang] Add UNSIGNED (#113504)Peter Klausler
Implement the UNSIGNED extension type and operations under control of a language feature flag (-funsigned). This is nearly identical to the UNSIGNED feature that has been available in Sun Fortran for years, and now implemented in GNU Fortran for gfortran 15, and proposed for ISO standardization in J3/24-116.txt. See the new documentation for details; but in short, this is C's unsigned type, with guaranteed modular arithmetic for +, -, and *, and the related transformational intrinsic functions SUM & al.
2024-12-17[flang] Fix generic resolution with actual/dummy procedure incompatib… ↵Peter Klausler
(#120105) …ility We generally allow any legal procedure pointer target as an actual argument for association with a dummy procedure, since many actual procedures are underspecified EXTERNALs. But for proper generic resolution, it is necessary to disallow incompatible functions with explicit result types. Fixes https://github.com/llvm/llvm-project/issues/119151.
2024-12-02[flang] Downgrade recently added error to a warning (#117217)Peter Klausler
An empty array shouldn't be subscripted, but sometimes they are in zero-trip loops in real applications. So change a recently added error message to a warning (off by default).
2024-11-08[fang][cuda] Allow * in call chevron syntax (#115381)Valentin Clement (バレンタイン クレメン)
Using `*` in call chevron syntax should be allowed. This patch updates the parser to allow this usage. ``` call sub<<<*,nbBlock>>>() ```
2024-10-02[flang] Tag warnings with LanguageFeature or UsageWarning (#110304)Peter Klausler
(This is a big patch, but it's nearly an NFC. No test results have changed and all Fortran tests in the LLVM test suites work as expected.) Allow a parser::Message for a warning to be marked with the common::LanguageFeature or common::UsageWarning that controls it. This will allow a later patch to add hooks whereby a driver will be able to decorate warning messages with the names of its options that enable each particular warning, and to add hooks whereby a driver can map those enumerators by name to command-line options that enable/disable the language feature and enable/disable the messages. The default settings in the constructor for LanguageFeatureControl were moved from its header file into its C++ source file. Hooks for a driver to use to map the name of a feature or warning to its enumerator were also added. To simplify the tagging of warnings with their corresponding language feature or usage warning, to ensure that they are properly controlled by ShouldWarn(), and to ensure that warnings never issue at code sites in module files, two new Warn() member function templates were added to SemanticsContext and other contextual frameworks. Warn() can't be used before source locations can be mapped to scopes, but the bulk of existing code blocks testing ShouldWarn() and FindModuleFile() before calling Say() were convertible into calls to Warn(). The ones that were not convertible were extended with explicit calls to Message::set_languageFeature() and set_usageWarning().
2024-10-02[flang] Implement GETUID and GETGID intrinsics (#110679)David Truby
GETUID and GETGID are non-standard intrinsics supported by a number of other Fortran compilers. On supported platforms these intrinsics simply call the POSIX getuid() and getgid() functions and return the result. The only platform we support that does not have these is Windows. Windows does not have the same concept of UIDs and GIDs, so on Windows we issue a warning indicating this and return 1 from both functions. Co-authored-by: Yi Wu <yi.wu2@arm.com>
2024-09-30Revert "[flang] Implement GETUID and GETGID intrinsics" (#110531)David Truby
Reverts llvm/llvm-project#108017
2024-09-30[flang] Implement GETUID and GETGID intrinsics (#108017)David Truby
GETUID and GETGID are non-standard intrinsics supported by a number of other Fortran compilers. On supported platforms these intrinsics simply call the POSIX getuid() and getgid() functions and return the result. The only platform we support that does not have these is Windows. Windows does not have the same concept of UIDs and GIDs, so on Windows we issue a warning indicating this and return 1 from both functions. Co-authored-by: Yi Wu <yi.wu2@arm.com> --------- Co-authored-by: Yi Wu <yi.wu2@arm.com>