Remove CompatibilityMatrix::getInstances/hasInstance

... by moving it from HalGroup to HalManifest. These APIs
should not be used by CompatibilityMatrix, becuase it doesn't
hold a list of exact instance names.

Bug: 73738616
Test: libvintf_test
Test: vintf_object_test
Test: vts_treble_vintf_test

Change-Id: I0ffc8de86ec3766630426288b28f6fafcda1730e
Merged-In: I0ffc8de86ec3766630426288b28f6fafcda1730e
diff --git a/CompatibilityMatrix.cpp b/CompatibilityMatrix.cpp
index 965923d..840899d 100644
--- a/CompatibilityMatrix.cpp
+++ b/CompatibilityMatrix.cpp
@@ -331,5 +331,17 @@
     return true;
 }
 
+bool CompatibilityMatrix::matchInstance(const std::string& halName, const Version& version,
+                                        const std::string& interfaceName,
+                                        const std::string& instance) const {
+    bool found = false;
+    (void)forEachInstanceOfInterface(halName, version, interfaceName,
+                                     [&found, &instance](const auto& e) {
+                                         found |= (e.matchInstance(instance));
+                                         return !found;  // if not found, continue
+                                     });
+    return found;
+}
+
 } // namespace vintf
 } // namespace android
diff --git a/HalManifest.cpp b/HalManifest.cpp
index a884b1f..d1230cb 100644
--- a/HalManifest.cpp
+++ b/HalManifest.cpp
@@ -218,8 +218,8 @@
 
     forEachInstance([&ret, &mat](const auto& manifestInstance) {
         const auto& fqInstance = manifestInstance.getFqInstance();
-        if (!mat.hasInstance(fqInstance.getPackage(), fqInstance.getVersion(),
-                             fqInstance.getInterface(), fqInstance.getInstance())) {
+        if (!mat.matchInstance(fqInstance.getPackage(), fqInstance.getVersion(),
+                               fqInstance.getInterface(), fqInstance.getInstance())) {
             ret.insert(fqInstance.string());
         }
         return true;
@@ -423,5 +423,28 @@
                 lft.framework.mSystemSdk == rgt.framework.mSystemSdk));
 }
 
+// Alternative to forEachInstance if you just need a set of instance names instead.
+std::set<std::string> HalManifest::getInstances(const std::string& halName, const Version& version,
+                                                const std::string& interfaceName) const {
+    std::set<std::string> ret;
+    (void)forEachInstanceOfInterface(halName, version, interfaceName, [&ret](const auto& e) {
+        ret.insert(e.instance());
+        return true;
+    });
+    return ret;
+}
+
+// Return whether instance is in getInstances(...).
+bool HalManifest::hasInstance(const std::string& halName, const Version& version,
+                              const std::string& interfaceName, const std::string& instance) const {
+    bool found = false;
+    (void)forEachInstanceOfInterface(halName, version, interfaceName,
+                                     [&found, &instance](const auto& e) {
+                                         found |= (instance == e.instance());
+                                         return !found;  // if not found, continue
+                                     });
+    return found;
+}
+
 } // namespace vintf
 } // namespace android
diff --git a/MatrixInstance.cpp b/MatrixInstance.cpp
index dd007dd..caf6861 100644
--- a/MatrixInstance.cpp
+++ b/MatrixInstance.cpp
@@ -54,10 +54,6 @@
     return mFqInstance.getInterface();
 }
 
-const std::string& MatrixInstance::instance() const {
-    return mFqInstance.getInstance();
-}
-
 bool MatrixInstance::optional() const {
     return mOptional;
 }
diff --git a/include/vintf/CompatibilityMatrix.h b/include/vintf/CompatibilityMatrix.h
index 02326ae..a7b8af4 100644
--- a/include/vintf/CompatibilityMatrix.h
+++ b/include/vintf/CompatibilityMatrix.h
@@ -94,6 +94,11 @@
     MatrixHal* splitInstance(MatrixHal* existingHal, const std::string& interface,
                              const std::string& instance);
 
