Remove dependency on libutils.

As part of this, TypeDef has also been converted to a NamedType. This is
because originally, Scope contained just a KeyedVector<localname, idx> of
types which it contained, and an std::vector<type> which idx indexes into
(KeyedVector<localname, type> alone would have also worked). However, now
it contains a std::map<localname, idx> instead. Because of this, we have lost
the ability to iterate over the keys (localnames) in O(n). By converting
TypeDef to a NamedType, name => idx => type such that name == type->name.
This also means that in function hierarchy calling Scope::addType, we no
longer have to pass around the tuple (name, type) since type->name == name,
and we can pass around only type.

Change-Id: I8f85afe0e389979a2fd98ff5eeccf47e3fcc8307
diff --git a/AST.cpp b/AST.cpp
index b514fe7..aa27a76 100644
--- a/AST.cpp
+++ b/AST.cpp
@@ -128,12 +128,12 @@
 }
 
 void AST::leaveScope() {
-    mScopePath.pop();
+    mScopePath.pop_back();
 }
 
 Scope *AST::scope() {
     CHECK(!mScopePath.empty());
-    return mScopePath.top();
+    return mScopePath.back();
 }
 
 bool AST::addTypeDef(
@@ -142,29 +142,19 @@
     // emitting any type definitions later on, since this is just an alias
     // to a type defined elsewhere.
     return addScopedTypeInternal(
-            localName, new TypeDef(type), errorMsg, true /* isTypeDef */);
+            new TypeDef(localName, type), errorMsg);
 }
 
 bool AST::addScopedType(NamedType *type, std::string *errorMsg) {
     return addScopedTypeInternal(
-            type->localName().c_str(), type, errorMsg, false /* isTypeDef */);
+            type, errorMsg);
 }
 
 bool AST::addScopedTypeInternal(
-        const char *localName,
-        Type *type,
-        std::string *errorMsg,
-        bool isTypeDef) {
-    if (!isTypeDef) {
-        // Resolve typeDefs to the target type.
-        while (type->isTypeDef()) {
-            type = static_cast<TypeDef *>(type)->referencedType();
-        }
-    }
+        NamedType *type,
+        std::string *errorMsg) {
 
-    // LOG(INFO) << "adding scoped type '" << localName << "'";
-
-    bool success = scope()->addType(localName, type,  errorMsg);
+    bool success = scope()->addType(type, errorMsg);
     if (!success) {
         return false;
     }
@@ -174,18 +164,13 @@
         path.append(mScopePath[i]->localName());
         path.append(".");
     }
-    path.append(localName);
+    path.append(type->localName());
 
     FQName fqName(mPackage.package(), mPackage.version(), path);
 
-    if (!isTypeDef) {
-        CHECK(type->isNamedType());
+    type->setFullName(fqName);
 
-        NamedType *namedType = static_cast<NamedType *>(type);
-        namedType->setFullName(fqName);
-    }
-
-    mDefinedTypesByFullName.add(fqName, type);
+    mDefinedTypesByFullName[fqName] = type;
 
     return true;
 }
@@ -311,7 +296,7 @@
 
             mImportedNames.insert(typesName);
 
