• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #include "google/protobuf/compiler/objectivec/primitive_field.h"
9 
10 #include <string>
11 
12 #include "absl/container/flat_hash_map.h"
13 #include "absl/strings/str_cat.h"
14 #include "google/protobuf/compiler/objectivec/field.h"
15 #include "google/protobuf/compiler/objectivec/helpers.h"
16 #include "google/protobuf/compiler/objectivec/options.h"
17 #include "google/protobuf/descriptor.h"
18 #include "google/protobuf/io/printer.h"
19 
20 namespace google {
21 namespace protobuf {
22 namespace compiler {
23 namespace objectivec {
24 
PrimitiveFieldGenerator(const FieldDescriptor * descriptor,const GenerationOptions & generation_options)25 PrimitiveFieldGenerator::PrimitiveFieldGenerator(
26     const FieldDescriptor* descriptor,
27     const GenerationOptions& generation_options)
28     : SingleFieldGenerator(descriptor, generation_options) {}
29 
GenerateFieldStorageDeclaration(io::Printer * printer) const30 void PrimitiveFieldGenerator::GenerateFieldStorageDeclaration(
31     io::Printer* printer) const {
32   if (GetObjectiveCType(descriptor_) == OBJECTIVECTYPE_BOOLEAN) {
33     // Nothing, BOOLs are stored in the has bits.
34   } else {
35     SingleFieldGenerator::GenerateFieldStorageDeclaration(printer);
36   }
37 }
38 
ExtraRuntimeHasBitsNeeded() const39 int PrimitiveFieldGenerator::ExtraRuntimeHasBitsNeeded() const {
40   if (GetObjectiveCType(descriptor_) == OBJECTIVECTYPE_BOOLEAN) {
41     // Reserve a bit for the storage of the boolean.
42     return 1;
43   }
44   return 0;
45 }
46 
SetExtraRuntimeHasBitsBase(int index_base)47 void PrimitiveFieldGenerator::SetExtraRuntimeHasBitsBase(int index_base) {
48   if (GetObjectiveCType(descriptor_) == OBJECTIVECTYPE_BOOLEAN) {
49     // Set into the offset the has bit to use for the actual value.
50     variables_["storage_offset_value"] = absl::StrCat(index_base);
51     variables_["storage_offset_comment"] =
52         "  // Stored in _has_storage_ to save space.";
53   }
54 }
55 
PrimitiveObjFieldGenerator(const FieldDescriptor * descriptor,const GenerationOptions & generation_options)56 PrimitiveObjFieldGenerator::PrimitiveObjFieldGenerator(
57     const FieldDescriptor* descriptor,
58     const GenerationOptions& generation_options)
59     : ObjCObjFieldGenerator(descriptor, generation_options) {
60   variables_["property_storage_attribute"] = "copy";
61 }
62 
RepeatedPrimitiveFieldGenerator(const FieldDescriptor * descriptor,const GenerationOptions & generation_options)63 RepeatedPrimitiveFieldGenerator::RepeatedPrimitiveFieldGenerator(
64     const FieldDescriptor* descriptor,
65     const GenerationOptions& generation_options)
66     : RepeatedFieldGenerator(descriptor, generation_options) {}
67 
68 }  // namespace objectivec
69 }  // namespace compiler
70 }  // namespace protobuf
71 }  // namespace google
72