Line data Source code
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 : #include <map>
32 : #include <string>
33 :
34 : #include <google/protobuf/compiler/objectivec/objectivec_primitive_field.h>
35 : #include <google/protobuf/stubs/common.h>
36 : #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
37 : #include <google/protobuf/io/printer.h>
38 : #include <google/protobuf/wire_format.h>
39 : #include <google/protobuf/wire_format_lite_inl.h>
40 : #include <google/protobuf/stubs/strutil.h>
41 : #include <google/protobuf/stubs/substitute.h>
42 :
43 : namespace google {
44 : namespace protobuf {
45 : namespace compiler {
46 : namespace objectivec {
47 :
48 : using internal::WireFormat;
49 : using internal::WireFormatLite;
50 :
51 : namespace {
52 :
53 0 : const char* PrimitiveTypeName(const FieldDescriptor* descriptor) {
54 0 : ObjectiveCType type = GetObjectiveCType(descriptor);
55 0 : switch (type) {
56 : case OBJECTIVECTYPE_INT32:
57 : return "int32_t";
58 : case OBJECTIVECTYPE_UINT32:
59 0 : return "uint32_t";
60 : case OBJECTIVECTYPE_INT64:
61 0 : return "int64_t";
62 : case OBJECTIVECTYPE_UINT64:
63 0 : return "uint64_t";
64 : case OBJECTIVECTYPE_FLOAT:
65 0 : return "float";
66 : case OBJECTIVECTYPE_DOUBLE:
67 0 : return "double";
68 : case OBJECTIVECTYPE_BOOLEAN:
69 0 : return "BOOL";
70 : case OBJECTIVECTYPE_STRING:
71 0 : return "NSString";
72 : case OBJECTIVECTYPE_DATA:
73 0 : return "NSData";
74 : case OBJECTIVECTYPE_ENUM:
75 : return "int32_t";
76 : case OBJECTIVECTYPE_MESSAGE:
77 0 : return NULL;
78 : }
79 :
80 : // Some compilers report reaching end of function even though all cases of
81 : // the enum are handed in the switch.
82 0 : GOOGLE_LOG(FATAL) << "Can't get here.";
83 0 : return NULL;
84 : }
85 :
86 0 : const char* PrimitiveArrayTypeName(const FieldDescriptor* descriptor) {
87 0 : ObjectiveCType type = GetObjectiveCType(descriptor);
88 0 : switch (type) {
89 : case OBJECTIVECTYPE_INT32:
90 : return "Int32";
91 : case OBJECTIVECTYPE_UINT32:
92 0 : return "UInt32";
93 : case OBJECTIVECTYPE_INT64:
94 0 : return "Int64";
95 : case OBJECTIVECTYPE_UINT64:
96 0 : return "UInt64";
97 : case OBJECTIVECTYPE_FLOAT:
98 0 : return "Float";
99 : case OBJECTIVECTYPE_DOUBLE:
100 0 : return "Double";
101 : case OBJECTIVECTYPE_BOOLEAN:
102 0 : return "Bool";
103 : case OBJECTIVECTYPE_STRING:
104 0 : return ""; // Want NSArray
105 : case OBJECTIVECTYPE_DATA:
106 0 : return ""; // Want NSArray
107 : case OBJECTIVECTYPE_ENUM:
108 0 : return "Enum";
109 : case OBJECTIVECTYPE_MESSAGE:
110 0 : return ""; // Want NSArray
111 : }
112 :
113 : // Some compilers report reaching end of function even though all cases of
114 : // the enum are handed in the switch.
115 0 : GOOGLE_LOG(FATAL) << "Can't get here.";
116 0 : return NULL;
117 : }
118 :
119 0 : void SetPrimitiveVariables(const FieldDescriptor* descriptor,
120 : map<string, string>* variables) {
121 0 : std::string primitive_name = PrimitiveTypeName(descriptor);
122 0 : (*variables)["type"] = primitive_name;
123 0 : (*variables)["storage_type"] = primitive_name;
124 0 : }
125 :
126 : } // namespace
127 :
128 0 : PrimitiveFieldGenerator::PrimitiveFieldGenerator(
129 : const FieldDescriptor* descriptor)
130 0 : : SingleFieldGenerator(descriptor) {
131 0 : SetPrimitiveVariables(descriptor, &variables_);
132 0 : }
133 :
134 0 : PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {}
135 :
136 0 : PrimitiveObjFieldGenerator::PrimitiveObjFieldGenerator(
137 : const FieldDescriptor* descriptor)
138 0 : : ObjCObjFieldGenerator(descriptor) {
139 0 : SetPrimitiveVariables(descriptor, &variables_);
140 0 : variables_["property_storage_attribute"] = "copy";
141 0 : }
142 :
143 0 : PrimitiveObjFieldGenerator::~PrimitiveObjFieldGenerator() {}
144 :
145 0 : RepeatedPrimitiveFieldGenerator::RepeatedPrimitiveFieldGenerator(
146 : const FieldDescriptor* descriptor)
147 0 : : RepeatedFieldGenerator(descriptor) {
148 0 : SetPrimitiveVariables(descriptor, &variables_);
149 :
150 0 : string base_name = PrimitiveArrayTypeName(descriptor);
151 0 : if (base_name.length()) {
152 0 : variables_["array_storage_type"] = "GPB" + base_name + "Array";
153 : } else {
154 0 : variables_["array_storage_type"] = "NSMutableArray";
155 : }
156 0 : }
157 :
158 0 : RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {}
159 :
160 0 : void RepeatedPrimitiveFieldGenerator::FinishInitialization(void) {
161 0 : RepeatedFieldGenerator::FinishInitialization();
162 0 : if (IsPrimitiveType(descriptor_)) {
163 : // No comment needed for primitive types.
164 0 : variables_["array_comment"] = "";
165 : }
166 0 : }
167 :
168 : } // namespace objectivec
169 : } // namespace compiler
170 : } // namespace protobuf
171 : } // namespace google
|