Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/topic/robin/gh-1123-ctor-express…
Browse files Browse the repository at this point in the history
…ions'

* origin/topic/robin/gh-1123-ctor-expressions: (23 commits)
  Change SafeInt repository over to a fork including a fix.
  Fix issue in Bison grammar.
  Suppress GCC warnings.
  Fix a couple of unit tests for Alpine.
  Fix clang-tidy warnings.
  Fix printing of ID scopes.
  Fix printing of enum constants.
  Add boolean folding.
  Outsource an integer range check to SafeInt.
  Suppress clang-tidy warnings.
  Remove debugging output from integer range checks.
  Add test to check substitution of ctor nodes.
  Update printer to output type constructors.
  Move ctor replacement from normalizer into constant folder.
  Add test exercising various integer out-of-range situations.
  Add general constant folding.
  Rework integer out-of-range checking.
  Move negation out of parser.
  Address review feedback.
  Port `port` ctor expression to new model.
  ...
  • Loading branch information
rsmmr committed Jan 27, 2023
2 parents a94de1e + 78ae64b commit 9f65883
Show file tree
Hide file tree
Showing 85 changed files with 1,408 additions and 295 deletions.
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Checks: '
-misc-unused-parameters,
-misc-const-correctness,
-misc-confusable-identifiers,
-misc-const-correctness,
modernize-*,
-modernize-avoid-c-arrays,
Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
url = https://github.com/onqtam/doctest.git
[submodule "3rdparty/SafeInt"]
path = 3rdparty/SafeInt
url = https://github.com/dcleblanc/SafeInt
url = https://github.com/rsmmr/SafeInt.git
[submodule "3rdparty/ArticleEnumClass-v2"]
path = 3rdparty/ArticleEnumClass-v2
url = https://github.com/Dalzhim/ArticleEnumClass-v2
Expand Down
2 changes: 1 addition & 1 deletion 3rdparty/SafeInt
Submodule SafeInt updated 47 files
+0 −65 .appveyor.yml
+11 −0 .gitignore
+0 −131 .travis.yml
+23 −27 CMakeLists.txt
+2 −0 README.md
+494 −710 SafeInt.hpp
+6,385 −0 Test/AddTestCase.cpp
+107 −6,633 Test/AddVerify.cpp
+1 −0 Test/CastVerify.cpp
+1 −0 Test/ClangTest/.gitignore
+205 −0 Test/ClangTest/CMakeLists.txt
+17 −1 Test/ClangTest/makefile
+2 −0 Test/ClangTest/readme.txt
+3 −2 Test/CompileTest.cpp
+2,178 −0 Test/DivTestCase.cpp
+276 −1,788 Test/DivVerify.cpp
+1 −0 Test/GccTest/.gitignore
+177 −0 Test/GccTest/CMakeLists.txt
+14 −1 Test/GccTest/makefile
+1 −0 Test/IncDecVerify.cpp
+1 −0 Test/ModVerify.cpp
+152 −0 Test/MsvcTest/CMakeLists.txt
+4 −0 Test/MsvcTest/build_test.cmd
+1,348 −0 Test/MultTestCase.cpp
+275 −1,186 Test/MultVerify.cpp
+5 −0 Test/MultVerify.h
+12 −2 Test/SafeIntTestVS17/SafeIntTestVS17.sln
+5 −0 Test/SafeIntTestVS17/SafeIntTestVS17/SafeIntTestVS17.vcxproj
+15 −0 Test/SafeIntTestVS17/SafeIntTestVS17/SafeIntTestVS17.vcxproj.filters
+6 −11 Test/SafeIntTestVS17/Scratch/Scratch.cpp
+6,343 −0 Test/SubTestCase.cpp
+65 −6,602 Test/SubVerify.cpp
+151 −0 Test/TestCase.h
+25 −0 Test/TestMain.cpp
+0 −49 Test/TestMain.h
+160 −0 Test/c_safe_math/c_safe_math.vcxproj
+494 −0 Test/c_safe_math/compile_test.c
+11 −0 Test/c_safe_math/safe_math_compile.c
+28 −0 Test/c_safe_math/safe_math_test.c
+107 −0 Test/c_safe_math/safe_math_test.h
+171 −0 Test/c_safe_math/safe_math_test_add.cpp
+171 −0 Test/c_safe_math/safe_math_test_div.cpp
+171 −0 Test/c_safe_math/safe_math_test_mult.cpp
+173 −0 Test/c_safe_math/safe_math_test_sub.cpp
+678 −0 helpfile.md
+1,034 −0 safe_math.h
+2,587 −0 safe_math_impl.h
28 changes: 28 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
1.7.0-dev.78 | 2023-01-27 11:16:01 +0100

