Switch ResourceName to use ResourceNamedType instead of ResourceType.

DD: go/custom-resource-types-in-aapt2

Bug: b/215108200
Test: Resource_test.cpp
Change-Id: I0b97fc0024523700e01adce788bb934d388da288
diff --git a/tools/aapt2/Debug.cpp b/tools/aapt2/Debug.cpp
index 7103944..f47d66e 100644
--- a/tools/aapt2/Debug.cpp
+++ b/tools/aapt2/Debug.cpp
@@ -80,7 +80,7 @@
           printer_->Print(parent_name.package);
           printer_->Print(":");
         }
-        printer_->Print(to_string(parent_name.type));
+        printer_->Print(parent_name.type.to_string());
         printer_->Print("/");
         printer_->Print(parent_name.entry);
         if (parent_ref.id) {
diff --git a/tools/aapt2/Resource.h b/tools/aapt2/Resource.h
index d394b24..b41d851 100644
--- a/tools/aapt2/Resource.h
+++ b/tools/aapt2/Resource.h
@@ -128,10 +128,12 @@
  */
 struct ResourceName {
   std::string package;
-  ResourceType type = ResourceType::kRaw;
+  ResourceNamedType type;
   std::string entry;
 
   ResourceName() = default;
+  ResourceName(const android::StringPiece& p, const ResourceNamedTypeRef& t,
+               const android::StringPiece& e);
   ResourceName(const android::StringPiece& p, ResourceType t, const android::StringPiece& e);
 
   int compare(const ResourceName& other) const;
@@ -148,13 +150,15 @@
  */
 struct ResourceNameRef {
   android::StringPiece package;
-  ResourceType type = ResourceType::kRaw;
+  ResourceNamedTypeRef type;
   android::StringPiece entry;
 
   ResourceNameRef() = default;
   ResourceNameRef(const ResourceNameRef&) = default;
   ResourceNameRef(ResourceNameRef&&) = default;
   ResourceNameRef(const ResourceName& rhs);  // NOLINT(google-explicit-constructor)
+  ResourceNameRef(const android::StringPiece& p, const ResourceNamedTypeRef& t,
+                  const android::StringPiece& e);
   ResourceNameRef(const android::StringPiece& p, ResourceType t, const android::StringPiece& e);
   ResourceNameRef& operator=(const ResourceNameRef& rhs) = default;
   ResourceNameRef& operator=(ResourceNameRef&& rhs) = default;
@@ -418,14 +422,20 @@
 // ResourceName implementation.
 //
 
+inline ResourceName::ResourceName(const android::StringPiece& p, const ResourceNamedTypeRef& t,
+                                  const android::StringPiece& e)
+    : package(p.to_string()), type(t.ToResourceNamedType()), entry(e.to_string()) {
+}
+
 inline ResourceName::ResourceName(const android::StringPiece& p, ResourceType t,
                                   const android::StringPiece& e)
-    : package(p.to_string()), type(t), entry(e.to_string()) {}
+    : ResourceName(p, ResourceNamedTypeWithDefaultName(t), e) {
+}
 
 inline int ResourceName::compare(const ResourceName& other) const {
   int cmp = package.compare(other.package);
   if (cmp != 0) return cmp;
-  cmp = static_cast<int>(type) - static_cast<int>(other.type);
+  cmp = type.compare(other.type);
   if (cmp != 0) return cmp;
   cmp = entry.compare(other.entry);
   return cmp;
@@ -461,9 +471,16 @@
 inline ResourceNameRef::ResourceNameRef(const ResourceName& rhs)
     : package(rhs.package), type(rhs.type), entry(rhs.entry) {}
 
+inline ResourceNameRef::ResourceNameRef(const android::StringPiece& p,
+                                        const ResourceNamedTypeRef& t,
+                                        const android::StringPiece& e)
+    : package(p), type(t), entry(e) {
+}
+
 inline ResourceNameRef::ResourceNameRef(const android::StringPiece& p, ResourceType t,
                                         const android::StringPiece& e)
-    : package(p), type(t), entry(e) {}
+    : ResourceNameRef(p, ResourceNamedTypeWithDefaultName(t), e) {
+}
 
 inline ResourceNameRef& ResourceNameRef::operator=(const ResourceName& rhs) {
   package = rhs.package;
@@ -520,7 +537,7 @@
   size_t operator()(const aapt::ResourceName& name) const {
     android::hash_t h = 0;
     h = android::JenkinsHashMix(h, static_cast<uint32_t>(hash<string>()(name.package)));
-    h = android::JenkinsHashMix(h, static_cast<uint32_t>(name.type));
+    h = android::JenkinsHashMix(h, static_cast<uint32_t>(hash<string>()(name.type.name)));
     h = android::JenkinsHashMix(h, static_cast<uint32_t>(hash<string>()(name.entry)));
     return static_cast<size_t>(h);
   }
diff --git a/tools/aapt2/ResourceParser.cpp b/tools/aapt2/ResourceParser.cpp
index 792a306..42715f9 100644
--- a/tools/aapt2/ResourceParser.cpp
+++ b/tools/aapt2/ResourceParser.cpp
@@ -604,7 +604,8 @@
       return false;
     }
 
-    out_resource->name.type = ResourceType::kId;
+    out_resource->name.type =
+        ResourceNamedTypeWithDefaultName(ResourceType::kId).ToResourceNamedType();
     out_resource->name.entry = maybe_name.value().to_string();
 
     // Ids either represent a unique resource id or reference another resource id
@@ -623,7 +624,7 @@
         // A null reference also means there is no inner element when ids are in the form:
         //    <id name="name"/>
         out_resource->value = util::make_unique<Id>();
-      } else if (!ref || ref->name.value().type != ResourceType::kId) {
+      } else if (!ref || ref->name.value().type.type != ResourceType::kId) {
         // If an inner element exists, the inner element must be a reference to another resource id
         diag_->Error(DiagMessage(out_resource->source)
                          << "<" << parser->element_name()
@@ -640,7 +641,8 @@
       return false;
     }
 
-    out_resource->name.type = ResourceType::kMacro;
+    out_resource->name.type =
+        ResourceNamedTypeWithDefaultName(ResourceType::kMacro).ToResourceNamedType();
     out_resource->name.entry = maybe_name.value().to_string();
     return ParseMacro(parser, out_resource);
   }
@@ -656,7 +658,8 @@
         return false;
       }
 
-      out_resource->name.type = item_iter->second.type;
+      out_resource->name.type =
+          ResourceNamedTypeWithDefaultName(item_iter->second.type).ToResourceNamedType();
       out_resource->name.entry = maybe_name.value().to_string();
 
       // Only use the implied format of the type when there is no explicit format.
@@ -699,7 +702,7 @@
   if (can_be_item) {
     // Try parsing the elementName (or type) as a resource. These shall only be
     // resources like 'layout' or 'xml' and they can only be references.
-    const ResourceType* parsed_type = ParseResourceType(resource_type);
+    std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(resource_type);
     if (parsed_type) {
       if (!maybe_name) {
         diag_->Error(DiagMessage(out_resource->source)
@@ -708,7 +711,7 @@
         return false;
       }
 
-      out_resource->name.type = *parsed_type;
+      out_resource->name.type = parsed_type->ToResourceNamedType();
       out_resource->name.entry = maybe_name.value().to_string();
       out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
       if (!out_resource->value) {
@@ -933,7 +936,7 @@
     return false;
   }
 
-  const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
+  std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(maybe_type.value());
   if (!parsed_type) {
     diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
                                                    << maybe_type.value()
@@ -941,7 +944,7 @@
     return false;
   }
 
-  out_resource->name.type = *parsed_type;
+  out_resource->name.type = parsed_type->ToResourceNamedType();
 
   if (std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
     std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
@@ -953,7 +956,7 @@
     out_resource->id = maybe_id.value();
   }
 
-  if (*parsed_type == ResourceType::kId) {
+  if (parsed_type->type == ResourceType::kId) {
     // An ID marked as public is also the definition of an ID.
     out_resource->value = util::make_unique<Id>();
   }
@@ -978,7 +981,7 @@
     return false;
   }
 
-  const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
+  std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(maybe_type.value());
   if (!parsed_type) {
     diag->Error(DiagMessage(out_resource->source)
                 << "invalid resource type '" << maybe_type.value() << "' in <" << tag_name << ">");
@@ -1096,7 +1099,7 @@
     return false;
   }
 
-  const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
+  std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(maybe_type.value());
   if (!parsed_type) {
     diag_->Error(DiagMessage(out_resource->source)
                  << "invalid resource type '" << maybe_type.value() << "' in <"
@@ -1104,7 +1107,7 @@
     return false;
   }
 
-  out_resource->name.type = *parsed_type;
+  out_resource->name.type = parsed_type->ToResourceNamedType();
   return true;
 }
 
@@ -1208,8 +1211,8 @@
         continue;
       }
 
-      const ResourceType* type = ParseResourceType(item_type.value());
-      if (type == nullptr) {
+      std::optional<ResourceNamedTypeRef> type = ParseResourceNamedType(item_type.value());
+      if (!type) {
         diag_->Error(DiagMessage(element_source)
                      << "invalid resource type '" << item_type.value()
                      << "' in <item> within an <overlayable>");
@@ -1223,7 +1226,7 @@
       overlayable_item.source = element_source;
 
       ParsedResource child_resource{};
-      child_resource.name.type = *type;
+      child_resource.name.type = type->ToResourceNamedType();
       child_resource.name.entry = item_name.value().to_string();
       child_resource.overlayable_item = overlayable_item;
       out_resource->child_resources.push_back(std::move(child_resource));
@@ -1289,7 +1292,8 @@
 
 bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
                                    ParsedResource* out_resource, bool weak) {
-  out_resource->name.type = ResourceType::kAttr;
+  out_resource->name.type =
+      ResourceNamedTypeWithDefaultName(ResourceType::kAttr).ToResourceNamedType();
 
   // Attributes only end up in default configuration.
   if (out_resource->config != ConfigDescription::DefaultConfig()) {
@@ -1475,7 +1479,8 @@
   }
 
   return Attribute::Symbol{
-      Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
+      Reference(ResourceNameRef({}, ResourceNamedTypeWithDefaultName(ResourceType::kId),
+                                maybe_name.value())),
       val.data, val.dataType};
 }
 
@@ -1509,7 +1514,7 @@
 
 bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
                                 ParsedResource* out_resource) {
-  out_resource->name.type = type;
+  out_resource->name.type = ResourceNamedTypeWithDefaultName(type).ToResourceNamedType();
 
   std::unique_ptr<Style> style = util::make_unique<Style>();
 
@@ -1535,7 +1540,8 @@
     size_t pos = style_name.find_last_of(u'.');
     if (pos != std::string::npos) {
       style->parent_inferred = true;
-      style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
+      style->parent = Reference(ResourceName(
+          {}, ResourceNamedTypeWithDefaultName(ResourceType::kStyle), style_name.substr(0, pos)));
     }
   }
 
@@ -1591,7 +1597,8 @@
 bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
                                     ParsedResource* out_resource,
                                     const uint32_t typeMask) {
-  out_resource->name.type = ResourceType::kArray;
+  out_resource->name.type =
+      ResourceNamedTypeWithDefaultName(ResourceType::kArray).ToResourceNamedType();
 
   std::unique_ptr<Array> array = util::make_unique<Array>();
 
@@ -1646,7 +1653,8 @@
 
 bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
                                  ParsedResource* out_resource) {
-  out_resource->name.type = ResourceType::kPlurals;
+  out_resource->name.type =
+      ResourceNamedTypeWithDefaultName(ResourceType::kPlurals).ToResourceNamedType();
 
   std::unique_ptr<Plural> plural = util::make_unique<Plural>();
 
@@ -1727,7 +1735,8 @@
 
 bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
                                            ParsedResource* out_resource) {
-  out_resource->name.type = ResourceType::kStyleable;
+  out_resource->name.type =
+      ResourceNamedTypeWithDefaultName(ResourceType::kStyleable).ToResourceNamedType();
 
   if (!options_.preserve_visibility_of_styleables) {
     // This was added in change Idd21b5de4d20be06c6f8c8eb5a22ccd68afc4927 to mimic aapt1, but no one
diff --git a/tools/aapt2/ResourceTable.cpp b/tools/aapt2/ResourceTable.cpp
index dae89b0..98cce26 100644
--- a/tools/aapt2/ResourceTable.cpp
+++ b/tools/aapt2/ResourceTable.cpp
@@ -473,7 +473,7 @@
   }
 
   auto package = FindOrCreatePackage(res.name.package);
-  auto type = package->FindOrCreateType(res.name.type);
+  auto type = package->FindOrCreateType(res.name.type.type);
   auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), res.name.entry,
                                    NameEqualRange<ResourceEntry>{});
   const size_t entry_count = std::distance(entry_it.first, entry_it.second);
@@ -593,7 +593,7 @@
     return {};
   }
 
-  ResourceTableType* type = package->FindType(name.type);
+  ResourceTableType* type = package->FindType(name.type.type);
   if (type == nullptr) {
     return {};
   }
@@ -612,7 +612,7 @@
     return {};
   }
 
