blob: 2c955d4ff19e757e70eb69db7f54d04d91dbf834 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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 Hong52165692016-08-12 18:06:40 -070017#ifndef CONSTANT_EXPRESSION_H_
18
19#define CONSTANT_EXPRESSION_H_
20
21#include <android-base/macros.h>
22#include <string>
Yifan Hong57886972016-08-17 10:42:15 -070023#include "ScalarType.h"
Yifan Hong52165692016-08-12 18:06:40 -070024
25namespace android {
26
Yifan Hong52165692016-08-12 18:06:40 -070027/**
28 * A constant expression is represented by a tree.
29 */
30struct ConstantExpression {
Yifan Hong57886972016-08-17 10:42:15 -070031
32 enum ConstExprType {
33 kConstExprLiteral,
Yifan Hong57886972016-08-17 10:42:15 -070034 kConstExprUnary,
35 kConstExprBinary,
36 kConstExprTernary
37 };
38
Yifan Hongf24fa852016-09-23 11:03:15 -070039 /* 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 Hong52165692016-08-12 18:06:40 -070047 /* 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 Hongf24fa852016-09-23 11:03:15 -070057 static ConstantExpression Zero(ScalarType::Kind kind);
58 static ConstantExpression One(ScalarType::Kind kind);
Yifan Hong30b5d1f2017-04-03 12:19:25 -070059 static ConstantExpression ValueOf(ScalarType::Kind kind, uint64_t value);
Yifan Hongf24fa852016-09-23 11:03:15 -070060
Yifan Hong57886972016-08-17 10:42:15 -070061 /* Evaluated result in a string form. */
Yifan Hongfc610cd2016-09-22 13:34:45 -070062 std::string value() const;
Yifan Hongf24fa852016-09-23 11:03:15 -070063 /* Evaluated result in a string form. */
64 std::string cppValue() const;
65 /* Evaluated result in a string form. */
66 std::string javaValue() const;
Yifan Hong57886972016-08-17 10:42:15 -070067 /* Evaluated result in a string form, with given contextual kind. */
Yifan Hongc07b2022016-11-08 12:44:24 -080068 std::string value(ScalarType::Kind castKind) const;
69 /* Evaluated result in a string form, with given contextual kind. */
Yifan Hongfc610cd2016-09-22 13:34:45 -070070 std::string cppValue(ScalarType::Kind castKind) const;
Yifan Hong19ca75a2016-08-31 10:20:03 -070071 /* Evaluated result in a string form, with given contextual kind. */
Yifan Hongfc610cd2016-09-22 13:34:45 -070072 std::string javaValue(ScalarType::Kind castKind) const;
Yifan Hong57886972016-08-17 10:42:15 -070073 /* Original expression with type. */
Yifan Hongf24fa852016-09-23 11:03:15 -070074 const std::string &description() const;
Yifan Hong5706a432016-11-02 09:44:18 -070075 /* See mTrivialDescription */
76 bool descriptionIsTrivial() const;
Yifan Hongf24fa852016-09-23 11:03:15 -070077 /* Return a ConstantExpression that is 1 plus the original. */
78 ConstantExpression addOne() const;
79 /* Assignment operator. */
80 ConstantExpression& operator=(const ConstantExpression& other);
Yifan Honge77ca132016-09-27 10:49:05 -070081
82 size_t castSizeT() const;
Yifan Hong52165692016-08-12 18:06:40 -070083
84private:
Yifan Hong57886972016-08-17 10:42:15 -070085 /* The formatted expression. */
Yifan Hongf24fa852016-09-23 11:03:15 -070086 std::string mExpr;
Yifan Hong57886972016-08-17 10:42:15 -070087 /* The type of the expression. Hints on its original form. */
88 ConstExprType mType;
Yifan Hongf24fa852016-09-23 11:03:15 -070089 /* The kind of the result value. */
Yifan Hong57886972016-08-17 10:42:15 -070090 ScalarType::Kind mValueKind;
Yifan Hongf24fa852016-09-23 11:03:15 -070091 /* The stored result value. */
Yifan Hong57886972016-08-17 10:42:15 -070092 uint64_t mValue;
Yifan Hong5706a432016-11-02 09:44:18 -070093 /* true if description() does not offer more information than value(). */
94 bool mTrivialDescription = false;
Yifan Hong52165692016-08-12 18:06:40 -070095
Yifan Hongf24fa852016-09-23 11:03:15 -070096 /*
97 * Helper function for all cpp/javaValue methods.
98 * Returns a plain string (without any prefixes or suffixes, just the
99 * digits) converted from mValue.
100 */
101 std::string rawValue(ScalarType::Kind castKind) const;
102 /* Trim unnecessary information. Only mValue and mValueKind is kept. */
103 ConstantExpression &toLiteral();
Yifan Honge77ca132016-09-27 10:49:05 -0700104
105 /*
106 * Return the value casted to the given type.
107 * First cast it according to mValueKind, then cast it to T.
108 * Assumes !containsIdentifiers()
109 */
110 template <typename T> T cast() const;
Yifan Hong52165692016-08-12 18:06:40 -0700111};
112
113} // namespace android
114
115#endif // CONSTANT_EXPRESSION_H_