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 <iomanip>
32 #include <sstream>
33
34 #include <google/protobuf/compiler/code_generator.h>
35 #include <google/protobuf/compiler/plugin.h>
36 #include <google/protobuf/descriptor.h>
37 #include <google/protobuf/descriptor.pb.h>
38 #include <google/protobuf/io/printer.h>
39 #include <google/protobuf/io/zero_copy_stream.h>
40
41 #include <google/protobuf/compiler/ruby/ruby_generator.h>
42
43 namespace google {
44 namespace protobuf {
45 namespace compiler {
46 namespace ruby {
47
48 // Forward decls.
49 template <class numeric_type>
50 std::string NumberToString(numeric_type value);
51 std::string GetRequireName(const std::string& proto_file);
52 std::string LabelForField(FieldDescriptor* field);
53 std::string TypeName(FieldDescriptor* field);
54 bool GenerateMessage(const Descriptor* message, io::Printer* printer,
55 std::string* error);
56 void GenerateEnum(const EnumDescriptor* en, io::Printer* printer);
57 void GenerateMessageAssignment(const std::string& prefix,
58 const Descriptor* message, io::Printer* printer);
59 void GenerateEnumAssignment(const std::string& prefix, const EnumDescriptor* en,
60 io::Printer* printer);
61 std::string DefaultValueForField(const FieldDescriptor* field);
62
63 template<class numeric_type>
NumberToString(numeric_type value)64 std::string NumberToString(numeric_type value) {
65 std::ostringstream os;
66 os << value;
67 return os.str();
68 }
69
GetRequireName(const std::string & proto_file)70 std::string GetRequireName(const std::string& proto_file) {
71 int lastindex = proto_file.find_last_of(".");
72 return proto_file.substr(0, lastindex) + "_pb";
73 }
74
GetOutputFilename(const std::string & proto_file)75 std::string GetOutputFilename(const std::string& proto_file) {
76 return GetRequireName(proto_file) + ".rb";
77 }
78
LabelForField(const FieldDescriptor * field)79 std::string LabelForField(const FieldDescriptor* field) {
80 if (field->has_optional_keyword() &&
81 field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3) {
82 return "proto3_optional";
83 }
84 switch (field->label()) {
85 case FieldDescriptor::LABEL_OPTIONAL: return "optional";
86 case FieldDescriptor::LABEL_REQUIRED: return "required";
87 case FieldDescriptor::LABEL_REPEATED: return "repeated";
88 default: assert(false); return "";
89 }
90 }
91
TypeName(const FieldDescriptor * field)92 std::string TypeName(const FieldDescriptor* field) {
93 switch (field->type()) {
94 case FieldDescriptor::TYPE_INT32: return "int32";
95 case FieldDescriptor::TYPE_INT64: return "int64";
96 case FieldDescriptor::TYPE_UINT32: return "uint32";
97 case FieldDescriptor::TYPE_UINT64: return "uint64";
98 case FieldDescriptor::TYPE_SINT32: return "sint32";
99 case FieldDescriptor::TYPE_SINT64: return "sint64";
100 case FieldDescriptor::TYPE_FIXED32: return "fixed32";
101 case FieldDescriptor::TYPE_FIXED64: return "fixed64";
102 case FieldDescriptor::TYPE_SFIXED32: return "sfixed32";
103 case FieldDescriptor::TYPE_SFIXED64: return "sfixed64";
104 case FieldDescriptor::TYPE_DOUBLE: return "double";
105 case FieldDescriptor::TYPE_FLOAT: return "float";
106 case FieldDescriptor::TYPE_BOOL: return "bool";
107 case FieldDescriptor::TYPE_ENUM: return "enum";
108 case FieldDescriptor::TYPE_STRING: return "string";
109 case FieldDescriptor::TYPE_BYTES: return "bytes";
110 case FieldDescriptor::TYPE_MESSAGE: return "message";
111 case FieldDescriptor::TYPE_GROUP: return "group";
112 default: assert(false); return "";
113 }
114 }
115
StringifySyntax(FileDescriptor::Syntax syntax)116 std::string StringifySyntax(FileDescriptor::Syntax syntax) {
117 switch (syntax) {
118 case FileDescriptor::SYNTAX_PROTO2:
119 return "proto2";
120 case FileDescriptor::SYNTAX_PROTO3:
121 return "proto3";
122 case FileDescriptor::SYNTAX_UNKNOWN:
123 default:
124 GOOGLE_LOG(FATAL) << "Unsupported syntax; this generator only supports "
125 "proto2 and proto3 syntax.";
126 return "";
127 }
128 }
129
DefaultValueForField(const FieldDescriptor * field)130 std::string DefaultValueForField(const FieldDescriptor* field) {
131 switch(field->cpp_type()) {
132 case FieldDescriptor::CPPTYPE_INT32:
133 return NumberToString(field->default_value_int32());
134 case FieldDescriptor::CPPTYPE_INT64:
135 return NumberToString(field->default_value_int64());
136 case FieldDescriptor::CPPTYPE_UINT32:
137 return NumberToString(field->default_value_uint32());
138 case FieldDescriptor::CPPTYPE_UINT64:
139 return NumberToString(field->default_value_uint64());
140 case FieldDescriptor::CPPTYPE_FLOAT:
141 return NumberToString(field->default_value_float());
142 case FieldDescriptor::CPPTYPE_DOUBLE:
143 return NumberToString(field->default_value_double());
144 case FieldDescriptor::CPPTYPE_BOOL:
145 return field->default_value_bool() ? "true" : "false";
146 case FieldDescriptor::CPPTYPE_ENUM:
147 return NumberToString(field->default_value_enum()->number());
148 case FieldDescriptor::CPPTYPE_STRING: {
149 std::ostringstream os;
150 std::string default_str = field->default_value_string();
151
152 if (field->type() == FieldDescriptor::TYPE_STRING) {
153 os << "\"" << default_str << "\"";
154 } else if (field->type() == FieldDescriptor::TYPE_BYTES) {
155 os << "\"";
156
157 os.fill('0');
158 for (int i = 0; i < default_str.length(); ++i) {
159 // Write the hex form of each byte.
160 os << "\\x" << std::hex << std::setw(2)
161 << ((uint16)((unsigned char)default_str.at(i)));
162 }
163 os << "\".force_encoding(\"ASCII-8BIT\")";
164 }
165
166 return os.str();
167 }
168 default: assert(false); return "";
169 }
170 }
171
GenerateField(const FieldDescriptor * field,io::Printer * printer)172 void GenerateField(const FieldDescriptor* field, io::Printer* printer) {
173 if (field->is_map()) {
174 const FieldDescriptor* key_field =
175 field->message_type()->FindFieldByNumber(1);
176 const FieldDescriptor* value_field =
177 field->message_type()->FindFieldByNumber(2);
178
179 printer->Print(
180 "map :$name$, :$key_type$, :$value_type$, $number$",
181 "name", field->name(),
182 "key_type", TypeName(key_field),
183 "value_type", TypeName(value_field),
184 "number", NumberToString(field->number()));
185
186 if (value_field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
187 printer->Print(
188 ", \"$subtype$\"\n",
189 "subtype", value_field->message_type()->full_name());
190 } else if (value_field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) {
191 printer->Print(
192 ", \"$subtype$\"\n",
193 "subtype", value_field->enum_type()->full_name());
194 } else {
195 printer->Print("\n");
196 }
197 } else {
198
199 printer->Print(
200 "$label$ :$name$, ",
201 "label", LabelForField(field),
202 "name", field->name());
203 printer->Print(
204 ":$type$, $number$",
205 "type", TypeName(field),
206 "number", NumberToString(field->number()));
207
208 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
209 printer->Print(
210 ", \"$subtype$\"",
211 "subtype", field->message_type()->full_name());
212 } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) {
213 printer->Print(
214 ", \"$subtype$\"",
215 "subtype", field->enum_type()->full_name());
216 }
217
218 if (field->has_default_value()) {
219 printer->Print(", default: $default$", "default",
220 DefaultValueForField(field));
221 }
222
223 if (field->has_json_name()) {
224 printer->Print(", json_name: \"$json_name$\"", "json_name",
225 field->json_name());
226 }
227
228 printer->Print("\n");
229 }
230 }
231
GenerateOneof(const OneofDescriptor * oneof,io::Printer * printer)232 void GenerateOneof(const OneofDescriptor* oneof, io::Printer* printer) {
233 printer->Print(
234 "oneof :$name$ do\n",
235 "name", oneof->name());
236 printer->Indent();
237
238 for (int i = 0; i < oneof->field_count(); i++) {
239 const FieldDescriptor* field = oneof->field(i);
240 GenerateField(field, printer);
241 }
242
243 printer->Outdent();
244 printer->Print("end\n");
245 }
246
GenerateMessage(const Descriptor * message,io::Printer * printer,std::string * error)247 bool GenerateMessage(const Descriptor* message, io::Printer* printer,
248 std::string* error) {
249 if (message->extension_range_count() > 0 || message->extension_count() > 0) {
250 GOOGLE_LOG(WARNING) << "Extensions are not yet supported for proto2 .proto files.";
251 }
252
253 // Don't generate MapEntry messages -- we use the Ruby extension's native
254 // support for map fields instead.
255 if (message->options().map_entry()) {
256 return true;
257 }
258
259 printer->Print(
260 "add_message \"$name$\" do\n",
261 "name", message->full_name());
262 printer->Indent();
263
264 for (int i = 0; i < message->field_count(); i++) {
265 const FieldDescriptor* field = message->field(i);
266 if (!field->real_containing_oneof()) {
267 GenerateField(field, printer);
268 }
269 }
270
271 for (int i = 0; i < message->real_oneof_decl_count(); i++) {
272 const OneofDescriptor* oneof = message->oneof_decl(i);
273 GenerateOneof(oneof, printer);
274 }
275
276 printer->Outdent();
277 printer->Print("end\n");
278
279 for (int i = 0; i < message->nested_type_count(); i++) {
280 if (!GenerateMessage(message->nested_type(i), printer, error)) {
281 return false;
282 }
283 }
284 for (int i = 0; i < message->enum_type_count(); i++) {
285 GenerateEnum(message->enum_type(i), printer);
286 }
287
288 return true;
289 }
290
GenerateEnum(const EnumDescriptor * en,io::Printer * printer)291 void GenerateEnum(const EnumDescriptor* en, io::Printer* printer) {
292 printer->Print(
293 "add_enum \"$name$\" do\n",
294 "name", en->full_name());
295 printer->Indent();
296
297 for (int i = 0; i < en->value_count(); i++) {
298 const EnumValueDescriptor* value = en->value(i);
299 printer->Print(
300 "value :$name$, $number$\n",
301 "name", value->name(),
302 "number", NumberToString(value->number()));
303 }
304
305 printer->Outdent();
306 printer->Print(
307 "end\n");
308 }
309
310 // Locale-agnostic utility functions.
IsLower(char ch)311 bool IsLower(char ch) { return ch >= 'a' && ch <= 'z'; }
312
IsUpper(char ch)313 bool IsUpper(char ch) { return ch >= 'A' && ch <= 'Z'; }
314
IsAlpha(char ch)315 bool IsAlpha(char ch) { return IsLower(ch) || IsUpper(ch); }
316
UpperChar(char ch)317 char UpperChar(char ch) { return IsLower(ch) ? (ch - 'a' + 'A') : ch; }
318
319
320 // Package names in protobuf are snake_case by convention, but Ruby module
321 // names must be PascalCased.
322 //
323 // foo_bar_baz -> FooBarBaz
PackageToModule(const std::string & name)324 std::string PackageToModule(const std::string& name) {
325 bool next_upper = true;
326 std::string result;
327 result.reserve(name.size());
328
329 for (int i = 0; i < name.size(); i++) {
330 if (name[i] == '_') {
331 next_upper = true;
332 } else {
333 if (next_upper) {
334 result.push_back(UpperChar(name[i]));
335 } else {
336 result.push_back(name[i]);
337 }
338 next_upper = false;
339 }
340 }
341
342 return result;
343 }
344
345 // Class and enum names in protobuf should be PascalCased by convention, but
346 // since there is nothing enforcing this we need to ensure that they are valid
347 // Ruby constants. That mainly means making sure that the first character is
348 // an upper-case letter.
RubifyConstant(const std::string & name)349 std::string RubifyConstant(const std::string& name) {
350 std::string ret = name;
351 if (!ret.empty()) {
352 if (IsLower(ret[0])) {
353 // If it starts with a lowercase letter, capitalize it.
354 ret[0] = UpperChar(ret[0]);
355 } else if (!IsAlpha(ret[0])) {
356 // Otherwise (e.g. if it begins with an underscore), we need to come up
357 // with some prefix that starts with a capital letter. We could be smarter
358 // here, e.g. try to strip leading underscores, but this may cause other
359 // problems if the user really intended the name. So let's just prepend a
360 // well-known suffix.
361 ret = "PB_" + ret;
362 }
363 }
364
365 return ret;
366 }
367
GenerateMessageAssignment(const std::string & prefix,const Descriptor * message,io::Printer * printer)368 void GenerateMessageAssignment(const std::string& prefix,
369 const Descriptor* message,
370 io::Printer* printer) {
371 // Don't generate MapEntry messages -- we use the Ruby extension's native
372 // support for map fields instead.
373 if (message->options().map_entry()) {
374 return;
375 }
376
377 printer->Print(
378 "$prefix$$name$ = ",
379 "prefix", prefix,
380 "name", RubifyConstant(message->name()));
381 printer->Print(
382 "::Google::Protobuf::DescriptorPool.generated_pool."
383 "lookup(\"$full_name$\").msgclass\n",
384 "full_name", message->full_name());
385
386 std::string nested_prefix = prefix + RubifyConstant(message->name()) + "::";
387 for (int i = 0; i < message->nested_type_count(); i++) {
388 GenerateMessageAssignment(nested_prefix, message->nested_type(i), printer);
389 }
390 for (int i = 0; i < message->enum_type_count(); i++) {
391 GenerateEnumAssignment(nested_prefix, message->enum_type(i), printer);
392 }
393 }
394
GenerateEnumAssignment(const std::string & prefix,const EnumDescriptor * en,io::Printer * printer)395 void GenerateEnumAssignment(const std::string& prefix, const EnumDescriptor* en,
396 io::Printer* printer) {
397 printer->Print(
398 "$prefix$$name$ = ",
399 "prefix", prefix,
400 "name", RubifyConstant(en->name()));
401 printer->Print(
402 "::Google::Protobuf::DescriptorPool.generated_pool."
403 "lookup(\"$full_name$\").enummodule\n",
404 "full_name", en->full_name());
405 }
406
GeneratePackageModules(const FileDescriptor * file,io::Printer * printer)407 int GeneratePackageModules(const FileDescriptor* file, io::Printer* printer) {
408 int levels = 0;
409 bool need_change_to_module = true;
410 std::string package_name;
411
412 // Determine the name to use in either format:
413 // proto package: one.two.three
414 // option ruby_package: One::Two::Three
415 if (file->options().has_ruby_package()) {
416 package_name = file->options().ruby_package();
417
418 // If :: is in the package use the Ruby formatted name as-is
419 // -> A::B::C
420 // otherwise, use the dot separator
421 // -> A.B.C
422 if (package_name.find("::") != std::string::npos) {
423 need_change_to_module = false;
424 } else {
425 GOOGLE_LOG(WARNING) << "ruby_package option should be in the form of:"
426 << " 'A::B::C' and not 'A.B.C'";
427 }
428 } else {
429 package_name = file->package();
430 }
431
432 // Use the appropriate delimiter
433 std::string delimiter = need_change_to_module ? "." : "::";
434 int delimiter_size = need_change_to_module ? 1 : 2;
435
436 // Extract each module name and indent
437 while (!package_name.empty()) {
438 size_t dot_index = package_name.find(delimiter);
439 std::string component;
440 if (dot_index == std::string::npos) {
441 component = package_name;
442 package_name = "";
443 } else {
444 component = package_name.substr(0, dot_index);
445 package_name = package_name.substr(dot_index + delimiter_size);
446 }
447 if (need_change_to_module) {
448 component = PackageToModule(component);
449 }
450 printer->Print(
451 "module $name$\n",
452 "name", component);
453 printer->Indent();
454 levels++;
455 }
456 return levels;
457 }
458
EndPackageModules(int levels,io::Printer * printer)459 void EndPackageModules(int levels, io::Printer* printer) {
460 while (levels > 0) {
461 levels--;
462 printer->Outdent();
463 printer->Print(
464 "end\n");
465 }
466 }
467
UsesTypeFromFile(const Descriptor * message,const FileDescriptor * file,std::string * error)468 bool UsesTypeFromFile(const Descriptor* message, const FileDescriptor* file,
469 std::string* error) {
470 for (int i = 0; i < message->field_count(); i++) {
471 const FieldDescriptor* field = message->field(i);
472 if ((field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
473 field->message_type()->file() == file) ||
474 (field->type() == FieldDescriptor::TYPE_ENUM &&
475 field->enum_type()->file() == file)) {
476 *error = "proto3 message field " + field->full_name() + " in file " +
477 file->name() + " has a dependency on a type from proto2 file " +
478 file->name() +
479 ". Ruby doesn't support proto2 yet, so we must fail.";
480 return true;
481 }
482 }
483
484 for (int i = 0; i < message->nested_type_count(); i++) {
485 if (UsesTypeFromFile(message->nested_type(i), file, error)) {
486 return true;
487 }
488 }
489
490 return false;
491 }
492
GenerateDslDescriptor(const FileDescriptor * file,io::Printer * printer,std::string * error)493 bool GenerateDslDescriptor(const FileDescriptor* file, io::Printer* printer,
494 std::string* error) {
495 printer->Print(
496 "require 'google/protobuf'\n\n");
497 printer->Print("Google::Protobuf::DescriptorPool.generated_pool.build do\n");
498 printer->Indent();
499 printer->Print("add_file(\"$filename$\", :syntax => :$syntax$) do\n",
500 "filename", file->name(), "syntax",
501 StringifySyntax(file->syntax()));
502 printer->Indent();
503 for (int i = 0; i < file->message_type_count(); i++) {
504 if (!GenerateMessage(file->message_type(i), printer, error)) {
505 return false;
506 }
507 }
508 for (int i = 0; i < file->enum_type_count(); i++) {
509 GenerateEnum(file->enum_type(i), printer);
510 }
511 printer->Outdent();
512 printer->Print("end\n");
513 printer->Outdent();
514 printer->Print(
515 "end\n\n");
516 return true;
517 }
518
GenerateBinaryDescriptor(const FileDescriptor * file,io::Printer * printer,std::string * error)519 bool GenerateBinaryDescriptor(const FileDescriptor* file, io::Printer* printer,
520 std::string* error) {
521 printer->Print(
522 R"(descriptor_data = File.binread(__FILE__).split("\n__END__\n", 2)[1])");
523 printer->Print(
524 "\nGoogle::Protobuf::DescriptorPool.generated_pool.add_serialized_file("
525 "descriptor_data)\n\n");
526 return true;
527 }
528
GenerateFile(const FileDescriptor * file,io::Printer * printer,std::string * error)529 bool GenerateFile(const FileDescriptor* file, io::Printer* printer,
530 std::string* error) {
531 printer->Print(
532 "# Generated by the protocol buffer compiler. DO NOT EDIT!\n"
533 "# source: $filename$\n"
534 "\n",
535 "filename", file->name());
536
537 for (int i = 0; i < file->dependency_count(); i++) {
538 printer->Print("require '$name$'\n", "name", GetRequireName(file->dependency(i)->name()));
539 }
540
541 // TODO: Remove this when ruby supports extensions for proto2 syntax.
542 if (file->syntax() == FileDescriptor::SYNTAX_PROTO2 &&
543 file->extension_count() > 0) {
544 GOOGLE_LOG(WARNING) << "Extensions are not yet supported for proto2 .proto files.";
545 }
546
547 bool use_raw_descriptor = file->name() == "google/protobuf/descriptor.proto";
548
549 if (use_raw_descriptor) {
550 GenerateBinaryDescriptor(file, printer, error);
551 } else {
552 GenerateDslDescriptor(file, printer, error);
553 }
554
555 int levels = GeneratePackageModules(file, printer);
556 for (int i = 0; i < file->message_type_count(); i++) {
557 GenerateMessageAssignment("", file->message_type(i), printer);
558 }
559 for (int i = 0; i < file->enum_type_count(); i++) {
560 GenerateEnumAssignment("", file->enum_type(i), printer);
561 }
562 EndPackageModules(levels, printer);
563
564 if (use_raw_descriptor) {
565 printer->Print("\n__END__\n");
566 FileDescriptorProto file_proto;
567 file->CopyTo(&file_proto);
568 std::string file_data;
569 file_proto.SerializeToString(&file_data);
570 printer->Print("$raw_descriptor$", "raw_descriptor", file_data);
571 }
572 return true;
573 }
574
Generate(const FileDescriptor * file,const std::string & parameter,GeneratorContext * generator_context,std::string * error) const575 bool Generator::Generate(
576 const FileDescriptor* file,
577 const std::string& parameter,
578 GeneratorContext* generator_context,
579 std::string* error) const {
580
581 if (file->syntax() != FileDescriptor::SYNTAX_PROTO3 &&
582 file->syntax() != FileDescriptor::SYNTAX_PROTO2) {
583 *error = "Invalid or unsupported proto syntax";
584 return false;
585 }
586
587 std::unique_ptr<io::ZeroCopyOutputStream> output(
588 generator_context->Open(GetOutputFilename(file->name())));
589 io::Printer printer(output.get(), '$');
590
591 return GenerateFile(file, &printer, error);
592 }
593
594 } // namespace ruby
595 } // namespace compiler
596 } // namespace protobuf
597 } // namespace google
598