Move file accesses for hash files to Coordinator. am: 04dea8d14f am: 55f6d0ff06
am: 645c9beaec

Change-Id: I32f57365af5158d39450d1f307942f830473ca84
diff --git a/AST.cpp b/AST.cpp
index 5548b60..6d26349 100644
--- a/AST.cpp
+++ b/AST.cpp
@@ -35,10 +35,11 @@
 
 namespace android {
 
-AST::AST(const Coordinator* coordinator, const std::string& path)
+AST::AST(const Coordinator* coordinator, const Hash* fileHash)
     : mCoordinator(coordinator),
-      mPath(path),
-      mRootScope("(root scope)", FQName(), Location::startOf(path), nullptr /* parent */) {}
+      mFileHash(fileHash),
+      mRootScope("(root scope)", FQName(), Location::startOf(fileHash->getPath()),
+                 nullptr /* parent */) {}
 
 Scope* AST::getRootScope() {
     return &mRootScope;
@@ -53,8 +54,11 @@
     return mSyntaxErrors;
 }
 
-const std::string &AST::getFilename() const {
-    return mPath;
+const std::string& AST::getFilename() const {
+    return mFileHash->getPath();
+}
+const Hash* AST::getFileHash() const {
+    return mFileHash;
 }
 
 bool AST::setPackage(const char *package) {
diff --git a/AST.h b/AST.h
index 1c5085b..dce89bd 100644
--- a/AST.h
+++ b/AST.h
@@ -19,6 +19,7 @@
 #define AST_H_
 
 #include <android-base/macros.h>
+#include <hidl-hash/Hash.h>
 #include <hidl-util/FQName.h>
 #include <functional>
 #include <map>
@@ -44,7 +45,7 @@
 struct Type;
 
 struct AST {
-    AST(const Coordinator *coordinator, const std::string &path);
+    AST(const Coordinator* coordinator, const Hash* fileHash);
 
     bool setPackage(const char *package);
     bool addImport(const char *import);
@@ -59,7 +60,8 @@
 
     void addScopedType(NamedType* type, Scope* scope);
 
-    const std::string &getFilename() const;
+    const std::string& getFilename() const;
+    const Hash* getFileHash() const;
 
     // Look up local identifier.
     // It could be plain identifier or enum value as described by lookupEnumValue.
@@ -197,7 +199,7 @@
 
    private:
     const Coordinator *mCoordinator;
-    std::string mPath;
+    const Hash* mFileHash;
 
     RootScope mRootScope;
 
diff --git a/Coordinator.cpp b/Coordinator.cpp
index 26067ab..4d133a9 100644
--- a/Coordinator.cpp
+++ b/Coordinator.cpp
@@ -182,7 +182,7 @@
     path.append(fqName.name());
     path.append(".hal");
 
-    AST *ast = new AST(this, path);
+    AST* ast = new AST(this, &Hash::getHash(path));
 
     if (typesAST != NULL) {
         // If types.hal for this AST's package existed, make it's defined
@@ -708,7 +708,7 @@
         return HashStatus::UNFROZEN;
     }
 
-    std::string currentHash = Hash::getHash(ast->getFilename()).hexString();
+    std::string currentHash = ast->getFileHash()->hexString();
 
     if (std::find(frozen.begin(), frozen.end(), currentHash) == frozen.end()) {
         std::cerr << "ERROR: " << fqName.string() << " has hash " << currentHash
diff --git a/Interface.cpp b/Interface.cpp
index d45250b..ee63b0f 100644
--- a/Interface.cpp
+++ b/Interface.cpp
@@ -33,7 +33,6 @@
 #include <unordered_map>
 
 #include <android-base/logging.h>
-#include <hidl-hash/Hash.h>
 #include <hidl-util/Formatter.h>
 #include <hidl-util/StringHelper.h>
 
@@ -70,13 +69,17 @@
 };
 
 Interface::Interface(const char* localName, const FQName& fullName, const Location& location,
-                     Scope* parent, const Reference<Type>& superType)
-    : Scope(localName, fullName, location, parent), mSuperType(superType) {}
+                     Scope* parent, const Reference<Type>& superType, const Hash* fileHash)
+    : Scope(localName, fullName, location, parent), mSuperType(superType), mFileHash(fileHash) {}
 
 std::string Interface::typeName() const {
     return "interface " + localName();
 }
 
+const Hash* Interface::getFileHash() const {
+    return mFileHash;
+}
+
 bool Interface::fillPingMethod(Method *method) const {
     if (method->name() != "ping") {
         return false;
@@ -286,20 +289,21 @@
     return true;
 }
 
-static void emitDigestChain(
+void Interface::emitDigestChain(
     Formatter& out, const std::string& prefix, const std::vector<const Interface*>& chain,
-    std::function<std::string(std::unique_ptr<ConstantExpression>)> byteToString) {
-    out.join(chain.begin(), chain.end(), ",\n", [&] (const auto &iface) {
-        const Hash &hash = Hash::getHash(iface->location().begin().filename());
+    std::function<std::string(std::unique_ptr<ConstantExpression>)> byteToString) const {
+    out.join(chain.begin(), chain.end(), ",\n", [&](const auto& iface) {
         out << prefix;
         out << "{";
-        out.join(hash.raw().begin(), hash.raw().end(), ",", [&](const auto &e) {
-            // Use ConstantExpression::cppValue / javaValue
-            // because Java used signed byte for uint8_t.
-            out << byteToString(ConstantExpression::ValueOf(ScalarType::Kind::KIND_UINT8, e));
-        });
+        out.join(
+            iface->getFileHash()->raw().begin(), iface->getFileHash()->raw().end(), ",",
+            [&](const auto& e) {
+                // Use ConstantExpression::cppValue / javaValue
+                // because Java used signed byte for uint8_t.
+                out << byteToString(ConstantExpression::ValueOf(ScalarType::Kind::KIND_UINT8, e));
+            });
         out << "} /* ";
-        out << hash.hexString();
+        out << iface->getFileHash()->hexString();
         out << " */";
     });
 }
diff --git a/Interface.h b/Interface.h
index a304ae1..becdd16 100644
--- a/Interface.h
+++ b/Interface.h
@@ -20,6 +20,8 @@
 
 #include <vector>
 
+#include <hidl-hash/Hash.h>
+
 #include "Reference.h"
 #include "Scope.h"
 
@@ -35,7 +37,9 @@
     };
 
     Interface(const char* localName, const FQName& fullName, const Location& location,
-              Scope* parent, const Reference<Type>& superType);
+              Scope* parent, const Reference<Type>& superType, const Hash* fileHash);
+
+    const Hash* getFileHash() const;
 
     bool addMethod(Method *method);
     bool addAllReservedMethods();
@@ -136,6 +140,8 @@
     std::vector<Method*> mUserMethods;
     std::vector<Method*> mReservedMethods;
 
+    const Hash* mFileHash;
+
     bool fillPingMethod(Method* method) const;
     bool fillDescriptorChainMethod(Method* method) const;
     bool fillGetDescriptorMethod(Method* method) const;
@@ -147,6 +153,10 @@
     bool fillGetDebugInfoMethod(Method* method) const;
     bool fillDebugMethod(Method* method) const;
 
+    void emitDigestChain(
+        Formatter& out, const std::string& prefix, const std::vector<const Interface*>& chain,
+        std::function<std::string(std::unique_ptr<ConstantExpression>)> byteToString) const;
+
     DISALLOW_COPY_AND_ASSIGN(Interface);
 };
 
diff --git a/hidl-gen_y.yy b/hidl-gen_y.yy
index 825ad90..e49b43c 100644
--- a/hidl-gen_y.yy
+++ b/hidl-gen_y.yy
@@ -662,7 +662,8 @@
           }
 
           Interface* iface = new Interface(
-              $2, ast->makeFullName($2, *scope), convertYYLoc(@2), *scope, *superType);
+              $2, ast->makeFullName($2, *scope), convertYYLoc(@2),
+              *scope, *superType, ast->getFileHash());
 
           enterScope(ast, scope, iface);
       }