Skip to content
This repository was archived by the owner on Feb 21, 2026. It is now read-only.

[CIR] Implement cast expression classes as l-values#2011

Draft
lanza wants to merge 2 commits intogh/lanza/12/basefrom
gh/lanza/12/head
Draft

[CIR] Implement cast expression classes as l-values#2011
lanza wants to merge 2 commits intogh/lanza/12/basefrom
gh/lanza/12/head

Conversation

@lanza
Copy link
Member

@lanza lanza commented Nov 23, 2025

Stack from ghstack (oldest at bottom):

This commit fixes l-value emission for CXXConstCastExpr,
CXXReinterpretCastExpr, CXXFunctionalCastExpr, CXXAddrspaceCastExpr,
and ObjCBridgedCastExpr by routing them through the existing
emitCastLValue() function, matching the behavior of CodeGen.

Previously, these cast expression classes would trigger an error and
assertion when used in l-value contexts (e.g., const_cast<int&>(x) = 1).
This was an artificial limitation - CodeGen routes all cast expression
classes through EmitCastLValue(), which then handles each cast kind
appropriately.

Key insight from CodeGen (CGExpr.cpp:5679-5685):
"Casts are never lvalues unless that cast is to a reference type."

The cast expression class (CXXConstCastExpr vs CXXReinterpretCastExpr)
doesn't determine if it's a valid l-value. Instead, the cast kind
(CK_NoOp, CK_LValueBitCast, etc.) and result type determine validity.
emitCastLValue() already handles this correctly based on getCastKind().

Implementation Details:

  • Removed emitError() and assert() for const_cast and related expr classes
  • Unified all cast expression classes in emitLValue() switch statement
  • All now route through emitCastLValue() like CodeGen (CGExpr.cpp:1823-1832)
  • Safety is maintained by emitCastLValue()'s cast kind handling

This follows the ClangIR principle of copying implementations from
CodeGen, which has been proven correct over 20 years.

Testing:

  • Single comprehensive test covering const_cast and reinterpret_cast
  • Tests verify CIR emission, LLVM lowering, and match with original CodeGen
  • All three outputs verified to ensure correctness

Test Plan:
$ ninja -C build/Release clang
$ build/Release/bin/llvm-lit clang/test/CIR/CodeGen/cast-lvalue.cpp

[ghstack-poisoned]
lanza added a commit that referenced this pull request Nov 23, 2025
This commit fixes l-value emission for CXXConstCastExpr,
CXXReinterpretCastExpr, CXXFunctionalCastExpr, CXXAddrspaceCastExpr,
and ObjCBridgedCastExpr by routing them through the existing
emitCastLValue() function, matching the behavior of CodeGen.

Previously, these cast expression classes would trigger an error and
assertion when used in l-value contexts (e.g., `const_cast<int&>(x) = 1`).
This was an artificial limitation - CodeGen routes all cast expression
classes through EmitCastLValue(), which then handles each cast *kind*
appropriately.

Key insight from CodeGen (CGExpr.cpp:5679-5685):
"Casts are never lvalues unless that cast is to a reference type."

The cast expression *class* (CXXConstCastExpr vs CXXReinterpretCastExpr)
doesn't determine if it's a valid l-value. Instead, the cast *kind*
(CK_NoOp, CK_LValueBitCast, etc.) and result type determine validity.
emitCastLValue() already handles this correctly based on getCastKind().

Implementation Details:
- Removed emitError() and assert() for const_cast and related expr classes
- Unified all cast expression classes in emitLValue() switch statement
- All now route through emitCastLValue() like CodeGen (CGExpr.cpp:1823-1832)
- Safety is maintained by emitCastLValue()'s cast kind handling

This follows the ClangIR principle of copying implementations from
CodeGen, which has been proven correct over 20 years.

Testing:
- Single comprehensive test covering const_cast and reinterpret_cast
- Tests verify CIR emission, LLVM lowering, and match with original CodeGen
- All three outputs verified to ensure correctness

Test Plan:
$ ninja -C build/Release clang
$ build/Release/bin/llvm-lit clang/test/CIR/CodeGen/cast-lvalue.cpp


ghstack-source-id: f4cfd63
Pull-Request: #2011
lanza added a commit that referenced this pull request Nov 24, 2025
This commit fixes l-value emission for CXXConstCastExpr,
CXXReinterpretCastExpr, CXXFunctionalCastExpr, CXXAddrspaceCastExpr,
and ObjCBridgedCastExpr by routing them through the existing
emitCastLValue() function, matching the behavior of CodeGen.

Previously, these cast expression classes would trigger an error and
assertion when used in l-value contexts (e.g., `const_cast<int&>(x) = 1`).
This was an artificial limitation - CodeGen routes all cast expression
classes through EmitCastLValue(), which then handles each cast *kind*
appropriately.

Key insight from CodeGen (CGExpr.cpp:5679-5685):
"Casts are never lvalues unless that cast is to a reference type."

