Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-13 08:23:31

0001 #ifndef GOOGLE_PROTOBUF_INTERNAL_FEATURE_HELPER_H__
0002 #define GOOGLE_PROTOBUF_INTERNAL_FEATURE_HELPER_H__
0003 
0004 #include <cstdint>
0005 
0006 #include "absl/strings/string_view.h"
0007 #include "google/protobuf/descriptor.h"
0008 #include "google/protobuf/descriptor.pb.h"
0009 #include "google/protobuf/extension_set.h"
0010 
0011 // Must be included last.
0012 #include "google/protobuf/port_def.inc"
0013 
0014 namespace google {
0015 namespace protobuf {
0016 namespace internal {
0017 class InternalFeatureHelperTest;
0018 // This class is for internal use only and provides access to the resolved
0019 // runtime FeatureSets of any descriptor.  These features are not designed
0020 // to be stable, and depending directly on them (vs the public descriptor APIs)
0021 // is not safe.
0022 class PROTOBUF_EXPORT InternalFeatureHelper {
0023  public:
0024   template <typename DescriptorT>
0025   static const FeatureSet& GetFeatures(const DescriptorT& desc) {
0026     return desc.features();
0027   }
0028 
0029  private:
0030   friend class ::google::protobuf::compiler::CodeGenerator;
0031   friend class ::google::protobuf::compiler::CommandLineInterface;
0032   friend class ::google::protobuf::internal::InternalFeatureHelperTest;
0033 
0034   static const DescriptorPool& GetDescriptorPool(const FileDescriptor& file) {
0035     return *file.pool();
0036   }
0037 
0038   template <typename DescriptorT>
0039   static const DescriptorPool& GetDescriptorPool(const DescriptorT& desc) {
0040     return GetDescriptorPool(*desc.file());
0041   }
0042 
0043   // Provides a restricted view exclusively to code generators to query their
0044   // own unresolved features.  Unresolved features are virtually meaningless to
0045   // everyone else. Code generators will need them to validate their own
0046   // features, and runtimes may need them internally to be able to properly
0047   // represent the original proto files from generated code.
0048   template <typename DescriptorT, typename TypeTraitsT, uint8_t field_type,
0049             bool is_packed>
0050   static typename TypeTraitsT::ConstType GetUnresolvedFeatures(
0051       const DescriptorT& descriptor,
0052       const google::protobuf::internal::ExtensionIdentifier<
0053           FeatureSet, TypeTraitsT, field_type, is_packed>& extension) {
0054     return descriptor.proto_features_->GetExtension(extension);
0055   }
0056 
0057   // Provides a restricted view exclusively to code generators to query the
0058   // edition of files being processed.  While most people should never write
0059   // edition-dependent code, generators frequently will need to.
0060   static Edition GetEdition(const FileDescriptor& desc) {
0061     return desc.edition();
0062   }
0063 
0064   template <typename DescriptorT>
0065   static Edition GetEdition(const DescriptorT& desc) {
0066     return GetEdition(*desc.file());
0067   }
0068 
0069   // Parses the serialized FeatureSetDefaults and returns the resolved
0070   // FeatureSet for a given edition.
0071   static FeatureSet ParseAndGetEditionResolvedFeatureSet(absl::string_view data,
0072                                                          Edition edition);
0073 
0074   // Gets the resolved FeatureSet extension for a given descriptor.
0075   //
0076   // If the descriptor's pool has already provided the resolved feature default
0077   // for the edition and the language FeatureSet extension, then the default
0078   // will be returned directly. Otherwise, the function will parse the
0079   // serialized FeatureSetDefaults data provided by the language FeatureSet
0080   // extension, and merge it with the original FeatureSet extension so that the
0081   // resolved feature set defaults will always be present.
0082   template <typename DescriptorT, typename ExtType, uint8_t field_type,
0083             bool is_packed>
0084   static auto GetResolvedFeatureExtension(
0085       const DescriptorT& descriptor,
0086       const google::protobuf::internal::ExtensionIdentifier<
0087           FeatureSet, MessageTypeTraits<ExtType>, field_type, is_packed>&
0088           extension) {
0089     auto lang_features = GetFeatures(descriptor).GetExtension(extension);
0090     if (GetDescriptorPool(descriptor).ResolvesFeaturesFor(extension)) {
0091       return lang_features;
0092     }
0093 
0094     auto lang_features_ret =
0095         ParseAndGetEditionResolvedFeatureSet(
0096             ::pb::internal::GetFeatureSetDefaultsData<ExtType>(),
0097             GetEdition(descriptor))
0098             .GetExtension(extension);
0099     lang_features_ret.MergeFrom(lang_features);
0100     return lang_features_ret;
0101   }
0102 };
0103 }  // namespace internal
0104 }  // namespace protobuf
0105 }  // namespace google
0106 
0107 #include "google/protobuf/port_undef.inc"
0108 
0109 #endif  // GOOGLE_PROTOBUF_INTERNAL_FEATURE_HELPER_H__