164d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// Protocol Buffers - Google's data interchange format 264d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// Copyright 2008 Google Inc. All rights reserved. 364d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// http://code.google.com/p/protobuf/ 464d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// 564d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// Redistribution and use in source and binary forms, with or without 664d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// modification, are permitted provided that the following conditions are 764d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// met: 864d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// 964d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// * Redistributions of source code must retain the above copyright 1064d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// notice, this list of conditions and the following disclaimer. 1164d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// * Redistributions in binary form must reproduce the above 1264d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// copyright notice, this list of conditions and the following disclaimer 1364d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// in the documentation and/or other materials provided with the 1464d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// distribution. 1564d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// * Neither the name of Google Inc. nor the names of its 1664d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// contributors may be used to endorse or promote products derived from 1764d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// this software without specific prior written permission. 1864d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// 1964d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2064d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2164d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2264d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2364d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2464d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2564d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2664d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2764d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2864d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2964d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3064d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 3164d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// Author: kenton@google.com (Kenton Varda) 3264d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// Based on original Protocol Buffers design by 3364d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// Sanjay Ghemawat, Jeff Dean, and others. 3464d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 3564d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ 3664d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci#define GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ 3764d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 3864d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci#include <string> 3964d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci#include <google/protobuf/compiler/javanano/javanano_params.h> 4064d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci#include <google/protobuf/descriptor.pb.h> 4164d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci#include <google/protobuf/descriptor.h> 42382ddccb550e1c822ef26a0e65988998f7446624Max Cai#include <google/protobuf/io/printer.h> 4364d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 4464d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazcinamespace google { 4564d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazcinamespace protobuf { 4664d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazcinamespace compiler { 4764d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazcinamespace javanano { 4864d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 4964d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// Commonly-used separator comments. Thick is a line of '=', thin is a line 5064d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// of '-'. 5164d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazciextern const char kThickSeparator[]; 5264d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazciextern const char kThinSeparator[]; 5364d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 5464d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// Converts the field's name to camel-case, e.g. "foo_bar_baz" becomes 5564d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// "fooBarBaz" or "FooBarBaz", respectively. 5664d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazcistring UnderscoresToCamelCase(const FieldDescriptor* field); 5764d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazcistring UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field); 5864d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 59f4e01452f159ae6b53f5edd25fa647ca2919ae10Tom Chao// Appends an "_" to the end of a field where the name is a reserved java 60f4e01452f159ae6b53f5edd25fa647ca2919ae10Tom Chao// keyword. For example int32 public = 1 will generate int public_. 61f4e01452f159ae6b53f5edd25fa647ca2919ae10Tom Chaostring RenameJavaKeywords(const string& input); 62f4e01452f159ae6b53f5edd25fa647ca2919ae10Tom Chao 6364d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// Similar, but for method names. (Typically, this merely has the effect 6464d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// of lower-casing the first letter of the name.) 6564d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazcistring UnderscoresToCamelCase(const MethodDescriptor* method); 6664d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 6764d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// Strips ".proto" or ".protodevel" from the end of a filename. 6864d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazcistring StripProto(const string& filename); 6964d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 7064d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// Gets the unqualified class name for the file. Each .proto file becomes a 7164d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// single Java class, with all its contents nested in that class. 7264d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazcistring FileClassName(const Params& params, const FileDescriptor* file); 7364d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 7464d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// Returns the file's Java package name. 7564d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazcistring FileJavaPackage(const Params& params, const FileDescriptor* file); 7664d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 77624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// Returns whether the Java outer class is needed, i.e. whether the option 78624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// java_multiple_files is false, or the proto file contains any file-scope 79624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// enums/extensions. 80624c448fbef20a1a2fad2289f622b468c25763d1Max Caibool IsOuterClassNeeded(const Params& params, const FileDescriptor* file); 81624c448fbef20a1a2fad2289f622b468c25763d1Max Cai 82624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// Converts the given simple name of a proto entity to its fully-qualified name 83624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// in the Java namespace, given that it is in the given file enclosed in the 84624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// given parent message (or NULL for file-scope entities). Whether the file's 85624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// outer class name should be included in the return value depends on factors 86624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// inferrable from the given arguments, including is_class which indicates 87624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// whether the entity translates to a Java class. 88624c448fbef20a1a2fad2289f622b468c25763d1Max Caistring ToJavaName(const Params& params, const string& name, bool is_class, 89624c448fbef20a1a2fad2289f622b468c25763d1Max Cai const Descriptor* parent, const FileDescriptor* file); 9064d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 9164d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// These return the fully-qualified class name corresponding to the given 9264d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// descriptor. 9364d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazciinline string ClassName(const Params& params, const Descriptor* descriptor) { 94624c448fbef20a1a2fad2289f622b468c25763d1Max Cai return ToJavaName(params, descriptor->name(), true, 95624c448fbef20a1a2fad2289f622b468c25763d1Max Cai descriptor->containing_type(), descriptor->file()); 9664d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci} 9764d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazcistring ClassName(const Params& params, const EnumDescriptor* descriptor); 9864d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazciinline string ClassName(const Params& params, 9964d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci const ServiceDescriptor* descriptor) { 100624c448fbef20a1a2fad2289f622b468c25763d1Max Cai return ToJavaName(params, descriptor->name(), true, NULL, descriptor->file()); 10164d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci} 10264d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazciinline string ExtensionIdentifierName(const Params& params, 10364d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci const FieldDescriptor* descriptor) { 104624c448fbef20a1a2fad2289f622b468c25763d1Max Cai return ToJavaName(params, descriptor->name(), false, 105624c448fbef20a1a2fad2289f622b468c25763d1Max Cai descriptor->extension_scope(), descriptor->file()); 10664d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci} 10764d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazcistring ClassName(const Params& params, const FileDescriptor* descriptor); 10864d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 10964d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// Get the unqualified name that should be used for a field's field 11064d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// number constant. 11164d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazcistring FieldConstantName(const FieldDescriptor *field); 11264d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 11364d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazcistring FieldDefaultConstantName(const FieldDescriptor *field); 11464d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 115382ddccb550e1c822ef26a0e65988998f7446624Max Cai// Print the field's proto-syntax definition as a comment. 116382ddccb550e1c822ef26a0e65988998f7446624Max Caivoid PrintFieldComment(io::Printer* printer, const FieldDescriptor* field); 117382ddccb550e1c822ef26a0e65988998f7446624Max Cai 11864d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazcienum JavaType { 11964d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci JAVATYPE_INT, 12064d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci JAVATYPE_LONG, 12164d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci JAVATYPE_FLOAT, 12264d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci JAVATYPE_DOUBLE, 12364d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci JAVATYPE_BOOLEAN, 12464d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci JAVATYPE_STRING, 12564d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci JAVATYPE_BYTES, 12664d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci JAVATYPE_ENUM, 12764d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci JAVATYPE_MESSAGE 12864d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci}; 12964d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 13064d8d8f89050c5ada85341f967af391f4716a7cbUlas KirazciJavaType GetJavaType(FieldDescriptor::Type field_type); 13164d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 13264d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazciinline JavaType GetJavaType(const FieldDescriptor* field) { 13364d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci return GetJavaType(field->type()); 13464d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci} 13564d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 136382ddccb550e1c822ef26a0e65988998f7446624Max Caistring PrimitiveTypeName(JavaType type); 137382ddccb550e1c822ef26a0e65988998f7446624Max Cai 13864d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// Get the fully-qualified class name for a boxed primitive type, e.g. 13964d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// "java.lang.Integer" for JAVATYPE_INT. Returns NULL for enum and message 14064d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci// types. 141382ddccb550e1c822ef26a0e65988998f7446624Max Caistring BoxedPrimitiveTypeName(JavaType type); 14264d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 14364d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazcistring EmptyArrayName(const Params& params, const FieldDescriptor* field); 14464d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 14564d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazcistring DefaultValue(const Params& params, const FieldDescriptor* field); 14664d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 14747dee56155c7bdb9855e51ff08c99db306d11a2dMax Cai 14847dee56155c7bdb9855e51ff08c99db306d11a2dMax Cai// Methods for shared bitfields. 14947dee56155c7bdb9855e51ff08c99db306d11a2dMax Cai 150ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff// Gets the name of the shared bitfield for the given field index. 15147dee56155c7bdb9855e51ff08c99db306d11a2dMax Caistring GetBitFieldName(int index); 15247dee56155c7bdb9855e51ff08c99db306d11a2dMax Cai 15347dee56155c7bdb9855e51ff08c99db306d11a2dMax Cai// Gets the name of the shared bitfield for the given bit index. 15447dee56155c7bdb9855e51ff08c99db306d11a2dMax Cai// Effectively, GetBitFieldName(bit_index / 32) 15547dee56155c7bdb9855e51ff08c99db306d11a2dMax Caistring GetBitFieldNameForBit(int bit_index); 15647dee56155c7bdb9855e51ff08c99db306d11a2dMax Cai 157ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff// Generates the java code for the expression that returns whether the bit at 158ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff// the given bit index is set. 159ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff// Example: "((bitField1_ & 0x04000000) != 0)" 16047dee56155c7bdb9855e51ff08c99db306d11a2dMax Caistring GenerateGetBit(int bit_index); 16147dee56155c7bdb9855e51ff08c99db306d11a2dMax Cai 162ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff// Generates the java code for the expression that sets the bit at the given 163ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff// bit index. 164ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff// Example: "bitField1_ |= 0x04000000" 16547dee56155c7bdb9855e51ff08c99db306d11a2dMax Caistring GenerateSetBit(int bit_index); 16647dee56155c7bdb9855e51ff08c99db306d11a2dMax Cai 167ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff// Generates the java code for the expression that clears the bit at the given 168ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff// bit index. 169ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff// Example: "bitField1_ = (bitField1_ & ~0x04000000)" 17047dee56155c7bdb9855e51ff08c99db306d11a2dMax Caistring GenerateClearBit(int bit_index); 17147dee56155c7bdb9855e51ff08c99db306d11a2dMax Cai 172ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff// Generates the java code for the expression that returns whether the bit at 173ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff// the given bit index contains different values in the current object and 174ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff// another object accessible via the variable 'other'. 175ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff// Example: "((bitField1_ & 0x04000000) != (other.bitField1_ & 0x04000000))" 176ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duffstring GenerateDifferentBit(int bit_index); 177ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff 178ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff// Sets the 'get_*', 'set_*', 'clear_*' and 'different_*' variables, where * is 179ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff// the given name of the bit, to the appropriate Java expressions for the given 180ccc48faf20dbf3b3cddcffe78d198876d543529bBrian Duff// bit index. 181e74fe623e115237968a3de1143d7cdb4df710858Max Caivoid SetBitOperationVariables(const string name, 182e74fe623e115237968a3de1143d7cdb4df710858Max Cai int bitIndex, map<string, string>* variables); 183e74fe623e115237968a3de1143d7cdb4df710858Max Cai 18464d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci} // namespace javanano 18564d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci} // namespace compiler 18664d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci} // namespace protobuf 18764d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci 18864d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci} // namespace google 18964d8d8f89050c5ada85341f967af391f4716a7cbUlas Kirazci#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ 190