2008-08-22 20:38:05 +00:00
|
|
|
// Protocol Buffers - Google's data interchange format
|
2013-11-18 20:25:34 -05:00
|
|
|
// Copyright 2008 Google Inc. All rights reserved.
|
2008-08-22 20:38:05 +00:00
|
|
|
// http://code.google.com/p/protobuf/
|
|
|
|
//
|
2013-11-18 20:25:34 -05:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
2008-08-22 20:38:05 +00:00
|
|
|
//
|
2013-11-18 20:25:34 -05:00
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived from
|
|
|
|
// this software without specific prior written permission.
|
2008-08-22 20:38:05 +00:00
|
|
|
//
|
2013-11-18 20:25:34 -05:00
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
// Author: kenton@google.com (Kenton Varda)
|
|
|
|
// Based on original Protocol Buffers design by
|
|
|
|
// Sanjay Ghemawat, Jeff Dean, and others.
|
|
|
|
|
2014-04-03 18:19:00 -04:00
|
|
|
// Copyright (c) 2008-2014, Dave Benson and the protobuf-c authors.
|
|
|
|
// All rights reserved.
|
2013-11-18 20:25:34 -05:00
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2008-08-22 20:38:05 +00:00
|
|
|
// Modified to implement C code by Dave Benson.
|
|
|
|
|
2013-11-16 17:11:48 -05:00
|
|
|
#include <protoc-c/c_file.h>
|
|
|
|
#include <protoc-c/c_enum.h>
|
|
|
|
#include <protoc-c/c_service.h>
|
|
|
|
#include <protoc-c/c_extension.h>
|
|
|
|
#include <protoc-c/c_helpers.h>
|
|
|
|
#include <protoc-c/c_message.h>
|
2008-08-22 20:38:05 +00:00
|
|
|
#include <google/protobuf/io/printer.h>
|
2021-03-19 18:54:16 -07:00
|
|
|
#include <protobuf-c/protobuf-c.pb.h>
|
2008-08-22 20:38:05 +00:00
|
|
|
|
2014-04-03 18:19:00 -04:00
|
|
|
#include "protobuf-c.h"
|
|
|
|
|
2008-08-22 20:38:05 +00:00
|
|
|
namespace google {
|
|
|
|
namespace protobuf {
|
|
|
|
namespace compiler {
|
|
|
|
namespace c {
|
|
|
|
|
|
|
|
// ===================================================================
|
|
|
|
|
|
|
|
FileGenerator::FileGenerator(const FileDescriptor* file,
|
2020-10-16 16:35:43 -07:00
|
|
|
const std::string& dllexport_decl)
|
2008-08-22 20:38:05 +00:00
|
|
|
: file_(file),
|
|
|
|
message_generators_(
|
2018-08-13 21:26:00 -04:00
|
|
|
new std::unique_ptr<MessageGenerator>[file->message_type_count()]),
|
2008-08-22 20:38:05 +00:00
|
|
|
enum_generators_(
|
2018-08-13 21:26:00 -04:00
|
|
|
new std::unique_ptr<EnumGenerator>[file->enum_type_count()]),
|
2008-08-22 20:38:05 +00:00
|
|
|
service_generators_(
|
2018-08-13 21:26:00 -04:00
|
|
|
new std::unique_ptr<ServiceGenerator>[file->service_count()]),
|
2008-08-22 20:38:05 +00:00
|
|
|
extension_generators_(
|
2018-08-13 21:26:00 -04:00
|
|
|
new std::unique_ptr<ExtensionGenerator>[file->extension_count()]) {
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < file->message_type_count(); i++) {
|
|
|
|
message_generators_[i].reset(
|
|
|
|
new MessageGenerator(file->message_type(i), dllexport_decl));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < file->enum_type_count(); i++) {
|
|
|
|
enum_generators_[i].reset(
|
|
|
|
new EnumGenerator(file->enum_type(i), dllexport_decl));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < file->service_count(); i++) {
|
|
|
|
service_generators_[i].reset(
|
|
|
|
new ServiceGenerator(file->service(i), dllexport_decl));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < file->extension_count(); i++) {
|
|
|
|
extension_generators_[i].reset(
|
|
|
|
new ExtensionGenerator(file->extension(i), dllexport_decl));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FileGenerator::~FileGenerator() {}
|
|
|
|
|
|
|
|
void FileGenerator::GenerateHeader(io::Printer* printer) {
|
2020-10-16 16:35:43 -07:00
|
|
|
std::string filename_identifier = FilenameIdentifier(file_->name());
|
2008-08-22 20:38:05 +00:00
|
|
|
|
2017-07-29 20:16:22 -04:00
|
|
|
int min_header_version = 1000000;
|
2023-07-08 23:13:59 -04:00
|
|
|
#if GOOGLE_PROTOBUF_VERSION >= 4023000
|
2023-07-02 22:57:57 -04:00
|
|
|
if (FileDescriptorLegacy(file_).syntax() == FileDescriptorLegacy::SYNTAX_PROTO3) {
|
2023-07-08 23:13:59 -04:00
|
|
|
#else
|
2017-07-29 20:16:22 -04:00
|
|
|
if (file_->syntax() == FileDescriptor::SYNTAX_PROTO3) {
|
2023-07-02 22:57:57 -04:00
|
|
|
#endif
|
2017-07-29 20:16:22 -04:00
|
|
|
min_header_version = 1003000;
|
|
|
|
}
|
2014-06-03 13:54:50 -04:00
|
|
|
|
2008-08-22 20:38:05 +00:00
|
|
|
// Generate top of header.
|
|
|
|
printer->Print(
|
2008-08-28 06:08:40 +00:00
|
|
|
"/* Generated by the protocol buffer compiler. DO NOT EDIT! */\n"
|
2013-11-18 21:05:33 -08:00
|
|
|
"/* Generated from: $filename$ */\n"
|
2008-08-22 20:38:05 +00:00
|
|
|
"\n"
|
|
|
|
"#ifndef PROTOBUF_C_$filename_identifier$__INCLUDED\n"
|
|
|
|
"#define PROTOBUF_C_$filename_identifier$__INCLUDED\n"
|
|
|
|
"\n"
|
2013-11-16 17:12:01 -05:00
|
|
|
"#include <protobuf-c/protobuf-c.h>\n"
|
2008-08-24 18:37:43 +00:00
|
|
|
"\n"
|
2014-06-06 14:50:14 -04:00
|
|
|
"PROTOBUF_C__BEGIN_DECLS\n"
|
2008-08-22 20:38:05 +00:00
|
|
|
"\n",
|
2013-11-18 21:05:33 -08:00
|
|
|
"filename", file_->name(),
|
2008-08-22 20:38:05 +00:00
|
|
|
"filename_identifier", filename_identifier);
|
|
|
|
|
2014-04-03 18:19:00 -04:00
|
|
|
// Verify the protobuf-c library header version is compatible with the
|
|
|
|
// protoc-c version before going any further.
|
2008-08-22 20:38:05 +00:00
|
|
|
printer->Print(
|
2014-04-03 18:19:00 -04:00
|
|
|
"#if PROTOBUF_C_VERSION_NUMBER < $min_header_version$\n"
|
|
|
|
"# error This file was generated by a newer version of protoc-c which is "
|
|
|
|
"incompatible with your libprotobuf-c headers. Please update your headers.\n"
|
2014-06-03 13:54:50 -04:00
|
|
|
"#elif $protoc_version$ < PROTOBUF_C_MIN_COMPILER_VERSION\n"
|
2014-04-03 18:19:00 -04:00
|
|
|
"# error This file was generated by an older version of protoc-c which is "
|
|
|
|
"incompatible with your libprotobuf-c headers. Please regenerate this file "
|
|
|
|
"with a newer version of protoc-c.\n"
|
2008-08-22 20:38:05 +00:00
|
|
|
"#endif\n"
|
|
|
|
"\n",
|
2014-06-03 13:54:50 -04:00
|
|
|
"min_header_version", SimpleItoa(min_header_version),
|
2014-04-03 18:19:00 -04:00
|
|
|
"protoc_version", SimpleItoa(PROTOBUF_C_VERSION_NUMBER));
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < file_->dependency_count(); i++) {
|
2021-03-20 14:07:23 -07:00
|
|
|
const ProtobufCFileOptions opt =
|
|
|
|
file_->dependency(i)->options().GetExtension(pb_c_file);
|
|
|
|
if (!opt.no_generate()) {
|
|
|
|
printer->Print(
|
|
|
|
"#include \"$dependency$.pb-c.h\"\n",
|
|
|
|
"dependency", StripProto(file_->dependency(i)->name()));
|
|
|
|
}
|
2008-08-22 20:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printer->Print("\n");
|
|
|
|
|
|
|
|
// Generate forward declarations of classes.
|
|
|
|
for (int i = 0; i < file_->message_type_count(); i++) {
|
|
|
|
message_generators_[i]->GenerateStructTypedef(printer);
|
|
|
|
}
|
|
|
|
|
|
|
|
printer->Print("\n");
|
|
|
|
|
|
|
|
// Generate enum definitions.
|
|
|
|
printer->Print("\n/* --- enums --- */\n\n");
|
|
|
|
for (int i = 0; i < file_->message_type_count(); i++) {
|
|
|
|
message_generators_[i]->GenerateEnumDefinitions(printer);
|
|
|
|
}
|
2008-08-23 19:59:17 +00:00
|
|
|
for (int i = 0; i < file_->enum_type_count(); i++) {
|
|
|
|
enum_generators_[i]->GenerateDefinition(printer);
|
|
|
|
}
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
// Generate class definitions.
|
|
|
|
printer->Print("\n/* --- messages --- */\n\n");
|
|
|
|
for (int i = 0; i < file_->message_type_count(); i++) {
|
|
|
|
message_generators_[i]->GenerateStructDefinition(printer);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < file_->message_type_count(); i++) {
|
2021-03-20 16:57:34 -07:00
|
|
|
const ProtobufCFileOptions opt = file_->options().GetExtension(pb_c_file);
|
|
|
|
|
|
|
|
message_generators_[i]->GenerateHelperFunctionDeclarations(
|
|
|
|
printer,
|
|
|
|
opt.has_gen_pack_helpers(),
|
|
|
|
opt.gen_pack_helpers(),
|
|
|
|
opt.gen_init_helpers());
|
2008-08-22 20:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printer->Print("/* --- per-message closures --- */\n\n");
|
|
|
|
for (int i = 0; i < file_->message_type_count(); i++) {
|
|
|
|
message_generators_[i]->GenerateClosureTypedef(printer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate service definitions.
|
|
|
|
printer->Print("\n/* --- services --- */\n\n");
|
|
|
|
for (int i = 0; i < file_->service_count(); i++) {
|
|
|
|
service_generators_[i]->GenerateMainHFile(printer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Declare extension identifiers.
|
|
|
|
for (int i = 0; i < file_->extension_count(); i++) {
|
|
|
|
extension_generators_[i]->GenerateDeclaration(printer);
|
|
|
|
}
|
|
|
|
|
|
|
|
printer->Print("\n/* --- descriptors --- */\n\n");
|
|
|
|
for (int i = 0; i < file_->enum_type_count(); i++) {
|
2008-08-23 19:59:17 +00:00
|
|
|
enum_generators_[i]->GenerateDescriptorDeclarations(printer);
|
2008-08-22 20:38:05 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < file_->message_type_count(); i++) {
|
|
|
|
message_generators_[i]->GenerateDescriptorDeclarations(printer);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < file_->service_count(); i++) {
|
|
|
|
service_generators_[i]->GenerateDescriptorDeclarations(printer);
|
|
|
|
}
|
|
|
|
|
|
|
|
printer->Print(
|
2008-08-24 18:37:43 +00:00
|
|
|
"\n"
|
2014-06-06 14:50:14 -04:00
|
|
|
"PROTOBUF_C__END_DECLS\n"
|
2014-06-06 13:06:53 +01:00
|
|
|
"\n\n#endif /* PROTOBUF_C_$filename_identifier$__INCLUDED */\n",
|
2008-08-22 20:38:05 +00:00
|
|
|
"filename_identifier", filename_identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileGenerator::GenerateSource(io::Printer* printer) {
|
|
|
|
printer->Print(
|
2008-08-28 06:08:40 +00:00
|
|
|
"/* Generated by the protocol buffer compiler. DO NOT EDIT! */\n"
|
2013-11-18 21:05:33 -08:00
|
|
|
"/* Generated from: $filename$ */\n"
|
2008-08-22 20:38:05 +00:00
|
|
|
"\n"
|
2010-08-12 19:38:03 +00:00
|
|
|
"/* Do not generate deprecated warnings for self */\n"
|
2014-06-06 14:55:11 -04:00
|
|
|
"#ifndef PROTOBUF_C__NO_DEPRECATED\n"
|
|
|
|
"#define PROTOBUF_C__NO_DEPRECATED\n"
|
2010-08-12 19:38:03 +00:00
|
|
|
"#endif\n"
|
|
|
|
"\n"
|
2008-08-22 20:38:05 +00:00
|
|
|
"#include \"$basename$.pb-c.h\"\n",
|
2013-11-18 21:05:33 -08:00
|
|
|
"filename", file_->name(),
|
2008-08-22 20:38:05 +00:00
|
|
|
"basename", StripProto(file_->name()));
|
|
|
|
|
2021-03-20 16:57:34 -07:00
|
|
|
const ProtobufCFileOptions opt = file_->options().GetExtension(pb_c_file);
|
|
|
|
|
2008-08-22 20:38:05 +00:00
|
|
|
for (int i = 0; i < file_->message_type_count(); i++) {
|
2021-03-20 16:57:34 -07:00
|
|
|
message_generators_[i]->GenerateHelperFunctionDefinitions(
|
|
|
|
printer,
|
|
|
|
opt.has_gen_pack_helpers(),
|
|
|
|
opt.gen_pack_helpers(),
|
|
|
|
opt.gen_init_helpers());
|
2008-08-22 20:38:05 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < file_->message_type_count(); i++) {
|
2021-03-20 16:57:34 -07:00
|
|
|
message_generators_[i]->GenerateMessageDescriptor(printer,
|
|
|
|
opt.gen_init_helpers());
|
2008-08-22 20:38:05 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < file_->enum_type_count(); i++) {
|
|
|
|
enum_generators_[i]->GenerateEnumDescriptor(printer);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < file_->service_count(); i++) {
|
|
|
|
service_generators_[i]->GenerateCFile(printer);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace c
|
|
|
|
} // namespace compiler
|
|
|
|
} // namespace protobuf
|
|
|
|
} // namespace google
|