Add word bitwise NOT with ~ and ~= sugar - #536
Open
axic wants to merge 1 commit into
Open
Conversation
23 tasks
Mirror the binary bitwise operators (`&`/`|`/`^` from argotorg#485) for the unary bitwise NOT, reusing the existing `bnotWord` primitive (already constant-folded via the `not` opcode in MastEval): - std: add a `BitNot` type class with `word` (backed by `bnotWord`) and `uint256` (via `Typedef.rep`/`abs`) instances, and export the class. Method name `bnot` is kept distinct from the boolean `not` used by `!`. - Parser: `~` prefix operator (same precedence level as `!`, guarded against `~=`) and the unary `~=` compound assignment. - AST: `ExpBNot` and `StmtBNotEq` (a single operand — it is unary), with name resolution desugaring `~` -> `BitNot.bnot` and `e ~=` to the in-place `e := ~e`. - Pretty printer and module type-ref renamer handle the new nodes. - Test: extend cases/bitwise.solc to exercise `~`, `~=`, and the bnotWord constant folding (`~(~x) == x`, `a & ~0 == a`). `~` (not `!`) matches Solidity, which uses `~` for bitwise NOT and `!` for logical NOT; `!` is already taken here for logical NOT (`ExpLNot`). Co-Authored-By: Alex Beregszaszi <alex@rtfs.hu>
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This follows #485 where this was missed. It uses
~which is the same as Solidity.Mirror the binary bitwise operators (
&/|/^from #485) for the unary bitwise NOT, reusing the existingbnotWordprimitive (already constant-folded via thenotopcode in MastEval):BitNottype class withword(backed bybnotWord) anduint256(viaTypedef.rep/abs) instances, and export the class. Method namebnotis kept distinct from the booleannotused by!.~prefix operator (same precedence level as!, guarded against~=) and the unary~=compound assignment.ExpBNotandStmtBNotEq(a single operand — it is unary), with name resolution desugaring~->BitNot.bnotande ~=to the in-placee := ~e.~,~=, and the bnotWord constant folding (~(~x) == x,a & ~0 == a).~(not!) matches Solidity, which uses~for bitwise NOT and!for logical NOT;!is already taken here for logical NOT (ExpLNot).