Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 17 | #include "ConstantExpression.h" |
| 18 | |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 19 | #include <android-base/logging.h> |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 20 | #include <android-base/parseint.h> |
| 21 | #include <stdio.h> |
| 22 | #include <algorithm> |
Timur Iskhakov | e8ee6a0 | 2017-09-06 11:42:10 -0700 | [diff] [blame] | 23 | #include <iostream> |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 24 | #include <sstream> |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 25 | #include <string> |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 26 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 27 | #include "EnumType.h" |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 28 | #include "Scope.h" // LocalIdentifier |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 29 | |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 30 | // The macros are really nasty here. Consider removing |
| 31 | // as many macros as possible. |
| 32 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 33 | #define OPEQ(__y__) (std::string(mOp) == std::string(__y__)) |
| 34 | #define COMPUTE_UNARY(__op__) if (op == std::string(#__op__)) return __op__ val; |
| 35 | #define COMPUTE_BINARY(__op__) if (op == std::string(#__op__)) return lval __op__ rval; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 36 | #define OP_IS_BIN_ARITHMETIC (OPEQ("+") || OPEQ("-") || OPEQ("*") || OPEQ("/") || OPEQ("%")) |
| 37 | #define OP_IS_BIN_BITFLIP (OPEQ("|") || OPEQ("^") || OPEQ("&")) |
| 38 | #define OP_IS_BIN_COMP (OPEQ("<") || OPEQ(">") || OPEQ("<=") || OPEQ(">=") || OPEQ("==") || OPEQ("!=")) |
| 39 | #define OP_IS_BIN_SHIFT (OPEQ(">>") || OPEQ("<<")) |
| 40 | #define OP_IS_BIN_LOGICAL (OPEQ("||") || OPEQ("&&")) |
| 41 | #define SK(__x__) ScalarType::Kind::KIND_##__x__ |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 42 | #define SHOULD_NOT_REACH() CHECK(false) << __LINE__ << ": should not reach here: " |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 43 | |
Chih-Hung Hsieh | 306cc4b | 2017-08-02 14:59:25 -0700 | [diff] [blame] | 44 | // NOLINT to suppress missing parentheses warnings about __def__. |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 45 | #define SWITCH_KIND(__cond__, __action__, __def__) \ |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 46 | switch(__cond__) { \ |
| 47 | case SK(BOOL): __action__(bool) \ |
| 48 | case SK(UINT8): __action__(uint8_t) \ |
| 49 | case SK(INT8): __action__(int8_t) \ |
| 50 | case SK(UINT16): __action__(uint16_t) \ |
| 51 | case SK(INT16): __action__(int16_t) \ |
| 52 | case SK(UINT32): __action__(uint32_t) \ |
| 53 | case SK(INT32): __action__(int32_t) \ |
| 54 | case SK(UINT64): __action__(uint64_t) \ |
| 55 | case SK(INT64): __action__(int64_t) \ |
Chih-Hung Hsieh | 306cc4b | 2017-08-02 14:59:25 -0700 | [diff] [blame] | 56 | default: __def__ /* NOLINT */ \ |
| 57 | } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 58 | |
| 59 | namespace android { |
| 60 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 61 | static inline bool isSupported(ScalarType::Kind kind) { |
Timur Iskhakov | 63f3990 | 2017-08-29 15:47:29 -0700 | [diff] [blame] | 62 | return SK(BOOL) == kind || ScalarType(kind, nullptr /* parent */).isValidEnumStorageType(); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 65 | /* See docs at the end for details on integral promotion. */ |
| 66 | ScalarType::Kind integralPromotion(ScalarType::Kind in) { |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 67 | return SK(INT32) < in ? in : SK(INT32); // note that KIND_INT32 < KIND_UINT32 |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 70 | /* See docs at the end for details on usual arithmetic conversion. */ |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 71 | ScalarType::Kind usualArithmeticConversion(ScalarType::Kind lft, |
| 72 | ScalarType::Kind rgt) { |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 73 | CHECK(isSupported(lft) && isSupported(rgt)); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 74 | // Kinds in concern: bool, (u)int[8|16|32|64] |
| 75 | if(lft == rgt) return lft; // easy case |
| 76 | if(lft == SK(BOOL)) return rgt; |
| 77 | if(rgt == SK(BOOL)) return lft; |
| 78 | bool isLftSigned = (lft == SK(INT8)) || (lft == SK(INT16)) |
| 79 | || (lft == SK(INT32)) || (lft == SK(INT64)); |
| 80 | bool isRgtSigned = (rgt == SK(INT8)) || (rgt == SK(INT16)) |
| 81 | || (rgt == SK(INT32)) || (rgt == SK(INT64)); |
| 82 | if(isLftSigned == isRgtSigned) return lft < rgt ? rgt : lft; |
| 83 | ScalarType::Kind unsignedRank = isLftSigned ? rgt : lft; |
| 84 | ScalarType::Kind signedRank = isLftSigned ? lft : rgt; |
| 85 | if(unsignedRank >= signedRank) return unsignedRank; |
| 86 | if(signedRank > unsignedRank) return signedRank; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 87 | |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 88 | // Although there is such rule to return "the unsigned counterpart of |
| 89 | // the signed operand", it should not reach here in our HIDL grammar. |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 90 | CHECK(false) << "Could not do usual arithmetic conversion for type " << lft << "and" << rgt; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 91 | switch(signedRank) { |
| 92 | case SK(INT8): return SK(UINT8); |
| 93 | case SK(INT16): return SK(UINT16); |
| 94 | case SK(INT32): return SK(UINT32); |
| 95 | case SK(INT64): return SK(UINT64); |
| 96 | default: return SK(UINT64); |
| 97 | } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 98 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 99 | |
| 100 | template <class T> |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 101 | T handleUnary(const std::string& op, T val) { |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 102 | COMPUTE_UNARY(+) |
| 103 | COMPUTE_UNARY(-) |
| 104 | COMPUTE_UNARY(!) |
Yifan Hong | 2ddbe4b | 2020-02-20 17:30:00 -0800 | [diff] [blame] | 105 | |
| 106 | // bitwise negation of a boolean expression always evaluates to 'true' |
| 107 | #pragma clang diagnostic push |
| 108 | #pragma clang diagnostic ignored "-Wbool-operation" |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 109 | COMPUTE_UNARY(~) |
Yifan Hong | 2ddbe4b | 2020-02-20 17:30:00 -0800 | [diff] [blame] | 110 | #pragma clang diagnostic pop |
| 111 | |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 112 | // Should not reach here. |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 113 | SHOULD_NOT_REACH() << "Could not handleUnary for " << op << " " << val; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 114 | return static_cast<T>(0xdeadbeef); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | template <class T> |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 118 | T handleBinaryCommon(T lval, const std::string& op, T rval) { |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 119 | COMPUTE_BINARY(+) |
| 120 | COMPUTE_BINARY(-) |
| 121 | COMPUTE_BINARY(*) |
| 122 | COMPUTE_BINARY(/) |
| 123 | COMPUTE_BINARY(%) |
| 124 | COMPUTE_BINARY(|) |
| 125 | COMPUTE_BINARY(^) |
| 126 | COMPUTE_BINARY(&) |
| 127 | // comparison operators: return 0 or 1 by nature. |
| 128 | COMPUTE_BINARY(==) |
| 129 | COMPUTE_BINARY(!=) |
| 130 | COMPUTE_BINARY(<) |
| 131 | COMPUTE_BINARY(>) |
| 132 | COMPUTE_BINARY(<=) |
| 133 | COMPUTE_BINARY(>=) |
| 134 | // Should not reach here. |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 135 | SHOULD_NOT_REACH() << "Could not handleBinaryCommon for " |
| 136 | << lval << " " << op << " " << rval; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 137 | return static_cast<T>(0xdeadbeef); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | template <class T> |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 141 | T handleShift(T lval, const std::string& op, int64_t rval) { |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 142 | // just cast rval to int64_t and it should fit. |
| 143 | COMPUTE_BINARY(>>) |
| 144 | COMPUTE_BINARY(<<) |
| 145 | // Should not reach here. |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 146 | SHOULD_NOT_REACH() << "Could not handleShift for " |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 147 | << lval << " " << op << " " << rval; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 148 | return static_cast<T>(0xdeadbeef); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 151 | bool handleLogical(bool lval, const std::string& op, bool rval) { |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 152 | COMPUTE_BINARY(||); |
| 153 | COMPUTE_BINARY(&&); |
| 154 | // Should not reach here. |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 155 | SHOULD_NOT_REACH() << "Could not handleLogical for " |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 156 | << lval << " " << op << " " << rval; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 157 | return false; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 160 | std::unique_ptr<ConstantExpression> ConstantExpression::Zero(ScalarType::Kind kind) { |
| 161 | return ValueOf(kind, 0); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 164 | std::unique_ptr<ConstantExpression> ConstantExpression::One(ScalarType::Kind kind) { |
| 165 | return ValueOf(kind, 1); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 168 | std::unique_ptr<ConstantExpression> ConstantExpression::ValueOf(ScalarType::Kind kind, |
| 169 | uint64_t value) { |
| 170 | return std::make_unique<LiteralConstantExpression>(kind, value); |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 173 | ConstantExpression::ConstantExpression(const std::string& expr) : mExpr(expr) {} |
| 174 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 175 | bool ConstantExpression::isEvaluated() const { |
| 176 | return mIsEvaluated; |
| 177 | } |
| 178 | |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 179 | LiteralConstantExpression::LiteralConstantExpression(ScalarType::Kind kind, uint64_t value, |
| 180 | const std::string& expr) |
| 181 | : ConstantExpression(expr) { |
Steven Moreland | d9d6dcb | 2017-09-20 15:55:39 -0700 | [diff] [blame] | 182 | CHECK(!expr.empty()); |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 183 | CHECK(isSupported(kind)); |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 184 | |
Steven Moreland | 7794369 | 2018-08-09 12:53:42 -0700 | [diff] [blame] | 185 | mTrivialDescription = std::to_string(value) == expr; |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 186 | mValueKind = kind; |
| 187 | mValue = value; |
| 188 | mIsEvaluated = true; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Steven Moreland | d9d6dcb | 2017-09-20 15:55:39 -0700 | [diff] [blame] | 191 | LiteralConstantExpression::LiteralConstantExpression(ScalarType::Kind kind, uint64_t value) |
| 192 | : LiteralConstantExpression(kind, value, std::to_string(value)) {} |
| 193 | |
| 194 | LiteralConstantExpression* LiteralConstantExpression::tryParse(const std::string& value) { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 195 | CHECK(!value.empty()); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 196 | |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 197 | bool isLong = false, isUnsigned = false; |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 198 | bool isHex = (value[0] == '0' && value.length() > 1 && (value[1] == 'x' || value[1] == 'X')); |
| 199 | |
| 200 | auto rbegin = value.rbegin(); |
| 201 | auto rend = value.rend(); |
| 202 | for (; rbegin != rend && (*rbegin == 'u' || *rbegin == 'U' || *rbegin == 'l' || *rbegin == 'L'); |
| 203 | ++rbegin) { |
| 204 | isUnsigned |= (*rbegin == 'u' || *rbegin == 'U'); |
| 205 | isLong |= (*rbegin == 'l' || *rbegin == 'L'); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 206 | } |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 207 | std::string newVal(value.begin(), rbegin.base()); |
| 208 | CHECK(!newVal.empty()); |
Steven Moreland | d9d6dcb | 2017-09-20 15:55:39 -0700 | [diff] [blame] | 209 | |
| 210 | uint64_t rawValue = 0; |
| 211 | |
| 212 | bool parseOK = base::ParseUint(newVal, &rawValue); |
| 213 | if (!parseOK) { |
| 214 | return nullptr; |
| 215 | } |
| 216 | |
| 217 | ScalarType::Kind kind; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 218 | |
| 219 | // guess literal type. |
| 220 | if(isLong) { |
| 221 | if(isUnsigned) // ul |
Steven Moreland | d9d6dcb | 2017-09-20 15:55:39 -0700 | [diff] [blame] | 222 | kind = SK(UINT64); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 223 | else // l |
Steven Moreland | d9d6dcb | 2017-09-20 15:55:39 -0700 | [diff] [blame] | 224 | kind = SK(INT64); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 225 | } else { // no l suffix |
| 226 | if(isUnsigned) { // u |
Steven Moreland | d9d6dcb | 2017-09-20 15:55:39 -0700 | [diff] [blame] | 227 | if(rawValue <= UINT32_MAX) |
| 228 | kind = SK(UINT32); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 229 | else |
Steven Moreland | d9d6dcb | 2017-09-20 15:55:39 -0700 | [diff] [blame] | 230 | kind = SK(UINT64); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 231 | } else { // no suffix |
| 232 | if(isHex) { |
Steven Moreland | d9d6dcb | 2017-09-20 15:55:39 -0700 | [diff] [blame] | 233 | if(rawValue <= INT32_MAX) // rawValue always >= 0 |
| 234 | kind = SK(INT32); |
| 235 | else if(rawValue <= UINT32_MAX) |
| 236 | kind = SK(UINT32); |
| 237 | else if(rawValue <= INT64_MAX) // rawValue always >= 0 |
| 238 | kind = SK(INT64); |
| 239 | else if(rawValue <= UINT64_MAX) |
| 240 | kind = SK(UINT64); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 241 | else |
Steven Moreland | d9d6dcb | 2017-09-20 15:55:39 -0700 | [diff] [blame] | 242 | return nullptr; |
| 243 | } else { |
| 244 | if(rawValue <= INT32_MAX) // rawValue always >= 0 |
| 245 | kind = SK(INT32); |
| 246 | else |
| 247 | kind = SK(INT64); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |
Steven Moreland | d9d6dcb | 2017-09-20 15:55:39 -0700 | [diff] [blame] | 251 | |
| 252 | return new LiteralConstantExpression(kind, rawValue, value); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 255 | void LiteralConstantExpression::evaluate() { |
| 256 | // Evaluated in constructor |
| 257 | CHECK(isEvaluated()); |
| 258 | } |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 259 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 260 | void UnaryConstantExpression::evaluate() { |
| 261 | if (isEvaluated()) return; |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 262 | CHECK(mUnary->isEvaluated()); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 263 | mIsEvaluated = true; |
| 264 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 265 | mValueKind = mUnary->mValueKind; |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 266 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 267 | #define CASE_UNARY(__type__) \ |
| 268 | mValue = handleUnary(mOp, static_cast<__type__>(mUnary->mValue)); \ |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 269 | return; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 270 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 271 | SWITCH_KIND(mValueKind, CASE_UNARY, SHOULD_NOT_REACH(); return;) |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 272 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 273 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 274 | void BinaryConstantExpression::evaluate() { |
| 275 | if (isEvaluated()) return; |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 276 | CHECK(mLval->isEvaluated()); |
| 277 | CHECK(mRval->isEvaluated()); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 278 | mIsEvaluated = true; |
| 279 | |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 280 | bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP; |
| 281 | |
| 282 | // CASE 1: + - * / % | ^ & < > <= >= == != |
| 283 | if(isArithmeticOrBitflip || OP_IS_BIN_COMP) { |
| 284 | // promoted kind for both operands. |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 285 | ScalarType::Kind promoted = usualArithmeticConversion(integralPromotion(mLval->mValueKind), |
| 286 | integralPromotion(mRval->mValueKind)); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 287 | // result kind. |
| 288 | mValueKind = isArithmeticOrBitflip |
| 289 | ? promoted // arithmetic or bitflip operators generates promoted type |
| 290 | : SK(BOOL); // comparison operators generates bool |
| 291 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 292 | #define CASE_BINARY_COMMON(__type__) \ |
| 293 | mValue = handleBinaryCommon(static_cast<__type__>(mLval->mValue), mOp, \ |
| 294 | static_cast<__type__>(mRval->mValue)); \ |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 295 | return; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 296 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 297 | SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); return;) |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // CASE 2: << >> |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 301 | std::string newOp = mOp; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 302 | if(OP_IS_BIN_SHIFT) { |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 303 | mValueKind = integralPromotion(mLval->mValueKind); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 304 | // instead of promoting rval, simply casting it to int64 should also be good. |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 305 | int64_t numBits = mRval->cast<int64_t>(); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 306 | if(numBits < 0) { |
| 307 | // shifting with negative number of bits is undefined in C. In HIDL it |
| 308 | // is defined as shifting into the other direction. |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 309 | newOp = OPEQ("<<") ? std::string(">>") : std::string("<<"); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 310 | numBits = -numBits; |
| 311 | } |
| 312 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 313 | #define CASE_SHIFT(__type__) \ |
| 314 | mValue = handleShift(static_cast<__type__>(mLval->mValue), newOp, numBits); \ |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 315 | return; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 316 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 317 | SWITCH_KIND(mValueKind, CASE_SHIFT, SHOULD_NOT_REACH(); return;) |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 318 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 319 | |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 320 | // CASE 3: && || |
| 321 | if(OP_IS_BIN_LOGICAL) { |
| 322 | mValueKind = SK(BOOL); |
| 323 | // easy; everything is bool. |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 324 | mValue = handleLogical(mLval->mValue, mOp, mRval->mValue); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 325 | return; |
| 326 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 327 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 328 | SHOULD_NOT_REACH(); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 331 | void TernaryConstantExpression::evaluate() { |
| 332 | if (isEvaluated()) return; |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 333 | CHECK(mCond->isEvaluated()); |
| 334 | CHECK(mTrueVal->isEvaluated()); |
| 335 | CHECK(mFalseVal->isEvaluated()); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 336 | mIsEvaluated = true; |
| 337 | |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 338 | // note: for ?:, unlike arithmetic ops, integral promotion is not processed. |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 339 | mValueKind = usualArithmeticConversion(mTrueVal->mValueKind, mFalseVal->mValueKind); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 340 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 341 | #define CASE_TERNARY(__type__) \ |
| 342 | mValue = mCond->mValue ? (static_cast<__type__>(mTrueVal->mValue)) \ |
| 343 | : (static_cast<__type__>(mFalseVal->mValue)); \ |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 344 | return; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 345 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 346 | SWITCH_KIND(mValueKind, CASE_TERNARY, SHOULD_NOT_REACH(); return;) |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 349 | void ReferenceConstantExpression::evaluate() { |
| 350 | if (isEvaluated()) return; |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 351 | CHECK(mReference->constExpr() != nullptr); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 352 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 353 | ConstantExpression* expr = mReference->constExpr(); |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 354 | CHECK(expr->isEvaluated()); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 355 | |
| 356 | mValueKind = expr->mValueKind; |
| 357 | mValue = expr->mValue; |
| 358 | mIsEvaluated = true; |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 359 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 360 | |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 361 | status_t AttributeConstantExpression::validate() const { |
| 362 | if (mTag == "len") { |
| 363 | if (!mReference->isEnum()) { |
| 364 | std::cerr << "ERROR: " << mExpr << " refers to " << mReference->typeName() |
| 365 | << " but should refer to an enum." << std::endl; |
| 366 | return UNKNOWN_ERROR; |
| 367 | } |
| 368 | } else { |
| 369 | std::cerr << "ERROR: " << mExpr << " is not a supported tag" << std::endl; |
| 370 | return UNKNOWN_ERROR; |
| 371 | } |
| 372 | |
| 373 | return OK; |
| 374 | } |
| 375 | |
| 376 | void AttributeConstantExpression::evaluate() { |
| 377 | if (isEvaluated()) return; |
| 378 | |
| 379 | CHECK(mTag == "len"); |
| 380 | CHECK(mReference->isEnum()); |
| 381 | |
| 382 | EnumType* enumType = static_cast<EnumType*>(mReference.get()); |
| 383 | mValue = enumType->numValueNames(); |
| 384 | |
| 385 | if (mValue <= INT32_MAX) |
| 386 | mValueKind = SK(INT32); |
| 387 | else |
| 388 | mValueKind = SK(INT64); |
| 389 | |
| 390 | mIsEvaluated = true; |
| 391 | } |
| 392 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 393 | std::unique_ptr<ConstantExpression> ConstantExpression::addOne(ScalarType::Kind baseKind) { |
| 394 | auto ret = std::make_unique<BinaryConstantExpression>( |
| 395 | this, "+", ConstantExpression::One(baseKind).release()); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 396 | return ret; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 399 | std::string ConstantExpression::value() const { |
Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 400 | return value(mValueKind); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Yifan Hong | c07b202 | 2016-11-08 12:44:24 -0800 | [diff] [blame] | 403 | std::string ConstantExpression::value(ScalarType::Kind castKind) const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 404 | CHECK(isEvaluated()); |
Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 405 | return rawValue(castKind) + descriptionSuffix(); |
Yifan Hong | c07b202 | 2016-11-08 12:44:24 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 408 | std::string ConstantExpression::cppValue() const { |
| 409 | return cppValue(mValueKind); |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 412 | std::string ConstantExpression::cppValue(ScalarType::Kind castKind) const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 413 | CHECK(isEvaluated()); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 414 | std::string literal(rawValue(castKind)); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 415 | // this is a hack to translate |
| 416 | // enum x : int64_t { y = 1l << 63 }; |
| 417 | // into |
| 418 | // enum class x : int64_t { y = (int64_t)-9223372036854775808ull }; |
| 419 | // by adding the explicit cast. |
| 420 | // Because 9223372036854775808 is uint64_t, and |
| 421 | // -(uint64_t)9223372036854775808 == 9223372036854775808 could not |
| 422 | // be narrowed to int64_t. |
| 423 | if(castKind == SK(INT64) && (int64_t)mValue == INT64_MIN) { |
Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 424 | literal = "static_cast<" + |
| 425 | ScalarType(SK(INT64), nullptr /* parent */).getCppStackType() // "int64_t" |
| 426 | + ">(" + literal + "ull)"; |
| 427 | } else { |
| 428 | // add suffix if necessary. |
| 429 | if (castKind == SK(UINT32) || castKind == SK(UINT64)) literal += "u"; |
| 430 | if (castKind == SK(UINT64) || castKind == SK(INT64)) literal += "ll"; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 431 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 432 | |
Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 433 | return literal + descriptionSuffix(); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 436 | std::string ConstantExpression::javaValue() const { |
| 437 | return javaValue(mValueKind); |
Yifan Hong | 19ca75a | 2016-08-31 10:20:03 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 440 | std::string ConstantExpression::javaValue(ScalarType::Kind castKind) const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 441 | CHECK(isEvaluated()); |
Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 442 | std::string literal; |
| 443 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 444 | switch(castKind) { |
Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 445 | case SK(UINT64): |
| 446 | literal = rawValue(SK(INT64)) + "L"; |
| 447 | break; |
| 448 | case SK(INT64): |
| 449 | literal = rawValue(SK(INT64)) + "L"; |
| 450 | break; |
| 451 | case SK(UINT32): |
| 452 | literal = rawValue(SK(INT32)); |
| 453 | break; |
| 454 | case SK(UINT16): |
| 455 | literal = rawValue(SK(INT16)); |
| 456 | break; |
| 457 | case SK(UINT8): |
| 458 | literal = rawValue(SK(INT8)); |
| 459 | break; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 460 | case SK(BOOL) : |
Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 461 | literal = this->cast<bool>() ? "true" : "false"; |
| 462 | break; |
| 463 | default: |
| 464 | literal = rawValue(castKind); |
| 465 | break; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 466 | } |
Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 467 | |
| 468 | return literal + descriptionSuffix(); |
| 469 | } |
| 470 | |
| 471 | const std::string& ConstantExpression::expression() const { |
Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 472 | return mExpr; |
| 473 | } |
| 474 | |
| 475 | std::string ConstantExpression::rawValue() const { |
| 476 | return rawValue(mValueKind); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | std::string ConstantExpression::rawValue(ScalarType::Kind castKind) const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 480 | CHECK(isEvaluated()); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 481 | |
Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 482 | #define CASE_STR(__type__) return std::to_string(this->cast<__type__>()); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 483 | |
Yi Kong | d7f8ab3 | 2018-07-24 11:27:02 -0700 | [diff] [blame] | 484 | SWITCH_KIND(castKind, CASE_STR, SHOULD_NOT_REACH(); return nullptr; ); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Yifan Hong | 19ca75a | 2016-08-31 10:20:03 -0700 | [diff] [blame] | 487 | template<typename T> |
| 488 | T ConstantExpression::cast() const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 489 | CHECK(isEvaluated()); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 490 | |
Yifan Hong | 19ca75a | 2016-08-31 10:20:03 -0700 | [diff] [blame] | 491 | #define CASE_CAST_T(__type__) return static_cast<T>(static_cast<__type__>(mValue)); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 492 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 493 | SWITCH_KIND(mValueKind, CASE_CAST_T, SHOULD_NOT_REACH(); return 0; ); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 496 | std::string ConstantExpression::descriptionSuffix() const { |
| 497 | CHECK(isEvaluated()); |
| 498 | |
| 499 | if (!mTrivialDescription) { |
| 500 | CHECK(!mExpr.empty()); |
| 501 | |
| 502 | return " /* " + mExpr + " */"; |
| 503 | } |
| 504 | return ""; |
| 505 | } |
| 506 | |
Yifan Hong | e77ca13 | 2016-09-27 10:49:05 -0700 | [diff] [blame] | 507 | size_t ConstantExpression::castSizeT() const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 508 | CHECK(isEvaluated()); |
Yifan Hong | e77ca13 | 2016-09-27 10:49:05 -0700 | [diff] [blame] | 509 | return this->cast<size_t>(); |
| 510 | } |
| 511 | |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 512 | bool ConstantExpression::isReferenceConstantExpression() const { |
| 513 | return false; |
| 514 | } |
| 515 | |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 516 | status_t ConstantExpression::validate() const { |
| 517 | return OK; |
| 518 | } |
| 519 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 520 | std::vector<ConstantExpression*> ConstantExpression::getConstantExpressions() { |
| 521 | const auto& constRet = static_cast<const ConstantExpression*>(this)->getConstantExpressions(); |
| 522 | std::vector<ConstantExpression*> ret(constRet.size()); |
| 523 | std::transform(constRet.begin(), constRet.end(), ret.begin(), |
| 524 | [](const auto* ce) { return const_cast<ConstantExpression*>(ce); }); |
| 525 | return ret; |
| 526 | } |
| 527 | |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 528 | std::vector<Reference<LocalIdentifier>*> ConstantExpression::getReferences() { |
| 529 | const auto& constRet = static_cast<const ConstantExpression*>(this)->getReferences(); |
| 530 | std::vector<Reference<LocalIdentifier>*> ret(constRet.size()); |
| 531 | std::transform(constRet.begin(), constRet.end(), ret.begin(), |
| 532 | [](const auto* ce) { return const_cast<Reference<LocalIdentifier>*>(ce); }); |
| 533 | return ret; |
| 534 | } |
| 535 | |
| 536 | std::vector<const Reference<LocalIdentifier>*> ConstantExpression::getReferences() const { |
| 537 | return {}; |
| 538 | } |
| 539 | |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 540 | std::vector<Reference<Type>*> ConstantExpression::getTypeReferences() { |
| 541 | const auto& constRet = static_cast<const ConstantExpression*>(this)->getTypeReferences(); |
| 542 | std::vector<Reference<Type>*> ret(constRet.size()); |
| 543 | std::transform(constRet.begin(), constRet.end(), ret.begin(), |
| 544 | [](const auto* ce) { return const_cast<Reference<Type>*>(ce); }); |
| 545 | return ret; |
| 546 | } |
| 547 | |
| 548 | std::vector<const Reference<Type>*> ConstantExpression::getTypeReferences() const { |
| 549 | return {}; |
| 550 | } |
| 551 | |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 552 | status_t ConstantExpression::recursivePass(const std::function<status_t(ConstantExpression*)>& func, |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 553 | std::unordered_set<const ConstantExpression*>* visited, |
| 554 | bool processBeforeDependencies) { |
Timur Iskhakov | 35930c4 | 2017-08-28 18:49:54 -0700 | [diff] [blame] | 555 | if (mIsPostParseCompleted) return OK; |
| 556 | |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 557 | if (visited->find(this) != visited->end()) return OK; |
| 558 | visited->insert(this); |
| 559 | |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 560 | if (processBeforeDependencies) { |
| 561 | status_t err = func(this); |
| 562 | if (err != OK) return err; |
| 563 | } |
| 564 | |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 565 | for (auto* nextCE : getConstantExpressions()) { |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 566 | status_t err = nextCE->recursivePass(func, visited, processBeforeDependencies); |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 567 | if (err != OK) return err; |
| 568 | } |
| 569 | |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 570 | for (auto* nextRef : getReferences()) { |
| 571 | auto* nextCE = nextRef->shallowGet()->constExpr(); |
| 572 | CHECK(nextCE != nullptr) << "Local identifier is not a constant expression"; |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 573 | status_t err = nextCE->recursivePass(func, visited, processBeforeDependencies); |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 574 | if (err != OK) return err; |
| 575 | } |
| 576 | |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 577 | if (!processBeforeDependencies) { |
| 578 | status_t err = func(this); |
| 579 | if (err != OK) return err; |
| 580 | } |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 581 | |
| 582 | return OK; |
| 583 | } |
| 584 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 585 | status_t ConstantExpression::recursivePass( |
| 586 | const std::function<status_t(const ConstantExpression*)>& func, |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 587 | std::unordered_set<const ConstantExpression*>* visited, bool processBeforeDependencies) const { |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 588 | if (mIsPostParseCompleted) return OK; |
| 589 | |
| 590 | if (visited->find(this) != visited->end()) return OK; |
| 591 | visited->insert(this); |
| 592 | |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 593 | if (processBeforeDependencies) { |
| 594 | status_t err = func(this); |
| 595 | if (err != OK) return err; |
| 596 | } |
| 597 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 598 | for (const auto* nextCE : getConstantExpressions()) { |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 599 | status_t err = nextCE->recursivePass(func, visited, processBeforeDependencies); |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 600 | if (err != OK) return err; |
| 601 | } |
| 602 | |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 603 | for (const auto* nextRef : getReferences()) { |
| 604 | const auto* nextCE = nextRef->shallowGet()->constExpr(); |
| 605 | CHECK(nextCE != nullptr) << "Local identifier is not a constant expression"; |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 606 | status_t err = nextCE->recursivePass(func, visited, processBeforeDependencies); |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 607 | if (err != OK) return err; |
| 608 | } |
| 609 | |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 610 | if (!processBeforeDependencies) { |
| 611 | status_t err = func(this); |
| 612 | if (err != OK) return err; |
| 613 | } |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 614 | |
| 615 | return OK; |
| 616 | } |
| 617 | |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 618 | ConstantExpression::CheckAcyclicStatus::CheckAcyclicStatus( |
| 619 | status_t status, const ConstantExpression* cycleEnd, |
| 620 | const ReferenceConstantExpression* lastReference) |
| 621 | : status(status), cycleEnd(cycleEnd), lastReference(lastReference) { |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 622 | CHECK(cycleEnd == nullptr || status != OK); |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 623 | CHECK((cycleEnd == nullptr) == (lastReference == nullptr)); |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | ConstantExpression::CheckAcyclicStatus ConstantExpression::checkAcyclic( |
| 627 | std::unordered_set<const ConstantExpression*>* visited, |
| 628 | std::unordered_set<const ConstantExpression*>* stack) const { |
| 629 | if (stack->find(this) != stack->end()) { |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 630 | CHECK(isReferenceConstantExpression()) |
| 631 | << "Only reference constant expression could be the cycle end"; |
| 632 | |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 633 | std::cerr << "ERROR: Cyclic declaration:\n"; |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 634 | return CheckAcyclicStatus(UNKNOWN_ERROR, this, |
| 635 | static_cast<const ReferenceConstantExpression*>(this)); |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | if (visited->find(this) != visited->end()) return CheckAcyclicStatus(OK); |
| 639 | visited->insert(this); |
| 640 | stack->insert(this); |
| 641 | |
| 642 | for (const auto* nextCE : getConstantExpressions()) { |
| 643 | auto err = nextCE->checkAcyclic(visited, stack); |
| 644 | if (err.status != OK) { |
| 645 | return err; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | for (const auto* nextRef : getReferences()) { |
| 650 | const auto* nextCE = nextRef->shallowGet()->constExpr(); |
| 651 | CHECK(nextCE != nullptr) << "Local identifier is not a constant expression"; |
| 652 | auto err = nextCE->checkAcyclic(visited, stack); |
| 653 | |
| 654 | if (err.status != OK) { |
| 655 | if (err.cycleEnd == nullptr) return err; |
| 656 | |
| 657 | // Only ReferenceConstantExpression has references, |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 658 | CHECK(isReferenceConstantExpression()) |
| 659 | << "Only reference constant expression could have refereneces"; |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 660 | |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 661 | // mExpr is defined explicitly before evaluation |
| 662 | std::cerr << " '" << err.lastReference->mExpr << "' in '" << mExpr << "' at " |
| 663 | << nextRef->location() << "\n"; |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 664 | |
| 665 | if (err.cycleEnd == this) { |
| 666 | return CheckAcyclicStatus(err.status); |
| 667 | } |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 668 | return CheckAcyclicStatus(err.status, err.cycleEnd, |
| 669 | static_cast<const ReferenceConstantExpression*>(this)); |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
| 673 | CHECK(stack->find(this) != stack->end()); |
| 674 | stack->erase(this); |
| 675 | return CheckAcyclicStatus(OK); |
| 676 | } |
| 677 | |
Timur Iskhakov | 35930c4 | 2017-08-28 18:49:54 -0700 | [diff] [blame] | 678 | void ConstantExpression::setPostParseCompleted() { |
| 679 | CHECK(!mIsPostParseCompleted); |
| 680 | mIsPostParseCompleted = true; |
| 681 | } |
| 682 | |
Neel Mehta | 1ca3e78 | 2019-07-18 15:16:22 -0700 | [diff] [blame] | 683 | void ConstantExpression::surroundWithParens() { |
| 684 | mExpr = "(" + mExpr + ")"; |
| 685 | } |
| 686 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 687 | std::vector<const ConstantExpression*> LiteralConstantExpression::getConstantExpressions() const { |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 688 | return {}; |
| 689 | } |
| 690 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 691 | UnaryConstantExpression::UnaryConstantExpression(const std::string& op, ConstantExpression* value) |
Neel Mehta | 1ca3e78 | 2019-07-18 15:16:22 -0700 | [diff] [blame] | 692 | : ConstantExpression(op + value->mExpr), mUnary(value), mOp(op) {} |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 693 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 694 | std::vector<const ConstantExpression*> UnaryConstantExpression::getConstantExpressions() const { |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 695 | return {mUnary}; |
| 696 | } |
| 697 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 698 | BinaryConstantExpression::BinaryConstantExpression(ConstantExpression* lval, const std::string& op, |
| 699 | ConstantExpression* rval) |
Neel Mehta | 1ca3e78 | 2019-07-18 15:16:22 -0700 | [diff] [blame] | 700 | : ConstantExpression(lval->mExpr + " " + op + " " + rval->mExpr), |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 701 | mLval(lval), |
| 702 | mRval(rval), |
| 703 | mOp(op) {} |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 704 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 705 | std::vector<const ConstantExpression*> BinaryConstantExpression::getConstantExpressions() const { |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 706 | return {mLval, mRval}; |
| 707 | } |
| 708 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 709 | TernaryConstantExpression::TernaryConstantExpression(ConstantExpression* cond, |
| 710 | ConstantExpression* trueVal, |
| 711 | ConstantExpression* falseVal) |
Neel Mehta | 1ca3e78 | 2019-07-18 15:16:22 -0700 | [diff] [blame] | 712 | : ConstantExpression(cond->mExpr + "?" + trueVal->mExpr + ":" + falseVal->mExpr), |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 713 | mCond(cond), |
| 714 | mTrueVal(trueVal), |
| 715 | mFalseVal(falseVal) {} |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 716 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 717 | std::vector<const ConstantExpression*> TernaryConstantExpression::getConstantExpressions() const { |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 718 | return {mCond, mTrueVal, mFalseVal}; |
| 719 | } |
| 720 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 721 | ReferenceConstantExpression::ReferenceConstantExpression(const Reference<LocalIdentifier>& value, |
| 722 | const std::string& expr) |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 723 | : ConstantExpression(expr), mReference(value) { |
Timur Iskhakov | 505e561 | 2017-08-27 18:26:48 -0700 | [diff] [blame] | 724 | mTrivialDescription = mExpr.empty(); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 727 | bool ReferenceConstantExpression::isReferenceConstantExpression() const { |
| 728 | return true; |
| 729 | } |
| 730 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 731 | std::vector<const ConstantExpression*> ReferenceConstantExpression::getConstantExpressions() const { |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 732 | // Returns reference instead |
| 733 | return {}; |
| 734 | } |
| 735 | |
| 736 | std::vector<const Reference<LocalIdentifier>*> ReferenceConstantExpression::getReferences() const { |
| 737 | return {&mReference}; |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 740 | AttributeConstantExpression::AttributeConstantExpression(const Reference<Type>& value, |
| 741 | const std::string& fqname, |
| 742 | const std::string& tag) |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 743 | : ConstantExpression(fqname + "#" + tag), mReference(value), mTag(tag) {} |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 744 | |
| 745 | std::vector<const ConstantExpression*> AttributeConstantExpression::getConstantExpressions() const { |
| 746 | // Returns reference instead |
| 747 | return {}; |
| 748 | } |
| 749 | |
| 750 | std::vector<const Reference<Type>*> AttributeConstantExpression::getTypeReferences() const { |
| 751 | return {&mReference}; |
| 752 | } |
| 753 | |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 754 | /* |
| 755 | |
| 756 | Evaluating expressions in HIDL language |
| 757 | |
| 758 | The following rules are mostly like that in: |
| 759 | http://en.cppreference.com/w/cpp/language/operator_arithmetic |
| 760 | http://en.cppreference.com/w/cpp/language/operator_logical |
| 761 | http://en.cppreference.com/w/cpp/language/operator_comparison |
| 762 | http://en.cppreference.com/w/cpp/language/operator_other |
| 763 | |
| 764 | The type of literal is the first type which the value |
| 765 | can fit from the list of types depending on the suffix and bases. |
| 766 | |
| 767 | suffix decimal bases hexadecimal bases |
| 768 | no suffix int32_t int32_t |
| 769 | int64_t uint32_t |
| 770 | int64_t |
| 771 | uint64_t |
| 772 | |
| 773 | u/U uint32_t (same as left) |
| 774 | uint64_t |
| 775 | |
| 776 | l/L int64_t int64_t |
| 777 | |
| 778 | ul/UL/uL/Ul uint64_t uint64_t |
| 779 | |
| 780 | |
| 781 | Note: There are no negative integer literals. |
| 782 | -1 is the unary minus applied to 1. |
| 783 | |
| 784 | Unary arithmetic and bitwise operators (~ + -): |
| 785 | don't change the type of the argument. |
| 786 | (so -1u = -(1u) has type uint32_t) |
| 787 | |
| 788 | Binary arithmetic and bitwise operators (except shifts) (+ - * / % & | ^): |
| 789 | 1. Integral promotion is first applied on both sides. |
| 790 | 2. If both operands have the same type, no promotion is necessary. |
| 791 | 3. Usual arithmetic conversions. |
| 792 | |
| 793 | Integral promotion: if an operand is of a type with less than 32 bits, |
| 794 | (including bool), it is promoted to int32_t. |
| 795 | |
| 796 | Usual arithmetic conversions: |
| 797 | 1. If operands are both signed or both unsigned, lesser conversion rank is |
| 798 | converted to greater conversion rank. |
| 799 | 2. Otherwise, if unsigned's rank >= signed's rank, -> unsigned's type |
| 800 | 3. Otherwise, if signed's type can hold all values in unsigned's type, |
| 801 | -> signed's type |
| 802 | 4. Otherwise, both converted to the unsigned counterpart of the signed operand's |
| 803 | type. |
| 804 | rank: bool < int8_t < int16_t < int32_t < int64_t |
| 805 | |
| 806 | |
| 807 | Shift operators (<< >>): |
| 808 | 1. Integral promotion is applied on both sides. |
| 809 | 2. For unsigned a, a << b discards bits that shifts out. |
| 810 | For signed non-negative a, a << b is legal if no bits shifts out, otherwise error. |
| 811 | For signed negative a, a << b gives error. |
| 812 | 3. For unsigned and signed non-negative a, a >> b discards bits that shifts out. |
| 813 | For signed negative a, a >> b discards bits that shifts out, and the signed |
| 814 | bit gets extended. ("arithmetic right shift") |
| 815 | 4. Shifting with negative number of bits is undefined. (Currently, the |
| 816 | parser will shift into the other direction. This behavior may change.) |
| 817 | 5. Shifting with number of bits exceeding the width of the type is undefined. |
| 818 | (Currently, 1 << 32 == 1. This behavior may change.) |
| 819 | |
| 820 | Logical operators (!, &&, ||): |
| 821 | 1. Convert first operand to bool. (true if non-zero, false otherwise) |
| 822 | 2. If short-circuited, return the result as type bool, value 1 or 0. |
| 823 | 3. Otherwise, convert second operand to bool, evaluate the result, and return |
| 824 | the result in the same fashion. |
| 825 | |
| 826 | Arithmetic comparison operators (< > <= >= == !=): |
| 827 | 1. Promote operands in the same way as binary arithmetic and bitwise operators. |
| 828 | (Integral promotion + Usual arithmetic conversions) |
| 829 | 2. Return type bool, value 0 or 1 the same way as logical operators. |
| 830 | |
| 831 | Ternary conditional operator (?:): |
| 832 | 1. Evaluate the conditional and evaluate the operands. |
| 833 | 2. Return type of expression is the type under usual arithmetic conversions on |
| 834 | the second and third operand. (No integral promotions necessary.) |
| 835 | |
| 836 | */ |
| 837 | |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 838 | } // namespace android |
| 839 | |