-  ResourceTableType* type = package->FindType(name.type);
+  ResourceTableType* type = package->FindType(name.type.type);
   if (type == nullptr) {
     return {};
   }
@@ -633,7 +633,7 @@
     return {};
   }
 
-  ResourceTableType* type = package->FindType(name.type);
+  ResourceTableType* type = package->FindType(name.type.type);
   if (type == nullptr) {
     return {};
   }
diff --git a/tools/aapt2/ResourceUtils.cpp b/tools/aapt2/ResourceUtils.cpp
index ead06bf..23f6c88 100644
--- a/tools/aapt2/ResourceUtils.cpp
+++ b/tools/aapt2/ResourceUtils.cpp
@@ -50,12 +50,11 @@
   name_out.package =
       util::Utf16ToUtf8(StringPiece16(name_in.package, name_in.packageLen));
 
-  const ResourceType* type;
+  std::optional<ResourceNamedTypeRef> type;
   if (name_in.type) {
-    type = ParseResourceType(
-        util::Utf16ToUtf8(StringPiece16(name_in.type, name_in.typeLen)));
+    type = ParseResourceNamedType(util::Utf16ToUtf8(StringPiece16(name_in.type, name_in.typeLen)));
   } else if (name_in.type8) {
-    type = ParseResourceType(StringPiece(name_in.type8, name_in.typeLen));
+    type = ParseResourceNamedType(StringPiece(name_in.type8, name_in.typeLen));
   } else {
     return {};
   }
