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>
22 #include <hidl-util/StringHelper.h>
23 #include <string>
24 
25 namespace android {
26 
27 struct AST;
28 struct Define;
29 
30 struct Expression {
ExpressionExpression31     Expression() {}
~ExpressionExpression32     virtual ~Expression() {}
33 
34     enum Type {
35         S32 = 0, // 0b00
36         S64 = 1, // 0b01
37         U32 = 2, // 0b10
38         U64 = 3, // 0b11
39         UNKNOWN = -1
40     };
41 
getTypeDescriptionExpression42     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";
48             case UNKNOWN:
49             default:
50                 return "UNKNOWN";
51         }
52     }
53 
getTypeNameExpression54     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";
60             case UNKNOWN:
61             default:
62                 return "/* UNKNOWN */";
63         }
64     }
65 
66     static Type integralType(const std::string& integer);
67     static Type coalesceTypes(Type lhs, Type rhs);
68 
69     static Expression *parenthesize(Expression *inner);
70     static Expression *atom(Type type, const std::string &value, bool isId = false);
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;
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;
83 
84 private:
85 
86     DISALLOW_COPY_AND_ASSIGN(Expression);
87 };
88 
89 }  // namespace android
90 
91 #endif  // EXPRESSION_H_
92