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> |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 22 | #include <functional> |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 23 | #include <memory> |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 24 | #include <string> |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 25 | #include <unordered_set> |
| 26 | #include <vector> |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 27 | |
| 28 | #include "Reference.h" |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 29 | #include "ScalarType.h" |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 33 | struct LocalIdentifier; |
| 34 | |
| 35 | struct LiteralConstantExpression; |
| 36 | struct UnaryConstantExpression; |
| 37 | struct BinaryConstantExpression; |
| 38 | struct TernaryConstantExpression; |
| 39 | struct ReferenceConstantExpression; |
| 40 | |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 41 | /** |
| 42 | * A constant expression is represented by a tree. |
| 43 | */ |
| 44 | struct ConstantExpression { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 45 | static std::unique_ptr<ConstantExpression> Zero(ScalarType::Kind kind); |
| 46 | static std::unique_ptr<ConstantExpression> One(ScalarType::Kind kind); |
| 47 | static std::unique_ptr<ConstantExpression> ValueOf(ScalarType::Kind kind, uint64_t value); |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 48 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 49 | virtual ~ConstantExpression() {} |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 50 | |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 51 | virtual bool isReferenceConstantExpression() const; |
| 52 | |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 53 | // Proceeds recursive pass |
| 54 | // Makes sure to visit each node only once |
| 55 | // Used to provide lookup and lazy evaluation |
| 56 | status_t recursivePass(const std::function<status_t(ConstantExpression*)>& func, |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 57 | std::unordered_set<const ConstantExpression*>* visited, |
| 58 | bool processBeforeDependencies); |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 59 | status_t recursivePass(const std::function<status_t(const ConstantExpression*)>& func, |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 60 | std::unordered_set<const ConstantExpression*>* visited, |
| 61 | bool processBeforeDependencies) const; |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 62 | |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 63 | // If this object is in an invalid state. |
| 64 | virtual status_t validate() const; |
| 65 | |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 66 | // Evaluates current constant expression |
| 67 | // Doesn't call recursive evaluation, so must be called after dependencies |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 68 | virtual void evaluate() = 0; |
Colin Cross | a549b71 | 2017-08-09 18:33:22 +0000 | [diff] [blame] | 69 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 70 | std::vector<ConstantExpression*> getConstantExpressions(); |
| 71 | virtual std::vector<const ConstantExpression*> getConstantExpressions() const = 0; |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 72 | |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 73 | std::vector<Reference<LocalIdentifier>*> getReferences(); |
| 74 | virtual std::vector<const Reference<LocalIdentifier>*> getReferences() const; |
| 75 | |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 76 | std::vector<Reference<Type>*> getTypeReferences(); |
| 77 | virtual std::vector<const Reference<Type>*> getTypeReferences() const; |
| 78 | |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 79 | // Recursive tree pass checkAcyclic return type. |
| 80 | // Stores cycle end for nice error messages. |
| 81 | struct CheckAcyclicStatus { |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 82 | CheckAcyclicStatus(status_t status, const ConstantExpression* cycleEnd = nullptr, |
| 83 | const ReferenceConstantExpression* lastReferenceExpression = nullptr); |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 84 | |
| 85 | status_t status; |
| 86 | |
| 87 | // If a cycle is found, stores the end of cycle. |
| 88 | // While going back in recursion, this is used to stop printing the cycle. |
| 89 | const ConstantExpression* cycleEnd; |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 90 | |
| 91 | // The last ReferenceConstantExpression visited on the cycle. |
| 92 | const ReferenceConstantExpression* lastReference; |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | // Recursive tree pass that ensures that constant expressions definitions |
| 96 | // are acyclic. |
| 97 | CheckAcyclicStatus checkAcyclic(std::unordered_set<const ConstantExpression*>* visited, |
| 98 | std::unordered_set<const ConstantExpression*>* stack) const; |
| 99 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 100 | /* Returns true iff the value has already been evaluated. */ |
| 101 | bool isEvaluated() const; |
Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 102 | /* Evaluated result in a string form with comment if applicable. */ |
Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 103 | std::string value() const; |
Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 104 | /* Evaluated result in a string form with comment if applicable. */ |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 105 | std::string cppValue() const; |
Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 106 | /* Evaluated result in a string form with comment if applicable. */ |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 107 | std::string javaValue() const; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 108 | /* Evaluated result in a string form, with given contextual kind. */ |
Yifan Hong | c07b202 | 2016-11-08 12:44:24 -0800 | [diff] [blame] | 109 | std::string value(ScalarType::Kind castKind) const; |
| 110 | /* Evaluated result in a string form, with given contextual kind. */ |
Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 111 | std::string cppValue(ScalarType::Kind castKind) const; |
Yifan Hong | 19ca75a | 2016-08-31 10:20:03 -0700 | [diff] [blame] | 112 | /* Evaluated result in a string form, with given contextual kind. */ |
Yifan Hong | fc610cd | 2016-09-22 13:34:45 -0700 | [diff] [blame] | 113 | std::string javaValue(ScalarType::Kind castKind) const; |
Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 114 | |
| 115 | /* The expression representing this value for use in comments when the value is not needed */ |
| 116 | const std::string& expression() const; |
| 117 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 118 | /* Return a ConstantExpression that is 1 plus the original. */ |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 119 | std::unique_ptr<ConstantExpression> addOne(ScalarType::Kind baseKind); |
Yifan Hong | e77ca13 | 2016-09-27 10:49:05 -0700 | [diff] [blame] | 120 | |
| 121 | size_t castSizeT() const; |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 122 | |
Timur Iskhakov | 35930c4 | 2017-08-28 18:49:54 -0700 | [diff] [blame] | 123 | // Marks that package proceeding is completed |
| 124 | // Post parse passes must be proceeded during owner package parsin |
| 125 | void setPostParseCompleted(); |
| 126 | |
Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 127 | /* |
| 128 | * Helper function for all cpp/javaValue methods. |
| 129 | * Returns a plain string (without any prefixes or suffixes, just the |
| 130 | * digits) converted from mValue. |
| 131 | */ |
| 132 | std::string rawValue() const; |
| 133 | std::string rawValue(ScalarType::Kind castKind) const; |
| 134 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 135 | private: |
| 136 | /* If the result value has been evaluated. */ |
| 137 | bool mIsEvaluated = false; |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 138 | /* The formatted expression. */ |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 139 | std::string mExpr; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 140 | /* The kind of the result value. */ |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 141 | ScalarType::Kind mValueKind; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 142 | /* The stored result value. */ |
Yifan Hong | 5788697 | 2016-08-17 10:42:15 -0700 | [diff] [blame] | 143 | uint64_t mValue; |
Yifan Hong | 5706a43 | 2016-11-02 09:44:18 -0700 | [diff] [blame] | 144 | /* true if description() does not offer more information than value(). */ |
| 145 | bool mTrivialDescription = false; |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 146 | |
Timur Iskhakov | 35930c4 | 2017-08-28 18:49:54 -0700 | [diff] [blame] | 147 | bool mIsPostParseCompleted = false; |
| 148 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 149 | /* |
Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 150 | * Helper function, gives suffix comment to add to value/cppValue/javaValue |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 151 | */ |
Steven Moreland | f21962d | 2018-08-09 12:44:40 -0700 | [diff] [blame] | 152 | std::string descriptionSuffix() const; |
Yifan Hong | e77ca13 | 2016-09-27 10:49:05 -0700 | [diff] [blame] | 153 | |
| 154 | /* |
| 155 | * Return the value casted to the given type. |
| 156 | * First cast it according to mValueKind, then cast it to T. |
| 157 | * Assumes !containsIdentifiers() |
| 158 | */ |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 159 | template <typename T> |
| 160 | T cast() const; |
| 161 | |
| 162 | friend struct LiteralConstantExpression; |
| 163 | friend struct UnaryConstantExpression; |
| 164 | friend struct BinaryConstantExpression; |
| 165 | friend struct TernaryConstantExpression; |
| 166 | friend struct ReferenceConstantExpression; |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 167 | friend struct AttributeConstantExpression; |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | struct LiteralConstantExpression : public ConstantExpression { |
| 171 | LiteralConstantExpression(ScalarType::Kind kind, uint64_t value); |
Steven Moreland | 7794369 | 2018-08-09 12:53:42 -0700 | [diff] [blame] | 172 | LiteralConstantExpression(ScalarType::Kind kind, uint64_t value, const std::string& expr); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 173 | void evaluate() override; |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 174 | std::vector<const ConstantExpression*> getConstantExpressions() const override; |
Steven Moreland | d9d6dcb | 2017-09-20 15:55:39 -0700 | [diff] [blame] | 175 | |
| 176 | static LiteralConstantExpression* tryParse(const std::string& value); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | struct UnaryConstantExpression : public ConstantExpression { |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 180 | UnaryConstantExpression(const std::string& mOp, ConstantExpression* value); |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 181 | void evaluate() override; |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 182 | std::vector<const ConstantExpression*> getConstantExpressions() const override; |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 183 | |
| 184 | private: |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 185 | ConstantExpression* const mUnary; |
| 186 | std::string mOp; |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | struct BinaryConstantExpression : public ConstantExpression { |
| 190 | BinaryConstantExpression(ConstantExpression* lval, const std::string& op, |
| 191 | ConstantExpression* rval); |
| 192 | void evaluate() override; |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 193 | std::vector<const ConstantExpression*> getConstantExpressions() const override; |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 194 | |
| 195 | private: |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 196 | ConstantExpression* const mLval; |
| 197 | ConstantExpression* const mRval; |
| 198 | const std::string mOp; |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | struct TernaryConstantExpression : public ConstantExpression { |
| 202 | TernaryConstantExpression(ConstantExpression* cond, ConstantExpression* trueVal, |
| 203 | ConstantExpression* falseVal); |
| 204 | void evaluate() override; |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 205 | std::vector<const ConstantExpression*> getConstantExpressions() const override; |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 206 | |
| 207 | private: |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 208 | ConstantExpression* const mCond; |
| 209 | ConstantExpression* const mTrueVal; |
| 210 | ConstantExpression* const mFalseVal; |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | struct ReferenceConstantExpression : public ConstantExpression { |
| 214 | ReferenceConstantExpression(const Reference<LocalIdentifier>& value, const std::string& expr); |
Timur Iskhakov | a6d3388 | 2017-09-01 13:02:09 -0700 | [diff] [blame] | 215 | |
| 216 | bool isReferenceConstantExpression() const override; |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 217 | void evaluate() override; |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 218 | std::vector<const ConstantExpression*> getConstantExpressions() const override; |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 219 | std::vector<const Reference<LocalIdentifier>*> getReferences() const override; |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 220 | |
| 221 | private: |
Timur Iskhakov | d27580c | 2017-08-09 20:14:52 -0700 | [diff] [blame] | 222 | Reference<LocalIdentifier> mReference; |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 223 | }; |
| 224 | |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 225 | // This constant expression is a compile-time calculatable expression based on another type |
| 226 | struct AttributeConstantExpression : public ConstantExpression { |
| 227 | AttributeConstantExpression(const Reference<Type>& value, const std::string& fqname, |
| 228 | const std::string& tag); |
| 229 | |
| 230 | status_t validate() const override; |
| 231 | void evaluate() override; |
| 232 | |
| 233 | std::vector<const ConstantExpression*> getConstantExpressions() const override; |
| 234 | std::vector<const Reference<Type>*> getTypeReferences() const override; |
| 235 | |
| 236 | private: |
| 237 | Reference<Type> mReference; |
| 238 | const std::string mTag; |
| 239 | }; |
| 240 | |
Yifan Hong | 5216569 | 2016-08-12 18:06:40 -0700 | [diff] [blame] | 241 | } // namespace android |
| 242 | |
| 243 | #endif // CONSTANT_EXPRESSION_H_ |