binpac: Improve storage type used for case-type index

The type used to store the index for a case-type now tracks the
type of the index expression rather than always using an "int".

The case fields also now have some checking done at code-gen-time to
ensure the constants used for cases does not exceed the numeric limit
of the type used in the case's index expression.  Then, assuming, it
looks safe, the C++ case labels are generated with casts to the type
of the Binpac case's index expression to ensure compilers accept it
(since all Binpac numbers use "int" for storage/printing internally).
This commit is contained in:
Jon Siwek 2019-05-16 08:44:42 -07:00 committed by Tim Wojtulewicz
parent b4b229acf7
commit 21cf20fc6f
7 changed files with 137 additions and 21 deletions

View file

@ -1,6 +1,9 @@
#ifndef binpac_exception_h
#define binpac_exception_h
#include <stdint.h>
#include <inttypes.h>
namespace binpac {
class Exception
@ -45,19 +48,19 @@ class ExceptionInvalidCase : public Exception
{
public:
ExceptionInvalidCase(const char* location,
int index,
int64_t index,
const char *expected)
: location_(location),
index_(index),
expected_(expected)
{
append(binpac_fmt("invalid case: %s: %d (%s)",
append(binpac_fmt("invalid case: %s: %" PRIi64 " (%s)",
location, index, expected));
}
protected:
const char* location_;
int index_;
int64_t index_;
string expected_;
};
@ -65,17 +68,17 @@ class ExceptionInvalidCaseIndex : public Exception
{
public:
ExceptionInvalidCaseIndex(const char* location,
int index)
int64_t index)
: location_(location),
index_(index)
{
append(binpac_fmt("invalid index for case: %s: %d",
append(binpac_fmt("invalid index for case: %s: %" PRIi64,
location, index));
}
protected:
const char* location_;
int index_;
int64_t index_;
};
class ExceptionInvalidOffset : public Exception