fix `byte[]` with negative values (C++/NDK)
In C++/NDK backends, byte[] is mapped to vector<uint8_t> while byte
alone is mapped to int8_t. When byte[]'s default value has negative
elements, resulting code won't compile without extra casting.
For example, while `byte[] a = {-1}` is perfectly valid, the C++ output
was `vector<uint8_t> a = {-1}`, which is an error.
Now, those negative values are casted to uint8_t when necessary. This is
fine because of how C++ casting(static_cast) works.
As a side-effect of this fix, C++ backend now doesn't emit extra value
casting (e.g. previously, `int32_t n = int32_t(1);`, but now it's just
`int32_t n = 1;`).
Bug: 204116012
Test: m (no regression)
Test: aidl_unittests
Merged-In: I9bbe9b4e6109502f182fc40bf1dc721ea9738971
Change-Id: I9bbe9b4e6109502f182fc40bf1dc721ea9738971
(cherry picked from commit a96a1090478a3fec8f4db9a2b423c514b4a40b3c)
diff --git a/aidl_const_expressions.cpp b/aidl_const_expressions.cpp
index 0521cea..2d19efd 100644
--- a/aidl_const_expressions.cpp
+++ b/aidl_const_expressions.cpp
@@ -502,7 +502,7 @@
}
const AidlDefinedType* defined_type = type.GetDefinedType();
- if (defined_type && !type.IsArray()) {
+ if (defined_type && final_type_ != Type::ARRAY) {
const AidlEnumDeclaration* enum_type = defined_type->AsEnumDeclaration();
if (!enum_type) {
AIDL_ERROR(this) << "Invalid type (" << defined_type->GetCanonicalName()
@@ -566,11 +566,11 @@
bool success = true;
for (const auto& value : values_) {
- string value_string;
- type.ViewAsArrayBase([&](const AidlTypeSpecifier& base) {
- value_string = value->ValueString(base, decorator);
- });
-
+ // Pass array type(T[]) as it is instead of converting it to base type(T)
+ // so that decorator can decorate the value in the context of array.
+ // In C++/NDK, 'byte[]' and 'byte' are mapped to different types. If we pass 'byte'
+ // decorator can't know the value should be treated as 'uint8_t'.
+ string value_string = value->ValueString(type, decorator);
if (value_string.empty()) {
success = false;
break;