build a structure around the VTS compiler

this uses a generator class which can generate some part of
C/C++ header or main body code of HAL and HAL_SUBMODULE for now.
that'll be extended to support HAL_BINDER in the future.
So this architecture generally improves the maintainability.

Change-Id: I4548b5d1abbfac015f36600d8d94c18534da0e3e
(cherry picked from commit b8edda3f3df6daf9b13e600a038fbb3a639dc86e)
diff --git a/sysfuzzer/vtscompiler/code_gen/CodeGenBase.cpp b/sysfuzzer/vtscompiler/code_gen/CodeGenBase.cpp
new file mode 100644
index 0000000..fb95eeb
--- /dev/null
+++ b/sysfuzzer/vtscompiler/code_gen/CodeGenBase.cpp
@@ -0,0 +1,135 @@
+/*
+ * Copyright 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "code_gen/CodeGenBase.h"
+
+#include <fstream>
+#include <iostream>
+#include <sstream>
+#include <string>
+
+#include "utils/InterfaceSpecUtil.h"
+
+#include "VtsCompilerUtils.h"
+
+using namespace std;
+
+namespace android {
+namespace vts {
+
+CodeGenBase::CodeGenBase(const char* input_vts_file_path,
+                         const char* vts_name)
+    : input_vts_file_path_(input_vts_file_path),
+      vts_name_(vts_name) {}
+
+
+CodeGenBase::~CodeGenBase() {}
+
+
+void CodeGenBase::GenerateAll(std::stringstream& cpp_ss,
+                              std::stringstream& h_ss,
+                              const InterfaceSpecificationMessage& message) {
+  cpp_ss << "#include \"" << string(input_vts_file_path_) << ".h\"" << endl;
+
+  cpp_ss << "#include <iostream>" << endl;
+  cpp_ss << "#include \"vts_datatype.h\"" << endl;
+  for (auto const& header : message.header()) {
+    cpp_ss << "#include " << header << endl;
+  }
+  GenerateOpenNameSpaces(cpp_ss);
+
+  string component_name = message.original_data_structure_name();
+  while (!component_name.empty()
+         && (std::isspace(component_name.back())
+             || component_name.back() == '*' )) {
+    component_name.pop_back();
+  }
+  const auto pos = component_name.find_last_of(" ");
+  if (pos != std::string::npos) {
+    component_name = component_name.substr(pos + 1);
+  }
+
+  string fuzzer_extended_class_name;
+  if (message.component_class() == HAL
+      || message.component_class() == HAL_SUBMODULE) {
+    fuzzer_extended_class_name = "FuzzerExtended_" + component_name;
+  }
+
+  h_ss << "#ifndef __VTS_SPEC_" << vts_name_ << "__" << endl;
+  h_ss << "#define __VTS_SPEC_" << vts_name_ << "__" << endl;
+  h_ss << endl;
+  h_ss << "#define LOG_TAG \"" << fuzzer_extended_class_name << "\"" << endl;
+  h_ss << "#include <utils/Log.h>" << endl;
+  h_ss << "#include \"common/fuzz_tester/FuzzerBase.h\"" << endl;
+  for (auto const& header : message.header()) {
+    h_ss << "#include " << header << endl;
+  }
+  h_ss << "\n\n" << endl;
+  GenerateOpenNameSpaces(h_ss);
+  h_ss << "class " << fuzzer_extended_class_name << " : public FuzzerBase {"
+      << endl;
+  h_ss << " protected:" << endl;
+  h_ss << "  bool Fuzz(const FunctionSpecificationMessage& func_msg," << endl;
+  h_ss << "            void** result);" << endl;
+  if (message.component_class() == HAL_SUBMODULE) {
+    h_ss << "  void SetSubModule(" << component_name << "* submodule) {" << endl;
+    h_ss << "    submodule_ = submodule;" << endl;
+    h_ss << "  }" << endl;
+    h_ss << endl;
+    h_ss << " private:" << endl;
+    h_ss << "  " << message.original_data_structure_name() << "* submodule_;"
+        << endl;
+  }
+  h_ss << "};" << endl;
+
+  string function_name_prefix = GetFunctionNamePrefix(message);
+
+  cpp_ss << endl;
+  GenerateCppBodyFuzzFunction(cpp_ss, message, fuzzer_extended_class_name);
+
+  std::stringstream ss;
+  // return type
+  h_ss << endl;
+  ss << "android::vts::FuzzerBase* " << endl;
+  // function name
+  ss << function_name_prefix << "(" << endl;
+  ss << ")";
+
+  GenerateHeaderGlobalFunctionDeclarations(h_ss, ss.str());
+
+  GenerateCloseNameSpaces(cpp_ss);
+  cpp_ss << endl << endl;
+
+  GenerateCppBodyGlobalFunctions(cpp_ss, ss.str(), fuzzer_extended_class_name);
+
+  GenerateCloseNameSpaces(h_ss);
+  h_ss << "#endif" << endl;
+}
+
+
+void CodeGenBase::GenerateOpenNameSpaces(std::stringstream& ss) {
+  ss << "namespace android {" << endl;
+  ss << "namespace vts {" << endl;
+}
+
+
+void CodeGenBase::GenerateCloseNameSpaces(std::stringstream& ss) {
+  ss << "}  // namespace vts" << endl;
+  ss << "}  // namespace android" << endl;
+}
+
+}  // namespace vts
+}  // namespace android
diff --git a/sysfuzzer/vtscompiler/code_gen/CodeGenBase.h b/sysfuzzer/vtscompiler/code_gen/CodeGenBase.h
new file mode 100644
index 0000000..f1a966b
--- /dev/null
+++ b/sysfuzzer/vtscompiler/code_gen/CodeGenBase.h
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __VTS_SYSFUZZER_COMPILER_CODEGENBASE_H__
+#define __VTS_SYSFUZZER_COMPILER_CODEGENBASE_H__
+
+#include <fstream>
+#include <iostream>
+#include <sstream>
+#include <string>
+
+#include "test/vts/sysfuzzer/common/proto/InterfaceSpecificationMessage.pb.h"
+
+using namespace std;
+
+namespace android {
+namespace vts {
+
+class CodeGenBase {
+ public:
+  explicit CodeGenBase(const char* input_vts_file_path, const char* vts_name);
+  virtual ~CodeGenBase();
+
+  // Generate both a C/C++ file and its header file.
+  void GenerateAll(std::stringstream& cpp_ss,
+                   std::stringstream& h_ss,
+                   const InterfaceSpecificationMessage& message);
+
+ protected:
+  // Generates code for Fuzz(...) function body.
+  virtual void GenerateCppBodyFuzzFunction(
+      std::stringstream& cpp_ss,
+      const InterfaceSpecificationMessage& message,
+      const string& fuzzer_extended_class_name) = 0;
+
+  // Generates header code to declare the C/C++ global functions.
+  virtual void GenerateHeaderGlobalFunctionDeclarations(
+      std::stringstream& h_ss,
+      const string& function_prototype) = 0;
+
+  // Generates code for the bodies of the C/C++ global functions.
+  virtual void GenerateCppBodyGlobalFunctions(
+      std::stringstream& cpp_ss,
+      const string& function_prototype,
+      const string& fuzzer_extended_class_name) = 0;
+
+  // Generates code that opens the default namespaces.
+  void GenerateOpenNameSpaces(std::stringstream& ss);
+
+  // Generates code that closes the default namespaces.
+  void GenerateCloseNameSpaces(std::stringstream& ss);
+
+ private:
+  const char* input_vts_file_path_;
+  const char* vts_name_;
+};
+
+}  // namespace vts
+}  // namespace android
+
+#endif  // __VTS_SYSFUZZER_COMPILER_CODEGENBASE_H__
diff --git a/sysfuzzer/vtscompiler/code_gen/HalCodeGen.cpp b/sysfuzzer/vtscompiler/code_gen/HalCodeGen.cpp
new file mode 100644
index 0000000..f71ec53
--- /dev/null
+++ b/sysfuzzer/vtscompiler/code_gen/HalCodeGen.cpp
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "code_gen/HalCodeGen.h"
+
+#include <fstream>
+#include <iostream>
+#include <sstream>
+#include <string>
+
+#include "test/vts/sysfuzzer/common/proto/InterfaceSpecificationMessage.pb.h"
+
+#include "VtsCompilerUtils.h"
+
+using namespace std;
+using namespace android;
+
+namespace android {
+namespace vts {
+
+const char* const HalCodeGen::kInstanceVariableName = "device_";
+
+
+void HalCodeGen::GenerateCppBodyFuzzFunction(
+    std::stringstream& cpp_ss,
+    const InterfaceSpecificationMessage& message,
+    const string& fuzzer_extended_class_name) {
+  cpp_ss << "bool " << fuzzer_extended_class_name << "::Fuzz(" << endl;
+  cpp_ss << "    const FunctionSpecificationMessage& func_msg," << endl;
+  cpp_ss << "    void** result) {" << endl;
+  cpp_ss << "  const char* func_name = func_msg.name().c_str();" << endl;
+  cpp_ss << "  cout << \"Function: \" << func_name << endl;" << endl;
+
+  for (auto const& api : message.api()) {
+    std::stringstream ss;
+
+    cpp_ss << "  if (!strcmp(func_name, \"" << api.name() << "\")) {" << endl;
+
+    // args - definition;
+    int arg_count = 0;
+    for (auto const& arg : api.arg()) {
+      cpp_ss << "    " << GetCppVariableType(arg) << " ";
+      cpp_ss << "arg" << arg_count << " = ";
+      if (arg_count == 0
+          && arg.has_aggregate_type()
+          && !strncmp(arg.aggregate_type().c_str(),
+                      message.original_data_structure_name().c_str(),
+                      message.original_data_structure_name().length())) {
+        cpp_ss << "reinterpret_cast<" << GetCppVariableType(arg) << ">("
+            << kInstanceVariableName << ")";
+      } else {
+        cpp_ss << GetCppInstanceType(arg);
+      }
+      cpp_ss << ";" << endl;
+      cpp_ss << "    cout << \"arg" << arg_count << " = \" << arg" << arg_count
+          << " << endl;" << endl;
+      arg_count++;
+    }
+
+    // actual function call
+    cpp_ss << "    ";
+    if (api.return_type().has_primitive_type()
+        && !strcmp(api.return_type().primitive_type().c_str(), "void")) {
+      cpp_ss << "*result = NULL;" << endl;
+    } else {
+      cpp_ss << "*result = const_cast<void*>(reinterpret_cast<const void*>(";
+    }
+    cpp_ss << "reinterpret_cast<" << message.original_data_structure_name()
+        << "*>(" << kInstanceVariableName << ")->" << api.name() << "(";
+    if (arg_count > 0) cpp_ss << endl;
+
+    for (int index = 0; index < arg_count; index++) {
+      cpp_ss << "      arg" << index;
+      if (index != (arg_count - 1)) {
+        cpp_ss << "," << endl;
+      }
+    }
+    if (!api.return_type().has_primitive_type()
+        || strcmp(api.return_type().primitive_type().c_str(), "void")) {
+      cpp_ss << "))";
+    }
+    cpp_ss << ");" << endl;
+
+    cpp_ss << "    return true;" << endl;
+    cpp_ss << "  }" << endl;
+  }
+  // TODO: if there were pointers, free them.
+  cpp_ss << "  return false;" << endl;
+  cpp_ss << "}" << endl;
+}
+
+
+void HalCodeGen::GenerateHeaderGlobalFunctionDeclarations(
+    std::stringstream& h_ss,
+    const string& function_prototype) {
+  h_ss << "extern \"C\" {" << endl;
+  h_ss << "extern " << function_prototype << ";" << endl;
+  h_ss << "}" << endl;
+}
+
+
+void HalCodeGen::GenerateCppBodyGlobalFunctions(
+    std::stringstream& cpp_ss,
+    const string& function_prototype,
+    const string& fuzzer_extended_class_name) {
+  cpp_ss << "extern \"C\" {" << endl;
+  cpp_ss << function_prototype << " {" << endl;
+  cpp_ss << "  return (android::vts::FuzzerBase*) "
+      << "new android::vts::" << fuzzer_extended_class_name << "();" << endl;
+  cpp_ss << "}" << endl << endl;
+  cpp_ss << "}" << endl;
+}
+
+}  // namespace vts
+}  // namespace android
diff --git a/sysfuzzer/vtscompiler/code_gen/HalCodeGen.h b/sysfuzzer/vtscompiler/code_gen/HalCodeGen.h
new file mode 100644
index 0000000..172b31c
--- /dev/null
+++ b/sysfuzzer/vtscompiler/code_gen/HalCodeGen.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __VTS_SYSFUZZER_COMPILER_HALCODEGEN_H__
+#define __VTS_SYSFUZZER_COMPILER_HALCODEGEN_H__
+
+#include <fstream>
+#include <iostream>
+#include <sstream>
+#include <string>
+
+#include "test/vts/sysfuzzer/common/proto/InterfaceSpecificationMessage.pb.h"
+
+#include "code_gen/CodeGenBase.h"
+
+using namespace std;
+
+namespace android {
+namespace vts {
+
+class HalCodeGen : public CodeGenBase {
+ public:
+  HalCodeGen(const char* input_vts_file_path, const char* vts_name)
+      : CodeGenBase(input_vts_file_path, vts_name) {}
+
+ protected:
+  void GenerateCppBodyFuzzFunction(
+      std::stringstream& cpp_ss,
+      const InterfaceSpecificationMessage& message,
+      const string& fuzzer_extended_class_name);
+
+  void GenerateHeaderGlobalFunctionDeclarations(
+      std::stringstream& h_ss,
+      const string& function_prototype);
+
+  void GenerateCppBodyGlobalFunctions(
+      std::stringstream& cpp_ss,
+      const string& function_prototype,
+      const string& fuzzer_extended_class_name);
+
+  // instance variable name (e.g., device_);
+  static const char* const kInstanceVariableName;
+};
+
+}  // namespace vts
+}  // namespace android
+
+#endif  // __VTS_SYSFUZZER_COMPILER_HALCODEGEN_H__
diff --git a/sysfuzzer/vtscompiler/code_gen/HalSubmoduleCodeGen.cpp b/sysfuzzer/vtscompiler/code_gen/HalSubmoduleCodeGen.cpp
new file mode 100644
index 0000000..a2421bb
--- /dev/null
+++ b/sysfuzzer/vtscompiler/code_gen/HalSubmoduleCodeGen.cpp
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "code_gen/HalSubmoduleCodeGen.h"
+
+#include <fstream>
+#include <iostream>
+#include <sstream>
+#include <string>
+
+#include "test/vts/sysfuzzer/common/proto/InterfaceSpecificationMessage.pb.h"
+
+#include "VtsCompilerUtils.h"
+
+using namespace std;
+using namespace android;
+
+namespace android {
+namespace vts {
+
+const char* const HalSubmoduleCodeGen::kInstanceVariableName = "submodule_";
+
+
+void HalSubmoduleCodeGen::GenerateHeaderGlobalFunctionDeclarations(
+    std::stringstream& /*h_ss*/,
+    const string& /*function_prototype*/) {}
+
+}  // namespace vts
+}  // namespace android
diff --git a/sysfuzzer/vtscompiler/code_gen/HalSubmoduleCodeGen.h b/sysfuzzer/vtscompiler/code_gen/HalSubmoduleCodeGen.h
new file mode 100644
index 0000000..7231112
--- /dev/null
+++ b/sysfuzzer/vtscompiler/code_gen/HalSubmoduleCodeGen.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __VTS_SYSFUZZER_COMPILER_HALSUBMODULECODEGEN_H__
+#define __VTS_SYSFUZZER_COMPILER_HALSUBMODULECODEGEN_H__
+
+#include <fstream>
+#include <iostream>
+#include <sstream>
+#include <string>
+
+#include "test/vts/sysfuzzer/common/proto/InterfaceSpecificationMessage.pb.h"
+
+#include "code_gen/CodeGenBase.h"
+#include "code_gen/HalCodeGen.h"
+
+using namespace std;
+
+namespace android {
+namespace vts {
+
+class HalSubmoduleCodeGen : public HalCodeGen {
+ public:
+  HalSubmoduleCodeGen(const char* input_vts_file_path, const char* vts_name)
+      : HalCodeGen(input_vts_file_path, vts_name) {}
+
+ protected:
+  void GenerateHeaderGlobalFunctionDeclarations(
+      std::stringstream& h_ss,
+      const string& function_prototype);
+
+  // instance variable name (e.g., submodule_);
+  static const char* const kInstanceVariableName;
+};
+
+}  // namespace vts
+}  // namespace android
+
+#endif  // __VTS_SYSFUZZER_COMPILER_HALSUBMODULECODEGEN_H__