AAPT2: Define intermediate compiled XML proto

This proto format is meant to encapsulate more information
that is specific to Android and allows for easier validation
and manipulation across tools.

Test: make aapt2_tests
Bug: 65645766
Change-Id: I13bc34a460671fc0a36246be0d287a3d37d244d6
Merged-In: I13bc34a460671fc0a36246be0d287a3d37d244d6
diff --git a/tools/aapt2/proto/TableProtoDeserializer.cpp b/tools/aapt2/proto/TableProtoDeserializer.cpp
index e891954..b9d5878 100644
--- a/tools/aapt2/proto/TableProtoDeserializer.cpp
+++ b/tools/aapt2/proto/TableProtoDeserializer.cpp
@@ -61,54 +61,55 @@
   }
 
  public:
-  bool DeserializeFromPb(const pb::Package& pbPackage, ResourceTable* table) {
+  bool DeserializeFromPb(const pb::Package& pb_package, ResourceTable* table) {
     Maybe<uint8_t> id;
-    if (pbPackage.has_package_id()) {
-      id = static_cast<uint8_t>(pbPackage.package_id());
+    if (pb_package.has_package_id()) {
+      id = static_cast<uint8_t>(pb_package.package_id());
     }
 
-    std::map<ResourceId, ResourceNameRef> idIndex;
+    std::map<ResourceId, ResourceNameRef> id_index;
 
-    ResourceTablePackage* pkg = table->CreatePackage(pbPackage.package_name(), id);
-    for (const pb::Type& pbType : pbPackage.types()) {
-      const ResourceType* resType = ParseResourceType(pbType.name());
-      if (!resType) {
-        diag_->Error(DiagMessage(source_) << "unknown type '" << pbType.name() << "'");
+    ResourceTablePackage* pkg = table->CreatePackage(pb_package.package_name(), id);
+    for (const pb::Type& pb_type : pb_package.type()) {
+      const ResourceType* res_type = ParseResourceType(pb_type.name());
+      if (res_type == nullptr) {
+        diag_->Error(DiagMessage(source_) << "unknown type '" << pb_type.name() << "'");
         return {};
       }
 
-      ResourceTableType* type = pkg->FindOrCreateType(*resType);
+      ResourceTableType* type = pkg->FindOrCreateType(*res_type);
 
-      for (const pb::Entry& pbEntry : pbType.entries()) {
-        ResourceEntry* entry = type->FindOrCreateEntry(pbEntry.name());
+      for (const pb::Entry& pb_entry : pb_type.entry()) {
+        ResourceEntry* entry = type->FindOrCreateEntry(pb_entry.name());
 
         // Deserialize the symbol status (public/private with source and comments).
-        if (pbEntry.has_symbol_status()) {
-          const pb::SymbolStatus& pbStatus = pbEntry.symbol_status();
-          if (pbStatus.has_source()) {
-            DeserializeSourceFromPb(pbStatus.source(), *source_pool_, &entry->symbol_status.source);
+        if (pb_entry.has_symbol_status()) {
+          const pb::SymbolStatus& pb_status = pb_entry.symbol_status();
+          if (pb_status.has_source()) {
+            DeserializeSourceFromPb(pb_status.source(), *source_pool_,
+                                    &entry->symbol_status.source);
           }
 
-          if (pbStatus.has_comment()) {
-            entry->symbol_status.comment = pbStatus.comment();
+          if (pb_status.has_comment()) {
+            entry->symbol_status.comment = pb_status.comment();
           }
 
-          entry->symbol_status.allow_new = pbStatus.allow_new();
+          entry->symbol_status.allow_new = pb_status.allow_new();
 
-          SymbolState visibility = DeserializeVisibilityFromPb(pbStatus.visibility());
+          SymbolState visibility = DeserializeVisibilityFromPb(pb_status.visibility());
           entry->symbol_status.state = visibility;
 
           if (visibility == SymbolState::kPublic) {
             // This is a public symbol, we must encode the ID now if there is one.
-            if (pbEntry.has_id()) {
-              entry->id = static_cast<uint16_t>(pbEntry.id());
+            if (pb_entry.has_id()) {
+              entry->id = static_cast<uint16_t>(pb_entry.id());
             }
 
             if (type->symbol_status.state != SymbolState::kPublic) {
               // If the type has not been made public, do so now.
               type->symbol_status.state = SymbolState::kPublic;
-              if (pbType.has_id()) {
-                type->id = static_cast<uint8_t>(pbType.id());
+              if (pb_type.has_id()) {
+                type->id = static_cast<uint8_t>(pb_type.id());
               }
             }
           } else if (visibility == SymbolState::kPrivate) {
@@ -118,37 +119,37 @@
           }
         }
 
-        ResourceId resId(pbPackage.package_id(), pbType.id(), pbEntry.id());
-        if (resId.is_valid()) {
-          idIndex[resId] = ResourceNameRef(pkg->name, type->type, entry->name);
+        ResourceId resid(pb_package.package_id(), pb_type.id(), pb_entry.id());
+        if (resid.is_valid()) {
+          id_index[resid] = ResourceNameRef(pkg->name, type->type, entry->name);
         }
 
-        for (const pb::ConfigValue& pbConfigValue : pbEntry.config_values()) {
-          const pb::ConfigDescription& pbConfig = pbConfigValue.config();
+        for (const pb::ConfigValue& pb_config_value : pb_entry.config_value()) {
+          const pb::ConfigDescription& pb_config = pb_config_value.config();
 
           ConfigDescription config;
-          if (!DeserializeConfigDescriptionFromPb(pbConfig, &config)) {
+          if (!DeserializeConfigDescriptionFromPb(pb_config, &config)) {
             diag_->Error(DiagMessage(source_) << "invalid configuration");
             return {};
           }
 
-          ResourceConfigValue* configValue = entry->FindOrCreateValue(config, pbConfig.product());
-          if (configValue->value) {
+          ResourceConfigValue* config_value = entry->FindOrCreateValue(config, pb_config.product());
+          if (config_value->value) {
             // Duplicate config.
             diag_->Error(DiagMessage(source_) << "duplicate configuration");
             return {};
           }
 
-          configValue->value =
-              DeserializeValueFromPb(pbConfigValue.value(), config, &table->string_pool);
-          if (!configValue->value) {
+          config_value->value =
+              DeserializeValueFromPb(pb_config_value.value(), config, &table->string_pool);
+          if (!config_value->value) {
             return {};
           }
         }
       }
     }
 
-    ReferenceIdToNameVisitor visitor(&idIndex);
+    ReferenceIdToNameVisitor visitor(&id_index);
     VisitAllValuesInPackage(pkg, &visitor);
     return true;
   }
@@ -202,8 +203,6 @@
   std::unique_ptr<Value> DeserializeValueFromPb(const pb::Value& pb_value,
                                                 const ConfigDescription& config,
                                                 StringPool* pool) {
-    const bool is_weak = pb_value.has_weak() ? pb_value.weak() : false;
-
     std::unique_ptr<Value> value;
     if (pb_value.has_item()) {
       value = DeserializeItemFromPb(pb_value.item(), config, pool);
@@ -215,11 +214,11 @@
       const pb::CompoundValue& pb_compound_value = pb_value.compound_value();
       if (pb_compound_value.has_attr()) {
         const pb::Attribute& pb_attr = pb_compound_value.attr();
-        std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(is_weak);
+        std::unique_ptr<Attribute> attr = util::make_unique<Attribute>();
         attr->type_mask = pb_attr.format_flags();
         attr->min_int = pb_attr.min_int();
         attr->max_int = pb_attr.max_int();
-        for (const pb::Attribute_Symbol& pb_symbol : pb_attr.symbols()) {
+        for (const pb::Attribute_Symbol& pb_symbol : pb_attr.symbol()) {
           Attribute::Symbol symbol;
           DeserializeItemCommon(pb_symbol, &symbol.symbol);
           if (!DeserializeReferenceFromPb(pb_symbol.name(), &symbol.symbol)) {
@@ -246,7 +245,7 @@
           }
         }
 
-        for (const pb::Style_Entry& pb_entry : pb_style.entries()) {
+        for (const pb::Style_Entry& pb_entry : pb_style.entry()) {
           Style::Entry entry;
           DeserializeItemCommon(pb_entry, &entry.key);
           if (!DeserializeReferenceFromPb(pb_entry.key(), &entry.key)) {
@@ -266,7 +265,7 @@
       } else if (pb_compound_value.has_styleable()) {
         const pb::Styleable& pb_styleable = pb_compound_value.styleable();
         std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
-        for (const pb::Styleable_Entry& pb_entry : pb_styleable.entries()) {
+        for (const pb::Styleable_Entry& pb_entry : pb_styleable.entry()) {
           Reference attr_ref;
           DeserializeItemCommon(pb_entry, &attr_ref);
           DeserializeReferenceFromPb(pb_entry.attr(), &attr_ref);
@@ -277,21 +276,21 @@
       } else if (pb_compound_value.has_array()) {
         const pb::Array& pb_array = pb_compound_value.array();
         std::unique_ptr<Array> array = util::make_unique<Array>();
-        for (const pb::Array_Entry& pb_entry : pb_array.entries()) {
+        for (const pb::Array_Element& pb_entry : pb_array.element()) {
           std::unique_ptr<Item> item = DeserializeItemFromPb(pb_entry.item(), config, pool);
           if (!item) {
             return {};
           }
 
           DeserializeItemCommon(pb_entry, item.get());
-          array->items.push_back(std::move(item));
+          array->elements.push_back(std::move(item));
         }
         value = std::move(array);
 
       } else if (pb_compound_value.has_plural()) {
         const pb::Plural& pb_plural = pb_compound_value.plural();
         std::unique_ptr<Plural> plural = util::make_unique<Plural>();
-        for (const pb::Plural_Entry& pb_entry : pb_plural.entries()) {
+        for (const pb::Plural_Entry& pb_entry : pb_plural.entry()) {
           size_t pluralIdx = DeserializePluralEnumFromPb(pb_entry.arity());
           plural->values[pluralIdx] = DeserializeItemFromPb(pb_entry.item(), config, pool);
           if (!plural->values[pluralIdx]) {
@@ -313,7 +312,7 @@
 
     CHECK(value) << "forgot to set value";
 
-    value->SetWeak(is_weak);
+    value->SetWeak(pb_value.weak());
     DeserializeItemCommon(pb_value, value.get());
     return value;
   }
@@ -378,7 +377,7 @@
   }
 
   PackagePbDeserializer package_pb_deserializer(&source_pool, source, diag);
-  for (const pb::Package& pb_package : pb_table.packages()) {
+  for (const pb::Package& pb_package : pb_table.package()) {
     if (!package_pb_deserializer.DeserializeFromPb(pb_package, table.get())) {
       return {};
     }
@@ -387,7 +386,7 @@
 }
 
 std::unique_ptr<ResourceFile> DeserializeCompiledFileFromPb(
-    const pb::CompiledFile& pb_file, const Source& source, IDiagnostics* diag) {
+    const pb::internal::CompiledFile& pb_file, const Source& source, IDiagnostics* diag) {
   std::unique_ptr<ResourceFile> file = util::make_unique<ResourceFile>();
 
   ResourceNameRef name_ref;
@@ -403,19 +402,20 @@
   file->source.path = pb_file.source_path();
   DeserializeConfigDescriptionFromPb(pb_file.config(), &file->config);
 
-  for (const pb::CompiledFile_Symbol& pb_symbol : pb_file.exported_symbols()) {
-    // Need to create an lvalue here so that nameRef can point to something
-    // real.
-    if (!ResourceUtils::ParseResourceName(pb_symbol.resource_name(),
-                                          &name_ref)) {
+  for (const pb::internal::CompiledFile_Symbol& pb_symbol : pb_file.exported_symbol()) {
+    // Need to create an lvalue here so that nameRef can point to something real.
+    if (!ResourceUtils::ParseResourceName(pb_symbol.resource_name(), &name_ref)) {
       diag->Error(DiagMessage(source)
                   << "invalid resource name for exported symbol in "
                      "compiled file header: "
                   << pb_file.resource_name());
       return {};
     }
-    file->exported_symbols.push_back(
-        SourcedResourceName{name_ref.ToResourceName(), pb_symbol.line_no()});
+    size_t line = 0u;
+    if (pb_symbol.has_source()) {
+      line = pb_symbol.source().line_number();
+    }
+    file->exported_symbols.push_back(SourcedResourceName{name_ref.ToResourceName(), line});
   }
   return file;
 }