diff --git a/include/codegen/idl_namer.h b/include/codegen/idl_namer.h index 2aa716ed386..9d3e033dcd2 100644 --- a/include/codegen/idl_namer.h +++ b/include/codegen/idl_namer.h @@ -151,6 +151,20 @@ class IdlNamer : public Namer { return ConvertCase(d.name, config_.fields, Case::kLowerCamel) + "_type"; } + // Php method for structs are a mix of keep case and lower camel case. + std::string LegacyPhpMethod(const StructDef& d, + const std::string& suffix) const { + return Type(d.name) + suffix; + } + std::string LegacyPhpMethod(const std::string& prefix, + const StructDef& d) const { + return prefix + Type(d.name); + } + std::string LegacyPhpField(const std::string& prefix, + const FieldDef& field) const { + return prefix + ConvertCase(field.name, Case::kUpperCamel); + } + private: std::string NamespacedString(const struct Namespace* ns, const std::string& str) const { diff --git a/include/codegen/namer.h b/include/codegen/namer.h index e99bf744c39..d447b565f8f 100644 --- a/include/codegen/namer.h +++ b/include/codegen/namer.h @@ -91,6 +91,14 @@ class Namer { std::string keyword_prefix; // Suffix used to escape keywords. It is usually "_". std::string keyword_suffix; + // The casing used for keywords when escaping. For most languages, keywords + // are case sensitive. PHP is an instance where some keywords are case + // insensitive. + enum class KeywordsCasing { + CaseSensitive, + CaseInsensitive, + }; + KeywordsCasing keywords_casing; // Files. @@ -204,8 +212,16 @@ class Namer { return result; } + virtual std::string NormalizeKeywordCase(const std::string& name) const { + if (config_.keywords_casing == Config::KeywordsCasing::CaseInsensitive) { + return ConvertCase(name, Case::kAllLower); + } else { + return name; + } + } + virtual std::string EscapeKeyword(const std::string& name) const { - if (keywords_.find(name) == keywords_.end()) { + if (keywords_.find(NormalizeKeywordCase(name)) == keywords_.end()) { return name; } else { return config_.keyword_prefix + name + config_.keyword_suffix; diff --git a/include/codegen/python.h b/include/codegen/python.h index cc6c241f2d6..a0a1ba81b99 100644 --- a/include/codegen/python.h +++ b/include/codegen/python.h @@ -26,6 +26,7 @@ static const Namer::Config kConfig = { /*object_suffix=*/"T", /*keyword_prefix=*/"", /*keyword_suffix=*/"_", + /*keywords_casing=*/Namer::Config::KeywordsCasing::CaseSensitive, /*filenames=*/Case::kKeep, /*directories=*/Case::kKeep, /*output_path=*/"", @@ -49,6 +50,7 @@ static const Namer::Config kStubConfig = { /*object_suffix=*/"T", /*keyword_prefix=*/"", /*keyword_suffix=*/"_", + /*keywords_casing=*/Namer::Config::KeywordsCasing::CaseSensitive, /*filenames=*/Case::kKeep, /*directories=*/Case::kKeep, /*output_path=*/"", diff --git a/scripts/generate_code.py b/scripts/generate_code.py index c89403efa8b..758dcd28a30 100755 --- a/scripts/generate_code.py +++ b/scripts/generate_code.py @@ -252,6 +252,12 @@ def glob(path, pattern): schema="union_vector/union_vector.fbs", ) +flatc( + PHP_OPTS, + prefix="PhpKeywordTest", + schema="php_keyword_test.fbs", +) + flatc( BASE_OPTS + TS_OPTS, cwd=ts_code_gen, diff --git a/src/bfbs_gen_lua.cpp b/src/bfbs_gen_lua.cpp index f21892953f7..4507fe6d62b 100644 --- a/src/bfbs_gen_lua.cpp +++ b/src/bfbs_gen_lua.cpp @@ -61,6 +61,7 @@ Namer::Config LuaDefaultConfig() { /*object_suffix=*/"", /*keyword_prefix=*/"", /*keyword_suffix=*/"_", + /*keywords_casing=*/Namer::Config::KeywordsCasing::CaseSensitive, /*filenames=*/Case::kKeep, /*directories=*/Case::kKeep, /*output_path=*/"", diff --git a/src/bfbs_gen_nim.cpp b/src/bfbs_gen_nim.cpp index d5ec2bd341c..ec33e74870a 100644 --- a/src/bfbs_gen_nim.cpp +++ b/src/bfbs_gen_nim.cpp @@ -70,6 +70,7 @@ Namer::Config NimDefaultConfig() { /*object_suffix=*/"T", /*keyword_prefix=*/"", /*keyword_suffix=*/"_", + /*keywords_casing=*/Namer::Config::KeywordsCasing::CaseSensitive, /*filenames=*/Case::kKeep, /*directories=*/Case::kKeep, /*output_path=*/"", diff --git a/src/idl_gen_dart.cpp b/src/idl_gen_dart.cpp index 589e5d91fb4..e55c46602cb 100644 --- a/src/idl_gen_dart.cpp +++ b/src/idl_gen_dart.cpp @@ -48,6 +48,7 @@ static Namer::Config DartDefaultConfig() { /*object_suffix=*/"T", /*keyword_prefix=*/"$", /*keyword_suffix=*/"", + /*keywords_casing=*/Namer::Config::KeywordsCasing::CaseSensitive, /*filenames=*/Case::kKeep, /*directories=*/Case::kKeep, /*output_path=*/"", diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp index 9e743dd2d79..a4a734588ca 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -75,6 +75,7 @@ static Namer::Config GoDefaultConfig() { /*object_suffix=*/"T", /*keyword_prefix=*/"", /*keyword_suffix=*/"_", + /*keywords_casing=*/Namer::Config::KeywordsCasing::CaseSensitive, /*filenames=*/Case::kKeep, /*directories=*/Case::kKeep, /*output_path=*/"", diff --git a/src/idl_gen_java.cpp b/src/idl_gen_java.cpp index 5469286dfc2..130751a749f 100644 --- a/src/idl_gen_java.cpp +++ b/src/idl_gen_java.cpp @@ -46,6 +46,7 @@ static Namer::Config JavaDefaultConfig() { /*object_suffix=*/"T", /*keyword_prefix=*/"", /*keyword_suffix=*/"_", + /*keywords_casing=*/Namer::Config::KeywordsCasing::CaseSensitive, /*filenames=*/Case::kKeep, /*directories=*/Case::kKeep, /*output_path=*/"", diff --git a/src/idl_gen_kotlin.cpp b/src/idl_gen_kotlin.cpp index 3a066d6812b..e9a83c696eb 100644 --- a/src/idl_gen_kotlin.cpp +++ b/src/idl_gen_kotlin.cpp @@ -64,6 +64,7 @@ static Namer::Config KotlinDefaultConfig() { /*object_suffix=*/"T", /*keyword_prefix=*/"", /*keyword_suffix=*/"_", + /*keywords_casing=*/Namer::Config::KeywordsCasing::CaseSensitive, /*filenames=*/Case::kKeep, /*directories=*/Case::kKeep, /*output_path=*/"", diff --git a/src/idl_gen_kotlin_kmp.cpp b/src/idl_gen_kotlin_kmp.cpp index 571d452f043..b9111eda9bd 100644 --- a/src/idl_gen_kotlin_kmp.cpp +++ b/src/idl_gen_kotlin_kmp.cpp @@ -62,6 +62,7 @@ static Namer::Config KotlinDefaultConfig() { /*object_suffix=*/"T", /*keyword_prefix=*/"", /*keyword_suffix=*/"E", + /*keywords_casing=*/Namer::Config::KeywordsCasing::CaseSensitive, /*filenames=*/Case::kUpperCamel, /*directories=*/Case::kLowerCamel, /*output_path=*/"", diff --git a/src/idl_gen_php.cpp b/src/idl_gen_php.cpp index 4b05703e8e2..3f8745dda0b 100644 --- a/src/idl_gen_php.cpp +++ b/src/idl_gen_php.cpp @@ -20,12 +20,126 @@ #include +#include "codegen/idl_namer.h" #include "flatbuffers/code_generators.h" #include "flatbuffers/flatbuffers.h" #include "flatbuffers/idl.h" #include "flatbuffers/util.h" namespace flatbuffers { + +static Namer::Config PhpDefaultConfig() { + return {/*types=*/Case::kKeep, + /*constants=*/Case::kKeep, + /*methods=*/Case::kLowerCamel, + /*functions=*/Case::kUpperCamel, + /*fields=*/Case::kKeep, + /*variables=*/Case::kLowerCamel, + /*variants=*/Case::kKeep, + /*enum_variant_seperator=*/"::", + /*escape_keywords=*/Namer::Config::Escape::BeforeConvertingCase, + /*namespaces=*/Case::kUnknown, + /*namespace_seperator=*/"_", + /*object_prefix=*/"", + /*object_suffix=*/"T", + /*keyword_prefix=*/"", + /*keyword_suffix=*/"_", + /*keywords_casing=*/Namer::Config::KeywordsCasing::CaseInsensitive, + /*filenames=*/Case::kKeep, + /*directories=*/Case::kKeep, + /*output_path=*/"", + /*filename_suffix=*/"_generated", + /*filename_extension=*/".php"}; +} + +static std::set PhpKeywords() { + // List of keywords retrieved from here: + // https://www.php.net/manual/en/reserved.keywords.php + // + // List of reserved words retrieved from here: + // https://www.php.net/manual/en/reserved.other-reserved-words.php + return {"__halt_compiler", + "abstract", + "and", + "array", + "as", + "break", + "callable", + "case", + "catch", + "class", + "clone", + "const", + "continue", + "declare", + "default", + "die", + "do", + "echo", + "else", + "elseif", + "empty", + "enddeclare", + "endfor", + "endforeach", + "endif", + "endswitch", + "endwhile", + "eval", + "exit", + "extends", + "final", + "for", + "foreach", + "function", + "global", + "goto", + "if", + "implements", + "include", + "include_once", + "instanceof", + "insteadof", + "interface", + "isset", + "list", + "namespace", + "new", + "or", + "print", + "private", + "protected", + "public", + "require", + "require_once", + "return", + "static", + "switch", + "throw", + "trait", + "try", + "unset", + "use", + "var", + "while", + "xor", + "int", + "float", + "bool", + "string", + "true", + "false", + "null", + "void", + "iterable", + "object", + "mixed", + "never", + "enum", + "resource", + "numeric"}; +} + namespace php { // Hardcode spaces per indentation. const std::string Indent = " "; @@ -33,7 +147,9 @@ class PhpGenerator : public BaseGenerator { public: PhpGenerator(const Parser& parser, const std::string& path, const std::string& file_name) - : BaseGenerator(parser, path, file_name, "\\", "\\", "php") {} + : BaseGenerator(parser, path, file_name, "\\", "\\", "php"), + namer_(WithFlagOptions(PhpDefaultConfig(), parser.opts, path), + PhpKeywords()) {} bool generate() { if (!GenerateEnums()) return false; if (!GenerateStructs()) return false; @@ -99,58 +215,59 @@ class PhpGenerator : public BaseGenerator { } // Begin a class declaration. - static void BeginClass(const StructDef& struct_def, std::string* code_ptr) { + void BeginClass(const StructDef& struct_def, std::string* code_ptr) { std::string& code = *code_ptr; if (struct_def.fixed) { - code += "class " + struct_def.name + " extends Struct\n"; + code += "class " + namer_.Type(struct_def) + " extends Struct\n"; } else { - code += "class " + struct_def.name + " extends Table\n"; + code += "class " + namer_.Type(struct_def) + " extends Table\n"; } code += "{\n"; } - static void EndClass(std::string* code_ptr) { + void EndClass(std::string* code_ptr) { std::string& code = *code_ptr; code += "}\n"; } // Begin enum code with a class declaration. - static void BeginEnum(const std::string& class_name, std::string* code_ptr) { + void BeginEnum(const std::string& class_name, std::string* code_ptr) { std::string& code = *code_ptr; - code += "class " + class_name + "\n{\n"; + code += "class " + namer_.Type(class_name) + "\n{\n"; } // A single enum member. - static void EnumMember(const EnumDef& enum_def, const EnumVal& ev, - std::string* code_ptr) { + void EnumMember(const EnumDef& enum_def, const EnumVal& ev, + std::string* code_ptr) { std::string& code = *code_ptr; code += Indent + "const "; - code += ev.name; + code += namer_.Variant(ev); code += " = "; code += enum_def.ToString(ev) + ";\n"; } // End enum code. - static void EndEnum(std::string* code_ptr) { + void EndEnum(std::string* code_ptr) { std::string& code = *code_ptr; code += "}\n"; } // Initialize a new struct or table from existing data. - static void NewRootTypeFromBuffer(const StructDef& struct_def, - std::string* code_ptr) { + void NewRootTypeFromBuffer(const StructDef& struct_def, + std::string* code_ptr) { std::string& code = *code_ptr; + const std::string struct_type = namer_.Type(struct_def); code += Indent + "/**\n"; code += Indent + " * @param ByteBuffer $bb\n"; - code += Indent + " * @return " + struct_def.name + "\n"; + code += Indent + " * @return " + struct_type + "\n"; code += Indent + " */\n"; - code += Indent + "public static function getRootAs"; - code += struct_def.name; + code += Indent + "public static function " + + namer_.Method("getRootAs", struct_type); code += "(ByteBuffer $bb)\n"; code += Indent + "{\n"; - code += Indent + Indent + "$obj = new " + struct_def.name + "();\n"; + code += Indent + Indent + "$obj = new " + struct_type + "();\n"; code += Indent + Indent; code += "return ($obj->init($bb->getInt($bb->getPosition())"; code += " + $bb->getPosition(), $bb));\n"; @@ -158,14 +275,13 @@ class PhpGenerator : public BaseGenerator { } // Initialize an existing object with other data, to avoid an allocation. - static void InitializeExisting(const StructDef& struct_def, - std::string* code_ptr) { + void InitializeExisting(const StructDef& struct_def, std::string* code_ptr) { std::string& code = *code_ptr; code += Indent + "/**\n"; code += Indent + " * @param int $_i offset\n"; code += Indent + " * @param ByteBuffer $_bb\n"; - code += Indent + " * @return " + struct_def.name + "\n"; + code += Indent + " * @return " + namer_.Type(struct_def) + "\n"; code += Indent + " **/\n"; code += Indent + "public function init($_i, ByteBuffer $_bb)\n"; code += Indent + "{\n"; @@ -176,14 +292,14 @@ class PhpGenerator : public BaseGenerator { } // Get the length of a vector. - static void GetVectorLen(const FieldDef& field, std::string* code_ptr) { + void GetVectorLen(const FieldDef& field, std::string* code_ptr) { std::string& code = *code_ptr; code += Indent + "/**\n"; code += Indent + " * @return int\n"; code += Indent + " */\n"; - code += Indent + "public function get"; - code += ConvertCase(field.name, Case::kUpperCamel) + "Length()\n"; + code += Indent + "public function " + + namer_.Method("get", field, "Length") + "()\n"; code += Indent + "{\n"; code += Indent + Indent + "$o = $this->__offset("; code += NumToString(field.value.offset) + ");\n"; @@ -193,14 +309,14 @@ class PhpGenerator : public BaseGenerator { } // Get a [ubyte] vector as a byte array. - static void GetUByte(const FieldDef& field, std::string* code_ptr) { + void GetUByte(const FieldDef& field, std::string* code_ptr) { std::string& code = *code_ptr; code += Indent + "/**\n"; code += Indent + " * @return string\n"; code += Indent + " */\n"; - code += Indent + "public function get"; - code += ConvertCase(field.name, Case::kUpperCamel) + "Bytes()\n"; + code += Indent + "public function " + + namer_.Method("get", field.name, "Bytes") + "()\n"; code += Indent + "{\n"; code += Indent + Indent + "return $this->__vector_as_bytes("; code += NumToString(field.value.offset) + ");\n"; @@ -208,22 +324,19 @@ class PhpGenerator : public BaseGenerator { } // Get the value of a struct's scalar. - static void GetScalarFieldOfStruct(const FieldDef& field, - std::string* code_ptr) { + void GetScalarFieldOfStruct(const FieldDef& field, std::string* code_ptr) { std::string& code = *code_ptr; - std::string getter = GenGetter(field.value.type); code += Indent + "/**\n"; - code += Indent + " * @return "; - code += GenTypeGet(field.value.type) + "\n"; + code += Indent + " * @return " + GenTypeGet(field.value.type) + "\n"; code += Indent + " */\n"; - code += Indent + "public function " + getter; - code += ConvertCase(field.name, Case::kUpperCamel) + "()\n"; + code += Indent + "public function " + + namer_.LegacyPhpField(GenGetter(field.value.type), field) + "()\n"; code += Indent + "{\n"; - code += Indent + Indent + "return "; - code += "$this->bb->get"; - code += ConvertCase(GenTypeGet(field.value.type), Case::kUpperCamel); + code += Indent + Indent + "return "; + code += "$this->bb->"; + code += namer_.Method("get", GenTypeGet(field.value.type)); code += "($this->bb_pos + "; code += NumToString(field.value.offset) + ")"; code += ";\n"; @@ -238,15 +351,13 @@ class PhpGenerator : public BaseGenerator { code += Indent + "/**\n"; code += Indent + " * @return " + GenTypeGet(field.value.type) + "\n"; code += Indent + " */\n"; - code += Indent + "public function get"; - code += ConvertCase(field.name, Case::kUpperCamel); - code += "()\n"; + code += Indent + "public function " + namer_.Method("get", field) + "()\n"; code += Indent + "{\n"; code += Indent + Indent + "$o = $this->__offset(" + NumToString(field.value.offset) + ");\n" + Indent + Indent + "return $o != 0 ? "; - code += "$this->bb->get"; - code += ConvertCase(GenTypeGet(field.value.type), Case::kUpperCamel) + + code += "$this->bb->"; + code += namer_.Method("get", GenTypeGet(field.value.type)) + "($o + $this->bb_pos)"; code += " : " + GenDefaultValue(field.value) + ";\n"; code += Indent + "}\n\n"; @@ -260,14 +371,13 @@ class PhpGenerator : public BaseGenerator { code += Indent + "/**\n"; code += Indent + " * @return " + GenTypeGet(field.value.type) + "\n"; code += Indent + " */\n"; - code += Indent + "public function get"; - code += ConvertCase(field.name, Case::kUpperCamel) + "()\n"; + code += Indent + "public function " + namer_.Method("get", field) + "()\n"; code += Indent + "{\n"; - code += Indent + Indent + "$obj = new "; - code += GenTypeGet(field.value.type) + "();\n"; - code += Indent + Indent + "$obj->init($this->bb_pos + "; - code += NumToString(field.value.offset) + ", $this->bb);"; - code += "\n" + Indent + Indent + "return $obj;\n"; + code += Indent + Indent + "$obj = new " + GenTypeGet(field.value.type) + + "();\n"; + code += Indent + Indent + "$obj->init($this->bb_pos + " + + NumToString(field.value.offset) + ", $this->bb);\n"; + code += Indent + Indent + "return $obj;\n"; code += Indent + "}\n\n"; } @@ -276,13 +386,10 @@ class PhpGenerator : public BaseGenerator { void GetStructFieldOfTable(const FieldDef& field, std::string* code_ptr) { std::string& code = *code_ptr; - code += Indent + "public function get"; - code += ConvertCase(field.name, Case::kUpperCamel); + code += Indent + "public function " + namer_.Method("get", field.name); code += "()\n"; code += Indent + "{\n"; - code += Indent + Indent + "$obj = new "; - code += - ConvertCase(GenTypeGet(field.value.type), Case::kUpperCamel) + "();\n"; + code += Indent + Indent + "$obj = new " + ScalarType(field) + "();\n"; code += Indent + Indent + "$o = $this->__offset(" + NumToString(field.value.offset) + ");\n"; code += Indent + Indent; @@ -299,9 +406,8 @@ class PhpGenerator : public BaseGenerator { // Get the value of a string. void GetStringField(const FieldDef& field, std::string* code_ptr) { std::string& code = *code_ptr; - code += Indent + "public function get"; - code += ConvertCase(field.name, Case::kUpperCamel); - code += "()\n"; + + code += Indent + "public function " + namer_.Method("get", field) + "()\n"; code += Indent + "{\n"; code += Indent + Indent + "$o = $this->__offset(" + NumToString(field.value.offset) + ");\n"; @@ -318,8 +424,8 @@ class PhpGenerator : public BaseGenerator { code += Indent + "/**\n"; code += Indent + " * @return" + GenTypeBasic(field.value.type) + "\n"; code += Indent + " */\n"; - code += Indent + "public function get"; - code += ConvertCase(field.name, Case::kUpperCamel) + "($obj)\n"; + code += + Indent + "public function " + namer_.Method("get", field) + "($obj)\n"; code += Indent + "{\n"; code += Indent + Indent + "$o = $this->__offset(" + NumToString(field.value.offset) + ");\n"; @@ -337,15 +443,12 @@ class PhpGenerator : public BaseGenerator { code += Indent + "/**\n"; code += Indent + " * @return" + GenTypeBasic(field.value.type) + "\n"; code += Indent + " */\n"; - code += Indent + "public function get"; - code += ConvertCase(field.name, Case::kUpperCamel); - code += "($j)\n"; + code += + Indent + "public function " + namer_.Method("get", field) + "($j)\n"; code += Indent + "{\n"; code += Indent + Indent + "$o = $this->__offset(" + NumToString(field.value.offset) + ");\n"; - code += Indent + Indent + "$obj = new "; - code += - ConvertCase(GenTypeGet(field.value.type), Case::kUpperCamel) + "();\n"; + code += Indent + Indent + "$obj = new " + ScalarType(field) + "();\n"; switch (field.value.type.base_type) { case BASE_TYPE_STRUCT: @@ -400,9 +503,8 @@ class PhpGenerator : public BaseGenerator { code += Indent + " * @param int offset\n"; code += Indent + " * @return " + GenTypeGet(field.value.type) + "\n"; code += Indent + " */\n"; - code += Indent + "public function get"; - code += ConvertCase(field.name, Case::kUpperCamel); - code += "($j)\n"; + code += + Indent + "public function " + namer_.Method("get", field) + "($j)\n"; code += Indent + "{\n"; code += Indent + Indent + "$o = $this->__offset(" + NumToString(field.value.offset) + ");\n"; @@ -413,8 +515,8 @@ class PhpGenerator : public BaseGenerator { code += NumToString(InlineSize(vectortype)) + ") : "; code += GenDefaultValue(field.value) + ";\n"; } else { - code += Indent + Indent + "return $o != 0 ? $this->bb->get"; - code += ConvertCase(GenTypeGet(field.value.type), Case::kUpperCamel); + code += Indent + Indent + "return $o != 0 ? $this->bb->"; + code += namer_.Method("get", GenTypeGet(field.value.type)); code += "($this->__vector($o) + $j * "; code += NumToString(InlineSize(vectortype)) + ") : "; code += GenDefaultValue(field.value) + ";\n"; @@ -432,9 +534,8 @@ class PhpGenerator : public BaseGenerator { code += Indent + " * @param int offset\n"; code += Indent + " * @return " + GenTypeGet(field.value.type) + "\n"; code += Indent + " */\n"; - code += Indent + "public function get"; - code += ConvertCase(field.name, Case::kUpperCamel); - code += "($j, $obj)\n"; + code += Indent + "public function " + namer_.Method("get", field) + + "($j, $obj)\n"; code += Indent + "{\n"; code += Indent + Indent + "$o = $this->__offset(" + NumToString(field.value.offset) + ");\n"; @@ -446,8 +547,8 @@ class PhpGenerator : public BaseGenerator { // Recursively generate arguments for a constructor, to deal with nested // structs. - static void StructBuilderArgs(const StructDef& struct_def, - const char* nameprefix, std::string* code_ptr) { + void StructBuilderArgs(const StructDef& struct_def, const char* nameprefix, + std::string* code_ptr) { for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto& field = **it; @@ -461,16 +562,17 @@ class PhpGenerator : public BaseGenerator { } else { std::string& code = *code_ptr; code += std::string(", $") + nameprefix; - code += ConvertCase(field.name, Case::kLowerCamel); + code += namer_.Variable(field); } } } // Recursively generate struct construction statements and instert manual // padding. - static void StructBuilderBody(const StructDef& struct_def, - const char* nameprefix, std::string* code_ptr) { + void StructBuilderBody(const StructDef& struct_def, const char* nameprefix, + std::string* code_ptr) { std::string& code = *code_ptr; + code += Indent + Indent + "$builder->prep("; code += NumToString(struct_def.minalign) + ", "; code += NumToString(struct_def.bytesize) + ");\n"; @@ -486,23 +588,22 @@ class PhpGenerator : public BaseGenerator { (nameprefix + (field.name + "_")).c_str(), code_ptr); } else { code += Indent + Indent + "$builder->put" + GenMethod(field) + "($"; - code += - nameprefix + ConvertCase(field.name, Case::kLowerCamel) + ");\n"; + code += nameprefix + namer_.Variable(field) + ");\n"; } } } // Get the value of a table's starting offset. - static void GetStartOfTable(const StructDef& struct_def, - std::string* code_ptr) { + void GetStartOfTable(const StructDef& struct_def, std::string* code_ptr) { std::string& code = *code_ptr; code += Indent + "/**\n"; code += Indent + " * @param FlatBufferBuilder $builder\n"; code += Indent + " * @return void\n"; code += Indent + " */\n"; - code += Indent + "public static function start" + struct_def.name; - code += "(FlatBufferBuilder $builder)\n"; + code += Indent + "public static function " + + namer_.LegacyPhpMethod("start", struct_def) + + "(FlatBufferBuilder $builder)\n"; code += Indent + "{\n"; code += Indent + Indent + "$builder->StartObject("; code += NumToString(struct_def.fields.vec.size()); @@ -511,9 +612,10 @@ class PhpGenerator : public BaseGenerator { code += Indent + "/**\n"; code += Indent + " * @param FlatBufferBuilder $builder\n"; - code += Indent + " * @return " + struct_def.name + "\n"; + code += Indent + " * @return " + namer_.Type(struct_def) + "\n"; code += Indent + " */\n"; - code += Indent + "public static function create" + struct_def.name; + code += Indent + "public static function " + + namer_.LegacyPhpMethod("create", struct_def); code += "(FlatBufferBuilder $builder, "; for (auto it = struct_def.fields.vec.begin(); @@ -536,9 +638,8 @@ class PhpGenerator : public BaseGenerator { auto& field = **it; if (field.deprecated) continue; - code += Indent + Indent + "self::add"; - code += ConvertCase(field.name, Case::kUpperCamel) + "($builder, $" + - field.name + ");\n"; + code += Indent + Indent + "self::" + namer_.Method("add", field); + code += "($builder, $" + field.name + ");\n"; } code += Indent + Indent + "$o = $builder->endObject();\n"; @@ -557,8 +658,8 @@ class PhpGenerator : public BaseGenerator { } // Set the value of a table's field. - static void BuildFieldOfTable(const FieldDef& field, const size_t offset, - std::string* code_ptr) { + void BuildFieldOfTable(const FieldDef& field, const size_t offset, + std::string* code_ptr) { std::string& code = *code_ptr; code += Indent + "/**\n"; @@ -566,17 +667,14 @@ class PhpGenerator : public BaseGenerator { code += Indent + " * @param " + GenTypeBasic(field.value.type) + "\n"; code += Indent + " * @return void\n"; code += Indent + " */\n"; - code += Indent + "public static function "; - code += "add" + ConvertCase(field.name, Case::kUpperCamel); - code += "(FlatBufferBuilder $builder, "; - code += "$" + ConvertCase(field.name, Case::kLowerCamel); - code += ")\n"; + code += Indent + "public static function " + namer_.Method("add", field) + + "(FlatBufferBuilder $builder, $" + namer_.Variable(field) + ")\n"; code += Indent + "{\n"; code += Indent + Indent + "$builder->add"; code += GenMethod(field) + "X("; code += NumToString(offset) + ", "; - code += "$" + ConvertCase(field.name, Case::kLowerCamel); + code += "$" + namer_.Variable(field); code += ", "; if (field.value.type.base_type == BASE_TYPE_BOOL) { @@ -589,20 +687,20 @@ class PhpGenerator : public BaseGenerator { } // Set the value of one of the members of a table's vector. - static void BuildVectorOfTable(const FieldDef& field, std::string* code_ptr) { + void BuildVectorOfTable(const FieldDef& field, std::string* code_ptr) { std::string& code = *code_ptr; - auto vector_type = field.value.type.VectorType(); auto alignment = InlineAlignment(vector_type); auto elem_size = InlineSize(vector_type); + code += Indent + "/**\n"; code += Indent + " * @param FlatBufferBuilder $builder\n"; code += Indent + " * @param array offset array\n"; code += Indent + " * @return int vector offset\n"; code += Indent + " */\n"; - code += Indent + "public static function create"; - code += ConvertCase(field.name, Case::kUpperCamel); - code += "Vector(FlatBufferBuilder $builder, array $data)\n"; + code += Indent + "public static function "; + code += namer_.Method("create", field, "Vector"); + code += "(FlatBufferBuilder $builder, array $data)\n"; code += Indent + "{\n"; code += Indent + Indent + "$builder->startVector("; code += NumToString(elem_size); @@ -612,9 +710,8 @@ class PhpGenerator : public BaseGenerator { code += "for ($i = count($data) - 1; $i >= 0; $i--) {\n"; if (IsScalar(field.value.type.VectorType().base_type)) { code += Indent + Indent + Indent; - code += "$builder->put"; - code += ConvertCase(GenTypeBasic(field.value.type.VectorType()), - Case::kUpperCamel); + code += "$builder->"; + code += namer_.Method("put", GenTypeBasic(field.value.type.VectorType())); code += "($data[$i]);\n"; } else { code += Indent + Indent + Indent; @@ -629,9 +726,9 @@ class PhpGenerator : public BaseGenerator { code += Indent + " * @param int $numElems\n"; code += Indent + " * @return void\n"; code += Indent + " */\n"; - code += Indent + "public static function start"; - code += ConvertCase(field.name, Case::kUpperCamel); - code += "Vector(FlatBufferBuilder $builder, $numElems)\n"; + code += Indent + "public static function "; + code += namer_.Method("start", field, "Vector"); + code += "(FlatBufferBuilder $builder, $numElems)\n"; code += Indent + "{\n"; code += Indent + Indent + "$builder->startVector("; code += NumToString(elem_size); @@ -648,7 +745,8 @@ class PhpGenerator : public BaseGenerator { code += Indent + " * @param FlatBufferBuilder $builder\n"; code += Indent + " * @return int table offset\n"; code += Indent + " */\n"; - code += Indent + "public static function end" + struct_def.name; + code += Indent + "public static function " + + namer_.LegacyPhpMethod("end", struct_def); code += "(FlatBufferBuilder $builder)\n"; code += Indent + "{\n"; code += Indent + Indent + "$o = $builder->endObject();\n"; @@ -741,8 +839,8 @@ class PhpGenerator : public BaseGenerator { auto offset = it - struct_def.fields.vec.begin(); if (field.value.type.base_type == BASE_TYPE_UNION) { std::string& code = *code_ptr; - code += Indent + "public static function add"; - code += ConvertCase(field.name, Case::kUpperCamel); + code += Indent + "public static function "; + code += namer_.Method("add", field); code += "(FlatBufferBuilder $builder, $offset)\n"; code += Indent + "{\n"; code += Indent + Indent + "$builder->addOffsetX("; @@ -776,27 +874,28 @@ class PhpGenerator : public BaseGenerator { if (!struct_def.fixed) { if (parser_.file_identifier_.length()) { // Return the identifier - code += Indent + "public static function " + struct_def.name; - code += "Identifier()\n"; + code += Indent + "public static function " + + namer_.LegacyPhpMethod(struct_def, "Identifier") + "()\n"; code += Indent + "{\n"; code += Indent + Indent + "return \""; code += parser_.file_identifier_ + "\";\n"; code += Indent + "}\n\n"; // Check if a buffer has the identifier. - code += Indent + "public static function " + struct_def.name; - code += "BufferHasIdentifier(ByteBuffer $buf)\n"; + code += Indent + "public static function " + + namer_.LegacyPhpMethod(struct_def, "BufferHasIdentifier") + + "(ByteBuffer $buf)\n"; code += Indent + "{\n"; code += Indent + Indent + "return self::"; code += "__has_identifier($buf, self::"; - code += struct_def.name + "Identifier());\n"; + code += namer_.LegacyPhpMethod(struct_def, "Identifier") + "());\n"; code += Indent + "}\n\n"; } if (parser_.file_extension_.length()) { // Return the extension - code += Indent + "public static function " + struct_def.name; - code += "Extension()\n"; + code += Indent + "public static function " + + namer_.LegacyPhpMethod(struct_def, "Extension") + "()\n"; code += Indent + "{\n"; code += Indent + Indent + "return \"" + parser_.file_extension_; code += "\";\n"; @@ -826,7 +925,7 @@ class PhpGenerator : public BaseGenerator { } // Generate enum declarations. - static void GenEnum(const EnumDef& enum_def, std::string* code_ptr) { + void GenEnum(const EnumDef& enum_def, std::string* code_ptr) { if (enum_def.generated) return; GenComment(enum_def.doc_comment, code_ptr, nullptr); @@ -858,7 +957,7 @@ class PhpGenerator : public BaseGenerator { } // Returns the function name that is able to read a value of the given type. - static std::string GenGetter(const Type& type) { + std::string GenGetter(const Type& type) { switch (type.base_type) { case BASE_TYPE_STRING: return "__string"; @@ -874,13 +973,13 @@ class PhpGenerator : public BaseGenerator { } // Returns the method name for use with add/put calls. - static std::string GenMethod(const FieldDef& field) { + std::string GenMethod(const FieldDef& field) { return IsScalar(field.value.type.base_type) ? ConvertCase(GenTypeBasic(field.value.type), Case::kUpperCamel) : (IsStruct(field.value.type) ? "Struct" : "Offset"); } - static std::string GenTypeBasic(const Type& type) { + std::string GenTypeBasic(const Type& type) { // clang-format off static const char *ctypename[] = { #define FLATBUFFERS_TD(ENUM, IDLTYPE, \ @@ -920,14 +1019,14 @@ class PhpGenerator : public BaseGenerator { } } - static std::string GenTypePointer(const Type& type) { + std::string GenTypePointer(const Type& type) { switch (type.base_type) { case BASE_TYPE_STRING: return "string"; case BASE_TYPE_VECTOR: return GenTypeGet(type.VectorType()); case BASE_TYPE_STRUCT: - return type.struct_def->name; + return namer_.Type(*type.struct_def); case BASE_TYPE_UNION: // fall through default: @@ -935,19 +1034,19 @@ class PhpGenerator : public BaseGenerator { } } - static std::string GenTypeGet(const Type& type) { + std::string GenTypeGet(const Type& type) { return IsScalar(type.base_type) ? GenTypeBasic(type) : GenTypePointer(type); } // Create a struct with a builder and the struct's arguments. - static void GenStructBuilder(const StructDef& struct_def, - std::string* code_ptr) { + void GenStructBuilder(const StructDef& struct_def, std::string* code_ptr) { std::string& code = *code_ptr; code += "\n"; code += Indent + "/**\n"; code += Indent + " * @return int offset\n"; code += Indent + " */\n"; - code += Indent + "public static function create" + struct_def.name; + code += Indent + "public static function " + + namer_.LegacyPhpMethod("create", struct_def); code += "(FlatBufferBuilder $builder"; StructBuilderArgs(struct_def, "", code_ptr); code += ")\n"; @@ -958,6 +1057,12 @@ class PhpGenerator : public BaseGenerator { code += Indent + Indent + "return $builder->offset();\n"; code += Indent + "}\n"; } + + std::string ScalarType(const FieldDef& field) { + return ConvertCase(GenTypeGet(field.value.type), Case::kUpperCamel); + } + + IdlNamer namer_; }; } // namespace php diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index 72d391e4b13..d198a072373 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -49,6 +49,7 @@ static Namer::Config RustDefaultConfig() { /*object_suffix=*/"T", /*keyword_prefix=*/"", /*keyword_suffix=*/"_", + /*keywords_casing=*/Namer::Config::KeywordsCasing::CaseSensitive, /*filenames=*/Case::kSnake, /*directories=*/Case::kSnake, /*output_path=*/"", diff --git a/src/idl_gen_swift.cpp b/src/idl_gen_swift.cpp index 63662b7fe6b..7d3a00d6c2c 100644 --- a/src/idl_gen_swift.cpp +++ b/src/idl_gen_swift.cpp @@ -47,6 +47,7 @@ static Namer::Config SwiftDefaultConfig() { /*object_suffix=*/"T", /*keyword_prefix=*/"", /*keyword_suffix=*/"_", + /*keywords_casing=*/Namer::Config::KeywordsCasing::CaseSensitive, /*filenames=*/Case::kKeep, /*directories=*/Case::kKeep, /*output_path=*/"", diff --git a/src/idl_gen_ts.cpp b/src/idl_gen_ts.cpp index aa05c23ab8f..14f366a83e8 100644 --- a/src/idl_gen_ts.cpp +++ b/src/idl_gen_ts.cpp @@ -67,6 +67,7 @@ Namer::Config TypeScriptDefaultConfig() { /*object_suffix=*/"T", /*keyword_prefix=*/"", /*keyword_suffix=*/"_", + /*keywords_casing=*/Namer::Config::KeywordsCasing::CaseSensitive, /*filenames=*/Case::kDasher, /*directories=*/Case::kDasher, /*output_path=*/"", diff --git a/tests/PhpKeywordTest/PhpKeywordTest/Bool.php b/tests/PhpKeywordTest/PhpKeywordTest/Bool.php new file mode 100644 index 00000000000..dbcdb2e4aad --- /dev/null +++ b/tests/PhpKeywordTest/PhpKeywordTest/Bool.php @@ -0,0 +1,89 @@ +init($bb->getInt($bb->getPosition()) + $bb->getPosition(), $bb)); + } + + /** + * @param int $_i offset + * @param ByteBuffer $_bb + * @return Bool_ + **/ + public function init($_i, ByteBuffer $_bb) + { + $this->bb_pos = $_i; + $this->bb = $_bb; + return $this; + } + + /** + * @return bool + */ + public function getValue() + { + $o = $this->__offset(4); + return $o != 0 ? $this->bb->getBool($o + $this->bb_pos) : false; + } + + /** + * @param FlatBufferBuilder $builder + * @return void + */ + public static function startBool_(FlatBufferBuilder $builder) + { + $builder->StartObject(1); + } + + /** + * @param FlatBufferBuilder $builder + * @return Bool_ + */ + public static function createBool_(FlatBufferBuilder $builder, $value) + { + $builder->startObject(1); + self::addValue($builder, $value); + $o = $builder->endObject(); + return $o; + } + + /** + * @param FlatBufferBuilder $builder + * @param bool + * @return void + */ + public static function addValue(FlatBufferBuilder $builder, $value) + { + $builder->addBoolX(0, $value, false); + } + + /** + * @param FlatBufferBuilder $builder + * @return int table offset + */ + public static function endBool_(FlatBufferBuilder $builder) + { + $o = $builder->endObject(); + return $o; + } + + public static function finishBoolBuffer(FlatBufferBuilder $builder, $offset) + { + $builder->finish($offset); + } +} diff --git a/tests/phpTest.php b/tests/phpTest.php index a854e783e35..0be3b0a0626 100644 --- a/tests/phpTest.php +++ b/tests/phpTest.php @@ -8,6 +8,7 @@ foreach (glob(join(DIRECTORY_SEPARATOR, array(dirname(__FILE__), "MyGame", "Example", "*.php"))) as $file) { require $file; } +require join(DIRECTORY_SEPARATOR, array(dirname(__FILE__), "PhpKeywordTest", "PhpKeywordTest", "Bool.php")); function main() { @@ -78,6 +79,7 @@ function main() testByteBuffer($assert); fuzzTest1($assert); // testUnicode($assert); + test_keyword_table($assert); echo 'FlatBuffers php test: completed successfully' . PHP_EOL; } @@ -598,6 +600,19 @@ function testByteBuffer(Assert $assert) { } +function test_keyword_table(Assert $assert) { + $fbb = new Google\FlatBuffers\FlatBufferBuilder(1); + + \PhpKeywordTest\Bool_::startBool_($fbb); + \PhpKeywordTest\Bool_::addValue($fbb, true); + $offset = \PhpKeywordTest\Bool_::endBool_($fbb); + \PhpKeywordTest\Bool_::finishBoolBuffer($fbb, $offset); + + $bb = $fbb->dataBuffer(); + $obj = \PhpKeywordTest\Bool_::getRootAsBool_($bb); + $assert->equal($obj->getValue(), true); +} + class Assert { public function ok($result, $message = "") { if (!$result){ diff --git a/tests/php_keyword_test.fbs b/tests/php_keyword_test.fbs new file mode 100644 index 00000000000..4878066661a --- /dev/null +++ b/tests/php_keyword_test.fbs @@ -0,0 +1,7 @@ +namespace PhpKeywordTest; + +table Bool { + value:bool; +} + +root_type Bool;