| 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 |  | 
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 358 | std::unique_ptr<ConstantExpression> ConstantExpression::addOne(ScalarType::Kind baseKind) { | 
 | 359 |     auto ret = std::make_unique<BinaryConstantExpression>( | 
 | 360 |         this, "+", ConstantExpression::One(baseKind).release()); | 
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 361 |     return ret; | 
| Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 362 | } | 
 | 363 |  | 
| Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 364 | std::string ConstantExpression::value() const { | 
| Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 365 |     return value(mValueKind); | 
| Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 366 | } | 
 | 367 |  | 
| Yifan Hong | c07b202 | 2016-11-08 12:44:24 -0800 | [diff] [blame] | 368 | std::string ConstantExpression::value(ScalarType::Kind castKind) const { | 
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 369 |     CHECK(isEvaluated()); | 
| Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 370 |     return rawValue(castKind) + descriptionSuffix(); | 
| Yifan Hong | c07b202 | 2016-11-08 12:44:24 -0800 | [diff] [blame] | 371 | } | 
 | 372 |  | 
| Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 373 | std::string ConstantExpression::cppValue() const { | 
 | 374 |     return cppValue(mValueKind); | 
| Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 375 | } | 
 | 376 |  | 
| Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 377 | std::string ConstantExpression::cppValue(ScalarType::Kind castKind) 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 |     std::string literal(rawValue(castKind)); | 
| Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 380 |     // this is a hack to translate | 
 | 381 |     //       enum x : int64_t {  y = 1l << 63 }; | 
 | 382 |     // into | 
 | 383 |     //       enum class x : int64_t { y = (int64_t)-9223372036854775808ull }; | 
 | 384 |     // by adding the explicit cast. | 
 | 385 |     // Because 9223372036854775808 is uint64_t, and | 
 | 386 |     // -(uint64_t)9223372036854775808 == 9223372036854775808 could not | 
 | 387 |     // be narrowed to int64_t. | 
 | 388 |     if(castKind == SK(INT64) && (int64_t)mValue == INT64_MIN) { | 
| Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 389 |         literal = "static_cast<" + | 
 | 390 |                   ScalarType(SK(INT64), nullptr /* parent */).getCppStackType()  // "int64_t" | 
 | 391 |                   + ">(" + literal + "ull)"; | 
 | 392 |     } else { | 
 | 393 |         // add suffix if necessary. | 
 | 394 |         if (castKind == SK(UINT32) || castKind == SK(UINT64)) literal += "u"; | 
 | 395 |         if (castKind == SK(UINT64) || castKind == SK(INT64)) literal += "ll"; | 
| Yifan Hong | 2bd3c1f | 2016-09-22 13:20:05 -0700 | [diff] [blame] | 396 |     } | 
| Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 397 |  | 
| Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 398 |     return literal + descriptionSuffix(); | 
| Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 399 | } | 
 | 400 |  | 
| Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 401 | std::string ConstantExpression::javaValue() const { | 
 | 402 |     return javaValue(mValueKind); | 
| Yifan Hong | 19ca75a | 2016-08-31 10:20:03 -0700 | [diff] [blame] | 403 | } | 
 | 404 |  | 
| Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 405 | std::string ConstantExpression::javaValue(ScalarType::Kind castKind) const { | 
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 406 |     CHECK(isEvaluated()); | 
| Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 407 |     std::string literal; | 
 | 408 |  | 
| Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 409 |     switch(castKind) { | 
| Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 410 |         case SK(UINT64): | 
 | 411 |             literal = rawValue(SK(INT64)) + "L"; | 
 | 412 |             break; | 
 | 413 |         case SK(INT64): | 
 | 414 |             literal = rawValue(SK(INT64)) + "L"; | 
 | 415 |             break; | 
 | 416 |         case SK(UINT32): | 
 | 417 |             literal = rawValue(SK(INT32)); | 
 | 418 |             break; | 
 | 419 |         case SK(UINT16): | 
 | 420 |             literal = rawValue(SK(INT16)); | 
 | 421 |             break; | 
 | 422 |         case SK(UINT8): | 
 | 423 |             literal = rawValue(SK(INT8)); | 
 | 424 |             break; | 
| Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 425 |         case SK(BOOL)  : | 
| Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 426 |             literal = this->cast<bool>() ? "true" : "false"; | 
 | 427 |             break; | 
 | 428 |         default: | 
 | 429 |             literal = rawValue(castKind); | 
 | 430 |             break; | 
| Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 431 |     } | 
| Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 432 |  | 
 | 433 |     return literal + descriptionSuffix(); | 
 | 434 | } | 
 | 435 |  | 
 | 436 | const std::string& ConstantExpression::expression() const { | 
 | 437 |     CHECK(isEvaluated()); | 
 | 438 |     return mExpr; | 
 | 439 | } | 
 | 440 |  | 
 | 441 | std::string ConstantExpression::rawValue() const { | 
 | 442 |     return rawValue(mValueKind); | 
| Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 443 | } | 
 | 444 |  | 
 | 445 | std::string ConstantExpression::rawValue(ScalarType::Kind castKind) 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 | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 448 | #define CASE_STR(__type__) return std::to_string(this->cast<__type__>()); | 
| Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 449 |  | 
| Yi Kong | d7f8ab3 | 2018-07-24 11:27:02 -0700 | [diff] [blame] | 450 |     SWITCH_KIND(castKind, CASE_STR, SHOULD_NOT_REACH(); return nullptr; ); | 
| Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 451 | } | 
 | 452 |  | 
| Yifan Hong | 19ca75a | 2016-08-31 10:20:03 -0700 | [diff] [blame] | 453 | template<typename T> | 
 | 454 | T ConstantExpression::cast() const { | 
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 455 |     CHECK(isEvaluated()); | 
| Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 456 |  | 
| Yifan Hong | 19ca75a | 2016-08-31 10:20:03 -0700 | [diff] [blame] | 457 | #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] | 458 |  | 
| Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 459 |     SWITCH_KIND(mValueKind, CASE_CAST_T, SHOULD_NOT_REACH(); return 0; ); | 
| Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 460 | } | 
 | 461 |  | 
| Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 462 | std::string ConstantExpression::descriptionSuffix() const { | 
 | 463 |     CHECK(isEvaluated()); | 
 | 464 |  | 
 | 465 |     if (!mTrivialDescription) { | 
 | 466 |         CHECK(!mExpr.empty()); | 
 | 467 |  | 
 | 468 |         return " /* " + mExpr + " */"; | 
 | 469 |     } | 
 | 470 |     return ""; | 
 | 471 | } | 
 | 472 |  | 
| Yifan Hong | e77ca13 | 2016-09-27 10:49:05 -0700 | [diff] [blame] | 473 | size_t ConstantExpression::castSizeT() const { | 
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 474 |     CHECK(isEvaluated()); | 
| Yifan Hong | e77ca13 | 2016-09-27 10:49:05 -0700 | [diff] [blame] | 475 |     return this->cast<size_t>(); | 
 | 476 | } | 
 | 477 |  | 
| Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 478 | bool ConstantExpression::isReferenceConstantExpression() const { | 
 | 479 |     return false; | 
 | 480 | } | 
 | 481 |  | 
| Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 482 | std::vector<ConstantExpression*> ConstantExpression::getConstantExpressions() { | 
 | 483 |     const auto& constRet = static_cast<const ConstantExpression*>(this)->getConstantExpressions(); | 
 | 484 |     std::vector<ConstantExpression*> ret(constRet.size()); | 
 | 485 |     std::transform(constRet.begin(), constRet.end(), ret.begin(), | 
 | 486 |                    [](const auto* ce) { return const_cast<ConstantExpression*>(ce); }); | 
 | 487 |     return ret; | 
 | 488 | } | 
 | 489 |  | 
| Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 490 | std::vector<Reference<LocalIdentifier>*> ConstantExpression::getReferences() { | 
 | 491 |     const auto& constRet = static_cast<const ConstantExpression*>(this)->getReferences(); | 
 | 492 |     std::vector<Reference<LocalIdentifier>*> ret(constRet.size()); | 
 | 493 |     std::transform(constRet.begin(), constRet.end(), ret.begin(), | 
 | 494 |                    [](const auto* ce) { return const_cast<Reference<LocalIdentifier>*>(ce); }); | 
 | 495 |     return ret; | 
 | 496 | } | 
 | 497 |  | 
 | 498 | std::vector<const Reference<LocalIdentifier>*> ConstantExpression::getReferences() const { | 
 | 499 |     return {}; | 
 | 500 | } | 
 | 501 |  | 
| Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 502 | status_t ConstantExpression::recursivePass(const std::function<status_t(ConstantExpression*)>& func, | 
| Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 503 |                                            std::unordered_set<const ConstantExpression*>* visited, | 
 | 504 |                                            bool processBeforeDependencies) { | 
| Timur Iskhakov | 35930c4 | 2017-08-28 18:49:54 -0700 | [diff] [blame] | 505 |     if (mIsPostParseCompleted) return OK; | 
 | 506 |  | 
| Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 507 |     if (visited->find(this) != visited->end()) return OK; | 
 | 508 |     visited->insert(this); | 
 | 509 |  | 
| Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 510 |     if (processBeforeDependencies) { | 
 | 511 |         status_t err = func(this); | 
 | 512 |         if (err != OK) return err; | 
 | 513 |     } | 
 | 514 |  | 
| Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 515 |     for (auto* nextCE : getConstantExpressions()) { | 
| Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 516 |         status_t err = nextCE->recursivePass(func, visited, processBeforeDependencies); | 
| Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 517 |         if (err != OK) return err; | 
 | 518 |     } | 
 | 519 |  | 
| Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 520 |     for (auto* nextRef : getReferences()) { | 
 | 521 |         auto* nextCE = nextRef->shallowGet()->constExpr(); | 
 | 522 |         CHECK(nextCE != nullptr) << "Local identifier is not a constant expression"; | 
| Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 523 |         status_t err = nextCE->recursivePass(func, visited, processBeforeDependencies); | 
| Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 524 |         if (err != OK) return err; | 
 | 525 |     } | 
 | 526 |  | 
| Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 527 |     if (!processBeforeDependencies) { | 
 | 528 |         status_t err = func(this); | 
 | 529 |         if (err != OK) return err; | 
 | 530 |     } | 
| Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 531 |  | 
 | 532 |     return OK; | 
 | 533 | } | 
 | 534 |  | 
| Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 535 | status_t ConstantExpression::recursivePass( | 
 | 536 |     const std::function<status_t(const ConstantExpression*)>& func, | 
| Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 537 |     std::unordered_set<const ConstantExpression*>* visited, bool processBeforeDependencies) const { | 
| Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 538 |     if (mIsPostParseCompleted) return OK; | 
 | 539 |  | 
 | 540 |     if (visited->find(this) != visited->end()) return OK; | 
 | 541 |     visited->insert(this); | 
 | 542 |  | 
| Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 543 |     if (processBeforeDependencies) { | 
 | 544 |         status_t err = func(this); | 
 | 545 |         if (err != OK) return err; | 
 | 546 |     } | 
 | 547 |  | 
| Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 548 |     for (const auto* nextCE : getConstantExpressions()) { | 
| Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 549 |         status_t err = nextCE->recursivePass(func, visited, processBeforeDependencies); | 
| Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 550 |         if (err != OK) return err; | 
 | 551 |     } | 
 | 552 |  | 
| Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 553 |     for (const auto* nextRef : getReferences()) { | 
 | 554 |         const auto* nextCE = nextRef->shallowGet()->constExpr(); | 
 | 555 |         CHECK(nextCE != nullptr) << "Local identifier is not a constant expression"; | 
| Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 556 |         status_t err = nextCE->recursivePass(func, visited, processBeforeDependencies); | 
| Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 557 |         if (err != OK) return err; | 
 | 558 |     } | 
 | 559 |  | 
| Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 560 |     if (!processBeforeDependencies) { | 
 | 561 |         status_t err = func(this); | 
 | 562 |         if (err != OK) return err; | 
 | 563 |     } | 
| Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 564 |  | 
 | 565 |     return OK; | 
 | 566 | } | 
 | 567 |  | 
| Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 568 | ConstantExpression::CheckAcyclicStatus::CheckAcyclicStatus( | 
 | 569 |     status_t status, const ConstantExpression* cycleEnd, | 
 | 570 |     const ReferenceConstantExpression* lastReference) | 
 | 571 |     : status(status), cycleEnd(cycleEnd), lastReference(lastReference) { | 
| Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 572 |     CHECK(cycleEnd == nullptr || status != OK); | 
| Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 573 |     CHECK((cycleEnd == nullptr) == (lastReference == nullptr)); | 
| Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 574 | } | 
 | 575 |  | 
 | 576 | ConstantExpression::CheckAcyclicStatus ConstantExpression::checkAcyclic( | 
 | 577 |     std::unordered_set<const ConstantExpression*>* visited, | 
 | 578 |     std::unordered_set<const ConstantExpression*>* stack) const { | 
 | 579 |     if (stack->find(this) != stack->end()) { | 
| Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 580 |         CHECK(isReferenceConstantExpression()) | 
 | 581 |             << "Only reference constant expression could be the cycle end"; | 
 | 582 |  | 
| Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 583 |         std::cerr << "ERROR: Cyclic declaration:\n"; | 
| Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 584 |         return CheckAcyclicStatus(UNKNOWN_ERROR, this, | 
 | 585 |                                   static_cast<const ReferenceConstantExpression*>(this)); | 
| Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 586 |     } | 
 | 587 |  | 
 | 588 |     if (visited->find(this) != visited->end()) return CheckAcyclicStatus(OK); | 
 | 589 |     visited->insert(this); | 
 | 590 |     stack->insert(this); | 
 | 591 |  | 
 | 592 |     for (const auto* nextCE : getConstantExpressions()) { | 
 | 593 |         auto err = nextCE->checkAcyclic(visited, stack); | 
 | 594 |         if (err.status != OK) { | 
 | 595 |             return err; | 
 | 596 |         } | 
 | 597 |     } | 
 | 598 |  | 
 | 599 |     for (const auto* nextRef : getReferences()) { | 
 | 600 |         const auto* nextCE = nextRef->shallowGet()->constExpr(); | 
 | 601 |         CHECK(nextCE != nullptr) << "Local identifier is not a constant expression"; | 
 | 602 |         auto err = nextCE->checkAcyclic(visited, stack); | 
 | 603 |  | 
 | 604 |         if (err.status != OK) { | 
 | 605 |             if (err.cycleEnd == nullptr) return err; | 
 | 606 |  | 
 | 607 |             // Only ReferenceConstantExpression has references, | 
| Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 608 |             CHECK(isReferenceConstantExpression()) | 
 | 609 |                 << "Only reference constant expression could have refereneces"; | 
| Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 610 |  | 
| Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 611 |             // mExpr is defined explicitly before evaluation | 
 | 612 |             std::cerr << "  '" << err.lastReference->mExpr << "' in '" << mExpr << "' at " | 
 | 613 |                       << nextRef->location() << "\n"; | 
| Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 614 |  | 
 | 615 |             if (err.cycleEnd == this) { | 
 | 616 |                 return CheckAcyclicStatus(err.status); | 
 | 617 |             } | 
| Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 618 |             return CheckAcyclicStatus(err.status, err.cycleEnd, | 
 | 619 |                                       static_cast<const ReferenceConstantExpression*>(this)); | 
| Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 620 |         } | 
 | 621 |     } | 
 | 622 |  | 
 | 623 |     CHECK(stack->find(this) != stack->end()); | 
 | 624 |     stack->erase(this); | 
 | 625 |     return CheckAcyclicStatus(OK); | 
 | 626 | } | 
 | 627 |  | 
| Timur Iskhakov | 35930c4 | 2017-08-28 18:49:54 -0700 | [diff] [blame] | 628 | void ConstantExpression::setPostParseCompleted() { | 
 | 629 |     CHECK(!mIsPostParseCompleted); | 
 | 630 |     mIsPostParseCompleted = true; | 
 | 631 | } | 
 | 632 |  | 
| Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 633 | std::vector<const ConstantExpression*> LiteralConstantExpression::getConstantExpressions() const { | 
| Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 634 |     return {}; | 
 | 635 | } | 
 | 636 |  | 
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 637 | UnaryConstantExpression::UnaryConstantExpression(const std::string& op, ConstantExpression* value) | 
| Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 638 |     : mUnary(value), mOp(op) {} | 
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 639 |  | 
| Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 640 | std::vector<const ConstantExpression*> UnaryConstantExpression::getConstantExpressions() const { | 
| Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 641 |     return {mUnary}; | 
 | 642 | } | 
 | 643 |  | 
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 644 | BinaryConstantExpression::BinaryConstantExpression(ConstantExpression* lval, const std::string& op, | 
 | 645 |                                                    ConstantExpression* rval) | 
| Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 646 |     : mLval(lval), mRval(rval), mOp(op) {} | 
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 647 |  | 
| Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 648 | std::vector<const ConstantExpression*> BinaryConstantExpression::getConstantExpressions() const { | 
| Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 649 |     return {mLval, mRval}; | 
 | 650 | } | 
 | 651 |  | 
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 652 | TernaryConstantExpression::TernaryConstantExpression(ConstantExpression* cond, | 
 | 653 |                                                      ConstantExpression* trueVal, | 
 | 654 |                                                      ConstantExpression* falseVal) | 
| Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 655 |     : mCond(cond), mTrueVal(trueVal), mFalseVal(falseVal) {} | 
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 656 |  | 
| Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 657 | std::vector<const ConstantExpression*> TernaryConstantExpression::getConstantExpressions() const { | 
| Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 658 |     return {mCond, mTrueVal, mFalseVal}; | 
 | 659 | } | 
 | 660 |  | 
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 661 | ReferenceConstantExpression::ReferenceConstantExpression(const Reference<LocalIdentifier>& value, | 
 | 662 |                                                          const std::string& expr) | 
| Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 663 |     : mReference(value) { | 
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 664 |     mExpr = expr; | 
| Timur Iskhakov | 505e561 | 2017-08-27 18:26:48 -0700 | [diff] [blame] | 665 |     mTrivialDescription = mExpr.empty(); | 
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 666 | } | 
 | 667 |  | 
| Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 668 | bool ReferenceConstantExpression::isReferenceConstantExpression() const { | 
 | 669 |     return true; | 
 | 670 | } | 
 | 671 |  | 
| Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 672 | std::vector<const ConstantExpression*> ReferenceConstantExpression::getConstantExpressions() const { | 
| Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 673 |     // Returns reference instead | 
 | 674 |     return {}; | 
 | 675 | } | 
 | 676 |  | 
 | 677 | std::vector<const Reference<LocalIdentifier>*> ReferenceConstantExpression::getReferences() const { | 
 | 678 |     return {&mReference}; | 
| Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 679 | } | 
 | 680 |  | 
| Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 681 | /* | 
 | 682 |  | 
 | 683 | Evaluating expressions in HIDL language | 
 | 684 |  | 
 | 685 | The following rules are mostly like that in: | 
 | 686 | http://en.cppreference.com/w/cpp/language/operator_arithmetic | 
 | 687 | http://en.cppreference.com/w/cpp/language/operator_logical | 
 | 688 | http://en.cppreference.com/w/cpp/language/operator_comparison | 
 | 689 | http://en.cppreference.com/w/cpp/language/operator_other | 
 | 690 |  | 
 | 691 | The type of literal is the first type which the value | 
 | 692 | can fit from the list of types depending on the suffix and bases. | 
 | 693 |  | 
 | 694 | suffix              decimal bases           hexadecimal bases | 
 | 695 | no suffix           int32_t                 int32_t | 
 | 696 |                     int64_t                 uint32_t | 
 | 697 |                                             int64_t | 
 | 698 |                                             uint64_t | 
 | 699 |  | 
 | 700 | u/U                 uint32_t                (same as left) | 
 | 701 |                     uint64_t | 
 | 702 |  | 
 | 703 | l/L                 int64_t                 int64_t | 
 | 704 |  | 
 | 705 | ul/UL/uL/Ul         uint64_t                uint64_t | 
 | 706 |  | 
 | 707 |  | 
 | 708 | Note: There are no negative integer literals. | 
 | 709 |       -1 is the unary minus applied to 1. | 
 | 710 |  | 
 | 711 | Unary arithmetic and bitwise operators (~ + -): | 
 | 712 |   don't change the type of the argument. | 
 | 713 |   (so -1u = -(1u) has type uint32_t) | 
 | 714 |  | 
 | 715 | Binary arithmetic and bitwise operators (except shifts) (+ - * / % & | ^): | 
 | 716 | 1. Integral promotion is first applied on both sides. | 
 | 717 | 2. If both operands have the same type, no promotion is necessary. | 
 | 718 | 3. Usual arithmetic conversions. | 
 | 719 |  | 
 | 720 | Integral promotion: if an operand is of a type with less than 32 bits, | 
 | 721 | (including bool), it is promoted to int32_t. | 
 | 722 |  | 
 | 723 | Usual arithmetic conversions: | 
 | 724 | 1. If operands are both signed or both unsigned, lesser conversion rank is | 
 | 725 |    converted to greater conversion rank. | 
 | 726 | 2. Otherwise, if unsigned's rank >= signed's rank, -> unsigned's type | 
 | 727 | 3. Otherwise, if signed's type can hold all values in unsigned's type, | 
 | 728 |    -> signed's type | 
 | 729 | 4. Otherwise, both converted to the unsigned counterpart of the signed operand's | 
 | 730 |    type. | 
 | 731 | rank: bool < int8_t < int16_t < int32_t < int64_t | 
 | 732 |  | 
 | 733 |  | 
 | 734 | Shift operators (<< >>): | 
 | 735 | 1. Integral promotion is applied on both sides. | 
 | 736 | 2. For unsigned a, a << b discards bits that shifts out. | 
 | 737 |    For signed non-negative a, a << b is legal if no bits shifts out, otherwise error. | 
 | 738 |    For signed negative a, a << b gives error. | 
 | 739 | 3. For unsigned and signed non-negative a, a >> b discards bits that shifts out. | 
 | 740 |    For signed negative a, a >> b discards bits that shifts out, and the signed | 
 | 741 |    bit gets extended. ("arithmetic right shift") | 
 | 742 | 4. Shifting with negative number of bits is undefined. (Currently, the | 
 | 743 |    parser will shift into the other direction. This behavior may change.) | 
 | 744 | 5. Shifting with number of bits exceeding the width of the type is undefined. | 
 | 745 |    (Currently, 1 << 32 == 1. This behavior may change.) | 
 | 746 |  | 
 | 747 | Logical operators (!, &&, ||): | 
 | 748 | 1. Convert first operand to bool. (true if non-zero, false otherwise) | 
 | 749 | 2. If short-circuited, return the result as type bool, value 1 or 0. | 
 | 750 | 3. Otherwise, convert second operand to bool, evaluate the result, and return | 
 | 751 |    the result in the same fashion. | 
 | 752 |  | 
 | 753 | Arithmetic comparison operators (< > <= >= == !=): | 
 | 754 | 1. Promote operands in the same way as binary arithmetic and bitwise operators. | 
 | 755 |    (Integral promotion + Usual arithmetic conversions) | 
 | 756 | 2. Return type bool, value 0 or 1 the same way as logical operators. | 
 | 757 |  | 
 | 758 | Ternary conditional operator (?:): | 
 | 759 | 1. Evaluate the conditional and evaluate the operands. | 
 | 760 | 2. Return type of expression is the type under usual arithmetic conversions on | 
 | 761 |    the second and third operand. (No integral promotions necessary.) | 
 | 762 |  | 
 | 763 | */ | 
 | 764 |  | 
| Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 765 | }  // namespace android | 
 | 766 |  |