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