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( 69 std::set<std::string>* fwd_decls) const; 70 virtual void DetermineObjectiveCClassDefinitions( 71 std::set<std::string>* fwd_decls) const; 72 73 // Used during generation, not intended to be extended by subclasses. 74 void GenerateFieldDescription( 75 io::Printer* printer, bool include_default) const; 76 void GenerateFieldNumberConstant(io::Printer* printer) const; 77 78 // Exposed to get and set the has bits information. 79 virtual bool RuntimeUsesHasBit(void) const = 0; 80 void SetRuntimeHasBit(int has_index); 81 void SetNoHasBit(void); 82 virtual int ExtraRuntimeHasBitsNeeded(void) const; 83 virtual void SetExtraRuntimeHasBitsBase(int index_base); 84 void SetOneofIndexBase(int index_base); 85 variable(const char * key)86 std::string variable(const char* key) const { 87 return variables_.find(key)->second; 88 } 89 needs_textformat_name_support()90 bool needs_textformat_name_support() const { 91 const std::string& field_flags = variable("fieldflags"); 92 return field_flags.find("GPBFieldTextFormatNameCustom") != 93 std::string::npos; 94 } generated_objc_name()95 std::string generated_objc_name() const { return variable("name"); } raw_field_name()96 std::string raw_field_name() const { return variable("raw_field_name"); } 97 98 protected: 99 FieldGenerator(const FieldDescriptor* descriptor, const Options& options); 100 101 virtual void FinishInitialization(void); 102 bool WantsHasProperty(void) const; 103 104 const FieldDescriptor* descriptor_; 105 std::map<std::string, std::string> variables_; 106 }; 107 108 class SingleFieldGenerator : public FieldGenerator { 109 public: 110 virtual ~SingleFieldGenerator(); 111 112 SingleFieldGenerator(const SingleFieldGenerator&) = delete; 113 SingleFieldGenerator& operator=(const SingleFieldGenerator&) = delete; 114 115 virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const; 116 virtual void GeneratePropertyDeclaration(io::Printer* printer) const; 117 118 virtual void GeneratePropertyImplementation(io::Printer* printer) const; 119 120 virtual bool RuntimeUsesHasBit(void) const; 121 122 protected: 123 SingleFieldGenerator(const FieldDescriptor* descriptor, 124 const Options& options); 125 }; 126 127 // Subclass with common support for when the field ends up as an ObjC Object. 128 class ObjCObjFieldGenerator : public SingleFieldGenerator { 129 public: 130 virtual ~ObjCObjFieldGenerator(); 131 132 ObjCObjFieldGenerator(const ObjCObjFieldGenerator&) = delete; 133 ObjCObjFieldGenerator& operator=(const ObjCObjFieldGenerator&) = delete; 134 135 virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const; 136 virtual void GeneratePropertyDeclaration(io::Printer* printer) const; 137 138 protected: 139 ObjCObjFieldGenerator(const FieldDescriptor* descriptor, 140 const Options& options); 141 }; 142 143 class RepeatedFieldGenerator : public ObjCObjFieldGenerator { 144 public: 145 virtual ~RepeatedFieldGenerator(); 146 147 RepeatedFieldGenerator(const RepeatedFieldGenerator&) = delete; 148 RepeatedFieldGenerator& operator=(const RepeatedFieldGenerator&) = delete; 149 150 virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const; 151 virtual void GeneratePropertyDeclaration(io::Printer* printer) const; 152 153 virtual void GeneratePropertyImplementation(io::Printer* printer) const; 154 155 virtual bool RuntimeUsesHasBit(void) const; 156 157 protected: 158 RepeatedFieldGenerator(const FieldDescriptor* descriptor, 159 const Options& options); 160 virtual void FinishInitialization(void); 161 }; 162 163 // Convenience class which constructs FieldGenerators for a Descriptor. 164 class FieldGeneratorMap { 165 public: 166 FieldGeneratorMap(const Descriptor* descriptor, const Options& options); 167 ~FieldGeneratorMap(); 168 169 FieldGeneratorMap(const FieldGeneratorMap&) = delete; 170 FieldGeneratorMap& operator=(const FieldGeneratorMap&) = delete; 171 172 const FieldGenerator& get(const FieldDescriptor* field) const; 173 const FieldGenerator& get_extension(int index) const; 174 175 // Assigns the has bits and returns the number of bits needed. 176 int CalculateHasBits(void); 177 178 void SetOneofIndexBase(int index_base); 179 180 // Check if any field of this message has a non zero default. 181 bool DoesAnyFieldHaveNonZeroDefault(void) const; 182 183 private: 184 const Descriptor* descriptor_; 185 std::vector<std::unique_ptr<FieldGenerator>> field_generators_; 186 std::vector<std::unique_ptr<FieldGenerator>> extension_generators_; 187 }; 188 189 } // namespace objectivec 190 } // namespace compiler 191 } // namespace protobuf 192 } // namespace google 193 194 #endif // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__ 195