• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sstream>
32 
33 #include <google/protobuf/compiler/code_generator.h>
34 #include <google/protobuf/compiler/plugin.h>
35 #include <google/protobuf/descriptor.h>
36 #include <google/protobuf/descriptor.pb.h>
37 #include <google/protobuf/io/printer.h>
38 #include <google/protobuf/io/zero_copy_stream.h>
39 
40 #include <google/protobuf/compiler/ruby/ruby_generator.h>
41 
42 using google::protobuf::internal::scoped_ptr;
43 
44 namespace google {
45 namespace protobuf {
46 namespace compiler {
47 namespace ruby {
48 
49 // Forward decls.
50 std::string IntToString(int32 value);
51 std::string StripDotProto(const std::string& proto_file);
52 std::string LabelForField(google::protobuf::FieldDescriptor* field);
53 std::string TypeName(google::protobuf::FieldDescriptor* field);
54 void GenerateMessage(const google::protobuf::Descriptor* message,
55                      google::protobuf::io::Printer* printer);
56 void GenerateEnum(const google::protobuf::EnumDescriptor* en,
57                   google::protobuf::io::Printer* printer);
58 void GenerateMessageAssignment(
59     const std::string& prefix,
60     const google::protobuf::Descriptor* message,
61     google::protobuf::io::Printer* printer);
62 void GenerateEnumAssignment(
63     const std::string& prefix,
64     const google::protobuf::EnumDescriptor* en,
65     google::protobuf::io::Printer* printer);
66 
IntToString(int32 value)67 std::string IntToString(int32 value) {
68   std::ostringstream os;
69   os << value;
70   return os.str();
71 }
72 
StripDotProto(const std::string & proto_file)73 std::string StripDotProto(const std::string& proto_file) {
74   int lastindex = proto_file.find_last_of(".");
75   return proto_file.substr(0, lastindex);
76 }
77 
GetOutputFilename(const std::string & proto_file)78 std::string GetOutputFilename(const std::string& proto_file) {
79   return StripDotProto(proto_file) + ".rb";
80 }
81 
LabelForField(const google::protobuf::FieldDescriptor * field)82 std::string LabelForField(const google::protobuf::FieldDescriptor* field) {
83   switch (field->label()) {
84     case FieldDescriptor::LABEL_OPTIONAL: return "optional";
85     case FieldDescriptor::LABEL_REQUIRED: return "required";
86     case FieldDescriptor::LABEL_REPEATED: return "repeated";
87     default: assert(false); return "";
88   }
89 }
90 
TypeName(const google::protobuf::FieldDescriptor * field)91 std::string TypeName(const google::protobuf::FieldDescriptor* field) {
92   switch (field->type()) {
93     case FieldDescriptor::TYPE_INT32: return "int32";
94     case FieldDescriptor::TYPE_INT64: return "int64";
95     case FieldDescriptor::TYPE_UINT32: return "uint32";
96     case FieldDescriptor::TYPE_UINT64: return "uint64";
97     case FieldDescriptor::TYPE_SINT32: return "sint32";
98     case FieldDescriptor::TYPE_SINT64: return "sint64";
99     case FieldDescriptor::TYPE_FIXED32: return "fixed32";
100     case FieldDescriptor::TYPE_FIXED64: return "fixed64";
101     case FieldDescriptor::TYPE_SFIXED32: return "sfixed32";
102     case FieldDescriptor::TYPE_SFIXED64: return "sfixed64";
103     case FieldDescriptor::TYPE_DOUBLE: return "double";
104     case FieldDescriptor::TYPE_FLOAT: return "float";
105     case FieldDescriptor::TYPE_BOOL: return "bool";
106     case FieldDescriptor::TYPE_ENUM: return "enum";
107     case FieldDescriptor::TYPE_STRING: return "string";
108     case FieldDescriptor::TYPE_BYTES: return "bytes";
109     case FieldDescriptor::TYPE_MESSAGE: return "message";
110     case FieldDescriptor::TYPE_GROUP: return "group";
111     default: assert(false); return "";
112   }
113 }
114 
GenerateField(const google::protobuf::FieldDescriptor * field,google::protobuf::io::Printer * printer)115 void GenerateField(const google::protobuf::FieldDescriptor* field,
116                    google::protobuf::io::Printer* printer) {
117 
118   if (field->is_map()) {
119     const FieldDescriptor* key_field =
120         field->message_type()->FindFieldByNumber(1);
121     const FieldDescriptor* value_field =
122         field->message_type()->FindFieldByNumber(2);
123 
124     printer->Print(
125       "map :$name$, :$key_type$, :$value_type$, $number$",
126       "name", field->name(),
127       "key_type", TypeName(key_field),
128       "value_type", TypeName(value_field),
129       "number", IntToString(field->number()));
130 
131     if (value_field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
132       printer->Print(
133         ", \"$subtype$\"\n",
134         "subtype", value_field->message_type()->full_name());
135     } else if (value_field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) {
136       printer->Print(
137         ", \"$subtype$\"\n",
138         "subtype", value_field->enum_type()->full_name());
139     } else {
140       printer->Print("\n");
141     }
142   } else {
143 
144     printer->Print(
145       "$label$ :$name$, ",
146       "label", LabelForField(field),
147       "name", field->name());
148     printer->Print(
149       ":$type$, $number$",
150       "type", TypeName(field),
151       "number", IntToString(field->number()));
152 
153     if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
154       printer->Print(
155         ", \"$subtype$\"\n",
156        "subtype", field->message_type()->full_name());
157     } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) {
158       printer->Print(
159         ", \"$subtype$\"\n",
160         "subtype", field->enum_type()->full_name());
161     } else {
162       printer->Print("\n");
163     }
164   }
165 }
166 
GenerateOneof(const google::protobuf::OneofDescriptor * oneof,google::protobuf::io::Printer * printer)167 void GenerateOneof(const google::protobuf::OneofDescriptor* oneof,
168                    google::protobuf::io::Printer* printer) {
169   printer->Print(
170       "oneof :$name$ do\n",
171       "name", oneof->name());
172   printer->Indent();
173 
174   for (int i = 0; i < oneof->field_count(); i++) {
175     const FieldDescriptor* field = oneof->field(i);
176     GenerateField(field, printer);
177   }
178 
179   printer->Outdent();
180   printer->Print("end\n");
181 }
182 
GenerateMessage(const google::protobuf::Descriptor * message,google::protobuf::io::Printer * printer)183 void GenerateMessage(const google::protobuf::Descriptor* message,
184                      google::protobuf::io::Printer* printer) {
185 
186   // Don't generate MapEntry messages -- we use the Ruby extension's native
187   // support for map fields instead.
188   if (message->options().map_entry()) {
189     return;
190   }
191 
192   printer->Print(
193     "add_message \"$name$\" do\n",
194     "name", message->full_name());
195   printer->Indent();
196 
197   for (int i = 0; i < message->field_count(); i++) {
198     const FieldDescriptor* field = message->field(i);
199     if (!field->containing_oneof()) {
200       GenerateField(field, printer);
201     }
202   }
203 
204   for (int i = 0; i < message->oneof_decl_count(); i++) {
205     const OneofDescriptor* oneof = message->oneof_decl(i);
206     GenerateOneof(oneof, printer);
207   }
208 
209   printer->Outdent();
210   printer->Print("end\n");
211 
212   for (int i = 0; i < message->nested_type_count(); i++) {
213     GenerateMessage(message->nested_type(i), printer);
214   }
215   for (int i = 0; i < message->enum_type_count(); i++) {
216     GenerateEnum(message->enum_type(i), printer);
217   }
218 }
219 
GenerateEnum(const google::protobuf::EnumDescriptor * en,google::protobuf::io::Printer * printer)220 void GenerateEnum(const google::protobuf::EnumDescriptor* en,
221                   google::protobuf::io::Printer* printer) {
222   printer->Print(
223     "add_enum \"$name$\" do\n",
224     "name", en->full_name());
225   printer->Indent();
226 
227   for (int i = 0; i < en->value_count(); i++) {
228     const EnumValueDescriptor* value = en->value(i);
229     printer->Print(
230       "value :$name$, $number$\n",
231       "name", value->name(),
232       "number", IntToString(value->number()));
233   }
234 
235   printer->Outdent();
236   printer->Print(
237     "end\n");
238 }
239 
240 // Module names, class names, and enum value names need to be Ruby constants,
241 // which must start with a capital letter.
RubifyConstant(const std::string & name)242 std::string RubifyConstant(const std::string& name) {
243   std::string ret = name;
244   if (!ret.empty()) {
245     if (ret[0] >= 'a' && ret[0] <= 'z') {
246       // If it starts with a lowercase letter, capitalize it.
247       ret[0] = ret[0] - 'a' + 'A';
248     } else if (ret[0] < 'A' || ret[0] > 'Z') {
249       // Otherwise (e.g. if it begins with an underscore), we need to come up
250       // with some prefix that starts with a capital letter. We could be smarter
251       // here, e.g. try to strip leading underscores, but this may cause other
252       // problems if the user really intended the name. So let's just prepend a
253       // well-known suffix.
254       ret = "PB_" + ret;
255     }
256   }
257   return ret;
258 }
259 
GenerateMessageAssignment(const std::string & prefix,const google::protobuf::Descriptor * message,google::protobuf::io::Printer * printer)260 void GenerateMessageAssignment(
261     const std::string& prefix,
262     const google::protobuf::Descriptor* message,
263     google::protobuf::io::Printer* printer) {
264 
265   // Don't generate MapEntry messages -- we use the Ruby extension's native
266   // support for map fields instead.
267   if (message->options().map_entry()) {
268     return;
269   }
270 
271   printer->Print(
272     "$prefix$$name$ = ",
273     "prefix", prefix,
274     "name", RubifyConstant(message->name()));
275   printer->Print(
276     "Google::Protobuf::DescriptorPool.generated_pool."
277     "lookup(\"$full_name$\").msgclass\n",
278     "full_name", message->full_name());
279 
280   std::string nested_prefix = prefix + message->name() + "::";
281   for (int i = 0; i < message->nested_type_count(); i++) {
282     GenerateMessageAssignment(nested_prefix, message->nested_type(i), printer);
283   }
284   for (int i = 0; i < message->enum_type_count(); i++) {
285     GenerateEnumAssignment(nested_prefix, message->enum_type(i), printer);
286   }
287 }
288 
GenerateEnumAssignment(const std::string & prefix,const google::protobuf::EnumDescriptor * en,google::protobuf::io::Printer * printer)289 void GenerateEnumAssignment(
290     const std::string& prefix,
291     const google::protobuf::EnumDescriptor* en,
292     google::protobuf::io::Printer* printer) {
293   printer->Print(
294     "$prefix$$name$ = ",
295     "prefix", prefix,
296     "name", RubifyConstant(en->name()));
297   printer->Print(
298     "Google::Protobuf::DescriptorPool.generated_pool."
299     "lookup(\"$full_name$\").enummodule\n",
300     "full_name", en->full_name());
301 }
302 
GeneratePackageModules(std::string package_name,google::protobuf::io::Printer * printer)303 int GeneratePackageModules(
304     std::string package_name,
305     google::protobuf::io::Printer* printer) {
306   int levels = 0;
307   while (!package_name.empty()) {
308     size_t dot_index = package_name.find(".");
309     string component;
310     if (dot_index == string::npos) {
311       component = package_name;
312       package_name = "";
313     } else {
314       component = package_name.substr(0, dot_index);
315       package_name = package_name.substr(dot_index + 1);
316     }
317     component = RubifyConstant(component);
318     printer->Print(
319       "module $name$\n",
320       "name", component);
321     printer->Indent();
322     levels++;
323   }
324   return levels;
325 }
326 
EndPackageModules(int levels,google::protobuf::io::Printer * printer)327 void EndPackageModules(
328     int levels,
329     google::protobuf::io::Printer* printer) {
330   while (levels > 0) {
331     levels--;
332     printer->Outdent();
333     printer->Print(
334       "end\n");
335   }
336 }
337 
UsesTypeFromFile(const Descriptor * message,const FileDescriptor * file,string * error)338 bool UsesTypeFromFile(const Descriptor* message, const FileDescriptor* file,
339                       string* error) {
340   for (int i = 0; i < message->field_count(); i++) {
341     const FieldDescriptor* field = message->field(i);
342     if ((field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
343          field->message_type()->file() == file) ||
344         (field->type() == FieldDescriptor::TYPE_ENUM &&
345          field->enum_type()->file() == file)) {
346       *error = "proto3 message field " + field->full_name() + " in file " +
347                file->name() + " has a dependency on a type from proto2 file " +
348                file->name() +
349                ".  Ruby doesn't support proto2 yet, so we must fail.";
350       return true;
351     }
352   }
353 
354   for (int i = 0; i < message->nested_type_count(); i++) {
355     if (UsesTypeFromFile(message->nested_type(i), file, error)) {
356       return true;
357     }
358   }
359 
360   return false;
361 }
362 
363 // Ruby doesn't currently support proto2.  This causes a failure even for proto3
364 // files that import proto2.  But in some cases, the proto2 file is only being
365 // imported to extend another proto2 message.  The prime example is declaring
366 // custom options by extending FileOptions/FieldOptions/etc.
367 //
368 // If the proto3 messages don't have any proto2 submessages, it is safe to omit
369 // the dependency completely.  Users won't be able to use any proto2 extensions,
370 // but they already couldn't because proto2 messages aren't supported.
371 //
372 // If/when we add proto2 support, we should remove this.
MaybeEmitDependency(const FileDescriptor * import,const FileDescriptor * from,io::Printer * printer,string * error)373 bool MaybeEmitDependency(const FileDescriptor* import,
374                          const FileDescriptor* from,
375                          io::Printer* printer,
376                          string* error) {
377   if (import->syntax() == FileDescriptor::SYNTAX_PROTO2) {
378     for (int i = 0; i < from->message_type_count(); i++) {
379       if (UsesTypeFromFile(from->message_type(i), import, error)) {
380         // Error text was already set by UsesTypeFromFile().
381         return false;
382       }
383     }
384 
385     // Ok to omit this proto2 dependency -- so we won't print anything.
386     GOOGLE_LOG(WARNING) << "Omitting proto2 dependency '" << import->name()
387                         << "' from proto3 output file '"
388                         << GetOutputFilename(from->name())
389                         << "' because we don't support proto2 and no proto2 "
390                            "types from that file are being used.";
391     return true;
392   } else {
393     printer->Print(
394       "require '$name$'\n", "name", StripDotProto(import->name()));
395     return true;
396   }
397 }
398 
GenerateFile(const FileDescriptor * file,io::Printer * printer,string * error)399 bool GenerateFile(const FileDescriptor* file, io::Printer* printer,
400                   string* error) {
401   printer->Print(
402     "# Generated by the protocol buffer compiler.  DO NOT EDIT!\n"
403     "# source: $filename$\n"
404     "\n",
405     "filename", file->name());
406 
407   printer->Print(
408     "require 'google/protobuf'\n\n");
409 
410   for (int i = 0; i < file->dependency_count(); i++) {
411     if (!MaybeEmitDependency(file->dependency(i), file, printer, error)) {
412       return false;
413     }
414   }
415 
416   printer->Print(
417     "Google::Protobuf::DescriptorPool.generated_pool.build do\n");
418   printer->Indent();
419   for (int i = 0; i < file->message_type_count(); i++) {
420     GenerateMessage(file->message_type(i), printer);
421   }
422   for (int i = 0; i < file->enum_type_count(); i++) {
423     GenerateEnum(file->enum_type(i), printer);
424   }
425   printer->Outdent();
426   printer->Print(
427     "end\n\n");
428 
429   int levels = GeneratePackageModules(file->package(), printer);
430   for (int i = 0; i < file->message_type_count(); i++) {
431     GenerateMessageAssignment("", file->message_type(i), printer);
432   }
433   for (int i = 0; i < file->enum_type_count(); i++) {
434     GenerateEnumAssignment("", file->enum_type(i), printer);
435   }
436   EndPackageModules(levels, printer);
437   return true;
438 }
439 
Generate(const FileDescriptor * file,const string & parameter,GeneratorContext * generator_context,string * error) const440 bool Generator::Generate(
441     const FileDescriptor* file,
442     const string& parameter,
443     GeneratorContext* generator_context,
444     string* error) const {
445 
446   if (file->syntax() != FileDescriptor::SYNTAX_PROTO3) {
447     *error =
448         "Can only generate Ruby code for proto3 .proto files.\n"
449         "Please add 'syntax = \"proto3\";' to the top of your .proto file.\n";
450     return false;
451   }
452 
453   scoped_ptr<io::ZeroCopyOutputStream> output(
454       generator_context->Open(GetOutputFilename(file->name())));
455   io::Printer printer(output.get(), '$');
456 
457   return GenerateFile(file, &printer, error);
458 }
459 
460 }  // namespace ruby
461 }  // namespace compiler
462 }  // namespace protobuf
463 }  // namespace google
464