-            if (resolvedType->isNamedType()) {
+            if (resolvedType->isNamedType() && !resolvedType->isTypeDef()) {
                 mImportedNamesForJava.insert(
                         static_cast<NamedType *>(resolvedType)->fqName());
             }
@@ -334,12 +319,13 @@
 }
 
 Type *AST::findDefinedType(const FQName &fqName, FQName *matchingName) const {
-    for (size_t i = 0; i < mDefinedTypesByFullName.size(); ++i) {
-        const FQName &key = mDefinedTypesByFullName.keyAt(i);
+    for (const auto &pair : mDefinedTypesByFullName) {
+        const FQName &key = pair.first;
+        Type* type = pair.second;
 
         if (key.endsWith(fqName)) {
             *matchingName = key;
-            return mDefinedTypesByFullName.valueAt(i);
+            return type;
         }
     }
 
@@ -362,10 +348,7 @@
 bool AST::isJavaCompatible() const {
     std::string ifaceName;
     if (!AST::isInterface(&ifaceName)) {
-        for (size_t i = 0; i < mRootScope->countTypes(); ++i) {
-            std::string typeName;
-            const Type *type = mRootScope->typeAt(i, &typeName);
-
+        for (const auto *type : mRootScope->getSubTypes()) {
             if (!type->isJavaCompatible()) {
                 return false;
             }
diff --git a/AST.h b/AST.h
index 3040c22..1360e7e 100644
--- a/AST.h
+++ b/AST.h
@@ -19,10 +19,9 @@
 #define AST_H_
 
 #include <android-base/macros.h>
+#include <map>
 #include <set>
 #include <string>
-#include <utils/KeyedVector.h>
-#include <utils/RefBase.h>
 #include <vector>
 
 #include "FQName.h"
@@ -96,7 +95,7 @@
 private:
     Coordinator *mCoordinator;
     std::string mPath;
-    Vector<Scope *> mScopePath;
+    std::vector<Scope *> mScopePath;
 
     void *mScanner;
     Scope *mRootScope;
@@ -115,13 +114,11 @@
     std::set<AST *> mImportedASTs;
 
     // Types keyed by full names defined in this AST.
-    KeyedVector<FQName, Type *> mDefinedTypesByFullName;
+    std::map<FQName, Type *> mDefinedTypesByFullName;
 
     bool addScopedTypeInternal(
-            const char *localName,
-            Type *type,
-            std::string *errorMsg,
-            bool isTypeDef);
+            NamedType *type,
+            std::string *errorMsg);
 
     // Find a type matching fqName (which may be partial) and if found
     // return the associated type and fill in the full "matchingName".
diff --git a/Android.mk b/Android.mk
index 4596522..4055af8 100644
--- a/Android.mk
+++ b/Android.mk
@@ -26,16 +26,13 @@
 
 include $(CLEAR_VARS)
 LOCAL_MODULE := libhidl-gen
-LOCAL_CFLAGS := -O0 -g -Wno-deprecated-register
+LOCAL_CFLAGS := -O0 -g
 LOCAL_SRC_FILES := $(common_libhidl-gen_src_files)
 LOCAL_SHARED_LIBRARIES :=       \
     libbase                     \
     liblog                      \
     libhidl-gen-utils           \
 
-LOCAL_STATIC_LIBRARIES :=       \
-    libutils                    \
-
 include $(BUILD_SHARED_LIBRARY)
 
 #
@@ -44,16 +41,13 @@
 
 include $(CLEAR_VARS)
 LOCAL_MODULE := libhidl-gen
-LOCAL_CFLAGS := -O0 -g -Wno-deprecated-register
+LOCAL_CFLAGS := -O0 -g
 LOCAL_SRC_FILES := $(common_libhidl-gen_src_files)
 LOCAL_SHARED_LIBRARIES :=       \
     libbase                     \
     liblog                      \
     libhidl-gen-utils           \
 
-LOCAL_STATIC_LIBRARIES :=       \
-    libutils                    \
-
 include $(BUILD_HOST_SHARED_LIBRARY)
 
 include $(CLEAR_VARS)
diff --git a/Annotation.cpp b/Annotation.cpp
index b1d496f..7b63054 100644
--- a/Annotation.cpp
+++ b/Annotation.cpp
@@ -21,9 +21,22 @@
 
 namespace android {
 
+
+AnnotationParam::AnnotationParam(const std::string &name,
+                std::vector<std::string> *values)
+: mName(name), mValues(values) {}
+
+const std::string &AnnotationParam::getName() const {
+    return mName;
+}
+
+const std::vector<std::string> *AnnotationParam::getValues() const {
+    return mValues;
+}
+
 Annotation::Annotation(const char *name,AnnotationParamVector *params)
         : mName(name),
-          mParamsByName(params) {
+          mParams(params) {
 }
 
 std::string Annotation::name() const {
@@ -31,32 +44,44 @@
 }
 
 const AnnotationParamVector &Annotation::params() const {
-    return *mParamsByName;
+    return *mParams;
+}
+
+const AnnotationParam *Annotation::getParam(const std::string &name) {
+    for (auto *i: *mParams) {
+        if (i->getName() == name) {
+            return i;
+        }
+    }
+
+    return nullptr;
 }
 
 void Annotation::dump(Formatter &out) const {
     out << "@" << mName;
 
-    if (mParamsByName->size() == 0) {
+    if (mParams->size() == 0) {
         return;
     }
 
     out << "(";
 
-    for (size_t i = 0; i < mParamsByName->size(); ++i) {
+    for (size_t i = 0; i < mParams->size(); ++i) {
         if (i > 0) {
             out << ", ";
         }
 
-        out << mParamsByName->keyAt(i) << "=";
+        const AnnotationParam* param = mParams->at(i);
 
-        const std::vector<std::string> *param = mParamsByName->valueAt(i);
-        if (param->size() > 1) {
+        out << param->getName() << "=";
+
+        const std::vector<std::string> *values = param->getValues();
+        if (values->size() > 1) {
             out << "{";
         }
 
         bool first = true;
-        for (const auto &value : *param) {
+        for (const auto &value : *values) {
             if (!first) {
                 out << ", ";
             }
@@ -66,7 +91,7 @@
             first = false;
         }
 
-        if (param->size() > 1) {
+        if (values->size() > 1) {
             out << "}";
         }
     }
diff --git a/Annotation.h b/Annotation.h
index f1aaf52..fc9b663 100644
--- a/Annotation.h
+++ b/Annotation.h
@@ -19,27 +19,39 @@
 #define ANNOTATION_H_
 
 #include <android-base/macros.h>
-
+#include <map>
 #include <string>
-#include <utils/KeyedVector.h>
 
 namespace android {
 
 struct Formatter;
-using AnnotationParamVector =
-    DefaultKeyedVector<std::string, std::vector<std::string> *>;
+
+struct AnnotationParam {
+    AnnotationParam(const std::string &name,
+                    std::vector<std::string> *values);
+
+    const std::string &getName() const;
+    const std::vector<std::string> *getValues() const;
+
+private:
+    const std::string mName;
+    std::vector<std::string> *mValues;
+};
+
+using AnnotationParamVector = std::vector<const AnnotationParam*>;
 
 struct Annotation {
     Annotation(const char *name, AnnotationParamVector *params);
 
     std::string name() const;
     const AnnotationParamVector &params() const;
+    const AnnotationParam *getParam(const std::string &name);
 
     void dump(Formatter &out) const;
 
 private:
     std::string mName;
-    AnnotationParamVector *mParamsByName;
+    AnnotationParamVector *mParams;
 
     DISALLOW_COPY_AND_ASSIGN(Annotation);
 };
diff --git a/ConstantExpression.cpp b/ConstantExpression.cpp
index 1dd14ed..9637230 100644
--- a/ConstantExpression.cpp
+++ b/ConstantExpression.cpp
@@ -18,7 +18,6 @@
 
 #include <stdio.h>
 #include <string>
-#include <utils/String8.h>
 #include <android-base/parseint.h>
 #include <android-base/logging.h>
 #include <sstream>
@@ -202,7 +201,7 @@
 
 /* Unary operations. */
 ConstantExpression::ConstantExpression(const char *op, const ConstantExpression *value)
-    : mFormatted(android::String8::format("(%s%s)", op, value->expr()).string()),
+    : mFormatted(std::string("(") + op + value->expr() + ")"),
       mType(kConstExprUnary),
       mValueKind(value->mValueKind) {
   if(value->mType == kConstExprUnknown) {
@@ -217,7 +216,8 @@
 
 /* Binary operations. */
 ConstantExpression::ConstantExpression(const ConstantExpression *lval, const char *op, const ConstantExpression* rval)
-    : mFormatted(android::String8::format("(%s %s %s)", lval->expr(), op, rval->expr()).string()),
+    : mFormatted(std::string("(") + lval->expr() + " "
+                       + op + " " + rval->expr() + ")"),
       mType(kConstExprBinary)
 {
   if(lval->mType == kConstExprUnknown || rval->mType == kConstExprUnknown) {
@@ -277,8 +277,8 @@
 ConstantExpression::ConstantExpression(const ConstantExpression *cond,
                                        const ConstantExpression *trueVal,
                                        const ConstantExpression *falseVal)
-    : mFormatted(android::String8::format("(%s?%s:%s)",
-             cond->expr(), trueVal->expr(), falseVal->expr()).string()),
+    : mFormatted(std::string("(") + cond->expr() + "?" + trueVal->expr()
+                 + ":" + falseVal->expr() + ")"),
       mType(kConstExprTernary) {
   // note: for ?:, unlike arithmetic ops, integral promotion is not necessary.
   mValueKind = usualArithmeticConversion(trueVal->mValueKind,
diff --git a/Coordinator.cpp b/Coordinator.cpp
index 6deca29..1422022 100644
--- a/Coordinator.cpp
+++ b/Coordinator.cpp
@@ -43,11 +43,9 @@
 AST *Coordinator::parse(const FQName &fqName, std::set<AST *> *parsedASTs) {
     CHECK(fqName.isFullyQualified());
 
-    // LOG(INFO) << "parsing " << fqName.string();
-
-    ssize_t index = mCache.indexOfKey(fqName);
-    if (index >= 0) {
-        AST *ast = mCache.valueAt(index);
+    auto it = mCache.find(fqName);
+    if (it != mCache.end()) {
+        AST *ast = (*it).second;
 
         if (ast != nullptr && parsedASTs != nullptr) {
             parsedASTs->insert(ast);
@@ -57,7 +55,7 @@
     }
 
     // Add this to the cache immediately, so we can discover circular imports.
-    mCache.add(fqName, nullptr);
+    mCache[fqName] = nullptr;
 
     AST *typesAST = nullptr;
 
@@ -141,7 +139,7 @@
 
     if (parsedASTs != nullptr) { parsedASTs->insert(ast); }
 
-    mCache.add(fqName, ast);
+    mCache[fqName] = ast;
 
     return ast;
 }
diff --git a/Coordinator.h b/Coordinator.h
index a8f600c..9a12d85 100644
--- a/Coordinator.h
+++ b/Coordinator.h
@@ -20,9 +20,10 @@
 
 #include <android-base/macros.h>
 #include <functional>
+#include <map>
 #include <set>
 #include <string>
-#include <utils/KeyedVector.h>
+#include <utils/Errors.h>
 #include <vector>
 
 namespace android {
@@ -92,7 +93,7 @@
     // "hardware/interfaces".
     std::vector<std::string> mPackageRootPaths;
     std::vector<std::string> mPackageRoots;
-    KeyedVector<FQName, AST *> mCache;
+    std::map<FQName, AST *> mCache;
 
     std::vector<std::string>::const_iterator findPackageRoot(
             const FQName &fqName) const;
diff --git a/Interface.cpp b/Interface.cpp
index a4059d0..66abf8b 100644
--- a/Interface.cpp
+++ b/Interface.cpp
@@ -25,10 +25,11 @@
 namespace android {
 
 Interface::Interface(
-        const char *localName, Interface *super, AnnotationVector *annotations)
+        const char *localName, Interface *super,
+        std::vector<Annotation *> *annotations)
     : Scope(localName),
       mSuperType(super),
-      mAnnotationsByName(annotations),
+      mAnnotations(annotations),
       mIsJavaCompatibleInProgress(false) {
 }
 
@@ -52,8 +53,8 @@
     return mMethods;
 }
 
-const AnnotationVector &Interface::annotations() const {
-    return *mAnnotationsByName;
+const std::vector<Annotation *> &Interface::annotations() const {
+    return *mAnnotations;
 }
 
 std::string Interface::getBaseName() const {
@@ -217,21 +218,21 @@
             out << "}\n";
         }
         // Generate declaration for each annotation.
-        const AnnotationVector & annotations = method->annotations();
-        for (size_t i = 0; i < annotations.size(); i++) {
+        for (const auto &annotation : method->annotations()) {
             out << "callflow: {\n";
             out.indent();
-            std::string name = annotations.keyAt(i);
+            std::string name = annotation->name();
             if (name == "entry") {
                 out << "entry: true\n";
             } else if (name == "exit") {
                 out << "exit: true\n";
             } else if (name == "callflow") {
-                Annotation* annotation = annotations.valueAt(i);
-                std::vector<std::string> * values = annotation->params()
-                        .valueFor("next");
-                for (auto value : *values) {
-                    out << "next: " << value << "\n";
+                const AnnotationParam *param =
+                        annotation->getParam("next");
+                if (param != nullptr) {
+                    for (auto value : *param->getValues()) {
+                        out << "next: " << value << "\n";
+                    }
                 }
             } else {
                 std::cerr << "Invalid annotation '"
diff --git a/Interface.h b/Interface.h
index dc16396..3e2a22a 100644
--- a/Interface.h
+++ b/Interface.h
@@ -18,10 +18,8 @@
 
 #define INTERFACE_H_
 
-#include "Method.h"
 #include "Scope.h"
 
-#include <utils/KeyedVector.h>
 #include <vector>
 
 namespace android {
@@ -33,7 +31,7 @@
     Interface(
             const char *localName,
             Interface *super,
-            AnnotationVector *annotations);
+            std::vector<Annotation *> *annotations);
 
     void addMethod(Method *method);
 
@@ -44,7 +42,7 @@
 
     const std::vector<Method *> &methods() const;
 
-    const AnnotationVector &annotations() const;
+    const std::vector<Annotation *> &annotations() const;
 
     std::string getBaseName() const;
 
@@ -78,7 +76,7 @@
 private:
     Interface *mSuperType;
     std::vector<Method *> mMethods;
-    AnnotationVector *mAnnotationsByName;
+    std::vector<Annotation *> *mAnnotations;
     mutable bool mIsJavaCompatibleInProgress;
 
     DISALLOW_COPY_AND_ASSIGN(Interface);
diff --git a/Method.cpp b/Method.cpp
index d9a6a8c..c051a77 100644
--- a/Method.cpp
+++ b/Method.cpp
@@ -28,12 +28,12 @@
        std::vector<TypedVar *> *args,
        std::vector<TypedVar *> *results,
        bool oneway,
-       AnnotationVector *annotations)
+       std::vector<Annotation *> *annotations)
     : mName(name),
       mArgs(args),
       mResults(results),
       mOneway(oneway),
-      mAnnotationsByName(annotations) {
+      mAnnotations(annotations) {
 }
 
 std::string Method::name() const {
@@ -48,8 +48,8 @@
     return *mResults;
 }
 
-const AnnotationVector &Method::annotations() const {
-    return *mAnnotationsByName;
+const std::vector<Annotation *> &Method::annotations() const {
+    return *mAnnotations;
 }
 
 void Method::generateCppSignature(Formatter &out,
@@ -135,16 +135,16 @@
 }
 
 void Method::dumpAnnotations(Formatter &out) const {
-    if (mAnnotationsByName->size() == 0) {
+    if (mAnnotations->size() == 0) {
         return;
     }
 
     out << "// ";
-    for (size_t i = 0; i < mAnnotationsByName->size(); ++i) {
+    for (size_t i = 0; i < mAnnotations->size(); ++i) {
         if (i > 0) {
             out << " ";
         }
-        mAnnotationsByName->valueAt(i)->dump(out);
+        mAnnotations->at(i)->dump(out);
     }
     out << "\n";
 }
diff --git a/Method.h b/Method.h
index 3faab57..aba7755 100644
--- a/Method.h
+++ b/Method.h
@@ -20,7 +20,6 @@
 
 #include <android-base/macros.h>
 #include <string>
-#include <utils/KeyedVector.h>
 #include <vector>
 
 namespace android {
@@ -31,21 +30,18 @@
 struct Type;
 struct TypedVar;
 
-using AnnotationVector =
-        DefaultKeyedVector<std::string, Annotation *>;
-
 struct Method {
     Method(const char *name,
            std::vector<TypedVar *> *args,
            std::vector<TypedVar *> *results,
            bool oneway,
-           AnnotationVector *annotations);
+           std::vector<Annotation *> *annotations);
 
     std::string name() const;
     const std::vector<TypedVar *> &args() const;
     const std::vector<TypedVar *> &results() const;
     bool isOneway() const { return mOneway; }
-    const AnnotationVector &annotations() const;
+    const std::vector<Annotation *> &annotations() const;
 
     void generateCppSignature(Formatter &out,
                               const std::string &className,
@@ -66,7 +62,7 @@
     std::vector<TypedVar *> *mArgs;
     std::vector<TypedVar *> *mResults;
     bool mOneway;
-    AnnotationVector *mAnnotationsByName;
+    std::vector<Annotation *> *mAnnotations;
 
     DISALLOW_COPY_AND_ASSIGN(Method);
 };
diff --git a/Scope.cpp b/Scope.cpp
index c1de97d..915934c 100644
--- a/Scope.cpp
+++ b/Scope.cpp
@@ -27,8 +27,12 @@
     : NamedType(localName) {
 }
 
-bool Scope::addType(const char *localName, Type *type, std::string *errorMsg) {
-    if (mTypeIndexByName.indexOfKey(localName) >= 0) {
+bool Scope::addType(NamedType *type, std::string *errorMsg) {
+    const std::string &localName = type->localName();
+
+    auto it = mTypeIndexByName.find(localName);
+
+    if (it != mTypeIndexByName.end()) {
         *errorMsg = "A type named '";
         (*errorMsg) += localName;
         (*errorMsg) += "' is already declared in the  current scope.";
@@ -38,16 +42,16 @@
 
     size_t index = mTypes.size();
     mTypes.push_back(type);
-    mTypeIndexByName.add(localName, index);
+    mTypeIndexByName[localName] = index;
 
     return true;
 }
 
-Type *Scope::lookupType(const char *name) const {
-    ssize_t index = mTypeIndexByName.indexOfKey(name);
+NamedType *Scope::lookupType(const char *name) const {
+    auto it = mTypeIndexByName.find(name);
 
-    if (index >= 0) {
-        return mTypes[mTypeIndexByName.valueAt(index)];
+    if (it != mTypeIndexByName.end()) {
+        return mTypes[it->second];
     }
 
     return NULL;
@@ -82,7 +86,9 @@
     for (;;) {
         std::string anonName = "_hidl_Anon_" + std::to_string(sNextID++);
 
-        if (mTypeIndexByName.indexOfKey(anonName) < 0) {
+        auto it = mTypeIndexByName.find(anonName);
+
+        if (it == mTypeIndexByName.end()) {
             return anonName;
         }
     }
@@ -131,7 +137,7 @@
     return OK;
 }
 
-const std::vector<Type *> &Scope::getSubTypes() const {
+const std::vector<NamedType *> &Scope::getSubTypes() const {
     return mTypes;
 }
 
@@ -155,14 +161,5 @@
     return true;
 }
 
-size_t Scope::countTypes() const {
-    return mTypeIndexByName.size();
-}
-
-const Type *Scope::typeAt(size_t index, std::string *name) const {
-    *name = mTypeIndexByName.keyAt(index);
-    return mTypes[mTypeIndexByName.valueAt(index)];
-}
-
 }  // namespace android
 
diff --git a/Scope.h b/Scope.h
index 519e13d..39748be 100644
--- a/Scope.h
+++ b/Scope.h
@@ -20,7 +20,7 @@
 
 #include "NamedType.h"
 
-#include <utils/KeyedVector.h>
+#include <map>
 #include <vector>
 
 namespace android {
@@ -31,9 +31,9 @@
 struct Scope : public NamedType {
     Scope(const char *localName);
 
-    bool addType(const char *localName, Type *type, std::string *errorMsg);
+    bool addType(NamedType *type, std::string *errorMsg);
 
-    Type *lookupType(const char *name) const;
+    NamedType *lookupType(const char *name) const;
 
     bool isScope() const override;
 
@@ -54,18 +54,15 @@
     status_t emitTypeDefinitions(
             Formatter &out, const std::string prefix) const override;
 
-    const std::vector<Type *> &getSubTypes() const;
+    const std::vector<NamedType *> &getSubTypes() const;
 
     status_t emitVtsTypeDeclarations(Formatter &out) const override;
 
     bool isJavaCompatible() const override;
 
-    size_t countTypes() const;
-    const Type *typeAt(size_t index, std::string *name) const;
-
 private:
-    std::vector<Type *> mTypes;
-    KeyedVector<std::string, size_t> mTypeIndexByName;
+    std::vector<NamedType *> mTypes;
+    std::map<std::string, size_t> mTypeIndexByName;
 
     DISALLOW_COPY_AND_ASSIGN(Scope);
 };
diff --git a/TypeDef.cpp b/TypeDef.cpp
index 3f5cbf6..34cd124 100644
--- a/TypeDef.cpp
+++ b/TypeDef.cpp
@@ -21,8 +21,8 @@
 
 namespace android {
 
-TypeDef::TypeDef(Type *type)
-    : Type(),
+TypeDef::TypeDef(const char* localName, Type *type)
+    : NamedType(localName),
       mReferencedType(type) {
 }
 
diff --git a/TypeDef.h b/TypeDef.h
index d71d595..5a59135 100644
--- a/TypeDef.h
+++ b/TypeDef.h
@@ -18,12 +18,12 @@
 
 #define TYPE_DEF_H_
 
-#include "Type.h"
+#include "NamedType.h"
 
 namespace android {
 
-struct TypeDef : public Type {
-    TypeDef(Type *type);
+struct TypeDef : public NamedType {
+    TypeDef(const char* localName, Type *type);
 
     const ScalarType *resolveToScalarType() const override;
 
diff --git a/generateJava.cpp b/generateJava.cpp
index 51c66bc..f9b0cff 100644
--- a/generateJava.cpp
+++ b/generateJava.cpp
@@ -46,9 +46,8 @@
         const std::string &outputPath, const char *limitToType) const {
     // Splits types.hal up into one java file per declared type.
 
-    for (size_t i = 0; i < mRootScope->countTypes(); ++i) {
-        std::string typeName;
-        const Type *type = mRootScope->typeAt(i, &typeName);
+    for (const auto &type : mRootScope->getSubTypes()) {
+        std::string typeName = type->localName();
 
         if (type->isTypeDef()) {
             continue;
diff --git a/hidl-gen_y.yy b/hidl-gen_y.yy
index 698f69d..9ced71f 100644
--- a/hidl-gen_y.yy
+++ b/hidl-gen_y.yy
@@ -30,7 +30,6 @@
 #include "hidl-gen_y.h"
 
 #include <stdio.h>
-#include <utils/String8.h>
 
 using namespace android;
 
@@ -132,10 +131,10 @@
     android::Method *method;
     android::CompoundType::Style compoundStyle;
     std::vector<std::string> *stringVec;
-    std::pair<std::string, std::vector<std::string> *> *annotationParam;
-    android::DefaultKeyedVector<std::string, std::vector<std::string> *> *annotationParams;
+    android::AnnotationParam *annotationParam;
+    android::AnnotationParamVector *annotationParams;
     android::Annotation *annotation;
-    android::DefaultKeyedVector<std::string, android::Annotation *> *annotations;
+    std::vector<android::Annotation *> *annotations;
 }
 
 %%
@@ -143,12 +142,12 @@
 opt_annotations
     : /* empty */
       {
-          $$ = new DefaultKeyedVector<std::string, Annotation *>;
+          $$ = new std::vector<Annotation *>;
       }
     | opt_annotations annotation
       {
           $$ = $1;
-          $$->add($2->name(), $2);
+          $$->push_back($2);
       }
     ;
 
@@ -162,7 +161,7 @@
 opt_annotation_params
     : /* empty */
       {
-          $$ = new DefaultKeyedVector<std::string, std::vector<std::string> *>;
+          $$ = new AnnotationParamVector;
       }
     | '(' annotation_params ')'
       {
@@ -173,20 +172,20 @@
 annotation_params
     : annotation_param
       {
-          $$ = new DefaultKeyedVector<std::string, std::vector<std::string> *>;
-          $$->add($1->first, $1->second);
+          $$ = new AnnotationParamVector;
+          $$->push_back($1);
       }
     | annotation_params ',' annotation_param
       {
           $$ = $1;
-          $$->add($3->first, $3->second);
+          $$->push_back($3);
       }
     ;
 
 annotation_param
     : IDENTIFIER '=' annotation_value
       {
-          $$ = new std::pair<std::string, std::vector<std::string> *>($1, $3);
+          $$ = new AnnotationParam($1, $3);
       }
     ;
 
diff --git a/main.cpp b/main.cpp
index 4e2e2b4..12d3e6a 100644
--- a/main.cpp
+++ b/main.cpp
@@ -228,16 +228,14 @@
             CHECK(typesAST != nullptr);
 
             Scope *rootScope = typesAST->scope();
-            for (size_t i = 0; i < rootScope->countTypes(); ++i) {
-                std::string typeName;
-                rootScope->typeAt(i, &typeName);
 
+            for (const auto &type : rootScope->getSubTypes()) {
                 generateMakefileSectionForLanguageAndType(
                         out,
                         coordinator,
                         packageFQName,
                         fqName,
-                        typeName.c_str(),
+                        type->localName().c_str(),
                         forJava);
             }