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
Draft
[CIR] Implement cast expression classes as l-values#2011lanza wants to merge 2 commits intogh/lanza/12/basefrom
lanza wants to merge 2 commits intogh/lanza/12/basefrom
Conversation
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
This was referenced Nov 24, 2025
Draft
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
This follows the ClangIR principle of copying implementations from
CodeGen, which has been proven correct over 20 years.
Testing:
Test Plan:
$ ninja -C build/Release clang
$ build/Release/bin/llvm-lit clang/test/CIR/CodeGen/cast-lvalue.cpp