• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #ifndef GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__
9 #define GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__
10 
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 #include "absl/container/btree_set.h"
16 #include "absl/container/flat_hash_map.h"
17 #include "absl/container/flat_hash_set.h"
18 #include "absl/strings/match.h"
19 #include "absl/strings/string_view.h"
20 #include "google/protobuf/compiler/objectivec/options.h"
21 #include "google/protobuf/descriptor.h"
22 #include "google/protobuf/io/printer.h"
23 
24 namespace google {
25 namespace protobuf {
26 namespace compiler {
27 namespace objectivec {
28 
29 class FieldGenerator {
30  public:
31   static FieldGenerator* Make(const FieldDescriptor* field,
32                               const GenerationOptions& generation_options);
33 
34   virtual ~FieldGenerator() = default;
35 
36   FieldGenerator(const FieldGenerator&) = delete;
37   FieldGenerator& operator=(const FieldGenerator&) = delete;
38 
39   // Exposed for subclasses to fill in.
40   virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const = 0;
41   virtual void GeneratePropertyDeclaration(io::Printer* printer) const = 0;
42   virtual void GeneratePropertyImplementation(io::Printer* printer) const = 0;
43 
44   // Called by GenerateFieldDescription, exposed for classes that need custom
45   // generation.
46 
47   // Exposed for subclasses to extend, base does nothing.
48   virtual void GenerateCFunctionDeclarations(io::Printer* printer) const;
49   virtual void GenerateCFunctionImplementations(io::Printer* printer) const;
50 
51   // Exposed for subclasses, should always call it on the parent class also.
52   virtual void DetermineForwardDeclarations(
53       absl::btree_set<std::string>* fwd_decls,
54       bool include_external_types) const;
55   virtual void DetermineObjectiveCClassDefinitions(
56       absl::btree_set<std::string>* fwd_decls) const;
57   virtual void DetermineNeededFiles(
58       absl::flat_hash_set<const FileDescriptor*>* deps) const;
59 
60   // Used during generation, not intended to be extended by subclasses.
61   void GenerateFieldDescription(io::Printer* printer,
62                                 bool include_default) const;
63   void GenerateFieldNumberConstant(io::Printer* printer) const;
64 
65   // Exposed to get and set the has bits information.
66   virtual bool RuntimeUsesHasBit() const = 0;
67   void SetRuntimeHasBit(int has_index);
68   void SetNoHasBit();
69   virtual int ExtraRuntimeHasBitsNeeded() const;
70   virtual void SetExtraRuntimeHasBitsBase(int index_base);
71   void SetOneofIndexBase(int index_base);
72 
variable(const char * key)73   std::string variable(const char* key) const {
74     return variables_.find(key)->second;
75   }
76 
needs_textformat_name_support()77   bool needs_textformat_name_support() const {
78     const std::string& field_flags = variable("fieldflags");
79     return absl::StrContains(field_flags, "GPBFieldTextFormatNameCustom");
80   }
generated_objc_name()81   std::string generated_objc_name() const { return variable("name"); }
raw_field_name()82   std::string raw_field_name() const { return variable("raw_field_name"); }
83 
84  protected:
85   FieldGenerator(const FieldDescriptor* descriptor,
86                  const GenerationOptions& generation_options);
87 
88   bool WantsHasProperty() const;
89 
90   const FieldDescriptor* descriptor_;
91   const GenerationOptions& generation_options_;
92   absl::flat_hash_map<absl::string_view, std::string> variables_;
93 };
94 
95 class SingleFieldGenerator : public FieldGenerator {
96  public:
97   ~SingleFieldGenerator() override = default;
98 
99   SingleFieldGenerator(const SingleFieldGenerator&) = delete;
100   SingleFieldGenerator& operator=(const SingleFieldGenerator&) = delete;
101 
102   void GenerateFieldStorageDeclaration(io::Printer* printer) const override;
103   void GeneratePropertyDeclaration(io::Printer* printer) const override;
104 
105   void GeneratePropertyImplementation(io::Printer* printer) const override;
106 
107   bool RuntimeUsesHasBit() const override;
108 
109  protected:
110   SingleFieldGenerator(const FieldDescriptor* descriptor,
111                        const GenerationOptions& generation_options);
112 };
113 
114 // Subclass with common support for when the field ends up as an ObjC Object.
115 class ObjCObjFieldGenerator : public SingleFieldGenerator {
116  public:
117   ~ObjCObjFieldGenerator() override = default;
118 
119   ObjCObjFieldGenerator(const ObjCObjFieldGenerator&) = delete;
120   ObjCObjFieldGenerator& operator=(const ObjCObjFieldGenerator&) = delete;
121 
122   void GenerateFieldStorageDeclaration(io::Printer* printer) const override;
123   void GeneratePropertyDeclaration(io::Printer* printer) const override;
124 
125  protected:
126   ObjCObjFieldGenerator(const FieldDescriptor* descriptor,
127                         const GenerationOptions& generation_options);
128 };
129 
130 class RepeatedFieldGenerator : public ObjCObjFieldGenerator {
131  public:
132   ~RepeatedFieldGenerator() override = default;
133 
134   RepeatedFieldGenerator(const RepeatedFieldGenerator&) = delete;
135   RepeatedFieldGenerator& operator=(const RepeatedFieldGenerator&) = delete;
136 
137   void GenerateFieldStorageDeclaration(io::Printer* printer) const override;
138   void GeneratePropertyDeclaration(io::Printer* printer) const override;
139 
140   void GeneratePropertyImplementation(io::Printer* printer) const override;
141 
142   bool RuntimeUsesHasBit() const override;
143 
144   virtual void EmitArrayComment(io::Printer* printer) const;
145 
146  protected:
147   RepeatedFieldGenerator(const FieldDescriptor* descriptor,
148                          const GenerationOptions& generation_options);
149 };
150 
151 // Convenience class which constructs FieldGenerators for a Descriptor.
152 class FieldGeneratorMap {
153  public:
154   FieldGeneratorMap(const Descriptor* descriptor,
155                     const GenerationOptions& generation_options);
156   ~FieldGeneratorMap() = default;
157 
158   FieldGeneratorMap(const FieldGeneratorMap&) = delete;
159   FieldGeneratorMap& operator=(const FieldGeneratorMap&) = delete;
160 
161   const FieldGenerator& get(const FieldDescriptor* field) const;
162 
163   // Assigns the has bits and returns the number of bits needed.
164   int CalculateHasBits();
165 
166   void SetOneofIndexBase(int index_base);
167 
168   // Check if any field of this message has a non zero default.
169   bool DoesAnyFieldHaveNonZeroDefault() const;
170 
171  private:
172   const Descriptor* descriptor_;
173   std::vector<std::unique_ptr<FieldGenerator>> field_generators_;
174 };
175 
176 }  // namespace objectivec
177 }  // namespace compiler
178 }  // namespace protobuf
179 }  // namespace google
180 
181 #endif  // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__
182