blob: 9f0483768dc71a7fd6fb25b7359d26fed825e933 [file] [log] [blame]
Steven Morelandf1a35f72016-08-17 08:41:49 -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
17#ifndef EXPRESSION_H_
18#define EXPRESSION_H_
19
20#include <android-base/macros.h>
21#include <android-base/logging.h>
Yifan Hong42009032016-09-29 09:38:15 -070022#include <hidl-util/StringHelper.h>
Steven Morelandf1a35f72016-08-17 08:41:49 -070023#include <string>
24
25namespace android {
26
27struct AST;
28struct Define;
29
30struct 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 Hongb33b5ee2016-09-29 13:48:54 -070039 UNKNOWN = -1
Steven Morelandf1a35f72016-08-17 08:41:49 -070040 };
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 Hongb33b5ee2016-09-29 13:48:54 -070048 case UNKNOWN:
Steven Morelandf1a35f72016-08-17 08:41:49 -070049 default:
Yifan Hongb33b5ee2016-09-29 13:48:54 -070050 return "UNKNOWN";
Steven Morelandf1a35f72016-08-17 08:41:49 -070051 }
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 Hongb33b5ee2016-09-29 13:48:54 -070060 case UNKNOWN:
Steven Morelandf1a35f72016-08-17 08:41:49 -070061 default:
Yifan Hongb33b5ee2016-09-29 13:48:54 -070062 return "/* UNKNOWN */";
Steven Morelandf1a35f72016-08-17 08:41:49 -070063 }
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 Hong42009032016-09-29 09:38:15 -070070 static Expression *atom(Type type, const std::string &value, bool isId = false);
Steven Morelandf1a35f72016-08-17 08:41:49 -070071 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 Hong42009032016-09-29 09:38:15 -070078
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 Morelandf1a35f72016-08-17 08:41:49 -070083
84private:
85
86 DISALLOW_COPY_AND_ASSIGN(Expression);
87};
88
89} // namespace android
90
Yifan Hong42009032016-09-29 09:38:15 -070091#endif // EXPRESSION_H_