Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 17 | #include "aidl_language.h" |
Steven Moreland | 4bcb05c | 2019-11-27 18:57:47 -0800 | [diff] [blame] | 18 | #include "aidl_typenames.h" |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 19 | #include "logging.h" |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <algorithm> |
| 23 | #include <iostream> |
| 24 | #include <memory> |
| 25 | |
| 26 | #include <android-base/parsedouble.h> |
| 27 | #include <android-base/parseint.h> |
| 28 | #include <android-base/strings.h> |
| 29 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 30 | using android::base::ConsumeSuffix; |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 31 | using android::base::EndsWith; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 32 | using android::base::Join; |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 33 | using android::base::StartsWith; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 34 | using std::string; |
| 35 | using std::unique_ptr; |
| 36 | using std::vector; |
| 37 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 38 | #define SHOULD_NOT_REACH() CHECK(false) << LOG(FATAL) << ": should not reach here: " |
| 39 | #define OPEQ(__y__) (string(op_) == string(__y__)) |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 40 | #define COMPUTE_UNARY(__op__) \ |
| 41 | if (op == string(#__op__)) { \ |
| 42 | *out = __op__ val; \ |
| 43 | return true; \ |
| 44 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 45 | #define COMPUTE_BINARY(__op__) \ |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 46 | if (op == string(#__op__)) { \ |
| 47 | *out = lval __op__ rval; \ |
| 48 | return true; \ |
| 49 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 50 | #define OP_IS_BIN_ARITHMETIC (OPEQ("+") || OPEQ("-") || OPEQ("*") || OPEQ("/") || OPEQ("%")) |
| 51 | #define OP_IS_BIN_BITFLIP (OPEQ("|") || OPEQ("^") || OPEQ("&")) |
| 52 | #define OP_IS_BIN_COMP \ |
| 53 | (OPEQ("<") || OPEQ(">") || OPEQ("<=") || OPEQ(">=") || OPEQ("==") || OPEQ("!=")) |
| 54 | #define OP_IS_BIN_SHIFT (OPEQ(">>") || OPEQ("<<")) |
| 55 | #define OP_IS_BIN_LOGICAL (OPEQ("||") || OPEQ("&&")) |
| 56 | |
| 57 | // NOLINT to suppress missing parentheses warnings about __def__. |
| 58 | #define SWITCH_KIND(__cond__, __action__, __def__) \ |
| 59 | switch (__cond__) { \ |
| 60 | case Type::BOOLEAN: \ |
| 61 | __action__(bool); \ |
| 62 | case Type::INT8: \ |
| 63 | __action__(int8_t); \ |
| 64 | case Type::INT32: \ |
| 65 | __action__(int32_t); \ |
| 66 | case Type::INT64: \ |
| 67 | __action__(int64_t); \ |
| 68 | default: \ |
| 69 | __def__; /* NOLINT */ \ |
| 70 | } |
| 71 | |
| 72 | template <class T> |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 73 | bool handleUnary(const AidlConstantValue& context, const string& op, T val, int64_t* out) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 74 | COMPUTE_UNARY(+) |
| 75 | COMPUTE_UNARY(-) |
| 76 | COMPUTE_UNARY(!) |
| 77 | COMPUTE_UNARY(~) |
Steven Moreland | 720a3cc | 2020-07-16 23:44:59 +0000 | [diff] [blame] | 78 | AIDL_FATAL(context) << "Could not handleUnary for " << op << " " << val; |
| 79 | return false; |
| 80 | } |
| 81 | template <> |
| 82 | bool handleUnary<bool>(const AidlConstantValue& context, const string& op, bool val, int64_t* out) { |
| 83 | COMPUTE_UNARY(+) |
| 84 | COMPUTE_UNARY(-) |
| 85 | COMPUTE_UNARY(!) |
Yifan Hong | f17e3a7 | 2020-02-20 17:34:58 -0800 | [diff] [blame] | 86 | |
Steven Moreland | 720a3cc | 2020-07-16 23:44:59 +0000 | [diff] [blame] | 87 | if (op == "~") { |
| 88 | AIDL_ERROR(context) << "Bitwise negation of a boolean expression is always true."; |
| 89 | return false; |
| 90 | } |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 91 | AIDL_FATAL(context) << "Could not handleUnary for " << op << " " << val; |
| 92 | return false; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | template <class T> |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 96 | bool handleBinaryCommon(const AidlConstantValue& context, T lval, const string& op, T rval, |
| 97 | int64_t* out) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 98 | COMPUTE_BINARY(+) |
| 99 | COMPUTE_BINARY(-) |
| 100 | COMPUTE_BINARY(*) |
| 101 | COMPUTE_BINARY(/) |
| 102 | COMPUTE_BINARY(%) |
| 103 | COMPUTE_BINARY(|) |
| 104 | COMPUTE_BINARY(^) |
| 105 | COMPUTE_BINARY(&) |
| 106 | // comparison operators: return 0 or 1 by nature. |
| 107 | COMPUTE_BINARY(==) |
| 108 | COMPUTE_BINARY(!=) |
| 109 | COMPUTE_BINARY(<) |
| 110 | COMPUTE_BINARY(>) |
| 111 | COMPUTE_BINARY(<=) |
| 112 | COMPUTE_BINARY(>=) |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 113 | |
| 114 | AIDL_FATAL(context) << "Could not handleBinaryCommon for " << lval << " " << op << " " << rval; |
| 115 | return false; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | template <class T> |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 119 | bool handleShift(const AidlConstantValue& context, T lval, const string& op, int64_t rval, |
| 120 | int64_t* out) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 121 | // just cast rval to int64_t and it should fit. |
| 122 | COMPUTE_BINARY(>>) |
| 123 | COMPUTE_BINARY(<<) |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 124 | |
| 125 | AIDL_FATAL(context) << "Could not handleShift for " << lval << " " << op << " " << rval; |
| 126 | return false; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 129 | bool handleLogical(const AidlConstantValue& context, bool lval, const string& op, bool rval, |
| 130 | int64_t* out) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 131 | COMPUTE_BINARY(||); |
| 132 | COMPUTE_BINARY(&&); |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 133 | |
| 134 | AIDL_FATAL(context) << "Could not handleLogical for " << lval << " " << op << " " << rval; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 135 | return false; |
| 136 | } |
| 137 | |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 138 | static bool isValidLiteralChar(char c) { |
| 139 | return !(c <= 0x1f || // control characters are < 0x20 |
| 140 | c >= 0x7f || // DEL is 0x7f |
| 141 | c == '\\'); // Disallow backslashes for future proofing. |
| 142 | } |
| 143 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 144 | bool AidlUnaryConstExpression::IsCompatibleType(Type type, const string& op) { |
| 145 | // Verify the unary type here |
| 146 | switch (type) { |
| 147 | case Type::BOOLEAN: // fall-through |
| 148 | case Type::INT8: // fall-through |
| 149 | case Type::INT32: // fall-through |
| 150 | case Type::INT64: |
| 151 | return true; |
| 152 | case Type::FLOATING: |
| 153 | return (op == "+" || op == "-"); |
| 154 | default: |
| 155 | return false; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | bool AidlBinaryConstExpression::AreCompatibleTypes(Type t1, Type t2) { |
| 160 | switch (t1) { |
| 161 | case Type::STRING: |
| 162 | if (t2 == Type::STRING) { |
| 163 | return true; |
| 164 | } |
| 165 | break; |
| 166 | case Type::BOOLEAN: // fall-through |
| 167 | case Type::INT8: // fall-through |
| 168 | case Type::INT32: // fall-through |
| 169 | case Type::INT64: |
| 170 | switch (t2) { |
| 171 | case Type::BOOLEAN: // fall-through |
| 172 | case Type::INT8: // fall-through |
| 173 | case Type::INT32: // fall-through |
| 174 | case Type::INT64: |
| 175 | return true; |
| 176 | break; |
| 177 | default: |
| 178 | break; |
| 179 | } |
| 180 | break; |
| 181 | default: |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | // Returns the promoted kind for both operands |
| 189 | AidlConstantValue::Type AidlBinaryConstExpression::UsualArithmeticConversion(Type left, |
| 190 | Type right) { |
| 191 | // These are handled as special cases |
| 192 | CHECK(left != Type::STRING && right != Type::STRING); |
| 193 | CHECK(left != Type::FLOATING && right != Type::FLOATING); |
| 194 | |
| 195 | // Kinds in concern: bool, (u)int[8|32|64] |
| 196 | if (left == right) return left; // easy case |
| 197 | if (left == Type::BOOLEAN) return right; |
| 198 | if (right == Type::BOOLEAN) return left; |
| 199 | |
| 200 | return left < right ? right : left; |
| 201 | } |
| 202 | |
| 203 | // Returns the promoted integral type where INT32 is the smallest type |
| 204 | AidlConstantValue::Type AidlBinaryConstExpression::IntegralPromotion(Type in) { |
| 205 | return (Type::INT32 < in) ? in : Type::INT32; |
| 206 | } |
| 207 | |
| 208 | template <typename T> |
| 209 | T AidlConstantValue::cast() const { |
| 210 | CHECK(is_evaluated_ == true); |
| 211 | |
| 212 | #define CASE_CAST_T(__type__) return static_cast<T>(static_cast<__type__>(final_value_)); |
| 213 | |
| 214 | SWITCH_KIND(final_type_, CASE_CAST_T, SHOULD_NOT_REACH(); return 0;); |
| 215 | } |
| 216 | |
Steven Moreland | 541788d | 2020-05-21 22:05:52 +0000 | [diff] [blame] | 217 | AidlConstantValue* AidlConstantValue::Default(const AidlTypeSpecifier& specifier) { |
| 218 | AidlLocation location = specifier.GetLocation(); |
| 219 | |
| 220 | // allocation of int[0] is a bit wasteful in Java |
| 221 | if (specifier.IsArray()) { |
| 222 | return nullptr; |
| 223 | } |
| 224 | |
| 225 | const std::string name = specifier.GetName(); |
| 226 | if (name == "boolean") { |
| 227 | return Boolean(location, false); |
| 228 | } |
| 229 | if (name == "byte" || name == "int" || name == "long") { |
| 230 | return Integral(location, "0"); |
| 231 | } |
| 232 | if (name == "float") { |
| 233 | return Floating(location, "0.0f"); |
| 234 | } |
| 235 | if (name == "double") { |
| 236 | return Floating(location, "0.0"); |
| 237 | } |
| 238 | return nullptr; |
| 239 | } |
| 240 | |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 241 | AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool value) { |
| 242 | return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false"); |
| 243 | } |
| 244 | |
| 245 | AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) { |
Steven Moreland | cdedd9b | 2019-12-02 10:54:47 -0800 | [diff] [blame] | 246 | const std::string explicit_value = string("'") + value + "'"; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 247 | if (!isValidLiteralChar(value)) { |
| 248 | AIDL_ERROR(location) << "Invalid character literal " << value; |
Steven Moreland | cdedd9b | 2019-12-02 10:54:47 -0800 | [diff] [blame] | 249 | return new AidlConstantValue(location, Type::ERROR, explicit_value); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 250 | } |
Steven Moreland | cdedd9b | 2019-12-02 10:54:47 -0800 | [diff] [blame] | 251 | return new AidlConstantValue(location, Type::CHARACTER, explicit_value); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location, |
| 255 | const std::string& value) { |
| 256 | return new AidlConstantValue(location, Type::FLOATING, value); |
| 257 | } |
| 258 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 259 | bool AidlConstantValue::IsHex(const string& value) { |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 260 | return StartsWith(value, "0x") || StartsWith(value, "0X"); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 263 | bool AidlConstantValue::ParseIntegral(const string& value, int64_t* parsed_value, |
| 264 | Type* parsed_type) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 265 | if (parsed_value == nullptr || parsed_type == nullptr) { |
| 266 | return false; |
| 267 | } |
| 268 | |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 269 | const bool isLong = EndsWith(value, 'l') || EndsWith(value, 'L'); |
| 270 | const std::string value_substr = isLong ? value.substr(0, value.size() - 1) : value; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 271 | |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 272 | if (IsHex(value)) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 273 | // AIDL considers 'const int foo = 0xffffffff' as -1, but if we want to |
| 274 | // handle that when computing constant expressions, then we need to |
| 275 | // represent 0xffffffff as a uint32_t. However, AIDL only has signed types; |
| 276 | // so we parse as an unsigned int when possible and then cast to a signed |
| 277 | // int. One example of this is in ICameraService.aidl where a constant int |
| 278 | // is used for bit manipulations which ideally should be handled with an |
| 279 | // unsigned int. |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 280 | // |
| 281 | // Note, for historical consistency, we need to consider small hex values |
| 282 | // as an integral type. Recognizing them as INT8 could break some files, |
| 283 | // even though it would simplify this code. |
| 284 | if (uint32_t rawValue32; |
| 285 | !isLong && android::base::ParseUint<uint32_t>(value_substr, &rawValue32)) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 286 | *parsed_value = static_cast<int32_t>(rawValue32); |
| 287 | *parsed_type = Type::INT32; |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 288 | } else if (uint64_t rawValue64; android::base::ParseUint<uint64_t>(value_substr, &rawValue64)) { |
| 289 | *parsed_value = static_cast<int64_t>(rawValue64); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 290 | *parsed_type = Type::INT64; |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 291 | } else { |
| 292 | *parsed_value = 0; |
| 293 | *parsed_type = Type::ERROR; |
| 294 | return false; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 295 | } |
| 296 | return true; |
| 297 | } |
| 298 | |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 299 | if (!android::base::ParseInt<int64_t>(value_substr, parsed_value)) { |
| 300 | *parsed_value = 0; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 301 | *parsed_type = Type::ERROR; |
| 302 | return false; |
| 303 | } |
| 304 | |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 305 | if (isLong) { |
| 306 | *parsed_type = Type::INT64; |
| 307 | } else { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 308 | // guess literal type. |
| 309 | if (*parsed_value <= INT8_MAX && *parsed_value >= INT8_MIN) { |
| 310 | *parsed_type = Type::INT8; |
| 311 | } else if (*parsed_value <= INT32_MAX && *parsed_value >= INT32_MIN) { |
| 312 | *parsed_type = Type::INT32; |
| 313 | } else { |
| 314 | *parsed_type = Type::INT64; |
| 315 | } |
| 316 | } |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location, const string& value) { |
| 321 | CHECK(!value.empty()); |
| 322 | |
| 323 | Type parsed_type; |
| 324 | int64_t parsed_value = 0; |
| 325 | bool success = ParseIntegral(value, &parsed_value, &parsed_type); |
| 326 | if (!success) { |
| 327 | return nullptr; |
| 328 | } |
| 329 | |
| 330 | return new AidlConstantValue(location, parsed_type, parsed_value, value); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | AidlConstantValue* AidlConstantValue::Array( |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 334 | const AidlLocation& location, std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values) { |
Steven Moreland | 541788d | 2020-05-21 22:05:52 +0000 | [diff] [blame] | 335 | CHECK(values != nullptr) << location; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 336 | return new AidlConstantValue(location, Type::ARRAY, std::move(values)); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 339 | AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) { |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 340 | for (size_t i = 0; i < value.length(); ++i) { |
| 341 | if (!isValidLiteralChar(value[i])) { |
| 342 | AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '" |
| 343 | << value << "'"; |
Steven Moreland | cdedd9b | 2019-12-02 10:54:47 -0800 | [diff] [blame] | 344 | return new AidlConstantValue(location, Type::ERROR, value); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
| 348 | return new AidlConstantValue(location, Type::STRING, value); |
| 349 | } |
| 350 | |
Daniel Norman | f0ca44f | 2019-10-25 09:59:44 -0700 | [diff] [blame] | 351 | AidlConstantValue* AidlConstantValue::ShallowIntegralCopy(const AidlConstantValue& other) { |
Daniel Norman | 3cce7cd | 2020-02-07 13:25:12 -0800 | [diff] [blame] | 352 | // TODO(b/141313220) Perform a full copy instead of parsing+unparsing |
Daniel Norman | f0ca44f | 2019-10-25 09:59:44 -0700 | [diff] [blame] | 353 | AidlTypeSpecifier type = AidlTypeSpecifier(AIDL_LOCATION_HERE, "long", false, nullptr, ""); |
Steven Moreland | 6560661 | 2019-11-10 21:21:25 -0800 | [diff] [blame] | 354 | // TODO(b/142722772) CheckValid() should be called before ValueString() |
Steven Moreland | cdedd9b | 2019-12-02 10:54:47 -0800 | [diff] [blame] | 355 | if (!other.CheckValid() || !other.evaluate(type)) { |
Steven Moreland | 59e53e4 | 2019-11-26 20:38:08 -0800 | [diff] [blame] | 356 | AIDL_ERROR(other) << "Failed to parse expression as integer: " << other.value_; |
| 357 | return nullptr; |
| 358 | } |
| 359 | const std::string& value = other.ValueString(type, AidlConstantValueDecorator); |
| 360 | if (value.empty()) { |
| 361 | return nullptr; // error already logged |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 362 | } |
Daniel Norman | f0ca44f | 2019-10-25 09:59:44 -0700 | [diff] [blame] | 363 | |
Steven Moreland | 59e53e4 | 2019-11-26 20:38:08 -0800 | [diff] [blame] | 364 | AidlConstantValue* result = Integral(AIDL_LOCATION_HERE, value); |
Daniel Norman | f0ca44f | 2019-10-25 09:59:44 -0700 | [diff] [blame] | 365 | if (result == nullptr) { |
| 366 | AIDL_FATAL(other) << "Unable to perform ShallowIntegralCopy."; |
| 367 | } |
| 368 | return result; |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 371 | string AidlConstantValue::ValueString(const AidlTypeSpecifier& type, |
| 372 | const ConstantValueDecorator& decorator) const { |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 373 | if (type.IsGeneric()) { |
| 374 | AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal."; |
| 375 | return ""; |
| 376 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 377 | if (!is_evaluated_) { |
| 378 | // TODO(b/142722772) CheckValid() should be called before ValueString() |
| 379 | bool success = CheckValid(); |
| 380 | success &= evaluate(type); |
| 381 | if (!success) { |
| 382 | // the detailed error message shall be printed in evaluate |
| 383 | return ""; |
| 384 | } |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 385 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 386 | if (!is_valid_) { |
| 387 | AIDL_ERROR(this) << "Invalid constant value: " + value_; |
| 388 | return ""; |
| 389 | } |
| 390 | const string& type_string = type.GetName(); |
| 391 | int err = 0; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 392 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 393 | switch (final_type_) { |
| 394 | case Type::CHARACTER: |
| 395 | if (type_string == "char") { |
| 396 | return decorator(type, final_string_value_); |
| 397 | } |
| 398 | err = -1; |
| 399 | break; |
| 400 | case Type::STRING: |
| 401 | if (type_string == "String") { |
| 402 | return decorator(type, final_string_value_); |
| 403 | } |
| 404 | err = -1; |
| 405 | break; |
| 406 | case Type::BOOLEAN: // fall-through |
| 407 | case Type::INT8: // fall-through |
| 408 | case Type::INT32: // fall-through |
| 409 | case Type::INT64: |
| 410 | if (type_string == "byte") { |
| 411 | if (final_value_ > INT8_MAX || final_value_ < INT8_MIN) { |
| 412 | err = -1; |
| 413 | break; |
| 414 | } |
| 415 | return decorator(type, std::to_string(static_cast<int8_t>(final_value_))); |
| 416 | } else if (type_string == "int") { |
| 417 | if (final_value_ > INT32_MAX || final_value_ < INT32_MIN) { |
| 418 | err = -1; |
| 419 | break; |
| 420 | } |
| 421 | return decorator(type, std::to_string(static_cast<int32_t>(final_value_))); |
| 422 | } else if (type_string == "long") { |
| 423 | return decorator(type, std::to_string(final_value_)); |
| 424 | } else if (type_string == "boolean") { |
| 425 | return decorator(type, final_value_ ? "true" : "false"); |
| 426 | } |
| 427 | err = -1; |
| 428 | break; |
| 429 | case Type::ARRAY: { |
| 430 | if (!type.IsArray()) { |
| 431 | err = -1; |
| 432 | break; |
| 433 | } |
| 434 | vector<string> value_strings; |
| 435 | value_strings.reserve(values_.size()); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 436 | bool success = true; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 437 | |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 438 | for (const auto& value : values_) { |
| 439 | const AidlTypeSpecifier& array_base = type.ArrayBase(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 440 | const string value_string = value->ValueString(array_base, decorator); |
| 441 | if (value_string.empty()) { |
| 442 | success = false; |
| 443 | break; |
| 444 | } |
| 445 | value_strings.push_back(value_string); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 446 | } |
| 447 | if (!success) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 448 | err = -1; |
| 449 | break; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 450 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 451 | |
| 452 | return decorator(type, "{" + Join(value_strings, ", ") + "}"); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 453 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 454 | case Type::FLOATING: { |
| 455 | std::string_view raw_view(value_.c_str()); |
| 456 | bool is_float_literal = ConsumeSuffix(&raw_view, "f"); |
| 457 | std::string stripped_value = std::string(raw_view); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 458 | |
| 459 | if (type_string == "double") { |
| 460 | double parsed_value; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 461 | if (!android::base::ParseDouble(stripped_value, &parsed_value)) { |
| 462 | AIDL_ERROR(this) << "Could not parse " << value_; |
| 463 | err = -1; |
| 464 | break; |
| 465 | } |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 466 | return decorator(type, std::to_string(parsed_value)); |
| 467 | } |
| 468 | if (is_float_literal && type_string == "float") { |
| 469 | float parsed_value; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 470 | if (!android::base::ParseFloat(stripped_value, &parsed_value)) { |
| 471 | AIDL_ERROR(this) << "Could not parse " << value_; |
| 472 | err = -1; |
| 473 | break; |
| 474 | } |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 475 | return decorator(type, std::to_string(parsed_value) + "f"); |
| 476 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 477 | err = -1; |
| 478 | break; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 479 | } |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 480 | default: |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 481 | err = -1; |
| 482 | break; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 485 | CHECK(err != 0); |
| 486 | AIDL_ERROR(this) << "Invalid type specifier for " << ToString(final_type_) << ": " << type_string; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 487 | return ""; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | bool AidlConstantValue::CheckValid() const { |
| 491 | // Nothing needs to be checked here. The constant value will be validated in |
| 492 | // the constructor or in the evaluate() function. |
| 493 | if (is_evaluated_) return is_valid_; |
| 494 | |
| 495 | switch (type_) { |
| 496 | case Type::BOOLEAN: // fall-through |
| 497 | case Type::INT8: // fall-through |
| 498 | case Type::INT32: // fall-through |
| 499 | case Type::INT64: // fall-through |
| 500 | case Type::ARRAY: // fall-through |
| 501 | case Type::CHARACTER: // fall-through |
| 502 | case Type::STRING: // fall-through |
| 503 | case Type::FLOATING: // fall-through |
| 504 | case Type::UNARY: // fall-through |
| 505 | case Type::BINARY: |
| 506 | is_valid_ = true; |
| 507 | break; |
Steven Moreland | 4ff04aa | 2019-12-02 10:44:29 -0800 | [diff] [blame] | 508 | case Type::ERROR: |
| 509 | return false; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 510 | default: |
| 511 | AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_); |
| 512 | return false; |
| 513 | } |
| 514 | |
| 515 | return true; |
| 516 | } |
| 517 | |
| 518 | bool AidlConstantValue::evaluate(const AidlTypeSpecifier& type) const { |
| 519 | if (is_evaluated_) { |
| 520 | return is_valid_; |
| 521 | } |
| 522 | int err = 0; |
| 523 | is_evaluated_ = true; |
| 524 | |
| 525 | switch (type_) { |
| 526 | case Type::ARRAY: { |
| 527 | if (!type.IsArray()) { |
| 528 | AIDL_ERROR(this) << "Invalid constant array type: " << type.GetName(); |
| 529 | err = -1; |
| 530 | break; |
| 531 | } |
| 532 | Type array_type = Type::ERROR; |
| 533 | bool success = true; |
| 534 | for (const auto& value : values_) { |
| 535 | success = value->CheckValid(); |
| 536 | if (success) { |
| 537 | success = value->evaluate(type.ArrayBase()); |
| 538 | if (!success) { |
| 539 | AIDL_ERROR(this) << "Invalid array element: " << value->value_; |
| 540 | break; |
| 541 | } |
| 542 | if (array_type == Type::ERROR) { |
| 543 | array_type = value->final_type_; |
| 544 | } else if (!AidlBinaryConstExpression::AreCompatibleTypes(array_type, |
| 545 | value->final_type_)) { |
| 546 | AIDL_ERROR(this) << "Incompatible array element type: " << ToString(value->final_type_) |
| 547 | << ". Expecting type compatible with " << ToString(array_type); |
| 548 | success = false; |
| 549 | break; |
| 550 | } |
| 551 | } else { |
| 552 | break; |
| 553 | } |
| 554 | } |
| 555 | if (!success) { |
| 556 | err = -1; |
| 557 | break; |
| 558 | } |
| 559 | final_type_ = type_; |
| 560 | break; |
| 561 | } |
| 562 | case Type::BOOLEAN: |
| 563 | if ((value_ != "true") && (value_ != "false")) { |
| 564 | AIDL_ERROR(this) << "Invalid constant boolean value: " << value_; |
| 565 | err = -1; |
| 566 | break; |
| 567 | } |
| 568 | final_value_ = (value_ == "true") ? 1 : 0; |
| 569 | final_type_ = type_; |
| 570 | break; |
| 571 | case Type::INT8: // fall-through |
| 572 | case Type::INT32: // fall-through |
| 573 | case Type::INT64: |
| 574 | // Parsing happens in the constructor |
| 575 | final_type_ = type_; |
| 576 | break; |
| 577 | case Type::CHARACTER: // fall-through |
| 578 | case Type::STRING: |
| 579 | final_string_value_ = value_; |
| 580 | final_type_ = type_; |
| 581 | break; |
| 582 | case Type::FLOATING: |
| 583 | // Just parse on the fly in ValueString |
| 584 | final_type_ = type_; |
| 585 | break; |
| 586 | default: |
| 587 | AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_); |
| 588 | err = -1; |
| 589 | } |
| 590 | |
| 591 | return (err == 0) ? true : false; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | string AidlConstantValue::ToString(Type type) { |
| 595 | switch (type) { |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 596 | case Type::BOOLEAN: |
| 597 | return "a literal boolean"; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 598 | case Type::INT8: |
| 599 | return "an int8 literal"; |
| 600 | case Type::INT32: |
| 601 | return "an int32 literal"; |
| 602 | case Type::INT64: |
| 603 | return "an int64 literal"; |
Steven Moreland | a923a72 | 2019-11-26 20:08:30 -0800 | [diff] [blame] | 604 | case Type::ARRAY: |
| 605 | return "a literal array"; |
| 606 | case Type::CHARACTER: |
| 607 | return "a literal char"; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 608 | case Type::STRING: |
| 609 | return "a literal string"; |
Steven Moreland | a923a72 | 2019-11-26 20:08:30 -0800 | [diff] [blame] | 610 | case Type::FLOATING: |
| 611 | return "a literal float"; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 612 | case Type::UNARY: |
| 613 | return "a unary expression"; |
| 614 | case Type::BINARY: |
| 615 | return "a binary expression"; |
Steven Moreland | a923a72 | 2019-11-26 20:08:30 -0800 | [diff] [blame] | 616 | case Type::ERROR: |
| 617 | LOG(FATAL) << "aidl internal error: error type failed to halt program"; |
| 618 | return ""; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 619 | default: |
| 620 | LOG(FATAL) << "aidl internal error: unknown constant type: " << static_cast<int>(type); |
| 621 | return ""; // not reached |
| 622 | } |
| 623 | } |
| 624 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 625 | bool AidlUnaryConstExpression::CheckValid() const { |
| 626 | if (is_evaluated_) return is_valid_; |
| 627 | CHECK(unary_ != nullptr); |
| 628 | |
| 629 | is_valid_ = unary_->CheckValid(); |
| 630 | if (!is_valid_) { |
| 631 | final_type_ = Type::ERROR; |
| 632 | return false; |
| 633 | } |
| 634 | |
Steven Moreland | 4bcb05c | 2019-11-27 18:57:47 -0800 | [diff] [blame] | 635 | return AidlConstantValue::CheckValid(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | bool AidlUnaryConstExpression::evaluate(const AidlTypeSpecifier& type) const { |
| 639 | if (is_evaluated_) { |
| 640 | return is_valid_; |
| 641 | } |
| 642 | is_evaluated_ = true; |
| 643 | |
| 644 | // Recursively evaluate the expression tree |
| 645 | if (!unary_->is_evaluated_) { |
| 646 | // TODO(b/142722772) CheckValid() should be called before ValueString() |
| 647 | bool success = CheckValid(); |
| 648 | success &= unary_->evaluate(type); |
| 649 | if (!success) { |
| 650 | is_valid_ = false; |
| 651 | return false; |
| 652 | } |
| 653 | } |
Devin Moore | c233fb8 | 2020-04-07 11:13:44 -0700 | [diff] [blame] | 654 | if (!IsCompatibleType(unary_->final_type_, op_)) { |
| 655 | AIDL_ERROR(unary_) << "'" << op_ << "'" |
| 656 | << " is not compatible with " << ToString(unary_->final_type_) |
| 657 | << ": " + value_; |
| 658 | is_valid_ = false; |
| 659 | return false; |
| 660 | } |
| 661 | if (!unary_->is_valid_) { |
| 662 | AIDL_ERROR(unary_) << "Invalid constant unary expression: " + value_; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 663 | is_valid_ = false; |
| 664 | return false; |
| 665 | } |
| 666 | final_type_ = unary_->final_type_; |
| 667 | |
| 668 | if (final_type_ == Type::FLOATING) { |
| 669 | // don't do anything here. ValueString() will handle everything. |
| 670 | is_valid_ = true; |
| 671 | return true; |
| 672 | } |
| 673 | |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 674 | #define CASE_UNARY(__type__) \ |
| 675 | return handleUnary(*this, op_, static_cast<__type__>(unary_->final_value_), &final_value_); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 676 | |
| 677 | SWITCH_KIND(final_type_, CASE_UNARY, SHOULD_NOT_REACH(); final_type_ = Type::ERROR; |
| 678 | is_valid_ = false; return false;) |
| 679 | } |
| 680 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 681 | bool AidlBinaryConstExpression::CheckValid() const { |
| 682 | bool success = false; |
| 683 | if (is_evaluated_) return is_valid_; |
| 684 | CHECK(left_val_ != nullptr); |
| 685 | CHECK(right_val_ != nullptr); |
| 686 | |
| 687 | success = left_val_->CheckValid(); |
| 688 | if (!success) { |
| 689 | final_type_ = Type::ERROR; |
| 690 | AIDL_ERROR(this) << "Invalid left operand in binary expression: " + value_; |
| 691 | } |
| 692 | |
| 693 | success = right_val_->CheckValid(); |
| 694 | if (!success) { |
| 695 | AIDL_ERROR(this) << "Invalid right operand in binary expression: " + value_; |
| 696 | final_type_ = Type::ERROR; |
| 697 | } |
| 698 | |
| 699 | if (final_type_ == Type::ERROR) { |
| 700 | is_valid_ = false; |
| 701 | return false; |
| 702 | } |
| 703 | |
| 704 | is_valid_ = true; |
Steven Moreland | 4bcb05c | 2019-11-27 18:57:47 -0800 | [diff] [blame] | 705 | return AidlConstantValue::CheckValid(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | bool AidlBinaryConstExpression::evaluate(const AidlTypeSpecifier& type) const { |
| 709 | if (is_evaluated_) { |
| 710 | return is_valid_; |
| 711 | } |
| 712 | is_evaluated_ = true; |
| 713 | CHECK(left_val_ != nullptr); |
| 714 | CHECK(right_val_ != nullptr); |
| 715 | |
| 716 | // Recursively evaluate the binary expression tree |
| 717 | if (!left_val_->is_evaluated_ || !right_val_->is_evaluated_) { |
| 718 | // TODO(b/142722772) CheckValid() should be called before ValueString() |
| 719 | bool success = CheckValid(); |
| 720 | success &= left_val_->evaluate(type); |
| 721 | success &= right_val_->evaluate(type); |
| 722 | if (!success) { |
| 723 | is_valid_ = false; |
| 724 | return false; |
| 725 | } |
| 726 | } |
| 727 | if (!left_val_->is_valid_ || !right_val_->is_valid_) { |
| 728 | is_valid_ = false; |
| 729 | return false; |
| 730 | } |
| 731 | is_valid_ = AreCompatibleTypes(left_val_->final_type_, right_val_->final_type_); |
| 732 | if (!is_valid_) { |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP; |
| 737 | |
| 738 | // Handle String case first |
| 739 | if (left_val_->final_type_ == Type::STRING) { |
| 740 | if (!OPEQ("+")) { |
| 741 | // invalid operation on strings |
| 742 | final_type_ = Type::ERROR; |
| 743 | is_valid_ = false; |
| 744 | return false; |
| 745 | } |
| 746 | |
| 747 | // Remove trailing " from lhs |
| 748 | const string& lhs = left_val_->final_string_value_; |
| 749 | if (lhs.back() != '"') { |
| 750 | AIDL_ERROR(this) << "'" << lhs << "' is missing a trailing quote."; |
| 751 | final_type_ = Type::ERROR; |
| 752 | is_valid_ = false; |
| 753 | return false; |
| 754 | } |
| 755 | const string& rhs = right_val_->final_string_value_; |
| 756 | // Remove starting " from rhs |
| 757 | if (rhs.front() != '"') { |
| 758 | AIDL_ERROR(this) << "'" << rhs << "' is missing a leading quote."; |
| 759 | final_type_ = Type::ERROR; |
| 760 | is_valid_ = false; |
| 761 | return false; |
| 762 | } |
| 763 | |
| 764 | final_string_value_ = string(lhs.begin(), lhs.end() - 1).append(rhs.begin() + 1, rhs.end()); |
| 765 | final_type_ = Type::STRING; |
| 766 | return true; |
| 767 | } |
| 768 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 769 | // CASE: + - * / % | ^ & < > <= >= == != |
| 770 | if (isArithmeticOrBitflip || OP_IS_BIN_COMP) { |
Steven Moreland | 485a51a | 2019-11-26 20:52:58 -0800 | [diff] [blame] | 771 | if ((op_ == "/" || op_ == "%") && right_val_->final_value_ == 0) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 772 | final_type_ = Type::ERROR; |
| 773 | is_valid_ = false; |
Steven Moreland | 485a51a | 2019-11-26 20:52:58 -0800 | [diff] [blame] | 774 | AIDL_ERROR(this) << "Cannot do division operation with zero for expression: " + value_; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 775 | return false; |
| 776 | } |
| 777 | |
| 778 | // promoted kind for both operands. |
| 779 | Type promoted = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_), |
| 780 | IntegralPromotion(right_val_->final_type_)); |
| 781 | // result kind. |
| 782 | final_type_ = isArithmeticOrBitflip |
| 783 | ? promoted // arithmetic or bitflip operators generates promoted type |
| 784 | : Type::BOOLEAN; // comparison operators generates bool |
| 785 | |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 786 | #define CASE_BINARY_COMMON(__type__) \ |
| 787 | return handleBinaryCommon(*this, static_cast<__type__>(left_val_->final_value_), op_, \ |
| 788 | static_cast<__type__>(right_val_->final_value_), &final_value_); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 789 | |
| 790 | SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); final_type_ = Type::ERROR; |
| 791 | is_valid_ = false; return false;) |
| 792 | } |
| 793 | |
| 794 | // CASE: << >> |
| 795 | string newOp = op_; |
| 796 | if (OP_IS_BIN_SHIFT) { |
| 797 | final_type_ = IntegralPromotion(left_val_->final_type_); |
| 798 | // instead of promoting rval, simply casting it to int64 should also be good. |
| 799 | int64_t numBits = right_val_->cast<int64_t>(); |
| 800 | if (numBits < 0) { |
Steven Moreland | 74d3f55 | 2020-02-04 15:57:50 -0800 | [diff] [blame] | 801 | // shifting with negative number of bits is undefined in C. In AIDL it |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 802 | // is defined as shifting into the other direction. |
| 803 | newOp = OPEQ("<<") ? ">>" : "<<"; |
| 804 | numBits = -numBits; |
| 805 | } |
| 806 | |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 807 | #define CASE_SHIFT(__type__) \ |
| 808 | return handleShift(*this, static_cast<__type__>(left_val_->final_value_), newOp, numBits, \ |
| 809 | &final_value_); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 810 | |
| 811 | SWITCH_KIND(final_type_, CASE_SHIFT, SHOULD_NOT_REACH(); final_type_ = Type::ERROR; |
| 812 | is_valid_ = false; return false;) |
| 813 | } |
| 814 | |
| 815 | // CASE: && || |
| 816 | if (OP_IS_BIN_LOGICAL) { |
| 817 | final_type_ = Type::BOOLEAN; |
| 818 | // easy; everything is bool. |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 819 | return handleLogical(*this, left_val_->final_value_, op_, right_val_->final_value_, |
| 820 | &final_value_); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | SHOULD_NOT_REACH(); |
| 824 | is_valid_ = false; |
| 825 | return false; |
| 826 | } |
| 827 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 828 | AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type, |
| 829 | int64_t parsed_value, const string& checked_value) |
| 830 | : AidlNode(location), |
| 831 | type_(parsed_type), |
| 832 | value_(checked_value), |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 833 | final_type_(parsed_type), |
| 834 | final_value_(parsed_value) { |
Steven Moreland | 541788d | 2020-05-21 22:05:52 +0000 | [diff] [blame] | 835 | CHECK(!value_.empty() || type_ == Type::ERROR) << location; |
| 836 | CHECK(type_ == Type::INT8 || type_ == Type::INT32 || type_ == Type::INT64) << location; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 837 | } |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 838 | |
| 839 | AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type, |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 840 | const string& checked_value) |
| 841 | : AidlNode(location), |
| 842 | type_(type), |
| 843 | value_(checked_value), |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 844 | final_type_(type) { |
Steven Moreland | 541788d | 2020-05-21 22:05:52 +0000 | [diff] [blame] | 845 | CHECK(!value_.empty() || type_ == Type::ERROR) << location; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 846 | switch (type_) { |
| 847 | case Type::INT8: |
| 848 | case Type::INT32: |
| 849 | case Type::INT64: |
| 850 | case Type::ARRAY: |
| 851 | AIDL_FATAL(this) << "Invalid type: " << ToString(type_); |
| 852 | break; |
| 853 | default: |
| 854 | break; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type, |
| 859 | std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values) |
| 860 | : AidlNode(location), |
| 861 | type_(type), |
| 862 | values_(std::move(*values)), |
| 863 | is_valid_(false), |
| 864 | is_evaluated_(false), |
| 865 | final_type_(type) { |
| 866 | CHECK(type_ == Type::ARRAY); |
| 867 | } |
| 868 | |
| 869 | AidlUnaryConstExpression::AidlUnaryConstExpression(const AidlLocation& location, const string& op, |
| 870 | std::unique_ptr<AidlConstantValue> rval) |
| 871 | : AidlConstantValue(location, Type::UNARY, op + rval->value_), |
| 872 | unary_(std::move(rval)), |
| 873 | op_(op) { |
| 874 | final_type_ = Type::UNARY; |
| 875 | } |
| 876 | |
| 877 | AidlBinaryConstExpression::AidlBinaryConstExpression(const AidlLocation& location, |
| 878 | std::unique_ptr<AidlConstantValue> lval, |
| 879 | const string& op, |
| 880 | std::unique_ptr<AidlConstantValue> rval) |
| 881 | : AidlConstantValue(location, Type::BINARY, lval->value_ + op + rval->value_), |
| 882 | left_val_(std::move(lval)), |
| 883 | right_val_(std::move(rval)), |
| 884 | op_(op) { |
| 885 | final_type_ = Type::BINARY; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 886 | } |