* GH-1123: Support arbitrary expression as argument to type
constructors, such as `interval(...)`. (Robin Sommer, Corelight)

So far we used constructor expressions like `interval(...)` to create
*constants* of the given type, and for that we required that the
argument was a single atomic value. The result was that these
constructor expressions were really more like literals for the
corresponding types than "normal" expressions. While that's useful
internally, it's confusing to the user. This commits changes that
to support arbitrary expressions as arguments.

To make this change without loosing a way to create actual constants
(which we need at some places), we implement this in two stages: the
parser initially turns such `interval(...)` expressions into call
operators[1] that the types now define for each desired constructor. We
then extend the normalizer with a new constants folding pass that
turns the call expressions into constants if possible.

The new constant folder remains limited to the cases we need for
this use case, but we'll be able to expand that later to more
general folding.

* Fix bug in cast from `real` to `interval`. (Robin Sommer, Corelight)

* Add anchors to FAQ items in docs [skip CI]. (Benjamin Bannier, Corelight)

1.7.0-dev.53 | 2023-01-25 09:39:02 +0100

* Add stringification to UnitContext. (Benjamin Bannier, Corelight)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.7.0-dev.53
1.7.0-dev.78
6 changes: 6 additions & 0 deletions doc/autogen/types/error.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
Retrieves the textual description associated with the error.

.. rubric:: Operators

.. spicy:operator:: error::Call error error(string)
Creates an error with the given message.

68 changes: 68 additions & 0 deletions doc/autogen/types/integer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,70 @@
Computes the bit-wise 'xor' of the two integers.

.. spicy:operator:: integer::Call int<16> int16(int)
Creates a 16-bit signed integer value.

.. spicy:operator:: integer::Call int<16> int16(uint)
Creates a 16-bit signed integer value.

.. spicy:operator:: integer::Call int<32> int32(int)
Creates a 32-bit signed integer value.

.. spicy:operator:: integer::Call int<32> int32(uint)
Creates a 32-bit signed integer value.

.. spicy:operator:: integer::Call int<64> int64(int)
Creates a 64-bit signed integer value.

.. spicy:operator:: integer::Call int<64> int64(uint)
Creates a 64-bit signed integer value.

.. spicy:operator:: integer::Call int<8> int8(int)
Creates a 8-bit signed integer value.

.. spicy:operator:: integer::Call int<8> int8(uint)
Creates a 8-bit signed integer value.

.. spicy:operator:: integer::Call uint<16> uint16(int)
Creates a 16-bit unsigned integer value.

.. spicy:operator:: integer::Call uint<16> uint16(uint)
Creates a 16-bit unsigned integer value.

.. spicy:operator:: integer::Call uint<32> uint32(int)
Creates a 32-bit unsigned integer value.

.. spicy:operator:: integer::Call uint<32> uint32(uint)
Creates a 32-bit unsigned integer value.

.. spicy:operator:: integer::Call uint<64> uint64(int)
Creates a 64-bit unsigned integer value.

.. spicy:operator:: integer::Call uint<64> uint64(uint)
Creates a 64-bit unsigned integer value.

.. spicy:operator:: integer::Call uint<8> uint8(int)
Creates a 8-bit unsigned integer value.

.. spicy:operator:: integer::Call uint<8> uint8(uint)
Creates a 8-bit unsigned integer value.

.. spicy:operator:: integer::Cast bool cast<bool-type>(int)
Converts the value to a boolean by comparing against zero
Expand Down Expand Up @@ -220,6 +284,10 @@
Inverts the sign of the integer.

.. spicy:operator:: integer::SignNeg int op:- t:uint op:
Inverts the sign of the integer.

.. spicy:operator:: integer::Sum int t:int <sp> op:+ <sp> t:int
Computes the sum of the integers.
Expand Down
22 changes: 22 additions & 0 deletions doc/autogen/types/interval.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@

.. rubric:: Operators

.. spicy:operator:: interval::Call interval interval(int)
Creates an interval interpreting the argument as number of seconds.

