ConstantValueDecorator with strings
ConstantValueDecorator now handles array-type values better. Previously,
array values are passed to ConstantValueDecorator after joining into a
single string, which makes it difficult to handle array values because
it should rely on formatting ("{", Join(), "}) and "wrong" type.
Now, when the type is an array, raw_value is passed as vector<string>.
This is a preparation step for multi-dimensional fixed-size arrays.
Nested constant arrays like "{{1,2,3}, {4,5,6}}" will be used as a
default value for fixed-size arrays.
Bug: 204116012
Bug: 207087196
Test: aidl_unittests
Change-Id: I15353816dafcc80b55f1878f6bcc923e21cd12e8
diff --git a/aidl_const_expressions.cpp b/aidl_const_expressions.cpp
index 2d19efd..47d81e1 100644
--- a/aidl_const_expressions.cpp
+++ b/aidl_const_expressions.cpp
@@ -462,11 +462,13 @@
AidlConstantValue* AidlConstantValue::Array(
const AidlLocation& location, std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values) {
AIDL_FATAL_IF(values == nullptr, location);
+ // Reconstruct literal value
std::vector<std::string> str_values;
for (const auto& v : *values) {
str_values.push_back(v->value_);
}
- return new AidlConstantValue(location, Type::ARRAY, std::move(values), Join(str_values, ", "));
+ return new AidlConstantValue(location, Type::ARRAY, std::move(values),
+ "{" + Join(str_values, ", ") + "}");
}
AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) {
@@ -517,7 +519,7 @@
return decorator(type, value_);
}
- const string& type_string = type.GetName();
+ const string& type_string = type.Signature();
int err = 0;
switch (final_type_) {
@@ -566,11 +568,10 @@
bool success = true;
for (const auto& value : values_) {
- // 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);
+ string value_string;
+ type.ViewAsArrayBase([&](const auto& base_type) {
+ value_string = value->ValueString(base_type, decorator);
+ });
if (value_string.empty()) {
success = false;
break;
@@ -581,8 +582,7 @@
err = -1;
break;
}
-
- return decorator(type, "{" + Join(value_strings, ", ") + "}");
+ return decorator(type, value_strings);
}
case Type::FLOATING: {
if (type_string == "double") {