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