Skip to content

[flang] Implement conditional expressions parser/semantics (F2023)#186489

Merged
kparzysz merged 12 commits intollvm:mainfrom
cenewcombe:feature/conditional-expr
Apr 8, 2026
Merged

[flang] Implement conditional expressions parser/semantics (F2023)#186489
kparzysz merged 12 commits intollvm:mainfrom
cenewcombe:feature/conditional-expr

Conversation

@cenewcombe
Copy link
Copy Markdown
Contributor

@cenewcombe cenewcombe commented Mar 13, 2026

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 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:

Limitations

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.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:fir-hlfir flang:parser flang:semantics flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants