Emit error if function is overloaded.

Test: check error is reported correclty, hidl_test
Bug: 31758541

Change-Id: If82cb3fbd8c068c6e25cbc131a8102e24cf83988
diff --git a/Interface.cpp b/Interface.cpp
index 4d22c5c..5951f87 100644
--- a/Interface.cpp
+++ b/Interface.cpp
@@ -19,6 +19,7 @@
 #include "Annotation.h"
 #include "Method.h"
 
+#include <android-base/logging.h>
 #include <hidl-util/Formatter.h>
 #include <iostream>
 
@@ -30,7 +31,12 @@
       mIsJavaCompatibleInProgress(false) {
 }
 
-void Interface::addMethod(Method *method) {
+bool Interface::addMethod(Method *method) {
+    if (lookupMethod(method->name()) != nullptr) {
+        LOG(ERROR) << "Redefinition of method " << method->name();
+        return false;
+    }
+
     /* It is very important that these values NEVER change. These values
      * must remain unchanged over the lifetime of android. This is
      * because the framework on a device will be updated independently of
@@ -41,17 +47,16 @@
      */
     size_t serial = 1; // hardware::IBinder::FIRST_CALL_TRANSACTION;
 
-    serial += methods().size();
-
-    const Interface *ancestor = mSuperType;
+    const Interface *ancestor = this;
     while (ancestor != nullptr) {
         serial += ancestor->methods().size();
         ancestor = ancestor->superType();
     }
 
     method->setSerialId(serial);
-
     mMethods.push_back(method);
+
+    return true;
 }
 
 const Interface *Interface::superType() const {
@@ -70,6 +75,20 @@
     return mMethods;
 }
 
+Method *Interface::lookupMethod(std::string name) const {
+    const Interface *ancestor = this;
+    while (ancestor != nullptr) {
+        for (const auto &method : mMethods) {
+            if (method->name() == name) {
+                return method;
+            }
+        }
+        ancestor = ancestor->superType();
+    }
+
+    return nullptr;
+}
+
 std::string Interface::getBaseName() const {
     return fqName().getInterfaceBaseName();
 }