PageRenderTime 24ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/compiler/as3/as3_file.cc

http://protobuf-actionscript3.googlecode.com/
C++ | 164 lines | 85 code | 22 blank | 57 comment | 5 complexity | 5436252e894d6e35e7b31d988e43b6f2 MD5 | raw file
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. // Author: Robert Blackwood (ported from Kenton's)
  17. // Based on original Protocol Buffers design by
  18. // Sanjay Ghemawat, Jeff Dean, and others.
  19. #include <google/protobuf/compiler/as3/as3_file.h>
  20. #include <google/protobuf/compiler/as3/as3_enum.h>
  21. #include <google/protobuf/compiler/as3/as3_service.h>
  22. #include <google/protobuf/compiler/as3/as3_extension.h>
  23. #include <google/protobuf/compiler/as3/as3_helpers.h>
  24. #include <google/protobuf/compiler/as3/as3_message.h>
  25. #include <google/protobuf/compiler/code_generator.h>
  26. #include <google/protobuf/io/printer.h>
  27. #include <google/protobuf/io/zero_copy_stream.h>
  28. #include <google/protobuf/descriptor.pb.h>
  29. #include <google/protobuf/stubs/strutil.h>
  30. namespace google {
  31. namespace protobuf {
  32. namespace compiler {
  33. namespace as3 {
  34. FileGenerator::FileGenerator(const FileDescriptor* file)
  35. : file_(file),
  36. as3_package_(FileAs3Package(file)),
  37. classname_(FileClassName(file)) {}
  38. FileGenerator::~FileGenerator() {}
  39. bool FileGenerator::Validate(string* error) {
  40. // Check that no class name matches the file's class name. This is a common
  41. // problem that leads to As3 compile errors that can be hard to understand.
  42. // It's especially bad when using the java_multiple_files, since we would
  43. // end up overwriting the outer class with one of the inner ones.
  44. //bool found_conflict = false;
  45. //for (int i = 0; i < file_->enum_type_count() && !found_conflict; i++) {
  46. // if (file_->enum_type(i)->name() == classname_) {
  47. // found_conflict = true;
  48. // }
  49. //}
  50. //for (int i = 0; i < file_->message_type_count() && !found_conflict; i++) {
  51. // if (file_->message_type(i)->name() == classname_) {
  52. // found_conflict = true;
  53. // }
  54. //}
  55. //for (int i = 0; i < file_->service_count() && !found_conflict; i++) {
  56. // if (file_->service(i)->name() == classname_) {
  57. // found_conflict = true;
  58. // }
  59. //}
  60. //if (found_conflict) {
  61. // error->assign(file_->name());
  62. // error->append(
  63. // ": Cannot generate As3 output because the file's outer class name, \"");
  64. // error->append(classname_);
  65. // error->append(
  66. // "\", matches the name of one of the types declared inside it. "
  67. // "Please either rename the type or use the as3_outer_classname "
  68. // "option to specify a different outer class name for the .proto file.");
  69. // return false;
  70. //}
  71. return true;
  72. }
  73. void FileGenerator::Generate(io::Printer* printer,int message_number) {
  74. // We don't import anything because we refer to all classes by their
  75. // fully-qualified names in the generated source.
  76. printer->Print("// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
  77. "\n");
  78. printer->Print("package $package$ {\n"
  79. "\n",
  80. "package", as3_package_);
  81. printer->Indent();
  82. MessageGenerator(file_->message_type(message_number)).Generate(printer);
  83. printer->Outdent();
  84. printer->Print("\n}");
  85. }
  86. template<typename GeneratorClass, typename DescriptorClass>
  87. static void GenerateSibling(const string& package_dir,
  88. const string& as3_package,
  89. const DescriptorClass* descriptor,
  90. OutputDirectory* output_directory,
  91. vector<string>* file_list) {
  92. string filename = package_dir + descriptor->name() + ".as";
  93. file_list->push_back(filename);
  94. scoped_ptr<io::ZeroCopyOutputStream> output(
  95. output_directory->Open(filename));
  96. io::Printer printer(output.get(), '$');
  97. printer.Print("// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
  98. "\n");
  99. printer.Print("package $package$ {\n"
  100. "\n",
  101. "package", as3_package);
  102. printer.Indent();
  103. GeneratorClass(descriptor).Generate(&printer);
  104. printer.Outdent();
  105. printer.Print("\n}");
  106. }
  107. void FileGenerator::GenerateSiblings(const string& package_dir,
  108. OutputDirectory* output_directory,
  109. vector<string>* file_list) {
  110. //if (file_->options().java_multiple_files()) {
  111. for (int i = 0; i < file_->enum_type_count(); i++) {
  112. GenerateSibling<EnumGenerator>(package_dir, as3_package_,
  113. file_->enum_type(i),
  114. output_directory, file_list);
  115. }
  116. for (int i = 0; i < file_->message_type_count(); i++) {
  117. //GenerateSibling<MessageGenerator>(package_dir, as3_package_,
  118. // file_->message_type(i),
  119. // output_directory, file_list);
  120. //This is awesomly bad... supports only 3 levels of nesting :( It's got to be changed to a recursive call
  121. for (int j = 0; j < file_->message_type(i)->nested_type_count(); j++) {
  122. GenerateSibling<MessageGenerator>(package_dir, as3_package_,
  123. file_->message_type(i)->nested_type(j),
  124. output_directory, file_list);
  125. for (int k = 0; k < file_->message_type(i)->nested_type(j)->nested_type_count(); k++) {
  126. GenerateSibling<MessageGenerator>(package_dir, as3_package_,
  127. file_->message_type(i)->nested_type(j)->nested_type(k),
  128. output_directory, file_list);
  129. }
  130. }
  131. }
  132. for (int i = 0; i < file_->service_count(); i++) {
  133. GenerateSibling<ServiceGenerator>(package_dir, as3_package_,
  134. file_->service(i),
  135. output_directory, file_list);
  136. }
  137. // }
  138. }
  139. } // namespace as3
  140. } // namespace compiler
  141. } // namespace protobuf
  142. } // namespace google