-Lc++-impl: better namespace handling

Intelligent namespace handling requires much more work
so using Formatter like the rest of hidl-gen.

Test: manual with -Lc++-impl
Fixes: 37294618
Change-Id: I9593ec31b8b8dd19b09750acf10c0330f5628a58
diff --git a/ArrayType.cpp b/ArrayType.cpp
index 8168992..6c639ba 100644
--- a/ArrayType.cpp
+++ b/ArrayType.cpp
@@ -46,10 +46,6 @@
     return mSizes.size();
 }
 
-void ArrayType::addNamedTypesToSet(std::set<const FQName> &set) const {
-    mElementType->addNamedTypesToSet(set);
-}
-
 bool ArrayType::isArray() const {
     return true;
 }
diff --git a/ArrayType.h b/ArrayType.h
index 3bb091f..33f2db6 100644
--- a/ArrayType.h
+++ b/ArrayType.h
@@ -46,8 +46,6 @@
 
     std::string getInternalDataCppType() const;
 
-    void addNamedTypesToSet(std::set<const FQName> &set) const override;
-
     std::string getJavaType(bool forInitializer) const override;
 
     std::string getJavaWrapperType() const override;
diff --git a/CompoundType.cpp b/CompoundType.cpp
index b70c998..154bc67 100644
--- a/CompoundType.cpp
+++ b/CompoundType.cpp
@@ -83,9 +83,8 @@
 
 std::string CompoundType::getCppType(
         StorageMode mode,
-        bool specifyNamespaces) const {
-    const std::string base =
-        specifyNamespaces ? fullName() : partialCppName();
+        bool /* specifyNamespaces */) const {
+    const std::string base = fullName();
 
     switch (mode) {
         case StorageMode_Stack:
diff --git a/DeathRecipientType.cpp b/DeathRecipientType.cpp
index 5ac58fe..4743e4f 100644
--- a/DeathRecipientType.cpp
+++ b/DeathRecipientType.cpp
@@ -23,10 +23,6 @@
 
 DeathRecipientType::DeathRecipientType() {}
 
-void DeathRecipientType::addNamedTypesToSet(std::set<const FQName> &) const {
-    // do nothing
-}
-
 std::string DeathRecipientType::getCppType(StorageMode mode,
                                    bool specifyNamespaces) const {
     const std::string base =
diff --git a/DeathRecipientType.h b/DeathRecipientType.h
index f0e9b6f..70f2c25 100644
--- a/DeathRecipientType.h
+++ b/DeathRecipientType.h
@@ -25,8 +25,6 @@
 struct DeathRecipientType : public Type {
     DeathRecipientType();
 
-    void addNamedTypesToSet(std::set<const FQName> &set) const override;
-
     std::string getCppType(
             StorageMode mode,
             bool specifyNamespaces) const override;
diff --git a/EnumType.cpp b/EnumType.cpp
index 0ef2590..7153696 100644
--- a/EnumType.cpp
+++ b/EnumType.cpp
@@ -83,8 +83,8 @@
 }
 
 std::string EnumType::getCppType(StorageMode,
-                                 bool specifyNamespaces) const {
-    return specifyNamespaces ? fullName() : partialCppName();
+                                 bool /* specifyNamespaces */) const {
+    return fullName();
 }
 
 std::string EnumType::getJavaType(bool forInitializer) const {
@@ -722,9 +722,6 @@
     return "mask" + (mElementType == nullptr ? "" : (" of " + mElementType->typeName()));
 }
 
-void BitFieldType::addNamedTypesToSet(std::set<const FQName> &) const {
-}
-
 bool BitFieldType::isCompatibleElementType(Type *elementType) const {
     return elementType->isEnum();
 }
diff --git a/EnumType.h b/EnumType.h
index b853054..ab9c4a7 100644
--- a/EnumType.h
+++ b/EnumType.h
@@ -148,8 +148,6 @@
 
     bool isBitField() const override;
 
-    void addNamedTypesToSet(std::set<const FQName> &set) const override;
-
     bool isCompatibleElementType(Type *elementType) const override;
 
     bool isElidableType() const override;
diff --git a/FmqType.cpp b/FmqType.cpp
index f9812cf..ccd9ba9 100644
--- a/FmqType.cpp
+++ b/FmqType.cpp
@@ -27,10 +27,6 @@
     : mNamespace(nsp), mName(name) {
 }
 
-void FmqType::addNamedTypesToSet(std::set<const FQName> &) const {
-    // do nothing
-}
-
 std::string FmqType::fullName() const {
     return mNamespace +
             (mNamespace.empty() ? "" : "::") +
diff --git a/FmqType.h b/FmqType.h
index d0ce997..3750497 100644
--- a/FmqType.h
+++ b/FmqType.h
@@ -25,8 +25,6 @@
 struct FmqType : public TemplatedType {
     FmqType(const char *nsp, const char *name);
 
-    void addNamedTypesToSet(std::set<const FQName> &set) const override;
-
     std::string fullName() const;
 
     std::string getCppType(
diff --git a/HandleType.cpp b/HandleType.cpp
index 2bda535..4554d85 100644
--- a/HandleType.cpp
+++ b/HandleType.cpp
@@ -25,10 +25,6 @@
 
 HandleType::HandleType() {}
 
-void HandleType::addNamedTypesToSet(std::set<const FQName> &) const {
-    // do nothing
-}
-
 bool HandleType::isHandle() const {
     return true;
 }
diff --git a/HandleType.h b/HandleType.h
index e206dc0..65ce9a6 100644
--- a/HandleType.h
+++ b/HandleType.h
@@ -27,8 +27,6 @@
 
     bool isHandle() const override;
 
-    void addNamedTypesToSet(std::set<const FQName> &set) const override;
-
     std::string getCppType(
             StorageMode mode,
             bool specifyNamespaces) const override;
diff --git a/Interface.cpp b/Interface.cpp
index a2fa82f..5974288 100644
--- a/Interface.cpp
+++ b/Interface.cpp
@@ -621,7 +621,7 @@
     const std::string base =
           std::string(specifyNamespaces ? "::android::" : "")
         + "sp<"
-        + (specifyNamespaces ? fullName() : partialCppName())
+        + fullName()
         + ">";
 
     switch (mode) {
diff --git a/MemoryType.cpp b/MemoryType.cpp
index f27439b..d6bfdbb 100644
--- a/MemoryType.cpp
+++ b/MemoryType.cpp
@@ -25,10 +25,6 @@
 
 MemoryType::MemoryType() {}
 
-void MemoryType::addNamedTypesToSet(std::set<const FQName> &) const {
-    // do nothing
-}
-
 std::string MemoryType::getCppType(StorageMode mode,
                                    bool specifyNamespaces) const {
     const std::string base =
diff --git a/MemoryType.h b/MemoryType.h
index 42992c2..e95ae2d 100644
--- a/MemoryType.h
+++ b/MemoryType.h
@@ -25,8 +25,6 @@
 struct MemoryType : public Type {
     MemoryType();
 
-    void addNamedTypesToSet(std::set<const FQName> &set) const override;
-
     std::string getCppType(
             StorageMode mode,
             bool specifyNamespaces) const override;
diff --git a/NamedType.cpp b/NamedType.cpp
index cb6556c..027245a 100644
--- a/NamedType.cpp
+++ b/NamedType.cpp
@@ -30,10 +30,6 @@
     mFullName = fullName;
 }
 
-void NamedType::addNamedTypesToSet(std::set<const FQName> &set) const {
-    set.insert(mFullName);
-}
-
 const FQName &NamedType::fqName() const {
     return mFullName;
 }
diff --git a/NamedType.h b/NamedType.h
index 9d88d93..8a1a79e 100644
--- a/NamedType.h
+++ b/NamedType.h
@@ -34,8 +34,6 @@
 
     void setFullName(const FQName &fullName);
 
-    void addNamedTypesToSet(std::set<const FQName> &set) const override;
-
     const FQName &fqName() const;
 
     std::string localName() const;
diff --git a/PointerType.cpp b/PointerType.cpp
index 058141f..53dda83 100644
--- a/PointerType.cpp
+++ b/PointerType.cpp
@@ -31,10 +31,6 @@
     return true;
 }
 
-void PointerType::addNamedTypesToSet(std::set<const FQName> &) const {
-    // do nothing
-}
-
 std::string PointerType::getCppType(StorageMode /* mode */,
                                    bool /* specifyNamespaces */) const {
     return "void*";
diff --git a/PointerType.h b/PointerType.h
index 415ba9f..7232247 100644
--- a/PointerType.h
+++ b/PointerType.h
@@ -29,8 +29,6 @@
 
     bool isElidableType() const override;
 
-    void addNamedTypesToSet(std::set<const FQName> &set) const override;
-
     std::string getCppType(
             StorageMode mode,
             bool specifyNamespaces) const override;
diff --git a/RefType.cpp b/RefType.cpp
index f2a3f7d..b43c832 100644
--- a/RefType.cpp
+++ b/RefType.cpp
@@ -75,10 +75,6 @@
             + " const*";
 }
 
-void RefType::addNamedTypesToSet(std::set<const FQName> &set) const {
-    mElementType->addNamedTypesToSet(set);
-}
-
 void RefType::emitReaderWriter(
         Formatter &,
         const std::string &,
diff --git a/RefType.h b/RefType.h
index 8fd2668..c44b8ad 100644
--- a/RefType.h
+++ b/RefType.h
@@ -28,7 +28,6 @@
     std::string typeName() const override;
     bool isCompatibleElementType(Type *elementType) const override;
 
-    void addNamedTypesToSet(std::set<const FQName> &set) const override;
     std::string getCppType(StorageMode mode,
                            bool specifyNamespaces) const override;
 
diff --git a/ScalarType.cpp b/ScalarType.cpp
index b8bd427..fefd418 100644
--- a/ScalarType.cpp
+++ b/ScalarType.cpp
@@ -33,10 +33,6 @@
     return mKind >= KIND_INT8 && mKind <= KIND_UINT64;
 }
 
-void ScalarType::addNamedTypesToSet(std::set<const FQName> &) const {
-    // do nothing
-}
-
 bool ScalarType::isScalar() const {
     return true;
 }
diff --git a/ScalarType.h b/ScalarType.h
index a8a5f68..737da9a 100644
--- a/ScalarType.h
+++ b/ScalarType.h
@@ -49,8 +49,6 @@
     std::string typeName() const override;
     bool isValidEnumStorageType() const;
 
-    void addNamedTypesToSet(std::set<const FQName> &set) const override;
-
     std::string getCppType(
             StorageMode mode,
             bool specifyNamespaces) const override;
diff --git a/StringType.cpp b/StringType.cpp
index 4051267..4739ace 100644
--- a/StringType.cpp
+++ b/StringType.cpp
@@ -24,10 +24,6 @@
 
 StringType::StringType() {}
 
-void StringType::addNamedTypesToSet(std::set<const FQName> &) const {
-    // do nothing
-}
-
 bool StringType::isString() const {
     return true;
 }
diff --git a/StringType.h b/StringType.h
index 3c1e10a..809b851 100644
--- a/StringType.h
+++ b/StringType.h
@@ -25,8 +25,6 @@
 struct StringType : public Type {
     StringType();
 
-    void addNamedTypesToSet(std::set<const FQName> &set) const override;
-
     bool isString() const override;
 
     bool canCheckEquality() const override;
diff --git a/Type.h b/Type.h
index 80401a1..26eeab0 100644
--- a/Type.h
+++ b/Type.h
@@ -65,6 +65,7 @@
         StorageMode_Result,
     };
 
+    // specifyNamespaces: whether to specify namespaces for built-in types
     virtual std::string getCppType(
             StorageMode mode,
             bool specifyNamespaces) const;
@@ -74,18 +75,6 @@
             StorageMode mode,
             bool specifyNamespaces) const;
 
-    /* gets all hidl-defined types used when this item is
-     * printed using getCppType or getJavaType. Examples:
-     *
-     * vec<vec<vec<IFoo>>>: IFoo is added to the set
-     * (the hypothetical type pair)
-     * pair<IFoo, IBar>: IFoo and IBar are added to the set
-     * int32_t: nothing is added to the set
-     * string: nothing is added to the set
-     * IFoo: IFoo is added to the set
-     */
-    virtual void addNamedTypesToSet(std::set<const FQName> &set) const = 0;
-
     std::string getCppStackType(bool specifyNamespaces = true) const;
 
     std::string getCppResultType(bool specifyNamespaces = true) const;
diff --git a/VectorType.cpp b/VectorType.cpp
index 9bdd91e..16723b2 100644
--- a/VectorType.cpp
+++ b/VectorType.cpp
@@ -66,10 +66,6 @@
     return false;
 }
 
-void VectorType::addNamedTypesToSet(std::set<const FQName> &set) const {
-    mElementType->addNamedTypesToSet(set);
-}
-
 bool VectorType::isVector() const {
     return true;
 }
diff --git a/VectorType.h b/VectorType.h
index 1b3f06d..e868355 100644
--- a/VectorType.h
+++ b/VectorType.h
@@ -32,8 +32,6 @@
 
     bool canCheckEquality() const override;
 
-    void addNamedTypesToSet(std::set<const FQName> &set) const override;
-
     std::string getCppType(
             StorageMode mode,
             bool specifyNamespaces) const override;
diff --git a/generateCppImpl.cpp b/generateCppImpl.cpp
index 95b09a4..874743b 100644
--- a/generateCppImpl.cpp
+++ b/generateCppImpl.cpp
@@ -116,38 +116,6 @@
     enterLeaveNamespace(out, true /* enter */);
     out << "namespace implementation {\n\n";
 
-    // this is namespace aware code and doesn't require post-processing
-    out.setNamespace("");
-
-    std::vector<const Interface *> chain = iface->typeChain();
-
-    std::set<const FQName> usedTypes{};
-
-    for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
-        const Interface *superInterface = *it;
-        superInterface->addNamedTypesToSet(usedTypes);
-    }
-
-    for (const auto &tuple : iface->allMethodsFromRoot()) {
-        const Method *method = tuple.method();
-        for(const auto & arg : method->args()) {
-            arg->type().addNamedTypesToSet(usedTypes);
-        }
-        for(const auto & results : method->results()) {
-            results->type().addNamedTypesToSet(usedTypes);
-        }
-    }
-
-    std::set<const FQName> topLevelTypes{};
-
-    for (const auto &name : usedTypes) {
-        topLevelTypes.insert(name.getTopLevelType());
-    }
-
-    for (const FQName &name : topLevelTypes) {
-        out << "using " << name.cppName() << ";\n";
-    }
-
     out << "using ::android::hardware::hidl_array;\n";
     out << "using ::android::hardware::hidl_memory;\n";
     out << "using ::android::hardware::hidl_string;\n";
@@ -228,9 +196,6 @@
     enterLeaveNamespace(out, true /* enter */);
     out << "namespace implementation {\n\n";
 
-    // this is namespace aware code and doesn't require post-processing
-    out.setNamespace("");
-
     status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
         return generateStubImplMethod(out, baseName, method);
     });