Commit dfa877e
committed
[CIR] Implement cast expression classes as l-values
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: #20111 parent 1cc78f0 commit dfa877e
File tree
4 files changed
+51
-18
lines changed- clang
- lib/CIR/CodeGen
- test/CIR
- CodeGen
- crashes
4 files changed
+51
-18
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2710 | 2710 | | |
2711 | 2711 | | |
2712 | 2712 | | |
| 2713 | + | |
| 2714 | + | |
2713 | 2715 | | |
| 2716 | + | |
| 2717 | + | |
2714 | 2718 | | |
2715 | 2719 | | |
2716 | 2720 | | |
2717 | 2721 | | |
2718 | | - | |
2719 | | - | |
2720 | | - | |
2721 | | - | |
2722 | | - | |
2723 | | - | |
2724 | | - | |
2725 | 2722 | | |
2726 | 2723 | | |
2727 | 2724 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
| |||
0 commit comments