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 | #ifndef CONSTANT_EXPRESSION_H_ |
| 18 | |
| 19 | #define CONSTANT_EXPRESSION_H_ |
| 20 | |
| 21 | #include <android-base/macros.h> |
| 22 | #include <string> |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 23 | #include "ScalarType.h" |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 27 | /** |
| 28 | * A constant expression is represented by a tree. |
| 29 | */ |
| 30 | struct ConstantExpression { |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 31 | |
| 32 | enum ConstExprType { |
| 33 | kConstExprLiteral, |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 34 | kConstExprUnary, |
| 35 | kConstExprBinary, |
| 36 | kConstExprTernary |
| 37 | }; |
| 38 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 39 | /* Default constructor. */ |
| 40 | ConstantExpression(); |
| 41 | /* Copy constructor. */ |
| 42 | ConstantExpression(const ConstantExpression& other); |
| 43 | /* Copy constructor, with the expr overriden. */ |
| 44 | ConstantExpression(const ConstantExpression& other, std::string expr); |
| 45 | /* Literals */ |
| 46 | ConstantExpression(const char *value); |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 47 | /* binary operations */ |
| 48 | ConstantExpression(const ConstantExpression *value1, |
| 49 | const char *op, const ConstantExpression* value2); |
| 50 | /* unary operations */ |
| 51 | ConstantExpression(const char *op, const ConstantExpression *value); |
| 52 | /* ternary ?: */ |
| 53 | ConstantExpression(const ConstantExpression *cond, |
| 54 | const ConstantExpression *trueVal, |
| 55 | const ConstantExpression *falseVal); |
| 56 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 57 | static ConstantExpression Zero(ScalarType::Kind kind); |
| 58 | static ConstantExpression One(ScalarType::Kind kind); |
| 59 | |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 60 | /* Evaluated result in a string form. */ |
Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 61 | std::string value() const; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 62 | /* Evaluated result in a string form. */ |
| 63 | std::string cppValue() const; |
| 64 | /* Evaluated result in a string form. */ |
| 65 | std::string javaValue() const; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 66 | /* Evaluated result in a string form, with given contextual kind. */ |
Yifan Hong | c07b202 | 2016-11-08 12:44:24 -0800 | [diff] [blame] | 67 | std::string value(ScalarType::Kind castKind) const; |
| 68 | /* Evaluated result in a string form, with given contextual kind. */ |
Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 69 | std::string cppValue(ScalarType::Kind castKind) const; |
Yifan Hong | 19ca75a | 2016-08-31 10:20:03 -0700 | [diff] [blame] | 70 | /* Evaluated result in a string form, with given contextual kind. */ |
Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 71 | std::string javaValue(ScalarType::Kind castKind) const; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 72 | /* Original expression with type. */ |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 73 | const std::string &description() const; |
Yifan Hong | 5706a43 | 2016-11-02 09:44:18 -0700 | [diff] [blame] | 74 | /* See mTrivialDescription */ |
| 75 | bool descriptionIsTrivial() const; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 76 | /* Return a ConstantExpression that is 1 plus the original. */ |
| 77 | ConstantExpression addOne() const; |
| 78 | /* Assignment operator. */ |
| 79 | ConstantExpression& operator=(const ConstantExpression& other); |
Yifan Hong | e77ca13 | 2016-09-27 10:49:05 -0700 | [diff] [blame] | 80 | |
| 81 | size_t castSizeT() const; |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 82 | |
| 83 | private: |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 84 | /* The formatted expression. */ |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 85 | std::string mExpr; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 86 | /* The type of the expression. Hints on its original form. */ |
| 87 | ConstExprType mType; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 88 | /* The kind of the result value. */ |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 89 | ScalarType::Kind mValueKind; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 90 | /* The stored result value. */ |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 91 | uint64_t mValue; |
Yifan Hong | 5706a43 | 2016-11-02 09:44:18 -0700 | [diff] [blame] | 92 | /* true if description() does not offer more information than value(). */ |
| 93 | bool mTrivialDescription = false; |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 94 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 95 | /* |
| 96 | * Helper function for all cpp/javaValue methods. |
| 97 | * Returns a plain string (without any prefixes or suffixes, just the |
| 98 | * digits) converted from mValue. |
| 99 | */ |
| 100 | std::string rawValue(ScalarType::Kind castKind) const; |
| 101 | /* Trim unnecessary information. Only mValue and mValueKind is kept. */ |
| 102 | ConstantExpression &toLiteral(); |
Yifan Hong | e77ca13 | 2016-09-27 10:49:05 -0700 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * Return the value casted to the given type. |
| 106 | * First cast it according to mValueKind, then cast it to T. |
| 107 | * Assumes !containsIdentifiers() |
| 108 | */ |
| 109 | template <typename T> T cast() const; |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | } // namespace android |
| 113 | |
| 114 | #endif // CONSTANT_EXPRESSION_H_ |