The cast expression *class* (CXXConstCastExpr vs CXXReinterpretCastExpr)
doesn't determine if it's a valid l-value. Instead, the cast *kind*
(CK_NoOp, CK_LValueBitCast, etc.) and result type determine validity.
emitCastLValue() already handles this correctly based on getCastKind().

Implementation Details:
- Removed emitError() and assert() for const_cast and related expr classes
- Unified all cast expression classes in emitLValue() switch statement
- All now route through emitCastLValue() like CodeGen (CGExpr.cpp:1823-1832)
- Safety is maintained by emitCastLValue()'s cast kind handling

This follows the ClangIR principle of copying implementations from
CodeGen, which has been proven correct over 20 years.

Testing:
- Single comprehensive test covering const_cast and reinterpret_cast
- Tests verify CIR emission, LLVM lowering, and match with original CodeGen
- All three outputs verified to ensure correctness

Test Plan:
$ ninja -C build/Release clang
$ build/Release/bin/llvm-lit clang/test/CIR/CodeGen/cast-lvalue.cpp

ghstack-source-id: f4cfd63
Pull-Request: #2011
[ghstack-poisoned]
@lanza lanza marked this pull request as draft November 24, 2025 18:39
lanza added a commit that referenced this pull request Nov 25, 2025
This commit fixes l-value emission for CXXConstCastExpr,
CXXReinterpretCastExpr, CXXFunctionalCastExpr, CXXAddrspaceCastExpr,
and ObjCBridgedCastExpr by routing them through the existing
emitCastLValue() function, matching the behavior of CodeGen.

Previously, these cast expression classes would trigger an error and
assertion when used in l-value contexts (e.g., `const_cast<int&>(x) = 1`).
This was an artificial limitation - CodeGen routes all cast expression
classes through EmitCastLValue(), which then handles each cast *kind*
appropriately.

Key insight from CodeGen (CGExpr.cpp:5679-5685):
"Casts are never lvalues unless that cast is to a reference type."

The cast expression *class* (CXXConstCastExpr vs CXXReinterpretCastExpr)
doesn't determine if it's a valid l-value. Instead, the cast *kind*
(CK_NoOp, CK_LValueBitCast, etc.) and result type determine validity.
emitCastLValue() already handles this correctly based on getCastKind().

Implementation Details:
- Removed emitError() and assert() for const_cast and related expr classes
- Unified all cast expression classes in emitLValue() switch statement
- All now route through emitCastLValue() like CodeGen (CGExpr.cpp:1823-1832)
- Safety is maintained by emitCastLValue()'s cast kind handling

This follows the ClangIR principle of copying implementations from
CodeGen, which has been proven correct over 20 years.

Testing:
- Single comprehensive test covering const_cast and reinterpret_cast
- Tests verify CIR emission, LLVM lowering, and match with original CodeGen
- All three outputs verified to ensure correctness

Test Plan:
$ ninja -C build/Release clang
$ build/Release/bin/llvm-lit clang/test/CIR/CodeGen/cast-lvalue.cpp

ghstack-source-id: f4cfd63
Pull-Request: #2011
lanza added a commit that referenced this pull request Nov 25, 2025
This commit fixes l-value emission for CXXConstCastExpr,
CXXReinterpretCastExpr, CXXFunctionalCastExpr, CXXAddrspaceCastExpr,
and ObjCBridgedCastExpr by routing them through the existing
emitCastLValue() function, matching the behavior of CodeGen.

Previously, these cast expression classes would trigger an error and
assertion when used in l-value contexts (e.g., `const_cast<int&>(x) = 1`).
This was an artificial limitation - CodeGen routes all cast expression
classes through EmitCastLValue(), which then handles each cast *kind*
appropriately.

Key insight from CodeGen (CGExpr.cpp:5679-5685):
"Casts are never lvalues unless that cast is to a reference type."

The cast expression *class* (CXXConstCastExpr vs CXXReinterpretCastExpr)
doesn't determine if it's a valid l-value. Instead, the cast *kind*
(CK_NoOp, CK_LValueBitCast, etc.) and result type determine validity.
emitCastLValue() already handles this correctly based on getCastKind().

Implementation Details:
- Removed emitError() and assert() for const_cast and related expr classes
- Unified all cast expression classes in emitLValue() switch statement
- All now route through emitCastLValue() like CodeGen (CGExpr.cpp:1823-1832)
- Safety is maintained by emitCastLValue()'s cast kind handling

This follows the ClangIR principle of copying implementations from
CodeGen, which has been proven correct over 20 years.

Testing:
- Single comprehensive test covering const_cast and reinterpret_cast
- Tests verify CIR emission, LLVM lowering, and match with original CodeGen
- All three outputs verified to ensure correctness

Test Plan:
$ ninja -C build/Release clang
$ build/Release/bin/llvm-lit clang/test/CIR/CodeGen/cast-lvalue.cpp

ghstack-source-id: f4cfd63
Pull-Request: #2011
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant