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.
|
|
|
|
|
2013-11-18 20:25:34 -05:00
|
|
|
// Copyright (c) 2008-2013, Dave Benson. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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_enum_field.h>
|
|
|
|
#include <protoc-c/c_helpers.h>
|
2008-08-22 20:38:05 +00:00
|
|
|
#include <google/protobuf/io/printer.h>
|
2010-01-24 16:42:14 +00:00
|
|
|
#include <google/protobuf/wire_format.h>
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
namespace google {
|
|
|
|
namespace protobuf {
|
|
|
|
namespace compiler {
|
|
|
|
namespace c {
|
|
|
|
|
|
|
|
using internal::WireFormat;
|
|
|
|
|
|
|
|
// TODO(kenton): Factor out a "SetCommonFieldVariables()" to get rid of
|
|
|
|
// repeat code between this and the other field types.
|
|
|
|
void SetEnumVariables(const FieldDescriptor* descriptor,
|
|
|
|
map<string, string>* variables) {
|
|
|
|
|
|
|
|
(*variables)["name"] = FieldName(descriptor);
|
|
|
|
(*variables)["type"] = FullNameToC(descriptor->enum_type()->full_name());
|
2008-11-30 06:10:28 +00:00
|
|
|
if (descriptor->has_default_value()) {
|
|
|
|
const EnumValueDescriptor* default_value = descriptor->default_value_enum();
|
|
|
|
(*variables)["default"] = FullNameToUpper(default_value->type()->full_name())
|
protoc-c: preserve case in C enum value names (Issue #129)
there is some confusion with regard to the use of lower case letters in
enum values. take the following message definition:
message LowerCase {
enum CaseEnum {
UPPER = 1;
lower = 2;
}
optional CaseEnum value = 1 [default = lower];
}
this generates the following C enum:
typedef enum _LowerCase__CaseEnum {
LOWER_CASE__CASE_ENUM__UPPER = 1,
LOWER_CASE__CASE_ENUM__lower = 2
_PROTOBUF_C_FORCE_ENUM_TO_BE_INT_SIZE(LOWER_CASE__CASE_ENUM)
} LowerCase__CaseEnum;
note that the case of the enum value 'lower' was preserved in the C
symbol name as 'LOWER_CASE__CASE_ENUM__lower', but that the _INIT macro
references the same enum value with the (non-existent) C symbol name
'LOWER_CASE__CASE_ENUM__LOWER':
#define LOWER_CASE__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&lower_case__descriptor) \
, 0,LOWER_CASE__CASE_ENUM__LOWER }
additionally, the ProtobufCEnumValue array generated also refers to the
same enum value with the (non-existent) upper cased version:
const ProtobufCEnumValue lower_case__case_enum__enum_values_by_number[2] =
{
{ "UPPER", "LOWER_CASE__CASE_ENUM__UPPER", 1 },
{ "lower", "LOWER_CASE__CASE_ENUM__LOWER", 2 },
};
we should preserve the existing behavior of copying the case from the
enum values in the message definition and fix up the places where the
(non-existent) upper case version is used, rather than changing the enum
definition itself to match the case used in the _INIT macro and
enum_values_by_number array, because it's possible that there might be
existing working code that uses enum values with lower case letters that
would be affected by such a change.
incidentally, google's C++ protobuf implementation preserves case in
enum values. protoc --cpp_out generates the following enum declaration
for the message descriptor above:
enum LowerCase_CaseEnum {
LowerCase_CaseEnum_UPPER = 1,
LowerCase_CaseEnum_lower = 2
};
2014-03-24 15:43:53 -04:00
|
|
|
+ "__" + default_value->name();
|
2008-11-30 06:10:28 +00:00
|
|
|
} else
|
|
|
|
(*variables)["default"] = "0";
|
2010-08-12 19:38:03 +00:00
|
|
|
(*variables)["deprecated"] = FieldDeprecated(descriptor);
|
2008-08-22 20:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ===================================================================
|
|
|
|
|
|
|
|
EnumFieldGenerator::
|
|
|
|
EnumFieldGenerator(const FieldDescriptor* descriptor)
|
|
|
|
: FieldGenerator(descriptor)
|
|
|
|
{
|
|
|
|
SetEnumVariables(descriptor, &variables_);
|
|
|
|
}
|
|
|
|
|
|
|
|
EnumFieldGenerator::~EnumFieldGenerator() {}
|
|
|
|
|
|
|
|
void EnumFieldGenerator::GenerateStructMembers(io::Printer* printer) const
|
|
|
|
{
|
|
|
|
switch (descriptor_->label()) {
|
|
|
|
case FieldDescriptor::LABEL_REQUIRED:
|
2010-08-12 19:38:03 +00:00
|
|
|
printer->Print(variables_, "$type$ $name$$deprecated$;\n");
|
2008-08-22 20:38:05 +00:00
|
|
|
break;
|
|
|
|
case FieldDescriptor::LABEL_OPTIONAL:
|
2014-11-04 00:37:45 -08:00
|
|
|
if (descriptor_->containing_oneof() == NULL)
|
|
|
|
printer->Print(variables_, "protobuf_c_boolean has_$name$$deprecated$;\n");
|
2010-08-12 19:38:03 +00:00
|
|
|
printer->Print(variables_, "$type$ $name$$deprecated$;\n");
|
2008-08-22 20:38:05 +00:00
|
|
|
break;
|
|
|
|
case FieldDescriptor::LABEL_REPEATED:
|
2010-08-12 19:38:03 +00:00
|
|
|
printer->Print(variables_, "size_t n_$name$$deprecated$;\n");
|
|
|
|
printer->Print(variables_, "$type$ *$name$$deprecated$;\n");
|
2008-08-22 20:38:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-11-30 06:10:28 +00:00
|
|
|
|
|
|
|
string EnumFieldGenerator::GetDefaultValue(void) const
|
|
|
|
{
|
|
|
|
return variables_.find("default")->second;
|
|
|
|
}
|
2008-08-23 19:59:17 +00:00
|
|
|
void EnumFieldGenerator::GenerateStaticInit(io::Printer* printer) const
|
|
|
|
{
|
|
|
|
switch (descriptor_->label()) {
|
|
|
|
case FieldDescriptor::LABEL_REQUIRED:
|
|
|
|
printer->Print(variables_, "$default$");
|
|
|
|
break;
|
2008-08-24 12:38:58 +00:00
|
|
|
case FieldDescriptor::LABEL_OPTIONAL:
|
|
|
|
printer->Print(variables_, "0,$default$");
|
|
|
|
break;
|
2008-08-23 19:59:17 +00:00
|
|
|
case FieldDescriptor::LABEL_REPEATED:
|
|
|
|
// no support for default?
|
|
|
|
printer->Print("0,NULL");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
void EnumFieldGenerator::GenerateDescriptorInitializer(io::Printer* printer) const
|
|
|
|
{
|
|
|
|
string addr = "&" + FullNameToLower(descriptor_->enum_type()->full_name()) + "__descriptor";
|
|
|
|
GenerateDescriptorInitializerGeneric(printer, true, "ENUM", addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace c
|
|
|
|
} // namespace compiler
|
|
|
|
} // namespace protobuf
|
|
|
|
} // namespace google
|