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 | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 62 | // TODO(b/64358435) move isSupported to EnumValue |
Timur Iskhakov | 63f3990 | 2017-08-29 15:47:29 -0700 | [diff] [blame] | 63 | return SK(BOOL) == kind || ScalarType(kind, nullptr /* parent */).isValidEnumStorageType(); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 66 | /* See docs at the end for details on integral promotion. */ |
| 67 | ScalarType::Kind integralPromotion(ScalarType::Kind in) { |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 68 | 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] | 69 | } |
| 70 | |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 71 | /* See docs at the end for details on usual arithmetic conversion. */ |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 72 | ScalarType::Kind usualArithmeticConversion(ScalarType::Kind lft, |
| 73 | ScalarType::Kind rgt) { |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 74 | CHECK(isSupported(lft) && isSupported(rgt)); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 75 | // Kinds in concern: bool, (u)int[8|16|32|64] |
| 76 | if(lft == rgt) return lft; // easy case |
| 77 | if(lft == SK(BOOL)) return rgt; |
| 78 | if(rgt == SK(BOOL)) return lft; |
| 79 | bool isLftSigned = (lft == SK(INT8)) || (lft == SK(INT16)) |
| 80 | || (lft == SK(INT32)) || (lft == SK(INT64)); |
| 81 | bool isRgtSigned = (rgt == SK(INT8)) || (rgt == SK(INT16)) |
| 82 | || (rgt == SK(INT32)) || (rgt == SK(INT64)); |
| 83 | if(isLftSigned == isRgtSigned) return lft < rgt ? rgt : lft; |
| 84 | ScalarType::Kind unsignedRank = isLftSigned ? rgt : lft; |
| 85 | ScalarType::Kind signedRank = isLftSigned ? lft : rgt; |
| 86 | if(unsignedRank >= signedRank) return unsignedRank; |
| 87 | if(signedRank > unsignedRank) return signedRank; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 88 | |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 89 | // Although there is such rule to return "the unsigned counterpart of |
| 90 | // the signed operand", it should not reach here in our HIDL grammar. |
| 91 | LOG(FATAL) << "Could not do usual arithmetic conversion for type " |
| 92 | << lft << "and" << rgt; |
| 93 | switch(signedRank) { |
| 94 | case SK(INT8): return SK(UINT8); |
| 95 | case SK(INT16): return SK(UINT16); |
| 96 | case SK(INT32): return SK(UINT32); |
| 97 | case SK(INT64): return SK(UINT64); |
| 98 | default: return SK(UINT64); |
| 99 | } |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 100 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 101 | |
| 102 | template <class T> |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 103 | T handleUnary(const std::string& op, T val) { |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 104 | COMPUTE_UNARY(+) |
| 105 | COMPUTE_UNARY(-) |
| 106 | COMPUTE_UNARY(!) |
| 107 | COMPUTE_UNARY(~) |
| 108 | // Should not reach here. |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 109 | SHOULD_NOT_REACH() << "Could not handleUnary for " << op << " " << val; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 110 | return static_cast<T>(0xdeadbeef); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | template <class T> |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 114 | T handleBinaryCommon(T lval, const std::string& op, T rval) { |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 115 | COMPUTE_BINARY(+) |
| 116 | COMPUTE_BINARY(-) |
| 117 | COMPUTE_BINARY(*) |
| 118 | COMPUTE_BINARY(/) |
| 119 | COMPUTE_BINARY(%) |
| 120 | COMPUTE_BINARY(|) |
| 121 | COMPUTE_BINARY(^) |
| 122 | COMPUTE_BINARY(&) |
| 123 | // comparison operators: return 0 or 1 by nature. |
| 124 | COMPUTE_BINARY(==) |
| 125 | COMPUTE_BINARY(!=) |
| 126 | COMPUTE_BINARY(<) |
| 127 | COMPUTE_BINARY(>) |
| 128 | COMPUTE_BINARY(<=) |
| 129 | COMPUTE_BINARY(>=) |
| 130 | // Should not reach here. |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 131 | SHOULD_NOT_REACH() << "Could not handleBinaryCommon for " |
| 132 | << lval << " " << op << " " << rval; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 133 | return static_cast<T>(0xdeadbeef); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | template <class T> |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 137 | T handleShift(T lval, const std::string& op, int64_t rval) { |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 138 | // just cast rval to int64_t and it should fit. |
| 139 | COMPUTE_BINARY(>>) |
| 140 | COMPUTE_BINARY(<<) |
| 141 | // Should not reach here. |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 142 | SHOULD_NOT_REACH() << "Could not handleShift for " |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 143 | << lval << " " << op << " " << rval; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 144 | return static_cast<T>(0xdeadbeef); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 147 | bool handleLogical(bool lval, const std::string& op, bool rval) { |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 148 | COMPUTE_BINARY(||); |
| 149 | COMPUTE_BINARY(&&); |
| 150 | // Should not reach here. |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 151 | SHOULD_NOT_REACH() << "Could not handleLogical for " |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 152 | << lval << " " << op << " " << rval; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 153 | return false; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 156 | std::unique_ptr<ConstantExpression> ConstantExpression::Zero(ScalarType::Kind kind) { |
| 157 | return ValueOf(kind, 0); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 160 | std::unique_ptr<ConstantExpression> ConstantExpression::One(ScalarType::Kind kind) { |
| 161 | return ValueOf(kind, 1); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 164 | std::unique_ptr<ConstantExpression> ConstantExpression::ValueOf(ScalarType::Kind kind, |
| 165 | uint64_t value) { |
| 166 | return std::make_unique<LiteralConstantExpression>(kind, value); |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 169 | bool ConstantExpression::isEvaluated() const { |
| 170 | return mIsEvaluated; |
| 171 | } |
| 172 | |
Steven Moreland | d9d6dcb | 2017-09-20 15:55:39 -0700 | [diff] [blame] | 173 | LiteralConstantExpression::LiteralConstantExpression( |
| 174 | ScalarType::Kind kind, uint64_t value, const std::string& expr) { |
| 175 | |
| 176 | CHECK(!expr.empty()); |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 177 | CHECK(isSupported(kind)); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 178 | mTrivialDescription = true; |
Steven Moreland | d9d6dcb | 2017-09-20 15:55:39 -0700 | [diff] [blame] | 179 | mExpr = 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 | mExpr = std::string("(") + mOp + mUnary->description() + ")"; |
| 260 | mValueKind = mUnary->mValueKind; |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 261 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 262 | #define CASE_UNARY(__type__) \ |
| 263 | mValue = handleUnary(mOp, static_cast<__type__>(mUnary->mValue)); \ |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 264 | return; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 265 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 266 | SWITCH_KIND(mValueKind, CASE_UNARY, SHOULD_NOT_REACH(); return;) |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 267 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 268 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 269 | void BinaryConstantExpression::evaluate() { |
| 270 | if (isEvaluated()) return; |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 271 | CHECK(mLval->isEvaluated()); |
| 272 | CHECK(mRval->isEvaluated()); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 273 | mIsEvaluated = true; |
| 274 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 275 | mExpr = std::string("(") + mLval->description() + " " + mOp + " " + mRval->description() + ")"; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 276 | |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 277 | bool isArithmeticOrBitflip = OP_IS_BIN_ARITHMETIC || OP_IS_BIN_BITFLIP; |
| 278 | |
| 279 | // CASE 1: + - * / % | ^ & < > <= >= == != |
| 280 | if(isArithmeticOrBitflip || OP_IS_BIN_COMP) { |
| 281 | // promoted kind for both operands. |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 282 | ScalarType::Kind promoted = usualArithmeticConversion(integralPromotion(mLval->mValueKind), |
| 283 | integralPromotion(mRval->mValueKind)); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 284 | // result kind. |
| 285 | mValueKind = isArithmeticOrBitflip |
| 286 | ? promoted // arithmetic or bitflip operators generates promoted type |
| 287 | : SK(BOOL); // comparison operators generates bool |
| 288 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 289 | #define CASE_BINARY_COMMON(__type__) \ |
| 290 | mValue = handleBinaryCommon(static_cast<__type__>(mLval->mValue), mOp, \ |
| 291 | static_cast<__type__>(mRval->mValue)); \ |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 292 | return; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 293 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 294 | SWITCH_KIND(promoted, CASE_BINARY_COMMON, SHOULD_NOT_REACH(); return;) |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | // CASE 2: << >> |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 298 | std::string newOp = mOp; |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 299 | if(OP_IS_BIN_SHIFT) { |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 300 | mValueKind = integralPromotion(mLval->mValueKind); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 301 | // 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] | 302 | int64_t numBits = mRval->cast<int64_t>(); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 303 | if(numBits < 0) { |
| 304 | // shifting with negative number of bits is undefined in C. In HIDL it |
| 305 | // is defined as shifting into the other direction. |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 306 | newOp = OPEQ("<<") ? std::string(">>") : std::string("<<"); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 307 | numBits = -numBits; |
| 308 | } |
| 309 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 310 | #define CASE_SHIFT(__type__) \ |
| 311 | mValue = handleShift(static_cast<__type__>(mLval->mValue), newOp, numBits); \ |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 312 | return; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 313 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 314 | SWITCH_KIND(mValueKind, CASE_SHIFT, SHOULD_NOT_REACH(); return;) |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 315 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 316 | |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 317 | // CASE 3: && || |
| 318 | if(OP_IS_BIN_LOGICAL) { |
| 319 | mValueKind = SK(BOOL); |
| 320 | // easy; everything is bool. |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 321 | mValue = handleLogical(mLval->mValue, mOp, mRval->mValue); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 322 | return; |
| 323 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 324 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 325 | SHOULD_NOT_REACH(); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 328 | void TernaryConstantExpression::evaluate() { |
| 329 | if (isEvaluated()) return; |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 330 | CHECK(mCond->isEvaluated()); |
| 331 | CHECK(mTrueVal->isEvaluated()); |
| 332 | CHECK(mFalseVal->isEvaluated()); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 333 | mIsEvaluated = true; |
| 334 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 335 | mExpr = std::string("(") + mCond->description() + "?" + mTrueVal->description() + ":" + |
| 336 | mFalseVal->description() + ")"; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 337 | |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 338 | // note: for ?:, unlike arithmetic ops, integral promotion is not processed. |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 339 | mValueKind = usualArithmeticConversion(mTrueVal->mValueKind, mFalseVal->mValueKind); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 340 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 341 | #define CASE_TERNARY(__type__) \ |
| 342 | mValue = mCond->mValue ? (static_cast<__type__>(mTrueVal->mValue)) \ |
| 343 | : (static_cast<__type__>(mFalseVal->mValue)); \ |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 344 | return; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 345 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 346 | SWITCH_KIND(mValueKind, CASE_TERNARY, SHOULD_NOT_REACH(); return;) |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 349 | void ReferenceConstantExpression::evaluate() { |
| 350 | if (isEvaluated()) return; |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 351 | CHECK(mReference->constExpr() != nullptr); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 352 | |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 353 | ConstantExpression* expr = mReference->constExpr(); |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 354 | CHECK(expr->isEvaluated()); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 355 | |
| 356 | mValueKind = expr->mValueKind; |
| 357 | mValue = expr->mValue; |
| 358 | mIsEvaluated = true; |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 359 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 360 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 361 | std::unique_ptr<ConstantExpression> ConstantExpression::addOne(ScalarType::Kind baseKind) { |
| 362 | auto ret = std::make_unique<BinaryConstantExpression>( |
| 363 | this, "+", ConstantExpression::One(baseKind).release()); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 364 | return ret; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 367 | const std::string& ConstantExpression::description() const { |
| 368 | CHECK(isEvaluated()); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 369 | return mExpr; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Yifan Hong | 5706a43 | 2016-11-02 09:44:18 -0700 | [diff] [blame] | 372 | bool ConstantExpression::descriptionIsTrivial() const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 373 | CHECK(isEvaluated()); |
Yifan Hong | 5706a43 | 2016-11-02 09:44:18 -0700 | [diff] [blame] | 374 | return mTrivialDescription; |
| 375 | } |
| 376 | |
Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 377 | std::string ConstantExpression::value() const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 378 | CHECK(isEvaluated()); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 379 | return rawValue(mValueKind); |
| 380 | } |
| 381 | |
Yifan Hong | c07b202 | 2016-11-08 12:44:24 -0800 | [diff] [blame] | 382 | std::string ConstantExpression::value(ScalarType::Kind castKind) const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 383 | CHECK(isEvaluated()); |
Yifan Hong | c07b202 | 2016-11-08 12:44:24 -0800 | [diff] [blame] | 384 | return rawValue(castKind); |
| 385 | } |
| 386 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 387 | std::string ConstantExpression::cppValue() const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 388 | CHECK(isEvaluated()); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 389 | return cppValue(mValueKind); |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 392 | std::string ConstantExpression::cppValue(ScalarType::Kind castKind) const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 393 | CHECK(isEvaluated()); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 394 | std::string literal(rawValue(castKind)); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 395 | // this is a hack to translate |
| 396 | // enum x : int64_t { y = 1l << 63 }; |
| 397 | // into |
| 398 | // enum class x : int64_t { y = (int64_t)-9223372036854775808ull }; |
| 399 | // by adding the explicit cast. |
| 400 | // Because 9223372036854775808 is uint64_t, and |
| 401 | // -(uint64_t)9223372036854775808 == 9223372036854775808 could not |
| 402 | // be narrowed to int64_t. |
| 403 | if(castKind == SK(INT64) && (int64_t)mValue == INT64_MIN) { |
Timur Iskhakov | 63f3990 | 2017-08-29 15:47:29 -0700 | [diff] [blame] | 404 | return strdup(("static_cast<" + |
| 405 | ScalarType(SK(INT64), nullptr /* parent */).getCppStackType() // "int64_t" |
| 406 | + ">(" + literal + "ull)") |
| 407 | .c_str()); |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 408 | } |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 409 | |
Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 410 | // add suffix if necessary. |
| 411 | if(castKind == SK(UINT32) || castKind == SK(UINT64)) literal += "u"; |
| 412 | if(castKind == SK(UINT64) || castKind == SK(INT64)) literal += "ll"; |
Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 413 | return literal; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 416 | std::string ConstantExpression::javaValue() const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 417 | CHECK(isEvaluated()); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 418 | return javaValue(mValueKind); |
Yifan Hong | 19ca75a | 2016-08-31 10:20:03 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 421 | std::string ConstantExpression::javaValue(ScalarType::Kind castKind) const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 422 | CHECK(isEvaluated()); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 423 | switch(castKind) { |
| 424 | case SK(UINT64): return rawValue(SK(INT64)) + "L"; |
| 425 | case SK(INT64): return rawValue(SK(INT64)) + "L"; |
| 426 | case SK(UINT32): return rawValue(SK(INT32)); |
| 427 | case SK(UINT16): return rawValue(SK(INT16)); |
| 428 | case SK(UINT8) : return rawValue(SK(INT8)); |
| 429 | case SK(BOOL) : |
| 430 | return this->cast<bool>() ? strdup("true") : strdup("false"); |
| 431 | default: break; |
| 432 | } |
| 433 | return rawValue(castKind); |
| 434 | } |
| 435 | |
| 436 | std::string ConstantExpression::rawValue(ScalarType::Kind castKind) const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 437 | CHECK(isEvaluated()); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 438 | |
Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 439 | #define CASE_STR(__type__) return std::to_string(this->cast<__type__>()); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 440 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 441 | SWITCH_KIND(castKind, CASE_STR, SHOULD_NOT_REACH(); return 0; ); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Yifan Hong | 19ca75a | 2016-08-31 10:20:03 -0700 | [diff] [blame] | 444 | template<typename T> |
| 445 | T ConstantExpression::cast() const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 446 | CHECK(isEvaluated()); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 447 | |
Yifan Hong | 19ca75a | 2016-08-31 10:20:03 -0700 | [diff] [blame] | 448 | #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] | 449 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 450 | SWITCH_KIND(mValueKind, CASE_CAST_T, SHOULD_NOT_REACH(); return 0; ); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Yifan Hong | e77ca13 | 2016-09-27 10:49:05 -0700 | [diff] [blame] | 453 | size_t ConstantExpression::castSizeT() const { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 454 | CHECK(isEvaluated()); |
Yifan Hong | e77ca13 | 2016-09-27 10:49:05 -0700 | [diff] [blame] | 455 | return this->cast<size_t>(); |
| 456 | } |
| 457 | |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 458 | bool ConstantExpression::isReferenceConstantExpression() const { |
| 459 | return false; |
| 460 | } |
| 461 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 462 | std::vector<ConstantExpression*> ConstantExpression::getConstantExpressions() { |
| 463 | const auto& constRet = static_cast<const ConstantExpression*>(this)->getConstantExpressions(); |
| 464 | std::vector<ConstantExpression*> ret(constRet.size()); |
| 465 | std::transform(constRet.begin(), constRet.end(), ret.begin(), |
| 466 | [](const auto* ce) { return const_cast<ConstantExpression*>(ce); }); |
| 467 | return ret; |
| 468 | } |
| 469 | |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 470 | std::vector<Reference<LocalIdentifier>*> ConstantExpression::getReferences() { |
| 471 | const auto& constRet = static_cast<const ConstantExpression*>(this)->getReferences(); |
| 472 | std::vector<Reference<LocalIdentifier>*> ret(constRet.size()); |
| 473 | std::transform(constRet.begin(), constRet.end(), ret.begin(), |
| 474 | [](const auto* ce) { return const_cast<Reference<LocalIdentifier>*>(ce); }); |
| 475 | return ret; |
| 476 | } |
| 477 | |
| 478 | std::vector<const Reference<LocalIdentifier>*> ConstantExpression::getReferences() const { |
| 479 | return {}; |
| 480 | } |
| 481 | |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 482 | status_t ConstantExpression::recursivePass(const std::function<status_t(ConstantExpression*)>& func, |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 483 | std::unordered_set<const ConstantExpression*>* visited, |
| 484 | bool processBeforeDependencies) { |
Timur Iskhakov | 35930c4 | 2017-08-28 18:49:54 -0700 | [diff] [blame] | 485 | if (mIsPostParseCompleted) return OK; |
| 486 | |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 487 | if (visited->find(this) != visited->end()) return OK; |
| 488 | visited->insert(this); |
| 489 | |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 490 | if (processBeforeDependencies) { |
| 491 | status_t err = func(this); |
| 492 | if (err != OK) return err; |
| 493 | } |
| 494 | |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 495 | for (auto* nextCE : getConstantExpressions()) { |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 496 | status_t err = nextCE->recursivePass(func, visited, processBeforeDependencies); |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 497 | if (err != OK) return err; |
| 498 | } |
| 499 | |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 500 | for (auto* nextRef : getReferences()) { |
| 501 | auto* nextCE = nextRef->shallowGet()->constExpr(); |
| 502 | CHECK(nextCE != nullptr) << "Local identifier is not a constant expression"; |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 503 | status_t err = nextCE->recursivePass(func, visited, processBeforeDependencies); |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 504 | if (err != OK) return err; |
| 505 | } |
| 506 | |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 507 | if (!processBeforeDependencies) { |
| 508 | status_t err = func(this); |
| 509 | if (err != OK) return err; |
| 510 | } |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 511 | |
| 512 | return OK; |
| 513 | } |
| 514 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 515 | status_t ConstantExpression::recursivePass( |
| 516 | const std::function<status_t(const ConstantExpression*)>& func, |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 517 | std::unordered_set<const ConstantExpression*>* visited, bool processBeforeDependencies) const { |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 518 | if (mIsPostParseCompleted) return OK; |
| 519 | |
| 520 | if (visited->find(this) != visited->end()) return OK; |
| 521 | visited->insert(this); |
| 522 | |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 523 | if (processBeforeDependencies) { |
| 524 | status_t err = func(this); |
| 525 | if (err != OK) return err; |
| 526 | } |
| 527 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 528 | for (const auto* nextCE : getConstantExpressions()) { |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 529 | status_t err = nextCE->recursivePass(func, visited, processBeforeDependencies); |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 530 | if (err != OK) return err; |
| 531 | } |
| 532 | |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 533 | for (const auto* nextRef : getReferences()) { |
| 534 | const auto* nextCE = nextRef->shallowGet()->constExpr(); |
| 535 | CHECK(nextCE != nullptr) << "Local identifier is not a constant expression"; |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 536 | status_t err = nextCE->recursivePass(func, visited, processBeforeDependencies); |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 537 | if (err != OK) return err; |
| 538 | } |
| 539 | |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 540 | if (!processBeforeDependencies) { |
| 541 | status_t err = func(this); |
| 542 | if (err != OK) return err; |
| 543 | } |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 544 | |
| 545 | return OK; |
| 546 | } |
| 547 | |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 548 | ConstantExpression::CheckAcyclicStatus::CheckAcyclicStatus( |
| 549 | status_t status, const ConstantExpression* cycleEnd, |
| 550 | const ReferenceConstantExpression* lastReference) |
| 551 | : status(status), cycleEnd(cycleEnd), lastReference(lastReference) { |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 552 | CHECK(cycleEnd == nullptr || status != OK); |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 553 | CHECK((cycleEnd == nullptr) == (lastReference == nullptr)); |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | ConstantExpression::CheckAcyclicStatus ConstantExpression::checkAcyclic( |
| 557 | std::unordered_set<const ConstantExpression*>* visited, |
| 558 | std::unordered_set<const ConstantExpression*>* stack) const { |
| 559 | if (stack->find(this) != stack->end()) { |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 560 | CHECK(isReferenceConstantExpression()) |
| 561 | << "Only reference constant expression could be the cycle end"; |
| 562 | |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 563 | std::cerr << "ERROR: Cyclic declaration:\n"; |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 564 | return CheckAcyclicStatus(UNKNOWN_ERROR, this, |
| 565 | static_cast<const ReferenceConstantExpression*>(this)); |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | if (visited->find(this) != visited->end()) return CheckAcyclicStatus(OK); |
| 569 | visited->insert(this); |
| 570 | stack->insert(this); |
| 571 | |
| 572 | for (const auto* nextCE : getConstantExpressions()) { |
| 573 | auto err = nextCE->checkAcyclic(visited, stack); |
| 574 | if (err.status != OK) { |
| 575 | return err; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | for (const auto* nextRef : getReferences()) { |
| 580 | const auto* nextCE = nextRef->shallowGet()->constExpr(); |
| 581 | CHECK(nextCE != nullptr) << "Local identifier is not a constant expression"; |
| 582 | auto err = nextCE->checkAcyclic(visited, stack); |
| 583 | |
| 584 | if (err.status != OK) { |
| 585 | if (err.cycleEnd == nullptr) return err; |
| 586 | |
| 587 | // Only ReferenceConstantExpression has references, |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 588 | CHECK(isReferenceConstantExpression()) |
| 589 | << "Only reference constant expression could have refereneces"; |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 590 | |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 591 | // mExpr is defined explicitly before evaluation |
| 592 | std::cerr << " '" << err.lastReference->mExpr << "' in '" << mExpr << "' at " |
| 593 | << nextRef->location() << "\n"; |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 594 | |
| 595 | if (err.cycleEnd == this) { |
| 596 | return CheckAcyclicStatus(err.status); |
| 597 | } |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 598 | return CheckAcyclicStatus(err.status, err.cycleEnd, |
| 599 | static_cast<const ReferenceConstantExpression*>(this)); |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
| 603 | CHECK(stack->find(this) != stack->end()); |
| 604 | stack->erase(this); |
| 605 | return CheckAcyclicStatus(OK); |
| 606 | } |
| 607 | |
Timur Iskhakov | 35930c4 | 2017-08-28 18:49:54 -0700 | [diff] [blame] | 608 | void ConstantExpression::setPostParseCompleted() { |
| 609 | CHECK(!mIsPostParseCompleted); |
| 610 | mIsPostParseCompleted = true; |
| 611 | } |
| 612 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 613 | std::vector<const ConstantExpression*> LiteralConstantExpression::getConstantExpressions() const { |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 614 | return {}; |
| 615 | } |
| 616 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 617 | UnaryConstantExpression::UnaryConstantExpression(const std::string& op, ConstantExpression* value) |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 618 | : mUnary(value), mOp(op) {} |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 619 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 620 | std::vector<const ConstantExpression*> UnaryConstantExpression::getConstantExpressions() const { |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 621 | return {mUnary}; |
| 622 | } |
| 623 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 624 | BinaryConstantExpression::BinaryConstantExpression(ConstantExpression* lval, const std::string& op, |
| 625 | ConstantExpression* rval) |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 626 | : mLval(lval), mRval(rval), mOp(op) {} |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 627 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 628 | std::vector<const ConstantExpression*> BinaryConstantExpression::getConstantExpressions() const { |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 629 | return {mLval, mRval}; |
| 630 | } |
| 631 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 632 | TernaryConstantExpression::TernaryConstantExpression(ConstantExpression* cond, |
| 633 | ConstantExpression* trueVal, |
| 634 | ConstantExpression* falseVal) |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 635 | : mCond(cond), mTrueVal(trueVal), mFalseVal(falseVal) {} |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 636 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 637 | std::vector<const ConstantExpression*> TernaryConstantExpression::getConstantExpressions() const { |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 638 | return {mCond, mTrueVal, mFalseVal}; |
| 639 | } |
| 640 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 641 | ReferenceConstantExpression::ReferenceConstantExpression(const Reference<LocalIdentifier>& value, |
| 642 | const std::string& expr) |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 643 | : mReference(value) { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 644 | mExpr = expr; |
Timur Iskhakov | 505e561 | 2017-08-27 18:26:48 -0700 | [diff] [blame] | 645 | mTrivialDescription = mExpr.empty(); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 648 | bool ReferenceConstantExpression::isReferenceConstantExpression() const { |
| 649 | return true; |
| 650 | } |
| 651 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 652 | std::vector<const ConstantExpression*> ReferenceConstantExpression::getConstantExpressions() const { |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 653 | // Returns reference instead |
| 654 | return {}; |
| 655 | } |
| 656 | |
| 657 | std::vector<const Reference<LocalIdentifier>*> ReferenceConstantExpression::getReferences() const { |
| 658 | return {&mReference}; |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 661 | /* |
| 662 | |
| 663 | Evaluating expressions in HIDL language |
| 664 | |
| 665 | The following rules are mostly like that in: |
| 666 | http://en.cppreference.com/w/cpp/language/operator_arithmetic |
| 667 | http://en.cppreference.com/w/cpp/language/operator_logical |
| 668 | http://en.cppreference.com/w/cpp/language/operator_comparison |
| 669 | http://en.cppreference.com/w/cpp/language/operator_other |
| 670 | |
| 671 | The type of literal is the first type which the value |
| 672 | can fit from the list of types depending on the suffix and bases. |
| 673 | |
| 674 | suffix decimal bases hexadecimal bases |
| 675 | no suffix int32_t int32_t |
| 676 | int64_t uint32_t |
| 677 | int64_t |
| 678 | uint64_t |
| 679 | |
| 680 | u/U uint32_t (same as left) |
| 681 | uint64_t |
| 682 | |
| 683 | l/L int64_t int64_t |
| 684 | |
| 685 | ul/UL/uL/Ul uint64_t uint64_t |
| 686 | |
| 687 | |
| 688 | Note: There are no negative integer literals. |
| 689 | -1 is the unary minus applied to 1. |
| 690 | |
| 691 | Unary arithmetic and bitwise operators (~ + -): |
| 692 | don't change the type of the argument. |
| 693 | (so -1u = -(1u) has type uint32_t) |
| 694 | |
| 695 | Binary arithmetic and bitwise operators (except shifts) (+ - * / % & | ^): |
| 696 | 1. Integral promotion is first applied on both sides. |
| 697 | 2. If both operands have the same type, no promotion is necessary. |
| 698 | 3. Usual arithmetic conversions. |
| 699 | |
| 700 | Integral promotion: if an operand is of a type with less than 32 bits, |
| 701 | (including bool), it is promoted to int32_t. |
| 702 | |
| 703 | Usual arithmetic conversions: |
| 704 | 1. If operands are both signed or both unsigned, lesser conversion rank is |
| 705 | converted to greater conversion rank. |
| 706 | 2. Otherwise, if unsigned's rank >= signed's rank, -> unsigned's type |
| 707 | 3. Otherwise, if signed's type can hold all values in unsigned's type, |
| 708 | -> signed's type |
| 709 | 4. Otherwise, both converted to the unsigned counterpart of the signed operand's |
| 710 | type. |
| 711 | rank: bool < int8_t < int16_t < int32_t < int64_t |
| 712 | |
| 713 | |
| 714 | Shift operators (<< >>): |
| 715 | 1. Integral promotion is applied on both sides. |
| 716 | 2. For unsigned a, a << b discards bits that shifts out. |
| 717 | For signed non-negative a, a << b is legal if no bits shifts out, otherwise error. |
| 718 | For signed negative a, a << b gives error. |
| 719 | 3. For unsigned and signed non-negative a, a >> b discards bits that shifts out. |
| 720 | For signed negative a, a >> b discards bits that shifts out, and the signed |
| 721 | bit gets extended. ("arithmetic right shift") |
| 722 | 4. Shifting with negative number of bits is undefined. (Currently, the |
| 723 | parser will shift into the other direction. This behavior may change.) |
| 724 | 5. Shifting with number of bits exceeding the width of the type is undefined. |
| 725 | (Currently, 1 << 32 == 1. This behavior may change.) |
| 726 | |
| 727 | Logical operators (!, &&, ||): |
| 728 | 1. Convert first operand to bool. (true if non-zero, false otherwise) |
| 729 | 2. If short-circuited, return the result as type bool, value 1 or 0. |
| 730 | 3. Otherwise, convert second operand to bool, evaluate the result, and return |
| 731 | the result in the same fashion. |
| 732 | |
| 733 | Arithmetic comparison operators (< > <= >= == !=): |
| 734 | 1. Promote operands in the same way as binary arithmetic and bitwise operators. |
| 735 | (Integral promotion + Usual arithmetic conversions) |
| 736 | 2. Return type bool, value 0 or 1 the same way as logical operators. |
| 737 | |
| 738 | Ternary conditional operator (?:): |
| 739 | 1. Evaluate the conditional and evaluate the operands. |
| 740 | 2. Return type of expression is the type under usual arithmetic conversions on |
| 741 | the second and third operand. (No integral promotions necessary.) |
| 742 | |
| 743 | */ |
| 744 | |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 745 | } // namespace android |
| 746 | |