| 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2015 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_map_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 | // MapFieldGenerator uses RepeatedFieldGenerator as the parent because it |
| 44 | // provides a bunch of things (no has* methods, comments for contained type, |
| 45 | // etc.). |
| 46 | |
| 47 | namespace { |
| 48 | |
| 49 | const char* MapEntryTypeName(const FieldDescriptor* descriptor, bool isKey) { |
| 50 | ObjectiveCType type = GetObjectiveCType(field: descriptor); |
| 51 | switch (type) { |
| 52 | case OBJECTIVECTYPE_INT32: |
| 53 | return "Int32" ; |
| 54 | case OBJECTIVECTYPE_UINT32: |
| 55 | return "UInt32" ; |
| 56 | case OBJECTIVECTYPE_INT64: |
| 57 | return "Int64" ; |
| 58 | case OBJECTIVECTYPE_UINT64: |
| 59 | return "UInt64" ; |
| 60 | case OBJECTIVECTYPE_FLOAT: |
| 61 | return "Float" ; |
| 62 | case OBJECTIVECTYPE_DOUBLE: |
| 63 | return "Double" ; |
| 64 | case OBJECTIVECTYPE_BOOLEAN: |
| 65 | return "Bool" ; |
| 66 | case OBJECTIVECTYPE_STRING: |
| 67 | return (isKey ? "String" : "Object" ); |
| 68 | case OBJECTIVECTYPE_DATA: |
| 69 | return "Object" ; |
| 70 | case OBJECTIVECTYPE_ENUM: |
| 71 | return "Enum" ; |
| 72 | case OBJECTIVECTYPE_MESSAGE: |
| 73 | return "Object" ; |
| 74 | } |
| 75 | |
| 76 | // Some compilers report reaching end of function even though all cases of |
| 77 | // the enum are handed in the switch. |
| 78 | GOOGLE_LOG(FATAL) << "Can't get here." ; |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | } // namespace |
| 83 | |
| 84 | MapFieldGenerator::MapFieldGenerator(const FieldDescriptor* descriptor) |
| 85 | : RepeatedFieldGenerator(descriptor) { |
| 86 | const FieldDescriptor* key_descriptor = |
| 87 | descriptor->message_type()->map_key(); |
| 88 | const FieldDescriptor* value_descriptor = |
| 89 | descriptor->message_type()->map_value(); |
| 90 | value_field_generator_.reset(p: FieldGenerator::Make(field: value_descriptor)); |
| 91 | |
| 92 | // Pull over some variables_ from the value. |
| 93 | variables_["field_type" ] = value_field_generator_->variable(key: "field_type" ); |
| 94 | variables_["default" ] = value_field_generator_->variable(key: "default" ); |
| 95 | variables_["default_name" ] = value_field_generator_->variable(key: "default_name" ); |
| 96 | |
| 97 | // Build custom field flags. |
| 98 | std::vector<std::string> field_flags; |
| 99 | field_flags.push_back(x: "GPBFieldMapKey" + GetCapitalizedType(field: key_descriptor)); |
| 100 | // Pull over the current text format custom name values that was calculated. |
| 101 | if (variables_["fieldflags" ].find(s: "GPBFieldTextFormatNameCustom" ) != |
| 102 | std::string::npos) { |
| 103 | field_flags.push_back(x: "GPBFieldTextFormatNameCustom" ); |
| 104 | } |
| 105 | // Pull over some info from the value's flags. |
| 106 | const std::string& value_field_flags = |
| 107 | value_field_generator_->variable(key: "fieldflags" ); |
| 108 | if (value_field_flags.find(s: "GPBFieldHasDefaultValue" ) != std::string::npos) { |
| 109 | field_flags.push_back(x: "GPBFieldHasDefaultValue" ); |
| 110 | } |
| 111 | if (value_field_flags.find(s: "GPBFieldHasEnumDescriptor" ) != |
| 112 | std::string::npos) { |
| 113 | field_flags.push_back(x: "GPBFieldHasEnumDescriptor" ); |
| 114 | } |
| 115 | |
| 116 | variables_["fieldflags" ] = BuildFlagsString(type: FLAGTYPE_FIELD, strings: field_flags); |
| 117 | |
| 118 | ObjectiveCType value_objc_type = GetObjectiveCType(field: value_descriptor); |
| 119 | const bool value_is_object_type = |
| 120 | ((value_objc_type == OBJECTIVECTYPE_STRING) || |
| 121 | (value_objc_type == OBJECTIVECTYPE_DATA) || |
| 122 | (value_objc_type == OBJECTIVECTYPE_MESSAGE)); |
| 123 | if ((GetObjectiveCType(field: key_descriptor) == OBJECTIVECTYPE_STRING) && |
| 124 | value_is_object_type) { |
| 125 | variables_["array_storage_type" ] = "NSMutableDictionary" ; |
| 126 | variables_["array_property_type" ] = |
| 127 | "NSMutableDictionary<NSString*, " + |
| 128 | value_field_generator_->variable(key: "storage_type" ) + "*>" ; |
| 129 | } else { |
| 130 | std::string class_name("GPB" ); |
| 131 | class_name += MapEntryTypeName(descriptor: key_descriptor, isKey: true); |
| 132 | class_name += MapEntryTypeName(descriptor: value_descriptor, isKey: false); |
| 133 | class_name += "Dictionary" ; |
| 134 | variables_["array_storage_type" ] = class_name; |
| 135 | if (value_is_object_type) { |
| 136 | variables_["array_property_type" ] = |
| 137 | class_name + "<" + |
| 138 | value_field_generator_->variable(key: "storage_type" ) + "*>" ; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | variables_["dataTypeSpecific_name" ] = |
| 143 | value_field_generator_->variable(key: "dataTypeSpecific_name" ); |
| 144 | variables_["dataTypeSpecific_value" ] = |
| 145 | value_field_generator_->variable(key: "dataTypeSpecific_value" ); |
| 146 | } |
| 147 | |
| 148 | MapFieldGenerator::~MapFieldGenerator() {} |
| 149 | |
| 150 | void MapFieldGenerator::FinishInitialization(void) { |
| 151 | RepeatedFieldGenerator::FinishInitialization(); |
| 152 | // Use the array_comment support in RepeatedFieldGenerator to output what the |
| 153 | // values in the map are. |
| 154 | const FieldDescriptor* value_descriptor = |
| 155 | descriptor_->message_type()->map_value(); |
| 156 | if (GetObjectiveCType(field: value_descriptor) == OBJECTIVECTYPE_ENUM) { |
| 157 | variables_["array_comment" ] = |
| 158 | "// |" + variables_["name" ] + "| values are |" + value_field_generator_->variable(key: "storage_type" ) + "|\n" ; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void MapFieldGenerator::DetermineForwardDeclarations( |
| 163 | std::set<std::string>* fwd_decls, |
| 164 | bool include_external_types) const { |
| 165 | RepeatedFieldGenerator::DetermineForwardDeclarations( |
| 166 | fwd_decls, include_external_types); |
| 167 | const FieldDescriptor* value_descriptor = |
| 168 | descriptor_->message_type()->map_value(); |
| 169 | // Within a file there is no requirement on the order of the messages, so |
| 170 | // local references need a forward declaration. External files (not WKTs), |
| 171 | // need one when requested. |
| 172 | if (GetObjectiveCType(field: value_descriptor) == OBJECTIVECTYPE_MESSAGE && |
| 173 | ((include_external_types && |
| 174 | !IsProtobufLibraryBundledProtoFile(file: value_descriptor->file())) || |
| 175 | descriptor_->file() == value_descriptor->file())) { |
| 176 | const std::string& value_storage_type = |
| 177 | value_field_generator_->variable(key: "storage_type" ); |
| 178 | fwd_decls->insert(x: "@class " + value_storage_type); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | void MapFieldGenerator::DetermineObjectiveCClassDefinitions( |
| 183 | std::set<std::string>* fwd_decls) const { |
| 184 | // Class name is already in "storage_type". |
| 185 | const FieldDescriptor* value_descriptor = |
| 186 | descriptor_->message_type()->map_value(); |
| 187 | if (GetObjectiveCType(field: value_descriptor) == OBJECTIVECTYPE_MESSAGE) { |
| 188 | fwd_decls->insert(x: ObjCClassDeclaration( |
| 189 | class_name: value_field_generator_->variable(key: "storage_type" ))); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | } // namespace objectivec |
| 194 | } // namespace compiler |
| 195 | } // namespace protobuf |
| 196 | } // namespace google |
| 197 | |