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