Commit 92e2e20
authored
Translate size_t/ssize_t as usize/isize (#185)
This PR translates `size_t/ssize_t` as `usize/isize` instead of
`u64/i64`. All tests and rules are affected by this change. That's why
this PR is very big. However the core of this change is quite small.
In `rules/` I had to change every `u64/i64` with `usize/isize`. This
makes the rules cleaner because we get rid of artificial `as usize`
casts in arguments and artificial `as u64` casts in return values.
In `libcc2rs/` I changed the arguments of some transalted functions
(malloc, fwrite, etc) from u64 to usize. I also added usize/isize
branches for VaArg.
In `cpp2rust/` the change has 3 parts:
1. I put usize/isize in `addBuiltinTypes`. This adds rules for: `size_t
/ size_type / __size_t -> usize` and `ssize_t -> isize`. __size_t is an
internal type that describes the return value of sizeof
2. `ToString` desugars its type by default, so I gave it a flag to leave
scalars alone, and made `search(QualType)` look for the sugared type
before the desugared one:
```cpp
std::string ToString(clang::QualType qual_type,
ScalarSugar sugar = ScalarSugar::kDesugar);
```
This is needed because size_t isn't a real type, just a typedef over an
integer.
3. In `Converter`, I added an extra default argument to `Convert`,
`ConvertRValue` and `ConvertFreshRValue`:
`std::optional<clang::QualType> implicit_convert_to`. In Clang, both
`size_t` and `unsigned long` are represented as the same type, but in
Rust, `usize` and `u64` are different types, an implicit cast between
the two is not allowed. To solve this, `Convert` is the single point of
adding a cast between 2 types that share the same underlying type in C
but have different types in Rust, this is decided by
`NeedsImplicitScalarCast`.
Callers of `Convert` have to pass the optional `implicit_convert_to` in
order to trigger the scalar cast. Callers pass the target type, for
example `ConvertFreshRValue(rhs, lhs->getType());` in ConvertAssignment,
and `Convert` takes care of the explicit Rust call if needed. Binary
operations do the same using GetOperandImplicitConversionTarget, which
converts both arguments to the same Rust type.1 parent 81c040d commit 92e2e20
180 files changed
Lines changed: 2485 additions & 1643 deletions
File tree
- cpp2rust/converter
- models
- libcc2rs/src
- rules
- array
- brotli
- cstdlib
- cstring
- initializer_list
- map
- pwd
- socket
- stdio
- string
- time
- unistd
- vector
- xattr
- tests
- ub/out
- refcount
- unsafe
- unit
- out
- refcount
- unsafe
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
204 | 204 | | |
205 | 205 | | |
206 | 206 | | |
207 | | - | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
208 | 211 | | |
209 | 212 | | |
210 | | - | |
| 213 | + | |
211 | 214 | | |
212 | 215 | | |
213 | | - | |
214 | | - | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
215 | 219 | | |
216 | 220 | | |
217 | 221 | | |
| |||
224 | 228 | | |
225 | 229 | | |
226 | 230 | | |
227 | | - | |
| 231 | + | |
| 232 | + | |
228 | 233 | | |
229 | 234 | | |
230 | 235 | | |
| |||
1295 | 1300 | | |
1296 | 1301 | | |
1297 | 1302 | | |
1298 | | - | |
| 1303 | + | |
| 1304 | + | |
| 1305 | + | |
| 1306 | + | |
| 1307 | + | |
| 1308 | + | |
| 1309 | + | |
| 1310 | + | |
| 1311 | + | |
| 1312 | + | |
| 1313 | + | |
| 1314 | + | |
| 1315 | + | |
| 1316 | + | |
1299 | 1317 | | |
1300 | 1318 | | |
1301 | 1319 | | |
| |||
1527 | 1545 | | |
1528 | 1546 | | |
1529 | 1547 | | |
1530 | | - | |
| 1548 | + | |
1531 | 1549 | | |
1532 | 1550 | | |
1533 | 1551 | | |
| |||
1763 | 1781 | | |
1764 | 1782 | | |
1765 | 1783 | | |
1766 | | - | |
| 1784 | + | |
1767 | 1785 | | |
1768 | 1786 | | |
1769 | 1787 | | |
| |||
2291 | 2309 | | |
2292 | 2310 | | |
2293 | 2311 | | |
| 2312 | + | |
| 2313 | + | |
| 2314 | + | |
2294 | 2315 | | |
2295 | 2316 | | |
2296 | 2317 | | |
2297 | | - | |
| 2318 | + | |
2298 | 2319 | | |
2299 | 2320 | | |
2300 | 2321 | | |
2301 | 2322 | | |
2302 | 2323 | | |
2303 | 2324 | | |
2304 | | - | |
| 2325 | + | |
2305 | 2326 | | |
2306 | 2327 | | |
2307 | 2328 | | |
| |||
2450 | 2471 | | |
2451 | 2472 | | |
2452 | 2473 | | |
2453 | | - | |
| 2474 | + | |
| 2475 | + | |
| 2476 | + | |
2454 | 2477 | | |
2455 | 2478 | | |
2456 | 2479 | | |
| |||
2460 | 2483 | | |
2461 | 2484 | | |
2462 | 2485 | | |
2463 | | - | |
| 2486 | + | |
| 2487 | + | |
| 2488 | + | |
2464 | 2489 | | |
2465 | 2490 | | |
2466 | 2491 | | |
| |||
3031 | 3056 | | |
3032 | 3057 | | |
3033 | 3058 | | |
3034 | | - | |
| 3059 | + | |
3035 | 3060 | | |
3036 | 3061 | | |
3037 | 3062 | | |
| |||
3511 | 3536 | | |
3512 | 3537 | | |
3513 | 3538 | | |
3514 | | - | |
| 3539 | + | |
3515 | 3540 | | |
3516 | 3541 | | |
3517 | 3542 | | |
3518 | | - | |
| 3543 | + | |
3519 | 3544 | | |
3520 | 3545 | | |
3521 | 3546 | | |
| |||
3525 | 3550 | | |
3526 | 3551 | | |
3527 | 3552 | | |
| 3553 | + | |
| 3554 | + | |
| 3555 | + | |
| 3556 | + | |
3528 | 3557 | | |
3529 | | - | |
3530 | | - | |
3531 | | - | |
| 3558 | + | |
3532 | 3559 | | |
3533 | 3560 | | |
3534 | 3561 | | |
| |||
3622 | 3649 | | |
3623 | 3650 | | |
3624 | 3651 | | |
3625 | | - | |
| 3652 | + | |
| 3653 | + | |
| 3654 | + | |
| 3655 | + | |
3626 | 3656 | | |
3627 | 3657 | | |
3628 | 3658 | | |
| |||
3632 | 3662 | | |
3633 | 3663 | | |
3634 | 3664 | | |
3635 | | - | |
| 3665 | + | |
3636 | 3666 | | |
3637 | 3667 | | |
3638 | 3668 | | |
| |||
3969 | 3999 | | |
3970 | 4000 | | |
3971 | 4001 | | |
3972 | | - | |
| 4002 | + | |
3973 | 4003 | | |
3974 | 4004 | | |
3975 | 4005 | | |
3976 | 4006 | | |
3977 | 4007 | | |
3978 | 4008 | | |
3979 | | - | |
3980 | | - | |
| 4009 | + | |
3981 | 4010 | | |
3982 | 4011 | | |
3983 | 4012 | | |
| |||
4075 | 4104 | | |
4076 | 4105 | | |
4077 | 4106 | | |
4078 | | - | |
| 4107 | + | |
4079 | 4108 | | |
4080 | 4109 | | |
4081 | 4110 | | |
| |||
4121 | 4150 | | |
4122 | 4151 | | |
4123 | 4152 | | |
| 4153 | + | |
4124 | 4154 | | |
4125 | 4155 | | |
4126 | 4156 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
188 | 189 | | |
189 | 190 | | |
190 | 191 | | |
| 192 | + | |
191 | 193 | | |
192 | 194 | | |
193 | 195 | | |
| |||
432 | 434 | | |
433 | 435 | | |
434 | 436 | | |
435 | | - | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
436 | 440 | | |
437 | | - | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
438 | 446 | | |
439 | 447 | | |
440 | 448 | | |
| |||
451 | 459 | | |
452 | 460 | | |
453 | 461 | | |
454 | | - | |
| 462 | + | |
| 463 | + | |
455 | 464 | | |
456 | 465 | | |
457 | 466 | | |
| |||
825 | 834 | | |
826 | 835 | | |
827 | 836 | | |
828 | | - | |
829 | | - | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
830 | 844 | | |
831 | 845 | | |
832 | 846 | | |
| |||
852 | 866 | | |
853 | 867 | | |
854 | 868 | | |
855 | | - | |
| 869 | + | |
856 | 870 | | |
857 | 871 | | |
858 | 872 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
667 | 667 | | |
668 | 668 | | |
669 | 669 | | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
670 | 687 | | |
671 | 688 | | |
672 | 689 | | |
| |||
753 | 770 | | |
754 | 771 | | |
755 | 772 | | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
756 | 824 | | |
757 | 825 | | |
758 | 826 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
146 | 149 | | |
147 | 150 | | |
148 | 151 | | |
| |||
159 | 162 | | |
160 | 163 | | |
161 | 164 | | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
162 | 176 | | |
163 | 177 | | |
164 | 178 | | |
| |||
0 commit comments