objectivec_field.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #ifndef GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__
  31. #define GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__
  32. #include <map>
  33. #include <string>
  34. #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
  35. #include <google/protobuf/stubs/common.h>
  36. #include <google/protobuf/descriptor.h>
  37. namespace google {
  38. namespace protobuf {
  39. namespace io {
  40. class Printer; // printer.h
  41. } // namespace io
  42. namespace compiler {
  43. namespace objectivec {
  44. class FieldGenerator {
  45. public:
  46. static FieldGenerator* Make(const FieldDescriptor* field,
  47. const Options& options);
  48. virtual ~FieldGenerator();
  49. // Exposed for subclasses to fill in.
  50. virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const = 0;
  51. virtual void GeneratePropertyDeclaration(io::Printer* printer) const = 0;
  52. virtual void GeneratePropertyImplementation(io::Printer* printer) const = 0;
  53. // Called by GenerateFieldDescription, exposed for classes that need custom
  54. // generation.
  55. // Exposed for subclasses to extend, base does nothing.
  56. virtual void GenerateCFunctionDeclarations(io::Printer* printer) const;
  57. virtual void GenerateCFunctionImplementations(io::Printer* printer) const;
  58. // Exposed for subclasses, should always call it on the parent class also.
  59. virtual void DetermineForwardDeclarations(std::set<string>* fwd_decls) const;
  60. // Used during generation, not intended to be extended by subclasses.
  61. void GenerateFieldDescription(
  62. io::Printer* printer, bool include_default) const;
  63. void GenerateFieldNumberConstant(io::Printer* printer) const;
  64. // Exposed to get and set the has bits information.
  65. virtual bool RuntimeUsesHasBit(void) const = 0;
  66. void SetRuntimeHasBit(int has_index);
  67. void SetNoHasBit(void);
  68. virtual int ExtraRuntimeHasBitsNeeded(void) const;
  69. virtual void SetExtraRuntimeHasBitsBase(int index_base);
  70. void SetOneofIndexBase(int index_base);
  71. string variable(const char* key) const {
  72. return variables_.find(key)->second;
  73. }
  74. bool needs_textformat_name_support() const {
  75. const string& field_flags = variable("fieldflags");
  76. return field_flags.find("GPBFieldTextFormatNameCustom") != string::npos;
  77. }
  78. string generated_objc_name() const { return variable("name"); }
  79. string raw_field_name() const { return variable("raw_field_name"); }
  80. protected:
  81. FieldGenerator(const FieldDescriptor* descriptor, const Options& options);
  82. virtual void FinishInitialization(void);
  83. virtual bool WantsHasProperty(void) const = 0;
  84. const FieldDescriptor* descriptor_;
  85. std::map<string, string> variables_;
  86. private:
  87. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator);
  88. };
  89. class SingleFieldGenerator : public FieldGenerator {
  90. public:
  91. virtual ~SingleFieldGenerator();
  92. virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const;
  93. virtual void GeneratePropertyDeclaration(io::Printer* printer) const;
  94. virtual void GeneratePropertyImplementation(io::Printer* printer) const;
  95. virtual bool RuntimeUsesHasBit(void) const;
  96. protected:
  97. SingleFieldGenerator(const FieldDescriptor* descriptor,
  98. const Options& options);
  99. virtual bool WantsHasProperty(void) const;
  100. private:
  101. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SingleFieldGenerator);
  102. };
  103. // Subclass with common support for when the field ends up as an ObjC Object.
  104. class ObjCObjFieldGenerator : public SingleFieldGenerator {
  105. public:
  106. virtual ~ObjCObjFieldGenerator();
  107. virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const;
  108. virtual void GeneratePropertyDeclaration(io::Printer* printer) const;
  109. protected:
  110. ObjCObjFieldGenerator(const FieldDescriptor* descriptor,
  111. const Options& options);
  112. private:
  113. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ObjCObjFieldGenerator);
  114. };
  115. class RepeatedFieldGenerator : public ObjCObjFieldGenerator {
  116. public:
  117. virtual ~RepeatedFieldGenerator();
  118. virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const;
  119. virtual void GeneratePropertyDeclaration(io::Printer* printer) const;
  120. virtual void GeneratePropertyImplementation(io::Printer* printer) const;
  121. virtual bool RuntimeUsesHasBit(void) const;
  122. protected:
  123. RepeatedFieldGenerator(const FieldDescriptor* descriptor,
  124. const Options& options);
  125. virtual void FinishInitialization(void);
  126. virtual bool WantsHasProperty(void) const;
  127. private:
  128. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedFieldGenerator);
  129. };
  130. // Convenience class which constructs FieldGenerators for a Descriptor.
  131. class FieldGeneratorMap {
  132. public:
  133. FieldGeneratorMap(const Descriptor* descriptor, const Options& options);
  134. ~FieldGeneratorMap();
  135. const FieldGenerator& get(const FieldDescriptor* field) const;
  136. const FieldGenerator& get_extension(int index) const;
  137. // Assigns the has bits and returns the number of bits needed.
  138. int CalculateHasBits(void);
  139. void SetOneofIndexBase(int index_base);
  140. // Check if any field of this message has a non zero default.
  141. bool DoesAnyFieldHaveNonZeroDefault(void) const;
  142. private:
  143. const Descriptor* descriptor_;
  144. std::vector<std::unique_ptr<FieldGenerator>> field_generators_;
  145. std::vector<std::unique_ptr<FieldGenerator>> extension_generators_;
  146. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap);
  147. };
  148. } // namespace objectivec
  149. } // namespace compiler
  150. } // namespace protobuf
  151. } // namespace google
  152. #endif // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__