Removed truncating of enum vals to int in ReverseLookup.

Some implementations (e.g. C++98) won't support 64-bit enum values,
but there is no reason to silently truncate them.

Change-Id: I8629563523a96e887068f9c0efcd53741f60e0d6
Tested: on Linux.
This commit is contained in:
Wouter van Oortmerssen 2018-01-22 09:49:06 -08:00
parent 462ce03ebe
commit 8df2d9a3ef
6 changed files with 9 additions and 9 deletions

View File

@ -317,7 +317,7 @@ struct EnumVal {
struct EnumDef : public Definition {
EnumDef() : is_union(false), uses_type_aliases(false) {}
EnumVal *ReverseLookup(int enum_idx, bool skip_union_default = true) {
EnumVal *ReverseLookup(int64_t enum_idx, bool skip_union_default = true) {
for (auto it = vals.vec.begin() +
static_cast<int>(is_union && skip_union_default);
it != vals.vec.end(); ++it) {

View File

@ -1295,7 +1295,7 @@ class CppGenerator : public BaseGenerator {
std::string GetDefaultScalarValue(const FieldDef &field) {
if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) {
auto ev = field.value.type.enum_def->ReverseLookup(
static_cast<int>(StringToInt(field.value.constant.c_str())), false);
StringToInt(field.value.constant.c_str()), false);
if (ev) {
return WrapInNameSpace(field.value.type.enum_def->defined_namespace,
GetEnumValUse(*field.value.type.enum_def, *ev));

View File

@ -371,7 +371,7 @@ class JsGenerator : public BaseGenerator {
std::string GenDefaultValue(const Value &value, const std::string &context) {
if (value.type.enum_def) {
if (auto val = value.type.enum_def->ReverseLookup(
atoi(value.constant.c_str()), false)) {
StringToInt(value.constant.c_str()), false)) {
if (lang_.language == IDLOptions::kTs) {
return GenPrefixedTypeName(WrapInNameSpace(*value.type.enum_def),
value.type.enum_def->file) +

View File

@ -876,7 +876,7 @@ class PhpGenerator : public BaseGenerator {
std::string GenDefaultValue(const Value &value) {
if (value.type.enum_def) {
if (auto val = value.type.enum_def->ReverseLookup(
atoi(value.constant.c_str()), false)) {
StringToInt(value.constant.c_str()), false)) {
return WrapInNameSpace(*value.type.enum_def) + "::" + val->name;
}
}

View File

@ -51,7 +51,7 @@ bool Print(T val, Type type, int /*indent*/, Type * /*union_type*/,
const IDLOptions &opts, std::string *_text) {
std::string &text = *_text;
if (type.enum_def && opts.output_enum_identifiers) {
auto enum_val = type.enum_def->ReverseLookup(static_cast<int>(val));
auto enum_val = type.enum_def->ReverseLookup(static_cast<int64_t>(val));
if (enum_val) {
text += "\"";
text += enum_val->name;

View File

@ -656,8 +656,8 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
if (type.enum_def &&
!type.enum_def->is_union &&
!type.enum_def->attributes.Lookup("bit_flags") &&
!type.enum_def->ReverseLookup(static_cast<int>(
StringToInt(field->value.constant.c_str())))) {
!type.enum_def->ReverseLookup(StringToInt(
field->value.constant.c_str()))) {
return Error("default value of " + field->value.constant + " for field " +
name + " is not part of enum " + type.enum_def->name);
}
@ -668,8 +668,8 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
if (type.enum_def && IsScalar(type.base_type) && !struct_def.fixed &&
!type.enum_def->attributes.Lookup("bit_flags") &&
!type.enum_def->ReverseLookup(
static_cast<int>(StringToInt(field->value.constant.c_str()))))
!type.enum_def->ReverseLookup(StringToInt(
field->value.constant.c_str())))
Warning("enum " + type.enum_def->name +
" does not have a declaration for this field\'s default of " +
field->value.constant);