Refactor interface and enum type- and parent-related methods
Make EnumType typeChain has the same syntax
(same name and same return way instead of result by pointer).
Add EnumType::superTypeChain by analogy with Interface::superTypeChain.
Add Interface::allSuperMethodsFromRoot as a mixture of
Interface::allSuperMethodsFromRoot and Interface::superTypeChain.
Test: build hidl-gen, hidl_test
Change-Id: I0af678eab7a563b471f041599ff83bc2b4b9764a
diff --git a/EnumType.cpp b/EnumType.cpp
index 02c6c69..eaadc23 100644
--- a/EnumType.cpp
+++ b/EnumType.cpp
@@ -46,8 +46,7 @@
CHECK(value != nullptr);
EnumValue *prev = nullptr;
- std::vector<const EnumType *> chain;
- getTypeChain(&chain);
+ std::vector<const EnumType*> chain = typeChain();
for (auto it = chain.begin(); it != chain.end(); ++it) {
const auto &type = *it;
if(!type->values().empty()) {
@@ -106,8 +105,7 @@
}
LocalIdentifier *EnumType::lookupIdentifier(const std::string &name) const {
- std::vector<const EnumType *> chain;
- getTypeChain(&chain);
+ std::vector<const EnumType*> chain = typeChain();
for (auto it = chain.begin(); it != chain.end(); ++it) {
const auto &type = *it;
for(EnumValue *v : type->values()) {
@@ -165,8 +163,7 @@
out.indent();
- std::vector<const EnumType *> chain;
- getTypeChain(&chain);
+ std::vector<const EnumType*> chain = typeChain();
for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
const auto &type = *it;
@@ -362,8 +359,7 @@
const std::string typeName =
scalarType->getJavaType(false /* forInitializer */);
- std::vector<const EnumType *> chain;
- getTypeChain(&chain);
+ std::vector<const EnumType*> chain = typeChain();
for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
const auto &type = *it;
@@ -448,8 +444,7 @@
out << "scalar_type: \""
<< scalarType->getVtsScalarType()
<< "\"\n\n";
- std::vector<const EnumType *> chain;
- getTypeChain(&chain);
+ std::vector<const EnumType*> chain = typeChain();
for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
const auto &type = *it;
@@ -489,19 +484,28 @@
<< name << "));\n";
}
-void EnumType::getTypeChain(std::vector<const EnumType *> *out) const {
- out->clear();
- const EnumType *type = this;
- for (;;) {
- out->push_back(type);
+std::vector<const EnumType*> EnumType::typeChain() const {
+ std::vector<const EnumType*> types;
+ for (const EnumType* type = this; type != nullptr;) {
+ types.push_back(type);
- const Type *superType = type->storageType();
- if (superType == NULL || !superType->isEnum()) {
- break;
+ const Type* superType = type->storageType();
+ if (superType != nullptr && superType->isEnum()) {
+ type = static_cast<const EnumType*>(superType);
+ } else {
+ type = nullptr;
}
-
- type = static_cast<const EnumType *>(superType);
}
+
+ return types;
+}
+
+std::vector<const EnumType*> EnumType::superTypeChain() const {
+ const Type* superType = storageType();
+ if (superType == nullptr || !superType->isEnum()) {
+ return {};
+ }
+ return static_cast<const EnumType*>(superType)->typeChain();
}
void EnumType::getAlignmentAndSize(size_t *align, size_t *size) const {
@@ -559,7 +563,7 @@
std::vector<const EnumType *> chain;
if (exportParent) {
- getTypeChain(&chain);
+ chain = typeChain();
} else {
chain = { this };
}
diff --git a/EnumType.h b/EnumType.h
index 95a6174..27f648b 100644
--- a/EnumType.h
+++ b/EnumType.h
@@ -99,8 +99,10 @@
status_t emitExportedHeader(Formatter &out, bool forJava) const override;
-private:
- void getTypeChain(std::vector<const EnumType *> *out) const;
+ private:
+ std::vector<const EnumType*> typeChain() const;
+ std::vector<const EnumType*> superTypeChain() const;
+
const Annotation *findExportAnnotation() const;
void emitEnumBitwiseOperator(
diff --git a/Interface.cpp b/Interface.cpp
index 2acb5a0..db5d682 100644
--- a/Interface.cpp
+++ b/Interface.cpp
@@ -572,6 +572,10 @@
return v;
}
+std::vector<InterfaceAndMethod> Interface::allSuperMethodsFromRoot() const {
+ return isIBase() ? std::vector<InterfaceAndMethod>() : superType()->allMethodsFromRoot();
+}
+
Method *Interface::lookupMethod(std::string name) const {
for (const auto &tuple : allMethodsFromRoot()) {
Method *method = tuple.method();
diff --git a/Interface.h b/Interface.h
index 9d6e50d..7489735 100644
--- a/Interface.h
+++ b/Interface.h
@@ -67,6 +67,9 @@
// this->hidlReservedMethods()
std::vector<InterfaceAndMethod> allMethodsFromRoot() const;
+ // allMethodsFromRoot for parent
+ std::vector<InterfaceAndMethod> allSuperMethodsFromRoot() const;
+
// aliases for corresponding methods in this->fqName()
std::string getBaseName() const;
std::string getProxyName() const;