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