hidl-gen: auto-generate depdendencies for nested types

HIDL allows for types to be defined within interfaces, and for these
types to be able to be referred to from outside of these interfaces.
hidl-gen was not emitting the correct #include and import paths in C++
and Java respectively for such cases, and also Makefile dependencies.

b/31268323

Change-Id: I168c52823098348b4161b5d2c151d1d8eadec9b8
Signed-off-by: Iliyan Malchev <malchev@google.com>
diff --git a/AST.cpp b/AST.cpp
index d25e4e1..8fb5ef0 100644
--- a/AST.cpp
+++ b/AST.cpp
@@ -217,6 +217,7 @@
     }
 
     Type *resolvedType = nullptr;
+    Type *returnedType = nullptr;
     FQName resolvedName;
 
     for (const auto &importedAST : mImportedASTs) {
@@ -236,6 +237,7 @@
             }
 
             resolvedType = match;
+            returnedType = resolvedType;
             resolvedName = matchingName;
 
             // Keep going even after finding a match.
@@ -257,6 +259,44 @@
                 static_cast<TypeDef *>(resolvedType)->referencedType();
         }
 
+        returnedType = resolvedType;
+
+        // If the resolved type is not an interface, we need to determine
+        // whether it is defined in types.hal, or in some other interface.  In
+        // the latter case, we need to emit a dependency for the interface in
+        // which the type is defined.
+        //
+        // Consider the following:
+        //    android.hardware.tests.foo@1.0::Record
+        //    android.hardware.tests.foo@1.0::IFoo.Folder
+        //    android.hardware.tests.foo@1.0::Folder
+        //
+        // If Record is an interface, then we keep track of it for the purpose
+        // of emitting dependencies in the target language (for example #include
+        // in C++).  If Record is a UDT, then we assume it is defined in
+        // types.hal in android.hardware.tests.foo@1.0.
+        //
+        // In the case of IFoo.Folder, the same applies.  If IFoo is an
+        // interface, we need to track this for the purpose of emitting
+        // dependencies.  If not, then it must have been defined in types.hal.
+        //
+        // In the case of just specifying Folder, the resolved type is
+        // android.hardware.tests.foo@1.0::IFoo.Folder, and the same logic as
+        // above applies.
+
+        if (!resolvedType->isInterface()) {
+            FQName ifc(resolvedName.package(),
+                       resolvedName.version(),
+                       resolvedName.names().at(0));
+            for (const auto &importedAST : mImportedASTs) {
+                FQName matchingName;
+                Type *match = importedAST->findDefinedType(ifc, &matchingName);
+                if (match != nullptr && match->isInterface()) {
+                    resolvedType = match;
+                }
+            }
+        }
+
         if (!resolvedType->isInterface()) {
             // Non-interface types are declared in the associated types header.
             FQName typesName(
@@ -283,7 +323,7 @@
         }
     }
 
-    return resolvedType->ref();
+    return returnedType->ref();
 }
 
 Type *AST::findDefinedType(const FQName &fqName, FQName *matchingName) const {