• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Author: kenton@google.com (Kenton Varda)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 
35 #include <google/protobuf/compiler/cpp/cpp_field.h>
36 #include <memory>
37 
38 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
39 #include <google/protobuf/compiler/cpp/cpp_primitive_field.h>
40 #include <google/protobuf/compiler/cpp/cpp_string_field.h>
41 #include <google/protobuf/stubs/logging.h>
42 #include <google/protobuf/stubs/common.h>
43 #include <google/protobuf/compiler/cpp/cpp_enum_field.h>
44 #include <google/protobuf/compiler/cpp/cpp_map_field.h>
45 #include <google/protobuf/compiler/cpp/cpp_message_field.h>
46 #include <google/protobuf/descriptor.pb.h>
47 #include <google/protobuf/io/printer.h>
48 #include <google/protobuf/wire_format.h>
49 #include <google/protobuf/stubs/strutil.h>
50 
51 namespace google {
52 namespace protobuf {
53 namespace compiler {
54 namespace cpp {
55 
56 using internal::WireFormat;
57 
SetCommonFieldVariables(const FieldDescriptor * descriptor,std::map<std::string,std::string> * variables,const Options & options)58 void SetCommonFieldVariables(const FieldDescriptor* descriptor,
59                              std::map<std::string, std::string>* variables,
60                              const Options& options) {
61   SetCommonVars(options, variables);
62   (*variables)["ns"] = Namespace(descriptor, options);
63   (*variables)["name"] = FieldName(descriptor);
64   (*variables)["index"] = StrCat(descriptor->index());
65   (*variables)["number"] = StrCat(descriptor->number());
66   (*variables)["classname"] = ClassName(FieldScope(descriptor), false);
67   (*variables)["declared_type"] = DeclaredTypeMethodName(descriptor->type());
68   (*variables)["field_member"] = FieldName(descriptor) + "_";
69 
70   (*variables)["tag_size"] = StrCat(
71       WireFormat::TagSize(descriptor->number(), descriptor->type()));
72   (*variables)["deprecated_attr"] = DeprecatedAttribute(options, descriptor);
73 
74   (*variables)["set_hasbit"] = "";
75   (*variables)["clear_hasbit"] = "";
76   if (HasHasbit(descriptor)) {
77     (*variables)["set_hasbit_io"] =
78         "_Internal::set_has_" + FieldName(descriptor) + "(&_has_bits_);";
79   } else {
80     (*variables)["set_hasbit_io"] = "";
81   }
82   (*variables)["annotate_accessor"] = "";
83 
84   // These variables are placeholders to pick out the beginning and ends of
85   // identifiers for annotations (when doing so with existing variables would
86   // be ambiguous or impossible). They should never be set to anything but the
87   // empty string.
88   (*variables)["{"] = "";
89   (*variables)["}"] = "";
90 }
91 
SetHasBitIndex(int32 has_bit_index)92 void FieldGenerator::SetHasBitIndex(int32 has_bit_index) {
93   if (!HasHasbit(descriptor_)) {
94     GOOGLE_CHECK_EQ(has_bit_index, -1);
95     return;
96   }
97   variables_["set_hasbit"] = StrCat(
98       "_has_bits_[", has_bit_index / 32, "] |= 0x",
99       strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8), "u;");
100   variables_["clear_hasbit"] = StrCat(
101       "_has_bits_[", has_bit_index / 32, "] &= ~0x",
102       strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8), "u;");
103 }
104 
SetCommonOneofFieldVariables(const FieldDescriptor * descriptor,std::map<std::string,std::string> * variables)105 void SetCommonOneofFieldVariables(
106     const FieldDescriptor* descriptor,
107     std::map<std::string, std::string>* variables) {
108   const std::string prefix = descriptor->containing_oneof()->name() + "_.";
109   (*variables)["oneof_name"] = descriptor->containing_oneof()->name();
110   (*variables)["field_member"] =
111       StrCat(prefix, (*variables)["name"], "_");
112 }
113 
~FieldGenerator()114 FieldGenerator::~FieldGenerator() {}
115 
FieldGeneratorMap(const Descriptor * descriptor,const Options & options,MessageSCCAnalyzer * scc_analyzer)116 FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor,
117                                      const Options& options,
118                                      MessageSCCAnalyzer* scc_analyzer)
119     : descriptor_(descriptor), field_generators_(descriptor->field_count()) {
120   // Construct all the FieldGenerators.
121   for (int i = 0; i < descriptor->field_count(); i++) {
122     field_generators_[i].reset(
123         MakeGenerator(descriptor->field(i), options, scc_analyzer));
124   }
125 }
126 
MakeGoogleInternalGenerator(const FieldDescriptor * field,const Options & options,MessageSCCAnalyzer * scc_analyzer)127 FieldGenerator* FieldGeneratorMap::MakeGoogleInternalGenerator(
128     const FieldDescriptor* field, const Options& options,
129     MessageSCCAnalyzer* scc_analyzer) {
130 
131   return nullptr;
132 }
133 
MakeGenerator(const FieldDescriptor * field,const Options & options,MessageSCCAnalyzer * scc_analyzer)134 FieldGenerator* FieldGeneratorMap::MakeGenerator(
135     const FieldDescriptor* field, const Options& options,
136     MessageSCCAnalyzer* scc_analyzer) {
137   FieldGenerator* generator =
138       MakeGoogleInternalGenerator(field, options, scc_analyzer);
139   if (generator) {
140     return generator;
141   }
142 
143   if (field->is_repeated()) {
144     switch (field->cpp_type()) {
145       case FieldDescriptor::CPPTYPE_MESSAGE:
146         if (field->is_map()) {
147           return new MapFieldGenerator(field, options);
148         } else {
149           return new RepeatedMessageFieldGenerator(field, options,
150                                                    scc_analyzer);
151         }
152       case FieldDescriptor::CPPTYPE_STRING:
153         return new RepeatedStringFieldGenerator(field, options);
154       case FieldDescriptor::CPPTYPE_ENUM:
155         return new RepeatedEnumFieldGenerator(field, options);
156       default:
157         return new RepeatedPrimitiveFieldGenerator(field, options);
158     }
159   } else if (field->real_containing_oneof()) {
160     switch (field->cpp_type()) {
161       case FieldDescriptor::CPPTYPE_MESSAGE:
162         return new MessageOneofFieldGenerator(field, options, scc_analyzer);
163       case FieldDescriptor::CPPTYPE_STRING:
164         return new StringOneofFieldGenerator(field, options);
165       case FieldDescriptor::CPPTYPE_ENUM:
166         return new EnumOneofFieldGenerator(field, options);
167       default:
168         return new PrimitiveOneofFieldGenerator(field, options);
169     }
170   } else {
171     switch (field->cpp_type()) {
172       case FieldDescriptor::CPPTYPE_MESSAGE:
173         return new MessageFieldGenerator(field, options, scc_analyzer);
174       case FieldDescriptor::CPPTYPE_STRING:
175         return new StringFieldGenerator(field, options);
176       case FieldDescriptor::CPPTYPE_ENUM:
177         return new EnumFieldGenerator(field, options);
178       default:
179         return new PrimitiveFieldGenerator(field, options);
180     }
181   }
182 }
183 
~FieldGeneratorMap()184 FieldGeneratorMap::~FieldGeneratorMap() {}
185 
get(const FieldDescriptor * field) const186 const FieldGenerator& FieldGeneratorMap::get(
187     const FieldDescriptor* field) const {
188   GOOGLE_CHECK_EQ(field->containing_type(), descriptor_);
189   return *field_generators_[field->index()];
190 }
191 
192 }  // namespace cpp
193 }  // namespace compiler
194 }  // namespace protobuf
195 }  // namespace google
196