• [RFC] Adding conditional expressions in Flang (F2023) I’ve been working on adding conditional expression support to Flang, and I wanted to open a discussion before finishing up and posting the PR. • I have posted an issue on Github (https://github.com/llvm/llvm-project/issues/176999) , but I thought Discourse might be a better place to discuss. • From F2023: R1002conditional-expris (scalar-logical-expr?expr[ :scalar-logical-expr?expr]… :expr) Implemented so far: Parser and semantic analysisValidates conditions are scalar LOGICALEnforces strict type/kind/rank matching per C1004 (no automatic promotion)BOZ and NULL() special handling Parser and semantic analysis Validates conditions are scalar LOGICAL Validates conditions are scalar LOGICAL Enforces strict type/kind/rank matching per C1004 (no automatic promotion) Enforces strict type/kind/rank matching per C1004 (no automatic promotion) BOZ and NULL() special handling BOZ and NULL() special handling Expression representation: ConditionalExpr integrated into Evaluate model Expression representation: ConditionalExpr integrated into Evaluate model HLFIR lowering for both scalar and array cases with short-circuit evaluation HLFIR lowering for both scalar and array cases with short-circuit evaluation Supported types: intrinsic and derived types, scalars and arrays Supported types: intrinsic and derived types, scalars and arrays LIT tests to be included LIT tests to be included Current limitations: For character arrays, the declared character length must match exactly across all branches For character arrays, the declared character length must match exactly across all branches No assumed-length characters or arrays No assumed-length characters or arrays No polymorphic (CLASS) types No polymorphic (CLASS) types BOZ literals need explicit conversion: INT(z’FF’) not just z’FF’However, arrays like [z'01’, z'02’] auto-convert to INTEGER in flang, so this works in a conditional expression BOZ literals need explicit conversion: INT(z'
Article Summaries:
- Summary
The Flang compiler team is proposing an RFC to add support for conditional expressions (the ternary operator) as defined in Fortran 2023. The implementation covers parsing, semantic checks (ensuring scalar logical conditions and strict type matching), BOZ and NULL handling, and lowering to HLFIR with short‑circuit evaluation for scalars and arrays. Current limitations include exact character‑array length matching, no assumed‑length or polymorphic types, and explicit conversion for BOZ literals. The community has approved the approach, noting that unimplemented cases should trigger clear “not yet implemented” errors. A pull request is forthcoming, with separate patches for parsing/semantics and lowering.
Sources: