Referring to types from other namespaces in C++ now works correctly.

Previously, it would ignore the fact that the type comes from a
different namespace. Now they are pre-declared in their own namespace,
and referenced with a qualified name if necessary.

Bug: 16851682
Change-Id: I5cb625b86d28e7436b9e93c70a0fa16a600d9884
Tested: on Linux
parent be894f09
...@@ -159,6 +159,11 @@ template<typename T> class SymbolTable { ...@@ -159,6 +159,11 @@ template<typename T> class SymbolTable {
std::vector<T *> vec; // Used to iterate in order of insertion std::vector<T *> vec; // Used to iterate in order of insertion
}; };
// A name space, as set in the schema.
struct Namespace {
std::vector<std::string> components;
};
// Base class for all definition types (fields, structs_, enums_). // Base class for all definition types (fields, structs_, enums_).
struct Definition { struct Definition {
Definition() : generated(false) {} Definition() : generated(false) {}
...@@ -183,7 +188,8 @@ struct StructDef : public Definition { ...@@ -183,7 +188,8 @@ struct StructDef : public Definition {
predecl(true), predecl(true),
sortbysize(true), sortbysize(true),
minalign(1), minalign(1),
bytesize(0) bytesize(0),
defined_namespace(nullptr)
{} {}
void PadLastField(size_t minalign) { void PadLastField(size_t minalign) {
...@@ -198,6 +204,7 @@ struct StructDef : public Definition { ...@@ -198,6 +204,7 @@ struct StructDef : public Definition {
bool sortbysize; // Whether fields come in the declaration or size order. bool sortbysize; // Whether fields come in the declaration or size order.
size_t minalign; // What the whole object needs to be aligned to. size_t minalign; // What the whole object needs to be aligned to.
size_t bytesize; // Size if fixed. size_t bytesize; // Size if fixed.
Namespace *defined_namespace; // Where it was defined.
}; };
inline bool IsStruct(const Type &type) { inline bool IsStruct(const Type &type) {
...@@ -245,7 +252,16 @@ class Parser { ...@@ -245,7 +252,16 @@ class Parser {
root_struct_def(nullptr), root_struct_def(nullptr),
source_(nullptr), source_(nullptr),
cursor_(nullptr), cursor_(nullptr),
line_(1) {} line_(1) {
// Just in case none are declared:
namespaces_.push_back(new Namespace());
}
~Parser() {
for (auto it = namespaces_.begin(); it != namespaces_.end(); ++it) {
delete *it;
}
}
// Parse the string containing either schema or JSON data, which will // Parse the string containing either schema or JSON data, which will
// populate the SymbolTable's or the FlatBufferBuilder above. // populate the SymbolTable's or the FlatBufferBuilder above.
...@@ -284,7 +300,7 @@ class Parser { ...@@ -284,7 +300,7 @@ class Parser {
public: public:
SymbolTable<StructDef> structs_; SymbolTable<StructDef> structs_;
SymbolTable<EnumDef> enums_; SymbolTable<EnumDef> enums_;
std::vector<std::string> name_space_; // As set in the schema. std::vector<Namespace *> namespaces_;
std::string error_; // User readable error_ if Parse() == false std::string error_; // User readable error_ if Parse() == false
FlatBufferBuilder builder_; // any data contained in the file FlatBufferBuilder builder_; // any data contained in the file
......
This diff is collapsed.
...@@ -585,24 +585,24 @@ static bool SaveType(const Parser &parser, const Definition &def, ...@@ -585,24 +585,24 @@ static bool SaveType(const Parser &parser, const Definition &def,
bool needs_imports) { bool needs_imports) {
if (!classcode.length()) return true; if (!classcode.length()) return true;
std::string name_space_name; std::string namespace_name;
std::string name_space_dir = path; std::string namespace_dir = path;
for (auto it = parser.name_space_.begin(); auto &namespaces = parser.namespaces_.back()->components;
it != parser.name_space_.end(); ++it) { for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
if (name_space_name.length()) { if (namespace_name.length()) {
name_space_name += "."; namespace_name += ".";
name_space_dir += PATH_SEPARATOR; namespace_dir += PATH_SEPARATOR;
} }
name_space_name = *it; namespace_name = *it;
name_space_dir += *it; namespace_dir += *it;
mkdir(name_space_dir.c_str(), S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); mkdir(namespace_dir.c_str(), S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
} }
std::string code = ""; std::string code = "";
BeginFile(name_space_name, needs_imports, &code); BeginFile(namespace_name, needs_imports, &code);
code += classcode; code += classcode;
std::string filename = name_space_dir + PATH_SEPARATOR + def.name + ".go"; std::string filename = namespace_dir + PATH_SEPARATOR + def.name + ".go";
return SaveFile(filename.c_str(), code, false); return SaveFile(filename.c_str(), code, false);
} }
......
...@@ -341,27 +341,27 @@ static bool SaveClass(const Parser &parser, const Definition &def, ...@@ -341,27 +341,27 @@ static bool SaveClass(const Parser &parser, const Definition &def,
bool needs_imports) { bool needs_imports) {
if (!classcode.length()) return true; if (!classcode.length()) return true;
std::string name_space_java; std::string namespace_java;
std::string name_space_dir = path; std::string namespace_dir = path;
for (auto it = parser.name_space_.begin(); auto &namespaces = parser.namespaces_.back()->components;
it != parser.name_space_.end(); ++it) { for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
if (name_space_java.length()) { if (namespace_java.length()) {
name_space_java += "."; namespace_java += ".";
name_space_dir += kPathSeparator; namespace_dir += kPathSeparator;
} }
name_space_java += *it; namespace_java += *it;
name_space_dir += *it; namespace_dir += *it;
mkdir(name_space_dir.c_str(), S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); mkdir(namespace_dir.c_str(), S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
} }
std::string code = "// automatically generated, do not modify\n\n"; std::string code = "// automatically generated, do not modify\n\n";
code += "package " + name_space_java + ";\n\n"; code += "package " + namespace_java + ";\n\n";
if (needs_imports) { if (needs_imports) {
code += "import java.nio.*;\nimport java.lang.*;\nimport java.util.*;\n"; code += "import java.nio.*;\nimport java.lang.*;\nimport java.util.*;\n";
code += "import flatbuffers.*;\n\n"; code += "import flatbuffers.*;\n\n";
} }
code += classcode; code += classcode;
auto filename = name_space_dir + kPathSeparator + def.name + ".java"; auto filename = namespace_dir + kPathSeparator + def.name + ".java";
return SaveFile(filename.c_str(), code, false); return SaveFile(filename.c_str(), code, false);
} }
......
...@@ -647,6 +647,7 @@ StructDef *Parser::LookupCreateStruct(const std::string &name) { ...@@ -647,6 +647,7 @@ StructDef *Parser::LookupCreateStruct(const std::string &name) {
structs_.Add(name, struct_def); structs_.Add(name, struct_def);
struct_def->name = name; struct_def->name = name;
struct_def->predecl = true; struct_def->predecl = true;
struct_def->defined_namespace = namespaces_.back();
} }
return struct_def; return struct_def;
} }
...@@ -838,9 +839,10 @@ bool Parser::Parse(const char *source, const char *filepath) { ...@@ -838,9 +839,10 @@ bool Parser::Parse(const char *source, const char *filepath) {
while (token_ != kTokenEof) { while (token_ != kTokenEof) {
if (token_ == kTokenNameSpace) { if (token_ == kTokenNameSpace) {
Next(); Next();
name_space_.clear(); auto ns = new Namespace();
namespaces_.push_back(ns);
for (;;) { for (;;) {
name_space_.push_back(attribute_); ns->components.push_back(attribute_);
Expect(kTokenIdentifier); Expect(kTokenIdentifier);
if (!IsNext('.')) break; if (!IsNext('.')) break;
} }
......
include "include_test2.fbs"; // should be skipped include "include_test2.fbs"; // should be skipped
namespace MyGame.OtherNameSpace;
enum FromInclude:long { IncludeVal } enum FromInclude:long { IncludeVal }
struct Unused {}
...@@ -5,9 +5,19 @@ ...@@ -5,9 +5,19 @@
#include "flatbuffers/flatbuffers.h" #include "flatbuffers/flatbuffers.h"
namespace MyGame {
namespace OtherNameSpace {
struct Unused;
} // namespace OtherNameSpace
} // namespace MyGame
namespace MyGame { namespace MyGame {
namespace Example { namespace Example {
struct Test;
struct Vec3;
struct Monster;
enum { enum {
Color_Red = 1, Color_Red = 1,
Color_Green = 2, Color_Green = 2,
...@@ -35,10 +45,6 @@ inline const char *EnumNameAny(int e) { return EnumNamesAny()[e]; } ...@@ -35,10 +45,6 @@ inline const char *EnumNameAny(int e) { return EnumNamesAny()[e]; }
bool VerifyAny(const flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type); bool VerifyAny(const flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type);
struct Test;
struct Vec3;
struct Monster;
MANUALLY_ALIGNED_STRUCT(2) Test { MANUALLY_ALIGNED_STRUCT(2) Test {
private: private:
int16_t a_; int16_t a_;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment