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