+    // Return whether instance is in "this"; that is, instance is in any <instance> tag or
+    // matches any <regex-instance> tag.
+    bool matchInstance(const std::string& halName, const Version& version,
+                       const std::string& interfaceName, const std::string& instance) const;
+
     friend struct HalManifest;
     friend struct RuntimeInfo;
     friend struct CompatibilityMatrixConverter;
diff --git a/include/vintf/HalGroup.h b/include/vintf/HalGroup.h
index 2a1e1c3..f59e495 100644
--- a/include/vintf/HalGroup.h
+++ b/include/vintf/HalGroup.h
@@ -135,29 +135,6 @@
         return v;
     }
 
-    // Alternative to forEachInstance if you just need a set of instance names instead.
-    std::set<std::string> getInstances(const std::string& halName, const Version& version,
-                                       const std::string& interfaceName) const {
-        std::set<std::string> ret;
-        (void)forEachInstanceOfInterface(halName, version, interfaceName, [&ret](const auto& e) {
-            ret.insert(e.instance());
-            return true;
-        });
-        return ret;
-    }
-
-    // Return whether instance is in getInstances(...).
-    bool hasInstance(const std::string& halName, const Version& version,
-                     const std::string& interfaceName, const std::string& instance) const {
-        bool found = false;
-        (void)forEachInstanceOfInterface(halName, version, interfaceName,
-                                         [&found, &instance](const auto& e) {
-                                             found |= (instance == e.instance());
-                                             return !found;  // if not found, continue
-                                         });
-        return found;
-    }
-
    protected:
     // sorted map from component name to the component.
     // The component name looks like: android.hardware.foo
diff --git a/include/vintf/HalManifest.h b/include/vintf/HalManifest.h
index a5d01c8..7a436a6 100644
--- a/include/vintf/HalManifest.h
+++ b/include/vintf/HalManifest.h
@@ -111,6 +111,14 @@
         const std::string& package, const Version& expectVersion,
         const std::function<bool(const ManifestInstance&)>& func) const override;
 
+    // Alternative to forEachInstance if you just need a set of instance names instead.
+    std::set<std::string> getInstances(const std::string& halName, const Version& version,
+                                       const std::string& interfaceName) const;
+
+    // Return whether instance is in getInstances(...).
+    bool hasInstance(const std::string& halName, const Version& version,
+                     const std::string& interfaceName, const std::string& instance) const;
+
    protected:
     // Check before add()
     bool shouldAdd(const ManifestHal& toAdd) const override;
diff --git a/include/vintf/MatrixInstance.h b/include/vintf/MatrixInstance.h
index 41e48f0..c34500c 100644
--- a/include/vintf/MatrixInstance.h
+++ b/include/vintf/MatrixInstance.h
@@ -42,8 +42,6 @@
     const std::string& package() const;
     const VersionRange& versionRange() const;
     const std::string& interface() const;
-    // deprecated; use regexPattern or exactInstance instead.
-    const std::string& instance() const;
     bool optional() const;
 
     bool isSatisfiedBy(const FqInstance& provided) const;
diff --git a/main.cpp b/main.cpp
index 1c3dfef..c501bf7 100644
--- a/main.cpp
+++ b/main.cpp
@@ -203,7 +203,9 @@
              minorVer <= matrixInstance.versionRange().maxMinor; ++minorVer) {
             std::string key = toFQNameString(
                 matrixInstance.package(), Version{matrixInstance.versionRange().majorVer, minorVer},
-                matrixInstance.interface(), matrixInstance.instance());
+                matrixInstance.interface(),
+                matrixInstance.isRegex() ? matrixInstance.regexPattern()
+                                         : matrixInstance.exactInstance());
             auto it = table->find(key);
             if (it == table->end()) {
                 mutate(&(*table)[key]);