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> |
Steven Moreland | 0521bf3 | 2020-09-09 22:44:07 +0000 | [diff] [blame] | 24 | #include <limits> |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 25 | #include <memory> |
| 26 | |
| 27 | #include <android-base/parsedouble.h> |
| 28 | #include <android-base/parseint.h> |
| 29 | #include <android-base/strings.h> |
| 30 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 31 | using android::base::ConsumeSuffix; |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 32 | using android::base::EndsWith; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 33 | using android::base::Join; |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 34 | using android::base::StartsWith; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 35 | using std::string; |
| 36 | using std::unique_ptr; |
| 37 | using std::vector; |
| 38 | |
Steven Moreland | 0521bf3 | 2020-09-09 22:44:07 +0000 | [diff] [blame] | 39 | template <typename T> |
Devin Moore | cff9369 | 2020-09-24 10:39:57 -0700 | [diff] [blame] | 40 | constexpr int CLZ(T x) { |
Devin Moore | e2de9e4 | 2020-10-02 08:55:08 -0700 | [diff] [blame] | 41 | // __builtin_clz(0) is undefined |
| 42 | if (x == 0) return sizeof(T) * 8; |
Devin Moore | cff9369 | 2020-09-24 10:39:57 -0700 | [diff] [blame] | 43 | return (sizeof(T) == sizeof(uint64_t)) ? __builtin_clzl(x) : __builtin_clz(x); |
| 44 | } |
| 45 | |
| 46 | template <typename T> |
Steven Moreland | 0521bf3 | 2020-09-09 22:44:07 +0000 | [diff] [blame] | 47 | class OverflowGuard { |
| 48 | public: |
| 49 | OverflowGuard(T value) : mValue(value) {} |
| 50 | bool Overflowed() const { return mOverflowed; } |
| 51 | |
| 52 | T operator+() { return +mValue; } |
| 53 | T operator-() { |
| 54 | if (isMin()) { |
| 55 | mOverflowed = true; |
| 56 | return 0; |
| 57 | } |
| 58 | return -mValue; |
| 59 | } |
| 60 | T operator!() { return !mValue; } |
| 61 | T operator~() { return ~mValue; } |
| 62 | |
| 63 | T operator+(T o) { |
| 64 | T out; |
| 65 | mOverflowed = __builtin_add_overflow(mValue, o, &out); |
| 66 | return out; |
| 67 | } |
| 68 | T operator-(T o) { |
| 69 | T out; |
| 70 | mOverflowed = __builtin_sub_overflow(mValue, o, &out); |
| 71 | return out; |
| 72 | } |
| 73 | T operator*(T o) { |
| 74 | T out; |
| 75 | #ifdef _WIN32 |
| 76 | // ___mulodi4 not on windows https://bugs.llvm.org/show_bug.cgi?id=46669 |
| 77 | // we should still get an error here from ubsan, but the nice error |
| 78 | // is needed on linux for aidl_parser_fuzzer, where we are more |
| 79 | // concerned about overflows elsewhere in the compiler in addition to |
| 80 | // those in interfaces. |
| 81 | out = mValue * o; |
| 82 | #else |
| 83 | mOverflowed = __builtin_mul_overflow(mValue, o, &out); |
| 84 | #endif |
| 85 | return out; |
| 86 | } |
| 87 | T operator/(T o) { |
| 88 | if (o == 0 || (isMin() && o == -1)) { |
| 89 | mOverflowed = true; |
| 90 | return 0; |
| 91 | } |
| 92 | return mValue / o; |
| 93 | } |
| 94 | T operator%(T o) { |
| 95 | if (o == 0 || (isMin() && o == -1)) { |
| 96 | mOverflowed = true; |
| 97 | return 0; |
| 98 | } |
| 99 | return mValue % o; |
| 100 | } |
| 101 | T operator|(T o) { return mValue | o; } |
| 102 | T operator^(T o) { return mValue ^ o; } |
| 103 | T operator&(T o) { return mValue & o; } |
| 104 | T operator<(T o) { return mValue < o; } |
| 105 | T operator>(T o) { return mValue > o; } |
| 106 | T operator<=(T o) { return mValue <= o; } |
| 107 | T operator>=(T o) { return mValue >= o; } |
| 108 | T operator==(T o) { return mValue == o; } |
| 109 | T operator!=(T o) { return mValue != o; } |
| 110 | T operator>>(T o) { |
Devin Moore | a9e64de | 2020-09-29 11:29:42 -0700 | [diff] [blame] | 111 | if (o < 0 || o >= static_cast<T>(sizeof(T) * 8) || mValue < 0) { |
Steven Moreland | 0521bf3 | 2020-09-09 22:44:07 +0000 | [diff] [blame] | 112 | mOverflowed = true; |
| 113 | return 0; |
| 114 | } |
| 115 | return mValue >> o; |
| 116 | } |
| 117 | T operator<<(T o) { |
Devin Moore | e2de9e4 | 2020-10-02 08:55:08 -0700 | [diff] [blame] | 118 | if (o < 0 || mValue < 0 || o > CLZ(mValue) || o >= static_cast<T>(sizeof(T) * 8)) { |
Steven Moreland | 0521bf3 | 2020-09-09 22:44:07 +0000 | [diff] [blame] | 119 | mOverflowed = true; |
| 120 | return 0; |
| 121 | } |
| 122 | return mValue << o; |
| 123 | } |
| 124 | T operator||(T o) { return mValue || o; } |
| 125 | T operator&&(T o) { return mValue && o; } |
| 126 | |
| 127 | private: |
| 128 | bool isMin() { return mValue == std::numeric_limits<T>::min(); } |
| 129 | |
| 130 | T mValue; |
| 131 | bool mOverflowed = false; |
| 132 | }; |
| 133 | |
| 134 | template <typename T> |
| 135 | bool processGuard(const OverflowGuard<T>& guard, const AidlConstantValue& context) { |
| 136 | if (guard.Overflowed()) { |
| 137 | AIDL_ERROR(context) << "Constant expression computation overflows."; |
| 138 | return false; |
| 139 | } |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | // TODO: factor out all these macros |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 144 | #define SHOULD_NOT_REACH() AIDL_FATAL(AIDL_LOCATION_HERE) << "Should not reach." |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 145 | #define OPEQ(__y__) (string(op_) == string(__y__)) |
Steven Moreland | 0521bf3 | 2020-09-09 22:44:07 +0000 | [diff] [blame] | 146 | #define COMPUTE_UNARY(T, __op__) \ |
| 147 | if (op == string(#__op__)) { \ |
| 148 | OverflowGuard<T> guard(val); \ |
| 149 | *out = __op__ guard; \ |
| 150 | return processGuard(guard, context); \ |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 151 | } |
Steven Moreland | 0521bf3 | 2020-09-09 22:44:07 +0000 | [diff] [blame] | 152 | #define COMPUTE_BINARY(T, __op__) \ |
| 153 | if (op == string(#__op__)) { \ |
| 154 | OverflowGuard<T> guard(lval); \ |
| 155 | *out = guard __op__ rval; \ |
| 156 | return processGuard(guard, context); \ |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 157 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 158 | #define OP_IS_BIN_ARITHMETIC (OPEQ("+") || OPEQ("-") || OPEQ("*") || OPEQ("/") || OPEQ("%")) |
| 159 | #define OP_IS_BIN_BITFLIP (OPEQ("|") || OPEQ("^") || OPEQ("&")) |
| 160 | #define OP_IS_BIN_COMP \ |
| 161 | (OPEQ("<") || OPEQ(">") || OPEQ("<=") || OPEQ(">=") || OPEQ("==") || OPEQ("!=")) |
| 162 | #define OP_IS_BIN_SHIFT (OPEQ(">>") || OPEQ("<<")) |
| 163 | #define OP_IS_BIN_LOGICAL (OPEQ("||") || OPEQ("&&")) |
| 164 | |
| 165 | // NOLINT to suppress missing parentheses warnings about __def__. |
| 166 | #define SWITCH_KIND(__cond__, __action__, __def__) \ |
| 167 | switch (__cond__) { \ |
| 168 | case Type::BOOLEAN: \ |
| 169 | __action__(bool); \ |
| 170 | case Type::INT8: \ |
| 171 | __action__(int8_t); \ |
| 172 | case Type::INT32: \ |
| 173 | __action__(int32_t); \ |
| 174 | case Type::INT64: \ |
| 175 | __action__(int64_t); \ |
| 176 | default: \ |
| 177 | __def__; /* NOLINT */ \ |
| 178 | } |
| 179 | |
| 180 | template <class T> |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 181 | bool handleUnary(const AidlConstantValue& context, const string& op, T val, int64_t* out) { |
Steven Moreland | 0521bf3 | 2020-09-09 22:44:07 +0000 | [diff] [blame] | 182 | COMPUTE_UNARY(T, +) |
| 183 | COMPUTE_UNARY(T, -) |
| 184 | COMPUTE_UNARY(T, !) |
| 185 | COMPUTE_UNARY(T, ~) |
Steven Moreland | 720a3cc | 2020-07-16 23:44:59 +0000 | [diff] [blame] | 186 | AIDL_FATAL(context) << "Could not handleUnary for " << op << " " << val; |
| 187 | return false; |
| 188 | } |
| 189 | template <> |
| 190 | bool handleUnary<bool>(const AidlConstantValue& context, const string& op, bool val, int64_t* out) { |
Steven Moreland | 0521bf3 | 2020-09-09 22:44:07 +0000 | [diff] [blame] | 191 | COMPUTE_UNARY(bool, +) |
| 192 | COMPUTE_UNARY(bool, -) |
| 193 | COMPUTE_UNARY(bool, !) |
Yifan Hong | f17e3a7 | 2020-02-20 17:34:58 -0800 | [diff] [blame] | 194 | |
Steven Moreland | 720a3cc | 2020-07-16 23:44:59 +0000 | [diff] [blame] | 195 | if (op == "~") { |
| 196 | AIDL_ERROR(context) << "Bitwise negation of a boolean expression is always true."; |
| 197 | return false; |
| 198 | } |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 199 | AIDL_FATAL(context) << "Could not handleUnary for " << op << " " << val; |
| 200 | return false; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | template <class T> |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 204 | bool handleBinaryCommon(const AidlConstantValue& context, T lval, const string& op, T rval, |
| 205 | int64_t* out) { |
Steven Moreland | 0521bf3 | 2020-09-09 22:44:07 +0000 | [diff] [blame] | 206 | COMPUTE_BINARY(T, +) |
| 207 | COMPUTE_BINARY(T, -) |
| 208 | COMPUTE_BINARY(T, *) |
| 209 | COMPUTE_BINARY(T, /) |
| 210 | COMPUTE_BINARY(T, %) |
| 211 | COMPUTE_BINARY(T, |) |
| 212 | COMPUTE_BINARY(T, ^) |
| 213 | COMPUTE_BINARY(T, &) |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 214 | // comparison operators: return 0 or 1 by nature. |
Steven Moreland | 0521bf3 | 2020-09-09 22:44:07 +0000 | [diff] [blame] | 215 | COMPUTE_BINARY(T, ==) |
| 216 | COMPUTE_BINARY(T, !=) |
| 217 | COMPUTE_BINARY(T, <) |
| 218 | COMPUTE_BINARY(T, >) |
| 219 | COMPUTE_BINARY(T, <=) |
| 220 | COMPUTE_BINARY(T, >=) |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 221 | |
| 222 | AIDL_FATAL(context) << "Could not handleBinaryCommon for " << lval << " " << op << " " << rval; |
| 223 | return false; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | template <class T> |
Devin Moore | 0482302 | 2020-09-11 10:43:35 -0700 | [diff] [blame] | 227 | bool handleShift(const AidlConstantValue& context, T lval, const string& op, T rval, int64_t* out) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 228 | // just cast rval to int64_t and it should fit. |
Steven Moreland | 0521bf3 | 2020-09-09 22:44:07 +0000 | [diff] [blame] | 229 | COMPUTE_BINARY(T, >>) |
| 230 | COMPUTE_BINARY(T, <<) |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 231 | |
| 232 | AIDL_FATAL(context) << "Could not handleShift for " << lval << " " << op << " " << rval; |
| 233 | return false; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 236 | bool handleLogical(const AidlConstantValue& context, bool lval, const string& op, bool rval, |
| 237 | int64_t* out) { |
Steven Moreland | 0521bf3 | 2020-09-09 22:44:07 +0000 | [diff] [blame] | 238 | COMPUTE_BINARY(bool, ||); |
| 239 | COMPUTE_BINARY(bool, &&); |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 240 | |
| 241 | AIDL_FATAL(context) << "Could not handleLogical for " << lval << " " << op << " " << rval; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 242 | return false; |
| 243 | } |
| 244 | |
Steven Moreland | e9f8aad | 2022-01-31 19:27:21 +0000 | [diff] [blame] | 245 | static bool isValidLiteralChar(char c) { |
| 246 | return !(c <= 0x1f || // control characters are < 0x20 |
| 247 | c >= 0x7f || // DEL is 0x7f |
| 248 | c == '\\'); // Disallow backslashes for future proofing. |
| 249 | } |
| 250 | |
Jooyung Han | 535c5e8 | 2020-12-29 15:16:59 +0900 | [diff] [blame] | 251 | bool ParseFloating(std::string_view sv, double* parsed) { |
| 252 | // float literal should be parsed successfully. |
Jooyung Han | 71a1b58 | 2020-12-25 23:58:41 +0900 | [diff] [blame] | 253 | android::base::ConsumeSuffix(&sv, "f"); |
Jooyung Han | 535c5e8 | 2020-12-29 15:16:59 +0900 | [diff] [blame] | 254 | return android::base::ParseDouble(std::string(sv).data(), parsed); |
| 255 | } |
| 256 | |
| 257 | bool ParseFloating(std::string_view sv, float* parsed) { |
| 258 | // we only care about float literal (with suffix "f"). |
| 259 | if (!android::base::ConsumeSuffix(&sv, "f")) { |
| 260 | return false; |
Jooyung Han | 71a1b58 | 2020-12-25 23:58:41 +0900 | [diff] [blame] | 261 | } |
Jooyung Han | 535c5e8 | 2020-12-29 15:16:59 +0900 | [diff] [blame] | 262 | return android::base::ParseFloat(std::string(sv).data(), parsed); |
Jooyung Han | 71a1b58 | 2020-12-25 23:58:41 +0900 | [diff] [blame] | 263 | } |
| 264 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 265 | bool AidlUnaryConstExpression::IsCompatibleType(Type type, const string& op) { |
| 266 | // Verify the unary type here |
| 267 | switch (type) { |
| 268 | case Type::BOOLEAN: // fall-through |
| 269 | case Type::INT8: // fall-through |
| 270 | case Type::INT32: // fall-through |
| 271 | case Type::INT64: |
| 272 | return true; |
| 273 | case Type::FLOATING: |
| 274 | return (op == "+" || op == "-"); |
| 275 | default: |
| 276 | return false; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | bool AidlBinaryConstExpression::AreCompatibleTypes(Type t1, Type t2) { |
| 281 | switch (t1) { |
Jooyung Han | 0cc9963 | 2021-11-30 17:19:05 +0900 | [diff] [blame] | 282 | case Type::ARRAY: |
| 283 | if (t2 == Type::ARRAY) { |
| 284 | return true; |
| 285 | } |
| 286 | break; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 287 | case Type::STRING: |
| 288 | if (t2 == Type::STRING) { |
| 289 | return true; |
| 290 | } |
| 291 | break; |
| 292 | case Type::BOOLEAN: // fall-through |
| 293 | case Type::INT8: // fall-through |
| 294 | case Type::INT32: // fall-through |
| 295 | case Type::INT64: |
| 296 | switch (t2) { |
| 297 | case Type::BOOLEAN: // fall-through |
| 298 | case Type::INT8: // fall-through |
| 299 | case Type::INT32: // fall-through |
| 300 | case Type::INT64: |
| 301 | return true; |
| 302 | break; |
| 303 | default: |
| 304 | break; |
| 305 | } |
| 306 | break; |
| 307 | default: |
| 308 | break; |
| 309 | } |
| 310 | |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | // Returns the promoted kind for both operands |
| 315 | AidlConstantValue::Type AidlBinaryConstExpression::UsualArithmeticConversion(Type left, |
| 316 | Type right) { |
| 317 | // These are handled as special cases |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 318 | AIDL_FATAL_IF(left == Type::STRING || right == Type::STRING, AIDL_LOCATION_HERE); |
| 319 | AIDL_FATAL_IF(left == Type::FLOATING || right == Type::FLOATING, AIDL_LOCATION_HERE); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 320 | |
| 321 | // Kinds in concern: bool, (u)int[8|32|64] |
| 322 | if (left == right) return left; // easy case |
| 323 | if (left == Type::BOOLEAN) return right; |
| 324 | if (right == Type::BOOLEAN) return left; |
| 325 | |
| 326 | return left < right ? right : left; |
| 327 | } |
| 328 | |
| 329 | // Returns the promoted integral type where INT32 is the smallest type |
| 330 | AidlConstantValue::Type AidlBinaryConstExpression::IntegralPromotion(Type in) { |
| 331 | return (Type::INT32 < in) ? in : Type::INT32; |
| 332 | } |
| 333 | |
Steven Moreland | 541788d | 2020-05-21 22:05:52 +0000 | [diff] [blame] | 334 | AidlConstantValue* AidlConstantValue::Default(const AidlTypeSpecifier& specifier) { |
| 335 | AidlLocation location = specifier.GetLocation(); |
| 336 | |
| 337 | // allocation of int[0] is a bit wasteful in Java |
| 338 | if (specifier.IsArray()) { |
| 339 | return nullptr; |
| 340 | } |
| 341 | |
| 342 | const std::string name = specifier.GetName(); |
| 343 | if (name == "boolean") { |
| 344 | return Boolean(location, false); |
| 345 | } |
| 346 | if (name == "byte" || name == "int" || name == "long") { |
| 347 | return Integral(location, "0"); |
| 348 | } |
| 349 | if (name == "float") { |
| 350 | return Floating(location, "0.0f"); |
| 351 | } |
| 352 | if (name == "double") { |
| 353 | return Floating(location, "0.0"); |
| 354 | } |
| 355 | return nullptr; |
| 356 | } |
| 357 | |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 358 | AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool value) { |
| 359 | return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false"); |
| 360 | } |
| 361 | |
| 362 | AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) { |
Steven Moreland | cdedd9b | 2019-12-02 10:54:47 -0800 | [diff] [blame] | 363 | const std::string explicit_value = string("'") + value + "'"; |
Steven Moreland | 8c9b7ec | 2022-01-26 22:50:12 +0000 | [diff] [blame] | 364 | if (!isValidLiteralChar(value)) { |
| 365 | AIDL_ERROR(location) << "Invalid character literal " << value; |
| 366 | return new AidlConstantValue(location, Type::ERROR, explicit_value); |
| 367 | } |
Steven Moreland | cdedd9b | 2019-12-02 10:54:47 -0800 | [diff] [blame] | 368 | return new AidlConstantValue(location, Type::CHARACTER, explicit_value); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location, |
| 372 | const std::string& value) { |
| 373 | return new AidlConstantValue(location, Type::FLOATING, value); |
| 374 | } |
| 375 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 376 | bool AidlConstantValue::IsHex(const string& value) { |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 377 | return StartsWith(value, "0x") || StartsWith(value, "0X"); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 380 | bool AidlConstantValue::ParseIntegral(const string& value, int64_t* parsed_value, |
| 381 | Type* parsed_type) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 382 | if (parsed_value == nullptr || parsed_type == nullptr) { |
| 383 | return false; |
| 384 | } |
| 385 | |
Steven Moreland | b7d5865 | 2021-10-25 15:10:02 -0700 | [diff] [blame] | 386 | std::string_view value_view = value; |
| 387 | const bool is_byte = ConsumeSuffix(&value_view, "u8"); |
| 388 | const bool is_long = ConsumeSuffix(&value_view, "l") || ConsumeSuffix(&value_view, "L"); |
| 389 | const std::string value_substr = std::string(value_view); |
| 390 | |
| 391 | *parsed_value = 0; |
| 392 | *parsed_type = Type::ERROR; |
| 393 | |
| 394 | if (is_byte && is_long) return false; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 395 | |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 396 | if (IsHex(value)) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 397 | // AIDL considers 'const int foo = 0xffffffff' as -1, but if we want to |
| 398 | // handle that when computing constant expressions, then we need to |
| 399 | // represent 0xffffffff as a uint32_t. However, AIDL only has signed types; |
| 400 | // so we parse as an unsigned int when possible and then cast to a signed |
| 401 | // int. One example of this is in ICameraService.aidl where a constant int |
| 402 | // is used for bit manipulations which ideally should be handled with an |
| 403 | // unsigned int. |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 404 | // |
| 405 | // Note, for historical consistency, we need to consider small hex values |
| 406 | // as an integral type. Recognizing them as INT8 could break some files, |
| 407 | // even though it would simplify this code. |
Steven Moreland | b7d5865 | 2021-10-25 15:10:02 -0700 | [diff] [blame] | 408 | if (is_byte) { |
| 409 | uint8_t raw_value8; |
| 410 | if (!android::base::ParseUint<uint8_t>(value_substr, &raw_value8)) { |
| 411 | return false; |
| 412 | } |
| 413 | *parsed_value = static_cast<int8_t>(raw_value8); |
| 414 | *parsed_type = Type::INT8; |
| 415 | } else if (uint32_t raw_value32; |
| 416 | !is_long && android::base::ParseUint<uint32_t>(value_substr, &raw_value32)) { |
| 417 | *parsed_value = static_cast<int32_t>(raw_value32); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 418 | *parsed_type = Type::INT32; |
Steven Moreland | b7d5865 | 2021-10-25 15:10:02 -0700 | [diff] [blame] | 419 | } else if (uint64_t raw_value64; |
| 420 | android::base::ParseUint<uint64_t>(value_substr, &raw_value64)) { |
| 421 | *parsed_value = static_cast<int64_t>(raw_value64); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 422 | *parsed_type = Type::INT64; |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 423 | } else { |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 424 | return false; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 425 | } |
| 426 | return true; |
| 427 | } |
| 428 | |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 429 | if (!android::base::ParseInt<int64_t>(value_substr, parsed_value)) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 430 | return false; |
| 431 | } |
| 432 | |
Steven Moreland | b7d5865 | 2021-10-25 15:10:02 -0700 | [diff] [blame] | 433 | if (is_byte) { |
| 434 | if (*parsed_value > UINT8_MAX || *parsed_value < 0) { |
| 435 | return false; |
| 436 | } |
| 437 | *parsed_value = static_cast<int8_t>(*parsed_value); |
| 438 | *parsed_type = Type::INT8; |
| 439 | } else if (is_long) { |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 440 | *parsed_type = Type::INT64; |
| 441 | } else { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 442 | // guess literal type. |
| 443 | if (*parsed_value <= INT8_MAX && *parsed_value >= INT8_MIN) { |
| 444 | *parsed_type = Type::INT8; |
| 445 | } else if (*parsed_value <= INT32_MAX && *parsed_value >= INT32_MIN) { |
| 446 | *parsed_type = Type::INT32; |
| 447 | } else { |
| 448 | *parsed_type = Type::INT64; |
| 449 | } |
| 450 | } |
| 451 | return true; |
| 452 | } |
| 453 | |
| 454 | AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location, const string& value) { |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 455 | AIDL_FATAL_IF(value.empty(), location); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 456 | |
| 457 | Type parsed_type; |
| 458 | int64_t parsed_value = 0; |
| 459 | bool success = ParseIntegral(value, &parsed_value, &parsed_type); |
| 460 | if (!success) { |
| 461 | return nullptr; |
| 462 | } |
| 463 | |
| 464 | return new AidlConstantValue(location, parsed_type, parsed_value, value); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | AidlConstantValue* AidlConstantValue::Array( |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 468 | const AidlLocation& location, std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values) { |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 469 | AIDL_FATAL_IF(values == nullptr, location); |
Jooyung Han | aeb0167 | 2021-11-30 17:29:22 +0900 | [diff] [blame] | 470 | // Reconstruct literal value |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 471 | std::vector<std::string> str_values; |
| 472 | for (const auto& v : *values) { |
| 473 | str_values.push_back(v->value_); |
| 474 | } |
Jooyung Han | aeb0167 | 2021-11-30 17:29:22 +0900 | [diff] [blame] | 475 | return new AidlConstantValue(location, Type::ARRAY, std::move(values), |
| 476 | "{" + Join(str_values, ", ") + "}"); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 479 | AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) { |
Steven Moreland | 797b06b | 2022-01-26 02:27:23 +0000 | [diff] [blame^] | 480 | AIDL_FATAL_IF(value.size() == 0, "If this is unquoted, we need to update the index log"); |
| 481 | AIDL_FATAL_IF(value[0] != '\"', "If this is unquoted, we need to update the index log"); |
| 482 | |
Steven Moreland | e9f8aad | 2022-01-31 19:27:21 +0000 | [diff] [blame] | 483 | for (size_t i = 0; i < value.length(); ++i) { |
| 484 | if (!isValidLiteralChar(value[i])) { |
Steven Moreland | 797b06b | 2022-01-26 02:27:23 +0000 | [diff] [blame^] | 485 | AIDL_ERROR(location) << "Found invalid character '" << value[i] << "' at index " << i - 1 |
| 486 | << " in string constant '" << value << "'"; |
Steven Moreland | e9f8aad | 2022-01-31 19:27:21 +0000 | [diff] [blame] | 487 | return new AidlConstantValue(location, Type::ERROR, value); |
| 488 | } |
| 489 | } |
| 490 | |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 491 | return new AidlConstantValue(location, Type::STRING, value); |
| 492 | } |
| 493 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 494 | string AidlConstantValue::ValueString(const AidlTypeSpecifier& type, |
| 495 | const ConstantValueDecorator& decorator) const { |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 496 | if (type.IsGeneric()) { |
| 497 | AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal."; |
| 498 | return ""; |
| 499 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 500 | if (!is_evaluated_) { |
| 501 | // TODO(b/142722772) CheckValid() should be called before ValueString() |
| 502 | bool success = CheckValid(); |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 503 | success &= evaluate(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 504 | if (!success) { |
| 505 | // the detailed error message shall be printed in evaluate |
| 506 | return ""; |
| 507 | } |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 508 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 509 | if (!is_valid_) { |
| 510 | AIDL_ERROR(this) << "Invalid constant value: " + value_; |
| 511 | return ""; |
| 512 | } |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 513 | |
| 514 | const AidlDefinedType* defined_type = type.GetDefinedType(); |
Jooyung Han | 981fc59 | 2021-11-06 20:24:45 +0900 | [diff] [blame] | 515 | if (defined_type && final_type_ != Type::ARRAY) { |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 516 | const AidlEnumDeclaration* enum_type = defined_type->AsEnumDeclaration(); |
| 517 | if (!enum_type) { |
| 518 | AIDL_ERROR(this) << "Invalid type (" << defined_type->GetCanonicalName() |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 519 | << ") for a const value (" << value_ << ")"; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 520 | return ""; |
| 521 | } |
| 522 | if (type_ != Type::REF) { |
| 523 | AIDL_ERROR(this) << "Invalid value (" << value_ << ") for enum " |
| 524 | << enum_type->GetCanonicalName(); |
| 525 | return ""; |
| 526 | } |
| 527 | return decorator(type, value_); |
| 528 | } |
| 529 | |
Jooyung Han | aeb0167 | 2021-11-30 17:29:22 +0900 | [diff] [blame] | 530 | const string& type_string = type.Signature(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 531 | int err = 0; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 532 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 533 | switch (final_type_) { |
| 534 | case Type::CHARACTER: |
| 535 | if (type_string == "char") { |
| 536 | return decorator(type, final_string_value_); |
| 537 | } |
| 538 | err = -1; |
| 539 | break; |
| 540 | case Type::STRING: |
| 541 | if (type_string == "String") { |
| 542 | return decorator(type, final_string_value_); |
| 543 | } |
| 544 | err = -1; |
| 545 | break; |
| 546 | case Type::BOOLEAN: // fall-through |
| 547 | case Type::INT8: // fall-through |
| 548 | case Type::INT32: // fall-through |
| 549 | case Type::INT64: |
| 550 | if (type_string == "byte") { |
| 551 | if (final_value_ > INT8_MAX || final_value_ < INT8_MIN) { |
| 552 | err = -1; |
| 553 | break; |
| 554 | } |
| 555 | return decorator(type, std::to_string(static_cast<int8_t>(final_value_))); |
| 556 | } else if (type_string == "int") { |
| 557 | if (final_value_ > INT32_MAX || final_value_ < INT32_MIN) { |
| 558 | err = -1; |
| 559 | break; |
| 560 | } |
| 561 | return decorator(type, std::to_string(static_cast<int32_t>(final_value_))); |
| 562 | } else if (type_string == "long") { |
| 563 | return decorator(type, std::to_string(final_value_)); |
| 564 | } else if (type_string == "boolean") { |
| 565 | return decorator(type, final_value_ ? "true" : "false"); |
| 566 | } |
| 567 | err = -1; |
| 568 | break; |
| 569 | case Type::ARRAY: { |
| 570 | if (!type.IsArray()) { |
| 571 | err = -1; |
| 572 | break; |
| 573 | } |
| 574 | vector<string> value_strings; |
| 575 | value_strings.reserve(values_.size()); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 576 | bool success = true; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 577 | |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 578 | for (const auto& value : values_) { |
Jooyung Han | aeb0167 | 2021-11-30 17:29:22 +0900 | [diff] [blame] | 579 | string value_string; |
| 580 | type.ViewAsArrayBase([&](const auto& base_type) { |
| 581 | value_string = value->ValueString(base_type, decorator); |
| 582 | }); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 583 | if (value_string.empty()) { |
| 584 | success = false; |
| 585 | break; |
| 586 | } |
| 587 | value_strings.push_back(value_string); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 588 | } |
| 589 | if (!success) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 590 | err = -1; |
| 591 | break; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 592 | } |
Jooyung Han | 0cc9963 | 2021-11-30 17:19:05 +0900 | [diff] [blame] | 593 | if (type.IsFixedSizeArray()) { |
| 594 | auto size = |
| 595 | std::get<FixedSizeArray>(type.GetArray()).dimensions.front()->EvaluatedValue<int32_t>(); |
Jooyung Han | e76bcc2 | 2022-01-23 22:49:45 +0900 | [diff] [blame] | 596 | if (values_.size() != static_cast<size_t>(size)) { |
Jooyung Han | 0cc9963 | 2021-11-30 17:19:05 +0900 | [diff] [blame] | 597 | AIDL_ERROR(this) << "Expected an array of " << size << " elements, but found one with " |
| 598 | << values_.size() << " elements"; |
| 599 | err = -1; |
| 600 | break; |
| 601 | } |
| 602 | } |
Jooyung Han | aeb0167 | 2021-11-30 17:29:22 +0900 | [diff] [blame] | 603 | return decorator(type, value_strings); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 604 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 605 | case Type::FLOATING: { |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 606 | if (type_string == "double") { |
| 607 | double parsed_value; |
Jooyung Han | 535c5e8 | 2020-12-29 15:16:59 +0900 | [diff] [blame] | 608 | if (!ParseFloating(value_, &parsed_value)) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 609 | AIDL_ERROR(this) << "Could not parse " << value_; |
| 610 | err = -1; |
| 611 | break; |
| 612 | } |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 613 | return decorator(type, std::to_string(parsed_value)); |
| 614 | } |
Jooyung Han | 535c5e8 | 2020-12-29 15:16:59 +0900 | [diff] [blame] | 615 | if (type_string == "float") { |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 616 | float parsed_value; |
Jooyung Han | 535c5e8 | 2020-12-29 15:16:59 +0900 | [diff] [blame] | 617 | if (!ParseFloating(value_, &parsed_value)) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 618 | AIDL_ERROR(this) << "Could not parse " << value_; |
| 619 | err = -1; |
| 620 | break; |
| 621 | } |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 622 | return decorator(type, std::to_string(parsed_value) + "f"); |
| 623 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 624 | err = -1; |
| 625 | break; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 626 | } |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 627 | default: |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 628 | err = -1; |
| 629 | break; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 632 | AIDL_FATAL_IF(err == 0, this); |
Steven Moreland | b7d5865 | 2021-10-25 15:10:02 -0700 | [diff] [blame] | 633 | AIDL_ERROR(this) << "Invalid type specifier for " << ToString(final_type_) << ": " << type_string |
| 634 | << " (" << value_ << ")"; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 635 | return ""; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | bool AidlConstantValue::CheckValid() const { |
| 639 | // Nothing needs to be checked here. The constant value will be validated in |
| 640 | // the constructor or in the evaluate() function. |
| 641 | if (is_evaluated_) return is_valid_; |
| 642 | |
| 643 | switch (type_) { |
| 644 | case Type::BOOLEAN: // fall-through |
| 645 | case Type::INT8: // fall-through |
| 646 | case Type::INT32: // fall-through |
| 647 | case Type::INT64: // fall-through |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 648 | case Type::CHARACTER: // fall-through |
| 649 | case Type::STRING: // fall-through |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 650 | case Type::REF: // fall-through |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 651 | case Type::FLOATING: // fall-through |
| 652 | case Type::UNARY: // fall-through |
| 653 | case Type::BINARY: |
| 654 | is_valid_ = true; |
| 655 | break; |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 656 | case Type::ARRAY: |
| 657 | is_valid_ = true; |
| 658 | for (const auto& v : values_) is_valid_ &= v->CheckValid(); |
| 659 | break; |
Steven Moreland | 4ff04aa | 2019-12-02 10:44:29 -0800 | [diff] [blame] | 660 | case Type::ERROR: |
| 661 | return false; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 662 | default: |
| 663 | AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_); |
| 664 | return false; |
| 665 | } |
| 666 | |
| 667 | return true; |
| 668 | } |
| 669 | |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 670 | bool AidlConstantValue::evaluate() const { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 671 | if (is_evaluated_) { |
| 672 | return is_valid_; |
| 673 | } |
| 674 | int err = 0; |
| 675 | is_evaluated_ = true; |
| 676 | |
| 677 | switch (type_) { |
| 678 | case Type::ARRAY: { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 679 | Type array_type = Type::ERROR; |
| 680 | bool success = true; |
| 681 | for (const auto& value : values_) { |
| 682 | success = value->CheckValid(); |
| 683 | if (success) { |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 684 | success = value->evaluate(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 685 | if (!success) { |
| 686 | AIDL_ERROR(this) << "Invalid array element: " << value->value_; |
| 687 | break; |
| 688 | } |
| 689 | if (array_type == Type::ERROR) { |
| 690 | array_type = value->final_type_; |
| 691 | } else if (!AidlBinaryConstExpression::AreCompatibleTypes(array_type, |
| 692 | value->final_type_)) { |
| 693 | AIDL_ERROR(this) << "Incompatible array element type: " << ToString(value->final_type_) |
| 694 | << ". Expecting type compatible with " << ToString(array_type); |
| 695 | success = false; |
| 696 | break; |
| 697 | } |
| 698 | } else { |
| 699 | break; |
| 700 | } |
| 701 | } |
| 702 | if (!success) { |
| 703 | err = -1; |
| 704 | break; |
| 705 | } |
| 706 | final_type_ = type_; |
| 707 | break; |
| 708 | } |
| 709 | case Type::BOOLEAN: |
| 710 | if ((value_ != "true") && (value_ != "false")) { |
| 711 | AIDL_ERROR(this) << "Invalid constant boolean value: " << value_; |
| 712 | err = -1; |
| 713 | break; |
| 714 | } |
| 715 | final_value_ = (value_ == "true") ? 1 : 0; |
| 716 | final_type_ = type_; |
| 717 | break; |
| 718 | case Type::INT8: // fall-through |
| 719 | case Type::INT32: // fall-through |
| 720 | case Type::INT64: |
| 721 | // Parsing happens in the constructor |
| 722 | final_type_ = type_; |
| 723 | break; |
| 724 | case Type::CHARACTER: // fall-through |
| 725 | case Type::STRING: |
| 726 | final_string_value_ = value_; |
| 727 | final_type_ = type_; |
| 728 | break; |
| 729 | case Type::FLOATING: |
| 730 | // Just parse on the fly in ValueString |
| 731 | final_type_ = type_; |
| 732 | break; |
| 733 | default: |
| 734 | AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_); |
| 735 | err = -1; |
| 736 | } |
| 737 | |
| 738 | return (err == 0) ? true : false; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | string AidlConstantValue::ToString(Type type) { |
| 742 | switch (type) { |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 743 | case Type::BOOLEAN: |
| 744 | return "a literal boolean"; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 745 | case Type::INT8: |
| 746 | return "an int8 literal"; |
| 747 | case Type::INT32: |
| 748 | return "an int32 literal"; |
| 749 | case Type::INT64: |
| 750 | return "an int64 literal"; |
Steven Moreland | a923a72 | 2019-11-26 20:08:30 -0800 | [diff] [blame] | 751 | case Type::ARRAY: |
| 752 | return "a literal array"; |
| 753 | case Type::CHARACTER: |
| 754 | return "a literal char"; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 755 | case Type::STRING: |
| 756 | return "a literal string"; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 757 | case Type::REF: |
| 758 | return "a reference"; |
Steven Moreland | a923a72 | 2019-11-26 20:08:30 -0800 | [diff] [blame] | 759 | case Type::FLOATING: |
| 760 | return "a literal float"; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 761 | case Type::UNARY: |
| 762 | return "a unary expression"; |
| 763 | case Type::BINARY: |
| 764 | return "a binary expression"; |
Steven Moreland | a923a72 | 2019-11-26 20:08:30 -0800 | [diff] [blame] | 765 | case Type::ERROR: |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 766 | AIDL_FATAL(AIDL_LOCATION_HERE) << "aidl internal error: error type failed to halt program"; |
Steven Moreland | a923a72 | 2019-11-26 20:08:30 -0800 | [diff] [blame] | 767 | return ""; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 768 | default: |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 769 | AIDL_FATAL(AIDL_LOCATION_HERE) |
| 770 | << "aidl internal error: unknown constant type: " << static_cast<int>(type); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 771 | return ""; // not reached |
| 772 | } |
| 773 | } |
| 774 | |
Jooyung Han | d0c8af0 | 2021-01-06 18:08:01 +0900 | [diff] [blame] | 775 | AidlConstantReference::AidlConstantReference(const AidlLocation& location, const std::string& value) |
| 776 | : AidlConstantValue(location, Type::REF, value) { |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 777 | const auto pos = value.find_last_of('.'); |
| 778 | if (pos == string::npos) { |
| 779 | field_name_ = value; |
| 780 | } else { |
Jooyung Han | 9fafb8d | 2021-11-30 13:19:33 +0900 | [diff] [blame] | 781 | ref_type_ = std::make_unique<AidlTypeSpecifier>(location, value.substr(0, pos), |
| 782 | /*array=*/std::nullopt, /*type_params=*/nullptr, |
Jooyung Han | 8451a20 | 2021-01-16 03:07:06 +0900 | [diff] [blame] | 783 | Comments{}); |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 784 | field_name_ = value.substr(pos + 1); |
| 785 | } |
| 786 | } |
| 787 | |
Jooyung Han | 9d3cbe2 | 2020-12-28 03:02:08 +0900 | [diff] [blame] | 788 | const AidlConstantValue* AidlConstantReference::Resolve(const AidlDefinedType* scope) const { |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 789 | if (resolved_) return resolved_; |
Jooyung Han | 9d3cbe2 | 2020-12-28 03:02:08 +0900 | [diff] [blame] | 790 | |
| 791 | const AidlDefinedType* defined_type; |
| 792 | if (ref_type_) { |
| 793 | defined_type = ref_type_->GetDefinedType(); |
| 794 | } else { |
| 795 | defined_type = scope; |
| 796 | } |
| 797 | |
| 798 | if (!defined_type) { |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 799 | // This can happen when "const reference" is used in an unsupported way, |
| 800 | // but missed in checks there. It works as a safety net. |
| 801 | AIDL_ERROR(*this) << "Can't resolve the reference (" << value_ << ")"; |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 802 | return nullptr; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 803 | } |
| 804 | |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 805 | if (auto enum_decl = defined_type->AsEnumDeclaration(); enum_decl) { |
| 806 | for (const auto& e : enum_decl->GetEnumerators()) { |
| 807 | if (e->GetName() == field_name_) { |
Jooyung Han | 9d3cbe2 | 2020-12-28 03:02:08 +0900 | [diff] [blame] | 808 | return resolved_ = e->GetValue(); |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | } else { |
| 812 | for (const auto& c : defined_type->GetConstantDeclarations()) { |
| 813 | if (c->GetName() == field_name_) { |
Jooyung Han | 9d3cbe2 | 2020-12-28 03:02:08 +0900 | [diff] [blame] | 814 | return resolved_ = &c->GetValue(); |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | } |
Jooyung Han | e9f5b27 | 2021-01-07 00:18:11 +0900 | [diff] [blame] | 818 | AIDL_ERROR(*this) << "Can't find " << field_name_ << " in " << defined_type->GetName(); |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 819 | return nullptr; |
| 820 | } |
| 821 | |
| 822 | bool AidlConstantReference::CheckValid() const { |
| 823 | if (is_evaluated_) return is_valid_; |
| 824 | AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_; |
| 825 | is_valid_ = resolved_->CheckValid(); |
| 826 | return is_valid_; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 827 | } |
| 828 | |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 829 | bool AidlConstantReference::evaluate() const { |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 830 | if (is_evaluated_) return is_valid_; |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 831 | AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_; |
| 832 | is_evaluated_ = true; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 833 | |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 834 | resolved_->evaluate(); |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 835 | is_valid_ = resolved_->is_valid_; |
| 836 | final_type_ = resolved_->final_type_; |
| 837 | if (is_valid_) { |
| 838 | if (final_type_ == Type::STRING) { |
| 839 | final_string_value_ = resolved_->final_string_value_; |
| 840 | } else { |
| 841 | final_value_ = resolved_->final_value_; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 842 | } |
| 843 | } |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 844 | return is_valid_; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 845 | } |
| 846 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 847 | bool AidlUnaryConstExpression::CheckValid() const { |
| 848 | if (is_evaluated_) return is_valid_; |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 849 | AIDL_FATAL_IF(unary_ == nullptr, this); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 850 | |
| 851 | is_valid_ = unary_->CheckValid(); |
| 852 | if (!is_valid_) { |
| 853 | final_type_ = Type::ERROR; |
| 854 | return false; |
| 855 | } |
| 856 | |
Steven Moreland | 4bcb05c | 2019-11-27 18:57:47 -0800 | [diff] [blame] | 857 | return AidlConstantValue::CheckValid(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 860 | bool AidlUnaryConstExpression::evaluate() const { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 861 | if (is_evaluated_) { |
| 862 | return is_valid_; |
| 863 | } |
| 864 | is_evaluated_ = true; |
| 865 | |
| 866 | // Recursively evaluate the expression tree |
| 867 | if (!unary_->is_evaluated_) { |
| 868 | // TODO(b/142722772) CheckValid() should be called before ValueString() |
| 869 | bool success = CheckValid(); |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 870 | success &= unary_->evaluate(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 871 | if (!success) { |
| 872 | is_valid_ = false; |
| 873 | return false; |
| 874 | } |
| 875 | } |
Devin Moore | c233fb8 | 2020-04-07 11:13:44 -0700 | [diff] [blame] | 876 | if (!IsCompatibleType(unary_->final_type_, op_)) { |
| 877 | AIDL_ERROR(unary_) << "'" << op_ << "'" |
| 878 | << " is not compatible with " << ToString(unary_->final_type_) |
| 879 | << ": " + value_; |
| 880 | is_valid_ = false; |
| 881 | return false; |
| 882 | } |
| 883 | if (!unary_->is_valid_) { |
| 884 | AIDL_ERROR(unary_) << "Invalid constant unary expression: " + value_; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 885 | is_valid_ = false; |
| 886 | return false; |
| 887 | } |
| 888 | final_type_ = unary_->final_type_; |
| 889 | |
| 890 | if (final_type_ == Type::FLOATING) { |
| 891 | // don't do anything here. ValueString() will handle everything. |
| 892 | is_valid_ = true; |
| 893 | return true; |
| 894 | } |
| 895 | |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 896 | #define CASE_UNARY(__type__) \ |
Devin Moore | 1f0360d | 2020-12-21 12:12:48 -0800 | [diff] [blame] | 897 | return is_valid_ = \ |
| 898 | handleUnary(*this, op_, static_cast<__type__>(unary_->final_value_), &final_value_); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 899 | |
| 900 | SWITCH_KIND(final_type_, CASE_UNARY, SHOULD_NOT_REACH(); final_type_ = Type::ERROR; |
| 901 | is_valid_ = false; return false;) |
| 902 | } |
| 903 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 904 | bool AidlBinaryConstExpression::CheckValid() const { |
| 905 | bool success = false; |
| 906 | if (is_evaluated_) return is_valid_; |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 907 | AIDL_FATAL_IF(left_val_ == nullptr, this); |
| 908 | AIDL_FATAL_IF(right_val_ == nullptr, this); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 909 | |
| 910 | success = left_val_->CheckValid(); |
| 911 | if (!success) { |
| 912 | final_type_ = Type::ERROR; |
| 913 | AIDL_ERROR(this) << "Invalid left operand in binary expression: " + value_; |
| 914 | } |
| 915 | |
| 916 | success = right_val_->CheckValid(); |
| 917 | if (!success) { |
| 918 | AIDL_ERROR(this) << "Invalid right operand in binary expression: " + value_; |
| 919 | final_type_ = Type::ERROR; |
| 920 | } |
| 921 | |
| 922 | if (final_type_ == Type::ERROR) { |
| 923 | is_valid_ = false; |
| 924 | return false; |
| 925 | } |
| 926 | |
| 927 | is_valid_ = true; |
Steven Moreland | 4bcb05c | 2019-11-27 18:57:47 -0800 | [diff] [blame] | 928 | return AidlConstantValue::CheckValid(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 929 | } |
| 930 | |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 931 | bool AidlBinaryConstExpression::evaluate() const { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 932 | if (is_evaluated_) { |
| 933 | return is_valid_; |
| 934 | } |
| 935 | is_evaluated_ = true; |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 936 | AIDL_FATAL_IF(left_val_ == nullptr, this); |
| 937 | AIDL_FATAL_IF(right_val_ == nullptr, this); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 938 | |
| 939 | // Recursively evaluate the binary expression tree |
| 940 | if (!left_val_->is_evaluated_ || !right_val_->is_evaluated_) { |
| 941 | // TODO(b/142722772) CheckValid() should be called before ValueString() |
| 942 | bool success = CheckValid(); |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 943 | success &= left_val_->evaluate(); |
| 944 | success &= right_val_->evaluate(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 945 | if (!success) { |
| 946 | is_valid_ = false; |
| 947 | return false; |
| 948 | } |
| 949 | } |
| 950 | if (!left_val_->is_valid_ || !right_val_->is_valid_) { |
| 951 | is_valid_ = false; |
| 952 | return false; |
| 953 | } |
| 954 | is_valid_ = AreCompatibleTypes(left_val_->final_type_, right_val_->final_type_); |
| 955 | if (!is_valid_) { |
Steven Moreland | 1f9f221 | 2020-09-24 18:20:15 +0000 | [diff] [blame] | 956 | AIDL_ERROR(this) << "Cannot perform operation '" << op_ << "' on " |
| 957 | << ToString(right_val_->GetType()) << " and " << ToString(left_val_->GetType()) |
| 958 | << "."; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 959 | return false; |
| 960 | } |
| 961 | |
| 962 | bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP; |
| 963 | |
| 964 | // Handle String case first |
| 965 | if (left_val_->final_type_ == Type::STRING) { |
Steven Moreland | 22e3611 | 2020-10-01 00:50:45 +0000 | [diff] [blame] | 966 | AIDL_FATAL_IF(right_val_->final_type_ != Type::STRING, this); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 967 | if (!OPEQ("+")) { |
Steven Moreland | 22e3611 | 2020-10-01 00:50:45 +0000 | [diff] [blame] | 968 | AIDL_ERROR(this) << "Only '+' is supported for strings, not '" << op_ << "'."; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 969 | final_type_ = Type::ERROR; |
| 970 | is_valid_ = false; |
| 971 | return false; |
| 972 | } |
| 973 | |
| 974 | // Remove trailing " from lhs |
| 975 | const string& lhs = left_val_->final_string_value_; |
| 976 | if (lhs.back() != '"') { |
| 977 | AIDL_ERROR(this) << "'" << lhs << "' is missing a trailing quote."; |
| 978 | final_type_ = Type::ERROR; |
| 979 | is_valid_ = false; |
| 980 | return false; |
| 981 | } |
| 982 | const string& rhs = right_val_->final_string_value_; |
| 983 | // Remove starting " from rhs |
| 984 | if (rhs.front() != '"') { |
| 985 | AIDL_ERROR(this) << "'" << rhs << "' is missing a leading quote."; |
| 986 | final_type_ = Type::ERROR; |
| 987 | is_valid_ = false; |
| 988 | return false; |
| 989 | } |
| 990 | |
| 991 | final_string_value_ = string(lhs.begin(), lhs.end() - 1).append(rhs.begin() + 1, rhs.end()); |
| 992 | final_type_ = Type::STRING; |
| 993 | return true; |
| 994 | } |
| 995 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 996 | // CASE: + - * / % | ^ & < > <= >= == != |
| 997 | if (isArithmeticOrBitflip || OP_IS_BIN_COMP) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 998 | // promoted kind for both operands. |
| 999 | Type promoted = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_), |
| 1000 | IntegralPromotion(right_val_->final_type_)); |
| 1001 | // result kind. |
| 1002 | final_type_ = isArithmeticOrBitflip |
| 1003 | ? promoted // arithmetic or bitflip operators generates promoted type |
| 1004 | : Type::BOOLEAN; // comparison operators generates bool |
| 1005 | |
Devin Moore | 1f0360d | 2020-12-21 12:12:48 -0800 | [diff] [blame] | 1006 | #define CASE_BINARY_COMMON(__type__) \ |
| 1007 | return is_valid_ = \ |
| 1008 | handleBinaryCommon(*this, static_cast<__type__>(left_val_->final_value_), op_, \ |
| 1009 | static_cast<__type__>(right_val_->final_value_), &final_value_); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1010 | |
| 1011 | SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); final_type_ = Type::ERROR; |
| 1012 | is_valid_ = false; return false;) |
| 1013 | } |
| 1014 | |
| 1015 | // CASE: << >> |
| 1016 | string newOp = op_; |
| 1017 | if (OP_IS_BIN_SHIFT) { |
Devin Moore | 0482302 | 2020-09-11 10:43:35 -0700 | [diff] [blame] | 1018 | // promoted kind for both operands. |
| 1019 | final_type_ = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_), |
| 1020 | IntegralPromotion(right_val_->final_type_)); |
| 1021 | auto numBits = right_val_->final_value_; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1022 | if (numBits < 0) { |
Steven Moreland | 74d3f55 | 2020-02-04 15:57:50 -0800 | [diff] [blame] | 1023 | // 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] | 1024 | // is defined as shifting into the other direction. |
| 1025 | newOp = OPEQ("<<") ? ">>" : "<<"; |
| 1026 | numBits = -numBits; |
| 1027 | } |
| 1028 | |
Devin Moore | 1f0360d | 2020-12-21 12:12:48 -0800 | [diff] [blame] | 1029 | #define CASE_SHIFT(__type__) \ |
| 1030 | return is_valid_ = handleShift(*this, static_cast<__type__>(left_val_->final_value_), newOp, \ |
| 1031 | static_cast<__type__>(numBits), &final_value_); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1032 | |
| 1033 | SWITCH_KIND(final_type_, CASE_SHIFT, SHOULD_NOT_REACH(); final_type_ = Type::ERROR; |
| 1034 | is_valid_ = false; return false;) |
| 1035 | } |
| 1036 | |
| 1037 | // CASE: && || |
| 1038 | if (OP_IS_BIN_LOGICAL) { |
| 1039 | final_type_ = Type::BOOLEAN; |
| 1040 | // easy; everything is bool. |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 1041 | return handleLogical(*this, left_val_->final_value_, op_, right_val_->final_value_, |
| 1042 | &final_value_); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | SHOULD_NOT_REACH(); |
| 1046 | is_valid_ = false; |
| 1047 | return false; |
| 1048 | } |
| 1049 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1050 | AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type, |
| 1051 | int64_t parsed_value, const string& checked_value) |
| 1052 | : AidlNode(location), |
| 1053 | type_(parsed_type), |
| 1054 | value_(checked_value), |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1055 | final_type_(parsed_type), |
| 1056 | final_value_(parsed_value) { |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 1057 | AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location); |
| 1058 | AIDL_FATAL_IF(type_ != Type::INT8 && type_ != Type::INT32 && type_ != Type::INT64, location); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1059 | } |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 1060 | |
| 1061 | AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type, |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1062 | const string& checked_value) |
| 1063 | : AidlNode(location), |
| 1064 | type_(type), |
| 1065 | value_(checked_value), |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1066 | final_type_(type) { |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 1067 | AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1068 | switch (type_) { |
| 1069 | case Type::INT8: |
| 1070 | case Type::INT32: |
| 1071 | case Type::INT64: |
| 1072 | case Type::ARRAY: |
| 1073 | AIDL_FATAL(this) << "Invalid type: " << ToString(type_); |
| 1074 | break; |
| 1075 | default: |
| 1076 | break; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type, |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 1081 | std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values, |
| 1082 | const std::string& value) |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1083 | : AidlNode(location), |
| 1084 | type_(type), |
| 1085 | values_(std::move(*values)), |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 1086 | value_(value), |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1087 | is_valid_(false), |
| 1088 | is_evaluated_(false), |
| 1089 | final_type_(type) { |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 1090 | AIDL_FATAL_IF(type_ != Type::ARRAY, location); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | AidlUnaryConstExpression::AidlUnaryConstExpression(const AidlLocation& location, const string& op, |
| 1094 | std::unique_ptr<AidlConstantValue> rval) |
| 1095 | : AidlConstantValue(location, Type::UNARY, op + rval->value_), |
| 1096 | unary_(std::move(rval)), |
| 1097 | op_(op) { |
| 1098 | final_type_ = Type::UNARY; |
| 1099 | } |
| 1100 | |
| 1101 | AidlBinaryConstExpression::AidlBinaryConstExpression(const AidlLocation& location, |
| 1102 | std::unique_ptr<AidlConstantValue> lval, |
| 1103 | const string& op, |
| 1104 | std::unique_ptr<AidlConstantValue> rval) |
| 1105 | : AidlConstantValue(location, Type::BINARY, lval->value_ + op + rval->value_), |
| 1106 | left_val_(std::move(lval)), |
| 1107 | right_val_(std::move(rval)), |
| 1108 | op_(op) { |
| 1109 | final_type_ = Type::BINARY; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 1110 | } |