temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2 | // Copyright 2008 Google Inc. All rights reserved. |
Feng Xiao | e428862 | 2014-10-01 16:26:23 -0700 | [diff] [blame] | 3 | // https://developers.google.com/protocol-buffers/ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 4 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 8 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 18 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 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_JAVA_MESSAGE_H__ |
| 36 | #define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__ |
| 37 | |
| 38 | #include <string> |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 39 | #include <map> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 40 | #include <google/protobuf/compiler/java/java_field.h> |
| 41 | |
| 42 | namespace google { |
| 43 | namespace protobuf { |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 44 | namespace compiler { |
| 45 | namespace java { |
| 46 | class Context; // context.h |
| 47 | class ClassNameResolver; // name_resolver.h |
| 48 | } |
| 49 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 50 | namespace io { |
| 51 | class Printer; // printer.h |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | namespace protobuf { |
| 56 | namespace compiler { |
| 57 | namespace java { |
| 58 | |
| 59 | class MessageGenerator { |
| 60 | public: |
| 61 | explicit MessageGenerator(const Descriptor* descriptor); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 62 | virtual ~MessageGenerator(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 63 | |
| 64 | // All static variables have to be declared at the top-level of the file |
| 65 | // so that we can control initialization order, which is important for |
| 66 | // DescriptorProto bootstrapping to work. |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 67 | virtual void GenerateStaticVariables(io::Printer* printer) = 0; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 68 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 69 | // Output code which initializes the static variables generated by |
Daniel Martin | 0f1393d | 2015-04-09 12:20:55 -0400 | [diff] [blame] | 70 | // GenerateStaticVariables(). Returns an estimate of bytecode size. |
Daniel Martin | e2416ca | 2014-11-25 10:37:57 -0500 | [diff] [blame] | 71 | virtual int GenerateStaticVariableInitializers(io::Printer* printer) = 0; |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 72 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 73 | // Generate the class itself. |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 74 | virtual void Generate(io::Printer* printer) = 0; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 75 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 76 | // Generates the base interface that both the class and its builder implement |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 77 | virtual void GenerateInterface(io::Printer* printer) = 0; |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 78 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 79 | // Generate code to register all contained extensions with an |
| 80 | // ExtensionRegistry. |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 81 | virtual void GenerateExtensionRegistrationCode(io::Printer* printer) = 0; |
| 82 | |
| 83 | protected: |
| 84 | const Descriptor* descriptor_; |
| 85 | |
| 86 | private: |
| 87 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator); |
| 88 | }; |
| 89 | |
| 90 | class ImmutableMessageGenerator : public MessageGenerator { |
| 91 | public: |
| 92 | explicit ImmutableMessageGenerator(const Descriptor* descriptor, |
| 93 | Context* context); |
| 94 | virtual ~ImmutableMessageGenerator(); |
| 95 | |
| 96 | virtual void Generate(io::Printer* printer); |
| 97 | virtual void GenerateInterface(io::Printer* printer); |
| 98 | virtual void GenerateExtensionRegistrationCode(io::Printer* printer); |
| 99 | virtual void GenerateStaticVariables(io::Printer* printer); |
Daniel Martin | 0f1393d | 2015-04-09 12:20:55 -0400 | [diff] [blame] | 100 | |
| 101 | // Returns an estimate of the number of bytes the printed code will compile to |
Daniel Martin | e2416ca | 2014-11-25 10:37:57 -0500 | [diff] [blame] | 102 | virtual int GenerateStaticVariableInitializers(io::Printer* printer); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 103 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 104 | private: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 105 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 106 | void GenerateFieldAccessorTable(io::Printer* printer); |
Daniel Martin | 0f1393d | 2015-04-09 12:20:55 -0400 | [diff] [blame] | 107 | |
| 108 | // Returns an estimate of the number of bytes the printed code will compile to |
Daniel Martin | e2416ca | 2014-11-25 10:37:57 -0500 | [diff] [blame] | 109 | int GenerateFieldAccessorTableInitializer(io::Printer* printer); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 110 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 111 | void GenerateMessageSerializationMethods(io::Printer* printer); |
| 112 | void GenerateParseFromMethods(io::Printer* printer); |
| 113 | void GenerateSerializeOneField(io::Printer* printer, |
| 114 | const FieldDescriptor* field); |
| 115 | void GenerateSerializeOneExtensionRange( |
| 116 | io::Printer* printer, const Descriptor::ExtensionRange* range); |
| 117 | |
| 118 | void GenerateBuilder(io::Printer* printer); |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 119 | void GenerateIsInitialized(io::Printer* printer); |
| 120 | void GenerateDescriptorMethods(io::Printer* printer); |
Feng Xiao | 99aa0f9 | 2014-11-20 16:18:53 -0800 | [diff] [blame] | 121 | void GenerateInitializers(io::Printer* printer); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 122 | void GenerateEqualsAndHashCode(io::Printer* printer); |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 123 | void GenerateParser(io::Printer* printer); |
| 124 | void GenerateParsingConstructor(io::Printer* printer); |
Feng Xiao | eee38b0 | 2015-08-22 18:25:48 -0700 | [diff] [blame] | 125 | void GenerateAnyMethods(io::Printer* printer); |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 126 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 127 | Context* context_; |
| 128 | ClassNameResolver* name_resolver_; |
| 129 | FieldGeneratorMap<ImmutableFieldGenerator> field_generators_; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 130 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 131 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableMessageGenerator); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | } // namespace java |
| 135 | } // namespace compiler |
| 136 | } // namespace protobuf |
| 137 | |
| 138 | } // namespace google |
| 139 | #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__ |