use switch statements for BASE_TYPE_ lookups (#7813)

This commit is contained in:
Derek Bailey
2023-02-05 14:29:09 -06:00
committed by GitHub
parent 02d7859f8b
commit f3a3f45159
5 changed files with 57 additions and 50 deletions

View File

@@ -146,13 +146,30 @@ inline bool IsUnsigned(BaseType t) {
(t == BASE_TYPE_ULONG); (t == BASE_TYPE_ULONG);
} }
inline size_t SizeOf(const BaseType t) {
switch (t) {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
case BASE_TYPE_##ENUM: return sizeof(CTYPE);
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
default: FLATBUFFERS_ASSERT(0);
}
return 0;
}
inline const char* TypeName(const BaseType t) {
switch (t) {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, ...) \
case BASE_TYPE_##ENUM: return IDLTYPE;
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
default: FLATBUFFERS_ASSERT(0);
}
return nullptr;
}
// clang-format on // clang-format on
extern const char *const kTypeNames[];
extern const char kTypeSizes[];
inline size_t SizeOf(BaseType t) { return kTypeSizes[t]; }
struct StructDef; struct StructDef;
struct EnumDef; struct EnumDef;
class Parser; class Parser;

View File

@@ -721,19 +721,20 @@ class CppGenerator : public BaseGenerator {
// Return a C++ type from the table in idl.h // Return a C++ type from the table in idl.h
std::string GenTypeBasic(const Type &type, bool user_facing_type) const { std::string GenTypeBasic(const Type &type, bool user_facing_type) const {
// clang-format off
static const char *const ctypename[] = {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
#CTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
};
// clang-format on
if (user_facing_type) { if (user_facing_type) {
if (type.enum_def) return WrapInNameSpace(*type.enum_def); if (type.enum_def) return WrapInNameSpace(*type.enum_def);
if (type.base_type == BASE_TYPE_BOOL) return "bool"; if (type.base_type == BASE_TYPE_BOOL) return "bool";
} }
return ctypename[type.base_type]; switch (type.base_type) {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
case BASE_TYPE_##ENUM: return #CTYPE;
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
//clang-format on
default: FLATBUFFERS_ASSERT(0);
}
return "";
} }
// Return a C++ pointer type, specialized to the actual struct/table types, // Return a C++ pointer type, specialized to the actual struct/table types,
@@ -2262,11 +2263,14 @@ class CppGenerator : public BaseGenerator {
const bool is_array = IsArray(curr_field->value.type); const bool is_array = IsArray(curr_field->value.type);
const bool is_struct = IsStruct(curr_field->value.type); const bool is_struct = IsStruct(curr_field->value.type);
// If encouter a key field, call KeyCompareWithValue to compare this field. // If encouter a key field, call KeyCompareWithValue to compare this
// field.
if (curr_field->key) { if (curr_field->key) {
code_ += code_ += space +
space + "const auto {{RHS}} = {{RHS_PREFIX}}.{{CURR_FIELD_NAME}}();"; "const auto {{RHS}} = {{RHS_PREFIX}}.{{CURR_FIELD_NAME}}();";
code_ += space + "const auto {{CURR_FIELD_NAME}}_compare_result = {{LHS_PREFIX}}.KeyCompareWithValue({{RHS}});"; code_ += space +
"const auto {{CURR_FIELD_NAME}}_compare_result = "
"{{LHS_PREFIX}}.KeyCompareWithValue({{RHS}});";
code_ += space + "if ({{CURR_FIELD_NAME}}_compare_result != 0)"; code_ += space + "if ({{CURR_FIELD_NAME}}_compare_result != 0)";
code_ += space + " return {{CURR_FIELD_NAME}}_compare_result;"; code_ += space + " return {{CURR_FIELD_NAME}}_compare_result;";
@@ -2298,7 +2302,9 @@ class CppGenerator : public BaseGenerator {
} else if (IsStruct(elem_type)) { } else if (IsStruct(elem_type)) {
if (curr_field->key) { if (curr_field->key) {
code_ += space + "const auto {{CURR_FIELD_NAME}}_compare_result = {{LHS_PREFIX}}.KeyCompareWithValue({{RHS}});"; code_ += space +
"const auto {{CURR_FIELD_NAME}}_compare_result = "
"{{LHS_PREFIX}}.KeyCompareWithValue({{RHS}});";
code_ += space + "if ({{CURR_FIELD_NAME}}_compare_result != 0)"; code_ += space + "if ({{CURR_FIELD_NAME}}_compare_result != 0)";
code_ += space + " return {{CURR_FIELD_NAME}}_compare_result;"; code_ += space + " return {{CURR_FIELD_NAME}}_compare_result;";
continue; continue;
@@ -2331,7 +2337,7 @@ class CppGenerator : public BaseGenerator {
code_ += " return *{{FIELD_NAME}}() < *o->{{FIELD_NAME}}();"; code_ += " return *{{FIELD_NAME}}() < *o->{{FIELD_NAME}}();";
} else if (is_array || is_struct) { } else if (is_array || is_struct) {
code_ += " return KeyCompareWithValue(o->{{FIELD_NAME}}()) < 0;"; code_ += " return KeyCompareWithValue(o->{{FIELD_NAME}}()) < 0;";
}else { } else {
code_ += " return {{FIELD_NAME}}() < o->{{FIELD_NAME}}();"; code_ += " return {{FIELD_NAME}}() < o->{{FIELD_NAME}}();";
} }
code_ += " }"; code_ += " }";
@@ -2343,8 +2349,8 @@ class CppGenerator : public BaseGenerator {
} else if (is_array) { } else if (is_array) {
const auto &elem_type = field.value.type.VectorType(); const auto &elem_type = field.value.type.VectorType();
std::string input_type = "::flatbuffers::Array<" + std::string input_type = "::flatbuffers::Array<" +
GenTypeGet(elem_type, "", "", "", false) + GenTypeGet(elem_type, "", "", "", false) + ", " +
", " + NumToString(elem_type.fixed_length) + ">"; NumToString(elem_type.fixed_length) + ">";
code_.SetValue("INPUT_TYPE", input_type); code_.SetValue("INPUT_TYPE", input_type);
code_ += code_ +=
" int KeyCompareWithValue(const {{INPUT_TYPE}} *_{{FIELD_NAME}}" " int KeyCompareWithValue(const {{INPUT_TYPE}} *_{{FIELD_NAME}}"
@@ -2367,7 +2373,8 @@ class CppGenerator : public BaseGenerator {
" const auto &lhs_{{FIELD_NAME}} = " " const auto &lhs_{{FIELD_NAME}} = "
"*(curr_{{FIELD_NAME}}->Get(i));"; "*(curr_{{FIELD_NAME}}->Get(i));";
code_ += code_ +=
" const auto &rhs_{{FIELD_NAME}} = *(_{{FIELD_NAME}}->Get(i));"; " const auto &rhs_{{FIELD_NAME}} = "
"*(_{{FIELD_NAME}}->Get(i));";
GenComparatorForStruct(*elem_type.struct_def, 6, GenComparatorForStruct(*elem_type.struct_def, 6,
"lhs_" + code_.GetValue("FIELD_NAME"), "lhs_" + code_.GetValue("FIELD_NAME"),
"rhs_" + code_.GetValue("FIELD_NAME")); "rhs_" + code_.GetValue("FIELD_NAME"));
@@ -3980,8 +3987,8 @@ class CppCodeGenerator : public CodeGenerator {
// Generate code from the provided `buffer` of given `length`. The buffer is a // Generate code from the provided `buffer` of given `length`. The buffer is a
// serialized reflection.fbs. // serialized reflection.fbs.
Status GenerateCode(const uint8_t *buffer, int64_t length) override { Status GenerateCode(const uint8_t *buffer, int64_t length) override {
(void) buffer; (void)buffer;
(void) length; (void)length;
return Status::NOT_IMPLEMENTED; return Status::NOT_IMPLEMENTED;
} }

View File

@@ -37,7 +37,7 @@ static std::string GenType(const Type &type, bool underlying = false) {
return type.enum_def->defined_namespace->GetFullyQualifiedName( return type.enum_def->defined_namespace->GetFullyQualifiedName(
type.enum_def->name); type.enum_def->name);
} else { } else {
return kTypeNames[type.base_type]; return TypeName(type.base_type);
} }
} }
} }

View File

@@ -356,23 +356,6 @@ template<typename T> static void AssignIndices(const std::vector<T *> &defvec) {
} // namespace } // namespace
// clang-format off
const char *const kTypeNames[] = {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, ...) \
IDLTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
nullptr
};
const char kTypeSizes[] = {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
sizeof(CTYPE),
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
};
// clang-format on
void Parser::Message(const std::string &msg) { void Parser::Message(const std::string &msg) {
if (!error_.empty()) error_ += "\n"; // log all warnings and errors if (!error_.empty()) error_ += "\n"; // log all warnings and errors
error_ += file_being_parsed_.length() ? AbsolutePath(file_being_parsed_) : ""; error_ += file_being_parsed_.length() ? AbsolutePath(file_being_parsed_) : "";
@@ -1947,8 +1930,8 @@ CheckedError Parser::ParseFunction(const std::string *name, Value &e) {
const auto functionname = attribute_; const auto functionname = attribute_;
if (!IsFloat(e.type.base_type)) { if (!IsFloat(e.type.base_type)) {
return Error(functionname + ": type of argument mismatch, expecting: " + return Error(functionname + ": type of argument mismatch, expecting: " +
kTypeNames[BASE_TYPE_DOUBLE] + TypeName(BASE_TYPE_DOUBLE) +
", found: " + kTypeNames[e.type.base_type] + ", found: " + TypeName(e.type.base_type) +
", name: " + (name ? *name : "") + ", value: " + e.constant); ", name: " + (name ? *name : "") + ", value: " + e.constant);
} }
NEXT(); NEXT();
@@ -1994,8 +1977,8 @@ CheckedError Parser::TryTypedValue(const std::string *name, int dtoken,
e.type.base_type = req; e.type.base_type = req;
} else { } else {
return Error(std::string("type mismatch: expecting: ") + return Error(std::string("type mismatch: expecting: ") +
kTypeNames[e.type.base_type] + TypeName(e.type.base_type) +
", found: " + kTypeNames[req] + ", found: " + TypeName(req) +
", name: " + (name ? *name : "") + ", value: " + e.constant); ", name: " + (name ? *name : "") + ", value: " + e.constant);
} }
} }
@@ -2056,7 +2039,7 @@ CheckedError Parser::ParseSingleValue(const std::string *name, Value &e,
return Error( return Error(
std::string("type mismatch or invalid value, an initializer of " std::string("type mismatch or invalid value, an initializer of "
"non-string field must be trivial ASCII string: type: ") + "non-string field must be trivial ASCII string: type: ") +
kTypeNames[in_type] + ", name: " + (name ? *name : "") + TypeName(in_type) + ", name: " + (name ? *name : "") +
", value: " + attribute_); ", value: " + attribute_);
} }
@@ -2128,7 +2111,7 @@ CheckedError Parser::ParseSingleValue(const std::string *name, Value &e,
if (!match) { if (!match) {
std::string msg; std::string msg;
msg += "Cannot assign token starting with '" + TokenToStringId(token_) + msg += "Cannot assign token starting with '" + TokenToStringId(token_) +
"' to value of <" + std::string(kTypeNames[in_type]) + "> type."; "' to value of <" + std::string(TypeName(in_type)) + "> type.";
return Error(msg); return Error(msg);
} }
const auto match_type = e.type.base_type; // may differ from in_type const auto match_type = e.type.base_type; // may differ from in_type

View File

@@ -234,7 +234,7 @@ void FuzzTest2() {
break; break;
default: default:
// All the scalar types. // All the scalar types.
schema += flatbuffers::kTypeNames[base_type]; schema += flatbuffers::TypeName(base_type);
if (!deprecated) { if (!deprecated) {
// We want each instance to use its own random value. // We want each instance to use its own random value.