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();
 }
diff --git a/Interface.h b/Interface.h
index 1c793e7..7f7edc7 100644
--- a/Interface.h
+++ b/Interface.h
@@ -27,7 +27,7 @@
 struct Interface : public Scope {
     Interface(const char *localName, Interface *super);
 
-    void addMethod(Method *method);
+    bool addMethod(Method *method);
 
     bool isInterface() const override;
     bool isBinder() const override;
@@ -35,6 +35,7 @@
     const Interface *superType() const;
 
     const std::vector<Method *> &methods() const;
+    Method *lookupMethod(std::string name) const;
 
     std::string getBaseName() const;
 
diff --git a/hidl-gen_y.yy b/hidl-gen_y.yy
index b79f195..3e1fb98 100644
--- a/hidl-gen_y.yy
+++ b/hidl-gen_y.yy
@@ -341,7 +341,12 @@
     | interface_declarations method_declaration
       {
           Interface *iface = static_cast<Interface *>(ast->scope());
-          iface->addMethod($2);
+          if (!iface->addMethod($2)) {
+              std::cerr << "ERROR: Unable to add method '" << $2->name()
+                        << "' at " << @2 << "\n";
+
+              YYERROR;
+          }
       }
     ;