Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:11:59

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  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_OBJECTIVEC_FIELD_H__
0009 #define GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__
0010 
0011 #include <memory>
0012 #include <string>
0013 #include <vector>
0014 
0015 #include "absl/container/btree_set.h"
0016 #include "absl/container/flat_hash_map.h"
0017 #include "absl/container/flat_hash_set.h"
0018 #include "absl/strings/match.h"
0019 #include "absl/strings/string_view.h"
0020 #include "google/protobuf/compiler/objectivec/options.h"
0021 #include "google/protobuf/descriptor.h"
0022 #include "google/protobuf/io/printer.h"
0023 
0024 namespace google {
0025 namespace protobuf {
0026 namespace compiler {
0027 namespace objectivec {
0028 
0029 class FieldGenerator {
0030  public:
0031   static FieldGenerator* Make(const FieldDescriptor* field,
0032                               const GenerationOptions& generation_options);
0033 
0034   virtual ~FieldGenerator() = default;
0035 
0036   FieldGenerator(const FieldGenerator&) = delete;
0037   FieldGenerator& operator=(const FieldGenerator&) = delete;
0038 
0039   // Exposed for subclasses to fill in.
0040   virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const = 0;
0041   virtual void GeneratePropertyDeclaration(io::Printer* printer) const = 0;
0042   virtual void GeneratePropertyImplementation(io::Printer* printer) const = 0;
0043 
0044   // Called by GenerateFieldDescription, exposed for classes that need custom
0045   // generation.
0046 
0047   // Exposed for subclasses to extend, base does nothing.
0048   virtual void GenerateCFunctionDeclarations(io::Printer* printer) const;
0049   virtual void GenerateCFunctionImplementations(io::Printer* printer) const;
0050 
0051   // Exposed for subclasses, should always call it on the parent class also.
0052   virtual void DetermineForwardDeclarations(
0053       absl::btree_set<std::string>* fwd_decls,
0054       bool include_external_types) const;
0055   virtual void DetermineObjectiveCClassDefinitions(
0056       absl::btree_set<std::string>* fwd_decls) const;
0057   virtual void DetermineNeededFiles(
0058       absl::flat_hash_set<const FileDescriptor*>* deps) const;
0059 
0060   // Used during generation, not intended to be extended by subclasses.
0061   void GenerateFieldDescription(io::Printer* printer,
0062                                 bool include_default) const;
0063   void GenerateFieldNumberConstant(io::Printer* printer) const;
0064 
0065   // Exposed to get and set the has bits information.
0066   virtual bool RuntimeUsesHasBit() const = 0;
0067   void SetRuntimeHasBit(int has_index);
0068   void SetNoHasBit();
0069   virtual int ExtraRuntimeHasBitsNeeded() const;
0070   virtual void SetExtraRuntimeHasBitsBase(int index_base);
0071   void SetOneofIndexBase(int index_base);
0072 
0073   std::string variable(const char* key) const {
0074     return variables_.find(key)->second;
0075   }
0076 
0077   bool needs_textformat_name_support() const {
0078     const std::string& field_flags = variable("fieldflags");
0079     return absl::StrContains(field_flags, "GPBFieldTextFormatNameCustom");
0080   }
0081   std::string generated_objc_name() const { return variable("name"); }
0082   std::string raw_field_name() const { return variable("raw_field_name"); }
0083 
0084  protected:
0085   FieldGenerator(const FieldDescriptor* descriptor,
0086                  const GenerationOptions& generation_options);
0087 
0088   bool WantsHasProperty() const;
0089 
0090   const FieldDescriptor* descriptor_;
0091   const GenerationOptions& generation_options_;
0092   absl::flat_hash_map<absl::string_view, std::string> variables_;
0093 };
0094 
0095 class SingleFieldGenerator : public FieldGenerator {
0096  public:
0097   ~SingleFieldGenerator() override = default;
0098 
0099   SingleFieldGenerator(const SingleFieldGenerator&) = delete;
0100   SingleFieldGenerator& operator=(const SingleFieldGenerator&) = delete;
0101 
0102   void GenerateFieldStorageDeclaration(io::Printer* printer) const override;
0103   void GeneratePropertyDeclaration(io::Printer* printer) const override;
0104 
0105   void GeneratePropertyImplementation(io::Printer* printer) const override;
0106 
0107   bool RuntimeUsesHasBit() const override;
0108 
0109  protected:
0110   SingleFieldGenerator(const FieldDescriptor* descriptor,
0111                        const GenerationOptions& generation_options);
0112 };
0113 
0114 // Subclass with common support for when the field ends up as an ObjC Object.
0115 class ObjCObjFieldGenerator : public SingleFieldGenerator {
0116  public:
0117   ~ObjCObjFieldGenerator() override = default;
0118 
0119   ObjCObjFieldGenerator(const ObjCObjFieldGenerator&) = delete;
0120   ObjCObjFieldGenerator& operator=(const ObjCObjFieldGenerator&) = delete;
0121 
0122   void GenerateFieldStorageDeclaration(io::Printer* printer) const override;
0123   void GeneratePropertyDeclaration(io::Printer* printer) const override;
0124 
0125  protected:
0126   ObjCObjFieldGenerator(const FieldDescriptor* descriptor,
0127                         const GenerationOptions& generation_options);
0128 };
0129 
0130 class RepeatedFieldGenerator : public ObjCObjFieldGenerator {
0131  public:
0132   ~RepeatedFieldGenerator() override = default;
0133 
0134   RepeatedFieldGenerator(const RepeatedFieldGenerator&) = delete;
0135   RepeatedFieldGenerator& operator=(const RepeatedFieldGenerator&) = delete;
0136 
0137   void GenerateFieldStorageDeclaration(io::Printer* printer) const override;
0138   void GeneratePropertyDeclaration(io::Printer* printer) const override;
0139 
0140   void GeneratePropertyImplementation(io::Printer* printer) const override;
0141 
0142   bool RuntimeUsesHasBit() const override;
0143 
0144   virtual void EmitArrayComment(io::Printer* printer) const;
0145 
0146  protected:
0147   RepeatedFieldGenerator(const FieldDescriptor* descriptor,
0148                          const GenerationOptions& generation_options);
0149 };
0150 
0151 // Convenience class which constructs FieldGenerators for a Descriptor.
0152 class FieldGeneratorMap {
0153  public:
0154   FieldGeneratorMap(const Descriptor* descriptor,
0155                     const GenerationOptions& generation_options);
0156   ~FieldGeneratorMap() = default;
0157 
0158   FieldGeneratorMap(const FieldGeneratorMap&) = delete;
0159   FieldGeneratorMap& operator=(const FieldGeneratorMap&) = delete;
0160 
0161   const FieldGenerator& get(const FieldDescriptor* field) const;
0162 
0163   // Assigns the has bits and returns the number of bits needed.
0164   int CalculateHasBits();
0165 
0166   void SetOneofIndexBase(int index_base);
0167 
0168   // Check if any field of this message has a non zero default.
0169   bool DoesAnyFieldHaveNonZeroDefault() const;
0170 
0171  private:
0172   const Descriptor* descriptor_;
0173   std::vector<std::unique_ptr<FieldGenerator>> field_generators_;
0174 };
0175 
0176 }  // namespace objectivec
0177 }  // namespace compiler
0178 }  // namespace protobuf
0179 }  // namespace google
0180 
0181 #endif  // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__