• 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<std::string,std::string> * variables)46 void SetEnumVariables(const FieldDescriptor* descriptor,
47                       std::map<std::string, std::string>* variables) {
48   std::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)68 EnumFieldGenerator::EnumFieldGenerator(const FieldDescriptor* descriptor)
69     : SingleFieldGenerator(descriptor) {
70   SetEnumVariables(descriptor, &variables_);
71 }
72 
~EnumFieldGenerator()73 EnumFieldGenerator::~EnumFieldGenerator() {}
74 
GenerateCFunctionDeclarations(io::Printer * printer) const75 void EnumFieldGenerator::GenerateCFunctionDeclarations(
76     io::Printer* printer) const {
77   if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) {
78     return;
79   }
80 
81   printer->Print(
82       variables_,
83       "/**\n"
84       " * Fetches the raw value of a @c $owning_message_class$'s @c $name$ property, even\n"
85       " * if the value was not defined by the enum at the time the code was generated.\n"
86       " **/\n"
87       "int32_t $owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message);\n"
88       "/**\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       " **/\n"
93       "void Set$owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message, int32_t value);\n"
94       "\n");
95 }
96 
GenerateCFunctionImplementations(io::Printer * printer) const97 void EnumFieldGenerator::GenerateCFunctionImplementations(
98     io::Printer* printer) const {
99   if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) return;
100 
101   printer->Print(
102       variables_,
103       "int32_t $owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message) {\n"
104       "  GPBDescriptor *descriptor = [$owning_message_class$ descriptor];\n"
105       "  GPBFieldDescriptor *field = [descriptor fieldWithNumber:$field_number_name$];\n"
106       "  return GPBGetMessageRawEnumField(message, field);\n"
107       "}\n"
108       "\n"
109       "void Set$owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message, int32_t value) {\n"
110       "  GPBDescriptor *descriptor = [$owning_message_class$ descriptor];\n"
111       "  GPBFieldDescriptor *field = [descriptor fieldWithNumber:$field_number_name$];\n"
112       "  GPBSetMessageRawEnumField(message, field, value);\n"
113       "}\n"
114       "\n");
115 }
116 
DetermineForwardDeclarations(std::set<std::string> * fwd_decls,bool include_external_types) const117 void EnumFieldGenerator::DetermineForwardDeclarations(
118     std::set<std::string>* fwd_decls,
119     bool include_external_types) const {
120   SingleFieldGenerator::DetermineForwardDeclarations(
121       fwd_decls, include_external_types);
122   // If it is an enum defined in a different file (and not a WKT), then we'll
123   // need a forward declaration for it.  When it is in our file, all the enums
124   // are output before the message, so it will be declared before it is needed.
125   if (include_external_types &&
126       descriptor_->file() != descriptor_->enum_type()->file() &&
127       !IsProtobufLibraryBundledProtoFile(descriptor_->enum_type()->file())) {
128     // Enum name is already in "storage_type".
129     const std::string& name = variable("storage_type");
130     fwd_decls->insert("GPB_ENUM_FWD_DECLARE(" + name + ")");
131   }
132 }
133 
RepeatedEnumFieldGenerator(const FieldDescriptor * descriptor)134 RepeatedEnumFieldGenerator::RepeatedEnumFieldGenerator(
135     const FieldDescriptor* descriptor)
136     : RepeatedFieldGenerator(descriptor) {
137   SetEnumVariables(descriptor, &variables_);
138   variables_["array_storage_type"] = "GPBEnumArray";
139 }
140 
~RepeatedEnumFieldGenerator()141 RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {}
142 
FinishInitialization(void)143 void RepeatedEnumFieldGenerator::FinishInitialization(void) {
144   RepeatedFieldGenerator::FinishInitialization();
145   variables_["array_comment"] =
146       "// |" + variables_["name"] + "| contains |" + variables_["storage_type"] + "|\n";
147 }
148 
149 }  // namespace objectivec
150 }  // namespace compiler
151 }  // namespace protobuf
152 }  // namespace google
153