Revert "Revert "Add automatic default value for char-type field""

This reverts commit ac1cb3eb26525c868fd7dfeba90b6ee85161c9d8.

Original commit message:

Add automatic default value for char-type field

char type fields are auto-initialized with '\0' when not specified.

Ignore-AOSP-First: security fix
Bug: 206718630
Test: aidl_unittests

Reason for re-submit:
Conflicts resolved in the downstream branches.

Merged-In: I0001331785c0f11e6a1ebab7fef10e690392d417
Merged-In: I69d049f38a71a2c052fc8a7b098dc8525c940505
Change-Id: Ib0ce80da13419192ec8aada05445f2fb2ba5d6ea
(cherry picked from commit 6466daabe5cadd58674ea2c96b7f92255834dc42)
Merged-In:Ib0ce80da13419192ec8aada05445f2fb2ba5d6ea
diff --git a/aidl_const_expressions.cpp b/aidl_const_expressions.cpp
index b97889d..b9b62ea 100644
--- a/aidl_const_expressions.cpp
+++ b/aidl_const_expressions.cpp
@@ -242,12 +242,6 @@
   return false;
 }
 
-static bool isValidLiteralChar(char c) {
-  return !(c <= 0x1f ||  // control characters are < 0x20
-           c >= 0x7f ||  // DEL is 0x7f
-           c == '\\');   // Disallow backslashes for future proofing.
-}
-
 bool ParseFloating(std::string_view sv, double* parsed) {
   // float literal should be parsed successfully.
   android::base::ConsumeSuffix(&sv, "f");
@@ -338,6 +332,9 @@
   if (name == "boolean") {
     return Boolean(location, false);
   }
+  if (name == "char") {
+    return Character(location, "'\\0'");  // literal to be used in backends
+  }
   if (name == "byte" || name == "int" || name == "long") {
     return Integral(location, "0");
   }
@@ -354,13 +351,9 @@
   return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false");
 }
 
-AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) {
-  const std::string explicit_value = string("'") + value + "'";
-  if (!isValidLiteralChar(value)) {
-    AIDL_ERROR(location) << "Invalid character literal " << value;
-    return new AidlConstantValue(location, Type::ERROR, explicit_value);
-  }
-  return new AidlConstantValue(location, Type::CHARACTER, explicit_value);
+AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location,
+                                                const std::string& value) {
+  return new AidlConstantValue(location, Type::CHARACTER, value);
 }
 
 AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location,
@@ -453,14 +446,6 @@
 }
 
 AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) {
-  for (size_t i = 0; i < value.length(); ++i) {
-    if (!isValidLiteralChar(value[i])) {
-      AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '"
-                           << value << "'";
-      return new AidlConstantValue(location, Type::ERROR, value);
-    }
-  }
-
   return new AidlConstantValue(location, Type::STRING, value);
 }
 
@@ -1007,6 +992,8 @@
   return false;
 }
 
+// Constructor for integer(byte, int, long)
+// Keep parsed integer & literal
 AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type,
                                      int64_t parsed_value, const string& checked_value)
     : AidlNode(location),
@@ -1018,6 +1005,8 @@
   AIDL_FATAL_IF(type_ != Type::INT8 && type_ != Type::INT32 && type_ != Type::INT64, location);
 }
 
+// Constructor for non-integer(String, char, boolean, float, double)
+// Keep literal as it is. (e.g. String literal has double quotes at both ends)
 AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
                                      const string& checked_value)
     : AidlNode(location),
@@ -1037,6 +1026,7 @@
   }
 }
 
+// Constructor for array
 AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
                                      std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
                                      const std::string& value)