Line data Source code
1 : // Protocol Buffers - Google's data interchange format
2 : // Copyright 2008 Google Inc. All rights reserved.
3 : // http://code.google.com/p/protobuf/
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: bduff@google.com (Brian Duff)
32 :
33 : #include <google/protobuf/compiler/javanano/javanano_extension.h>
34 : #include <google/protobuf/compiler/javanano/javanano_helpers.h>
35 : #include <google/protobuf/io/printer.h>
36 : #include <google/protobuf/stubs/strutil.h>
37 : #include <google/protobuf/wire_format.h>
38 :
39 : namespace google {
40 : namespace protobuf {
41 : namespace compiler {
42 : namespace javanano {
43 :
44 : using internal::WireFormat;
45 : using internal::WireFormatLite;
46 :
47 : namespace {
48 :
49 0 : const char* GetTypeConstantName(const FieldDescriptor::Type type) {
50 0 : switch (type) {
51 : case FieldDescriptor::TYPE_INT32 : return "TYPE_INT32" ;
52 0 : case FieldDescriptor::TYPE_UINT32 : return "TYPE_UINT32" ;
53 0 : case FieldDescriptor::TYPE_SINT32 : return "TYPE_SINT32" ;
54 0 : case FieldDescriptor::TYPE_FIXED32 : return "TYPE_FIXED32" ;
55 0 : case FieldDescriptor::TYPE_SFIXED32: return "TYPE_SFIXED32";
56 0 : case FieldDescriptor::TYPE_INT64 : return "TYPE_INT64" ;
57 0 : case FieldDescriptor::TYPE_UINT64 : return "TYPE_UINT64" ;
58 0 : case FieldDescriptor::TYPE_SINT64 : return "TYPE_SINT64" ;
59 0 : case FieldDescriptor::TYPE_FIXED64 : return "TYPE_FIXED64" ;
60 0 : case FieldDescriptor::TYPE_SFIXED64: return "TYPE_SFIXED64";
61 0 : case FieldDescriptor::TYPE_FLOAT : return "TYPE_FLOAT" ;
62 0 : case FieldDescriptor::TYPE_DOUBLE : return "TYPE_DOUBLE" ;
63 0 : case FieldDescriptor::TYPE_BOOL : return "TYPE_BOOL" ;
64 0 : case FieldDescriptor::TYPE_STRING : return "TYPE_STRING" ;
65 0 : case FieldDescriptor::TYPE_BYTES : return "TYPE_BYTES" ;
66 0 : case FieldDescriptor::TYPE_ENUM : return "TYPE_ENUM" ;
67 0 : case FieldDescriptor::TYPE_GROUP : return "TYPE_GROUP" ;
68 0 : case FieldDescriptor::TYPE_MESSAGE : return "TYPE_MESSAGE" ;
69 :
70 : // No default because we want the compiler to complain if any new
71 : // types are added.
72 : }
73 :
74 0 : GOOGLE_LOG(FATAL) << "Can't get here.";
75 0 : return NULL;
76 : }
77 :
78 : } // namespace
79 :
80 0 : void SetVariables(const FieldDescriptor* descriptor, const Params params,
81 : map<string, string>* variables) {
82 0 : (*variables)["extends"] = ClassName(params, descriptor->containing_type());
83 0 : (*variables)["name"] = RenameJavaKeywords(UnderscoresToCamelCase(descriptor));
84 0 : bool repeated = descriptor->is_repeated();
85 0 : (*variables)["repeated"] = repeated ? "Repeated" : "";
86 0 : (*variables)["type"] = GetTypeConstantName(descriptor->type());
87 0 : JavaType java_type = GetJavaType(descriptor->type());
88 0 : string tag = SimpleItoa(WireFormat::MakeTag(descriptor));
89 0 : if (java_type == JAVATYPE_MESSAGE) {
90 0 : (*variables)["ext_type"] = "MessageTyped";
91 0 : string message_type = ClassName(params, descriptor->message_type());
92 0 : if (repeated) {
93 : message_type += "[]";
94 : }
95 0 : (*variables)["class"] = message_type;
96 : // For message typed extensions, tags_params contains a single tag
97 : // for both singular and repeated cases.
98 0 : (*variables)["tag_params"] = tag;
99 : } else {
100 0 : (*variables)["ext_type"] = "PrimitiveTyped";
101 0 : if (!repeated) {
102 0 : (*variables)["class"] = BoxedPrimitiveTypeName(java_type);
103 0 : (*variables)["tag_params"] = tag;
104 : } else {
105 0 : (*variables)["class"] = PrimitiveTypeName(java_type) + "[]";
106 0 : if (!descriptor->is_packable()) {
107 : // Non-packable: nonPackedTag == tag, packedTag == 0
108 0 : (*variables)["tag_params"] = tag + ", " + tag + ", 0";
109 0 : } else if (descriptor->options().packed()) {
110 : // Packable and packed: tag == packedTag
111 : string non_packed_tag = SimpleItoa(WireFormatLite::MakeTag(
112 : descriptor->number(),
113 0 : WireFormat::WireTypeForFieldType(descriptor->type())));
114 0 : (*variables)["tag_params"] = tag + ", " + non_packed_tag + ", " + tag;
115 : } else {
116 : // Packable and not packed: tag == nonPackedTag
117 : string packed_tag = SimpleItoa(WireFormatLite::MakeTag(
118 0 : descriptor->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED));
119 0 : (*variables)["tag_params"] = tag + ", " + tag + ", " + packed_tag;
120 : }
121 : }
122 : }
123 0 : }
124 :
125 0 : ExtensionGenerator::
126 : ExtensionGenerator(const FieldDescriptor* descriptor, const Params& params)
127 0 : : params_(params), descriptor_(descriptor) {
128 0 : SetVariables(descriptor, params, &variables_);
129 0 : }
130 :
131 0 : ExtensionGenerator::~ExtensionGenerator() {}
132 :
133 0 : void ExtensionGenerator::Generate(io::Printer* printer) const {
134 0 : printer->Print("\n");
135 0 : PrintFieldComment(printer, descriptor_);
136 : printer->Print(variables_,
137 : "public static final com.google.protobuf.nano.Extension<\n"
138 : " $extends$,\n"
139 : " $class$> $name$ =\n"
140 : " com.google.protobuf.nano.Extension.create$repeated$$ext_type$(\n"
141 : " com.google.protobuf.nano.Extension.$type$,\n"
142 : " $class$.class,\n"
143 0 : " $tag_params$L);\n");
144 0 : }
145 :
146 : } // namespace javanano
147 : } // namespace compiler
148 : } // namespace protobuf
149 : } // namespace google
150 :
|