Update hidl-gen support for vts.

* Support the new Enum type (based on scalar_data)
* Support sub_struct/sub_union defined within compound type.
* Code cleanup: use getVtsType() instead of hard code ones.

Test: make hidl-gen, locally run make hidl_gen_test.
Bug: 30762234
Change-Id: I9a21b5757e0a9fc6cd1bf829ab123565a7990ad5
diff --git a/ArrayType.cpp b/ArrayType.cpp
index 8294806..ec91531 100644
--- a/ArrayType.cpp
+++ b/ArrayType.cpp
@@ -120,6 +120,10 @@
     return mElementType->getJavaWrapperType();
 }
 
+std::string ArrayType::getVtsType() const {
+    return "TYPE_ARRAY";
+}
+
 void ArrayType::emitReaderWriter(
         Formatter &out,
         const std::string &name,
@@ -478,13 +482,14 @@
 
 status_t ArrayType::emitVtsTypeDeclarations(Formatter &out) const {
     if (mSizes.size() > 1) {
-        // Multi-dimensional arrays are yet to be supported in VTS.
+        CHECK(!"Multi-dimensional arrays are yet to be supported in VTS.");
         return UNKNOWN_ERROR;
     }
 
-    out << "type: TYPE_ARRAY\n" << "vector_value: {\n";
+    out << "type: " << getVtsType() << "\n";
+    out << "vector_value: {\n";
     out.indent();
-    out << "size: " << mSizes[0] << "\n";
+    out << "vector_size: " << mSizes[0] << "\n";
     status_t err = mElementType->emitVtsTypeDeclarations(out);
     if (err != OK) {
         return err;
diff --git a/ArrayType.h b/ArrayType.h
index 878dc51..e8f143a 100644
--- a/ArrayType.h
+++ b/ArrayType.h
@@ -52,6 +52,8 @@
 
     std::string getJavaWrapperType() const override;
 
+    std::string getVtsType() const override;
+
     void emitReaderWriter(
             Formatter &out,
             const std::string &name,
diff --git a/CompoundType.cpp b/CompoundType.cpp
index 400cc19..7c8e04a 100644
--- a/CompoundType.cpp
+++ b/CompoundType.cpp
@@ -88,6 +88,19 @@
     return fullJavaName();
 }
 
+std::string CompoundType::getVtsType() const {
+    switch (mStyle) {
+        case STYLE_STRUCT:
+        {
+            return "TYPE_STRUCT";
+        }
+        case STYLE_UNION:
+        {
+            return "TYPE_UNION";
+        }
+    }
+}
+
 void CompoundType::emitReaderWriter(
         Formatter &out,
         const std::string &name,
@@ -763,36 +776,21 @@
 
 status_t CompoundType::emitVtsTypeDeclarations(Formatter &out) const {
     out << "name: \"" << localName() << "\"\n";
-    switch (mStyle) {
-        case STYLE_STRUCT:
-        {
-            out << "type: TYPE_STRUCT\n";
-            break;
-        }
-        case STYLE_UNION:
-        {
-            out << "type: TYPE_UNION\n";
-            break;
-        }
-        default:
-            break;
-    }
+    out << "type: " << getVtsType() << "\n";
 
     // Emit declaration for each subtype.
     for (const auto &type : getSubTypes()) {
         switch (mStyle) {
             case STYLE_STRUCT:
             {
-                out << "struct_value: {\n";
+                out << "sub_struct: {\n";
                 break;
             }
             case STYLE_UNION:
             {
-                out << "union_value: {\n";
+                out << "sub_union: {\n";
                 break;
             }
-            default:
-                break;
         }
         out.indent();
         status_t status(type->emitVtsTypeDeclarations(out));
@@ -816,8 +814,6 @@
                 out << "union_value: {\n";
                 break;
             }
-            default:
-                break;
         }
         out.indent();
         out << "name: \"" << field->name() << "\"\n";
@@ -833,20 +829,7 @@
 }
 
 status_t CompoundType::emitVtsAttributeType(Formatter &out) const {
-    switch (mStyle) {
-        case STYLE_STRUCT:
-        {
-            out << "type: TYPE_STRUCT\n";
-            break;
-        }
-        case STYLE_UNION:
-        {
-            out << "type: TYPE_UNION\n";
-            break;
-        }
-        default:
-            break;
-    }
+    out << "type: " << getVtsType() << "\n";
     out << "predefined_type: \"" << localName() << "\"\n";
     return OK;
 }
diff --git a/CompoundType.h b/CompoundType.h
index 5cd6efa..3bc3d13 100644
--- a/CompoundType.h
+++ b/CompoundType.h
@@ -45,6 +45,8 @@
     std::string getJavaType(
             std::string *extra, bool forInitializer) const override;
 
+    std::string getVtsType() const override;
+
     void emitReaderWriter(
             Formatter &out,
             const std::string &name,
diff --git a/EnumType.cpp b/EnumType.cpp
index 5a8706b..d5abf6d 100644
--- a/EnumType.cpp
+++ b/EnumType.cpp
@@ -92,6 +92,10 @@
     return mStorageType->resolveToScalarType()->getJavaWrapperType();
 }
 
+std::string EnumType::getVtsType() const {
+    return "TYPE_ENUM";
+}
+
 LocalIdentifier *EnumType::lookupIdentifier(const std::string &name) const {
     std::vector<const EnumType *> chain;
     getTypeChain(&chain);
@@ -232,11 +236,14 @@
 }
 
 status_t EnumType::emitVtsTypeDeclarations(Formatter &out) const {
-    out << "name: \"" << localName() << "\"\n"
-        << "type: TYPE_ENUM\n"
-        << "enum_value: {\n";
+    out << "name: \"" << localName() << "\"\n";
+    out << "type: " << getVtsType() << "\n";
+    out << "enum_value: {\n";
     out.indent();
 
+    out << "scalar_type: \""
+        << mStorageType->resolveToScalarType()->getVtsScalarType()
+        << "\"\n\n";
     std::vector<const EnumType *> chain;
     getTypeChain(&chain);
 
@@ -245,13 +252,19 @@
 
         for (const auto &entry : type->values()) {
             out << "enumerator: \"" << entry->name() << "\"\n";
-
+            out << "scalar_value: {\n";
+            out.indent();
             if (!entry->isAutoFill()) {
                 // don't autofill values for vts.
                 std::string value = entry->value();
                 CHECK(!value.empty());
-                out << "value: " << value << "\n";
+                out << mStorageType->resolveToScalarType()->getVtsScalarType()
+                    << ": "
+                    << value
+                    << "\n";
             }
+            out.unindent();
+            out << "}\n";
         }
     }
 
@@ -261,10 +274,8 @@
 }
 
 status_t EnumType::emitVtsAttributeType(Formatter &out) const {
-    out << "type: TYPE_ENUM\n"
-        << "predefined_type: \""
-        << localName()
-        << "\"\n";
+    out << "type: " << getVtsType() << "\n";
+    out << "predefined_type: \"" << localName() << "\"\n";
     return OK;
 }
 
diff --git a/EnumType.h b/EnumType.h
index 1e6a334..b4d0b15 100644
--- a/EnumType.h
+++ b/EnumType.h
@@ -52,6 +52,8 @@
 
     std::string getJavaWrapperType() const override;
 
+    std::string getVtsType() const override;
+
     void emitReaderWriter(
             Formatter &out,
             const std::string &name,
diff --git a/Interface.cpp b/Interface.cpp
index ba5ea8c..cd9dcd3 100644
--- a/Interface.cpp
+++ b/Interface.cpp
@@ -172,6 +172,10 @@
 
 status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
     for (const auto &type : getSubTypes()) {
+        // Skip for TypeDef as it is just an alias of a defined type.
+        if (type->isTypeDef()) {
+            continue;
+        }
         out << "attribute: {\n";
         out.indent();
         status_t status = type->emitVtsTypeDeclarations(out);
diff --git a/ScalarType.cpp b/ScalarType.cpp
index 9065e13..b89001f 100644
--- a/ScalarType.cpp
+++ b/ScalarType.cpp
@@ -113,6 +113,28 @@
     return kSuffix[mKind];
 }
 
+std::string ScalarType::getVtsType() const {
+    return "TYPE_SCALAR";
+}
+
+std::string ScalarType::getVtsScalarType() const {
+    static const char * const kName[] = {
+            "bool_t",
+            "int8_t",
+            "uint8_t",
+            "int16_t",
+            "uint16_t",
+            "int32_t",
+            "uint32_t",
+            "int64_t",
+            "uint64_t",
+            "float_t",
+            "double_t"
+    };
+
+    return kName[mKind];
+}
+
 void ScalarType::emitReaderWriter(
         Formatter &out,
         const std::string &name,
@@ -211,20 +233,8 @@
 }
 
 status_t ScalarType::emitVtsTypeDeclarations(Formatter &out) const {
-    static const char *const kName[] = {
-            "bool_t",
-            "int8_t",
-            "uint8_t",
-            "int16_t",
-            "uint16_t",
-            "int32_t",
-            "uint32_t",
-            "int64_t",
-            "uint64_t",
-            "float_t",
-            "double_t"
-    };
-    out << "type: TYPE_SCALAR\n"<< "scalar_type: \""<< kName[mKind]<< "\"\n";
+    out << "type: " << getVtsType() << "\n";
+    out << "scalar_type: \"" << getVtsScalarType() << "\"\n";
     return OK;
 }
 
diff --git a/ScalarType.h b/ScalarType.h
index 3dcdae7..135f6fb 100644
--- a/ScalarType.h
+++ b/ScalarType.h
@@ -56,6 +56,9 @@
     std::string getJavaWrapperType() const override;
     std::string getJavaSuffix() const override;
 
+    std::string getVtsType() const override;
+    std::string getVtsScalarType() const;
+
     void emitReaderWriter(
             Formatter &out,
             const std::string &name,
diff --git a/StringType.cpp b/StringType.cpp
index 35caa8b..62fed1c 100644
--- a/StringType.cpp
+++ b/StringType.cpp
@@ -57,6 +57,10 @@
     return "String";
 }
 
+std::string StringType::getVtsType() const {
+    return "TYPE_STRING";
+}
+
 void StringType::emitReaderWriter(
         Formatter &out,
         const std::string &name,
diff --git a/StringType.h b/StringType.h
index 2fb585f..7322554 100644
--- a/StringType.h
+++ b/StringType.h
@@ -37,6 +37,8 @@
 
     std::string getJavaSuffix() const override;
 
+    std::string getVtsType() const override;
+
     void emitReaderWriter(
             Formatter &out,
             const std::string &name,
diff --git a/Type.cpp b/Type.cpp
index db877d4..d81eeef 100644
--- a/Type.cpp
+++ b/Type.cpp
@@ -110,6 +110,11 @@
     return std::string();
 }
 
+std::string Type::getVtsType() const {
+    CHECK(!"Should not be here");
+    return std::string();
+}
+
 void Type::emitReaderWriter(
         Formatter &,
         const std::string &,
diff --git a/Type.h b/Type.h
index 0e0f55e..4156405 100644
--- a/Type.h
+++ b/Type.h
@@ -92,6 +92,8 @@
     virtual std::string getJavaWrapperType() const;
     virtual std::string getJavaSuffix() const;
 
+    virtual std::string getVtsType() const;
+
     enum ErrorMode {
         ErrorMode_Ignore,
         ErrorMode_Goto,
diff --git a/VectorType.cpp b/VectorType.cpp
index 2e7d932..f656281 100644
--- a/VectorType.cpp
+++ b/VectorType.cpp
@@ -65,6 +65,10 @@
     return mElementType->getJavaType(&elementExtra) + elementExtra;
 }
 
+std::string VectorType::getVtsType() const {
+    return "TYPE_VECTOR";
+}
+
 void VectorType::emitReaderWriter(
         Formatter &out,
         const std::string &name,
@@ -540,7 +544,8 @@
 }
 
 status_t VectorType::emitVtsTypeDeclarations(Formatter &out) const {
-    out << "type: TYPE_VECTOR\n" << "vector_value: {\n";
+    out << "type: " << getVtsType() << "\n";
+    out << "vector_value: {\n";
     out.indent();
     status_t err = mElementType->emitVtsTypeDeclarations(out);
     if (err != OK) {
diff --git a/VectorType.h b/VectorType.h
index 0e0a07c..e093777 100644
--- a/VectorType.h
+++ b/VectorType.h
@@ -35,6 +35,8 @@
     std::string getJavaType(
             std::string *extra, bool forInitializer) const override;
 
+    std::string getVtsType() const override;
+
     void emitReaderWriter(
             Formatter &out,
             const std::string &name,
diff --git a/test/data/android/hardware/nfc/1.0/Nfc.vts b/test/data/android/hardware/nfc/1.0/Nfc.vts
index 4dd78f6..03a5464 100644
--- a/test/data/android/hardware/nfc/1.0/Nfc.vts
+++ b/test/data/android/hardware/nfc/1.0/Nfc.vts
@@ -8,76 +8,76 @@
 import: "android.hardware.nfc@1.0::types"
 
 interface: {
-  api: {
-    name: "open"
-    return_type_hidl: {
-      type: TYPE_SCALAR
-      scalar_type: "int32_t"
+    api: {
+        name: "open"
+        return_type_hidl: {
+            type: TYPE_SCALAR
+            scalar_type: "int32_t"
+        }
+        arg: {
+            type: TYPE_HIDL_CALLBACK
+            predefined_type: "INfcClientCallback"
+            is_callback: true
+        }
     }
-    arg: {
-      type: TYPE_HIDL_CALLBACK
-      predefined_type: "INfcClientCallback"
-      is_callback: true
-    }
-  }
 
-  api: {
-    name: "write"
-    return_type_hidl: {
-      type: TYPE_SCALAR
-      scalar_type: "int32_t"
+    api: {
+        name: "write"
+        return_type_hidl: {
+            type: TYPE_SCALAR
+            scalar_type: "int32_t"
+        }
+        arg: {
+            type: TYPE_STRUCT
+            predefined_type: "nfc_data_t"
+        }
     }
-    arg: {
-      type: TYPE_STRUCT
-      predefined_type: "nfc_data_t"
-    }
-  }
 
-  api: {
-    name: "core_initialized"
-    return_type_hidl: {
-      type: TYPE_SCALAR
-      scalar_type: "int32_t"
+    api: {
+        name: "core_initialized"
+        return_type_hidl: {
+            type: TYPE_SCALAR
+            scalar_type: "int32_t"
+        }
+        arg: {
+            type: TYPE_VECTOR
+            vector_value: {
+                type: TYPE_SCALAR
+                scalar_type: "uint8_t"
+            }
+        }
     }
-    arg: {
-      type: TYPE_VECTOR
-      vector_value: {
-        type: TYPE_SCALAR
-        scalar_type: "uint8_t"
-      }
-    }
-  }
 
-  api: {
-    name: "pre_discover"
-    return_type_hidl: {
-      type: TYPE_SCALAR
-      scalar_type: "int32_t"
+    api: {
+        name: "pre_discover"
+        return_type_hidl: {
+            type: TYPE_SCALAR
+            scalar_type: "int32_t"
+        }
     }
-  }
 
-  api: {
-    name: "close"
-    return_type_hidl: {
-      type: TYPE_SCALAR
-      scalar_type: "int32_t"
+    api: {
+        name: "close"
+        return_type_hidl: {
+            type: TYPE_SCALAR
+            scalar_type: "int32_t"
+        }
     }
-  }
 
-  api: {
-    name: "control_granted"
-    return_type_hidl: {
-      type: TYPE_SCALAR
-      scalar_type: "int32_t"
+    api: {
+        name: "control_granted"
+        return_type_hidl: {
+            type: TYPE_SCALAR
+            scalar_type: "int32_t"
+        }
     }
-  }
 
-  api: {
-    name: "power_cycle"
-    return_type_hidl: {
-      type: TYPE_SCALAR
-      scalar_type: "int32_t"
+    api: {
+        name: "power_cycle"
+        return_type_hidl: {
+            type: TYPE_SCALAR
+            scalar_type: "int32_t"
+        }
     }
-  }
 
 }
diff --git a/test/data/android/hardware/nfc/1.0/NfcClientCallback.vts b/test/data/android/hardware/nfc/1.0/NfcClientCallback.vts
index 39d1ef8..d69f26d 100644
--- a/test/data/android/hardware/nfc/1.0/NfcClientCallback.vts
+++ b/test/data/android/hardware/nfc/1.0/NfcClientCallback.vts
@@ -7,24 +7,24 @@
 import: "android.hardware.nfc@1.0::types"
 
 interface: {
-  api: {
-    name: "sendEvent"
-    arg: {
-      type: TYPE_ENUM
-      predefined_type: "nfc_event_t"
+    api: {
+        name: "sendEvent"
+        arg: {
+            type: TYPE_ENUM
+            predefined_type: "nfc_event_t"
+        }
+        arg: {
+            type: TYPE_ENUM
+            predefined_type: "nfc_status_t"
+        }
     }
-    arg: {
-      type: TYPE_ENUM
-      predefined_type: "nfc_status_t"
-    }
-  }
 
-  api: {
-    name: "sendData"
-    arg: {
-      type: TYPE_STRUCT
-      predefined_type: "nfc_data_t"
+    api: {
+        name: "sendData"
+        arg: {
+            type: TYPE_STRUCT
+            predefined_type: "nfc_data_t"
+        }
     }
-  }
 
 }
diff --git a/test/data/android/hardware/nfc/1.0/types.vts b/test/data/android/hardware/nfc/1.0/types.vts
index b8cbb59..3ea29af 100644
--- a/test/data/android/hardware/nfc/1.0/types.vts
+++ b/test/data/android/hardware/nfc/1.0/types.vts
@@ -6,53 +6,81 @@
 
 
 attribute: {
-  name: "nfc_event_t"
-  type: TYPE_ENUM
-  enum_value: {
-    enumerator: "HAL_NFC_OPEN_CPLT_EVT"
-    value: 0
-    enumerator: "HAL_NFC_CLOSE_CPLT_EVT"
-    value: 1
-    enumerator: "HAL_NFC_POST_INIT_CPLT_EVT"
-    value: 2
-    enumerator: "HAL_NFC_PRE_DISCOVER_CPLT_EVT"
-    value: 3
-    enumerator: "HAL_NFC_REQUEST_CONTROL_EVT"
-    value: 4
-    enumerator: "HAL_NFC_RELEASE_CONTROL_EVT"
-    value: 5
-    enumerator: "HAL_NFC_ERROR_EVT"
-    value: 6
-  }
-}
+    name: "nfc_event_t"
+    type: TYPE_ENUM
+    enum_value: {
+        scalar_type: "uint32_t"
 
-attribute: {
-  name: "nfc_status_t"
-  type: TYPE_ENUM
-  enum_value: {
-    enumerator: "HAL_NFC_STATUS_OK"
-    value: 0
-    enumerator: "HAL_NFC_STATUS_FAILED"
-    value: 1
-    enumerator: "HAL_NFC_STATUS_ERR_TRANSPORT"
-    value: 2
-    enumerator: "HAL_NFC_STATUS_ERR_CMD_TIMEOUT"
-    value: 3
-    enumerator: "HAL_NFC_STATUS_REFUSED"
-    value: 4
-  }
-}
-
-attribute: {
-  name: "nfc_data_t"
-  type: TYPE_STRUCT
-  struct_value: {
-    name: "data"
-    type: TYPE_VECTOR
-    vector_value: {
-      type: TYPE_SCALAR
-      scalar_type: "uint8_t"
+        enumerator: "HAL_NFC_OPEN_CPLT_EVT"
+        scalar_value: {
+            uint32_t: 0
+        }
+        enumerator: "HAL_NFC_CLOSE_CPLT_EVT"
+        scalar_value: {
+            uint32_t: 1
+        }
+        enumerator: "HAL_NFC_POST_INIT_CPLT_EVT"
+        scalar_value: {
+            uint32_t: 2
+        }
+        enumerator: "HAL_NFC_PRE_DISCOVER_CPLT_EVT"
+        scalar_value: {
+            uint32_t: 3
+        }
+        enumerator: "HAL_NFC_REQUEST_CONTROL_EVT"
+        scalar_value: {
+            uint32_t: 4
+        }
+        enumerator: "HAL_NFC_RELEASE_CONTROL_EVT"
+        scalar_value: {
+            uint32_t: 5
+        }
+        enumerator: "HAL_NFC_ERROR_EVT"
+        scalar_value: {
+            uint32_t: 6
+        }
     }
-  }
+}
+
+attribute: {
+    name: "nfc_status_t"
+    type: TYPE_ENUM
+    enum_value: {
+        scalar_type: "uint32_t"
+
+        enumerator: "HAL_NFC_STATUS_OK"
+        scalar_value: {
+            uint32_t: 0
+        }
+        enumerator: "HAL_NFC_STATUS_FAILED"
+        scalar_value: {
+            uint32_t: 1
+        }
+        enumerator: "HAL_NFC_STATUS_ERR_TRANSPORT"
+        scalar_value: {
+            uint32_t: 2
+        }
+        enumerator: "HAL_NFC_STATUS_ERR_CMD_TIMEOUT"
+        scalar_value: {
+            uint32_t: 3
+        }
+        enumerator: "HAL_NFC_STATUS_REFUSED"
+        scalar_value: {
+            uint32_t: 4
+        }
+    }
+}
+
+attribute: {
+    name: "nfc_data_t"
+    type: TYPE_STRUCT
+    struct_value: {
+        name: "data"
+        type: TYPE_VECTOR
+        vector_value: {
+            type: TYPE_SCALAR
+            scalar_type: "uint8_t"
+        }
+    }
 }