1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc.  All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9//     * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11//     * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15//     * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31#include <map>
32#include <string>
33
34#include <google/protobuf/compiler/objectivec/objectivec_enum_field.h>
35#include <google/protobuf/stubs/common.h>
36#include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
37#include <google/protobuf/io/printer.h>
38#include <google/protobuf/wire_format.h>
39#include <google/protobuf/stubs/strutil.h>
40
41namespace google {
42namespace protobuf {
43namespace compiler {
44namespace objectivec {
45
46namespace {
47
48void SetEnumVariables(const FieldDescriptor* descriptor,
49                      map<string, string>* variables) {
50  string type = EnumName(descriptor->enum_type());
51  (*variables)["storage_type"] = type;
52  // For non repeated fields, if it was defined in a different file, the
53  // property decls need to use "enum NAME" rather than just "NAME" to support
54  // the forward declaration of the enums.
55  if (!descriptor->is_repeated() &&
56      (descriptor->file() != descriptor->enum_type()->file())) {
57    (*variables)["property_type"] = "enum " + type;
58  }
59  (*variables)["enum_verifier"] = type + "_IsValidValue";
60  (*variables)["enum_desc_func"] = type + "_EnumDescriptor";
61
62  (*variables)["dataTypeSpecific_name"] = "enumDescFunc";
63  (*variables)["dataTypeSpecific_value"] = (*variables)["enum_desc_func"];
64
65  const Descriptor* msg_descriptor = descriptor->containing_type();
66  (*variables)["owning_message_class"] = ClassName(msg_descriptor);
67}
68}  // namespace
69
70EnumFieldGenerator::EnumFieldGenerator(const FieldDescriptor* descriptor,
71                                       const Options& options)
72    : SingleFieldGenerator(descriptor, options) {
73  SetEnumVariables(descriptor, &variables_);
74}
75
76EnumFieldGenerator::~EnumFieldGenerator() {}
77
78void EnumFieldGenerator::GenerateCFunctionDeclarations(
79    io::Printer* printer) const {
80  if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) {
81    return;
82  }
83
84  printer->Print(
85      variables_,
86      "/// Fetches the raw value of a @c $owning_message_class$'s @c $name$ property, even\n"
87      "/// if the value was not defined by the enum at the time the code was generated.\n"
88      "int32_t $owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message);\n"
89      "/// Sets the raw value of an @c $owning_message_class$'s @c $name$ property, allowing\n"
90      "/// it to be set to a value that was not defined by the enum at the time the code\n"
91      "/// was generated.\n"
92      "void Set$owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message, int32_t value);\n"
93      "\n");
94}
95
96void EnumFieldGenerator::GenerateCFunctionImplementations(
97    io::Printer* printer) const {
98  if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) return;
99
100  printer->Print(
101      variables_,
102      "int32_t $owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message) {\n"
103      "  GPBDescriptor *descriptor = [$owning_message_class$ descriptor];\n"
104      "  GPBFieldDescriptor *field = [descriptor fieldWithNumber:$field_number_name$];\n"
105      "  return GPBGetMessageInt32Field(message, field);\n"
106      "}\n"
107      "\n"
108      "void Set$owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message, int32_t value) {\n"
109      "  GPBDescriptor *descriptor = [$owning_message_class$ descriptor];\n"
110      "  GPBFieldDescriptor *field = [descriptor fieldWithNumber:$field_number_name$];\n"
111      "  GPBSetInt32IvarWithFieldInternal(message, field, value, descriptor.file.syntax);\n"
112      "}\n"
113      "\n");
114}
115
116void EnumFieldGenerator::DetermineForwardDeclarations(
117    set<string>* fwd_decls) const {
118  SingleFieldGenerator::DetermineForwardDeclarations(fwd_decls);
119  // If it is an enum defined in a different file, then we'll need a forward
120  // declaration for it.  When it is in our file, all the enums are output
121  // before the message, so it will be declared before it is needed.
122  if (descriptor_->file() != descriptor_->enum_type()->file()) {
123    // Enum name is already in "storage_type".
124    const string& name = variable("storage_type");
125    fwd_decls->insert("GPB_ENUM_FWD_DECLARE(" + name + ")");
126  }
127}
128
129RepeatedEnumFieldGenerator::RepeatedEnumFieldGenerator(
130    const FieldDescriptor* descriptor, const Options& options)
131    : RepeatedFieldGenerator(descriptor, options) {
132  SetEnumVariables(descriptor, &variables_);
133  variables_["array_storage_type"] = "GPBEnumArray";
134}
135
136RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {}
137
138void RepeatedEnumFieldGenerator::FinishInitialization(void) {
139  RepeatedFieldGenerator::FinishInitialization();
140  variables_["array_comment"] =
141      "// |" + variables_["name"] + "| contains |" + variables_["storage_type"] + "|\n";
142}
143
144}  // namespace objectivec
145}  // namespace compiler
146}  // namespace protobuf
147}  // namespace google
148