blob: 4816984327118d3b84a6bbb6544fa88d6dd77fa0 [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 Jin0473f88b2017-10-09 11:21:40 -070021static void
22write_enum(stringstream& text, const EnumDescriptorProto& enu, const string& indent)
23{
24 const int N = enu.value_size();
25 text << indent << "// enum " << enu.name() << endl;
26 for (int i=0; i<N; i++) {
27 const EnumValueDescriptorProto& value = enu.value(i);
28 text << indent << "const uint32_t "
29 << make_constant_name(value.name())
30 << " = " << value.number() << ";" << endl;
31 }
32 text << endl;
33}
34
Yi Jin0473f88b2017-10-09 11:21:40 -070035static void
36write_field(stringstream& text, const FieldDescriptorProto& field, const string& indent)
37{
38 string optional_comment = field.label() == FieldDescriptorProto::LABEL_OPTIONAL
39 ? "optional " : "";
40 string repeated_comment = field.label() == FieldDescriptorProto::LABEL_REPEATED
41 ? "repeated " : "";
42 string proto_type = get_proto_type(field);
43 string packed_comment = field.options().packed()
44 ? " [packed=true]" : "";
45 text << indent << "// " << optional_comment << repeated_comment << proto_type << ' '
46 << field.name() << " = " << field.number() << packed_comment << ';' << endl;
47
48 text << indent << "const uint64_t " << make_constant_name(field.name()) << " = 0x";
49
50 ios::fmtflags fmt(text.flags());
51 text << setfill('0') << setw(16) << hex << get_field_id(field);
52 text.flags(fmt);
53
54 text << "LL;" << endl;
55
56 text << endl;
57}
58
Yi Jin04625ad2017-10-17 18:29:33 -070059static inline bool
60should_generate_fields_mapping(const DescriptorProto& message)
61{
62 return message.options().GetExtension(stream).enable_fields_mapping();
63}
64
Yi Jin0473f88b2017-10-09 11:21:40 -070065static void
66write_message(stringstream& text, const DescriptorProto& message, const string& indent)
67{
68 int N;
69 const string indented = indent + INDENT;
70
71 text << indent << "// message " << message.name() << endl;
Yi Jin04625ad2017-10-17 18:29:33 -070072 text << indent << "namespace " << message.name() << " {" << endl;
Yi Jin0473f88b2017-10-09 11:21:40 -070073
74 // Enums
75 N = message.enum_type_size();
76 for (int i=0; i<N; i++) {
77 write_enum(text, message.enum_type(i), indented);
78 }
79
80 // Nested classes
81 N = message.nested_type_size();
82 for (int i=0; i<N; i++) {
83 write_message(text, message.nested_type(i), indented);
84 }
85
86 // Fields
87 N = message.field_size();
88 for (int i=0; i<N; i++) {
89 write_field(text, message.field(i), indented);
90 }
91
Yi Jin04625ad2017-10-17 18:29:33 -070092 if (should_generate_fields_mapping(message)) {
93 N = message.field_size();
94 text << indented << "const int _FIELD_COUNT = " << N << ";" << endl;
95 text << indented << "const char* _FIELD_NAMES[" << N << "] = {" << endl;
96 for (int i=0; i<N; i++) {
97 text << indented << INDENT << "\"" << message.field(i).name() << "\"," << endl;
98 }
99 text << indented << "};" << endl;
100 text << indented << "const uint64_t _FIELD_IDS[" << N << "] = {" << endl;
101 for (int i=0; i<N; i++) {
102 text << indented << INDENT << make_constant_name(message.field(i).name()) << "," << endl;
103 }
104 text << indented << "};" << endl << endl;
105 }
106
107 text << indent << "} //" << message.name() << endl;
Yi Jin0473f88b2017-10-09 11:21:40 -0700108 text << endl;
109}
110
111static void
Yi Jin04625ad2017-10-17 18:29:33 -0700112write_header_file(CodeGeneratorResponse* response, const FileDescriptorProto& file_descriptor)
Yi Jin0473f88b2017-10-09 11:21:40 -0700113{
114 stringstream text;
115
116 text << "// Generated by protoc-gen-cppstream. DO NOT MODIFY." << endl;
117 text << "// source: " << file_descriptor.name() << endl << endl;
118
119 string header = "ANDROID_" + replace_string(file_descriptor.name(), '/', '_');
120 header = replace_string(header, '.', '_') + "_stream_h";
121 header = make_constant_name(header);
122
123 text << "#ifndef " << header << endl;
124 text << "#define " << header << endl;
125 text << endl;
126
127 vector<string> namespaces = split(file_descriptor.package(), '.');
128 for (vector<string>::iterator it = namespaces.begin(); it != namespaces.end(); it++) {
129 text << "namespace " << *it << " {" << endl;
130 }
131 text << endl;
132
133 size_t N;
134 N = file_descriptor.enum_type_size();
135 for (size_t i=0; i<N; i++) {
136 write_enum(text, file_descriptor.enum_type(i), "");
137 }
138
139 N = file_descriptor.message_type_size();
140 for (size_t i=0; i<N; i++) {
141 write_message(text, file_descriptor.message_type(i), "");
142 }
143
144 for (vector<string>::iterator it = namespaces.begin(); it != namespaces.end(); it++) {
145 text << "} // " << *it << endl;
146 }
147
148 text << endl;
149 text << "#endif // " << header << endl;
150
151 CodeGeneratorResponse::File* file_response = response->add_file();
152 file_response->set_name(make_filename(file_descriptor));
153 file_response->set_content(text.str());
154}
155
156int main(int argc, char const *argv[])
157{
158 (void)argc;
159 (void)argv;
160
161 GOOGLE_PROTOBUF_VERIFY_VERSION;
162
163 CodeGeneratorRequest request;
164 CodeGeneratorResponse response;
165
166 // Read the request
167 request.ParseFromIstream(&cin);
168
169 // Build the files we need.
170 const int N = request.proto_file_size();
171 for (int i=0; i<N; i++) {
172 const FileDescriptorProto& file_descriptor = request.proto_file(i);
173 if (should_generate_for_file(request, file_descriptor.name())) {
Yi Jin04625ad2017-10-17 18:29:33 -0700174 write_header_file(&response, file_descriptor);
Yi Jin0473f88b2017-10-09 11:21:40 -0700175 }
176 }
177
178 // If we had errors, don't write the response. Print the errors and exit.
179 if (ERRORS.HasErrors()) {
180 ERRORS.Print();
181 return 1;
182 }
183
184 // If we didn't have errors, write the response and exit happily.
185 response.SerializeToOstream(&cout);
186
187 /* code */
188 return 0;
Yi Jin04625ad2017-10-17 18:29:33 -0700189}