Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:00

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2023 Google LLC.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 #ifndef GOOGLE_PROTOBUF_COMPILER_RUST_NAMING_H__
0009 #define GOOGLE_PROTOBUF_COMPILER_RUST_NAMING_H__
0010 
0011 #include <array>
0012 #include <string>
0013 
0014 #include "absl/strings/string_view.h"
0015 #include "google/protobuf/compiler/rust/context.h"
0016 #include "google/protobuf/descriptor.h"
0017 #include "google/protobuf/descriptor.pb.h"
0018 
0019 namespace google {
0020 namespace protobuf {
0021 namespace compiler {
0022 namespace rust {
0023 std::string GetCrateName(Context& ctx, const FileDescriptor& dep);
0024 
0025 std::string GetRsFile(Context& ctx, const FileDescriptor& file);
0026 std::string GetThunkCcFile(Context& ctx, const FileDescriptor& file);
0027 std::string GetHeaderFile(Context& ctx, const FileDescriptor& file);
0028 
0029 std::string ThunkName(Context& ctx, const FieldDescriptor& field,
0030                       absl::string_view op);
0031 std::string ThunkName(Context& ctx, const OneofDescriptor& field,
0032                       absl::string_view op);
0033 
0034 std::string ThunkName(Context& ctx, const Descriptor& msg,
0035                       absl::string_view op);
0036 std::string RawMapThunk(Context& ctx, const Descriptor& msg,
0037                         absl::string_view key_t, absl::string_view op);
0038 std::string RawMapThunk(Context& ctx, const EnumDescriptor& desc,
0039                         absl::string_view key_t, absl::string_view op);
0040 
0041 // Returns an absolute path to the Proxied Rust type of the given field.
0042 // The absolute path is guaranteed to work in the crate that defines the field.
0043 // It may be crate-relative, or directly reference the owning crate of the type.
0044 std::string RsTypePath(Context& ctx, const FieldDescriptor& field);
0045 
0046 std::string EnumRsName(const EnumDescriptor& desc);
0047 std::string EnumValueRsName(const EnumValueDescriptor& value);
0048 
0049 std::string OneofViewEnumRsName(const OneofDescriptor& oneof);
0050 std::string OneofCaseEnumRsName(const OneofDescriptor& oneof);
0051 std::string OneofCaseRsName(const FieldDescriptor& oneof_field);
0052 
0053 std::string FieldInfoComment(Context& ctx, const FieldDescriptor& field);
0054 
0055 // Return how to name a field with 'collision avoidance'. This adds a suffix
0056 // of the field number to the field name if it appears that it will collide with
0057 // another field's non-getter accessor.
0058 //
0059 // For example, for the message:
0060 // message M { bool set_x = 1; int32 x = 2; string x_mut = 8; }
0061 // All accessors for the field `set_x` will be constructed as though the field
0062 // was instead named `set_x_1`, and all accessors for `x_mut` will be as though
0063 // the field was instead named `x_mut_8`.
0064 //
0065 // This is a best-effort heuristic to avoid realistic accidental
0066 // collisions. It is still possible to create a message definition that will
0067 // have a collision, and it may rename a field even if there's no collision (as
0068 // in the case of x_mut in the example).
0069 //
0070 // Note the returned name may still be a rust keyword: RsSafeName() should
0071 // additionally be used if there is no prefix/suffix being appended to the name.
0072 std::string FieldNameWithCollisionAvoidance(const FieldDescriptor& field);
0073 
0074 // Returns how to 'spell' the provided name in Rust, which is the provided name
0075 // verbatim unless it is a Rust keyword that isn't a legal symbol name.
0076 std::string RsSafeName(absl::string_view name);
0077 
0078 // Constructs a string of the Rust modules which will contain the message.
0079 //
0080 // Example: Given a message 'NestedMessage' which is defined in package 'x.y'
0081 // which is inside 'ParentMessage', the message will be placed in the
0082 // x::y::ParentMessage_ Rust module, so this function will return the string
0083 // "x::y::ParentMessage_::".
0084 //
0085 // If the message has no package and no containing messages then this returns
0086 // empty string.
0087 std::string RustModuleForContainingType(Context& ctx,
0088                                         const Descriptor* containing_type);
0089 std::string RustModule(Context& ctx, const Descriptor& msg);
0090 std::string RustModule(Context& ctx, const EnumDescriptor& enum_);
0091 std::string RustModule(Context& ctx, const OneofDescriptor& oneof);
0092 std::string RustInternalModuleName(Context& ctx, const FileDescriptor& file);
0093 
0094 std::string GetCrateRelativeQualifiedPath(Context& ctx, const Descriptor& msg);
0095 std::string GetCrateRelativeQualifiedPath(Context& ctx,
0096                                           const EnumDescriptor& enum_);
0097 std::string GetCrateRelativeQualifiedPath(Context& ctx,
0098                                           const OneofDescriptor& oneof);
0099 
0100 template <typename Desc>
0101 std::string GetUnderscoreDelimitedFullName(Context& ctx, const Desc& desc);
0102 
0103 std::string UnderscoreDelimitFullName(Context& ctx,
0104                                       absl::string_view full_name);
0105 
0106 // TODO: Unify these with other case-conversion functions.
0107 
0108 // Converts an UpperCamel or lowerCamel string to a snake_case string.
0109 std::string CamelToSnakeCase(absl::string_view input);
0110 
0111 // Converts a snake_case string to an UpperCamelCase string.
0112 std::string SnakeToUpperCamelCase(absl::string_view input);
0113 
0114 // Converts a SCREAMING_SNAKE_CASE string to an UpperCamelCase string.
0115 std::string ScreamingSnakeToUpperCamelCase(absl::string_view input);
0116 
0117 // Given a fixed prefix, this will repeatedly strip provided
0118 // string_views if they start with the prefix, the prefix in UpperCamel, or
0119 // the prefix in snake_case.
0120 class MultiCasePrefixStripper final {
0121  public:
0122   explicit MultiCasePrefixStripper(absl::string_view prefix);
0123 
0124   // Strip a prefix from the name in UpperCamel or snake_case, if present.
0125   // If there is an underscore after the prefix, that will also be stripped.
0126   // The stripping is case-insensitive.
0127   absl::string_view StripPrefix(absl::string_view name) const;
0128 
0129  private:
0130   std::array<std::string, 3> prefixes_;
0131 };
0132 
0133 // More efficient overload if a stripper is already constructed.
0134 std::string EnumValueRsName(const MultiCasePrefixStripper& stripper,
0135                             absl::string_view value_name);
0136 
0137 // Describes the names and conversions for a supported map key type.
0138 struct MapKeyType {
0139   // Identifier used in thunk name.
0140   absl::string_view thunk_ident;
0141 
0142   // Rust key typename (K in Map<K, V>, so e.g. `[u8]` for bytes).
0143   // This field may have an unexpanded `$pb$` variable.
0144   absl::string_view rs_key_t;
0145 
0146   // Rust key typename used by thunks for FFI (e.g. `PtrAndLen` for bytes).
0147   // This field may have an unexpanded `$pbi$` variable.
0148   absl::string_view rs_ffi_key_t;
0149 
0150   // Rust expression converting `key: rs_key_t` into an `rs_ffi_key_t`.
0151   absl::string_view rs_to_ffi_key_expr;
0152 
0153   // Rust expression converting `ffi_key: rs_ffi_key_t` into an `rs_key_t`.
0154   // This field may have an unexpanded `$pb$` variable.
0155   absl::string_view rs_from_ffi_key_expr;
0156 
0157   // C++ key typename (K in Map<K, V>, so e.g. `std::string` for bytes).
0158   absl::string_view cc_key_t;
0159 
0160   // C++ key typename used by thunks for FFI (e.g. `PtrAndLen` for bytes).
0161   absl::string_view cc_ffi_key_t;
0162 
0163   // C++ expression converting `cc_ffi_key_t key` into a `cc_key_t`.
0164   absl::string_view cc_from_ffi_key_expr;
0165 
0166   // C++ expression converting `cc_key_t cpp_key` into a `cc_ffi_key_t`.
0167   absl::string_view cc_to_ffi_key_expr;
0168 };
0169 
0170 extern const MapKeyType kMapKeyTypes[6];
0171 
0172 }  // namespace rust
0173 }  // namespace compiler
0174 }  // namespace protobuf
0175 }  // namespace google
0176 
0177 #endif  // GOOGLE_PROTOBUF_COMPILER_RUST_NAMING_H__