Steven Moreland | f1a35f7 | 2016-08-17 08:41:49 -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 | |
| 17 | #ifndef EXPRESSION_H_ |
| 18 | #define EXPRESSION_H_ |
| 19 | |
| 20 | #include <android-base/macros.h> |
| 21 | #include <android-base/logging.h> |
Yifan Hong | 4200903 | 2016-09-29 09:38:15 -0700 | [diff] [blame] | 22 | #include <hidl-util/StringHelper.h> |
Steven Moreland | f1a35f7 | 2016-08-17 08:41:49 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | struct AST; |
| 28 | struct Define; |
| 29 | |
| 30 | struct Expression { |
| 31 | Expression() {} |
| 32 | virtual ~Expression() {} |
| 33 | |
| 34 | enum Type { |
| 35 | S32 = 0, // 0b00 |
| 36 | S64 = 1, // 0b01 |
| 37 | U32 = 2, // 0b10 |
| 38 | U64 = 3, // 0b11 |
Yifan Hong | b33b5ee | 2016-09-29 13:48:54 -0700 | [diff] [blame] | 39 | UNKNOWN = -1 |
Steven Moreland | f1a35f7 | 2016-08-17 08:41:49 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static std::string getTypeDescription(Type type) { |
| 43 | switch (type) { |
| 44 | case S32: return "S32"; |
| 45 | case S64: return "S64"; |
| 46 | case U32: return "U32"; |
| 47 | case U64: return "U64"; |
Yifan Hong | b33b5ee | 2016-09-29 13:48:54 -0700 | [diff] [blame] | 48 | case UNKNOWN: |
Steven Moreland | f1a35f7 | 2016-08-17 08:41:49 -0700 | [diff] [blame] | 49 | default: |
Yifan Hong | b33b5ee | 2016-09-29 13:48:54 -0700 | [diff] [blame] | 50 | return "UNKNOWN"; |
Steven Moreland | f1a35f7 | 2016-08-17 08:41:49 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | static std::string getTypeName(Type type) { |
| 55 | switch (type) { |
| 56 | case S32: return "int32_t"; |
| 57 | case S64: return "int64_t"; |
| 58 | case U32: return "uint32_t"; |
| 59 | case U64: return "uint64_t"; |
Yifan Hong | b33b5ee | 2016-09-29 13:48:54 -0700 | [diff] [blame] | 60 | case UNKNOWN: |
Steven Moreland | f1a35f7 | 2016-08-17 08:41:49 -0700 | [diff] [blame] | 61 | default: |
Yifan Hong | b33b5ee | 2016-09-29 13:48:54 -0700 | [diff] [blame] | 62 | return "/* UNKNOWN */"; |
Steven Moreland | f1a35f7 | 2016-08-17 08:41:49 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | static Type integralType(std::string integer); |
| 67 | static Type coalesceTypes(Type lhs, Type rhs); |
| 68 | |
| 69 | static Expression *parenthesize(Expression *inner); |
Yifan Hong | 4200903 | 2016-09-29 09:38:15 -0700 | [diff] [blame] | 70 | static Expression *atom(Type type, const std::string &value, bool isId = false); |
Steven Moreland | f1a35f7 | 2016-08-17 08:41:49 -0700 | [diff] [blame] | 71 | static Expression *unary(std::string op, Expression *rhs); |
| 72 | static Expression *binary(Expression *lhs, std::string op, Expression *rhs); |
| 73 | static Expression *ternary(Expression *lhs, Expression *mhs, Expression *rhs); |
| 74 | static Expression *arraySubscript(std::string id, Expression *subscript); |
| 75 | static Expression *functionCall(std::string id, std::vector<Expression *> *args); |
| 76 | |
| 77 | virtual Type getType(const AST &scope) = 0; |
Yifan Hong | 4200903 | 2016-09-29 09:38:15 -0700 | [diff] [blame] | 78 | |
| 79 | // convert this expression to a string. |
| 80 | // atomCase: when it comes to atoms, force identifiers into a certain case. |
| 81 | // numerical values are not affected. |
| 82 | virtual std::string toString(StringHelper::Case atomCase = StringHelper::kNoCase) = 0; |
Steven Moreland | f1a35f7 | 2016-08-17 08:41:49 -0700 | [diff] [blame] | 83 | |
| 84 | private: |
| 85 | |
| 86 | DISALLOW_COPY_AND_ASSIGN(Expression); |
| 87 | }; |
| 88 | |
| 89 | } // namespace android |
| 90 | |
Yifan Hong | 4200903 | 2016-09-29 09:38:15 -0700 | [diff] [blame] | 91 | #endif // EXPRESSION_H_ |