move read/writeEmbedded[References]From/ToParcel out

from hidl_string, hidl_vec, and structs.

* Add a hwtypes.h in the autogenerated files, which
  contains the read/writeEmbeddedFrom/ToParcel
  methods for structs defined in types.h.

* Fixes the hack that remove the warnings when compiling
  the generated code (useParentInEmitResolveReferencesEmbedded())
  and add one more hack (useNameInEmitReaderWriterEmbedded())

* Some clean-up on Scope.cpp (add a forEachType function)

Test: mma
Test: hidl_test

Bug: 32756130

Change-Id: Icfd116b5d92fef78d257337c3f2ef02071f7600b
diff --git a/generateCpp.cpp b/generateCpp.cpp
index c14119a..b659f7b 100644
--- a/generateCpp.cpp
+++ b/generateCpp.cpp
@@ -313,22 +313,25 @@
 
 status_t AST::generateHwBinderHeader(const std::string &outputPath) const {
     std::string ifaceName;
-    if(!AST::isInterface(&ifaceName)) {
-        // types.hal does not get an HwBinder header.
-        return OK;
+    bool isInterface = AST::isInterface(&ifaceName);
+    const Interface *iface = nullptr;
+    std::string baseName{};
+    std::string klassName{};
+
+    if(isInterface) {
+        iface = mRootScope->getInterface();
+        baseName = iface->getBaseName();
+        klassName = "IHw" + baseName;
+    } else {
+        klassName = "hwtypes";
     }
 
-    const Interface *iface = mRootScope->getInterface();
-    const std::string baseName = iface->getBaseName();
-
-    const std::string klassName = "IHw" + baseName;
-
     std::string path = outputPath;
     path.append(mCoordinator->convertPackageRootToPath(mPackage));
     path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
     path.append(klassName + ".h");
 
-    FILE* file = fopen(path.c_str(), "w");
+    FILE *file = fopen(path.c_str(), "w");
 
     if (file == NULL) {
         return -errno;
@@ -341,16 +344,20 @@
     out << "#ifndef " << guard << "\n";
     out << "#define " << guard << "\n\n";
 
-    generateCppPackageInclude(out, mPackage, ifaceName);
+    if (isInterface) {
+        generateCppPackageInclude(out, mPackage, ifaceName);
+    } else {
+        generateCppPackageInclude(out, mPackage, "types");
+    }
 
     out << "\n";
 
     for (const auto &item : mImportedNames) {
         if (item.name() == "types") {
-            continue;
+            generateCppPackageInclude(out, item, "hwtypes");
+        } else {
+            generateCppPackageInclude(out, item, "Bn" + item.getInterfaceBaseName());
         }
-
-        generateCppPackageInclude(out, item, "Bn" + item.getInterfaceBaseName());
     }
 
     out << "\n";
@@ -363,26 +370,34 @@
     out << "\n";
 
     enterLeaveNamespace(out, true /* enter */);
-    out << "\n";
 
-    out << "struct "
-        << klassName
-        << " : public "
-        << ifaceName;
+    if (isInterface) {
+        out << "\n";
 
-    const Interface *superType = iface->superType();
+        out << "struct "
+            << klassName
+            << " : public "
+            << ifaceName;
 
-    out << ", public ::android::hardware::IInterface";
+        const Interface *superType = iface->superType();
 
-    out << " {\n";
+        out << ", public ::android::hardware::IInterface";
 
-    out.indent();
+        out << " {\n";
 
-    out << "DECLARE_HWBINDER_META_INTERFACE(" << baseName << ");\n\n";
+        out.indent();
 
-    out.unindent();
+        out << "DECLARE_HWBINDER_META_INTERFACE(" << baseName << ");\n\n";
 
-    out << "};\n\n";
+        out.unindent();
+
+        out << "};\n\n";
+    }
+
+    status_t err = mRootScope->emitGlobalHwDeclarations(out);
+    if (err != OK) {
+        return err;
+    }
 
     enterLeaveNamespace(out, false /* enter */);
 
@@ -740,6 +755,7 @@
         }
     } else {
         generateCppPackageInclude(out, mPackage, "types");
+        generateCppPackageInclude(out, mPackage, "hwtypes");
     }
 
     out << "\n";