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 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__
36 #define GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__
37 
38 #include <memory>
39 #include <string>
40 #include <vector>
41 #include <google/protobuf/compiler/cpp/cpp_field.h>
42 #include <google/protobuf/compiler/cpp/cpp_options.h>
43 
44 namespace google {
45 namespace protobuf {
46   namespace io {
47     class Printer;             // printer.h
48   }
49 }
50 
51 namespace protobuf {
52 namespace compiler {
53 namespace cpp {
54 
55 class EnumGenerator;           // enum.h
56 class ExtensionGenerator;      // extension.h
57 
58 class MessageGenerator {
59  public:
60   // See generator.cc for the meaning of dllexport_decl.
61   explicit MessageGenerator(const Descriptor* descriptor,
62                             const Options& options);
63   ~MessageGenerator();
64 
65   // Header stuff.
66 
67   // Generate foward declarations for this class and all its nested types.
68   void GenerateForwardDeclaration(io::Printer* printer);
69 
70   // Generate definitions of all nested enums (must come before class
71   // definitions because those classes use the enums definitions).
72   void GenerateEnumDefinitions(io::Printer* printer);
73 
74   // Generate specializations of GetEnumDescriptor<MyEnum>().
75   // Precondition: in ::google::protobuf namespace.
76   void GenerateGetEnumDescriptorSpecializations(io::Printer* printer);
77 
78   // Generate definitions for this class and all its nested types.
79   void GenerateClassDefinition(io::Printer* printer);
80 
81   // Generate definitions of inline methods (placed at the end of the header
82   // file).
83   void GenerateInlineMethods(io::Printer* printer);
84 
85   // Source file stuff.
86 
87   // Generate code which declares all the global descriptor pointers which
88   // will be initialized by the methods below.
89   void GenerateDescriptorDeclarations(io::Printer* printer);
90 
91   // Generate code that initializes the global variable storing the message's
92   // descriptor.
93   void GenerateDescriptorInitializer(io::Printer* printer, int index);
94 
95   // Generate code that calls MessageFactory::InternalRegisterGeneratedMessage()
96   // for all types.
97   void GenerateTypeRegistrations(io::Printer* printer);
98 
99   // Generates code that allocates the message's default instance.
100   void GenerateDefaultInstanceAllocator(io::Printer* printer);
101 
102   // Generates code that initializes the message's default instance.  This
103   // is separate from allocating because all default instances must be
104   // allocated before any can be initialized.
105   void GenerateDefaultInstanceInitializer(io::Printer* printer);
106 
107   // Generates code that should be run when ShutdownProtobufLibrary() is called,
108   // to delete all dynamically-allocated objects.
109   void GenerateShutdownCode(io::Printer* printer);
110 
111   // Generate all non-inline methods for this class.
112   void GenerateClassMethods(io::Printer* printer);
113 
114  private:
115   // Generate declarations and definitions of accessors for fields.
116   void GenerateFieldAccessorDeclarations(io::Printer* printer);
117   void GenerateFieldAccessorDefinitions(io::Printer* printer);
118 
119   // Generate the field offsets array.
120   void GenerateOffsets(io::Printer* printer);
121 
122   // Generate constructors and destructor.
123   void GenerateStructors(io::Printer* printer);
124 
125   // The compiler typically generates multiple copies of each constructor and
126   // destructor: http://gcc.gnu.org/bugs.html#nonbugs_cxx
127   // Placing common code in a separate method reduces the generated code size.
128   //
129   // Generate the shared constructor code.
130   void GenerateSharedConstructorCode(io::Printer* printer);
131   // Generate the shared destructor code.
132   void GenerateSharedDestructorCode(io::Printer* printer);
133 
134   // Generate standard Message methods.
135   void GenerateClear(io::Printer* printer);
136   void GenerateOneofClear(io::Printer* printer);
137   void GenerateMergeFromCodedStream(io::Printer* printer);
138   void GenerateSerializeWithCachedSizes(io::Printer* printer);
139   void GenerateSerializeWithCachedSizesToArray(io::Printer* printer);
140   void GenerateSerializeWithCachedSizesBody(io::Printer* printer,
141                                             bool to_array);
142   void GenerateByteSize(io::Printer* printer);
143   void GenerateMergeFrom(io::Printer* printer);
144   void GenerateCopyFrom(io::Printer* printer);
145   void GenerateSwap(io::Printer* printer);
146   void GenerateIsInitialized(io::Printer* printer);
147 
148   // Helpers for GenerateSerializeWithCachedSizes().
149   void GenerateSerializeOneField(io::Printer* printer,
150                                  const FieldDescriptor* field,
151                                  bool unbounded);
152   void GenerateSerializeOneExtensionRange(
153       io::Printer* printer, const Descriptor::ExtensionRange* range,
154       bool unbounded);
155 
156 
157   const Descriptor* descriptor_;
158   string classname_;
159   Options options_;
160   FieldGeneratorMap field_generators_;
161   vector< vector<string> > runs_of_fields_;  // that might be trivially cleared
162   scoped_array<scoped_ptr<MessageGenerator> > nested_generators_;
163   scoped_array<scoped_ptr<EnumGenerator> > enum_generators_;
164   scoped_array<scoped_ptr<ExtensionGenerator> > extension_generators_;
165   bool uses_string_;
166 
167   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator);
168 };
169 
170 }  // namespace cpp
171 }  // namespace compiler
172 }  // namespace protobuf
173 
174 }  // namespace google
175 #endif  // GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__
176