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 <stdio.h> |
| 20 | #include <string> |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 21 | #include <android-base/parseint.h> |
| 22 | #include <android-base/logging.h> |
| 23 | #include <sstream> |
| 24 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 25 | #include "EnumType.h" |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 26 | #include "Scope.h" // LocalIdentifier |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 27 | |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 28 | // The macros are really nasty here. Consider removing |
| 29 | // as many macros as possible. |
| 30 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 31 | #define OPEQ(__y__) (std::string(mOp) == std::string(__y__)) |
| 32 | #define COMPUTE_UNARY(__op__) if (op == std::string(#__op__)) return __op__ val; |
| 33 | #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] | 34 | #define OP_IS_BIN_ARITHMETIC (OPEQ("+") || OPEQ("-") || OPEQ("*") || OPEQ("/") || OPEQ("%")) |
| 35 | #define OP_IS_BIN_BITFLIP (OPEQ("|") || OPEQ("^") || OPEQ("&")) |
| 36 | #define OP_IS_BIN_COMP (OPEQ("<") || OPEQ(">") || OPEQ("<=") || OPEQ(">=") || OPEQ("==") || OPEQ("!=")) |
| 37 | #define OP_IS_BIN_SHIFT (OPEQ(">>") || OPEQ("<<")) |
| 38 | #define OP_IS_BIN_LOGICAL (OPEQ("||") || OPEQ("&&")) |
| 39 | #define SK(__x__) ScalarType::Kind::KIND_##__x__ |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 40 | #define SHOULD_NOT_REACH() CHECK(false) << __LINE__ << ": should not reach here: " |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 41 | |
Chih-Hung Hsieh | 306cc4b | 2017-08-02 14:59:25 -0700 | [diff] [blame] | 42 | // NOLINT to suppress missing parentheses warnings about __def__. |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 43 | #define SWITCH_KIND(__cond__, __action__, __def__) \ |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 44 | switch(__cond__) { \ |
| 45 | case SK(BOOL): __action__(bool) \ |
| 46 | case SK(UINT8): __action__(uint8_t) \ |
| 47 | case SK(INT8): __action__(int8_t) \ |
| 48 | case SK(UINT16): __action__(uint16_t) \ |
| 49 | case SK(INT16): __action__(int16_t) \ |
| 50 | case SK(UINT32): __action__(uint32_t) \ |
| 51 | case SK(INT32): __action__(int32_t) \ |
| 52 | case SK(UINT64): __action__(uint64_t) \ |
| 53 | case SK(INT64): __action__(int64_t) \ |
Chih-Hung Hsieh | 306cc4b | 2017-08-02 14:59:25 -0700 | [diff] [blame] | 54 | default: __def__ /* NOLINT */ \ |
| 55 | } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 56 | |
| 57 | namespace android { |
| 58 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 59 | static inline bool isSupported(ScalarType::Kind kind) { |
| 60 | return SK(BOOL) == kind || ScalarType(kind).isValidEnumStorageType(); |
| 61 | } |
| 62 | |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 63 | /* See docs at the end for details on integral promotion. */ |
| 64 | ScalarType::Kind integralPromotion(ScalarType::Kind in) { |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 65 | 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] | 66 | } |
| 67 | |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 68 | /* See docs at the end for details on usual arithmetic conversion. */ |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 69 | ScalarType::Kind usualArithmeticConversion(ScalarType::Kind lft, |
| 70 | ScalarType::Kind rgt) { |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 71 | CHECK(isSupported(lft) && isSupported(rgt)); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 72 | // Kinds in concern: bool, (u)int[8|16|32|64] |
| 73 | if(lft == rgt) return lft; // easy case |
| 74 | if(lft == SK(BOOL)) return rgt; |
| 75 | if(rgt == SK(BOOL)) return lft; |
| 76 | bool isLftSigned = (lft == SK(INT8)) || (lft == SK(INT16)) |
| 77 | || (lft == SK(INT32)) || (lft == SK(INT64)); |
| 78 | bool isRgtSigned = (rgt == SK(INT8)) || (rgt == SK(INT16)) |
| 79 | || (rgt == SK(INT32)) || (rgt == SK(INT64)); |
| 80 | if(isLftSigned == isRgtSigned) return lft < rgt ? rgt : lft; |
| 81 | ScalarType::Kind unsignedRank = isLftSigned ? rgt : lft; |
| 82 | ScalarType::Kind signedRank = isLftSigned ? lft : rgt; |
| 83 | if(unsignedRank >= signedRank) return unsignedRank; |
| 84 | if(signedRank > unsignedRank) return signedRank; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 85 | |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 86 | // Although there is such rule to return "the unsigned counterpart of |
| 87 | // the signed operand", it should not reach here in our HIDL grammar. |
| 88 | LOG(FATAL) << "Could not do usual arithmetic conversion for type " |
| 89 | << lft << "and" << rgt; |
| 90 | switch(signedRank) { |
| 91 | case SK(INT8): return SK(UINT8); |
| 92 | case SK(INT16): return SK(UINT16); |
| 93 | case SK(INT32): return SK(UINT32); |
| 94 | case SK(INT64): return SK(UINT64); |
| 95 | default: return SK(UINT64); |
| 96 | } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 97 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 98 | |
| 99 | template <class T> |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 100 | T handleUnary(const std::string& op, T val) { |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 101 | COMPUTE_UNARY(+) |
| 102 | COMPUTE_UNARY(-) |
| 103 | COMPUTE_UNARY(!) |
| 104 | COMPUTE_UNARY(~) |
| 105 | // Should not reach here. |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 106 | SHOULD_NOT_REACH() << "Could not handleUnary for " << op << " " << val; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 107 | return static_cast<T>(0xdeadbeef); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | template <class T> |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 111 | T handleBinaryCommon(T lval, const std::string& op, T rval) { |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 112 | COMPUTE_BINARY(+) |
| 113 | COMPUTE_BINARY(-) |
| 114 | COMPUTE_BINARY(*) |
| 115 | COMPUTE_BINARY(/) |
| 116 | COMPUTE_BINARY(%) |
| 117 | COMPUTE_BINARY(|) |
| 118 | COMPUTE_BINARY(^) |
| 119 | COMPUTE_BINARY(&) |
| 120 | // comparison operators: return 0 or 1 by nature. |
| 121 | COMPUTE_BINARY(==) |
| 122 | COMPUTE_BINARY(!=) |
| 123 | COMPUTE_BINARY(<) |
| 124 | COMPUTE_BINARY(>) |
| 125 | COMPUTE_BINARY(<=) |
| 126 | COMPUTE_BINARY(>=) |
| 127 | // Should not reach here. |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 128 | SHOULD_NOT_REACH() << "Could not handleBinaryCommon for " |
| 129 | << lval << " " << op << " " << rval; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 130 | return static_cast<T>(0xdeadbeef); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | template <class T> |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 134 | T handleShift(T lval, const std::string& op, int64_t rval) { |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 135 | // just cast rval to int64_t and it should fit. |
| 136 | COMPUTE_BINARY(>>) |
| 137 | COMPUTE_BINARY(<<) |
| 138 | // Should not reach here. |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 139 | SHOULD_NOT_REACH() << "Could not handleShift for " |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 140 | << lval << " " << op << " " << rval; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 141 | return static_cast<T>(0xdeadbeef); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 144 | bool handleLogical(bool lval, const std::string& op, bool rval) { |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 145 | COMPUTE_BINARY(||); |
| 146 | COMPUTE_BINARY(&&); |
| 147 | // Should not reach here. |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 148 | SHOULD_NOT_REACH() << "Could not handleLogical for " |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 149 | << lval << " " << op << " " << rval; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 150 | return false; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 153 | std::unique_ptr<ConstantExpression> ConstantExpression::Zero(ScalarType::Kind kind) { |
| 154 | return ValueOf(kind, 0); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 157 | std::unique_ptr<ConstantExpression> ConstantExpression::One(ScalarType::Kind kind) { |
| 158 | return ValueOf(kind, 1); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 161 | std::unique_ptr<ConstantExpression> ConstantExpression::ValueOf(ScalarType::Kind kind, |
| 162 | uint64_t value) { |
| 163 | return std::make_unique<LiteralConstantExpression>(kind, value); |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 166 | bool ConstantExpression::isEvaluated() const { |
| 167 | return mIsEvaluated; |
| 168 | } |
| 169 | |
| 170 | LiteralConstantExpression::LiteralConstantExpression(ScalarType::Kind kind, uint64_t value) { |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 171 | CHECK(isSupported(kind)); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 172 | mTrivialDescription = true; |
| 173 | mExpr = std::to_string(value); |
| 174 | mValueKind = kind; |
| 175 | mValue = value; |
| 176 | mIsEvaluated = true; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 179 | LiteralConstantExpression::LiteralConstantExpression(const std::string& value) { |
| 180 | CHECK(!value.empty()); |
| 181 | mIsEvaluated = true; |
| 182 | mTrivialDescription = true; |
| 183 | mExpr = value; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 184 | |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 185 | bool isLong = false, isUnsigned = false; |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 186 | bool isHex = (value[0] == '0' && value.length() > 1 && (value[1] == 'x' || value[1] == 'X')); |
| 187 | |
| 188 | auto rbegin = value.rbegin(); |
| 189 | auto rend = value.rend(); |
| 190 | for (; rbegin != rend && (*rbegin == 'u' || *rbegin == 'U' || *rbegin == 'l' || *rbegin == 'L'); |
| 191 | ++rbegin) { |
| 192 | isUnsigned |= (*rbegin == 'u' || *rbegin == 'U'); |
| 193 | isLong |= (*rbegin == 'l' || *rbegin == 'L'); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 194 | } |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 195 | std::string newVal(value.begin(), rbegin.base()); |
| 196 | CHECK(!newVal.empty()); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 197 | bool parseOK = base::ParseUint(newVal, &mValue); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 198 | CHECK(parseOK) << "Could not parse as integer: " << value; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 199 | |
| 200 | // guess literal type. |
| 201 | if(isLong) { |
| 202 | if(isUnsigned) // ul |
| 203 | mValueKind = SK(UINT64); |
| 204 | else // l |
| 205 | mValueKind = SK(INT64); |
| 206 | } else { // no l suffix |
| 207 | if(isUnsigned) { // u |
| 208 | if(mValue <= UINT32_MAX) |
| 209 | mValueKind = SK(UINT32); |
| 210 | else |
| 211 | mValueKind = SK(UINT64); |
| 212 | } else { // no suffix |
| 213 | if(isHex) { |
| 214 | if(mValue <= INT32_MAX) // mValue always >= 0 |
| 215 | mValueKind = SK(INT32); |
| 216 | else if(mValue <= UINT32_MAX) |
| 217 | mValueKind = SK(UINT32); |
| 218 | else if(mValue <= INT64_MAX) // mValue always >= 0 |
| 219 | mValueKind = SK(INT64); |
| 220 | else if(mValue <= UINT64_MAX) |
| 221 | mValueKind = SK(UINT64); |
| 222 | } else { |
| 223 | if(mValue <= INT32_MAX) // mValue always >= 0 |
| 224 | mValueKind = SK(INT32); |
| 225 | else |
| 226 | mValueKind = SK(INT64); |
| 227 | } |
| 228 | } |
| 229 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 232 | void LiteralConstantExpression::evaluate() { |
| 233 | // Evaluated in constructor |
| 234 | CHECK(isEvaluated()); |
| 235 | } |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 236 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 237 | void UnaryConstantExpression::evaluate() { |
| 238 | if (isEvaluated()) return; |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 239 | mUnary->evaluate(); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 240 | mIsEvaluated = true; |
| 241 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 242 | mExpr = std::string("(") + mOp + mUnary->description() + ")"; |
| 243 | mValueKind = mUnary->mValueKind; |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 244 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 245 | #define CASE_UNARY(__type__) \ |
| 246 | mValue = handleUnary(mOp, static_cast<__type__>(mUnary->mValue)); \ |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 247 | return; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 248 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 249 | SWITCH_KIND(mValueKind, CASE_UNARY, SHOULD_NOT_REACH(); return;) |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 250 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 251 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 252 | void BinaryConstantExpression::evaluate() { |
| 253 | if (isEvaluated()) return; |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 254 | mLval->evaluate(); |
| 255 | mRval->evaluate(); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 256 | mIsEvaluated = true; |
| 257 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 258 | mExpr = std::string("(") + mLval->description() + " " + mOp + " " + mRval->description() + ")"; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 259 | |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 260 | bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP; |
| 261 | |
| 262 | // CASE 1: + - * / % | ^ & < > <= >= == != |
| 263 | if(isArithmeticOrBitflip || OP_IS_BIN_COMP) { |
| 264 | // promoted kind for both operands. |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 265 | ScalarType::Kind promoted = usualArithmeticConversion(integralPromotion(mLval->mValueKind), |
| 266 | integralPromotion(mRval->mValueKind)); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 267 | // result kind. |
| 268 | mValueKind = isArithmeticOrBitflip |
| 269 | ? promoted // arithmetic or bitflip operators generates promoted type |
| 270 | : SK(BOOL); // comparison operators generates bool |
| 271 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 272 | #define CASE_BINARY_COMMON(__type__) \ |
| 273 | mValue = handleBinaryCommon(static_cast<__type__>(mLval->mValue), mOp, \ |
| 274 | static_cast<__type__>(mRval->mValue)); \ |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 275 | return; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 276 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 277 | SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); return;) |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // CASE 2: << >> |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 281 | std::string newOp = mOp; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 282 | if(OP_IS_BIN_SHIFT) { |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 283 | mValueKind = integralPromotion(mLval->mValueKind); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 284 | // 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] | 285 | int64_t numBits = mRval->cast<int64_t>(); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 286 | if(numBits < 0) { |
| 287 | // shifting with negative number of bits is undefined in C. In HIDL it |
| 288 | // is defined as shifting into the other direction. |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 289 | newOp = OPEQ("<<") ? std::string(">>") : std::string("<<"); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 290 | numBits = -numBits; |
| 291 | } |
| 292 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 293 | #define CASE_SHIFT(__type__) \ |
| 294 | mValue = handleShift(static_cast<__type__>(mLval->mValue), newOp, numBits); \ |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 295 | return; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 296 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 297 | SWITCH_KIND(mValueKind, CASE_SHIFT, SHOULD_NOT_REACH(); return;) |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 298 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 299 | |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 300 | // CASE 3: && || |
| 301 | if(OP_IS_BIN_LOGICAL) { |
| 302 | mValueKind = SK(BOOL); |
| 303 | // easy; everything is bool. |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 304 | mValue = handleLogical(mLval->mValue, mOp, mRval->mValue); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 305 | return; |
| 306 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 307 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 308 | SHOULD_NOT_REACH(); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 311 | void TernaryConstantExpression::evaluate() { |
| 312 | if (isEvaluated()) return; |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 313 | mCond->evaluate(); |
| 314 | mTrueVal->evaluate(); |
| 315 | mFalseVal->evaluate(); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 316 | mIsEvaluated = true; |
| 317 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 318 | mExpr = std::string("(") + mCond->description() + "?" + mTrueVal->description() + ":" + |
| 319 | mFalseVal->description() + ")"; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 320 | |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 321 | // note: for ?:, unlike arithmetic ops, integral promotion is not necessary. |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 322 | mValueKind = usualArithmeticConversion(mTrueVal->mValueKind, mFalseVal->mValueKind); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 323 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 324 | #define CASE_TERNARY(__type__) \ |
| 325 | mValue = mCond->mValue ? (static_cast<__type__>(mTrueVal->mValue)) \ |
| 326 | : (static_cast<__type__>(mFalseVal->mValue)); \ |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 327 | return; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 328 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 329 | SWITCH_KIND(mValueKind, CASE_TERNARY, SHOULD_NOT_REACH(); return;) |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 332 | void ReferenceConstantExpression::evaluate() { |
| 333 | if (isEvaluated()) return; |
| 334 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 335 | ConstantExpression* expr = mReference->constExpr(); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 336 | CHECK(expr != nullptr); |
| 337 | expr->evaluate(); |
| 338 | |
| 339 | mValueKind = expr->mValueKind; |
| 340 | mValue = expr->mValue; |
| 341 | mIsEvaluated = true; |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 342 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 343 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 344 | std::unique_ptr<ConstantExpression> ConstantExpression::addOne(ScalarType::Kind baseKind) { |
| 345 | auto ret = std::make_unique<BinaryConstantExpression>( |
| 346 | this, "+", ConstantExpression::One(baseKind).release()); |
| 347 | ret->mTrivialDescription = true; |
| 348 | return ret; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 351 | const std::string& ConstantExpression::description() const { |
| 352 | CHECK(isEvaluated()); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 353 | return mExpr; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Yifan Hong | 5706a43 | 2016-11-02 09:44:18 -0700 | [diff] [blame] | 356 | bool ConstantExpression::descriptionIsTrivial() const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 357 | CHECK(isEvaluated()); |
Yifan Hong | 5706a43 | 2016-11-02 09:44:18 -0700 | [diff] [blame] | 358 | return mTrivialDescription; |
| 359 | } |
| 360 | |
Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 361 | std::string ConstantExpression::value() const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 362 | CHECK(isEvaluated()); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 363 | return rawValue(mValueKind); |
| 364 | } |
| 365 | |
Yifan Hong | c07b202 | 2016-11-08 12:44:24 -0800 | [diff] [blame] | 366 | std::string ConstantExpression::value(ScalarType::Kind castKind) const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 367 | CHECK(isEvaluated()); |
Yifan Hong | c07b202 | 2016-11-08 12:44:24 -0800 | [diff] [blame] | 368 | return rawValue(castKind); |
| 369 | } |
| 370 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 371 | std::string ConstantExpression::cppValue() const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 372 | CHECK(isEvaluated()); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 373 | return cppValue(mValueKind); |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 376 | std::string ConstantExpression::cppValue(ScalarType::Kind castKind) const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 377 | CHECK(isEvaluated()); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 378 | std::string literal(rawValue(castKind)); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 379 | // this is a hack to translate |
| 380 | // enum x : int64_t { y = 1l << 63 }; |
| 381 | // into |
| 382 | // enum class x : int64_t { y = (int64_t)-9223372036854775808ull }; |
| 383 | // by adding the explicit cast. |
| 384 | // Because 9223372036854775808 is uint64_t, and |
| 385 | // -(uint64_t)9223372036854775808 == 9223372036854775808 could not |
| 386 | // be narrowed to int64_t. |
| 387 | if(castKind == SK(INT64) && (int64_t)mValue == INT64_MIN) { |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 388 | return strdup(("static_cast<" |
| 389 | + ScalarType(SK(INT64)).getCppStackType() // "int64_t" |
| 390 | + ">(" + literal + "ull)").c_str()); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 391 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 392 | |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 393 | // add suffix if necessary. |
| 394 | if(castKind == SK(UINT32) || castKind == SK(UINT64)) literal += "u"; |
| 395 | if(castKind == SK(UINT64) || castKind == SK(INT64)) literal += "ll"; |
Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 396 | return literal; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 399 | std::string ConstantExpression::javaValue() const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 400 | CHECK(isEvaluated()); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 401 | return javaValue(mValueKind); |
Yifan Hong | 19ca75a | 2016-08-31 10:20:03 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 404 | std::string ConstantExpression::javaValue(ScalarType::Kind castKind) const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 405 | CHECK(isEvaluated()); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 406 | switch(castKind) { |
| 407 | case SK(UINT64): return rawValue(SK(INT64)) + "L"; |
| 408 | case SK(INT64): return rawValue(SK(INT64)) + "L"; |
| 409 | case SK(UINT32): return rawValue(SK(INT32)); |
| 410 | case SK(UINT16): return rawValue(SK(INT16)); |
| 411 | case SK(UINT8) : return rawValue(SK(INT8)); |
| 412 | case SK(BOOL) : |
| 413 | return this->cast<bool>() ? strdup("true") : strdup("false"); |
| 414 | default: break; |
| 415 | } |
| 416 | return rawValue(castKind); |
| 417 | } |
| 418 | |
| 419 | std::string ConstantExpression::rawValue(ScalarType::Kind castKind) const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 420 | CHECK(isEvaluated()); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 421 | |
Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 422 | #define CASE_STR(__type__) return std::to_string(this->cast<__type__>()); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 423 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 424 | SWITCH_KIND(castKind, CASE_STR, SHOULD_NOT_REACH(); return 0; ); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Yifan Hong | 19ca75a | 2016-08-31 10:20:03 -0700 | [diff] [blame] | 427 | template<typename T> |
| 428 | T ConstantExpression::cast() const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 429 | CHECK(isEvaluated()); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 430 | |
Yifan Hong | 19ca75a | 2016-08-31 10:20:03 -0700 | [diff] [blame] | 431 | #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] | 432 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 433 | SWITCH_KIND(mValueKind, CASE_CAST_T, SHOULD_NOT_REACH(); return 0; ); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Yifan Hong | e77ca13 | 2016-09-27 10:49:05 -0700 | [diff] [blame] | 436 | size_t ConstantExpression::castSizeT() const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 437 | CHECK(isEvaluated()); |
Yifan Hong | e77ca13 | 2016-09-27 10:49:05 -0700 | [diff] [blame] | 438 | return this->cast<size_t>(); |
| 439 | } |
| 440 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 441 | UnaryConstantExpression::UnaryConstantExpression(const std::string& op, ConstantExpression* value) |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 442 | : mUnary(value), mOp(op) {} |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 443 | |
| 444 | BinaryConstantExpression::BinaryConstantExpression(ConstantExpression* lval, const std::string& op, |
| 445 | ConstantExpression* rval) |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 446 | : mLval(lval), mRval(rval), mOp(op) {} |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 447 | |
| 448 | TernaryConstantExpression::TernaryConstantExpression(ConstantExpression* cond, |
| 449 | ConstantExpression* trueVal, |
| 450 | ConstantExpression* falseVal) |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 451 | : mCond(cond), mTrueVal(trueVal), mFalseVal(falseVal) {} |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 452 | |
| 453 | ReferenceConstantExpression::ReferenceConstantExpression(const Reference<LocalIdentifier>& value, |
| 454 | const std::string& expr) |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 455 | : mReference(value) { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 456 | mExpr = expr; |
| 457 | } |
| 458 | |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 459 | /* |
| 460 | |
| 461 | Evaluating expressions in HIDL language |
| 462 | |
| 463 | The following rules are mostly like that in: |
| 464 | http://en.cppreference.com/w/cpp/language/operator_arithmetic |
| 465 | http://en.cppreference.com/w/cpp/language/operator_logical |
| 466 | http://en.cppreference.com/w/cpp/language/operator_comparison |
| 467 | http://en.cppreference.com/w/cpp/language/operator_other |
| 468 | |
| 469 | The type of literal is the first type which the value |
| 470 | can fit from the list of types depending on the suffix and bases. |
| 471 | |
| 472 | suffix decimal bases hexadecimal bases |
| 473 | no suffix int32_t int32_t |
| 474 | int64_t uint32_t |
| 475 | int64_t |
| 476 | uint64_t |
| 477 | |
| 478 | u/U uint32_t (same as left) |
| 479 | uint64_t |
| 480 | |
| 481 | l/L int64_t int64_t |
| 482 | |
| 483 | ul/UL/uL/Ul uint64_t uint64_t |
| 484 | |
| 485 | |
| 486 | Note: There are no negative integer literals. |
| 487 | -1 is the unary minus applied to 1. |
| 488 | |
| 489 | Unary arithmetic and bitwise operators (~ + -): |
| 490 | don't change the type of the argument. |
| 491 | (so -1u = -(1u) has type uint32_t) |
| 492 | |
| 493 | Binary arithmetic and bitwise operators (except shifts) (+ - * / % & | ^): |
| 494 | 1. Integral promotion is first applied on both sides. |
| 495 | 2. If both operands have the same type, no promotion is necessary. |
| 496 | 3. Usual arithmetic conversions. |
| 497 | |
| 498 | Integral promotion: if an operand is of a type with less than 32 bits, |
| 499 | (including bool), it is promoted to int32_t. |
| 500 | |
| 501 | Usual arithmetic conversions: |
| 502 | 1. If operands are both signed or both unsigned, lesser conversion rank is |
| 503 | converted to greater conversion rank. |
| 504 | 2. Otherwise, if unsigned's rank >= signed's rank, -> unsigned's type |
| 505 | 3. Otherwise, if signed's type can hold all values in unsigned's type, |
| 506 | -> signed's type |
| 507 | 4. Otherwise, both converted to the unsigned counterpart of the signed operand's |
| 508 | type. |
| 509 | rank: bool < int8_t < int16_t < int32_t < int64_t |
| 510 | |
| 511 | |
| 512 | Shift operators (<< >>): |
| 513 | 1. Integral promotion is applied on both sides. |
| 514 | 2. For unsigned a, a << b discards bits that shifts out. |
| 515 | For signed non-negative a, a << b is legal if no bits shifts out, otherwise error. |
| 516 | For signed negative a, a << b gives error. |
| 517 | 3. For unsigned and signed non-negative a, a >> b discards bits that shifts out. |
| 518 | For signed negative a, a >> b discards bits that shifts out, and the signed |
| 519 | bit gets extended. ("arithmetic right shift") |
| 520 | 4. Shifting with negative number of bits is undefined. (Currently, the |
| 521 | parser will shift into the other direction. This behavior may change.) |
| 522 | 5. Shifting with number of bits exceeding the width of the type is undefined. |
| 523 | (Currently, 1 << 32 == 1. This behavior may change.) |
| 524 | |
| 525 | Logical operators (!, &&, ||): |
| 526 | 1. Convert first operand to bool. (true if non-zero, false otherwise) |
| 527 | 2. If short-circuited, return the result as type bool, value 1 or 0. |
| 528 | 3. Otherwise, convert second operand to bool, evaluate the result, and return |
| 529 | the result in the same fashion. |
| 530 | |
| 531 | Arithmetic comparison operators (< > <= >= == !=): |
| 532 | 1. Promote operands in the same way as binary arithmetic and bitwise operators. |
| 533 | (Integral promotion + Usual arithmetic conversions) |
| 534 | 2. Return type bool, value 0 or 1 the same way as logical operators. |
| 535 | |
| 536 | Ternary conditional operator (?:): |
| 537 | 1. Evaluate the conditional and evaluate the operands. |
| 538 | 2. Return type of expression is the type under usual arithmetic conversions on |
| 539 | the second and third operand. (No integral promotions necessary.) |
| 540 | |
| 541 | */ |
| 542 | |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 543 | } // namespace android |
| 544 | |