@@ -64,7 +63,7 @@
     return {};
   }
 
-  name_out.type = *type;
+  name_out.type = type->ToResourceNamedType();
 
   if (name_in.name) {
     name_out.entry =
@@ -85,12 +84,12 @@
 
   name_out.package = std::string(name_in.package, name_in.package_len);
 
-  const ResourceType* type;
+  std::optional<ResourceNamedTypeRef> type;
   if (name_in.type16) {
-    type = ParseResourceType(
-        util::Utf16ToUtf8(StringPiece16(name_in.type16, name_in.type_len)));
+    type =
+        ParseResourceNamedType(util::Utf16ToUtf8(StringPiece16(name_in.type16, name_in.type_len)));
   } else if (name_in.type) {
-    type = ParseResourceType(StringPiece(name_in.type, name_in.type_len));
+    type = ParseResourceNamedType(StringPiece(name_in.type, name_in.type_len));
   } else {
     return {};
   }
@@ -99,7 +98,7 @@
     return {};
   }
 
-  name_out.type = *type;
+  name_out.type = type->ToResourceNamedType();
 
   if (name_in.entry16) {
     name_out.entry =
@@ -133,7 +132,7 @@
     return false;
   }
 
-  const ResourceType* parsed_type = ParseResourceType(type);
+  std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(type);
   if (!parsed_type) {
     return false;
   }
