MIN & MAX are useless for bit_flags

Instead we need NONE (0) and ANY (all orred values), if the user didn't
already defined them
This commit is contained in:
BogDan Vatra 2016-06-01 10:03:40 +03:00
parent 038ea7c1d3
commit cccd7003ed
1 changed files with 13 additions and 4 deletions

View File

@ -166,6 +166,7 @@ static void GenEnum(const Parser &parser, EnumDef &enum_def,
if (parser.opts.scoped_enums) if (parser.opts.scoped_enums)
code += " : " + GenTypeBasic(enum_def.underlying_type, false); code += " : " + GenTypeBasic(enum_def.underlying_type, false);
code += " {\n"; code += " {\n";
int64_t anyv = 0;
EnumVal *minv = nullptr, *maxv = nullptr; EnumVal *minv = nullptr, *maxv = nullptr;
for (auto it = enum_def.vals.vec.begin(); for (auto it = enum_def.vals.vec.begin();
it != enum_def.vals.vec.end(); it != enum_def.vals.vec.end();
@ -176,12 +177,20 @@ static void GenEnum(const Parser &parser, EnumDef &enum_def,
code += NumToString(ev.value) + ",\n"; code += NumToString(ev.value) + ",\n";
minv = !minv || minv->value > ev.value ? &ev : minv; minv = !minv || minv->value > ev.value ? &ev : minv;
maxv = !maxv || maxv->value < ev.value ? &ev : maxv; maxv = !maxv || maxv->value < ev.value ? &ev : maxv;
anyv |= ev.value;
} }
assert(minv && maxv); assert(minv && maxv);
if (enum_def.attributes.Lookup("bit_flags")) {
if (minv->value != 0) // If the user didn't defined NONE value
code += " " + GenEnumVal(enum_def, "NONE", parser.opts) + " = 0,\n";
if (maxv->value != anyv) // If the user didn't defined ANY value
code += " " + GenEnumVal(enum_def, "ANY", parser.opts) + " = " + NumToString(anyv) + "\n";
} else { // MIN & MAX are useless for bit_flags
code += " " + GenEnumVal(enum_def, "MIN", parser.opts) + " = "; code += " " + GenEnumVal(enum_def, "MIN", parser.opts) + " = ";
code += GenEnumVal(enum_def, minv->name, parser.opts) + ",\n"; code += GenEnumVal(enum_def, minv->name, parser.opts) + ",\n";
code += " " + GenEnumVal(enum_def, "MAX", parser.opts) + " = "; code += " " + GenEnumVal(enum_def, "MAX", parser.opts) + " = ";
code += GenEnumVal(enum_def, maxv->name, parser.opts) + "\n"; code += GenEnumVal(enum_def, maxv->name, parser.opts) + "\n";
}
code += "};\n"; code += "};\n";
if (parser.opts.scoped_enums && enum_def.attributes.Lookup("bit_flags")) if (parser.opts.scoped_enums && enum_def.attributes.Lookup("bit_flags"))
code += "DEFINE_BITMASK_OPERATORS(" + enum_def.name + ", " + GenTypeBasic(enum_def.underlying_type, false) + ")\n"; code += "DEFINE_BITMASK_OPERATORS(" + enum_def.name + ", " + GenTypeBasic(enum_def.underlying_type, false) + ")\n";