Commit 4a3ebd71 authored by Robert Edmonds's avatar Robert Edmonds

protoc-gen-c/: Rename namespace google::protobuf::compiler::c → protobuf_c

Also rename GOOGLE_PROTOBUF_* include guard defines.
parent 44b90935
......@@ -69,14 +69,9 @@
#include "c_bytes_field.h"
#include "c_helpers.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
using internal::WireFormat;
void SetBytesVariables(const FieldDescriptor* descriptor,
void SetBytesVariables(const google::protobuf::FieldDescriptor* descriptor,
std::map<std::string, std::string>* variables) {
(*variables)["name"] = FieldName(descriptor);
(*variables)["default"] =
......@@ -87,7 +82,7 @@ void SetBytesVariables(const FieldDescriptor* descriptor,
// ===================================================================
BytesFieldGenerator::
BytesFieldGenerator(const FieldDescriptor* descriptor)
BytesFieldGenerator(const google::protobuf::FieldDescriptor* descriptor)
: FieldGenerator(descriptor) {
SetBytesVariables(descriptor, &variables_);
variables_["default_value"] = descriptor->has_default_value()
......@@ -97,24 +92,24 @@ BytesFieldGenerator(const FieldDescriptor* descriptor)
BytesFieldGenerator::~BytesFieldGenerator() {}
void BytesFieldGenerator::GenerateStructMembers(io::Printer* printer) const
void BytesFieldGenerator::GenerateStructMembers(google::protobuf::io::Printer* printer) const
{
switch (descriptor_->label()) {
case FieldDescriptor::LABEL_REQUIRED:
case google::protobuf::FieldDescriptor::LABEL_REQUIRED:
printer->Print(variables_, "ProtobufCBinaryData $name$$deprecated$;\n");
break;
case FieldDescriptor::LABEL_OPTIONAL:
case google::protobuf::FieldDescriptor::LABEL_OPTIONAL:
if (descriptor_->containing_oneof() == NULL && FieldSyntax(descriptor_) == 2)
printer->Print(variables_, "protobuf_c_boolean has_$name$$deprecated$;\n");
printer->Print(variables_, "ProtobufCBinaryData $name$$deprecated$;\n");
break;
case FieldDescriptor::LABEL_REPEATED:
case google::protobuf::FieldDescriptor::LABEL_REPEATED:
printer->Print(variables_, "size_t n_$name$$deprecated$;\n");
printer->Print(variables_, "ProtobufCBinaryData *$name$$deprecated$;\n");
break;
}
}
void BytesFieldGenerator::GenerateDefaultValueDeclarations(io::Printer* printer) const
void BytesFieldGenerator::GenerateDefaultValueDeclarations(google::protobuf::io::Printer* printer) const
{
std::map<std::string, std::string> vars;
vars["default_value_data"] = FullNameToLower(descriptor_->full_name(), descriptor_->file())
......@@ -122,7 +117,7 @@ void BytesFieldGenerator::GenerateDefaultValueDeclarations(io::Printer* printer)
printer->Print(vars, "extern uint8_t $default_value_data$[];\n");
}
void BytesFieldGenerator::GenerateDefaultValueImplementations(io::Printer* printer) const
void BytesFieldGenerator::GenerateDefaultValueImplementations(google::protobuf::io::Printer* printer) const
{
std::map<std::string, std::string> vars;
vars["default_value_data"] = FullNameToLower(descriptor_->full_name(), descriptor_->file())
......@@ -138,29 +133,26 @@ std::string BytesFieldGenerator::GetDefaultValue(void) const
+ FullNameToLower(descriptor_->full_name(), descriptor_->file())
+ "__default_value_data }";
}
void BytesFieldGenerator::GenerateStaticInit(io::Printer* printer) const
void BytesFieldGenerator::GenerateStaticInit(google::protobuf::io::Printer* printer) const
{
switch (descriptor_->label()) {
case FieldDescriptor::LABEL_REQUIRED:
case google::protobuf::FieldDescriptor::LABEL_REQUIRED:
printer->Print(variables_, "$default_value$");
break;
case FieldDescriptor::LABEL_OPTIONAL:
case google::protobuf::FieldDescriptor::LABEL_OPTIONAL:
if (FieldSyntax(descriptor_) == 2)
printer->Print(variables_, "0, ");
printer->Print(variables_, "$default_value$");
break;
case FieldDescriptor::LABEL_REPEATED:
case google::protobuf::FieldDescriptor::LABEL_REPEATED:
// no support for default?
printer->Print("0,NULL");
break;
}
}
void BytesFieldGenerator::GenerateDescriptorInitializer(io::Printer* printer) const
void BytesFieldGenerator::GenerateDescriptorInitializer(google::protobuf::io::Printer* printer) const
{
GenerateDescriptorInitializerGeneric(printer, true, "BYTES", "NULL");
}
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace google
} // namespace protobuf_c
......@@ -61,40 +61,35 @@
// Modified to implement C code by Dave Benson.
#ifndef GOOGLE_PROTOBUF_COMPILER_C_BYTES_FIELD_H__
#define GOOGLE_PROTOBUF_COMPILER_C_BYTES_FIELD_H__
#ifndef PROTOBUF_C_PROTOC_GEN_C_C_BYTES_FIELD_H__
#define PROTOBUF_C_PROTOC_GEN_C_C_BYTES_FIELD_H__
#include <map>
#include <string>
#include <google/protobuf/io/printer.h>
#include "c_field.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
class BytesFieldGenerator : public FieldGenerator {
public:
explicit BytesFieldGenerator(const FieldDescriptor* descriptor);
explicit BytesFieldGenerator(const google::protobuf::FieldDescriptor* descriptor);
~BytesFieldGenerator();
// implements FieldGenerator ---------------------------------------
void GenerateStructMembers(io::Printer* printer) const;
void GenerateDescriptorInitializer(io::Printer* printer) const;
void GenerateDefaultValueDeclarations(io::Printer* printer) const;
void GenerateDefaultValueImplementations(io::Printer* printer) const;
void GenerateStructMembers(google::protobuf::io::Printer* printer) const;
void GenerateDescriptorInitializer(google::protobuf::io::Printer* printer) const;
void GenerateDefaultValueDeclarations(google::protobuf::io::Printer* printer) const;
void GenerateDefaultValueImplementations(google::protobuf::io::Printer* printer) const;
std::string GetDefaultValue(void) const;
void GenerateStaticInit(io::Printer* printer) const;
void GenerateStaticInit(google::protobuf::io::Printer* printer) const;
private:
std::map<std::string, std::string> variables_;
};
} // namespace protobuf_c
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_C_STRING_FIELD_H__
#endif // PROTOBUF_C_PROTOC_GEN_C_C_BYTES_FIELD_H__
......@@ -62,19 +62,15 @@
// Modified to implement C code by Dave Benson.
#include <map>
#include <set>
#include <google/protobuf/io/printer.h>
#include "c_enum.h"
#include "c_helpers.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor,
EnumGenerator::EnumGenerator(const google::protobuf::EnumDescriptor* descriptor,
const std::string& dllexport_decl)
: descriptor_(descriptor),
dllexport_decl_(dllexport_decl) {
......@@ -82,21 +78,21 @@ EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor,
EnumGenerator::~EnumGenerator() {}
void EnumGenerator::GenerateDefinition(io::Printer* printer) {
void EnumGenerator::GenerateDefinition(google::protobuf::io::Printer* printer) {
std::map<std::string, std::string> vars;
vars["classname"] = FullNameToC(descriptor_->full_name(), descriptor_->file());
vars["shortname"] = descriptor_->name();
vars["uc_name"] = FullNameToUpper(descriptor_->full_name(), descriptor_->file());
SourceLocation sourceLoc;
google::protobuf::SourceLocation sourceLoc;
descriptor_->GetSourceLocation(&sourceLoc);
PrintComment (printer, sourceLoc.leading_comments);
printer->Print(vars, "typedef enum _$classname$ {\n");
printer->Indent();
const EnumValueDescriptor* min_value = descriptor_->value(0);
const EnumValueDescriptor* max_value = descriptor_->value(0);
const google::protobuf::EnumValueDescriptor* min_value = descriptor_->value(0);
const google::protobuf::EnumValueDescriptor* max_value = descriptor_->value(0);
vars["opt_comma"] = ",";
......@@ -107,7 +103,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) {
if (i + 1 == descriptor_->value_count())
vars["opt_comma"] = "";
SourceLocation valSourceLoc;
google::protobuf::SourceLocation valSourceLoc;
descriptor_->value(i)->GetSourceLocation(&valSourceLoc);
PrintComment (printer, valSourceLoc.leading_comments);
......@@ -127,7 +123,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) {
printer->Print(vars, "} $classname$;\n");
}
void EnumGenerator::GenerateDescriptorDeclarations(io::Printer* printer) {
void EnumGenerator::GenerateDescriptorDeclarations(google::protobuf::io::Printer* printer) {
std::map<std::string, std::string> vars;
if (dllexport_decl_.empty()) {
vars["dllexport"] = "";
......@@ -148,13 +144,13 @@ struct ValueIndex
unsigned final_index; /* index in uniqified array of values */
const char *name;
};
void EnumGenerator::GenerateValueInitializer(io::Printer *printer, int index)
void EnumGenerator::GenerateValueInitializer(google::protobuf::io::Printer *printer, int index)
{
const EnumValueDescriptor *vd = descriptor_->value(index);
const google::protobuf::EnumValueDescriptor *vd = descriptor_->value(index);
std::map<std::string, std::string> vars;
bool optimize_code_size = descriptor_->file()->options().has_optimize_for() &&
descriptor_->file()->options().optimize_for() ==
FileOptions_OptimizeMode_CODE_SIZE;
google::protobuf::FileOptions_OptimizeMode_CODE_SIZE;
vars["enum_value_name"] = vd->name();
vars["c_enum_value_name"] = FullNameToUpper(descriptor_->full_name(), descriptor_->file()) + "__" + vd->name();
vars["value"] = SimpleItoa(vd->number());
......@@ -183,7 +179,7 @@ static int compare_value_indices_by_name(const void *a, const void *b)
return strcmp (vi_a->name, vi_b->name);
}
void EnumGenerator::GenerateEnumDescriptor(io::Printer* printer) {
void EnumGenerator::GenerateEnumDescriptor(google::protobuf::io::Printer* printer) {
std::map<std::string, std::string> vars;
vars["fullname"] = descriptor_->full_name();
vars["lcclassname"] = FullNameToLower(descriptor_->full_name(), descriptor_->file());
......@@ -194,14 +190,14 @@ void EnumGenerator::GenerateEnumDescriptor(io::Printer* printer) {
bool optimize_code_size = descriptor_->file()->options().has_optimize_for() &&
descriptor_->file()->options().optimize_for() ==
FileOptions_OptimizeMode_CODE_SIZE;
google::protobuf::FileOptions_OptimizeMode_CODE_SIZE;
// Sort by name and value, dropping duplicate values if they appear later.
// TODO: use a c++ paradigm for this!
NameIndex *name_index = new NameIndex[descriptor_->value_count()];
ValueIndex *value_index = new ValueIndex[descriptor_->value_count()];
for (int j = 0; j < descriptor_->value_count(); j++) {
const EnumValueDescriptor *vd = descriptor_->value(j);
const google::protobuf::EnumValueDescriptor *vd = descriptor_->value(j);
name_index[j].index = j;
name_index[j].name = vd->name().c_str();
value_index[j].index = j;
......@@ -328,9 +324,4 @@ void EnumGenerator::GenerateEnumDescriptor(io::Printer* printer) {
delete[] name_index;
}
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace google
} // namespace protobuf_c
......@@ -61,23 +61,20 @@
// Modified to implement C code by Dave Benson.
#ifndef GOOGLE_PROTOBUF_COMPILER_C_ENUM_H__
#define GOOGLE_PROTOBUF_COMPILER_C_ENUM_H__
#ifndef PROTOBUF_C_PROTOC_GEN_C_C_ENUM_H__
#define PROTOBUF_C_PROTOC_GEN_C_C_ENUM_H__
#include <string>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/printer.h>
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
class EnumGenerator {
public:
// See generator.cc for the meaning of dllexport_decl.
explicit EnumGenerator(const EnumDescriptor* descriptor,
explicit EnumGenerator(const google::protobuf::EnumDescriptor* descriptor,
const std::string& dllexport_decl);
~EnumGenerator();
......@@ -86,28 +83,25 @@ class EnumGenerator {
// Generate header code defining the enum. This code should be placed
// within the enum's package namespace, but NOT within any class, even for
// nested enums.
void GenerateDefinition(io::Printer* printer);
void GenerateDefinition(google::protobuf::io::Printer* printer);
void GenerateDescriptorDeclarations(io::Printer* printer);
void GenerateDescriptorDeclarations(google::protobuf::io::Printer* printer);
// Source file stuff.
// Generate the ProtobufCEnumDescriptor for this enum
void GenerateEnumDescriptor(io::Printer* printer);
void GenerateEnumDescriptor(google::protobuf::io::Printer* printer);
// Generate static initializer for a ProtobufCEnumValue
// given the index of the value in the enum.
void GenerateValueInitializer(io::Printer *printer, int index);
void GenerateValueInitializer(google::protobuf::io::Printer *printer, int index);
private:
const EnumDescriptor* descriptor_;
const google::protobuf::EnumDescriptor* descriptor_;
std::string dllexport_decl_;
};
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace protobuf_c
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_C_ENUM_H__
#endif // PROTOBUF_C_PROTOC_GEN_C_C_ENUM_H__
......@@ -67,21 +67,16 @@
#include "c_enum_field.h"
#include "c_helpers.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
using internal::WireFormat;
namespace protobuf_c {
// TODO(kenton): Factor out a "SetCommonFieldVariables()" to get rid of
// repeat code between this and the other field types.
void SetEnumVariables(const FieldDescriptor* descriptor,
void SetEnumVariables(const google::protobuf::FieldDescriptor* descriptor,
std::map<std::string, std::string>* variables) {
(*variables)["name"] = FieldName(descriptor);
(*variables)["type"] = FullNameToC(descriptor->enum_type()->full_name(), descriptor->enum_type()->file());
const EnumValueDescriptor* default_value = descriptor->default_value_enum();
const google::protobuf::EnumValueDescriptor* default_value = descriptor->default_value_enum();
(*variables)["default"] = FullNameToUpper(default_value->type()->full_name(), default_value->type()->file())
+ "__" + default_value->name();
(*variables)["deprecated"] = FieldDeprecated(descriptor);
......@@ -90,7 +85,7 @@ void SetEnumVariables(const FieldDescriptor* descriptor,
// ===================================================================
EnumFieldGenerator::
EnumFieldGenerator(const FieldDescriptor* descriptor)
EnumFieldGenerator(const google::protobuf::FieldDescriptor* descriptor)
: FieldGenerator(descriptor)
{
SetEnumVariables(descriptor, &variables_);
......@@ -98,18 +93,18 @@ EnumFieldGenerator(const FieldDescriptor* descriptor)
EnumFieldGenerator::~EnumFieldGenerator() {}
void EnumFieldGenerator::GenerateStructMembers(io::Printer* printer) const
void EnumFieldGenerator::GenerateStructMembers(google::protobuf::io::Printer* printer) const
{
switch (descriptor_->label()) {
case FieldDescriptor::LABEL_REQUIRED:
case google::protobuf::FieldDescriptor::LABEL_REQUIRED:
printer->Print(variables_, "$type$ $name$$deprecated$;\n");
break;
case FieldDescriptor::LABEL_OPTIONAL:
case google::protobuf::FieldDescriptor::LABEL_OPTIONAL:
if (descriptor_->containing_oneof() == NULL && FieldSyntax(descriptor_) == 2)
printer->Print(variables_, "protobuf_c_boolean has_$name$$deprecated$;\n");
printer->Print(variables_, "$type$ $name$$deprecated$;\n");
break;
case FieldDescriptor::LABEL_REPEATED:
case google::protobuf::FieldDescriptor::LABEL_REPEATED:
printer->Print(variables_, "size_t n_$name$$deprecated$;\n");
printer->Print(variables_, "$type$ *$name$$deprecated$;\n");
break;
......@@ -120,32 +115,28 @@ std::string EnumFieldGenerator::GetDefaultValue(void) const
{
return variables_.find("default")->second;
}
void EnumFieldGenerator::GenerateStaticInit(io::Printer* printer) const
void EnumFieldGenerator::GenerateStaticInit(google::protobuf::io::Printer* printer) const
{
switch (descriptor_->label()) {
case FieldDescriptor::LABEL_REQUIRED:
case google::protobuf::FieldDescriptor::LABEL_REQUIRED:
printer->Print(variables_, "$default$");
break;
case FieldDescriptor::LABEL_OPTIONAL:
case google::protobuf::FieldDescriptor::LABEL_OPTIONAL:
if (FieldSyntax(descriptor_) == 2)
printer->Print(variables_, "0, ");
printer->Print(variables_, "$default$");
break;
case FieldDescriptor::LABEL_REPEATED:
case google::protobuf::FieldDescriptor::LABEL_REPEATED:
// no support for default?
printer->Print("0,NULL");
break;
}
}
void EnumFieldGenerator::GenerateDescriptorInitializer(io::Printer* printer) const
void EnumFieldGenerator::GenerateDescriptorInitializer(google::protobuf::io::Printer* printer) const
{
std::string addr = "&" + FullNameToLower(descriptor_->enum_type()->full_name(), descriptor_->enum_type()->file()) + "__descriptor";
GenerateDescriptorInitializerGeneric(printer, true, "ENUM", addr);
}
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace google
} // namespace protobuf_c
......@@ -61,38 +61,34 @@
// Modified to implement C code by Dave Benson.
#ifndef GOOGLE_PROTOBUF_COMPILER_C_ENUM_FIELD_H__
#define GOOGLE_PROTOBUF_COMPILER_C_ENUM_FIELD_H__
#ifndef PROTOBUF_C_PROTOC_GEN_C_C_ENUM_FIELD_H__
#define PROTOBUF_C_PROTOC_GEN_C_C_ENUM_FIELD_H__
#include <map>
#include <string>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/printer.h>
#include "c_field.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
class EnumFieldGenerator : public FieldGenerator {
public:
explicit EnumFieldGenerator(const FieldDescriptor* descriptor);
explicit EnumFieldGenerator(const google::protobuf::FieldDescriptor* descriptor);
~EnumFieldGenerator();
// implements FieldGenerator ---------------------------------------
void GenerateStructMembers(io::Printer* printer) const;
void GenerateDescriptorInitializer(io::Printer* printer) const;
void GenerateStructMembers(google::protobuf::io::Printer* printer) const;
void GenerateDescriptorInitializer(google::protobuf::io::Printer* printer) const;
std::string GetDefaultValue(void) const;
void GenerateStaticInit(io::Printer* printer) const;
void GenerateStaticInit(google::protobuf::io::Printer* printer) const;
private:
std::map<std::string, std::string> variables_;
};
} // namespace protobuf_c
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_C_ENUM_FIELD_H__
#endif // PROTOBUF_C_PROTOC_GEN_C_C_ENUM_FIELD_H__
......@@ -61,16 +61,14 @@
// Modified to implement C code by Dave Benson.
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/printer.h>
#include "c_extension.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
ExtensionGenerator::ExtensionGenerator(const FieldDescriptor* descriptor,
ExtensionGenerator::ExtensionGenerator(const google::protobuf::FieldDescriptor* descriptor,
const std::string& dllexport_decl)
: descriptor_(descriptor),
dllexport_decl_(dllexport_decl) {
......@@ -78,14 +76,10 @@ ExtensionGenerator::ExtensionGenerator(const FieldDescriptor* descriptor,
ExtensionGenerator::~ExtensionGenerator() {}
void ExtensionGenerator::GenerateDeclaration(io::Printer* printer) {
void ExtensionGenerator::GenerateDeclaration(google::protobuf::io::Printer* printer) {
}
void ExtensionGenerator::GenerateDefinition(io::Printer* printer) {
void ExtensionGenerator::GenerateDefinition(google::protobuf::io::Printer* printer) {
}
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace google
} // namespace protobuf_c
......@@ -61,8 +61,8 @@
// Modified to implement C code by Dave Benson.
#ifndef GOOGLE_PROTOBUF_COMPILER_C_EXTENSION_H__
#define GOOGLE_PROTOBUF_COMPILER_C_EXTENSION_H__
#ifndef PROTOBUF_C_PROTOC_GEN_C_C_EXTENSION_H__
#define PROTOBUF_C_PROTOC_GEN_C_C_EXTENSION_H__
#include <string>
......@@ -70,10 +70,7 @@
#include <google/protobuf/io/printer.h>
#include <google/protobuf/stubs/common.h>
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
// Generates code for an extension, which may be within the scope of some
// message or may be at file scope. This is much simpler than FieldGenerator
......@@ -81,25 +78,22 @@ namespace c {
class ExtensionGenerator {
public:
// See generator.cc for the meaning of dllexport_decl.
explicit ExtensionGenerator(const FieldDescriptor* descriptor,
explicit ExtensionGenerator(const google::protobuf::FieldDescriptor* descriptor,
const std::string& dllexport_decl);
~ExtensionGenerator();
// Header stuff.
void GenerateDeclaration(io::Printer* printer);
void GenerateDeclaration(google::protobuf::io::Printer* printer);
// Source file stuff.
void GenerateDefinition(io::Printer* printer);
void GenerateDefinition(google::protobuf::io::Printer* printer);
private:
const FieldDescriptor* descriptor_;
const google::protobuf::FieldDescriptor* descriptor_;
std::string type_traits_;
std::string dllexport_decl_;
};
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace protobuf_c
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_C_MESSAGE_H__
#endif // PROTOBUF_C_PROTOC_GEN_C_C_EXTENSION_H__
......@@ -61,6 +61,7 @@
// Modified to implement C code by Dave Benson.
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/stubs/common.h>
......@@ -74,43 +75,41 @@
#include "c_primitive_field.h"
#include "c_string_field.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
FieldGenerator::~FieldGenerator()
{
}
static bool is_packable_type(FieldDescriptor::Type type)
static bool is_packable_type(google::protobuf::FieldDescriptor::Type type)
{
return type == FieldDescriptor::TYPE_DOUBLE
|| type == FieldDescriptor::TYPE_FLOAT
|| type == FieldDescriptor::TYPE_INT64
|| type == FieldDescriptor::TYPE_UINT64
|| type == FieldDescriptor::TYPE_INT32
|| type == FieldDescriptor::TYPE_FIXED64
|| type == FieldDescriptor::TYPE_FIXED32
|| type == FieldDescriptor::TYPE_BOOL
|| type == FieldDescriptor::TYPE_UINT32
|| type == FieldDescriptor::TYPE_ENUM
|| type == FieldDescriptor::TYPE_SFIXED32
|| type == FieldDescriptor::TYPE_SFIXED64
|| type == FieldDescriptor::TYPE_SINT32
|| type == FieldDescriptor::TYPE_SINT64;
return type == google::protobuf::FieldDescriptor::TYPE_DOUBLE
|| type == google::protobuf::FieldDescriptor::TYPE_FLOAT
|| type == google::protobuf::FieldDescriptor::TYPE_INT64
|| type == google::protobuf::FieldDescriptor::TYPE_UINT64
|| type == google::protobuf::FieldDescriptor::TYPE_INT32
|| type == google::protobuf::FieldDescriptor::TYPE_FIXED64
|| type == google::protobuf::FieldDescriptor::TYPE_FIXED32
|| type == google::protobuf::FieldDescriptor::TYPE_BOOL
|| type == google::protobuf::FieldDescriptor::TYPE_UINT32
|| type == google::protobuf::FieldDescriptor::TYPE_ENUM
|| type == google::protobuf::FieldDescriptor::TYPE_SFIXED32
|| type == google::protobuf::FieldDescriptor::TYPE_SFIXED64
|| type == google::protobuf::FieldDescriptor::TYPE_SINT32
|| type == google::protobuf::FieldDescriptor::TYPE_SINT64;
//TYPE_BYTES
//TYPE_STRING
//TYPE_GROUP
//TYPE_MESSAGE
}
void FieldGenerator::GenerateDescriptorInitializerGeneric(io::Printer* printer,
void FieldGenerator::GenerateDescriptorInitializerGeneric(google::protobuf::io::Printer* printer,
bool optional_uses_has,
const std::string &type_macro,
const std::string &descriptor_addr) const
{
std::map<std::string, std::string> variables;
const OneofDescriptor *oneof = descriptor_->containing_oneof();
const google::protobuf::OneofDescriptor *oneof = descriptor_->containing_oneof();
const ProtobufCFileOptions opt = descriptor_->file()->options().GetExtension(pb_c_file);
variables["TYPE"] = type_macro;
variables["classname"] = FullNameToC(FieldScope(descriptor_)->full_name(), FieldScope(descriptor_)->file());
......@@ -125,7 +124,7 @@ void FieldGenerator::GenerateDescriptorInitializerGeneric(io::Printer* printer,
variables["oneofname"] = CamelToLower(oneof->name());
if (FieldSyntax(descriptor_) == 3 &&
descriptor_->label() == FieldDescriptor::LABEL_OPTIONAL) {
descriptor_->label() == google::protobuf::FieldDescriptor::LABEL_OPTIONAL) {
variables["LABEL"] = "NONE";
optional_uses_has = false;
} else {
......@@ -137,7 +136,7 @@ void FieldGenerator::GenerateDescriptorInitializerGeneric(io::Printer* printer,
+ FullNameToLower(descriptor_->full_name(), descriptor_->file())
+ "__default_value";
} else if (FieldSyntax(descriptor_) == 3 &&
descriptor_->type() == FieldDescriptor::TYPE_STRING) {
descriptor_->type() == google::protobuf::FieldDescriptor::TYPE_STRING) {
variables["default_value"] = "&protobuf_c_empty_string";
} else {
variables["default_value"] = "NULL";
......@@ -145,11 +144,11 @@ void FieldGenerator::GenerateDescriptorInitializerGeneric(io::Printer* printer,
variables["flags"] = "0";
if (descriptor_->label() == FieldDescriptor::LABEL_REPEATED
if (descriptor_->label() == google::protobuf::FieldDescriptor::LABEL_REPEATED
&& is_packable_type (descriptor_->type())
&& descriptor_->options().packed()) {
variables["flags"] += " | PROTOBUF_C_FIELD_FLAG_PACKED";
} else if (descriptor_->label() == FieldDescriptor::LABEL_REPEATED
} else if (descriptor_->label() == google::protobuf::FieldDescriptor::LABEL_REPEATED
&& is_packable_type (descriptor_->type())
&& FieldSyntax(descriptor_) == 3
&& !descriptor_->options().has_packed()) {
......@@ -170,7 +169,7 @@ void FieldGenerator::GenerateDescriptorInitializerGeneric(io::Printer* printer,
printer->Print("{\n");
if (descriptor_->file()->options().has_optimize_for() &&
descriptor_->file()->options().optimize_for() ==
FileOptions_OptimizeMode_CODE_SIZE) {
google::protobuf::FileOptions_OptimizeMode_CODE_SIZE) {
printer->Print(" NULL, /* CODE_SIZE */\n");
} else {
printer->Print(variables, " \"$proto_name$\",\n");
......@@ -180,10 +179,10 @@ void FieldGenerator::GenerateDescriptorInitializerGeneric(io::Printer* printer,
" PROTOBUF_C_LABEL_$LABEL$,\n"
" PROTOBUF_C_TYPE_$TYPE$,\n");
switch (descriptor_->label()) {
case FieldDescriptor::LABEL_REQUIRED:
case google::protobuf::FieldDescriptor::LABEL_REQUIRED:
printer->Print(variables, " 0, /* quantifier_offset */\n");
break;
case FieldDescriptor::LABEL_OPTIONAL:
case google::protobuf::FieldDescriptor::LABEL_OPTIONAL:
if (oneof != NULL) {
printer->Print(variables, " offsetof($classname$, $oneofname$_case),\n");
} else if (optional_uses_has) {
......@@ -192,7 +191,7 @@ void FieldGenerator::GenerateDescriptorInitializerGeneric(io::Printer* printer,
printer->Print(variables, " 0, /* quantifier_offset */\n");
}
break;
case FieldDescriptor::LABEL_REPEATED:
case google::protobuf::FieldDescriptor::LABEL_REPEATED:
printer->Print(variables, " offsetof($classname$, n_$name$),\n");
break;
}
......@@ -204,7 +203,7 @@ void FieldGenerator::GenerateDescriptorInitializerGeneric(io::Printer* printer,
printer->Print("},\n");
}
FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor)
FieldGeneratorMap::FieldGeneratorMap(const google::protobuf::Descriptor* descriptor)
: descriptor_(descriptor),
field_generators_(
new std::unique_ptr<FieldGenerator>[descriptor->field_count()]) {
......@@ -214,21 +213,21 @@ FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor)
}
}
FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field) {
FieldGenerator* FieldGeneratorMap::MakeGenerator(const google::protobuf::FieldDescriptor* field) {
const ProtobufCFieldOptions opt = field->options().GetExtension(pb_c_field);
switch (field->type()) {
case FieldDescriptor::TYPE_MESSAGE:
case google::protobuf::FieldDescriptor::TYPE_MESSAGE:
return new MessageFieldGenerator(field);
case FieldDescriptor::TYPE_STRING:
case google::protobuf::FieldDescriptor::TYPE_STRING:
if (opt.string_as_bytes())
return new BytesFieldGenerator(field);
else
return new StringFieldGenerator(field);
case FieldDescriptor::TYPE_BYTES:
case google::protobuf::FieldDescriptor::TYPE_BYTES:
return new BytesFieldGenerator(field);
case FieldDescriptor::TYPE_ENUM:
case google::protobuf::FieldDescriptor::TYPE_ENUM:
return new EnumFieldGenerator(field);
case FieldDescriptor::TYPE_GROUP:
case google::protobuf::FieldDescriptor::TYPE_GROUP:
return 0; // XXX
default:
return new PrimitiveFieldGenerator(field);
......@@ -238,12 +237,9 @@ FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field) {
FieldGeneratorMap::~FieldGeneratorMap() {}
const FieldGenerator& FieldGeneratorMap::get(
const FieldDescriptor* field) const {
const google::protobuf::FieldDescriptor* field) const {
GOOGLE_CHECK_EQ(field->containing_type(), descriptor_);
return *field_generators_[field->index()];
}
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace google
} // namespace protobuf_c
......@@ -61,8 +61,8 @@
// Modified to implement C code by Dave Benson.
#ifndef GOOGLE_PROTOBUF_COMPILER_C_FIELD_H__
#define GOOGLE_PROTOBUF_COMPILER_C_FIELD_H__
#ifndef PROTOBUF_C_PROTOC_GEN_C_C_FIELD_H__
#define PROTOBUF_C_PROTOC_GEN_C_C_FIELD_H__
#include <memory>
......@@ -70,56 +70,50 @@
#include <google/protobuf/io/printer.h>
#include <google/protobuf/stubs/common.h>
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
class FieldGenerator {
public:
explicit FieldGenerator(const FieldDescriptor *descriptor) : descriptor_(descriptor) {}
explicit FieldGenerator(const google::protobuf::FieldDescriptor *descriptor) : descriptor_(descriptor) {}
virtual ~FieldGenerator();
// Generate definitions to be included in the structure.
virtual void GenerateStructMembers(io::Printer* printer) const = 0;
virtual void GenerateStructMembers(google::protobuf::io::Printer* printer) const = 0;
// Generate a static initializer for this field.
virtual void GenerateDescriptorInitializer(io::Printer* printer) const = 0;
virtual void GenerateDescriptorInitializer(google::protobuf::io::Printer* printer) const = 0;
virtual void GenerateDefaultValueDeclarations(io::Printer* printer) const { }
virtual void GenerateDefaultValueImplementations(io::Printer* printer) const { }
virtual void GenerateDefaultValueDeclarations(google::protobuf::io::Printer* printer) const { }
virtual void GenerateDefaultValueImplementations(google::protobuf::io::Printer* printer) const { }
virtual std::string GetDefaultValue() const = 0;
// Generate members to initialize this field from a static initializer
virtual void GenerateStaticInit(io::Printer* printer) const = 0;
virtual void GenerateStaticInit(google::protobuf::io::Printer* printer) const = 0;
protected:
void GenerateDescriptorInitializerGeneric(io::Printer* printer,
void GenerateDescriptorInitializerGeneric(google::protobuf::io::Printer* printer,
bool optional_uses_has,
const std::string &type_macro,
const std::string &descriptor_addr) const;
const FieldDescriptor *descriptor_;
const google::protobuf::FieldDescriptor *descriptor_;
};
// Convenience class which constructs FieldGenerators for a Descriptor.
class FieldGeneratorMap {
public:
explicit FieldGeneratorMap(const Descriptor* descriptor);
explicit FieldGeneratorMap(const google::protobuf::Descriptor* descriptor);
~FieldGeneratorMap();
const FieldGenerator& get(const FieldDescriptor* field) const;
const FieldGenerator& get(const google::protobuf::FieldDescriptor* field) const;
private:
const Descriptor* descriptor_;
const google::protobuf::Descriptor* descriptor_;
std::unique_ptr<std::unique_ptr<FieldGenerator>[]> field_generators_;
static FieldGenerator* MakeGenerator(const FieldDescriptor* field);
static FieldGenerator* MakeGenerator(const google::protobuf::FieldDescriptor* field);
};
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace protobuf_c
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_C_FIELD_H__
#endif // PROTOBUF_C_PROTOC_GEN_C_C_FIELD_H__
......@@ -61,6 +61,7 @@
// Modified to implement C code by Dave Benson.
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/printer.h>
#include <protobuf-c/protobuf-c.pb.h>
......@@ -73,14 +74,11 @@
#include "c_message.h"
#include "c_service.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
// ===================================================================
FileGenerator::FileGenerator(const FileDescriptor* file,
FileGenerator::FileGenerator(const google::protobuf::FileDescriptor* file,
const std::string& dllexport_decl)
: file_(file),
message_generators_(
......@@ -115,7 +113,7 @@ FileGenerator::FileGenerator(const FileDescriptor* file,
FileGenerator::~FileGenerator() {}
void FileGenerator::GenerateHeader(io::Printer* printer) {
void FileGenerator::GenerateHeader(google::protobuf::io::Printer* printer) {
std::string filename_identifier = FilenameIdentifier(file_->name());
const int min_header_version = 1003000;
......@@ -228,7 +226,7 @@ void FileGenerator::GenerateHeader(io::Printer* printer) {
"filename_identifier", filename_identifier);
}
void FileGenerator::GenerateSource(io::Printer* printer) {
void FileGenerator::GenerateSource(google::protobuf::io::Printer* printer) {
printer->Print(
"/* Generated by the protocol buffer compiler. DO NOT EDIT! */\n"
"/* Generated from: $filename$ */\n"
......@@ -264,7 +262,4 @@ void FileGenerator::GenerateSource(io::Printer* printer) {
}
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace google
} // namespace protobuf_c
......@@ -61,8 +61,8 @@
// Modified to implement C code by Dave Benson.
#ifndef GOOGLE_PROTOBUF_COMPILER_C_FILE_H__
#define GOOGLE_PROTOBUF_COMPILER_C_FILE_H__
#ifndef PROTOBUF_C_PROTOC_GEN_C_C_FILE_H__
#define PROTOBUF_C_PROTOC_GEN_C_C_FILE_H__
#include <memory>
#include <string>
......@@ -72,30 +72,26 @@
#include <google/protobuf/io/printer.h>
#include <google/protobuf/stubs/common.h>
#include "c_enum.h"
#include "c_extension.h"
#include "c_field.h"
#include "c_message.h"
#include "c_service.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
class EnumGenerator; // enum.h
class MessageGenerator; // message.h
class ServiceGenerator; // service.h
class ExtensionGenerator; // extension.h
namespace protobuf_c {
class FileGenerator {
public:
// See generator.cc for the meaning of dllexport_decl.
explicit FileGenerator(const FileDescriptor* file,
explicit FileGenerator(const google::protobuf::FileDescriptor* file,
const std::string& dllexport_decl);
~FileGenerator();
void GenerateHeader(io::Printer* printer);
void GenerateSource(io::Printer* printer);
void GenerateHeader(google::protobuf::io::Printer* printer);
void GenerateSource(google::protobuf::io::Printer* printer);
private:
const FileDescriptor* file_;
const google::protobuf::FileDescriptor* file_;
std::unique_ptr<std::unique_ptr<MessageGenerator>[]> message_generators_;
std::unique_ptr<std::unique_ptr<EnumGenerator>[]> enum_generators_;
......@@ -103,9 +99,6 @@ class FileGenerator {
std::unique_ptr<std::unique_ptr<ExtensionGenerator>[]> extension_generators_;
};
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace protobuf_c
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_C_FILE_H__
#endif // PROTOBUF_C_PROTOC_GEN_C_C_FILE_H__
......@@ -73,10 +73,7 @@
#include "c_generator.h"
#include "c_helpers.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
// Parses a set of comma-delimited name/value pairs, e.g.:
// "foo=bar,baz,qux=corge"
......@@ -103,9 +100,9 @@ void ParseOptions(const std::string& text, std::vector<std::pair<std::string, st
CGenerator::CGenerator() {}
CGenerator::~CGenerator() {}
bool CGenerator::Generate(const FileDescriptor* file,
bool CGenerator::Generate(const google::protobuf::FileDescriptor* file,
const std::string& parameter,
OutputDirectory* output_directory,
google::protobuf::compiler::OutputDirectory* output_directory,
std::string* error) const {
if (file->options().GetExtension(pb_c_file).no_generate())
return true;
......@@ -154,24 +151,21 @@ bool CGenerator::Generate(const FileDescriptor* file,
// Generate header.
{
std::unique_ptr<io::ZeroCopyOutputStream> output(
std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output(
output_directory->Open(basename + ".h"));
io::Printer printer(output.get(), '$');
google::protobuf::io::Printer printer(output.get(), '$');
file_generator.GenerateHeader(&printer);
}
// Generate cc file.
{
std::unique_ptr<io::ZeroCopyOutputStream> output(
std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output(
output_directory->Open(basename + ".c"));
io::Printer printer(output.get(), '$');
google::protobuf::io::Printer printer(output.get(), '$');
file_generator.GenerateSource(&printer);
}
return true;
}
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace google
} // namespace protobuf_c
......@@ -63,49 +63,43 @@
// Generates C code for a given .proto file.
#ifndef GOOGLE_PROTOBUF_COMPILER_C_GENERATOR_H__
#define GOOGLE_PROTOBUF_COMPILER_C_GENERATOR_H__
#ifndef PROTOBUF_C_PROTOC_GEN_C_C_GENERATOR_H__
#define PROTOBUF_C_PROTOC_GEN_C_C_GENERATOR_H__
#include <string>
#include <google/protobuf/compiler/code_generator.h>
#if defined(_WIN32) && defined(PROTOBUF_C_USE_SHARED_LIB)
# define PROTOC_C_EXPORT __declspec(dllexport)
# define PROTOBUF_C_EXPORT __declspec(dllexport)
#else
# define PROTOC_C_EXPORT
# define PROTOBUF_C_EXPORT
#endif
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
// CodeGenerator implementation which generates a C++ source file and
// header. If you create your own protocol compiler binary and you want
// it to support C++ output, you can do so by registering an instance of this
// CodeGenerator with the CommandLineInterface in your main() function.
class PROTOC_C_EXPORT CGenerator : public CodeGenerator {
class PROTOBUF_C_EXPORT CGenerator : public google::protobuf::compiler::CodeGenerator {
public:
CGenerator();
~CGenerator();
// implements CodeGenerator ----------------------------------------
bool Generate(const FileDescriptor* file,
bool Generate(const google::protobuf::FileDescriptor* file,
const std::string& parameter,
OutputDirectory* output_directory,
google::protobuf::compiler::OutputDirectory* output_directory,
std::string* error) const;
#if GOOGLE_PROTOBUF_VERSION >= 5026000
uint64_t GetSupportedFeatures() const { return 0; }
Edition GetMinimumEdition() const { return Edition::EDITION_PROTO2; }
Edition GetMaximumEdition() const { return Edition::EDITION_PROTO3; }
google::protobuf::Edition GetMinimumEdition() const { return google::protobuf::Edition::EDITION_PROTO2; }
google::protobuf::Edition GetMaximumEdition() const { return google::protobuf::Edition::EDITION_PROTO3; }
#endif
};
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace protobuf_c
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_C_GENERATOR_H__
#endif // PROTOBUF_C_PROTOC_GEN_C_C_GENERATOR_H__
......@@ -61,6 +61,7 @@
// Modified to implement C code by Dave Benson.
#include <cstdint>
#include <memory>
#include <set>
#include <vector>
......@@ -68,14 +69,12 @@
#include <float.h>
#include <stdio.h> // for snprintf
#include <google/protobuf/descriptor.h>
#include <google/protobuf/stubs/common.h>
#include "c_helpers.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
#if defined(_MSC_VER)
// FIXME: In the case where the generated string is longer than the buffer,
......@@ -180,8 +179,7 @@ std::string ToCamel(const std::string &name) {
return rv;
}
std::string OverrideFullName(const std::string &full_name,
const FileDescriptor *file) {
std::string OverrideFullName(const std::string &full_name, const google::protobuf::FileDescriptor* file) {
const ProtobufCFileOptions opt = file->options().GetExtension(pb_c_file);
if (!opt.has_c_package())
return full_name;
......@@ -193,8 +191,7 @@ std::string OverrideFullName(const std::string &full_name,
return new_name + full_name.substr(file->package().length());
}
std::string FullNameToLower(const std::string &full_name,
const FileDescriptor *file) {
std::string FullNameToLower(const std::string &full_name, const google::protobuf::FileDescriptor* file) {
std::vector<std::string> pieces;
SplitStringUsing(OverrideFullName(full_name, file), ".", &pieces);
std::string rv = "";
......@@ -205,8 +202,7 @@ std::string FullNameToLower(const std::string &full_name,
}
return rv;
}
std::string FullNameToUpper(const std::string &full_name,
const FileDescriptor *file) {
std::string FullNameToUpper(const std::string &full_name, const google::protobuf::FileDescriptor* file) {
std::vector<std::string> pieces;
SplitStringUsing(OverrideFullName(full_name, file), ".", &pieces);
std::string rv = "";
......@@ -217,8 +213,7 @@ std::string FullNameToUpper(const std::string &full_name,
}
return rv;
}
std::string FullNameToC(const std::string &full_name,
const FileDescriptor *file) {
std::string FullNameToC(const std::string &full_name, const google::protobuf::FileDescriptor* file) {
std::vector<std::string> pieces;
SplitStringUsing(OverrideFullName(full_name, file), ".", &pieces);
std::string rv = "";
......@@ -230,7 +225,7 @@ std::string FullNameToC(const std::string &full_name,
return rv;
}
void PrintComment (io::Printer* printer, std::string comment)
void PrintComment(google::protobuf::io::Printer* printer, std::string comment)
{
if (!comment.empty())
{
......@@ -297,7 +292,7 @@ std::set<std::string> MakeKeywordsMap() {
std::set<std::string> kKeywords = MakeKeywordsMap();
std::string FieldName(const FieldDescriptor* field) {
std::string FieldName(const google::protobuf::FieldDescriptor* field) {
std::string result = ToLower(field->name());
if (kKeywords.count(result) > 0) {
result.append("_");
......@@ -305,7 +300,7 @@ std::string FieldName(const FieldDescriptor* field) {
return result;
}
std::string FieldDeprecated(const FieldDescriptor* field) {
std::string FieldDeprecated(const google::protobuf::FieldDescriptor* field) {
if (field->options().deprecated()) {
return " PROTOBUF_C__DEPRECATED";
}
......@@ -331,7 +326,7 @@ std::string FilenameIdentifier(const std::string& filename) {
// use the hex code for the character.
result.push_back('_');
char buffer[32];
result.append(FastHexToBuffer(static_cast<uint8>(filename[i]), buffer));
result.append(FastHexToBuffer(static_cast<std::uint8_t>(filename[i]), buffer));
}
}
return result;
......@@ -342,17 +337,17 @@ std::string GlobalBuildDescriptorsName(const std::string& filename) {
return "proto_BuildDescriptors_" + FilenameIdentifier(filename);
}
std::string GetLabelName(FieldDescriptor::Label label) {
std::string GetLabelName(google::protobuf::FieldDescriptor::Label label) {
switch (label) {
case FieldDescriptor::LABEL_OPTIONAL: return "optional";
case FieldDescriptor::LABEL_REQUIRED: return "required";
case FieldDescriptor::LABEL_REPEATED: return "repeated";
case google::protobuf::FieldDescriptor::LABEL_OPTIONAL: return "optional";
case google::protobuf::FieldDescriptor::LABEL_REQUIRED: return "required";
case google::protobuf::FieldDescriptor::LABEL_REPEATED: return "repeated";
}
return "bad-label";
}
unsigned
WriteIntRanges(io::Printer* printer, int n_values, const int *values, const std::string &name)
WriteIntRanges(google::protobuf::io::Printer* printer, int n_values, const int *values, const std::string &name)
{
std::map<std::string, std::string> vars;
vars["name"] = name;
......@@ -530,7 +525,7 @@ static int CEscapeInternal(const char* src, int src_len, char* dest,
if (dest_len - used < 4) // need space for 4 letter escape
return -1;
snprintf(dest + used, dest_len - used, (use_hex ? "\\x%02x" : "\\%03o"),
static_cast<uint8>(*src));
static_cast<std::uint8_t>(*src));
is_hex_escape = use_hex;
used += 4;
} else {
......@@ -555,7 +550,4 @@ std::string CEscape(const std::string& src) {
return std::string(dest.get(), len);
}
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace google
} // namespace protobuf_c
......@@ -61,8 +61,8 @@
// Modified to implement C code by Dave Benson.
#ifndef GOOGLE_PROTOBUF_COMPILER_C_HELPERS_H__
#define GOOGLE_PROTOBUF_COMPILER_C_HELPERS_H__
#ifndef PROTOBUF_C_PROTOC_GEN_C_C_HELPERS_H__
#define PROTOBUF_C_PROTOC_GEN_C_C_HELPERS_H__
#include <string>
#include <vector>
......@@ -73,10 +73,7 @@
#include <protobuf-c/protobuf-c.pb.h>
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
// --- Borrowed from stubs. ---
template <typename T> std::string SimpleItoa(T n) {
......@@ -99,14 +96,14 @@ char* FastHexToBuffer(int i, char* buffer);
// The name is coerced to lower-case to emulate proto1 behavior. People
// should be using lowercase-with-underscores style for proto field names
// anyway, so normally this just returns field->name().
std::string FieldName(const FieldDescriptor* field);
std::string FieldName(const google::protobuf::FieldDescriptor* field);
// Get macro string for deprecated field
std::string FieldDeprecated(const FieldDescriptor* field);
std::string FieldDeprecated(const google::protobuf::FieldDescriptor* field);
// Returns the scope where the field was defined (for extensions, this is
// different from the message type to which the field applies).
inline const Descriptor* FieldScope(const FieldDescriptor* field) {
inline const google::protobuf::Descriptor* FieldScope(const google::protobuf::FieldDescriptor* field) {
return field->is_extension() ?
field->extension_scope() : field->containing_type();
}
......@@ -124,14 +121,14 @@ std::string ToLower(const std::string &class_name);
std::string ToUpper(const std::string &class_name);
// full_name() to lowercase with underscores
std::string FullNameToLower(const std::string &full_name, const FileDescriptor *file);
std::string FullNameToUpper(const std::string &full_name, const FileDescriptor *file);
std::string FullNameToLower(const std::string &full_name, const google::protobuf::FileDescriptor *file);
std::string FullNameToUpper(const std::string &full_name, const google::protobuf::FileDescriptor *file);
// full_name() to c-typename (with underscores for packages, otherwise camel case)
std::string FullNameToC(const std::string &class_name, const FileDescriptor *file);
std::string FullNameToC(const std::string &class_name, const google::protobuf::FileDescriptor *file);
// Splits, indents, formats, and prints comment lines
void PrintComment (io::Printer* printer, std::string comment);
void PrintComment(google::protobuf::io::Printer* printer, std::string comment);
// make a string of spaces as long as input
std::string ConvertToSpaces(const std::string &input);
......@@ -144,11 +141,11 @@ std::string StripProto(const std::string& filename);
// with a ::. If you are using the type as a template parameter, you will
// need to insure there is a space between the < and the ::, because the
// ridiculous C++ standard defines "<:" to be a synonym for "[".
const char* PrimitiveTypeName(FieldDescriptor::CppType type);
const char* PrimitiveTypeName(google::protobuf::FieldDescriptor::CppType type);
// Get the declared type name in CamelCase format, as is used e.g. for the
// methods of WireFormat. For example, TYPE_INT32 becomes "Int32".
const char* DeclaredTypeMethodName(FieldDescriptor::Type type);
const char* DeclaredTypeMethodName(google::protobuf::FieldDescriptor::Type type);
// Convert a file name into a valid identifier.
std::string FilenameIdentifier(const std::string& filename);
......@@ -157,12 +154,12 @@ std::string FilenameIdentifier(const std::string& filename);
std::string GlobalBuildDescriptorsName(const std::string& filename);
// return 'required', 'optional', or 'repeated'
std::string GetLabelName(FieldDescriptor::Label label);
std::string GetLabelName(google::protobuf::FieldDescriptor::Label label);
// write IntRanges entries for a bunch of sorted values.
// returns the number of ranges there are to bsearch.
unsigned WriteIntRanges(io::Printer* printer, int n_values, const int *values, const std::string &name);
unsigned WriteIntRanges(google::protobuf::io::Printer* printer, int n_values, const int *values, const std::string &name);
struct NameIndex
{
......@@ -172,8 +169,8 @@ struct NameIndex
int compare_name_indices_by_name(const void*, const void*);
// Return the syntax version of the file containing the field.
inline int FieldSyntax(const FieldDescriptor* field) {
auto proto = FileDescriptorProto();
inline int FieldSyntax(const google::protobuf::FieldDescriptor* field) {
auto proto = google::protobuf::FileDescriptorProto();
field->file()->CopyTo(&proto);
if (proto.has_syntax()) {
......@@ -199,9 +196,6 @@ inline int FieldSyntax(const FieldDescriptor* field) {
# define GOOGLE_LOG ABSL_LOG
#endif
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace protobuf_c
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_C_HELPERS_H__
#endif // PROTOBUF_C_PROTOC_GEN_C_C_HELPERS_H__
This diff is collapsed.
......@@ -61,8 +61,8 @@
// Modified to implement C code by Dave Benson.
#ifndef GOOGLE_PROTOBUF_COMPILER_C_MESSAGE_H__
#define GOOGLE_PROTOBUF_COMPILER_C_MESSAGE_H__
#ifndef PROTOBUF_C_PROTOC_GEN_C_C_MESSAGE_H__
#define PROTOBUF_C_PROTOC_GEN_C_C_MESSAGE_H__
#include <memory>
#include <string>
......@@ -74,41 +74,38 @@
#include "c_extension.h"
#include "c_field.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
class MessageGenerator {
public:
// See generator.cc for the meaning of dllexport_decl.
explicit MessageGenerator(const Descriptor* descriptor,
explicit MessageGenerator(const google::protobuf::Descriptor* descriptor,
const std::string& dllexport_decl);
~MessageGenerator();
// Header stuff.
// Generate typedef.
void GenerateStructTypedef(io::Printer* printer);
void GenerateStructTypedef(google::protobuf::io::Printer* printer);
// Generate descriptor prototype
void GenerateDescriptorDeclarations(io::Printer* printer);
void GenerateDescriptorDeclarations(google::protobuf::io::Printer* printer);
// Generate descriptor prototype
void GenerateClosureTypedef(io::Printer* printer);
void GenerateClosureTypedef(google::protobuf::io::Printer* printer);
// Generate definitions of all nested enums (must come before class
// definitions because those classes use the enums definitions).
void GenerateEnumDefinitions(io::Printer* printer);
void GenerateEnumDefinitions(google::protobuf::io::Printer* printer);
// Generate definitions for this class and all its nested types.
void GenerateStructDefinition(io::Printer* printer);
void GenerateStructDefinition(google::protobuf::io::Printer* printer);
// Generate __INIT macro for populating this structure
void GenerateStructStaticInitMacro(io::Printer* printer);
void GenerateStructStaticInitMacro(google::protobuf::io::Printer* printer);
// Generate standard helper functions declarations for this message.
void GenerateHelperFunctionDeclarations(io::Printer* printer,
void GenerateHelperFunctionDeclarations(google::protobuf::io::Printer* printer,
bool is_pack_deep,
bool gen_pack,
bool gen_init);
......@@ -117,17 +114,17 @@ class MessageGenerator {
// Generate code that initializes the global variable storing the message's
// descriptor.
void GenerateMessageDescriptor(io::Printer* printer, bool gen_init);
void GenerateHelperFunctionDefinitions(io::Printer* printer,
void GenerateMessageDescriptor(google::protobuf::io::Printer* printer, bool gen_init);
void GenerateHelperFunctionDefinitions(google::protobuf::io::Printer* printer,
bool is_pack_deep,
bool gen_pack,
bool gen_init);
private:
int GetOneofUnionOrder(const FieldDescriptor *fd);
int GetOneofUnionOrder(const google::protobuf::FieldDescriptor *fd);
const Descriptor* descriptor_;
const google::protobuf::Descriptor* descriptor_;
std::string dllexport_decl_;
FieldGeneratorMap field_generators_;
std::unique_ptr<std::unique_ptr<MessageGenerator>[]> nested_generators_;
......@@ -135,9 +132,6 @@ class MessageGenerator {
std::unique_ptr<std::unique_ptr<ExtensionGenerator>[]> extension_generators_;
};
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace protobuf_c
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_C_MESSAGE_H__
#endif // PROTOBUF_C_PROTOC_GEN_C_C_MESSAGE_H__
......@@ -61,40 +61,34 @@
// Modified to implement C code by Dave Benson.
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/wire_format.h>
#include "c_helpers.h"
#include "c_message_field.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
using internal::WireFormat;
// ===================================================================
namespace protobuf_c {
MessageFieldGenerator::
MessageFieldGenerator(const FieldDescriptor* descriptor)
MessageFieldGenerator(const google::protobuf::FieldDescriptor* descriptor)
: FieldGenerator(descriptor) {
}
MessageFieldGenerator::~MessageFieldGenerator() {}
void MessageFieldGenerator::GenerateStructMembers(io::Printer* printer) const
void MessageFieldGenerator::GenerateStructMembers(google::protobuf::io::Printer* printer) const
{
std::map<std::string, std::string> vars;
vars["name"] = FieldName(descriptor_);
vars["type"] = FullNameToC(descriptor_->message_type()->full_name(), descriptor_->message_type()->file());
vars["deprecated"] = FieldDeprecated(descriptor_);
switch (descriptor_->label()) {
case FieldDescriptor::LABEL_REQUIRED:
case FieldDescriptor::LABEL_OPTIONAL:
case google::protobuf::FieldDescriptor::LABEL_REQUIRED:
case google::protobuf::FieldDescriptor::LABEL_OPTIONAL:
printer->Print(vars, "$type$ *$name$$deprecated$;\n");
break;
case FieldDescriptor::LABEL_REPEATED:
case google::protobuf::FieldDescriptor::LABEL_REPEATED:
printer->Print(vars, "size_t n_$name$$deprecated$;\n");
printer->Print(vars, "$type$ **$name$$deprecated$;\n");
break;
......@@ -107,25 +101,22 @@ std::string MessageFieldGenerator::GetDefaultValue(void) const
*/
return "NULL";
}
void MessageFieldGenerator::GenerateStaticInit(io::Printer* printer) const
void MessageFieldGenerator::GenerateStaticInit(google::protobuf::io::Printer* printer) const
{
switch (descriptor_->label()) {
case FieldDescriptor::LABEL_REQUIRED:
case FieldDescriptor::LABEL_OPTIONAL:
case google::protobuf::FieldDescriptor::LABEL_REQUIRED:
case google::protobuf::FieldDescriptor::LABEL_OPTIONAL:
printer->Print("NULL");
break;
case FieldDescriptor::LABEL_REPEATED:
case google::protobuf::FieldDescriptor::LABEL_REPEATED:
printer->Print("0,NULL");
break;
}
}
void MessageFieldGenerator::GenerateDescriptorInitializer(io::Printer* printer) const
void MessageFieldGenerator::GenerateDescriptorInitializer(google::protobuf::io::Printer* printer) const
{
std::string addr = "&" + FullNameToLower(descriptor_->message_type()->full_name(), descriptor_->message_type()->file()) + "__descriptor";
GenerateDescriptorInitializerGeneric(printer, false, "MESSAGE", addr);
}
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace google
} // namespace protobuf_c
......@@ -61,35 +61,28 @@
// Modified to implement C code by Dave Benson.
#ifndef GOOGLE_PROTOBUF_COMPILER_C_MESSAGE_FIELD_H__
#define GOOGLE_PROTOBUF_COMPILER_C_MESSAGE_FIELD_H__
#ifndef PROTOBUF_C_PROTOC_GEN_C_C_MESSAGE_FIELD_H__
#define PROTOBUF_C_PROTOC_GEN_C_C_MESSAGE_FIELD_H__
#include <map>
#include <string>
#include "c_field.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
class MessageFieldGenerator : public FieldGenerator {
public:
explicit MessageFieldGenerator(const FieldDescriptor* descriptor);
explicit MessageFieldGenerator(const google::protobuf::FieldDescriptor* descriptor);
~MessageFieldGenerator();
// implements FieldGenerator ---------------------------------------
void GenerateStructMembers(io::Printer* printer) const;
void GenerateDescriptorInitializer(io::Printer* printer) const;
void GenerateStructMembers(google::protobuf::io::Printer* printer) const;
void GenerateDescriptorInitializer(google::protobuf::io::Printer* printer) const;
std::string GetDefaultValue(void) const;
void GenerateStaticInit(io::Printer* printer) const;
void GenerateStaticInit(google::protobuf::io::Printer* printer) const;
};
} // namespace protobuf_c
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_C_MESSAGE_FIELD_H__
#endif // PROTOBUF_C_PROTOC_GEN_C_C_MESSAGE_FIELD_H__
......@@ -61,47 +61,45 @@
// Modified to implement C code by Dave Benson.
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/wire_format.h>
#include "c_helpers.h"
#include "c_primitive_field.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
PrimitiveFieldGenerator::
PrimitiveFieldGenerator(const FieldDescriptor* descriptor)
PrimitiveFieldGenerator(const google::protobuf::FieldDescriptor* descriptor)
: FieldGenerator(descriptor) {
}
PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {}
void PrimitiveFieldGenerator::GenerateStructMembers(io::Printer* printer) const
void PrimitiveFieldGenerator::GenerateStructMembers(google::protobuf::io::Printer* printer) const
{
std::string c_type;
std::map<std::string, std::string> vars;
switch (descriptor_->type()) {
case FieldDescriptor::TYPE_SINT32 :
case FieldDescriptor::TYPE_SFIXED32:
case FieldDescriptor::TYPE_INT32 : c_type = "int32_t"; break;
case FieldDescriptor::TYPE_SINT64 :
case FieldDescriptor::TYPE_SFIXED64:
case FieldDescriptor::TYPE_INT64 : c_type = "int64_t"; break;
case FieldDescriptor::TYPE_UINT32 :
case FieldDescriptor::TYPE_FIXED32 : c_type = "uint32_t"; break;
case FieldDescriptor::TYPE_UINT64 :
case FieldDescriptor::TYPE_FIXED64 : c_type = "uint64_t"; break;
case FieldDescriptor::TYPE_FLOAT : c_type = "float"; break;
case FieldDescriptor::TYPE_DOUBLE : c_type = "double"; break;
case FieldDescriptor::TYPE_BOOL : c_type = "protobuf_c_boolean"; break;
case FieldDescriptor::TYPE_ENUM :
case FieldDescriptor::TYPE_STRING :
case FieldDescriptor::TYPE_BYTES :
case FieldDescriptor::TYPE_GROUP :
case FieldDescriptor::TYPE_MESSAGE : GOOGLE_LOG(FATAL) << "not a primitive type"; break;
case google::protobuf::FieldDescriptor::TYPE_SINT32 :
case google::protobuf::FieldDescriptor::TYPE_SFIXED32:
case google::protobuf::FieldDescriptor::TYPE_INT32 : c_type = "int32_t"; break;
case google::protobuf::FieldDescriptor::TYPE_SINT64 :
case google::protobuf::FieldDescriptor::TYPE_SFIXED64:
case google::protobuf::FieldDescriptor::TYPE_INT64 : c_type = "int64_t"; break;
case google::protobuf::FieldDescriptor::TYPE_UINT32 :
case google::protobuf::FieldDescriptor::TYPE_FIXED32 : c_type = "uint32_t"; break;
case google::protobuf::FieldDescriptor::TYPE_UINT64 :
case google::protobuf::FieldDescriptor::TYPE_FIXED64 : c_type = "uint64_t"; break;
case google::protobuf::FieldDescriptor::TYPE_FLOAT : c_type = "float"; break;
case google::protobuf::FieldDescriptor::TYPE_DOUBLE : c_type = "double"; break;
case google::protobuf::FieldDescriptor::TYPE_BOOL : c_type = "protobuf_c_boolean"; break;
case google::protobuf::FieldDescriptor::TYPE_ENUM :
case google::protobuf::FieldDescriptor::TYPE_STRING :
case google::protobuf::FieldDescriptor::TYPE_BYTES :
case google::protobuf::FieldDescriptor::TYPE_GROUP :
case google::protobuf::FieldDescriptor::TYPE_MESSAGE : GOOGLE_LOG(FATAL) << "not a primitive type"; break;
// No default because we want the compiler to complain if any new
// types are added.
......@@ -111,15 +109,15 @@ void PrimitiveFieldGenerator::GenerateStructMembers(io::Printer* printer) const
vars["deprecated"] = FieldDeprecated(descriptor_);
switch (descriptor_->label()) {
case FieldDescriptor::LABEL_REQUIRED:
case google::protobuf::FieldDescriptor::LABEL_REQUIRED:
printer->Print(vars, "$c_type$ $name$$deprecated$;\n");
break;
case FieldDescriptor::LABEL_OPTIONAL:
case google::protobuf::FieldDescriptor::LABEL_OPTIONAL:
if (descriptor_->containing_oneof() == NULL && FieldSyntax(descriptor_) == 2)
printer->Print(vars, "protobuf_c_boolean has_$name$$deprecated$;\n");
printer->Print(vars, "$c_type$ $name$$deprecated$;\n");
break;
case FieldDescriptor::LABEL_REPEATED:
case google::protobuf::FieldDescriptor::LABEL_REPEATED:
printer->Print(vars, "size_t n_$name$$deprecated$;\n");
printer->Print(vars, "$c_type$ *$name$$deprecated$;\n");
break;
......@@ -130,26 +128,26 @@ std::string PrimitiveFieldGenerator::GetDefaultValue() const
/* XXX: SimpleItoa seems woefully inadequate for anything but int32,
* but that's what protobuf uses. */
switch (descriptor_->cpp_type()) {
case FieldDescriptor::CPPTYPE_INT32:
case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
return SimpleItoa(descriptor_->default_value_int32());
case FieldDescriptor::CPPTYPE_INT64:
case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
return SimpleItoa(descriptor_->default_value_int64()) + "ll";
case FieldDescriptor::CPPTYPE_UINT32:
case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
return SimpleItoa(descriptor_->default_value_uint32()) + "u";
case FieldDescriptor::CPPTYPE_UINT64:
case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
return SimpleItoa(descriptor_->default_value_uint64()) + "ull";
case FieldDescriptor::CPPTYPE_FLOAT:
case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
return SimpleFtoa(descriptor_->default_value_float());
case FieldDescriptor::CPPTYPE_DOUBLE:
case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
return SimpleDtoa(descriptor_->default_value_double());
case FieldDescriptor::CPPTYPE_BOOL:
case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
return descriptor_->default_value_bool() ? "1" : "0";
default:
GOOGLE_LOG(FATAL) << "unexpected CPPTYPE in c_primitive_field";
return "UNEXPECTED_CPPTYPE";
}
}
void PrimitiveFieldGenerator::GenerateStaticInit(io::Printer* printer) const
void PrimitiveFieldGenerator::GenerateStaticInit(google::protobuf::io::Printer* printer) const
{
std::map<std::string, std::string> vars;
if (descriptor_->has_default_value()) {
......@@ -158,25 +156,25 @@ void PrimitiveFieldGenerator::GenerateStaticInit(io::Printer* printer) const
vars["default_value"] = "0";
}
switch (descriptor_->label()) {
case FieldDescriptor::LABEL_REQUIRED:
case google::protobuf::FieldDescriptor::LABEL_REQUIRED:
printer->Print(vars, "$default_value$");
break;
case FieldDescriptor::LABEL_OPTIONAL:
case google::protobuf::FieldDescriptor::LABEL_OPTIONAL:
if (FieldSyntax(descriptor_) == 2)
printer->Print(vars, "0, ");
printer->Print(vars, "$default_value$");
break;
case FieldDescriptor::LABEL_REPEATED:
case google::protobuf::FieldDescriptor::LABEL_REPEATED:
printer->Print("0,NULL");
break;
}
}
void PrimitiveFieldGenerator::GenerateDescriptorInitializer(io::Printer* printer) const
void PrimitiveFieldGenerator::GenerateDescriptorInitializer(google::protobuf::io::Printer* printer) const
{
std::string c_type_macro;
switch (descriptor_->type()) {
#define WRITE_CASE(shortname) case FieldDescriptor::TYPE_##shortname: c_type_macro = #shortname; break;
#define WRITE_CASE(shortname) case google::protobuf::FieldDescriptor::TYPE_##shortname: c_type_macro = #shortname; break;
WRITE_CASE(INT32)
WRITE_CASE(SINT32)
WRITE_CASE(UINT32)
......@@ -195,11 +193,11 @@ void PrimitiveFieldGenerator::GenerateDescriptorInitializer(io::Printer* printer
WRITE_CASE(BOOL)
#undef WRITE_CASE
case FieldDescriptor::TYPE_ENUM :
case FieldDescriptor::TYPE_STRING :
case FieldDescriptor::TYPE_BYTES :
case FieldDescriptor::TYPE_GROUP :
case FieldDescriptor::TYPE_MESSAGE : GOOGLE_LOG(FATAL) << "not a primitive type"; break;
case google::protobuf::FieldDescriptor::TYPE_ENUM :
case google::protobuf::FieldDescriptor::TYPE_STRING :
case google::protobuf::FieldDescriptor::TYPE_BYTES :
case google::protobuf::FieldDescriptor::TYPE_GROUP :
case google::protobuf::FieldDescriptor::TYPE_MESSAGE : GOOGLE_LOG(FATAL) << "not a primitive type"; break;
// No default because we want the compiler to complain if any new
// types are added.
......@@ -207,7 +205,4 @@ void PrimitiveFieldGenerator::GenerateDescriptorInitializer(io::Printer* printer
GenerateDescriptorInitializerGeneric(printer, true, c_type_macro, "NULL");
}
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace google
} // namespace protobuf_c
......@@ -61,34 +61,28 @@
// Modified to implement C code by Dave Benson.
#ifndef GOOGLE_PROTOBUF_COMPILER_C_PRIMITIVE_FIELD_H__
#define GOOGLE_PROTOBUF_COMPILER_C_PRIMITIVE_FIELD_H__
#ifndef PROTOBUF_C_PROTOC_GEN_C_C_PRIMITIVE_FIELD_H__
#define PROTOBUF_C_PROTOC_GEN_C_C_PRIMITIVE_FIELD_H__
#include <map>
#include <string>
#include "c_field.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
class PrimitiveFieldGenerator : public FieldGenerator {
public:
explicit PrimitiveFieldGenerator(const FieldDescriptor* descriptor);
explicit PrimitiveFieldGenerator(const google::protobuf::FieldDescriptor* descriptor);
~PrimitiveFieldGenerator();
// implements FieldGenerator ---------------------------------------
void GenerateStructMembers(io::Printer* printer) const;
void GenerateDescriptorInitializer(io::Printer* printer) const;
void GenerateStructMembers(google::protobuf::io::Printer* printer) const;
void GenerateDescriptorInitializer(google::protobuf::io::Printer* printer) const;
std::string GetDefaultValue(void) const;
void GenerateStaticInit(io::Printer* printer) const;
void GenerateStaticInit(google::protobuf::io::Printer* printer) const;
};
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace protobuf_c
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_C_PRIMITIVE_FIELD_H__
#endif // PROTOBUF_C_PROTOC_GEN_C_C_PRIMITIVE_FIELD_H__
......@@ -61,17 +61,15 @@
// Modified to implement C code by Dave Benson.
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/printer.h>
#include "c_helpers.h"
#include "c_service.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
ServiceGenerator::ServiceGenerator(const ServiceDescriptor* descriptor,
ServiceGenerator::ServiceGenerator(const google::protobuf::ServiceDescriptor* descriptor,
const std::string& dllexport_decl)
: descriptor_(descriptor) {
vars_["name"] = descriptor_->name();
......@@ -91,13 +89,13 @@ ServiceGenerator::ServiceGenerator(const ServiceDescriptor* descriptor,
ServiceGenerator::~ServiceGenerator() {}
// Header stuff.
void ServiceGenerator::GenerateMainHFile(io::Printer* printer)
void ServiceGenerator::GenerateMainHFile(google::protobuf::io::Printer* printer)
{
GenerateVfuncs(printer);
GenerateInitMacros(printer);
GenerateCallersDeclarations(printer);
}
void ServiceGenerator::GenerateVfuncs(io::Printer* printer)
void ServiceGenerator::GenerateVfuncs(google::protobuf::io::Printer* printer)
{
printer->Print(vars_,
"typedef struct $cname$_Service $cname$_Service;\n"
......@@ -105,7 +103,7 @@ void ServiceGenerator::GenerateVfuncs(io::Printer* printer)
"{\n"
" ProtobufCService base;\n");
for (int i = 0; i < descriptor_->method_count(); i++) {
const MethodDescriptor *method = descriptor_->method(i);
const google::protobuf::MethodDescriptor* method = descriptor_->method(i);
std::string lcname = CamelToLower(method->name());
vars_["method"] = lcname;
vars_["metpad"] = ConvertToSpaces(lcname);
......@@ -124,7 +122,7 @@ void ServiceGenerator::GenerateVfuncs(io::Printer* printer)
"void $lcfullname$__init ($cname$_Service *service,\n"
" $lcfullpadd$ $cname$_ServiceDestroy destroy);\n");
}
void ServiceGenerator::GenerateInitMacros(io::Printer* printer)
void ServiceGenerator::GenerateInitMacros(google::protobuf::io::Printer* printer)
{
printer->Print(vars_,
"#define $ucfullname$__BASE_INIT \\\n"
......@@ -132,7 +130,7 @@ void ServiceGenerator::GenerateInitMacros(io::Printer* printer)
"#define $ucfullname$__INIT(function_prefix__) \\\n"
" { $ucfullname$__BASE_INIT");
for (int i = 0; i < descriptor_->method_count(); i++) {
const MethodDescriptor *method = descriptor_->method(i);
const google::protobuf::MethodDescriptor* method = descriptor_->method(i);
std::string lcname = CamelToLower(method->name());
vars_["method"] = lcname;
vars_["metpad"] = ConvertToSpaces(lcname);
......@@ -142,10 +140,10 @@ void ServiceGenerator::GenerateInitMacros(io::Printer* printer)
printer->Print(vars_,
" }\n");
}
void ServiceGenerator::GenerateCallersDeclarations(io::Printer* printer)
void ServiceGenerator::GenerateCallersDeclarations(google::protobuf::io::Printer* printer)
{
for (int i = 0; i < descriptor_->method_count(); i++) {
const MethodDescriptor *method = descriptor_->method(i);
const google::protobuf::MethodDescriptor* method = descriptor_->method(i);
std::string lcname = CamelToLower(method->name());
std::string lcfullname = FullNameToLower(descriptor_->full_name(), descriptor_->file());
vars_["method"] = lcname;
......@@ -161,20 +159,20 @@ void ServiceGenerator::GenerateCallersDeclarations(io::Printer* printer)
}
}
void ServiceGenerator::GenerateDescriptorDeclarations(io::Printer* printer)
void ServiceGenerator::GenerateDescriptorDeclarations(google::protobuf::io::Printer* printer)
{
printer->Print(vars_, "extern const ProtobufCServiceDescriptor $lcfullname$__descriptor;\n");
}
// Source file stuff.
void ServiceGenerator::GenerateCFile(io::Printer* printer)
void ServiceGenerator::GenerateCFile(google::protobuf::io::Printer* printer)
{
GenerateServiceDescriptor(printer);
GenerateCallersImplementations(printer);
GenerateInit(printer);
}
void ServiceGenerator::GenerateInit(io::Printer* printer)
void ServiceGenerator::GenerateInit(google::protobuf::io::Printer* printer)
{
printer->Print(vars_,
"void $lcfullname$__init ($cname$_Service *service,\n"
......@@ -195,20 +193,20 @@ compare_method_index_and_name_by_name (const void *a, const void *b)
return strcmp (ma->name, mb->name);
}
void ServiceGenerator::GenerateServiceDescriptor(io::Printer* printer)
void ServiceGenerator::GenerateServiceDescriptor(google::protobuf::io::Printer* printer)
{
int n_methods = descriptor_->method_count();
MethodIndexAndName *mi_array = new MethodIndexAndName[n_methods];
bool optimize_code_size = descriptor_->file()->options().has_optimize_for() &&
descriptor_->file()->options().optimize_for() ==
FileOptions_OptimizeMode_CODE_SIZE;
google::protobuf::FileOptions_OptimizeMode_CODE_SIZE;
vars_["n_methods"] = SimpleItoa(n_methods);
printer->Print(vars_, "static const ProtobufCMethodDescriptor $lcfullname$__method_descriptors[$n_methods$] =\n"
"{\n");
for (int i = 0; i < n_methods; i++) {
const MethodDescriptor *method = descriptor_->method(i);
const google::protobuf::MethodDescriptor* method = descriptor_->method(i);
vars_["method"] = method->name();
vars_["input_descriptor"] = "&" + FullNameToLower(method->input_type()->full_name(), method->input_type()->file()) + "__descriptor";
vars_["output_descriptor"] = "&" + FullNameToLower(method->output_type()->full_name(), method->output_type()->file()) + "__descriptor";
......@@ -264,10 +262,10 @@ void ServiceGenerator::GenerateServiceDescriptor(io::Printer* printer)
delete[] mi_array;
}
void ServiceGenerator::GenerateCallersImplementations(io::Printer* printer)
void ServiceGenerator::GenerateCallersImplementations(google::protobuf::io::Printer* printer)
{
for (int i = 0; i < descriptor_->method_count(); i++) {
const MethodDescriptor *method = descriptor_->method(i);
const google::protobuf::MethodDescriptor* method = descriptor_->method(i);
std::string lcname = CamelToLower(method->name());
std::string lcfullname = FullNameToLower(descriptor_->full_name(), descriptor_->file());
vars_["method"] = lcname;
......@@ -289,8 +287,4 @@ void ServiceGenerator::GenerateCallersImplementations(io::Printer* printer)
}
}
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace google
} // namespace protobuf_c
......@@ -61,8 +61,8 @@
// Modified to implement C code by Dave Benson.
#ifndef GOOGLE_PROTOBUF_COMPILER_C_SERVICE_H__
#define GOOGLE_PROTOBUF_COMPILER_C_SERVICE_H__
#ifndef PROTOBUF_C_PROTOC_GEN_C_C_SERVICE_H__
#define PROTOBUF_C_PROTOC_GEN_C_C_SERVICE_H__
#include <map>
#include <string>
......@@ -70,38 +70,32 @@
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/printer.h>
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
class ServiceGenerator {
public:
// See generator.cc for the meaning of dllexport_decl.
explicit ServiceGenerator(const ServiceDescriptor* descriptor,
explicit ServiceGenerator(const google::protobuf::ServiceDescriptor* descriptor,
const std::string& dllexport_decl);
~ServiceGenerator();
// Header stuff.
void GenerateMainHFile(io::Printer* printer);
void GenerateVfuncs(io::Printer* printer);
void GenerateInitMacros(io::Printer* printer);
void GenerateDescriptorDeclarations(io::Printer* printer);
void GenerateCallersDeclarations(io::Printer* printer);
void GenerateMainHFile(google::protobuf::io::Printer* printer);
void GenerateVfuncs(google::protobuf::io::Printer* printer);
void GenerateInitMacros(google::protobuf::io::Printer* printer);
void GenerateDescriptorDeclarations(google::protobuf::io::Printer* printer);
void GenerateCallersDeclarations(google::protobuf::io::Printer* printer);
// Source file stuff.
void GenerateCFile(io::Printer* printer);
void GenerateServiceDescriptor(io::Printer* printer);
void GenerateInit(io::Printer* printer);
void GenerateCallersImplementations(io::Printer* printer);
void GenerateCFile(google::protobuf::io::Printer* printer);
void GenerateServiceDescriptor(google::protobuf::io::Printer* printer);
void GenerateInit(google::protobuf::io::Printer* printer);
void GenerateCallersImplementations(google::protobuf::io::Printer* printer);
const ServiceDescriptor* descriptor_;
const google::protobuf::ServiceDescriptor* descriptor_;
std::map<std::string, std::string> vars_;
};
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace protobuf_c
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_C_SERVICE_H__
#endif // PROTOBUF_C_PROTOC_GEN_C_C_SERVICE_H__
......@@ -61,6 +61,7 @@
// Modified to implement C code by Dave Benson.
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/wire_format.h>
......@@ -69,14 +70,9 @@
#include "c_helpers.h"
#include "c_string_field.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
using internal::WireFormat;
void SetStringVariables(const FieldDescriptor* descriptor,
void SetStringVariables(const google::protobuf::FieldDescriptor* descriptor,
std::map<std::string, std::string>* variables) {
(*variables)["name"] = FieldName(descriptor);
(*variables)["default"] = FullNameToLower(descriptor->full_name(), descriptor->file())
......@@ -87,25 +83,25 @@ void SetStringVariables(const FieldDescriptor* descriptor,
// ===================================================================
StringFieldGenerator::
StringFieldGenerator(const FieldDescriptor* descriptor)
StringFieldGenerator(const google::protobuf::FieldDescriptor* descriptor)
: FieldGenerator(descriptor) {
SetStringVariables(descriptor, &variables_);
}
StringFieldGenerator::~StringFieldGenerator() {}
void StringFieldGenerator::GenerateStructMembers(io::Printer* printer) const
void StringFieldGenerator::GenerateStructMembers(google::protobuf::io::Printer* printer) const
{
const ProtobufCFileOptions opt = descriptor_->file()->options().GetExtension(pb_c_file);
switch (descriptor_->label()) {
case FieldDescriptor::LABEL_REQUIRED:
case FieldDescriptor::LABEL_OPTIONAL:
case google::protobuf::FieldDescriptor::LABEL_REQUIRED:
case google::protobuf::FieldDescriptor::LABEL_OPTIONAL:
if (opt.const_strings())
printer->Print(variables_, "const ");
printer->Print(variables_, "char *$name$$deprecated$;\n");
break;
case FieldDescriptor::LABEL_REPEATED:
case google::protobuf::FieldDescriptor::LABEL_REPEATED:
printer->Print(variables_, "size_t n_$name$$deprecated$;\n");
if (opt.const_strings())
printer->Print(variables_, "const ");
......@@ -113,11 +109,11 @@ void StringFieldGenerator::GenerateStructMembers(io::Printer* printer) const
break;
}
}
void StringFieldGenerator::GenerateDefaultValueDeclarations(io::Printer* printer) const
void StringFieldGenerator::GenerateDefaultValueDeclarations(google::protobuf::io::Printer* printer) const
{
printer->Print(variables_, "extern char $default$[];\n");
}
void StringFieldGenerator::GenerateDefaultValueImplementations(io::Printer* printer) const
void StringFieldGenerator::GenerateDefaultValueImplementations(google::protobuf::io::Printer* printer) const
{
std::map<std::string, std::string> vars;
vars["default"] = variables_.find("default")->second;
......@@ -129,7 +125,7 @@ std::string StringFieldGenerator::GetDefaultValue(void) const
{
return variables_.find("default")->second;
}
void StringFieldGenerator::GenerateStaticInit(io::Printer* printer) const
void StringFieldGenerator::GenerateStaticInit(google::protobuf::io::Printer* printer) const
{
std::map<std::string, std::string> vars;
const ProtobufCFileOptions opt = descriptor_->file()->options().GetExtension(pb_c_file);
......@@ -143,21 +139,18 @@ void StringFieldGenerator::GenerateStaticInit(io::Printer* printer) const
vars["default"] = "(char *)protobuf_c_empty_string";
}
switch (descriptor_->label()) {
case FieldDescriptor::LABEL_REQUIRED:
case FieldDescriptor::LABEL_OPTIONAL:
case google::protobuf::FieldDescriptor::LABEL_REQUIRED:
case google::protobuf::FieldDescriptor::LABEL_OPTIONAL:
printer->Print(vars, "$default$");
break;
case FieldDescriptor::LABEL_REPEATED:
case google::protobuf::FieldDescriptor::LABEL_REPEATED:
printer->Print(vars, "0,NULL");
break;
}
}
void StringFieldGenerator::GenerateDescriptorInitializer(io::Printer* printer) const
void StringFieldGenerator::GenerateDescriptorInitializer(google::protobuf::io::Printer* printer) const
{
GenerateDescriptorInitializerGeneric(printer, false, "STRING", "NULL");
}
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace google
} // namespace protobuf_c
......@@ -61,40 +61,33 @@
// Modified to implement C code by Dave Benson.
#ifndef GOOGLE_PROTOBUF_COMPILER_C_STRING_FIELD_H__
#define GOOGLE_PROTOBUF_COMPILER_C_STRING_FIELD_H__
#ifndef PROTOBUF_C_PROTOC_GEN_C_C_STRING_FIELD_H__
#define PROTOBUF_C_PROTOC_GEN_C_C_STRING_FIELD_H__
#include <map>
#include <string>
#include "c_field.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace c {
namespace protobuf_c {
class StringFieldGenerator : public FieldGenerator {
public:
explicit StringFieldGenerator(const FieldDescriptor* descriptor);
explicit StringFieldGenerator(const google::protobuf::FieldDescriptor* descriptor);
~StringFieldGenerator();
// implements FieldGenerator ---------------------------------------
void GenerateStructMembers(io::Printer* printer) const;
void GenerateDescriptorInitializer(io::Printer* printer) const;
void GenerateDefaultValueDeclarations(io::Printer* printer) const;
void GenerateDefaultValueImplementations(io::Printer* printer) const;
void GenerateStructMembers(google::protobuf::io::Printer* printer) const;
void GenerateDescriptorInitializer(google::protobuf::io::Printer* printer) const;
void GenerateDefaultValueDeclarations(google::protobuf::io::Printer* printer) const;
void GenerateDefaultValueImplementations(google::protobuf::io::Printer* printer) const;
std::string GetDefaultValue(void) const;
void GenerateStaticInit(io::Printer* printer) const;
void GenerateStaticInit(google::protobuf::io::Printer* printer) const;
private:
std::map<std::string, std::string> variables_;
};
} // namespace protobuf_c
} // namespace c
} // namespace compiler
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_C_STRING_FIELD_H__
#endif // PROTOBUF_C_PROTOC_GEN_C_C_STRING_FIELD_H__
......@@ -33,7 +33,7 @@
#include "c_generator.h"
int main(int argc, char* argv[]) {
google::protobuf::compiler::c::CGenerator c_generator;
protobuf_c::CGenerator c_generator;
std::string invocation_name = argv[0];
std::string invocation_basename = invocation_name.substr(invocation_name.find_last_of("/") + 1);
......
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