blob: 9aef56270ee2657cfb56c11780b2c0f87b37f33f [file] [log] [blame]
Yi Jin0473f88b2017-10-09 11:21:40 -07001#include "Errors.h"
Yi Jinf9ed04b2017-10-20 16:17:58 -07002#include "stream_proto_utils.h"
Yi Jin0473f88b2017-10-09 11:21:40 -07003#include "string_utils.h"
4
Yi Jin04625ad2017-10-17 18:29:33 -07005#include <frameworks/base/tools/streaming_proto/stream.pb.h>
6
Yi Jin0473f88b2017-10-09 11:21:40 -07007#include <iomanip>
8#include <iostream>
9#include <sstream>
10
11using namespace android::stream_proto;
Yi Jin0473f88b2017-10-09 11:21:40 -070012using namespace google::protobuf::io;
13using namespace std;
14
Yi Jin0473f88b2017-10-09 11:21:40 -070015static string
16make_filename(const FileDescriptorProto& file_descriptor)
17{
18 return file_descriptor.name() + ".h";
19}
20
Yi Jine2f7f792017-11-01 17:08:27 -070021static inline bool
22should_generate_enums_mapping(const EnumDescriptorProto& enu)
23{
24 return enu.options().GetExtension(stream_enum).enable_enums_mapping();
25}
26
Yi Jin0473f88b2017-10-09 11:21:40 -070027static void
28write_enum(stringstream& text, const EnumDescriptorProto& enu, const string& indent)
29{
30 const int N = enu.value_size();
31 text << indent << "// enum " << enu.name() << endl;
32 for (int i=0; i<N; i++) {
33 const EnumValueDescriptorProto& value = enu.value(i);
34 text << indent << "const uint32_t "
35 << make_constant_name(value.name())
36 << " = " << value.number() << ";" << endl;
37 }
Yi Jine2f7f792017-11-01 17:08:27 -070038
39 if (should_generate_enums_mapping(enu)) {
40 string name = make_constant_name(enu.name());
41 string prefix = name + "_";
42 text << indent << "const int _ENUM_" << name << "_COUNT = " << N << ";" << endl;
43 text << indent << "const char* _ENUM_" << name << "_NAMES[" << N << "] = {" << endl;
44 for (int i=0; i<N; i++) {
45 text << indent << INDENT << "\"" << stripPrefix(enu.value(i).name(), prefix) << "\"," << endl;
46 }
47 text << indent << "};" << endl;
48 text << indent << "const uint32_t _ENUM_" << name << "_VALUES[" << N << "] = {" << endl;
49 for (int i=0; i<N; i++) {
50 text << indent << INDENT << make_constant_name(enu.value(i).name()) << "," << endl;
51 }
52 text << indent << "};" << endl;
53 }
54
Yi Jin0473f88b2017-10-09 11:21:40 -070055 text << endl;
56}
57
Yi Jin0473f88b2017-10-09 11:21:40 -070058static void
59write_field(stringstream& text, const FieldDescriptorProto& field, const string& indent)
60{
61 string optional_comment = field.label() == FieldDescriptorProto::LABEL_OPTIONAL
62 ? "optional " : "";
63 string repeated_comment = field.label() == FieldDescriptorProto::LABEL_REPEATED
64 ? "repeated " : "";
65 string proto_type = get_proto_type(field);
66 string packed_comment = field.options().packed()
67 ? " [packed=true]" : "";
68 text << indent << "// " << optional_comment << repeated_comment << proto_type << ' '
69 << field.name() << " = " << field.number() << packed_comment << ';' << endl;
70
71 text << indent << "const uint64_t " << make_constant_name(field.name()) << " = 0x";
72
73 ios::fmtflags fmt(text.flags());
74 text << setfill('0') << setw(16) << hex << get_field_id(field);
75 text.flags(fmt);
76
77 text << "LL;" << endl;
78
79 text << endl;
80}
81
Yi Jin04625ad2017-10-17 18:29:33 -070082static inline bool
83should_generate_fields_mapping(const DescriptorProto& message)
84{
Yi Jine2f7f792017-11-01 17:08:27 -070085 return message.options().GetExtension(stream_msg).enable_fields_mapping();
Yi Jin04625ad2017-10-17 18:29:33 -070086}
87
Yi Jin0473f88b2017-10-09 11:21:40 -070088static void
89write_message(stringstream& text, const DescriptorProto& message, const string& indent)
90{
91 int N;
92 const string indented = indent + INDENT;
93
94 text << indent << "// message " << message.name() << endl;
Yi Jin04625ad2017-10-17 18:29:33 -070095 text << indent << "namespace " << message.name() << " {" << endl;
Yi Jin0473f88b2017-10-09 11:21:40 -070096
97 // Enums
98 N = message.enum_type_size();
99 for (int i=0; i<N; i++) {
100 write_enum(text, message.enum_type(i), indented);
101 }
102
103 // Nested classes
104 N = message.nested_type_size();
105 for (int i=0; i<N; i++) {
106 write_message(text, message.nested_type(i), indented);
107 }
108
109 // Fields
110 N = message.field_size();
111 for (int i=0; i<N; i++) {
112 write_field(text, message.field(i), indented);
113 }
114
Yi Jin04625ad2017-10-17 18:29:33 -0700115 if (should_generate_fields_mapping(message)) {
116 N = message.field_size();
117 text << indented << "const int _FIELD_COUNT = " << N << ";" << endl;
118 text << indented << "const char* _FIELD_NAMES[" << N << "] = {" << endl;
119 for (int i=0; i<N; i++) {
120 text << indented << INDENT << "\"" << message.field(i).name() << "\"," << endl;
121 }
122 text << indented << "};" << endl;
123 text << indented << "const uint64_t _FIELD_IDS[" << N << "] = {" << endl;
124 for (int i=0; i<N; i++) {
125 text << indented << INDENT << make_constant_name(message.field(i).name()) << "," << endl;
126 }
127 text << indented << "};" << endl << endl;
128 }
129
130 text << indent << "} //" << message.name() << endl;
Yi Jin0473f88b2017-10-09 11:21:40 -0700131 text << endl;
132}
133
134static void
Yi Jin04625ad2017-10-17 18:29:33 -0700135write_header_file(CodeGeneratorResponse* response, const FileDescriptorProto& file_descriptor)
Yi Jin0473f88b2017-10-09 11:21:40 -0700136{
137 stringstream text;
138
139 text << "// Generated by protoc-gen-cppstream. DO NOT MODIFY." << endl;
140 text << "// source: " << file_descriptor.name() << endl << endl;
141
142 string header = "ANDROID_" + replace_string(file_descriptor.name(), '/', '_');
143 header = replace_string(header, '.', '_') + "_stream_h";
144 header = make_constant_name(header);
145
146 text << "#ifndef " << header << endl;
147 text << "#define " << header << endl;
148 text << endl;
149
150 vector<string> namespaces = split(file_descriptor.package(), '.');
151 for (vector<string>::iterator it = namespaces.begin(); it != namespaces.end(); it++) {
152 text << "namespace " << *it << " {" << endl;
153 }
154 text << endl;
155
156 size_t N;
157 N = file_descriptor.enum_type_size();
158 for (size_t i=0; i<N; i++) {
159 write_enum(text, file_descriptor.enum_type(i), "");
160 }
161
162 N = file_descriptor.message_type_size();
163 for (size_t i=0; i<N; i++) {
164 write_message(text, file_descriptor.message_type(i), "");
165 }
166
167 for (vector<string>::iterator it = namespaces.begin(); it != namespaces.end(); it++) {
168 text << "} // " << *it << endl;
169 }
170
171 text << endl;
172 text << "#endif // " << header << endl;
173
174 CodeGeneratorResponse::File* file_response = response->add_file();
175 file_response->set_name(make_filename(file_descriptor));
176 file_response->set_content(text.str());
177}
178
179int main(int argc, char const *argv[])
180{
181 (void)argc;
182 (void)argv;
183
184 GOOGLE_PROTOBUF_VERIFY_VERSION;
185
186 CodeGeneratorRequest request;
187 CodeGeneratorResponse response;
188
189 // Read the request
190 request.ParseFromIstream(&cin);
191
192 // Build the files we need.
193 const int N = request.proto_file_size();
194 for (int i=0; i<N; i++) {
195 const FileDescriptorProto& file_descriptor = request.proto_file(i);
196 if (should_generate_for_file(request, file_descriptor.name())) {
Yi Jin04625ad2017-10-17 18:29:33 -0700197 write_header_file(&response, file_descriptor);
Yi Jin0473f88b2017-10-09 11:21:40 -0700198 }
199 }
200
201 // If we had errors, don't write the response. Print the errors and exit.
202 if (ERRORS.HasErrors()) {
203 ERRORS.Print();
204 return 1;
205 }
206
207 // If we didn't have errors, write the response and exit happily.
208 response.SerializeToOstream(&cout);
209
210 /* code */
211 return 0;
Yi Jin04625ad2017-10-17 18:29:33 -0700212}