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 | |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [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 | 71a1b58 | 2020-12-25 23:58:41 +0900 | [diff] [blame^] | 251 | // helper to see if T is the same to one of Args types. |
| 252 | template <typename T, typename... Args> |
| 253 | struct is_one_of : std::false_type {}; |
| 254 | |
| 255 | template <typename T, typename S, typename... Args> |
| 256 | struct is_one_of<T, S, Args...> { |
| 257 | enum { value = std::is_same_v<T, S> || is_one_of<T, Args...>::value }; |
| 258 | }; |
| 259 | |
| 260 | // helper to see if T is std::vector of something. |
| 261 | template <typename> |
| 262 | struct is_vector : std::false_type {}; |
| 263 | |
| 264 | template <typename T> |
| 265 | struct is_vector<std::vector<T>> : std::true_type {}; |
| 266 | |
| 267 | template <typename T> |
| 268 | static bool ParseFloating(std::string_view sv, T* parsed) { |
| 269 | static_assert(is_one_of<T, float, double>::value); |
| 270 | android::base::ConsumeSuffix(&sv, "f"); |
| 271 | if constexpr (std::is_same_v<T, float>) { |
| 272 | return android::base::ParseFloat(sv.data(), parsed); |
| 273 | } else { |
| 274 | return android::base::ParseDouble(sv.data(), parsed); |
| 275 | } |
| 276 | } |
| 277 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 278 | bool AidlUnaryConstExpression::IsCompatibleType(Type type, const string& op) { |
| 279 | // Verify the unary type here |
| 280 | switch (type) { |
| 281 | case Type::BOOLEAN: // fall-through |
| 282 | case Type::INT8: // fall-through |
| 283 | case Type::INT32: // fall-through |
| 284 | case Type::INT64: |
| 285 | return true; |
| 286 | case Type::FLOATING: |
| 287 | return (op == "+" || op == "-"); |
| 288 | default: |
| 289 | return false; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | bool AidlBinaryConstExpression::AreCompatibleTypes(Type t1, Type t2) { |
| 294 | switch (t1) { |
| 295 | case Type::STRING: |
| 296 | if (t2 == Type::STRING) { |
| 297 | return true; |
| 298 | } |
| 299 | break; |
| 300 | case Type::BOOLEAN: // fall-through |
| 301 | case Type::INT8: // fall-through |
| 302 | case Type::INT32: // fall-through |
| 303 | case Type::INT64: |
| 304 | switch (t2) { |
| 305 | case Type::BOOLEAN: // fall-through |
| 306 | case Type::INT8: // fall-through |
| 307 | case Type::INT32: // fall-through |
| 308 | case Type::INT64: |
| 309 | return true; |
| 310 | break; |
| 311 | default: |
| 312 | break; |
| 313 | } |
| 314 | break; |
| 315 | default: |
| 316 | break; |
| 317 | } |
| 318 | |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | // Returns the promoted kind for both operands |
| 323 | AidlConstantValue::Type AidlBinaryConstExpression::UsualArithmeticConversion(Type left, |
| 324 | Type right) { |
| 325 | // These are handled as special cases |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 326 | AIDL_FATAL_IF(left == Type::STRING || right == Type::STRING, AIDL_LOCATION_HERE); |
| 327 | AIDL_FATAL_IF(left == Type::FLOATING || right == Type::FLOATING, AIDL_LOCATION_HERE); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 328 | |
| 329 | // Kinds in concern: bool, (u)int[8|32|64] |
| 330 | if (left == right) return left; // easy case |
| 331 | if (left == Type::BOOLEAN) return right; |
| 332 | if (right == Type::BOOLEAN) return left; |
| 333 | |
| 334 | return left < right ? right : left; |
| 335 | } |
| 336 | |
| 337 | // Returns the promoted integral type where INT32 is the smallest type |
| 338 | AidlConstantValue::Type AidlBinaryConstExpression::IntegralPromotion(Type in) { |
| 339 | return (Type::INT32 < in) ? in : Type::INT32; |
| 340 | } |
| 341 | |
| 342 | template <typename T> |
Jooyung Han | 71a1b58 | 2020-12-25 23:58:41 +0900 | [diff] [blame^] | 343 | T AidlConstantValue::Cast() const { |
| 344 | static_assert( |
| 345 | is_one_of<T, bool, char, int8_t, int32_t, int64_t, std::string, float, double>::value || |
| 346 | is_vector<T>::value); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 347 | |
Jooyung Han | 71a1b58 | 2020-12-25 23:58:41 +0900 | [diff] [blame^] | 348 | is_evaluated_ || (CheckValid() && evaluate()); |
| 349 | AIDL_FATAL_IF(!is_valid_, this); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 350 | |
Jooyung Han | 71a1b58 | 2020-12-25 23:58:41 +0900 | [diff] [blame^] | 351 | if constexpr (is_vector<T>::value) { |
| 352 | AIDL_FATAL_IF(final_type_ != Type::ARRAY, this); |
| 353 | T result; |
| 354 | for (const auto& v : values_) { |
| 355 | result.push_back(v->Cast<typename T::value_type>()); |
| 356 | } |
| 357 | return result; |
| 358 | |
| 359 | } else if constexpr (is_one_of<T, float, double>::value) { |
| 360 | AIDL_FATAL_IF(final_type_ != Type::FLOATING, this); |
| 361 | T result; |
| 362 | AIDL_FATAL_IF(!ParseFloating(value_, &result), this); |
| 363 | return result; |
| 364 | } else if constexpr (std::is_same<T, std::string>::value) { |
| 365 | AIDL_FATAL_IF(final_type_ != Type::STRING, this); |
| 366 | return final_string_value_.substr(1, final_string_value_.size() - 2); // unquote " |
| 367 | } else if constexpr (is_one_of<T, int8_t, int32_t, int64_t>::value) { |
| 368 | AIDL_FATAL_IF(final_type_ < Type::INT8 && final_type_ > Type::INT64, this); |
| 369 | return static_cast<T>(final_value_); |
| 370 | } else if constexpr (std::is_same<T, char>::value) { |
| 371 | AIDL_FATAL_IF(final_type_ != Type::CHARACTER, this); |
| 372 | return final_string_value_.at(1); // unquote ' |
| 373 | } else if constexpr (std::is_same<T, bool>::value) { |
| 374 | AIDL_FATAL_IF(final_type_ != Type::BOOLEAN, this); |
| 375 | return final_value_ != 0; |
| 376 | } |
| 377 | SHOULD_NOT_REACH(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Jooyung Han | 71a1b58 | 2020-12-25 23:58:41 +0900 | [diff] [blame^] | 380 | #define INSTANTIATE_AidlConstantValue_Cast(T) \ |
| 381 | template T AidlConstantValue::Cast<T>() const; \ |
| 382 | template std::vector<T> AidlConstantValue::Cast<std::vector<T>>() const |
| 383 | |
| 384 | INSTANTIATE_AidlConstantValue_Cast(int8_t); |
| 385 | INSTANTIATE_AidlConstantValue_Cast(int32_t); |
| 386 | INSTANTIATE_AidlConstantValue_Cast(int64_t); |
| 387 | INSTANTIATE_AidlConstantValue_Cast(float); |
| 388 | INSTANTIATE_AidlConstantValue_Cast(double); |
| 389 | INSTANTIATE_AidlConstantValue_Cast(char); |
| 390 | INSTANTIATE_AidlConstantValue_Cast(bool); |
| 391 | INSTANTIATE_AidlConstantValue_Cast(std::string); |
| 392 | #undef INSTANTIATE_AidlConstantValue_Cast |
| 393 | |
Steven Moreland | 541788d | 2020-05-21 22:05:52 +0000 | [diff] [blame] | 394 | AidlConstantValue* AidlConstantValue::Default(const AidlTypeSpecifier& specifier) { |
| 395 | AidlLocation location = specifier.GetLocation(); |
| 396 | |
| 397 | // allocation of int[0] is a bit wasteful in Java |
| 398 | if (specifier.IsArray()) { |
| 399 | return nullptr; |
| 400 | } |
| 401 | |
| 402 | const std::string name = specifier.GetName(); |
| 403 | if (name == "boolean") { |
| 404 | return Boolean(location, false); |
| 405 | } |
| 406 | if (name == "byte" || name == "int" || name == "long") { |
| 407 | return Integral(location, "0"); |
| 408 | } |
| 409 | if (name == "float") { |
| 410 | return Floating(location, "0.0f"); |
| 411 | } |
| 412 | if (name == "double") { |
| 413 | return Floating(location, "0.0"); |
| 414 | } |
| 415 | return nullptr; |
| 416 | } |
| 417 | |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 418 | AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool value) { |
| 419 | return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false"); |
| 420 | } |
| 421 | |
| 422 | AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) { |
Steven Moreland | cdedd9b | 2019-12-02 10:54:47 -0800 | [diff] [blame] | 423 | const std::string explicit_value = string("'") + value + "'"; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 424 | if (!isValidLiteralChar(value)) { |
| 425 | AIDL_ERROR(location) << "Invalid character literal " << value; |
Steven Moreland | cdedd9b | 2019-12-02 10:54:47 -0800 | [diff] [blame] | 426 | return new AidlConstantValue(location, Type::ERROR, explicit_value); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 427 | } |
Steven Moreland | cdedd9b | 2019-12-02 10:54:47 -0800 | [diff] [blame] | 428 | return new AidlConstantValue(location, Type::CHARACTER, explicit_value); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location, |
| 432 | const std::string& value) { |
| 433 | return new AidlConstantValue(location, Type::FLOATING, value); |
| 434 | } |
| 435 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 436 | bool AidlConstantValue::IsHex(const string& value) { |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 437 | return StartsWith(value, "0x") || StartsWith(value, "0X"); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 440 | bool AidlConstantValue::ParseIntegral(const string& value, int64_t* parsed_value, |
| 441 | Type* parsed_type) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 442 | if (parsed_value == nullptr || parsed_type == nullptr) { |
| 443 | return false; |
| 444 | } |
| 445 | |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 446 | const bool isLong = EndsWith(value, 'l') || EndsWith(value, 'L'); |
| 447 | 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] | 448 | |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 449 | if (IsHex(value)) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 450 | // AIDL considers 'const int foo = 0xffffffff' as -1, but if we want to |
| 451 | // handle that when computing constant expressions, then we need to |
| 452 | // represent 0xffffffff as a uint32_t. However, AIDL only has signed types; |
| 453 | // so we parse as an unsigned int when possible and then cast to a signed |
| 454 | // int. One example of this is in ICameraService.aidl where a constant int |
| 455 | // is used for bit manipulations which ideally should be handled with an |
| 456 | // unsigned int. |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 457 | // |
| 458 | // Note, for historical consistency, we need to consider small hex values |
| 459 | // as an integral type. Recognizing them as INT8 could break some files, |
| 460 | // even though it would simplify this code. |
| 461 | if (uint32_t rawValue32; |
| 462 | !isLong && android::base::ParseUint<uint32_t>(value_substr, &rawValue32)) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 463 | *parsed_value = static_cast<int32_t>(rawValue32); |
| 464 | *parsed_type = Type::INT32; |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 465 | } else if (uint64_t rawValue64; android::base::ParseUint<uint64_t>(value_substr, &rawValue64)) { |
| 466 | *parsed_value = static_cast<int64_t>(rawValue64); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 467 | *parsed_type = Type::INT64; |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 468 | } else { |
| 469 | *parsed_value = 0; |
| 470 | *parsed_type = Type::ERROR; |
| 471 | return false; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 472 | } |
| 473 | return true; |
| 474 | } |
| 475 | |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 476 | if (!android::base::ParseInt<int64_t>(value_substr, parsed_value)) { |
| 477 | *parsed_value = 0; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 478 | *parsed_type = Type::ERROR; |
| 479 | return false; |
| 480 | } |
| 481 | |
Steven Moreland | cef2266 | 2020-07-08 20:54:28 +0000 | [diff] [blame] | 482 | if (isLong) { |
| 483 | *parsed_type = Type::INT64; |
| 484 | } else { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 485 | // guess literal type. |
| 486 | if (*parsed_value <= INT8_MAX && *parsed_value >= INT8_MIN) { |
| 487 | *parsed_type = Type::INT8; |
| 488 | } else if (*parsed_value <= INT32_MAX && *parsed_value >= INT32_MIN) { |
| 489 | *parsed_type = Type::INT32; |
| 490 | } else { |
| 491 | *parsed_type = Type::INT64; |
| 492 | } |
| 493 | } |
| 494 | return true; |
| 495 | } |
| 496 | |
| 497 | AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location, const string& value) { |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 498 | AIDL_FATAL_IF(value.empty(), location); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 499 | |
| 500 | Type parsed_type; |
| 501 | int64_t parsed_value = 0; |
| 502 | bool success = ParseIntegral(value, &parsed_value, &parsed_type); |
| 503 | if (!success) { |
| 504 | return nullptr; |
| 505 | } |
| 506 | |
| 507 | return new AidlConstantValue(location, parsed_type, parsed_value, value); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | AidlConstantValue* AidlConstantValue::Array( |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 511 | const AidlLocation& location, std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values) { |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 512 | AIDL_FATAL_IF(values == nullptr, location); |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 513 | std::vector<std::string> str_values; |
| 514 | for (const auto& v : *values) { |
| 515 | str_values.push_back(v->value_); |
| 516 | } |
| 517 | return new AidlConstantValue(location, Type::ARRAY, std::move(values), Join(str_values, ", ")); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 520 | AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, const string& value) { |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 521 | for (size_t i = 0; i < value.length(); ++i) { |
| 522 | if (!isValidLiteralChar(value[i])) { |
| 523 | AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '" |
| 524 | << value << "'"; |
Steven Moreland | cdedd9b | 2019-12-02 10:54:47 -0800 | [diff] [blame] | 525 | return new AidlConstantValue(location, Type::ERROR, value); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
| 529 | return new AidlConstantValue(location, Type::STRING, value); |
| 530 | } |
| 531 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 532 | string AidlConstantValue::ValueString(const AidlTypeSpecifier& type, |
| 533 | const ConstantValueDecorator& decorator) const { |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 534 | if (type.IsGeneric()) { |
| 535 | AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal."; |
| 536 | return ""; |
| 537 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 538 | if (!is_evaluated_) { |
| 539 | // TODO(b/142722772) CheckValid() should be called before ValueString() |
| 540 | bool success = CheckValid(); |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 541 | success &= evaluate(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 542 | if (!success) { |
| 543 | // the detailed error message shall be printed in evaluate |
| 544 | return ""; |
| 545 | } |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 546 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 547 | if (!is_valid_) { |
| 548 | AIDL_ERROR(this) << "Invalid constant value: " + value_; |
| 549 | return ""; |
| 550 | } |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 551 | |
| 552 | const AidlDefinedType* defined_type = type.GetDefinedType(); |
| 553 | if (defined_type && !type.IsArray()) { |
| 554 | const AidlEnumDeclaration* enum_type = defined_type->AsEnumDeclaration(); |
| 555 | if (!enum_type) { |
| 556 | AIDL_ERROR(this) << "Invalid type (" << defined_type->GetCanonicalName() |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 557 | << ") for a const value (" << value_ << ")"; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 558 | return ""; |
| 559 | } |
| 560 | if (type_ != Type::REF) { |
| 561 | AIDL_ERROR(this) << "Invalid value (" << value_ << ") for enum " |
| 562 | << enum_type->GetCanonicalName(); |
| 563 | return ""; |
| 564 | } |
| 565 | return decorator(type, value_); |
| 566 | } |
| 567 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 568 | const string& type_string = type.GetName(); |
| 569 | int err = 0; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 570 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 571 | switch (final_type_) { |
| 572 | case Type::CHARACTER: |
| 573 | if (type_string == "char") { |
| 574 | return decorator(type, final_string_value_); |
| 575 | } |
| 576 | err = -1; |
| 577 | break; |
| 578 | case Type::STRING: |
| 579 | if (type_string == "String") { |
| 580 | return decorator(type, final_string_value_); |
| 581 | } |
| 582 | err = -1; |
| 583 | break; |
| 584 | case Type::BOOLEAN: // fall-through |
| 585 | case Type::INT8: // fall-through |
| 586 | case Type::INT32: // fall-through |
| 587 | case Type::INT64: |
| 588 | if (type_string == "byte") { |
| 589 | if (final_value_ > INT8_MAX || final_value_ < INT8_MIN) { |
| 590 | err = -1; |
| 591 | break; |
| 592 | } |
| 593 | return decorator(type, std::to_string(static_cast<int8_t>(final_value_))); |
| 594 | } else if (type_string == "int") { |
| 595 | if (final_value_ > INT32_MAX || final_value_ < INT32_MIN) { |
| 596 | err = -1; |
| 597 | break; |
| 598 | } |
| 599 | return decorator(type, std::to_string(static_cast<int32_t>(final_value_))); |
| 600 | } else if (type_string == "long") { |
| 601 | return decorator(type, std::to_string(final_value_)); |
| 602 | } else if (type_string == "boolean") { |
| 603 | return decorator(type, final_value_ ? "true" : "false"); |
| 604 | } |
| 605 | err = -1; |
| 606 | break; |
| 607 | case Type::ARRAY: { |
| 608 | if (!type.IsArray()) { |
| 609 | err = -1; |
| 610 | break; |
| 611 | } |
| 612 | vector<string> value_strings; |
| 613 | value_strings.reserve(values_.size()); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 614 | bool success = true; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 615 | |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 616 | for (const auto& value : values_) { |
| 617 | const AidlTypeSpecifier& array_base = type.ArrayBase(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 618 | const string value_string = value->ValueString(array_base, decorator); |
| 619 | if (value_string.empty()) { |
| 620 | success = false; |
| 621 | break; |
| 622 | } |
| 623 | value_strings.push_back(value_string); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 624 | } |
| 625 | if (!success) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 626 | err = -1; |
| 627 | break; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 628 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 629 | |
| 630 | return decorator(type, "{" + Join(value_strings, ", ") + "}"); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 631 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 632 | case Type::FLOATING: { |
| 633 | std::string_view raw_view(value_.c_str()); |
| 634 | bool is_float_literal = ConsumeSuffix(&raw_view, "f"); |
| 635 | std::string stripped_value = std::string(raw_view); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 636 | |
| 637 | if (type_string == "double") { |
| 638 | double parsed_value; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 639 | if (!android::base::ParseDouble(stripped_value, &parsed_value)) { |
| 640 | AIDL_ERROR(this) << "Could not parse " << value_; |
| 641 | err = -1; |
| 642 | break; |
| 643 | } |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 644 | return decorator(type, std::to_string(parsed_value)); |
| 645 | } |
| 646 | if (is_float_literal && type_string == "float") { |
| 647 | float parsed_value; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 648 | if (!android::base::ParseFloat(stripped_value, &parsed_value)) { |
| 649 | AIDL_ERROR(this) << "Could not parse " << value_; |
| 650 | err = -1; |
| 651 | break; |
| 652 | } |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 653 | return decorator(type, std::to_string(parsed_value) + "f"); |
| 654 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 655 | err = -1; |
| 656 | break; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 657 | } |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 658 | default: |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 659 | err = -1; |
| 660 | break; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 663 | AIDL_FATAL_IF(err == 0, this); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 664 | AIDL_ERROR(this) << "Invalid type specifier for " << ToString(final_type_) << ": " << type_string; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 665 | return ""; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | bool AidlConstantValue::CheckValid() const { |
| 669 | // Nothing needs to be checked here. The constant value will be validated in |
| 670 | // the constructor or in the evaluate() function. |
| 671 | if (is_evaluated_) return is_valid_; |
| 672 | |
| 673 | switch (type_) { |
| 674 | case Type::BOOLEAN: // fall-through |
| 675 | case Type::INT8: // fall-through |
| 676 | case Type::INT32: // fall-through |
| 677 | case Type::INT64: // fall-through |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 678 | case Type::CHARACTER: // fall-through |
| 679 | case Type::STRING: // fall-through |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 680 | case Type::REF: // fall-through |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 681 | case Type::FLOATING: // fall-through |
| 682 | case Type::UNARY: // fall-through |
| 683 | case Type::BINARY: |
| 684 | is_valid_ = true; |
| 685 | break; |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 686 | case Type::ARRAY: |
| 687 | is_valid_ = true; |
| 688 | for (const auto& v : values_) is_valid_ &= v->CheckValid(); |
| 689 | break; |
Steven Moreland | 4ff04aa | 2019-12-02 10:44:29 -0800 | [diff] [blame] | 690 | case Type::ERROR: |
| 691 | return false; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 692 | default: |
| 693 | AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_); |
| 694 | return false; |
| 695 | } |
| 696 | |
| 697 | return true; |
| 698 | } |
| 699 | |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 700 | bool AidlConstantValue::evaluate() const { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 701 | if (is_evaluated_) { |
| 702 | return is_valid_; |
| 703 | } |
| 704 | int err = 0; |
| 705 | is_evaluated_ = true; |
| 706 | |
| 707 | switch (type_) { |
| 708 | case Type::ARRAY: { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 709 | Type array_type = Type::ERROR; |
| 710 | bool success = true; |
| 711 | for (const auto& value : values_) { |
| 712 | success = value->CheckValid(); |
| 713 | if (success) { |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 714 | success = value->evaluate(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 715 | if (!success) { |
| 716 | AIDL_ERROR(this) << "Invalid array element: " << value->value_; |
| 717 | break; |
| 718 | } |
| 719 | if (array_type == Type::ERROR) { |
| 720 | array_type = value->final_type_; |
| 721 | } else if (!AidlBinaryConstExpression::AreCompatibleTypes(array_type, |
| 722 | value->final_type_)) { |
| 723 | AIDL_ERROR(this) << "Incompatible array element type: " << ToString(value->final_type_) |
| 724 | << ". Expecting type compatible with " << ToString(array_type); |
| 725 | success = false; |
| 726 | break; |
| 727 | } |
| 728 | } else { |
| 729 | break; |
| 730 | } |
| 731 | } |
| 732 | if (!success) { |
| 733 | err = -1; |
| 734 | break; |
| 735 | } |
| 736 | final_type_ = type_; |
| 737 | break; |
| 738 | } |
| 739 | case Type::BOOLEAN: |
| 740 | if ((value_ != "true") && (value_ != "false")) { |
| 741 | AIDL_ERROR(this) << "Invalid constant boolean value: " << value_; |
| 742 | err = -1; |
| 743 | break; |
| 744 | } |
| 745 | final_value_ = (value_ == "true") ? 1 : 0; |
| 746 | final_type_ = type_; |
| 747 | break; |
| 748 | case Type::INT8: // fall-through |
| 749 | case Type::INT32: // fall-through |
| 750 | case Type::INT64: |
| 751 | // Parsing happens in the constructor |
| 752 | final_type_ = type_; |
| 753 | break; |
| 754 | case Type::CHARACTER: // fall-through |
| 755 | case Type::STRING: |
| 756 | final_string_value_ = value_; |
| 757 | final_type_ = type_; |
| 758 | break; |
| 759 | case Type::FLOATING: |
| 760 | // Just parse on the fly in ValueString |
| 761 | final_type_ = type_; |
| 762 | break; |
| 763 | default: |
| 764 | AIDL_FATAL(this) << "Unrecognized constant value type: " << ToString(type_); |
| 765 | err = -1; |
| 766 | } |
| 767 | |
| 768 | return (err == 0) ? true : false; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | string AidlConstantValue::ToString(Type type) { |
| 772 | switch (type) { |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 773 | case Type::BOOLEAN: |
| 774 | return "a literal boolean"; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 775 | case Type::INT8: |
| 776 | return "an int8 literal"; |
| 777 | case Type::INT32: |
| 778 | return "an int32 literal"; |
| 779 | case Type::INT64: |
| 780 | return "an int64 literal"; |
Steven Moreland | a923a72 | 2019-11-26 20:08:30 -0800 | [diff] [blame] | 781 | case Type::ARRAY: |
| 782 | return "a literal array"; |
| 783 | case Type::CHARACTER: |
| 784 | return "a literal char"; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 785 | case Type::STRING: |
| 786 | return "a literal string"; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 787 | case Type::REF: |
| 788 | return "a reference"; |
Steven Moreland | a923a72 | 2019-11-26 20:08:30 -0800 | [diff] [blame] | 789 | case Type::FLOATING: |
| 790 | return "a literal float"; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 791 | case Type::UNARY: |
| 792 | return "a unary expression"; |
| 793 | case Type::BINARY: |
| 794 | return "a binary expression"; |
Steven Moreland | a923a72 | 2019-11-26 20:08:30 -0800 | [diff] [blame] | 795 | case Type::ERROR: |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 796 | 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] | 797 | return ""; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 798 | default: |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 799 | AIDL_FATAL(AIDL_LOCATION_HERE) |
| 800 | << "aidl internal error: unknown constant type: " << static_cast<int>(type); |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 801 | return ""; // not reached |
| 802 | } |
| 803 | } |
| 804 | |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 805 | AidlConstantReference::AidlConstantReference(const AidlLocation& location, const std::string& value, |
| 806 | const std::string& comments) |
| 807 | : AidlConstantValue(location, Type::REF, value), comments_(comments) { |
| 808 | const auto pos = value.find_last_of('.'); |
| 809 | if (pos == string::npos) { |
| 810 | field_name_ = value; |
| 811 | } else { |
| 812 | ref_type_ = |
| 813 | std::make_unique<AidlTypeSpecifier>(location, value.substr(0, pos), false, nullptr, ""); |
| 814 | field_name_ = value.substr(pos + 1); |
| 815 | } |
| 816 | } |
| 817 | |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 818 | const AidlConstantValue* AidlConstantReference::Resolve() { |
| 819 | if (resolved_) return resolved_; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 820 | if (!GetRefType() || !GetRefType()->GetDefinedType()) { |
| 821 | // This can happen when "const reference" is used in an unsupported way, |
| 822 | // but missed in checks there. It works as a safety net. |
| 823 | AIDL_ERROR(*this) << "Can't resolve the reference (" << value_ << ")"; |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 824 | return nullptr; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | auto defined_type = GetRefType()->GetDefinedType(); |
| 828 | if (auto enum_decl = defined_type->AsEnumDeclaration(); enum_decl) { |
| 829 | for (const auto& e : enum_decl->GetEnumerators()) { |
| 830 | if (e->GetName() == field_name_) { |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 831 | resolved_ = e->GetValue(); |
| 832 | return resolved_; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 833 | } |
| 834 | } |
| 835 | } else { |
| 836 | for (const auto& c : defined_type->GetConstantDeclarations()) { |
| 837 | if (c->GetName() == field_name_) { |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 838 | resolved_ = &c->GetValue(); |
| 839 | return resolved_; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | } |
| 843 | AIDL_ERROR(*this) << "Can't find " << field_name_ << " in " << ref_type_->GetName(); |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 844 | return nullptr; |
| 845 | } |
| 846 | |
| 847 | bool AidlConstantReference::CheckValid() const { |
| 848 | if (is_evaluated_) return is_valid_; |
| 849 | AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_; |
| 850 | is_valid_ = resolved_->CheckValid(); |
| 851 | return is_valid_; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 852 | } |
| 853 | |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 854 | bool AidlConstantReference::evaluate() const { |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 855 | if (is_evaluated_) return is_valid_; |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 856 | AIDL_FATAL_IF(!resolved_, this) << "Should be resolved first: " << value_; |
| 857 | is_evaluated_ = true; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 858 | |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 859 | resolved_->evaluate(); |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 860 | is_valid_ = resolved_->is_valid_; |
| 861 | final_type_ = resolved_->final_type_; |
| 862 | if (is_valid_) { |
| 863 | if (final_type_ == Type::STRING) { |
| 864 | final_string_value_ = resolved_->final_string_value_; |
| 865 | } else { |
| 866 | final_value_ = resolved_->final_value_; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 867 | } |
| 868 | } |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 869 | return is_valid_; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 870 | } |
| 871 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 872 | bool AidlUnaryConstExpression::CheckValid() const { |
| 873 | if (is_evaluated_) return is_valid_; |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 874 | AIDL_FATAL_IF(unary_ == nullptr, this); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 875 | |
| 876 | is_valid_ = unary_->CheckValid(); |
| 877 | if (!is_valid_) { |
| 878 | final_type_ = Type::ERROR; |
| 879 | return false; |
| 880 | } |
| 881 | |
Steven Moreland | 4bcb05c | 2019-11-27 18:57:47 -0800 | [diff] [blame] | 882 | return AidlConstantValue::CheckValid(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 885 | bool AidlUnaryConstExpression::evaluate() const { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 886 | if (is_evaluated_) { |
| 887 | return is_valid_; |
| 888 | } |
| 889 | is_evaluated_ = true; |
| 890 | |
| 891 | // Recursively evaluate the expression tree |
| 892 | if (!unary_->is_evaluated_) { |
| 893 | // TODO(b/142722772) CheckValid() should be called before ValueString() |
| 894 | bool success = CheckValid(); |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 895 | success &= unary_->evaluate(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 896 | if (!success) { |
| 897 | is_valid_ = false; |
| 898 | return false; |
| 899 | } |
| 900 | } |
Devin Moore | c233fb8 | 2020-04-07 11:13:44 -0700 | [diff] [blame] | 901 | if (!IsCompatibleType(unary_->final_type_, op_)) { |
| 902 | AIDL_ERROR(unary_) << "'" << op_ << "'" |
| 903 | << " is not compatible with " << ToString(unary_->final_type_) |
| 904 | << ": " + value_; |
| 905 | is_valid_ = false; |
| 906 | return false; |
| 907 | } |
| 908 | if (!unary_->is_valid_) { |
| 909 | AIDL_ERROR(unary_) << "Invalid constant unary expression: " + value_; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 910 | is_valid_ = false; |
| 911 | return false; |
| 912 | } |
| 913 | final_type_ = unary_->final_type_; |
| 914 | |
| 915 | if (final_type_ == Type::FLOATING) { |
| 916 | // don't do anything here. ValueString() will handle everything. |
| 917 | is_valid_ = true; |
| 918 | return true; |
| 919 | } |
| 920 | |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 921 | #define CASE_UNARY(__type__) \ |
Devin Moore | 1f0360d | 2020-12-21 12:12:48 -0800 | [diff] [blame] | 922 | return is_valid_ = \ |
| 923 | handleUnary(*this, op_, static_cast<__type__>(unary_->final_value_), &final_value_); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 924 | |
| 925 | SWITCH_KIND(final_type_, CASE_UNARY, SHOULD_NOT_REACH(); final_type_ = Type::ERROR; |
| 926 | is_valid_ = false; return false;) |
| 927 | } |
| 928 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 929 | bool AidlBinaryConstExpression::CheckValid() const { |
| 930 | bool success = false; |
| 931 | if (is_evaluated_) return is_valid_; |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 932 | AIDL_FATAL_IF(left_val_ == nullptr, this); |
| 933 | AIDL_FATAL_IF(right_val_ == nullptr, this); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 934 | |
| 935 | success = left_val_->CheckValid(); |
| 936 | if (!success) { |
| 937 | final_type_ = Type::ERROR; |
| 938 | AIDL_ERROR(this) << "Invalid left operand in binary expression: " + value_; |
| 939 | } |
| 940 | |
| 941 | success = right_val_->CheckValid(); |
| 942 | if (!success) { |
| 943 | AIDL_ERROR(this) << "Invalid right operand in binary expression: " + value_; |
| 944 | final_type_ = Type::ERROR; |
| 945 | } |
| 946 | |
| 947 | if (final_type_ == Type::ERROR) { |
| 948 | is_valid_ = false; |
| 949 | return false; |
| 950 | } |
| 951 | |
| 952 | is_valid_ = true; |
Steven Moreland | 4bcb05c | 2019-11-27 18:57:47 -0800 | [diff] [blame] | 953 | return AidlConstantValue::CheckValid(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 956 | bool AidlBinaryConstExpression::evaluate() const { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 957 | if (is_evaluated_) { |
| 958 | return is_valid_; |
| 959 | } |
| 960 | is_evaluated_ = true; |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 961 | AIDL_FATAL_IF(left_val_ == nullptr, this); |
| 962 | AIDL_FATAL_IF(right_val_ == nullptr, this); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 963 | |
| 964 | // Recursively evaluate the binary expression tree |
| 965 | if (!left_val_->is_evaluated_ || !right_val_->is_evaluated_) { |
| 966 | // TODO(b/142722772) CheckValid() should be called before ValueString() |
| 967 | bool success = CheckValid(); |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 968 | success &= left_val_->evaluate(); |
| 969 | success &= right_val_->evaluate(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 970 | if (!success) { |
| 971 | is_valid_ = false; |
| 972 | return false; |
| 973 | } |
| 974 | } |
| 975 | if (!left_val_->is_valid_ || !right_val_->is_valid_) { |
| 976 | is_valid_ = false; |
| 977 | return false; |
| 978 | } |
| 979 | is_valid_ = AreCompatibleTypes(left_val_->final_type_, right_val_->final_type_); |
| 980 | if (!is_valid_) { |
Steven Moreland | 1f9f221 | 2020-09-24 18:20:15 +0000 | [diff] [blame] | 981 | AIDL_ERROR(this) << "Cannot perform operation '" << op_ << "' on " |
| 982 | << ToString(right_val_->GetType()) << " and " << ToString(left_val_->GetType()) |
| 983 | << "."; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 984 | return false; |
| 985 | } |
| 986 | |
| 987 | bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP; |
| 988 | |
| 989 | // Handle String case first |
| 990 | if (left_val_->final_type_ == Type::STRING) { |
Steven Moreland | 22e3611 | 2020-10-01 00:50:45 +0000 | [diff] [blame] | 991 | AIDL_FATAL_IF(right_val_->final_type_ != Type::STRING, this); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 992 | if (!OPEQ("+")) { |
Steven Moreland | 22e3611 | 2020-10-01 00:50:45 +0000 | [diff] [blame] | 993 | AIDL_ERROR(this) << "Only '+' is supported for strings, not '" << op_ << "'."; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 994 | final_type_ = Type::ERROR; |
| 995 | is_valid_ = false; |
| 996 | return false; |
| 997 | } |
| 998 | |
| 999 | // Remove trailing " from lhs |
| 1000 | const string& lhs = left_val_->final_string_value_; |
| 1001 | if (lhs.back() != '"') { |
| 1002 | AIDL_ERROR(this) << "'" << lhs << "' is missing a trailing quote."; |
| 1003 | final_type_ = Type::ERROR; |
| 1004 | is_valid_ = false; |
| 1005 | return false; |
| 1006 | } |
| 1007 | const string& rhs = right_val_->final_string_value_; |
| 1008 | // Remove starting " from rhs |
| 1009 | if (rhs.front() != '"') { |
| 1010 | AIDL_ERROR(this) << "'" << rhs << "' is missing a leading quote."; |
| 1011 | final_type_ = Type::ERROR; |
| 1012 | is_valid_ = false; |
| 1013 | return false; |
| 1014 | } |
| 1015 | |
| 1016 | final_string_value_ = string(lhs.begin(), lhs.end() - 1).append(rhs.begin() + 1, rhs.end()); |
| 1017 | final_type_ = Type::STRING; |
| 1018 | return true; |
| 1019 | } |
| 1020 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1021 | // CASE: + - * / % | ^ & < > <= >= == != |
| 1022 | if (isArithmeticOrBitflip || OP_IS_BIN_COMP) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1023 | // promoted kind for both operands. |
| 1024 | Type promoted = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_), |
| 1025 | IntegralPromotion(right_val_->final_type_)); |
| 1026 | // result kind. |
| 1027 | final_type_ = isArithmeticOrBitflip |
| 1028 | ? promoted // arithmetic or bitflip operators generates promoted type |
| 1029 | : Type::BOOLEAN; // comparison operators generates bool |
| 1030 | |
Devin Moore | 1f0360d | 2020-12-21 12:12:48 -0800 | [diff] [blame] | 1031 | #define CASE_BINARY_COMMON(__type__) \ |
| 1032 | return is_valid_ = \ |
| 1033 | handleBinaryCommon(*this, static_cast<__type__>(left_val_->final_value_), op_, \ |
| 1034 | static_cast<__type__>(right_val_->final_value_), &final_value_); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1035 | |
| 1036 | SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); final_type_ = Type::ERROR; |
| 1037 | is_valid_ = false; return false;) |
| 1038 | } |
| 1039 | |
| 1040 | // CASE: << >> |
| 1041 | string newOp = op_; |
| 1042 | if (OP_IS_BIN_SHIFT) { |
Devin Moore | 0482302 | 2020-09-11 10:43:35 -0700 | [diff] [blame] | 1043 | // promoted kind for both operands. |
| 1044 | final_type_ = UsualArithmeticConversion(IntegralPromotion(left_val_->final_type_), |
| 1045 | IntegralPromotion(right_val_->final_type_)); |
| 1046 | auto numBits = right_val_->final_value_; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1047 | if (numBits < 0) { |
Steven Moreland | 74d3f55 | 2020-02-04 15:57:50 -0800 | [diff] [blame] | 1048 | // 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] | 1049 | // is defined as shifting into the other direction. |
| 1050 | newOp = OPEQ("<<") ? ">>" : "<<"; |
| 1051 | numBits = -numBits; |
| 1052 | } |
| 1053 | |
Devin Moore | 1f0360d | 2020-12-21 12:12:48 -0800 | [diff] [blame] | 1054 | #define CASE_SHIFT(__type__) \ |
| 1055 | return is_valid_ = handleShift(*this, static_cast<__type__>(left_val_->final_value_), newOp, \ |
| 1056 | static_cast<__type__>(numBits), &final_value_); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1057 | |
| 1058 | SWITCH_KIND(final_type_, CASE_SHIFT, SHOULD_NOT_REACH(); final_type_ = Type::ERROR; |
| 1059 | is_valid_ = false; return false;) |
| 1060 | } |
| 1061 | |
| 1062 | // CASE: && || |
| 1063 | if (OP_IS_BIN_LOGICAL) { |
| 1064 | final_type_ = Type::BOOLEAN; |
| 1065 | // easy; everything is bool. |
Steven Moreland | e1ff67e | 2020-07-16 23:22:36 +0000 | [diff] [blame] | 1066 | return handleLogical(*this, left_val_->final_value_, op_, right_val_->final_value_, |
| 1067 | &final_value_); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | SHOULD_NOT_REACH(); |
| 1071 | is_valid_ = false; |
| 1072 | return false; |
| 1073 | } |
| 1074 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1075 | AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type parsed_type, |
| 1076 | int64_t parsed_value, const string& checked_value) |
| 1077 | : AidlNode(location), |
| 1078 | type_(parsed_type), |
| 1079 | value_(checked_value), |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1080 | final_type_(parsed_type), |
| 1081 | final_value_(parsed_value) { |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 1082 | AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location); |
| 1083 | 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] | 1084 | } |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 1085 | |
| 1086 | AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type, |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1087 | const string& checked_value) |
| 1088 | : AidlNode(location), |
| 1089 | type_(type), |
| 1090 | value_(checked_value), |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1091 | final_type_(type) { |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 1092 | AIDL_FATAL_IF(value_.empty() && type_ != Type::ERROR, location); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1093 | switch (type_) { |
| 1094 | case Type::INT8: |
| 1095 | case Type::INT32: |
| 1096 | case Type::INT64: |
| 1097 | case Type::ARRAY: |
| 1098 | AIDL_FATAL(this) << "Invalid type: " << ToString(type_); |
| 1099 | break; |
| 1100 | default: |
| 1101 | break; |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type, |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 1106 | std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values, |
| 1107 | const std::string& value) |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1108 | : AidlNode(location), |
| 1109 | type_(type), |
| 1110 | values_(std::move(*values)), |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 1111 | value_(value), |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1112 | is_valid_(false), |
| 1113 | is_evaluated_(false), |
| 1114 | final_type_(type) { |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 1115 | AIDL_FATAL_IF(type_ != Type::ARRAY, location); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | AidlUnaryConstExpression::AidlUnaryConstExpression(const AidlLocation& location, const string& op, |
| 1119 | std::unique_ptr<AidlConstantValue> rval) |
| 1120 | : AidlConstantValue(location, Type::UNARY, op + rval->value_), |
| 1121 | unary_(std::move(rval)), |
| 1122 | op_(op) { |
| 1123 | final_type_ = Type::UNARY; |
| 1124 | } |
| 1125 | |
| 1126 | AidlBinaryConstExpression::AidlBinaryConstExpression(const AidlLocation& location, |
| 1127 | std::unique_ptr<AidlConstantValue> lval, |
| 1128 | const string& op, |
| 1129 | std::unique_ptr<AidlConstantValue> rval) |
| 1130 | : AidlConstantValue(location, Type::BINARY, lval->value_ + op + rval->value_), |
| 1131 | left_val_(std::move(lval)), |
| 1132 | right_val_(std::move(rval)), |
| 1133 | op_(op) { |
| 1134 | final_type_ = Type::BINARY; |
Will McVicker | efd970d | 2019-09-25 15:28:30 -0700 | [diff] [blame] | 1135 | } |