@@ -181,7 +180,7 @@
       return false;
     }
 
-    if (create && name.type != ResourceType::kId) {
+    if (create && name.type.type != ResourceType::kId) {
       return false;
     }
 
@@ -230,7 +229,7 @@
 
     if (out_ref) {
       out_ref->package = package;
-      out_ref->type = ResourceType::kAttr;
+      out_ref->type = ResourceNamedTypeWithDefaultName(ResourceType::kAttr);
       out_ref->entry = entry;
     }
     return true;
@@ -272,7 +271,7 @@
   }
 
   ResourceNameRef ref;
-  ref.type = ResourceType::kStyle;
+  ref.type = ResourceNamedTypeWithDefaultName(ResourceType::kStyle);
 
   StringPiece type_str;
   android::ExtractResourceName(name, &ref.package, &type_str, &ref.entry);
@@ -323,7 +322,8 @@
     p++;
   }
 
-  ref.name = ResourceName(package, ResourceType::kAttr, name.empty() ? trimmed_str : name);
+  ref.name = ResourceName(package, ResourceNamedTypeWithDefaultName(ResourceType::kAttr),
+                          name.empty() ? trimmed_str : name);
   return std::optional<Reference>(std::move(ref));
 }
 
diff --git a/tools/aapt2/ResourceValues.cpp b/tools/aapt2/ResourceValues.cpp
index b3ab4ff..b796eb0 100644
--- a/tools/aapt2/ResourceValues.cpp
+++ b/tools/aapt2/ResourceValues.cpp
@@ -116,7 +116,7 @@
 }
 
 bool Reference::Flatten(android::Res_value* out_value) const {
-  if (name && name.value().type == ResourceType::kMacro) {
+  if (name && name.value().type.type == ResourceType::kMacro) {
     return false;
   }
 
@@ -192,7 +192,7 @@
     if (print_package) {
       printer->Print(name.to_string());
     } else {
-      printer->Print(to_string(name.type));
+      printer->Print(name.type.to_string());
       printer->Print("/");
       printer->Print(name.entry);
     }
diff --git a/tools/aapt2/cmd/Link.cpp b/tools/aapt2/cmd/Link.cpp
index e614a75..790f2b3 100644
--- a/tools/aapt2/cmd/Link.cpp
+++ b/tools/aapt2/cmd/Link.cpp
@@ -207,7 +207,7 @@
     }
 
     // Check to see if this is an 'id' with the target package.
-    if (name.type == ResourceType::kId && symbol->id) {
+    if (name.type.type == ResourceType::kId && symbol->id) {
       ResourceId* id = &symbol->id.value();
       if (id->package_id() > kAppPackageId) {
         // Rewrite the resource ID to be compatible pre-O.
diff --git a/tools/aapt2/compile/IdAssigner.cpp b/tools/aapt2/compile/IdAssigner.cpp
index 339b8af..fa816be 100644
--- a/tools/aapt2/compile/IdAssigner.cpp
+++ b/tools/aapt2/compile/IdAssigner.cpp
@@ -275,7 +275,7 @@
     return false;
   }
 
-  auto key = ResourceTypeKey{name.type, id.type_id()};
+  auto key = ResourceTypeKey{name.type.type, id.type_id()};
   auto type = types_.find(key);
   if (type == types_.end()) {
     // The type has not been assigned an id yet. Ensure that the specified id is not being used by
@@ -291,7 +291,7 @@
 
   if (!visibility.staged_api) {
     // Ensure that non-staged resources can only exist in one type ID.
-    auto non_staged_type = non_staged_type_ids_.emplace(name.type, id.type_id());
+    auto non_staged_type = non_staged_type_ids_.emplace(name.type.type, id.type_id());
     if (!non_staged_type.second && non_staged_type.first->second != id.type_id()) {
       diag->Error(DiagMessage() << "can't assign ID " << id << " to resource " << name
                                 << " because type already has ID " << std::hex
@@ -316,14 +316,14 @@
   CHECK(name.package.empty() || name.package == package_name_);
 
   // Find the type id for non-staged resources of this type.
-  auto non_staged_type = non_staged_type_ids_.find(name.type);
+  auto non_staged_type = non_staged_type_ids_.find(name.type.type);
   if (non_staged_type == non_staged_type_ids_.end()) {
     auto next_type_id = type_id_finder_.NextId();
     CHECK(next_type_id.has_value()) << "resource type IDs allocated have exceeded maximum (256)";
-    non_staged_type = non_staged_type_ids_.emplace(name.type, *next_type_id).first;
+    non_staged_type = non_staged_type_ids_.emplace(name.type.type, *next_type_id).first;
   }
 
-  ResourceTypeKey key{name.type, non_staged_type->second};
+  ResourceTypeKey key{name.type.type, non_staged_type->second};
   auto type = types_.find(key);
   if (type == types_.end()) {
     type = types_.emplace(key, TypeGroup(package_id_, key.id)).first;
diff --git a/tools/aapt2/compile/XmlIdCollector.cpp b/tools/aapt2/compile/XmlIdCollector.cpp
index 5054115..bb72159 100644
--- a/tools/aapt2/compile/XmlIdCollector.cpp
+++ b/tools/aapt2/compile/XmlIdCollector.cpp
@@ -46,7 +46,7 @@
       ResourceNameRef name;
       bool create = false;
       if (ResourceUtils::ParseReference(attr.value, &name, &create, nullptr)) {
-        if (create && name.type == ResourceType::kId) {
+        if (create && name.type.type == ResourceType::kId) {
           if (!text::IsValidResourceEntryName(name.entry)) {
             source_diag_->Error(DiagMessage(element->line_number)
                                    << "id '" << name << "' has an invalid entry name");
diff --git a/tools/aapt2/format/binary/BinaryResourceParser.cpp b/tools/aapt2/format/binary/BinaryResourceParser.cpp
index 11d02f0..c65c550 100644
--- a/tools/aapt2/format/binary/BinaryResourceParser.cpp
+++ b/tools/aapt2/format/binary/BinaryResourceParser.cpp
@@ -556,8 +556,8 @@
 std::unique_ptr<Item> BinaryResourceParser::ParseValue(const ResourceNameRef& name,
                                                        const ConfigDescription& config,
                                                        const android::Res_value& value) {
-  std::unique_ptr<Item> item = ResourceUtils::ParseBinaryResValue(name.type, config, value_pool_,
-                                                                  value, &table_->string_pool);
+  std::unique_ptr<Item> item = ResourceUtils::ParseBinaryResValue(
+      name.type.type, config, value_pool_, value, &table_->string_pool);
   if (files_ != nullptr) {
     FileReference* file_ref = ValueCast<FileReference>(item.get());
     if (file_ref != nullptr) {
@@ -575,7 +575,7 @@
 std::unique_ptr<Value> BinaryResourceParser::ParseMapEntry(const ResourceNameRef& name,
                                                            const ConfigDescription& config,
                                                            const ResTable_map_entry* map) {
-  switch (name.type) {
+  switch (name.type.type) {
     case ResourceType::kStyle:
       // fallthrough
     case ResourceType::kConfigVarying:  // legacy thing used in tests
@@ -594,8 +594,8 @@
       // We can ignore the value here.
       return util::make_unique<Id>();
     default:
-      diag_->Error(DiagMessage() << "illegal map type '" << to_string(name.type) << "' ("
-                                 << (int)name.type << ")");
+      diag_->Error(DiagMessage() << "illegal map type '" << name.type << "' ("
+                                 << (int)name.type.type << ")");
       break;
   }
   return {};
diff --git a/tools/aapt2/java/JavaClassGenerator.cpp b/tools/aapt2/java/JavaClassGenerator.cpp
index 3b3c6e1..a963d98 100644
--- a/tools/aapt2/java/JavaClassGenerator.cpp
+++ b/tools/aapt2/java/JavaClassGenerator.cpp
@@ -229,8 +229,8 @@
   const std::string package_name =
       name.package.empty() ? fallback_package_name.to_string() : name.package;
   const std::string entry = JavaClassGenerator::TransformToFieldName(name.entry);
-  return FieldReference(
-      StringPrintf("%s.R.%s.%s", package_name.c_str(), to_string(name.type).data(), entry.c_str()));
+  return FieldReference(StringPrintf("%s.R.%s.%s", package_name.c_str(),
+                                     name.type.to_string().data(), entry.c_str()));
 }
 
 bool JavaClassGenerator::ProcessStyleable(const ResourceNameRef& name, const ResourceId& id,
@@ -451,7 +451,7 @@
                                          MethodDefinition* out_rewrite_method,
                                          text::Printer* r_txt_printer) {
   ResourceId real_id = id;
-  if (context_->GetMinSdkVersion() < SDK_O && name.type == ResourceType::kId &&
+  if (context_->GetMinSdkVersion() < SDK_O && name.type.type == ResourceType::kId &&
       id.package_id() > kAppPackageId) {
     // Workaround for feature splits using package IDs > 0x7F.
     // See b/37498913.
@@ -489,7 +489,7 @@
 
   if (r_txt_printer != nullptr) {
     r_txt_printer->Print("int ")
-        .Print(to_string(name.type))
+        .Print(name.type.to_string())
         .Print(" ")
         .Print(field_name)
         .Print(" ")
@@ -497,7 +497,7 @@
   }
 
   if (out_rewrite_method != nullptr) {
-    const StringPiece& type_str = to_string(name.type);
+    const std::string type_str = name.type.to_string();
     out_rewrite_method->AppendStatement(
         StringPrintf("%s.%s = (%s.%s & 0x00ffffff) | packageIdBits;", type_str.data(),
                      field_name.data(), type_str.data(), field_name.data()));
@@ -561,7 +561,7 @@
       return false;
     }
 
-    if (resource_name.type == ResourceType::kStyleable) {
+    if (resource_name.type.type == ResourceType::kStyleable) {
       CHECK(!entry->values.empty());
       const auto styleable = reinterpret_cast<const Styleable*>(entry->values.front()->value.get());
       if (!ProcessStyleable(resource_name, id, *styleable, package_name_to_generate,
diff --git a/tools/aapt2/java/ProguardRules.cpp b/tools/aapt2/java/ProguardRules.cpp
index 4a2d0ae..e53e220 100644
--- a/tools/aapt2/java/ProguardRules.cpp
+++ b/tools/aapt2/java/ProguardRules.cpp
@@ -358,7 +358,7 @@
     return false;
   }
 
-  switch (res->file.name.type) {
+  switch (res->file.name.type.type) {
     case ResourceType::kLayout: {
       LayoutVisitor visitor(res->file, keep_set);
       res->root->Accept(&visitor);
@@ -465,7 +465,7 @@
   locations->insert(location);
 
   // TODO: allow for more reference types if we can determine its safe.
-  if (location.name.type != ResourceType::kLayout) {
+  if (location.name.type.type != ResourceType::kLayout) {
     return false;
   }
 
diff --git a/tools/aapt2/link/ReferenceLinker.cpp b/tools/aapt2/link/ReferenceLinker.cpp
index 47c804c..5372cf2 100644
--- a/tools/aapt2/link/ReferenceLinker.cpp
+++ b/tools/aapt2/link/ReferenceLinker.cpp
@@ -352,7 +352,7 @@
   }
 
   const ResourceName& ref_name = ref.name.value();
-  CHECK_EQ(ref_name.type, ResourceType::kAttr);
+  CHECK_EQ(ref_name.type.type, ResourceType::kAttr);
 
   if (!ref_name.package.empty()) {
     *out_msg << ref_name.package << ":";
@@ -385,7 +385,7 @@
   Reference transformed_reference = reference;
   xml::ResolvePackage(decls, &transformed_reference);
 
-  if (transformed_reference.name.value().type == ResourceType::kMacro) {
+  if (transformed_reference.name.value().type.type == ResourceType::kMacro) {
     if (transformed_reference.name.value().package.empty()) {
       transformed_reference.name.value().package = callsite.package;
     }
diff --git a/tools/aapt2/link/TableMerger.cpp b/tools/aapt2/link/TableMerger.cpp
index d094d36..d78f0ac 100644
--- a/tools/aapt2/link/TableMerger.cpp
+++ b/tools/aapt2/link/TableMerger.cpp
@@ -349,7 +349,7 @@
   file_ref->file = file;
 
   ResourceTablePackage* pkg = table.FindOrCreatePackage(file_desc.name.package);
-  pkg->FindOrCreateType(file_desc.name.type)
+  pkg->FindOrCreateType(file_desc.name.type.type)
       ->FindOrCreateEntry(file_desc.name.entry)
       ->FindOrCreateValue(file_desc.config, {})
       ->value = std::move(file_ref);
diff --git a/tools/aapt2/process/SymbolTable.cpp b/tools/aapt2/process/SymbolTable.cpp
index 2d58cbf..92b45c3 100644
--- a/tools/aapt2/process/SymbolTable.cpp
+++ b/tools/aapt2/process/SymbolTable.cpp
@@ -185,7 +185,7 @@
     const ResourceName& name) {
   std::optional<ResourceTable::SearchResult> result = table_->FindResource(name);
   if (!result) {
-    if (name.type == ResourceType::kAttr) {
+    if (name.type.type == ResourceType::kAttr) {
       // Recurse and try looking up a private attribute.
       return FindByName(ResourceName(name.package, ResourceType::kAttrPrivate, name.entry));
     }
@@ -203,7 +203,7 @@
         (sr.entry->id.value().package_id() == 0) || sr.entry->visibility.staged_api;
   }
 
-  if (name.type == ResourceType::kAttr || name.type == ResourceType::kAttrPrivate) {
+  if (name.type.type == ResourceType::kAttr || name.type.type == ResourceType::kAttrPrivate) {
     const ConfigDescription kDefaultConfig;
     ResourceConfigValue* config_value = sr.entry->FindValue(kDefaultConfig);
     if (config_value) {
@@ -366,7 +366,7 @@
   }
 
   std::unique_ptr<SymbolTable::Symbol> s;
-  if (real_name.type == ResourceType::kAttr) {
+  if (real_name.type.type == ResourceType::kAttr) {
     s = LookupAttributeInTable(asset_manager_, res_id);
   } else {
     s = util::make_unique<SymbolTable::Symbol>();
@@ -413,7 +413,7 @@
 
   ResourceName& name = maybe_name.value();
   std::unique_ptr<SymbolTable::Symbol> s;
-  if (name.type == ResourceType::kAttr) {
+  if (name.type.type == ResourceType::kAttr) {
     s = LookupAttributeInTable(asset_manager_, id);
   } else {
     s = util::make_unique<SymbolTable::Symbol>();
diff --git a/tools/aapt2/process/SymbolTable.h b/tools/aapt2/process/SymbolTable.h
index 65ae7be..c17837c 100644
--- a/tools/aapt2/process/SymbolTable.h
+++ b/tools/aapt2/process/SymbolTable.h
@@ -38,7 +38,7 @@
   std::hash<std::string> str_hash;
   android::hash_t hash = 0;
   hash = android::JenkinsHashMix(hash, (uint32_t)str_hash(name.package));
-  hash = android::JenkinsHashMix(hash, (uint32_t)name.type);
+  hash = android::JenkinsHashMix(hash, (uint32_t)str_hash(name.type.name));
   hash = android::JenkinsHashMix(hash, (uint32_t)str_hash(name.entry));
   return hash;
 }