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