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