Clean up ArrayType.cpp
mSizeComments are removed
mSizes are now a vector of ConstantExpression's
Bug: 32559427 clean up hidl-gen
Bug: 32592564 Remove extraneous comments from array type's sizes.
Test: hidl_test
Test: make hidl_test_java
Change-Id: I2a0dbf7e31a425ce851c9be3b413accdfcb79789
diff --git a/ArrayType.cpp b/ArrayType.cpp
index 2757bad..13c861e 100644
--- a/ArrayType.cpp
+++ b/ArrayType.cpp
@@ -25,8 +25,7 @@
ArrayType::ArrayType(ArrayType *srcArray, ConstantExpression *size)
: mElementType(srcArray->mElementType),
- mSizes(srcArray->mSizes),
- mSizeComments(srcArray->mSizeComments) {
+ mSizes(srcArray->mSizes) {
prependDimension(size);
}
@@ -36,13 +35,11 @@
}
void ArrayType::prependDimension(ConstantExpression *size) {
- mSizes.insert(mSizes.begin(), size->castSizeT());
- mSizeComments.insert(mSizeComments.begin(), size->description());
+ mSizes.insert(mSizes.begin(), size);
}
void ArrayType::appendDimension(ConstantExpression *size) {
- mSizes.push_back(size->castSizeT());
- mSizeComments.push_back(size->description());
+ mSizes.push_back(size);
}
size_t ArrayType::countDimensions() const {
@@ -72,11 +69,13 @@
for (size_t i = 0; i < mSizes.size(); ++i) {
arrayType += ", ";
- arrayType += std::to_string(mSizes[i]);
+ arrayType += mSizes[i]->cppValue();
- arrayType += " /* ";
- arrayType += mSizeComments[i];
- arrayType += " */";
+ if (!mSizes[i]->descriptionIsTrivial()) {
+ arrayType += " /* ";
+ arrayType += mSizes[i]->description();
+ arrayType += " */";
+ }
}
arrayType += ">";
@@ -105,16 +104,18 @@
extra->clear();
- CHECK(mSizes.size() == mSizeComments.size());
for (size_t i = 0; i < mSizes.size(); ++i) {
*extra += "[";
if (forInitializer) {
- *extra += std::to_string(mSizes[i]);
- *extra += " ";
+ *extra += mSizes[i]->javaValue();
}
- *extra += "/* " + mSizeComments[i] + " */";
+ if (!forInitializer || !mSizes[i]->descriptionIsTrivial()) {
+ if (forInitializer)
+ *extra += " ";
+ *extra += "/* " + mSizes[i]->description() + " */";
+ }
*extra += "]";
}
@@ -172,7 +173,7 @@
} else {
size_t numArrayElements = 1;
for (auto size : mSizes) {
- numArrayElements *= size;
+ numArrayElements *= size->castSizeT();
}
out << "_hidl_err = "
@@ -439,7 +440,7 @@
<< " = 0; "
<< iteratorName
<< " < "
- << mSizes[dim]
+ << mSizes[dim]->javaValue()
<< "; ++"
<< iteratorName
<< ") {\n";
@@ -490,7 +491,7 @@
out << "type: " << getVtsType() << "\n";
out << "vector_value: {\n";
out.indent();
- out << "vector_size: " << mSizes[0] << "\n";
+ out << "vector_size: " << mSizes[0]->value() << "\n";
// Simple array case.
if (mSizes.size() == 1) {
status_t err = mElementType->emitVtsTypeDeclarations(out);
@@ -502,7 +503,7 @@
out << "type: " << getVtsType() << "\n";
out << "vector_value: {\n";
out.indent();
- out << "vector_size: " << mSizes[index] << "\n";
+ out << "vector_size: " << mSizes[index]->value() << "\n";
if (index == mSizes.size() - 1) {
status_t err = mElementType->emitVtsTypeDeclarations(out);
if (err != OK) {
@@ -526,14 +527,14 @@
mElementType->getAlignmentAndSize(align, size);
for (auto sizeInDimension : mSizes) {
- (*size) *= sizeInDimension;
+ (*size) *= sizeInDimension->castSizeT();
}
}
size_t ArrayType::dimension() const {
size_t numArrayElements = 1;
for (auto size : mSizes) {
- numArrayElements *= size;
+ numArrayElements *= size->castSizeT();
}
return numArrayElements;
}
diff --git a/ArrayType.h b/ArrayType.h
index 9704b32..e46c09a 100644
--- a/ArrayType.h
+++ b/ArrayType.h
@@ -126,8 +126,7 @@
private:
Type *mElementType;
- std::vector<size_t> mSizes;
- std::vector<std::string> mSizeComments;
+ std::vector<ConstantExpression *> mSizes;
size_t dimension() const;
diff --git a/ConstantExpression.cpp b/ConstantExpression.cpp
index 9c98046..06edf0c 100644
--- a/ConstantExpression.cpp
+++ b/ConstantExpression.cpp
@@ -169,10 +169,11 @@
*this = other;
}
-/* Copy constructor, with the expr overriden. */
+/* Copy constructor, with the expr overriden and treated non-trivial */
ConstantExpression::ConstantExpression(const ConstantExpression& other, std::string expr) {
*this = other;
mExpr = expr;
+ mTrivialDescription = false;
}
ConstantExpression& ConstantExpression::operator=(const ConstantExpression& other) {
@@ -180,12 +181,13 @@
mValueKind = other.mValueKind;
mValue = other.mValue;
mExpr = other.mExpr;
+ mTrivialDescription = other.mTrivialDescription;
return *this;
}
/* Literals. */
ConstantExpression::ConstantExpression(const char *value)
- : mExpr(value), mType(kConstExprLiteral) {
+ : mExpr(value), mType(kConstExprLiteral), mTrivialDescription(true) {
const char* head = value, *tail = head + strlen(value) - 1;
bool isLong = false, isUnsigned = false;
bool isHex = (value[0] == '0' && (value[1] == 'x' || value[1] == 'X'));
@@ -333,6 +335,10 @@
return mExpr;
}
+bool ConstantExpression::descriptionIsTrivial() const {
+ return mTrivialDescription;
+}
+
std::string ConstantExpression::value() const {
return rawValue(mValueKind);
}
diff --git a/ConstantExpression.h b/ConstantExpression.h
index ba1ca14..0140a75 100644
--- a/ConstantExpression.h
+++ b/ConstantExpression.h
@@ -69,6 +69,8 @@
std::string javaValue(ScalarType::Kind castKind) const;
/* Original expression with type. */
const std::string &description() const;
+ /* See mTrivialDescription */
+ bool descriptionIsTrivial() const;
/* Return a ConstantExpression that is 1 plus the original. */
ConstantExpression addOne() const;
/* Assignment operator. */
@@ -85,6 +87,8 @@
ScalarType::Kind mValueKind;
/* The stored result value. */
uint64_t mValue;
+ /* true if description() does not offer more information than value(). */
+ bool mTrivialDescription = false;
/*
* Helper function for all cpp/javaValue methods.