OLD | NEW |
(Empty) | |
| 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 <sstream> |
| 32 |
| 33 #include <google/protobuf/compiler/code_generator.h> |
| 34 #include <google/protobuf/compiler/plugin.h> |
| 35 #include <google/protobuf/descriptor.h> |
| 36 #include <google/protobuf/descriptor.pb.h> |
| 37 #include <google/protobuf/io/printer.h> |
| 38 #include <google/protobuf/io/zero_copy_stream.h> |
| 39 #include <google/protobuf/stubs/strutil.h> |
| 40 |
| 41 #include <google/protobuf/compiler/csharp/csharp_doc_comment.h> |
| 42 #include <google/protobuf/compiler/csharp/csharp_helpers.h> |
| 43 #include <google/protobuf/compiler/csharp/csharp_message_field.h> |
| 44 |
| 45 namespace google { |
| 46 namespace protobuf { |
| 47 namespace compiler { |
| 48 namespace csharp { |
| 49 |
| 50 MessageFieldGenerator::MessageFieldGenerator(const FieldDescriptor* descriptor, |
| 51 int fieldOrdinal) |
| 52 : FieldGeneratorBase(descriptor, fieldOrdinal) { |
| 53 variables_["has_property_check"] = name() + "_ != null"; |
| 54 variables_["has_not_property_check"] = name() + "_ == null"; |
| 55 } |
| 56 |
| 57 MessageFieldGenerator::~MessageFieldGenerator() { |
| 58 |
| 59 } |
| 60 |
| 61 void MessageFieldGenerator::GenerateMembers(io::Printer* printer) { |
| 62 printer->Print( |
| 63 variables_, |
| 64 "private $type_name$ $name$_;\n"); |
| 65 WritePropertyDocComment(printer, descriptor_); |
| 66 AddDeprecatedFlag(printer); |
| 67 printer->Print( |
| 68 variables_, |
| 69 "$access_level$ $type_name$ $property_name$ {\n" |
| 70 " get { return $name$_; }\n" |
| 71 " set {\n" |
| 72 " $name$_ = value;\n" |
| 73 " }\n" |
| 74 "}\n"); |
| 75 } |
| 76 |
| 77 void MessageFieldGenerator::GenerateMergingCode(io::Printer* printer) { |
| 78 printer->Print( |
| 79 variables_, |
| 80 "if (other.$has_property_check$) {\n" |
| 81 " if ($has_not_property_check$) {\n" |
| 82 " $name$_ = new $type_name$();\n" |
| 83 " }\n" |
| 84 " $property_name$.MergeFrom(other.$property_name$);\n" |
| 85 "}\n"); |
| 86 } |
| 87 |
| 88 void MessageFieldGenerator::GenerateParsingCode(io::Printer* printer) { |
| 89 printer->Print( |
| 90 variables_, |
| 91 "if ($has_not_property_check$) {\n" |
| 92 " $name$_ = new $type_name$();\n" |
| 93 "}\n" |
| 94 // TODO(jonskeet): Do we really need merging behaviour like this? |
| 95 "input.ReadMessage($name$_);\n"); // No need to support TYPE_GROUP... |
| 96 } |
| 97 |
| 98 void MessageFieldGenerator::GenerateSerializationCode(io::Printer* printer) { |
| 99 printer->Print( |
| 100 variables_, |
| 101 "if ($has_property_check$) {\n" |
| 102 " output.WriteRawTag($tag_bytes$);\n" |
| 103 " output.WriteMessage($property_name$);\n" |
| 104 "}\n"); |
| 105 } |
| 106 |
| 107 void MessageFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) { |
| 108 printer->Print( |
| 109 variables_, |
| 110 "if ($has_property_check$) {\n" |
| 111 " size += $tag_size$ + pb::CodedOutputStream.ComputeMessageSize($property_n
ame$);\n" |
| 112 "}\n"); |
| 113 } |
| 114 |
| 115 void MessageFieldGenerator::WriteHash(io::Printer* printer) { |
| 116 printer->Print( |
| 117 variables_, |
| 118 "if ($has_property_check$) hash ^= $property_name$.GetHashCode();\n"); |
| 119 } |
| 120 void MessageFieldGenerator::WriteEquals(io::Printer* printer) { |
| 121 printer->Print( |
| 122 variables_, |
| 123 "if (!object.Equals($property_name$, other.$property_name$)) return false;\n
"); |
| 124 } |
| 125 void MessageFieldGenerator::WriteToString(io::Printer* printer) { |
| 126 variables_["field_name"] = GetFieldName(descriptor_); |
| 127 printer->Print( |
| 128 variables_, |
| 129 "PrintField(\"$field_name$\", has$property_name$, $name$_, writer);\n"); |
| 130 } |
| 131 |
| 132 void MessageFieldGenerator::GenerateCloningCode(io::Printer* printer) { |
| 133 printer->Print(variables_, |
| 134 "$property_name$ = other.$has_property_check$ ? other.$property_name$.Clone(
) : null;\n"); |
| 135 } |
| 136 |
| 137 void MessageFieldGenerator::GenerateFreezingCode(io::Printer* printer) { |
| 138 } |
| 139 |
| 140 void MessageFieldGenerator::GenerateCodecCode(io::Printer* printer) { |
| 141 printer->Print( |
| 142 variables_, |
| 143 "pb::FieldCodec.ForMessage($tag$, $type_name$.Parser)"); |
| 144 } |
| 145 |
| 146 MessageOneofFieldGenerator::MessageOneofFieldGenerator(const FieldDescriptor* de
scriptor, |
| 147 int fieldOrdinal) |
| 148 : MessageFieldGenerator(descriptor, fieldOrdinal) { |
| 149 SetCommonOneofFieldVariables(&variables_); |
| 150 } |
| 151 |
| 152 MessageOneofFieldGenerator::~MessageOneofFieldGenerator() { |
| 153 |
| 154 } |
| 155 |
| 156 void MessageOneofFieldGenerator::GenerateMembers(io::Printer* printer) { |
| 157 WritePropertyDocComment(printer, descriptor_); |
| 158 AddDeprecatedFlag(printer); |
| 159 printer->Print( |
| 160 variables_, |
| 161 "$access_level$ $type_name$ $property_name$ {\n" |
| 162 " get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : null; }
\n" |
| 163 " set {\n" |
| 164 " $oneof_name$_ = value;\n" |
| 165 " $oneof_name$Case_ = value == null ? $oneof_property_name$OneofCase.None
: $oneof_property_name$OneofCase.$property_name$;\n" |
| 166 " }\n" |
| 167 "}\n"); |
| 168 } |
| 169 |
| 170 void MessageOneofFieldGenerator::GenerateParsingCode(io::Printer* printer) { |
| 171 // TODO(jonskeet): We may be able to do better than this |
| 172 printer->Print( |
| 173 variables_, |
| 174 "$type_name$ subBuilder = new $type_name$();\n" |
| 175 "if ($has_property_check$) {\n" |
| 176 " subBuilder.MergeFrom($property_name$);\n" |
| 177 "}\n" |
| 178 "input.ReadMessage(subBuilder);\n" // No support of TYPE_GROUP |
| 179 "$property_name$ = subBuilder;\n"); |
| 180 } |
| 181 |
| 182 void MessageOneofFieldGenerator::WriteToString(io::Printer* printer) { |
| 183 printer->Print( |
| 184 variables_, |
| 185 "PrintField(\"$descriptor_name$\", $has_property_check$, $oneof_name$_, writ
er);\n"); |
| 186 } |
| 187 |
| 188 void MessageOneofFieldGenerator::GenerateCloningCode(io::Printer* printer) { |
| 189 printer->Print(variables_, |
| 190 "$property_name$ = other.$property_name$.Clone();\n"); |
| 191 } |
| 192 |
| 193 } // namespace csharp |
| 194 } // namespace compiler |
| 195 } // namespace protobuf |
| 196 } // namespace google |
OLD | NEW |