## Implement Fortran 2023 Conditional Expressions (R1002) ***This PR contains the implementation for parsing and semantic analysis. Lowering is implemented in a separate PR (#186490)*** Implements Fortran 2023 conditional expressions with syntax: `result = (condition ? value1 : condition2 ? value2 : ... : elseValue)` Issue: #176999 Discourse: https://discourse.llvm.org/t/rfc-adding-conditional-expressions-in-flang-f2023/89869/1 -- note that some of the details provided in the RFC post are no longer accurate ### Implementation Details **Parser:** - Added ConditionalExpr as primary expression (F2023 R1002) - Right-associative chaining for multi-way conditionals **Semantics:** - Expression tree node ConditionalExpr<T> with N conditions and N+1 values - Strict type checking: all values must have identical type, kind, and rank - Conditions must be scalar LOGICAL **LIT Testing:** - Parser tests: Syntax validation, precedence, nesting - Semantic tests: Type checking, error messages - Note: Executable tests will be added to the llvm-test-suite repo (https://github.com/llvm/llvm-test-suite/pull/369) **Limitations** - Conditional arguments are not yet supported. This work is planned - #180592 - Polymorphic types (CLASS) not yet supported in lowering - Both limitations will emit clear error message if encountered ### Examples ``` ! Simple conditional x = (flag ? 10 : 20) ! Chained result = (x > 0 ? 1 : x < 0 ? -1 : 0) ! Examples from F2023 ( ABS (RESIDUAL)<=TOLERANCE ? ’ok’ : ’did not converge’ ) ( I>0 .AND. I<=SIZE (A) ? A (I) : PRESENT (VAL) ? VAL : 0.0 ) ``` AI Usage Disclosure: AI tools (Claude Sonnet 4.5) were used to assist with implementation of this feature and test code generation. I have reviewed, modified, and tested all AI-generated code.
42 lines
2.1 KiB
Fortran
42 lines
2.1 KiB
Fortran
! RUN: %python %S/test_folding.py %s %flang_fc1
|
|
! Tests folding of conditional expressions (Fortran 2023)
|
|
module m
|
|
! Basic scalar folding: constant condition selects the chosen branch.
|
|
logical, parameter :: test_true_int = (.true. ? 1 : 2) == 1
|
|
logical, parameter :: test_false_int = (.false. ? 1 : 2) == 2
|
|
logical, parameter :: test_true_real = (.true. ? 1.0 : 2.0) == 1.0
|
|
logical, parameter :: test_false_real = (.false. ? 1.0 : 2.0) == 2.0
|
|
logical, parameter :: test_true_logical = (.true. ? .true. : .false.)
|
|
logical, parameter :: test_false_logical = (.false. ? .false. : .true.)
|
|
|
|
! Multi-branch: right-skewed tree folds correctly.
|
|
! (.true. ? 10 : .false. ? 20 : 30) == 10
|
|
logical, parameter :: test_multi_first = (.true. ? 10 : .false. ? 20 : 30) == 10
|
|
! (.false. ? 10 : .true. ? 20 : 30) == 20
|
|
logical, parameter :: test_multi_second = (.false. ? 10 : .true. ? 20 : 30) == 20
|
|
! (.false. ? 10 : .false. ? 20 : 30) == 30
|
|
logical, parameter :: test_multi_third = (.false. ? 10 : .false. ? 20 : 30) == 30
|
|
|
|
! Named constant expressions in branches are folded.
|
|
integer, parameter :: x = 5
|
|
logical, parameter :: test_branch_fold = (.true. ? x + 1 : x + 2) == 6
|
|
|
|
! Named constant as condition.
|
|
logical, parameter :: cond = .true.
|
|
logical, parameter :: test_named_cond = (cond ? 42 : 0) == 42
|
|
|
|
! Character: constant condition selects the branch value.
|
|
logical, parameter :: test_char = (.true. ? 'yes' : 'no') == 'yes'
|
|
|
|
! Non-constant branch: only the selected branch need be constant (F2023 10.1.12).
|
|
integer :: non_const = 99
|
|
logical, parameter :: test_true_const_else_nonconstant = (.true. ? 10 : non_const) == 10
|
|
logical, parameter :: test_false_const_then_nonconstant = (.false. ? non_const : 10) == 10
|
|
|
|
! Named constant condition with a non-constant branch.
|
|
logical, parameter :: flag = .true.
|
|
logical, parameter :: test_named_cond_nonconstant = (flag ? 1 : non_const) == 1
|
|
logical, parameter :: flag_false = .false.
|
|
logical, parameter :: test_named_cond_false_nonconstant = (flag_false ? non_const : 1) == 1
|
|
|
|
end module
|