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