• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/compiler/objectivec/objectivec_helpers.h>
36 #include <google/protobuf/io/printer.h>
37 #include <google/protobuf/wire_format.h>
38 
39 namespace google {
40 namespace protobuf {
41 namespace compiler {
42 namespace objectivec {
43 
44 namespace {
45 
SetEnumVariables(const FieldDescriptor * descriptor,std::map<string,string> * variables)46 void SetEnumVariables(const FieldDescriptor* descriptor,
47                       std::map<string, string>* variables) {
48   string type = EnumName(descriptor->enum_type());
49   (*variables)["storage_type"] = type;
50   // For non repeated fields, if it was defined in a different file, the
51   // property decls need to use "enum NAME" rather than just "NAME" to support
52   // the forward declaration of the enums.
53   if (!descriptor->is_repeated() &&
54       (descriptor->file() != descriptor->enum_type()->file())) {
55     (*variables)["property_type"] = "enum " + type;
56   }
57   (*variables)["enum_verifier"] = type + "_IsValidValue";
58   (*variables)["enum_desc_func"] = type + "_EnumDescriptor";
59 
60   (*variables)["dataTypeSpecific_name"] = "enumDescFunc";
61   (*variables)["dataTypeSpecific_value"] = (*variables)["enum_desc_func"];
62 
63   const Descriptor* msg_descriptor = descriptor->containing_type();
64   (*variables)["owning_message_class"] = ClassName(msg_descriptor);
65 }
66 }  // namespace
67 
EnumFieldGenerator(const FieldDescriptor * descriptor,const Options & options)68 EnumFieldGenerator::EnumFieldGenerator(const FieldDescriptor* descriptor,
69                                        const Options& options)
70     : SingleFieldGenerator(descriptor, options) {
71   SetEnumVariables(descriptor, &variables_);
72 }
73 
~EnumFieldGenerator()74 EnumFieldGenerator::~EnumFieldGenerator() {}
75 
GenerateCFunctionDeclarations(io::Printer * printer) const76 void EnumFieldGenerator::GenerateCFunctionDeclarations(
77     io::Printer* printer) const {
78   if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) {
79     return;
80   }
81 
82   printer->Print(
83       variables_,
84       "/**\n"
85       " * Fetches the raw value of a @c $owning_message_class$'s @c $name$ property, even\n"
86       " * if the value was not defined by the enum at the time the code was generated.\n"
87       " **/\n"
88       "int32_t $owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message);\n"
89       "/**\n"
90       " * Sets the raw value of an @c $owning_message_class$'s @c $name$ property, allowing\n"
91       " * it to be set to a value that was not defined by the enum at the time the code\n"
92       " * was generated.\n"
93       " **/\n"
94       "void Set$owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message, int32_t value);\n"
95       "\n");
96 }
97 
GenerateCFunctionImplementations(io::Printer * printer) const98 void EnumFieldGenerator::GenerateCFunctionImplementations(
99     io::Printer* printer) const {
100   if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) return;
101 
102   printer->Print(
103       variables_,
104       "int32_t $owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message) {\n"
105       "  GPBDescriptor *descriptor = [$owning_message_class$ descriptor];\n"
106       "  GPBFieldDescriptor *field = [descriptor fieldWithNumber:$field_number_name$];\n"
107       "  return GPBGetMessageInt32Field(message, field);\n"
108       "}\n"
109       "\n"
110       "void Set$owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message, int32_t value) {\n"
111       "  GPBDescriptor *descriptor = [$owning_message_class$ descriptor];\n"
112       "  GPBFieldDescriptor *field = [descriptor fieldWithNumber:$field_number_name$];\n"
113       "  GPBSetInt32IvarWithFieldInternal(message, field, value, descriptor.file.syntax);\n"
114       "}\n"
115       "\n");
116 }
117 
DetermineForwardDeclarations(std::set<string> * fwd_decls) const118 void EnumFieldGenerator::DetermineForwardDeclarations(
119     std::set<string>* fwd_decls) const {
120   SingleFieldGenerator::DetermineForwardDeclarations(fwd_decls);
121   // If it is an enum defined in a different file, then we'll need a forward
122   // declaration for it.  When it is in our file, all the enums are output
123   // before the message, so it will be declared before it is needed.
124   if (descriptor_->file() != descriptor_->enum_type()->file()) {
125     // Enum name is already in "storage_type".
126     const string& name = variable("storage_type");
127     fwd_decls->insert("GPB_ENUM_FWD_DECLARE(" + name + ")");
128   }
129 }
130 
RepeatedEnumFieldGenerator(const FieldDescriptor * descriptor,const Options & options)131 RepeatedEnumFieldGenerator::RepeatedEnumFieldGenerator(
132     const FieldDescriptor* descriptor, const Options& options)
133     : RepeatedFieldGenerator(descriptor, options) {
134   SetEnumVariables(descriptor, &variables_);
135   variables_["array_storage_type"] = "GPBEnumArray";
136 }
137 
~RepeatedEnumFieldGenerator()138 RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {}
139 
FinishInitialization(void)140 void RepeatedEnumFieldGenerator::FinishInitialization(void) {
141   RepeatedFieldGenerator::FinishInitialization();
142   variables_["array_comment"] =
143       "// |" + variables_["name"] + "| contains |" + variables_["storage_type"] + "|\n";
144 }
145 
146 }  // namespace objectivec
147 }  // namespace compiler
148 }  // namespace protobuf
149 }  // namespace google
150