aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDevin Moore <devinmoore@google.com>2020-09-18 15:22:01 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2020-09-18 15:22:01 +0000
commit60abe5dfd3cc1ee4c1b189eb75b5e67c84ca01ba (patch)
treed136267c3b63c15ebc6b7fdb010510357d9757e5
parent3e17a22c2c7e6478b5f6688fda1cf5d332158c85 (diff)
parentd20f70dbd497f7b5eccbdeb89bf0c46a05f7982d (diff)
downloadplatform_system_tools_hidl-60abe5dfd3cc1ee4c1b189eb75b5e67c84ca01ba.tar.gz
platform_system_tools_hidl-60abe5dfd3cc1ee4c1b189eb75b5e67c84ca01ba.tar.bz2
platform_system_tools_hidl-60abe5dfd3cc1ee4c1b189eb75b5e67c84ca01ba.zip
Merge "hidl2aidl: move import handling to a single place"
-rw-r--r--hidl2aidl/AidlHelper.cpp60
-rw-r--r--hidl2aidl/AidlHelper.h19
-rw-r--r--hidl2aidl/AidlNamedType.cpp83
3 files changed, 80 insertions, 82 deletions
diff --git a/hidl2aidl/AidlHelper.cpp b/hidl2aidl/AidlHelper.cpp
index ce575bcee..4ea8fc668 100644
--- a/hidl2aidl/AidlHelper.cpp
+++ b/hidl2aidl/AidlHelper.cpp
@@ -25,6 +25,7 @@
#include "AidlHelper.h"
#include "ArrayType.h"
+#include "CompoundType.h"
#include "Coordinator.h"
#include "Interface.h"
#include "Method.h"
@@ -113,6 +114,17 @@ void AidlHelper::emitFileHeader(Formatter& out, const NamedType& type) {
importLocallyReferencedType(*ref->get(), &imports);
}
}
+ } else if (type.isCompoundType()) {
+ // Get all of the imports for the flattened compound type that may
+ // include additional fields and subtypes from older versions
+ ProcessedCompoundType processedType;
+ processCompoundType(static_cast<const CompoundType&>(type), &processedType);
+ for (const auto& field : processedType.fields) {
+ importLocallyReferencedType(*field.field->get(), &imports);
+ }
+ for (const auto& subType : processedType.subTypes) {
+ importLocallyReferencedType(*subType, &imports);
+ }
} else {
for (const Reference<Type>* ref : type.getReferences()) {
if (ref->get()->definedName() == type.fqName().name()) {
@@ -142,4 +154,52 @@ Formatter AidlHelper::getFileWithHeader(const NamedType& namedType,
return out;
}
+void AidlHelper::processCompoundType(const CompoundType& compoundType,
+ ProcessedCompoundType* processedType) {
+ // Gather all of the subtypes defined in this type
+ for (const NamedType* subType : compoundType.getSubTypes()) {
+ processedType->subTypes.insert(subType);
+ }
+ std::pair<size_t, size_t> version = compoundType.fqName().hasVersion()
+ ? compoundType.fqName().getVersion()
+ : std::pair<size_t, size_t>{0, 0};
+ for (const NamedReference<Type>* field : compoundType.getFields()) {
+ // Check for references to an older version of itself
+ if (field->get()->typeName() == compoundType.typeName()) {
+ processCompoundType(static_cast<const CompoundType&>(*field->get()), processedType);
+ } else {
+ // Handle duplicate field names. Keep only the most recent definitions.
+ auto it = std::find_if(processedType->fields.begin(), processedType->fields.end(),
+ [field](auto& processedField) {
+ return processedField.field->name() == field->name();
+ });
+ if (it != processedType->fields.end()) {
+ AidlHelper::notes()
+ << "Found conflicting field name \"" << field->name()
+ << "\" in different versions of " << compoundType.fqName().name() << ". ";
+
+ if (version.first > it->version.first ||
+ (version.first == it->version.first && version.second > it->version.second)) {
+ AidlHelper::notes()
+ << "Keeping " << field->get()->typeName() << " from " << version.first
+ << "." << version.second << " and discarding "
+ << (it->field)->get()->typeName() << " from " << it->version.first
+ << "." << it->version.second << ".\n";
+
+ it->field = field;
+ it->version = version;
+ } else {
+ AidlHelper::notes()
+ << "Keeping " << (it->field)->get()->typeName() << " from "
+ << it->version.first << "." << it->version.second << " and discarding "
+ << field->get()->typeName() << " from " << version.first << "."
+ << version.second << ".\n";
+ }
+ } else {
+ processedType->fields.push_back({field, field->name(), version});
+ }
+ }
+ }
+}
+
} // namespace android
diff --git a/hidl2aidl/AidlHelper.h b/hidl2aidl/AidlHelper.h
index 9ced102fe..73c6c8c42 100644
--- a/hidl2aidl/AidlHelper.h
+++ b/hidl2aidl/AidlHelper.h
@@ -16,12 +16,15 @@
#pragma once
+#include <map>
#include <set>
#include <string>
#include <vector>
+#include "Reference.h"
namespace android {
+struct CompoundType;
struct Coordinator;
struct Formatter;
struct FQName;
@@ -31,6 +34,19 @@ struct NamedType;
struct Scope;
struct Type;
+struct FieldWithVersion {
+ const NamedReference<Type>* field;
+ // name of the field appended by the
+ std::string fullName;
+ std::pair<size_t, size_t> version;
+};
+
+struct ProcessedCompoundType {
+ // map modified name to field. This modified name is old.new
+ std::vector<FieldWithVersion> fields;
+ std::set<const NamedType*> subTypes;
+};
+
struct AidlHelper {
/* FQName helpers */
// getAidlName returns the type names
@@ -60,6 +76,9 @@ struct AidlHelper {
// Returns all methods that would exist in an AIDL equivalent interface
static std::vector<const Method*> getUserDefinedMethods(const Interface& interface);
+ static void processCompoundType(const CompoundType& compoundType,
+ ProcessedCompoundType* processedType);
+
static Formatter& notes();
static void setNotes(Formatter* formatter);
diff --git a/hidl2aidl/AidlNamedType.cpp b/hidl2aidl/AidlNamedType.cpp
index da3abb778..d0f8ec358 100644
--- a/hidl2aidl/AidlNamedType.cpp
+++ b/hidl2aidl/AidlNamedType.cpp
@@ -24,16 +24,6 @@
namespace android {
-struct FieldWithVersion {
- const NamedReference<Type>* field;
- std::pair<size_t, size_t> version;
-};
-
-struct ProcessedCompoundType {
- std::vector<FieldWithVersion> fields;
- std::set<const NamedType*> subTypes;
-};
-
static void emitConversionNotes(Formatter& out, const NamedType& namedType) {
out << "// This is the HIDL definition of " << namedType.fqName().string() << "\n";
out.pushLinePrefix("// ");
@@ -68,82 +58,11 @@ static void emitEnumAidlDefinition(Formatter& out, const EnumType& enumType) {
});
}
-void processCompoundType(const CompoundType& compoundType, ProcessedCompoundType* processedType) {
- // Gather all of the subtypes defined in this type
- for (const NamedType* subType : compoundType.getSubTypes()) {
- processedType->subTypes.insert(subType);
- }
- std::pair<size_t, size_t> version = compoundType.fqName().hasVersion()
- ? compoundType.fqName().getVersion()
- : std::pair<size_t, size_t>{0, 0};
- for (const NamedReference<Type>* field : compoundType.getFields()) {
- // Check for references to an older version of itself
- if (field->get()->typeName() == compoundType.typeName()) {
- processCompoundType(static_cast<const CompoundType&>(*field->get()), processedType);
- } else {
- // Handle duplicate field names. Keep only the most recent definitions.
- auto it = std::find_if(processedType->fields.begin(), processedType->fields.end(),
- [field](auto& processedField) {
- return processedField.field->name() == field->name();
- });
- if (it != processedType->fields.end()) {
- AidlHelper::notes()
- << "Found conflicting field name \"" << field->name()
- << "\" in different versions of " << compoundType.fqName().name() << ". ";
-
- if (version.first > it->version.first ||
- (version.first == it->version.first && version.second > it->version.second)) {
- AidlHelper::notes()
- << "Keeping " << field->get()->typeName() << " from " << version.first
- << "." << version.second << " and discarding "
- << (it->field)->get()->typeName() << " from " << it->version.first
- << "." << it->version.second << ".\n";
-
- it->field = field;
- it->version = version;
- } else {
- AidlHelper::notes()
- << "Keeping " << (it->field)->get()->typeName() << " from "
- << it->version.first << "." << it->version.second << " and discarding "
- << field->get()->typeName() << " from " << version.first << "."
- << version.second << ".\n";
- }
- } else {
- processedType->fields.push_back({field, version});
- }
- }
- }
-}
-
static void emitCompoundTypeAidlDefinition(Formatter& out, const CompoundType& compoundType) {
// Get all of the subtypes and fields from this type and any older versions
// that it references.
ProcessedCompoundType processedType;
- processCompoundType(compoundType, &processedType);
-
- // Add all of the necessary imports for types that were found in older versions and missed
- // when emitting the file header.
- std::set<std::string> imports;
- const std::vector<const NamedReference<Type>*>& latestFields = compoundType.getFields();
- const std::vector<NamedType*>& latestSubTypes = compoundType.getSubTypes();
- for (auto const& fieldWithVersion : processedType.fields) {
- if (std::find(latestFields.begin(), latestFields.end(), fieldWithVersion.field) ==
- latestFields.end()) {
- AidlHelper::importLocallyReferencedType(*fieldWithVersion.field->get(), &imports);
- }
- }
- for (const NamedType* subType : processedType.subTypes) {
- if (std::find(latestSubTypes.begin(), latestSubTypes.end(), subType) ==
- latestSubTypes.end()) {
- AidlHelper::importLocallyReferencedType(*subType, &imports);
- }
- }
- for (const std::string& import : imports) {
- out << "import " << import << ";\n";
- }
- if (imports.size() > 0) {
- out << "\n";
- }
+ AidlHelper::processCompoundType(compoundType, &processedType);
compoundType.emitDocComment(out);
out << "@VintfStability \n";