[all-commits] [llvm/llvm-project] 802c5c: [flang] Implement conditional expressions parser/s...

Caroline Newcombe via All-commits all-commits at lists.llvm.org
Wed Apr 8 09:27:50 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 802c5cbe5b4be7eb7b442ddbd76acb781921e5f7
      https://github.com/llvm/llvm-project/commit/802c5cbe5b4be7eb7b442ddbd76acb781921e5f7
  Author: Caroline Newcombe <caroline.newcombe at hpe.com>
  Date:   2026-04-08 (Wed, 08 Apr 2026)

  Changed paths:
    M flang/examples/FeatureList/FeatureList.cpp
    M flang/include/flang/Evaluate/expression.h
    M flang/include/flang/Evaluate/fold.h
    M flang/include/flang/Evaluate/shape.h
    M flang/include/flang/Evaluate/tools.h
    M flang/include/flang/Evaluate/traverse.h
    M flang/include/flang/Parser/characters.h
    M flang/include/flang/Parser/dump-parse-tree.h
    M flang/include/flang/Parser/parse-tree.h
    M flang/include/flang/Semantics/dump-expr.h
    M flang/include/flang/Semantics/expression.h
    M flang/lib/Evaluate/check-expression.cpp
    M flang/lib/Evaluate/expression.cpp
    M flang/lib/Evaluate/fold-implementation.h
    M flang/lib/Evaluate/formatting.cpp
    M flang/lib/Evaluate/tools.cpp
    M flang/lib/Lower/ConvertExpr.cpp
    M flang/lib/Lower/ConvertExprToHLFIR.cpp
    M flang/lib/Lower/IterationSpace.cpp
    M flang/lib/Lower/Support/Utils.cpp
    M flang/lib/Parser/expr-parsers.cpp
    M flang/lib/Parser/unparse.cpp
    M flang/lib/Semantics/definable.cpp
    M flang/lib/Semantics/expression.cpp
    M flang/lib/Semantics/openmp-utils.cpp
    A flang/test/Evaluate/fold-conditional-expr.f90
    A flang/test/Parser/conditional-expr.f90
    A flang/test/Semantics/conditional-expr.f90

  Log Message:
  -----------
  [flang] Implement conditional expressions parser/semantics (F2023) (#186489)

## 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.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list