blob: e9fc57c26aaaa1b4ad012c9a9139b5c5fb651c9c [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
temporal40ee5512008-07-10 02:12:20 +00004//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * 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.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// 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.
temporal40ee5512008-07-10 02:12:20 +000030
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.com4de8f552014-07-18 00:47:59 +000039#include <map>
temporal40ee5512008-07-10 02:12:20 +000040#include <google/protobuf/compiler/java/java_field.h>
41
42namespace google {
43namespace protobuf {
jieluo@google.com4de8f552014-07-18 00:47:59 +000044 namespace compiler {
45 namespace java {
46 class Context; // context.h
47 class ClassNameResolver; // name_resolver.h
48 }
49 }
temporal40ee5512008-07-10 02:12:20 +000050 namespace io {
51 class Printer; // printer.h
52 }
53}
54
55namespace protobuf {
56namespace compiler {
57namespace java {
58
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070059static const int kMaxStaticSize = 1 << 15; // aka 32k
60
temporal40ee5512008-07-10 02:12:20 +000061class MessageGenerator {
62 public:
63 explicit MessageGenerator(const Descriptor* descriptor);
jieluo@google.com4de8f552014-07-18 00:47:59 +000064 virtual ~MessageGenerator();
temporal40ee5512008-07-10 02:12:20 +000065
66 // All static variables have to be declared at the top-level of the file
67 // so that we can control initialization order, which is important for
68 // DescriptorProto bootstrapping to work.
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070069 virtual void GenerateStaticVariables(
70 io::Printer* printer, int* bytecode_estimate) = 0;
temporal40ee5512008-07-10 02:12:20 +000071
kenton@google.com24bf56f2008-09-24 20:31:01 +000072 // Output code which initializes the static variables generated by
Daniel Martin0f1393d2015-04-09 12:20:55 -040073 // GenerateStaticVariables(). Returns an estimate of bytecode size.
Daniel Martine2416ca2014-11-25 10:37:57 -050074 virtual int GenerateStaticVariableInitializers(io::Printer* printer) = 0;
kenton@google.com24bf56f2008-09-24 20:31:01 +000075
temporal40ee5512008-07-10 02:12:20 +000076 // Generate the class itself.
jieluo@google.com4de8f552014-07-18 00:47:59 +000077 virtual void Generate(io::Printer* printer) = 0;
temporal40ee5512008-07-10 02:12:20 +000078
liujisi@google.com33165fe2010-11-02 13:14:58 +000079 // Generates the base interface that both the class and its builder implement
jieluo@google.com4de8f552014-07-18 00:47:59 +000080 virtual void GenerateInterface(io::Printer* printer) = 0;
liujisi@google.com33165fe2010-11-02 13:14:58 +000081
kenton@google.com24bf56f2008-09-24 20:31:01 +000082 // Generate code to register all contained extensions with an
83 // ExtensionRegistry.
jieluo@google.com4de8f552014-07-18 00:47:59 +000084 virtual void GenerateExtensionRegistrationCode(io::Printer* printer) = 0;
85
86 protected:
87 const Descriptor* descriptor_;
88
89 private:
90 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator);
91};
92
93class ImmutableMessageGenerator : public MessageGenerator {
94 public:
95 explicit ImmutableMessageGenerator(const Descriptor* descriptor,
96 Context* context);
97 virtual ~ImmutableMessageGenerator();
98
99 virtual void Generate(io::Printer* printer);
100 virtual void GenerateInterface(io::Printer* printer);
101 virtual void GenerateExtensionRegistrationCode(io::Printer* printer);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700102 virtual void GenerateStaticVariables(
103 io::Printer* printer, int* bytecode_estimate);
Daniel Martin0f1393d2015-04-09 12:20:55 -0400104
105 // Returns an estimate of the number of bytes the printed code will compile to
Daniel Martine2416ca2014-11-25 10:37:57 -0500106 virtual int GenerateStaticVariableInitializers(io::Printer* printer);
kenton@google.com24bf56f2008-09-24 20:31:01 +0000107
temporal40ee5512008-07-10 02:12:20 +0000108 private:
liujisi@google.com33165fe2010-11-02 13:14:58 +0000109
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700110 void GenerateFieldAccessorTable(io::Printer* printer, int* bytecode_estimate);
Daniel Martin0f1393d2015-04-09 12:20:55 -0400111
112 // Returns an estimate of the number of bytes the printed code will compile to
Daniel Martine2416ca2014-11-25 10:37:57 -0500113 int GenerateFieldAccessorTableInitializer(io::Printer* printer);
jieluo@google.com4de8f552014-07-18 00:47:59 +0000114
temporal40ee5512008-07-10 02:12:20 +0000115 void GenerateMessageSerializationMethods(io::Printer* printer);
116 void GenerateParseFromMethods(io::Printer* printer);
117 void GenerateSerializeOneField(io::Printer* printer,
118 const FieldDescriptor* field);
119 void GenerateSerializeOneExtensionRange(
120 io::Printer* printer, const Descriptor::ExtensionRange* range);
121
122 void GenerateBuilder(io::Printer* printer);
Bo Yang5db21732015-05-21 14:28:59 -0700123 void GenerateIsInitialized(io::Printer* printer);
124 void GenerateDescriptorMethods(io::Printer* printer);
Feng Xiao99aa0f92014-11-20 16:18:53 -0800125 void GenerateInitializers(io::Printer* printer);
liujisi@google.com33165fe2010-11-02 13:14:58 +0000126 void GenerateEqualsAndHashCode(io::Printer* printer);
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000127 void GenerateParser(io::Printer* printer);
128 void GenerateParsingConstructor(io::Printer* printer);
Feng Xiaoeee38b02015-08-22 18:25:48 -0700129 void GenerateAnyMethods(io::Printer* printer);
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000130
jieluo@google.com4de8f552014-07-18 00:47:59 +0000131 Context* context_;
132 ClassNameResolver* name_resolver_;
133 FieldGeneratorMap<ImmutableFieldGenerator> field_generators_;
temporal40ee5512008-07-10 02:12:20 +0000134
jieluo@google.com4de8f552014-07-18 00:47:59 +0000135 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableMessageGenerator);
temporal40ee5512008-07-10 02:12:20 +0000136};
137
138} // namespace java
139} // namespace compiler
140} // namespace protobuf
141
142} // namespace google
143#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__