#include <assert.h>
#include "generator/well_known_types_embed.h"
#include <google/protobuf/compiler/scc.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/io/zero_copy_stream.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/logging.h>
#include <google/protobuf/stubs/stringprintf.h>
#include <google/protobuf/stubs/strutil.h>
#include <algorithm>
#include <limits>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "generator/js_generator.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace js {
const char* kKeyword[] = …;
static const int kNumKeyword = …;
namespace {
enum BytesMode { … };
bool IsReserved(const std::string& ident) { … }
std::string GetSnakeFilename(const std::string& filename) { … }
std::string GetJSFilename(const GeneratorOptions& options,
const std::string& filename) { … }
std::string GetRootPath(const std::string& from_filename,
const std::string& to_filename) { … }
std::string ModuleAlias(const std::string& filename) { … }
std::string GetNamespace(const GeneratorOptions& options,
const FileDescriptor* file) { … }
std::string GetNestedMessageName(const Descriptor* descriptor) { … }
std::string GetPrefix(const GeneratorOptions& options,
const FileDescriptor* file_descriptor,
const Descriptor* containing_type) { … }
std::string GetMessagePathPrefix(const GeneratorOptions& options,
const Descriptor* descriptor) { … }
std::string GetMessagePath(const GeneratorOptions& options,
const Descriptor* descriptor) { … }
std::string GetEnumPathPrefix(const GeneratorOptions& options,
const EnumDescriptor* enum_descriptor) { … }
std::string GetEnumPath(const GeneratorOptions& options,
const EnumDescriptor* enum_descriptor) { … }
std::string MaybeCrossFileRef(const GeneratorOptions& options,
const FileDescriptor* from_file,
const Descriptor* to_message) { … }
std::string SubmessageTypeRef(const GeneratorOptions& options,
const FieldDescriptor* field) { … }
char ToLowerASCII(char c) { … }
std::vector<std::string> ParseLowerUnderscore(const std::string& input) { … }
std::vector<std::string> ParseUpperCamel(const std::string& input) { … }
std::string ToLowerCamel(const std::vector<std::string>& words) { … }
std::string ToUpperCamel(const std::vector<std::string>& words) { … }
std::string ToEnumCase(const std::string& input) { … }
std::string ToLower(const std::string& input) { … }
std::string GetExtensionFileName(const GeneratorOptions& options,
const FileDescriptor* file,
bool with_filename) { … }
std::string GetMessagesFileName(const GeneratorOptions& options, const SCC* scc,
bool with_package) { … }
std::string GetEnumFileName(const GeneratorOptions& options,
const EnumDescriptor* desc, bool with_package) { … }
std::string GetMessageId(const Descriptor* desc) { … }
bool IgnoreExtensionField(const FieldDescriptor* field) { … }
bool IsResponse(const Descriptor* desc) { … }
bool IgnoreField(const FieldDescriptor* field) { … }
bool IgnoreMessage(const Descriptor* d) { … }
bool IgnoreOneof(const OneofDescriptor* oneof) { … }
std::string JSIdent(const GeneratorOptions& options,
const FieldDescriptor* field, bool is_upper_camel,
bool is_map, bool drop_list) { … }
std::string JSObjectFieldName(const GeneratorOptions& options,
const FieldDescriptor* field) { … }
std::string JSByteGetterSuffix(BytesMode bytes_mode) { … }
std::string JSGetterName(const GeneratorOptions& options,
const FieldDescriptor* field,
BytesMode bytes_mode = BYTES_DEFAULT,
bool drop_list = false) { … }
std::string JSOneofName(const OneofDescriptor* oneof) { … }
std::string JSFieldIndex(const FieldDescriptor* field) { … }
std::string JSOneofIndex(const OneofDescriptor* oneof) { … }
uint16_t DecodeUTF8Codepoint(uint8_t* bytes, size_t* length) { … }
bool EscapeJSString(const std::string& in, std::string* out) { … }
std::string EscapeBase64(const std::string& in) { … }
std::string PostProcessFloat(std::string result) { … }
std::string FloatToString(float value) { … }
std::string DoubleToString(double value) { … }
bool InRealOneof(const FieldDescriptor* field) { … }
bool IsIntegralFieldWithStringJSType(const FieldDescriptor* field) { … }
std::string MaybeNumberString(const FieldDescriptor* field,
const std::string& orig) { … }
std::string JSFieldDefault(const FieldDescriptor* field) { … }
std::string ProtoTypeName(const GeneratorOptions& options,
const FieldDescriptor* field) { … }
std::string JSIntegerTypeName(const FieldDescriptor* field) { … }
std::string JSStringTypeName(const GeneratorOptions& options,
const FieldDescriptor* field,
BytesMode bytes_mode) { … }
std::string JSTypeName(const GeneratorOptions& options,
const FieldDescriptor* field, BytesMode bytes_mode) { … }
bool UseBrokenPresenceSemantics(const GeneratorOptions& options,
const FieldDescriptor* field) { … }
bool ReturnsNullWhenUnset(const GeneratorOptions& options,
const FieldDescriptor* field) { … }
bool DeclaredReturnTypeIsNullable(const GeneratorOptions& options,
const FieldDescriptor* field) { … }
bool SetterAcceptsUndefined(const GeneratorOptions& options,
const FieldDescriptor* field) { … }
bool SetterAcceptsNull(const GeneratorOptions& options,
const FieldDescriptor* field) { … }
bool IsPrimitive(const std::string& type) { … }
std::string JSFieldTypeAnnotation(const GeneratorOptions& options,
const FieldDescriptor* field,
bool is_setter_argument, bool force_present,
bool singular_if_not_packed,
BytesMode bytes_mode = BYTES_DEFAULT,
bool force_singular = false) { … }
std::string JSBinaryReaderMethodType(const FieldDescriptor* field) { … }
std::string JSBinaryReadWriteMethodName(const FieldDescriptor* field,
bool is_writer) { … }
std::string JSBinaryReaderMethodName(const GeneratorOptions& options,
const FieldDescriptor* field) { … }
std::string JSBinaryWriterMethodName(const GeneratorOptions& options,
const FieldDescriptor* field) { … }
std::string JSTypeTag(const FieldDescriptor* desc) { … }
bool HasRepeatedFields(const GeneratorOptions& options,
const Descriptor* desc) { … }
static const char* kRepeatedFieldArrayName = …;
std::string RepeatedFieldsArrayName(const GeneratorOptions& options,
const Descriptor* desc) { … }
bool HasOneofFields(const Descriptor* desc) { … }
static const char* kOneofGroupArrayName = …;
std::string OneofFieldsArrayName(const GeneratorOptions& options,
const Descriptor* desc) { … }
std::string RepeatedFieldNumberList(const GeneratorOptions& options,
const Descriptor* desc) { … }
std::string OneofGroupList(const Descriptor* desc) { … }
std::string JSOneofArray(const GeneratorOptions& options,
const FieldDescriptor* field) { … }
std::string RelativeTypeName(const FieldDescriptor* field) { … }
std::string JSExtensionsObjectName(const GeneratorOptions& options,
const FileDescriptor* from_file,
const Descriptor* desc) { … }
static const int kMapKeyField = …;
static const int kMapValueField = …;
const FieldDescriptor* MapFieldKey(const FieldDescriptor* field) { … }
const FieldDescriptor* MapFieldValue(const FieldDescriptor* field) { … }
std::string FieldDefinition(const GeneratorOptions& options,
const FieldDescriptor* field) { … }
std::string FieldComments(const FieldDescriptor* field, BytesMode bytes_mode) { … }
bool ShouldGenerateExtension(const FieldDescriptor* field) { … }
bool HasExtensions(const Descriptor* desc) { … }
bool HasExtensions(const FileDescriptor* file) { … }
bool HasMap(const GeneratorOptions& options, const Descriptor* desc) { … }
bool FileHasMap(const GeneratorOptions& options, const FileDescriptor* desc) { … }
bool IsExtendable(const Descriptor* desc) { … }
std::string GetPivot(const Descriptor* desc) { … }
bool HasFieldPresence(const GeneratorOptions& options,
const FieldDescriptor* field) { … }
class FileDeduplicator { … };
void DepthFirstSearch(const FileDescriptor* file,
std::vector<const FileDescriptor*>* list,
std::set<const FileDescriptor*>* seen) { … }
class NotInSet { … };
void GenerateJspbFileOrder(const std::vector<const FileDescriptor*>& input,
std::vector<const FileDescriptor*>* ordered) { … }
struct DepsGenerator { … };
bool GenerateJspbAllowedMap(const GeneratorOptions& options,
const std::vector<const FileDescriptor*>& files,
std::map<const void*, std::string>* allowed_set,
SCCAnalyzer<DepsGenerator>* analyzer) { … }
void EmbedCodeAnnotations(const GeneratedCodeInfo& annotations,
io::Printer* printer) { … }
bool IsWellKnownTypeFile(const FileDescriptor* file) { … }
}
void Generator::GenerateHeader(const GeneratorOptions& options,
const FileDescriptor* file,
io::Printer* printer) const { … }
void Generator::FindProvidesForFile(const GeneratorOptions& options,
io::Printer* printer,
const FileDescriptor* file,
std::set<std::string>* provided) const { … }
void Generator::FindProvides(const GeneratorOptions& options,
io::Printer* printer,
const std::vector<const FileDescriptor*>& files,
std::set<std::string>* provided) const { … }
void FindProvidesForOneOfEnum(const GeneratorOptions& options,
const OneofDescriptor* oneof,
std::set<std::string>* provided) { … }
void FindProvidesForOneOfEnums(const GeneratorOptions& options,
io::Printer* printer, const Descriptor* desc,
std::set<std::string>* provided) { … }
void Generator::FindProvidesForMessage(const GeneratorOptions& options,
io::Printer* printer,
const Descriptor* desc,
std::set<std::string>* provided) const { … }
void Generator::FindProvidesForEnum(const GeneratorOptions& options,
io::Printer* printer,
const EnumDescriptor* enumdesc,
std::set<std::string>* provided) const { … }
void Generator::FindProvidesForFields(
const GeneratorOptions& options, io::Printer* printer,
const std::vector<const FieldDescriptor*>& fields,
std::set<std::string>* provided) const { … }
void Generator::GenerateProvides(const GeneratorOptions& options,
io::Printer* printer,
std::set<std::string>* provided) const { … }
void Generator::GenerateRequiresForSCC(const GeneratorOptions& options,
io::Printer* printer, const SCC* scc,
std::set<std::string>* provided) const { … }
void Generator::GenerateRequiresForLibrary(
const GeneratorOptions& options, io::Printer* printer,
const std::vector<const FileDescriptor*>& files,
std::set<std::string>* provided) const { … }
void Generator::GenerateRequiresForExtensions(
const GeneratorOptions& options, io::Printer* printer,
const std::vector<const FieldDescriptor*>& fields,
std::set<std::string>* provided) const { … }
void Generator::GenerateRequiresImpl(const GeneratorOptions& options,
io::Printer* printer,
std::set<std::string>* required,
std::set<std::string>* forwards,
std::set<std::string>* provided,
bool require_jspb, bool require_extension,
bool require_map) const { … }
bool NamespaceOnly(const Descriptor* desc) { … }
void Generator::FindRequiresForMessage(const GeneratorOptions& options,
const Descriptor* desc,
std::set<std::string>* required,
std::set<std::string>* forwards,
bool* have_message) const { … }
void Generator::FindRequiresForField(const GeneratorOptions& options,
const FieldDescriptor* field,
std::set<std::string>* required,
std::set<std::string>* forwards) const { … }
void Generator::FindRequiresForExtension(
const GeneratorOptions& options, const FieldDescriptor* field,
std::set<std::string>* required, std::set<std::string>* forwards) const { … }
void Generator::GenerateTestOnly(const GeneratorOptions& options,
io::Printer* printer) const { … }
void Generator::GenerateClassesAndEnums(const GeneratorOptions& options,
io::Printer* printer,
const FileDescriptor* file) const { … }
void Generator::GenerateClass(const GeneratorOptions& options,
io::Printer* printer,
const Descriptor* desc) const { … }
void Generator::GenerateClassConstructor(const GeneratorOptions& options,
io::Printer* printer,
const Descriptor* desc) const { … }
void Generator::GenerateClassConstructorAndDeclareExtensionFieldInfo(
const GeneratorOptions& options, io::Printer* printer,
const Descriptor* desc) const { … }
void Generator::GenerateClassFieldInfo(const GeneratorOptions& options,
io::Printer* printer,
const Descriptor* desc) const { … }
void Generator::GenerateClassXid(const GeneratorOptions& options,
io::Printer* printer,
const Descriptor* desc) const { … }
void Generator::GenerateOneofCaseDefinition(
const GeneratorOptions& options, io::Printer* printer,
const OneofDescriptor* oneof) const { … }
void Generator::GenerateClassToObject(const GeneratorOptions& options,
io::Printer* printer,
const Descriptor* desc) const { … }
void Generator::GenerateFieldValueExpression(io::Printer* printer,
const char* obj_reference,
const FieldDescriptor* field,
bool use_default) const { … }
void Generator::GenerateClassFieldToObject(const GeneratorOptions& options,
io::Printer* printer,
const FieldDescriptor* field) const { … }
void Generator::GenerateObjectTypedef(const GeneratorOptions& options,
io::Printer* printer,
const Descriptor* desc) const { … }
void Generator::GenerateClassFromObject(const GeneratorOptions& options,
io::Printer* printer,
const Descriptor* desc) const { … }
void Generator::GenerateClassFieldFromObject(
const GeneratorOptions& options, io::Printer* printer,
const FieldDescriptor* field) const { … }
void Generator::GenerateClassRegistration(const GeneratorOptions& options,
io::Printer* printer,
const Descriptor* desc) const { … }
void Generator::GenerateClassFields(const GeneratorOptions& options,
io::Printer* printer,
const Descriptor* desc) const { … }
void GenerateBytesWrapper(const GeneratorOptions& options, io::Printer* printer,
const FieldDescriptor* field, BytesMode bytes_mode) { … }
void Generator::GenerateClassField(const GeneratorOptions& options,
io::Printer* printer,
const FieldDescriptor* field) const { … }
void Generator::GenerateRepeatedPrimitiveHelperMethods(
const GeneratorOptions& options, io::Printer* printer,
const FieldDescriptor* field, bool untyped) const { … }
void Generator::GenerateRepeatedMessageHelperMethods(
const GeneratorOptions& options, io::Printer* printer,
const FieldDescriptor* field) const { … }
void Generator::GenerateClassExtensionFieldInfo(const GeneratorOptions& options,
io::Printer* printer,
const Descriptor* desc) const { … }
void Generator::GenerateClassDeserializeBinary(const GeneratorOptions& options,
io::Printer* printer,
const Descriptor* desc) const { … }
void Generator::GenerateClassDeserializeBinaryField(
const GeneratorOptions& options, io::Printer* printer,
const FieldDescriptor* field) const { … }
void Generator::GenerateClassSerializeBinary(const GeneratorOptions& options,
io::Printer* printer,
const Descriptor* desc) const { … }
void Generator::GenerateClassSerializeBinaryField(
const GeneratorOptions& options, io::Printer* printer,
const FieldDescriptor* field) const { … }
void Generator::GenerateEnum(const GeneratorOptions& options,
io::Printer* printer,
const EnumDescriptor* enumdesc) const { … }
void Generator::GenerateExtension(const GeneratorOptions& options,
io::Printer* printer,
const FieldDescriptor* field) const { … }
bool GeneratorOptions::ParseFromOptions(
const std::vector<std::pair<std::string, std::string> >& options,
std::string* error) { … }
GeneratorOptions::OutputMode GeneratorOptions::output_mode() const { … }
void Generator::GenerateFilesInDepOrder(
const GeneratorOptions& options, io::Printer* printer,
const std::vector<const FileDescriptor*>& files) const { … }
void Generator::GenerateFileAndDeps(
const GeneratorOptions& options, io::Printer* printer,
const FileDescriptor* root, std::set<const FileDescriptor*>* all_files,
std::set<const FileDescriptor*>* generated) const { … }
bool Generator::GenerateFile(const FileDescriptor* file,
const GeneratorOptions& options,
GeneratorContext* context,
bool use_short_name) const { … }
void Generator::GenerateFile(const GeneratorOptions& options,
io::Printer* printer,
const FileDescriptor* file) const { … }
bool Generator::GenerateAll(const std::vector<const FileDescriptor*>& files,
const std::string& parameter,
GeneratorContext* context,
std::string* error) const { … }
}
}
}
}