.. spicy:operator:: interval::Call interval interval(real)
Creates an interval interpreting the argument as number of seconds.

.. spicy:operator:: interval::Call interval interval(uint)
Creates an interval interpreting the argument as number of seconds.

.. spicy:operator:: interval::Call interval interval_ns(int)
Creates an interval interpreting the argument as number of
nanoseconds.

.. spicy:operator:: interval::Call interval interval_ns(uint)
Creates an interval interpreting the argument as number of
nanoseconds.

.. spicy:operator:: interval::Difference interval t:interval <sp> op:- <sp> t:interval
Returns the difference of the intervals.
Expand Down
4 changes: 4 additions & 0 deletions doc/autogen/types/port.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

.. rubric:: Operators

.. spicy:operator:: port::Call port port(uint<16>, enum)
Creates a port instance.

.. spicy:operator:: port::Equal bool t:port <sp> op:== <sp> t:port
Compares two port values.
Expand Down
4 changes: 4 additions & 0 deletions doc/autogen/types/stream.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
Returns an iterator to the beginning of the container's content.

.. spicy:operator:: stream::Call stream stream(bytes)
Creates a stream instance preinitialized with the given data.

.. spicy:operator:: stream::End <iterator> end(<container>)
Returns an iterator to the end of the container's content.
Expand Down
20 changes: 20 additions & 0 deletions doc/autogen/types/time.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@

.. rubric:: Operators

.. spicy:operator:: time::Call time time(int)
Creates an time interpreting the argument as number of seconds.

.. spicy:operator:: time::Call time time(real)
Creates an time interpreting the argument as number of seconds.

.. spicy:operator:: time::Call time time(uint)
Creates an time interpreting the argument as number of seconds.

.. spicy:operator:: time::Call time time_ns(int)
Creates an time interpreting the argument as number of nanoseconds.

.. spicy:operator:: time::Call time time_ns(uint)
Creates an time interpreting the argument as number of nanoseconds.

.. spicy:operator:: time::Difference interval t:time <sp> op:- <sp> t:time
Returns the difference of the times.
Expand Down
1 change: 1 addition & 0 deletions hilti/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set(SOURCES
src/library.cc
src/logging.cc
src/main.cc
src/safe-math.cc
src/type-info.cc
src/types/address.cc
src/types/bytes.cc
Expand Down
19 changes: 19 additions & 0 deletions hilti/runtime/include/safe-math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2020-2022 by the Zeek Project. See LICENSE for details.
//
// Wrapper around functionality from 3rdparty's safe-math that we need. This
// needs to be in a separate implementation file because one cannot include
// both SafeInt.h and safe-math.h at the same time.

#pragma once

#include <cstdint>

namespace hilti::rt::integer {

/**
* Negates an unsigned value, returning a signed value. Will through a
* `OutOfRange` if not possible.
*/
extern int64_t safe_negate(uint64_t x);

} // namespace hilti::rt::integer
8 changes: 5 additions & 3 deletions hilti/runtime/include/types/interval.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Interval {
*
* @param nsecs interval in nanoseconds.
*/
explicit Interval(hilti::rt::integer::safe<int64_t> nsecs, NanosecondTag /*unused*/) : _nsecs(nsecs) {}
explicit Interval(const hilti::rt::integer::safe<int64_t>& nsecs, NanosecondTag /*unused*/) : _nsecs(nsecs) {}

/**
* Constructs an interval from a double value.
Expand Down Expand Up @@ -73,9 +73,11 @@ class Interval {
Interval operator+(const Interval& other) const { return Interval(_nsecs + other._nsecs, NanosecondTag()); }
Interval operator-(const Interval& other) const { return Interval(_nsecs - other._nsecs, NanosecondTag()); }

Interval operator*(hilti::rt::integer::safe<std::int64_t> i) const { return Interval(_nsecs * i, NanosecondTag()); }
Interval operator*(const hilti::rt::integer::safe<std::int64_t>& i) const {
return Interval(_nsecs * i, NanosecondTag());
}

Interval operator*(hilti::rt::integer::safe<std::uint64_t> i) const {
Interval operator*(const hilti::rt::integer::safe<std::uint64_t>& i) const {
return Interval(_nsecs * i.Ref(), NanosecondTag());
}

Expand Down
Loading

0 comments on commit 9f65883

Please sign in to comment.