• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__
32 #define GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__
33 
34 #include <map>
35 #include <string>
36 #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
37 #include <google/protobuf/descriptor.h>
38 #include <google/protobuf/io/printer.h>
39 
40 namespace google {
41 namespace protobuf {
42 namespace compiler {
43 namespace objectivec {
44 
45 class FieldGenerator {
46  public:
47   static FieldGenerator* Make(const FieldDescriptor* field,
48                               const Options& options);
49 
50   virtual ~FieldGenerator();
51 
52   FieldGenerator(const FieldGenerator&) = delete;
53   FieldGenerator& operator=(const FieldGenerator&) = delete;
54 
55   // Exposed for subclasses to fill in.
56   virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const = 0;
57   virtual void GeneratePropertyDeclaration(io::Printer* printer) const = 0;
58   virtual void GeneratePropertyImplementation(io::Printer* printer) const = 0;
59 
60   // Called by GenerateFieldDescription, exposed for classes that need custom
61   // generation.
62 
63   // Exposed for subclasses to extend, base does nothing.
64   virtual void GenerateCFunctionDeclarations(io::Printer* printer) const;
65   virtual void GenerateCFunctionImplementations(io::Printer* printer) const;
66 
67   // Exposed for subclasses, should always call it on the parent class also.
68   virtual void DetermineForwardDeclarations(std::set<string>* fwd_decls) const;
69   virtual void DetermineObjectiveCClassDefinitions(std::set<string>* fwd_decls) const;
70 
71   // Used during generation, not intended to be extended by subclasses.
72   void GenerateFieldDescription(
73       io::Printer* printer, bool include_default) const;
74   void GenerateFieldNumberConstant(io::Printer* printer) const;
75 
76   // Exposed to get and set the has bits information.
77   virtual bool RuntimeUsesHasBit(void) const = 0;
78   void SetRuntimeHasBit(int has_index);
79   void SetNoHasBit(void);
80   virtual int ExtraRuntimeHasBitsNeeded(void) const;
81   virtual void SetExtraRuntimeHasBitsBase(int index_base);
82   void SetOneofIndexBase(int index_base);
83 
variable(const char * key)84   string variable(const char* key) const {
85     return variables_.find(key)->second;
86   }
87 
needs_textformat_name_support()88   bool needs_textformat_name_support() const {
89     const string& field_flags = variable("fieldflags");
90     return field_flags.find("GPBFieldTextFormatNameCustom") != string::npos;
91   }
generated_objc_name()92   string generated_objc_name() const { return variable("name"); }
raw_field_name()93   string raw_field_name() const { return variable("raw_field_name"); }
94 
95  protected:
96   FieldGenerator(const FieldDescriptor* descriptor, const Options& options);
97 
98   virtual void FinishInitialization(void);
99   bool WantsHasProperty(void) const;
100 
101   const FieldDescriptor* descriptor_;
102   std::map<string, string> variables_;
103 };
104 
105 class SingleFieldGenerator : public FieldGenerator {
106  public:
107   virtual ~SingleFieldGenerator();
108 
109   SingleFieldGenerator(const SingleFieldGenerator&) = delete;
110   SingleFieldGenerator& operator=(const SingleFieldGenerator&) = delete;
111 
112   virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const;
113   virtual void GeneratePropertyDeclaration(io::Printer* printer) const;
114 
115   virtual void GeneratePropertyImplementation(io::Printer* printer) const;
116 
117   virtual bool RuntimeUsesHasBit(void) const;
118 
119  protected:
120   SingleFieldGenerator(const FieldDescriptor* descriptor,
121                        const Options& options);
122 };
123 
124 // Subclass with common support for when the field ends up as an ObjC Object.
125 class ObjCObjFieldGenerator : public SingleFieldGenerator {
126  public:
127   virtual ~ObjCObjFieldGenerator();
128 
129   ObjCObjFieldGenerator(const ObjCObjFieldGenerator&) = delete;
130   ObjCObjFieldGenerator& operator=(const ObjCObjFieldGenerator&) = delete;
131 
132   virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const;
133   virtual void GeneratePropertyDeclaration(io::Printer* printer) const;
134 
135  protected:
136   ObjCObjFieldGenerator(const FieldDescriptor* descriptor,
137                         const Options& options);
138 };
139 
140 class RepeatedFieldGenerator : public ObjCObjFieldGenerator {
141  public:
142   virtual ~RepeatedFieldGenerator();
143 
144   RepeatedFieldGenerator(const RepeatedFieldGenerator&) = delete;
145   RepeatedFieldGenerator& operator=(const RepeatedFieldGenerator&) = delete;
146 
147   virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const;
148   virtual void GeneratePropertyDeclaration(io::Printer* printer) const;
149 
150   virtual void GeneratePropertyImplementation(io::Printer* printer) const;
151 
152   virtual bool RuntimeUsesHasBit(void) const;
153 
154  protected:
155   RepeatedFieldGenerator(const FieldDescriptor* descriptor,
156                          const Options& options);
157   virtual void FinishInitialization(void);
158 };
159 
160 // Convenience class which constructs FieldGenerators for a Descriptor.
161 class FieldGeneratorMap {
162  public:
163   FieldGeneratorMap(const Descriptor* descriptor, const Options& options);
164   ~FieldGeneratorMap();
165 
166   FieldGeneratorMap(const FieldGeneratorMap&) = delete;
167   FieldGeneratorMap& operator=(const FieldGeneratorMap&) = delete;
168 
169   const FieldGenerator& get(const FieldDescriptor* field) const;
170   const FieldGenerator& get_extension(int index) const;
171 
172   // Assigns the has bits and returns the number of bits needed.
173   int CalculateHasBits(void);
174 
175   void SetOneofIndexBase(int index_base);
176 
177   // Check if any field of this message has a non zero default.
178   bool DoesAnyFieldHaveNonZeroDefault(void) const;
179 
180  private:
181   const Descriptor* descriptor_;
182   std::vector<std::unique_ptr<FieldGenerator>> field_generators_;
183   std::vector<std::unique_ptr<FieldGenerator>> extension_generators_;
184 };
185 
186 }  // namespace objectivec
187 }  // namespace compiler
188 }  // namespace protobuf
189 }  // namespace google
190 
191 #endif  // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__
192