ConstantExpression.h revision e77ca13d32e329fb1e516c27831b082082ef83c3
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 17#ifndef CONSTANT_EXPRESSION_H_ 18 19#define CONSTANT_EXPRESSION_H_ 20 21#include <android-base/macros.h> 22#include <string> 23#include "ScalarType.h" 24 25namespace android { 26 27/** 28 * A constant expression is represented by a tree. 29 */ 30struct ConstantExpression { 31 32 enum ConstExprType { 33 kConstExprLiteral, 34 kConstExprUnary, 35 kConstExprBinary, 36 kConstExprTernary 37 }; 38 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); 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 57 static ConstantExpression Zero(ScalarType::Kind kind); 58 static ConstantExpression One(ScalarType::Kind kind); 59 60 /* Evaluated result in a string form. */ 61 std::string value() const; 62 /* Evaluated result in a string form. */ 63 std::string cppValue() const; 64 /* Evaluated result in a string form. */ 65 std::string javaValue() const; 66 /* Evaluated result in a string form, with given contextual kind. */ 67 std::string cppValue(ScalarType::Kind castKind) const; 68 /* Evaluated result in a string form, with given contextual kind. */ 69 std::string javaValue(ScalarType::Kind castKind) const; 70 /* Original expression with type. */ 71 const std::string &description() const; 72 /* Return a ConstantExpression that is 1 plus the original. */ 73 ConstantExpression addOne() const; 74 /* Assignment operator. */ 75 ConstantExpression& operator=(const ConstantExpression& other); 76 77 size_t castSizeT() const; 78 79private: 80 /* The formatted expression. */ 81 std::string mExpr; 82 /* The type of the expression. Hints on its original form. */ 83 ConstExprType mType; 84 /* The kind of the result value. */ 85 ScalarType::Kind mValueKind; 86 /* The stored result value. */ 87 uint64_t mValue; 88 89 /* 90 * Helper function for all cpp/javaValue methods. 91 * Returns a plain string (without any prefixes or suffixes, just the 92 * digits) converted from mValue. 93 */ 94 std::string rawValue(ScalarType::Kind castKind) const; 95 /* Trim unnecessary information. Only mValue and mValueKind is kept. */ 96 ConstantExpression &toLiteral(); 97 98 /* 99 * Return the value casted to the given type. 100 * First cast it according to mValueKind, then cast it to T. 101 * Assumes !containsIdentifiers() 102 */ 103 template <typename T> T cast() const; 104}; 105 106} // namespace android 107 108#endif // CONSTANT_EXPRESSION_H_ 109