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