mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-09 22:56:27 +00:00
Const correctness in generated code and in code generators. Added missing \reflection\generate_code.bat file. (#4679)
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
c0a6e5120d
commit
79f62ee353
@@ -60,7 +60,7 @@ struct LanguageParameters {
|
||||
};
|
||||
|
||||
const LanguageParameters &GetLangParams(IDLOptions::Language lang) {
|
||||
static LanguageParameters language_parameters[] = {
|
||||
static const LanguageParameters language_parameters[] = {
|
||||
{
|
||||
IDLOptions::kJava,
|
||||
false,
|
||||
@@ -191,7 +191,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
// Save out the generated code for a single class while adding
|
||||
// declaration boilerplate.
|
||||
bool SaveType(const std::string &defname, const Namespace &ns,
|
||||
const std::string &classcode, bool needs_includes) {
|
||||
const std::string &classcode, bool needs_includes) const {
|
||||
if (!classcode.length()) return true;
|
||||
|
||||
std::string code;
|
||||
@@ -226,13 +226,13 @@ class GeneralGenerator : public BaseGenerator {
|
||||
|
||||
const Namespace *CurrentNameSpace() const { return cur_name_space_; }
|
||||
|
||||
std::string FunctionStart(char upper) {
|
||||
std::string FunctionStart(char upper) const {
|
||||
return std::string() + (lang_.language == IDLOptions::kJava
|
||||
? static_cast<char>(tolower(upper))
|
||||
: upper);
|
||||
}
|
||||
|
||||
std::string GenNullableAnnotation(const Type &t) {
|
||||
std::string GenNullableAnnotation(const Type &t) const {
|
||||
return lang_.language == IDLOptions::kJava && parser_.opts.gen_nullable &&
|
||||
!IsScalar(DestinationType(t, true).base_type)
|
||||
? " @Nullable "
|
||||
@@ -243,9 +243,9 @@ class GeneralGenerator : public BaseGenerator {
|
||||
return type.enum_def != nullptr && IsInteger(type.base_type);
|
||||
}
|
||||
|
||||
std::string GenTypeBasic(const Type &type, bool enableLangOverrides) {
|
||||
std::string GenTypeBasic(const Type &type, bool enableLangOverrides) const {
|
||||
// clang-format off
|
||||
static const char *java_typename[] = {
|
||||
static const char * const java_typename[] = {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
|
||||
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
|
||||
#JTYPE,
|
||||
@@ -253,7 +253,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
#undef FLATBUFFERS_TD
|
||||
};
|
||||
|
||||
static const char *csharp_typename[] = {
|
||||
static const char * const csharp_typename[] = {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
|
||||
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
|
||||
#NTYPE,
|
||||
@@ -279,11 +279,11 @@ class GeneralGenerator : public BaseGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
std::string GenTypeBasic(const Type &type) {
|
||||
std::string GenTypeBasic(const Type &type) const {
|
||||
return GenTypeBasic(type, true);
|
||||
}
|
||||
|
||||
std::string GenTypePointer(const Type &type) {
|
||||
std::string GenTypePointer(const Type &type) const {
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_STRING: return lang_.string_type;
|
||||
case BASE_TYPE_VECTOR: return GenTypeGet(type.VectorType());
|
||||
@@ -296,13 +296,13 @@ class GeneralGenerator : public BaseGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
std::string GenTypeGet(const Type &type) {
|
||||
std::string GenTypeGet(const Type &type) const {
|
||||
return IsScalar(type.base_type) ? GenTypeBasic(type) : GenTypePointer(type);
|
||||
}
|
||||
|
||||
// Find the destination type the user wants to receive the value in (e.g.
|
||||
// one size higher signed types for unsigned serialized values in Java).
|
||||
Type DestinationType(const Type &type, bool vectorelem) {
|
||||
Type DestinationType(const Type &type, bool vectorelem) const {
|
||||
if (lang_.language != IDLOptions::kJava) return type;
|
||||
switch (type.base_type) {
|
||||
// We use int for both uchar/ushort, since that generally means less
|
||||
@@ -317,7 +317,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
std::string GenOffsetType(const StructDef &struct_def) {
|
||||
std::string GenOffsetType(const StructDef &struct_def) const {
|
||||
if (lang_.language == IDLOptions::kCSharp) {
|
||||
return "Offset<" + WrapInNameSpace(struct_def) + ">";
|
||||
} else {
|
||||
@@ -326,7 +326,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
std::string GenOffsetConstruct(const StructDef &struct_def,
|
||||
const std::string &variable_name) {
|
||||
const std::string &variable_name) const {
|
||||
if (lang_.language == IDLOptions::kCSharp) {
|
||||
return "new Offset<" + WrapInNameSpace(struct_def) + ">(" +
|
||||
variable_name + ")";
|
||||
@@ -334,7 +334,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
return variable_name;
|
||||
}
|
||||
|
||||
std::string GenVectorOffsetType() {
|
||||
std::string GenVectorOffsetType() const {
|
||||
if (lang_.language == IDLOptions::kCSharp) {
|
||||
return "VectorOffset";
|
||||
} else {
|
||||
@@ -343,12 +343,12 @@ class GeneralGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
// Generate destination type name
|
||||
std::string GenTypeNameDest(const Type &type) {
|
||||
std::string GenTypeNameDest(const Type &type) const {
|
||||
return GenTypeGet(DestinationType(type, true));
|
||||
}
|
||||
|
||||
// Mask to turn serialized value into destination type value.
|
||||
std::string DestinationMask(const Type &type, bool vectorelem) {
|
||||
std::string DestinationMask(const Type &type, bool vectorelem) const {
|
||||
if (lang_.language != IDLOptions::kJava) return "";
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_UCHAR: return " & 0xFF";
|
||||
@@ -362,7 +362,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
// Casts necessary to correctly read serialized data
|
||||
std::string DestinationCast(const Type &type) {
|
||||
std::string DestinationCast(const Type &type) const {
|
||||
if (type.base_type == BASE_TYPE_VECTOR) {
|
||||
return DestinationCast(type.VectorType());
|
||||
} else {
|
||||
@@ -389,7 +389,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
// would be cast down to int before being put onto the buffer. In C#, one cast
|
||||
// directly cast an Enum to its underlying type, which is essential before
|
||||
// putting it onto the buffer.
|
||||
std::string SourceCast(const Type &type, bool castFromDest) {
|
||||
std::string SourceCast(const Type &type, bool castFromDest) const {
|
||||
if (type.base_type == BASE_TYPE_VECTOR) {
|
||||
return SourceCast(type.VectorType(), castFromDest);
|
||||
} else {
|
||||
@@ -413,17 +413,17 @@ class GeneralGenerator : public BaseGenerator {
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string SourceCast(const Type &type) { return SourceCast(type, true); }
|
||||
std::string SourceCast(const Type &type) const { return SourceCast(type, true); }
|
||||
|
||||
std::string SourceCastBasic(const Type &type, bool castFromDest) {
|
||||
std::string SourceCastBasic(const Type &type, bool castFromDest) const {
|
||||
return IsScalar(type.base_type) ? SourceCast(type, castFromDest) : "";
|
||||
}
|
||||
|
||||
std::string SourceCastBasic(const Type &type) {
|
||||
std::string SourceCastBasic(const Type &type) const {
|
||||
return SourceCastBasic(type, true);
|
||||
}
|
||||
|
||||
std::string GenEnumDefaultValue(const Value &value) {
|
||||
std::string GenEnumDefaultValue(const Value &value) const {
|
||||
auto enum_def = value.type.enum_def;
|
||||
auto vec = enum_def->vals.vec;
|
||||
auto default_value = StringToInt(value.constant.c_str());
|
||||
@@ -440,7 +440,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string GenDefaultValue(const Value &value, bool enableLangOverrides) {
|
||||
std::string GenDefaultValue(const Value &value, bool enableLangOverrides) const {
|
||||
if (enableLangOverrides) {
|
||||
// handles both enum case and vector of enum case
|
||||
if (lang_.language == IDLOptions::kCSharp &&
|
||||
@@ -466,12 +466,12 @@ class GeneralGenerator : public BaseGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
std::string GenDefaultValue(const Value &value) {
|
||||
std::string GenDefaultValue(const Value &value) const {
|
||||
return GenDefaultValue(value, true);
|
||||
}
|
||||
|
||||
std::string GenDefaultValueBasic(const Value &value,
|
||||
bool enableLangOverrides) {
|
||||
bool enableLangOverrides) const {
|
||||
if (!IsScalar(value.type.base_type)) {
|
||||
if (enableLangOverrides) {
|
||||
if (lang_.language == IDLOptions::kCSharp) {
|
||||
@@ -490,11 +490,11 @@ class GeneralGenerator : public BaseGenerator {
|
||||
return GenDefaultValue(value, enableLangOverrides);
|
||||
}
|
||||
|
||||
std::string GenDefaultValueBasic(const Value &value) {
|
||||
std::string GenDefaultValueBasic(const Value &value) const {
|
||||
return GenDefaultValueBasic(value, true);
|
||||
}
|
||||
|
||||
void GenEnum(EnumDef &enum_def, std::string *code_ptr) {
|
||||
void GenEnum(EnumDef &enum_def, std::string *code_ptr) const {
|
||||
std::string &code = *code_ptr;
|
||||
if (enum_def.generated) return;
|
||||
|
||||
@@ -569,7 +569,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
// Returns the function name that is able to read a value of the given type.
|
||||
std::string GenGetter(const Type &type) {
|
||||
std::string GenGetter(const Type &type) const {
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_STRING: return lang_.accessor_prefix + "__string";
|
||||
case BASE_TYPE_STRUCT: return lang_.accessor_prefix + "__struct";
|
||||
@@ -591,7 +591,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
// Returns the function name that is able to read a value of the given type.
|
||||
std::string GenGetterForLookupByKey(flatbuffers::FieldDef *key_field,
|
||||
const std::string &data_buffer,
|
||||
const char *num = nullptr) {
|
||||
const char *num = nullptr) const {
|
||||
auto type = key_field->value.type;
|
||||
auto dest_mask = DestinationMask(type, true);
|
||||
auto dest_cast = DestinationCast(type);
|
||||
@@ -606,7 +606,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
|
||||
// Direct mutation is only allowed for scalar fields.
|
||||
// Hence a setter method will only be generated for such fields.
|
||||
std::string GenSetter(const Type &type) {
|
||||
std::string GenSetter(const Type &type) const {
|
||||
if (IsScalar(type.base_type)) {
|
||||
std::string setter =
|
||||
lang_.accessor_prefix + "bb." + FunctionStart('P') + "ut";
|
||||
@@ -621,7 +621,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
// Returns the method name for use with add/put calls.
|
||||
std::string GenMethod(const Type &type) {
|
||||
std::string GenMethod(const Type &type) const {
|
||||
return IsScalar(type.base_type) ? MakeCamel(GenTypeBasic(type, false))
|
||||
: (IsStruct(type) ? "Struct" : "Offset");
|
||||
}
|
||||
@@ -629,7 +629,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
// Recursively generate arguments for a constructor, to deal with nested
|
||||
// structs.
|
||||
void GenStructArgs(const StructDef &struct_def, std::string *code_ptr,
|
||||
const char *nameprefix) {
|
||||
const char *nameprefix) const {
|
||||
std::string &code = *code_ptr;
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
@@ -654,7 +654,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
// builder.putType(name);
|
||||
// and insert manual padding.
|
||||
void GenStructBody(const StructDef &struct_def, std::string *code_ptr,
|
||||
const char *nameprefix) {
|
||||
const char *nameprefix) const {
|
||||
std::string &code = *code_ptr;
|
||||
code += " builder." + FunctionStart('P') + "rep(";
|
||||
code += NumToString(struct_def.minalign) + ", ";
|
||||
@@ -681,7 +681,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
std::string GenByteBufferLength(const char *bb_name) {
|
||||
std::string GenByteBufferLength(const char *bb_name) const {
|
||||
std::string bb_len = bb_name;
|
||||
if (lang_.language == IDLOptions::kCSharp)
|
||||
bb_len += ".Length";
|
||||
@@ -691,7 +691,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
std::string GenOffsetGetter(flatbuffers::FieldDef *key_field,
|
||||
const char *num = nullptr) {
|
||||
const char *num = nullptr) const {
|
||||
std::string key_offset = "";
|
||||
key_offset += lang_.accessor_prefix_static + "__offset(" +
|
||||
NumToString(key_field->value.offset) + ", ";
|
||||
@@ -707,7 +707,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
return key_offset;
|
||||
}
|
||||
|
||||
std::string GenLookupKeyGetter(flatbuffers::FieldDef *key_field) {
|
||||
std::string GenLookupKeyGetter(flatbuffers::FieldDef *key_field) const {
|
||||
std::string key_getter = " ";
|
||||
key_getter += "int tableOffset = " + lang_.accessor_prefix_static;
|
||||
key_getter += "__indirect(vectorLocation + 4 * (start + middle)";
|
||||
@@ -730,7 +730,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
return key_getter;
|
||||
}
|
||||
|
||||
std::string GenKeyGetter(flatbuffers::FieldDef *key_field) {
|
||||
std::string GenKeyGetter(flatbuffers::FieldDef *key_field) const {
|
||||
std::string key_getter = "";
|
||||
auto data_buffer =
|
||||
(lang_.language == IDLOptions::kCSharp) ? "builder.DataBuffer" : "_bb";
|
||||
@@ -762,7 +762,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||
return key_getter;
|
||||
}
|
||||
|
||||
void GenStruct(StructDef &struct_def, std::string *code_ptr) {
|
||||
void GenStruct(StructDef &struct_def, std::string *code_ptr) const {
|
||||
if (struct_def.generated) return;
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user