Remove static gAllReservedMethods from Interface.cpp

This was a global static that was used to determine all the methods in
IBase so that they could be added to all inheriting children. This
removes that global static and adds it as a member in AST.

Bison now calls into AST to add a method to an interface, which the AST
uses to store the reserved methods (similar to how gAllReservedMethods
did). The call to addAllReservedMethods also goes through AST which
finds all the reserved methods by checking all its importedAST (one of
which is guaranteed to have the methods).

Bug: N/A
Test: ./test/run_all_host_tests.sh && ./test/run_all_device_tests.sh
Change-Id: I47c592d724047c8e78e7b978b46837b39ffe4221
diff --git a/AST.cpp b/AST.cpp
index b05c458..07f50be 100644
--- a/AST.cpp
+++ b/AST.cpp
@@ -22,6 +22,7 @@
 #include "HandleType.h"
 #include "Interface.h"
 #include "Location.h"
+#include "Method.h"
 #include "Scope.h"
 #include "TypeDef.h"
 
@@ -30,8 +31,11 @@
 #include <hidl-util/Formatter.h>
 #include <hidl-util/StringHelper.h>
 #include <stdlib.h>
+
 #include <algorithm>
 #include <iostream>
+#include <map>
+#include <string>
 
 namespace android {
 
@@ -845,4 +849,32 @@
             });
 }
 
+bool AST::addMethod(Method* method, Interface* iface) {
+    if (iface->isIBase()) {
+        if (!mAllReservedMethods.emplace(method->name(), method).second) {
+            std::cerr << "ERROR: hidl-gen encountered duplicated reserved method " << method->name()
+                      << std::endl;
+            return false;
+        }
+
+        // methods will be added to iface in addAllReservedMethodsToInterface
+        return true;
+    }
+
+    iface->addUserDefinedMethod(method);
+
+    return true;
+}
+
+bool AST::addAllReservedMethodsToInterface(Interface* iface) {
+    std::map<std::string, Method*> allReservedMethods(mAllReservedMethods);
+    // Looking for the IBase AST which is imported for all interfaces that are not IBase
+    for (const AST* importedAST : mImportedASTs) {
+        allReservedMethods.insert(importedAST->mAllReservedMethods.begin(),
+                                  importedAST->mAllReservedMethods.end());
+    }
+
+    return iface->addAllReservedMethods(allReservedMethods);
+}
+
 }  // namespace android;