webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
javanano_helpers.h
Go to the documentation of this file.
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: 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_JAVANANO_HELPERS_H__
36 #define GOOGLE_PROTOBUF_COMPILER_JAVANANO_HELPERS_H__
37 
38 #include <string>
43 
44 namespace google {
45 namespace protobuf {
46 namespace compiler {
47 namespace javanano {
48 
49 // Commonly-used separator comments. Thick is a line of '=', thin is a line
50 // of '-'.
51 extern const char kThickSeparator[];
52 extern const char kThinSeparator[];
53 
54 // Converts the field's name to camel-case, e.g. "foo_bar_baz" becomes
55 // "fooBarBaz" or "FooBarBaz", respectively.
57 string UnderscoresToCamelCase(const OneofDescriptor* oneof);
60 
61 // Appends an "_" to the end of a field where the name is a reserved java
62 // keyword. For example int32 public = 1 will generate int public_.
63 string RenameJavaKeywords(const string& input);
64 
65 // Similar, but for method names. (Typically, this merely has the effect
66 // of lower-casing the first letter of the name.)
67 string UnderscoresToCamelCase(const MethodDescriptor* method);
68 
69 // Strips ".proto" or ".protodevel" from the end of a filename.
70 string StripProto(const string& filename);
71 
72 // Gets the unqualified class name for the file. Each .proto file becomes a
73 // single Java class, with all its contents nested in that class.
74 string FileClassName(const Params& params, const FileDescriptor* file);
75 
76 // Returns the file's Java package name.
77 string FileJavaPackage(const Params& params, const FileDescriptor* file);
78 
79 // Returns whether the Java outer class is needed, i.e. whether the option
80 // java_multiple_files is false, or the proto file contains any file-scope
81 // enums/extensions.
82 bool IsOuterClassNeeded(const Params& params, const FileDescriptor* file);
83 
84 // Converts the given simple name of a proto entity to its fully-qualified name
85 // in the Java namespace, given that it is in the given file enclosed in the
86 // given parent message (or NULL for file-scope entities). Whether the file's
87 // outer class name should be included in the return value depends on factors
88 // inferrable from the given arguments, including is_class which indicates
89 // whether the entity translates to a Java class.
90 string ToJavaName(const Params& params, const string& name, bool is_class,
91  const Descriptor* parent, const FileDescriptor* file);
92 
93 // These return the fully-qualified class name corresponding to the given
94 // descriptor.
95 inline string ClassName(const Params& params, const Descriptor* descriptor) {
96  return ToJavaName(params, descriptor->name(), true,
97  descriptor->containing_type(), descriptor->file());
98 }
99 string ClassName(const Params& params, const EnumDescriptor* descriptor);
100 inline string ClassName(const Params& params,
101  const ServiceDescriptor* descriptor) {
102  return ToJavaName(params, descriptor->name(), true, NULL, descriptor->file());
103 }
104 inline string ExtensionIdentifierName(const Params& params,
105  const FieldDescriptor* descriptor) {
106  return ToJavaName(params, descriptor->name(), false,
107  descriptor->extension_scope(), descriptor->file());
108 }
109 string ClassName(const Params& params, const FileDescriptor* descriptor);
110 
111 // Get the unqualified name that should be used for a field's field
112 // number constant.
114 
115 string FieldDefaultConstantName(const FieldDescriptor *field);
116 
117 // Print the field's proto-syntax definition as a comment.
118 void PrintFieldComment(io::Printer* printer, const FieldDescriptor* field);
119 
120 enum JavaType {
130 };
131 
133 
134 inline JavaType GetJavaType(const FieldDescriptor* field) {
135  return GetJavaType(field->type());
136 }
137 
139 
140 // Get the fully-qualified class name for a boxed primitive type, e.g.
141 // "java.lang.Integer" for JAVATYPE_INT. Returns NULL for enum and message
142 // types.
143 string BoxedPrimitiveTypeName(JavaType type);
144 
145 string EmptyArrayName(const Params& params, const FieldDescriptor* field);
146 
147 string DefaultValue(const Params& params, const FieldDescriptor* field);
148 
149 
150 // Methods for shared bitfields.
151 
152 // Gets the name of the shared bitfield for the given field index.
153 string GetBitFieldName(int index);
154 
155 // Gets the name of the shared bitfield for the given bit index.
156 // Effectively, GetBitFieldName(bit_index / 32)
157 string GetBitFieldNameForBit(int bit_index);
158 
159 // Generates the java code for the expression that returns whether the bit at
160 // the given bit index is set.
161 // Example: "((bitField1_ & 0x04000000) != 0)"
162 string GenerateGetBit(int bit_index);
163 
164 // Generates the java code for the expression that sets the bit at the given
165 // bit index.
166 // Example: "bitField1_ |= 0x04000000"
167 string GenerateSetBit(int bit_index);
168 
169 // Generates the java code for the expression that clears the bit at the given
170 // bit index.
171 // Example: "bitField1_ = (bitField1_ & ~0x04000000)"
172 string GenerateClearBit(int bit_index);
173 
174 // Generates the java code for the expression that returns whether the bit at
175 // the given bit index contains different values in the current object and
176 // another object accessible via the variable 'other'.
177 // Example: "((bitField1_ & 0x04000000) != (other.bitField1_ & 0x04000000))"
178 string GenerateDifferentBit(int bit_index);
179 
180 // Sets the 'get_*', 'set_*', 'clear_*' and 'different_*' variables, where * is
181 // the given name of the bit, to the appropriate Java expressions for the given
182 // bit index.
183 void SetBitOperationVariables(const string name,
184  int bitIndex, map<string, string>* variables);
185 
186 inline bool IsMapEntry(const Descriptor* descriptor) {
187  // TODO(liujisi): Add an option to turn on maps for proto2 syntax as well.
188  return descriptor->options().map_entry() &&
189  descriptor->file()->syntax() == FileDescriptor::SYNTAX_PROTO3;
190 }
191 
192 bool HasMapField(const Descriptor* descriptor);
193 
194 } // namespace javanano
195 } // namespace compiler
196 } // namespace protobuf
197 
198 } // namespace google
199 #endif // GOOGLE_PROTOBUF_COMPILER_JAVANANO_HELPERS_H__
Definition: upb.c:6604
Definition: descriptor.h:801
string FieldConstantName(const FieldDescriptor *field)
Definition: javanano_helpers.cc:271
const Descriptor * containing_type() const
Definition: printer.h:164
const FieldDescriptor * field
Definition: parser_unittest.cc:2279
string FieldDefaultConstantName(const FieldDescriptor *field)
Definition: javanano_helpers.cc:277
string ToJavaName(const Params &params, const string &name, bool is_class, const Descriptor *parent, const FileDescriptor *file)
Definition: javanano_helpers.cc:233
Definition: protobuf.h:90
Definition: descriptor.h:994
const MessageOptions & options() const
Definition: javanano_helpers.h:128
const FileDescriptor * file() const
const Descriptor * descriptor
Definition: descriptor.cc:271
Definition: method.py:1
string UnderscoresToCapitalizedCamelCase(const FieldDescriptor *field)
Definition: javanano_helpers.cc:149
string GenerateDifferentBit(int bit_index)
Definition: javanano_helpers.cc:559
string FileClassName(const Params &params, const FileDescriptor *file)
Definition: javanano_helpers.cc:177
const Descriptor * extension_scope() const
Definition: descriptor.h:1156
Definition: javanano_params.h:48
Definition: descriptor.h:172
void SetBitOperationVariables(const string name, int bitIndex, map< string, string > *variables)
Definition: javanano_helpers.cc:569
USVString filename
Definition: ErrorEvent.idl:46
string BoxedPrimitiveTypeName(JavaType type)
Definition: javanano_helpers.cc:363
const char kThinSeparator[]
Definition: javanano_helpers.cc:52
bool IsOuterClassNeeded(const Params &params, const FileDescriptor *file)
Definition: javanano_helpers.cc:213
Definition: descriptor.h:439
VoEFile * file
Definition: voe_cmd_test.cc:59
Definition: protobuf.h:85
string StripProto(const string &filename)
Definition: javanano_helpers.cc:169
Type
Definition: descriptor.h:443
GLuint index
Definition: gl2.h:383
string GetBitFieldNameForBit(int bit_index)
Definition: javanano_helpers.cc:528
string RenameJavaKeywords(const string &input)
Definition: javanano_helpers.cc:165
const char kThickSeparator[]
Definition: javanano_helpers.cc:50
EGLImageKHR EGLint * name
Definition: eglext.h:851
string UnderscoresToCamelCase(const FieldDescriptor *field)
Definition: javanano_helpers.cc:145
Definition: __init__.py:1
string GenerateGetBit(int bit_index)
Definition: javanano_helpers.cc:532
string GetBitFieldName(int index)
Definition: javanano_helpers.cc:521
Definition: javanano_helpers.h:121
WireFormatLite::FieldType field_type(FieldType type)
Definition: extension_set_heavy.cc:133
EGLenum type
Definition: eglext.h:63
bool IsMapEntry(const Descriptor *descriptor)
Definition: javanano_helpers.h:186
bool HasMapField(const Descriptor *descriptor)
Definition: javanano_helpers.cc:577
string GenerateClearBit(int bit_index)
Definition: javanano_helpers.cc:550
#define NULL
Definition: common_types.h:41
Syntax syntax() const
Definition: descriptor.h:1899
string ClassName(const Params &params, const FileDescriptor *descriptor)
Definition: javanano_helpers.cc:248
string PrimitiveTypeName(JavaType type)
Definition: javanano_helpers.cc:343
bool map_entry() const
Definition: descriptor.pb.h:6368
const string & name() const
JavaType GetJavaType(FieldDescriptor::Type field_type)
Definition: javanano_helpers.cc:297
Definition: javanano_helpers.h:122
Definition: gflags_completions.h:115
string input
Definition: tokenizer_unittest.cc:198
string GenerateSetBit(int bit_index)
Definition: javanano_helpers.cc:541
JavaType
Definition: javanano_helpers.h:120
string DefaultValue(const Params &params, const FieldDescriptor *field)
Definition: javanano_helpers.cc:403
void PrintFieldComment(io::Printer *printer, const FieldDescriptor *field)
Definition: javanano_helpers.cc:281
string ExtensionIdentifierName(const Params &params, const FieldDescriptor *descriptor)
Definition: javanano_helpers.h:104
GLenum GLint * params
Definition: gl2.h:440
string FileJavaPackage(const Params &params, const FileDescriptor *file)
Definition: javanano_helpers.cc:194
string EmptyArrayName(const Params &params, const FieldDescriptor *field)
Definition: javanano_helpers.cc:383
Definition: protobuf.h:70
const string & name() const
const FileDescriptor * file() const
const FileDescriptor * file() const