blob: 47760962896161ae8ab9f38835c59bcdd1fbcf9e [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#include "Expression.h"
18#include "Define.h"
19#include "AST.h"
20#include "Scope.h"
21
22#include <vector>
23#include <regex>
24
25namespace android {
26
27static const std::regex RE_S32("[^ul]$");
28static const std::regex RE_U32("[^ul]u$");
29static const std::regex RE_S64("[^ul](l|ll)$");
30static const std::regex RE_U64("[^ul](ul|ull)$");
31
32// static
33Expression::Type Expression::integralType(std::string integer) {
34 if (std::regex_search(integer, RE_S32)) {
35 return Type::S32;
36 }
37
38 if (std::regex_search(integer, RE_U32)) {
39 return Type::U32;
40 }
41
42 if (std::regex_search(integer, RE_S64)) {
43 return Type::S64;
44 }
45
46 if (std::regex_search(integer, RE_U64)) {
47 return Type::U64;
48 }
49
Yifan Hongb33b5ee2016-09-29 13:48:54 -070050 LOG(WARNING) << "UNKNOWN INTEGER LITERAL: " << integer;
Steven Morelandf1a35f72016-08-17 08:41:49 -070051
Yifan Hongb33b5ee2016-09-29 13:48:54 -070052 return Type::UNKNOWN;
Steven Morelandf1a35f72016-08-17 08:41:49 -070053}
54
55// static
56Expression::Type Expression::coalesceTypes(Type lhs, Type rhs) {
57 // because we are reducing everything to two ranks, we can heavily simplify
58 // conversion rules
59
60#define SIGNED(i) (i & 2) // i & 0b10
61#define MAX_RANK(i) (i | 1) // i | 0b01
62
63 if (lhs == rhs) {
64 return lhs;
65 }
66
67 // lhs != rhs
68 if (SIGNED(lhs) == SIGNED(rhs)) {
69 return (Type)MAX_RANK(lhs);
70 }
71
72 // lhs != rhs && SIGNED(lhs) != SIGNED(rhs)
73 if (lhs == U32 || rhs == U32) {
74 return S64;
75 }
76
Yifan Hongb33b5ee2016-09-29 13:48:54 -070077 return Type::UNKNOWN;
Steven Morelandf1a35f72016-08-17 08:41:49 -070078
79#undef SIGNED
80#undef MAX_RANK
81
82}
83
84struct ParenthesizedExpression : Expression {
85 ParenthesizedExpression(Expression* inner)
86 : mInner(inner) {}
87 ~ParenthesizedExpression() {
88 delete mInner;
89 }
90
91 virtual Type getType(const AST &ast) {
92 return mInner->getType(ast);
93 }
Yifan Hong42009032016-09-29 09:38:15 -070094 virtual std::string toString(StringHelper::Case atomCase) {
95 return "(" + mInner->toString(atomCase) + ")";
Steven Morelandf1a35f72016-08-17 08:41:49 -070096 }
97
98private:
99 Expression* mInner;
100
101 DISALLOW_COPY_AND_ASSIGN(ParenthesizedExpression);
102};
103
104struct AtomExpression : Expression {
Yifan Hong42009032016-09-29 09:38:15 -0700105 AtomExpression(Type type, const std::string &value, bool isId)
106 : mType(type), mValue(value), mIsId(isId)
Steven Morelandf1a35f72016-08-17 08:41:49 -0700107 {}
108
109 virtual Type getType(const AST &ast) {
Yifan Hongb33b5ee2016-09-29 13:48:54 -0700110 if (mType != Type::UNKNOWN) {
Steven Moreland95b46232016-09-20 14:46:55 -0700111 return mType;
Steven Morelandf1a35f72016-08-17 08:41:49 -0700112 }
113
114 Define *define = ast.getDefinesScope().lookup(mValue);
115
116 if (define == NULL) {
Yifan Hongb33b5ee2016-09-29 13:48:54 -0700117 return Type::UNKNOWN;
Steven Morelandf1a35f72016-08-17 08:41:49 -0700118 }
119
120 return define->getExpressionType();
121 }
Yifan Hong42009032016-09-29 09:38:15 -0700122 virtual std::string toString(StringHelper::Case atomCase) {
123 // do not enforce case if it is not an identifier.
124 return mIsId ? StringHelper::ToCase(atomCase, mValue) : mValue;
Steven Morelandf1a35f72016-08-17 08:41:49 -0700125 }
126
127private:
128 Type mType;
129 std::string mValue;
Yifan Hong42009032016-09-29 09:38:15 -0700130 bool mIsId;
Steven Morelandf1a35f72016-08-17 08:41:49 -0700131
132 DISALLOW_COPY_AND_ASSIGN(AtomExpression);
133};
134
135struct UnaryExpression : Expression {
136 UnaryExpression(std::string op, Expression* rhs)
137 : mOp(op), mRhs(rhs)
138 {}
139 ~UnaryExpression() {
140 delete mRhs;
141 }
142
143 virtual Type getType(const AST &ast) {
144 return mRhs->getType(ast);
145 }
Yifan Hong42009032016-09-29 09:38:15 -0700146 virtual std::string toString(StringHelper::Case atomCase) {
147 return mOp + mRhs->toString(atomCase);
Steven Morelandf1a35f72016-08-17 08:41:49 -0700148 }
149
150private:
151 std::string mOp;
152 Expression* mRhs;
153
154 DISALLOW_COPY_AND_ASSIGN(UnaryExpression);
155};
156
157struct BinaryExpression : Expression {
158 BinaryExpression(Expression *lhs, std::string op, Expression* rhs)
159 : mLhs(lhs), mOp(op), mRhs(rhs)
160 {}
161 ~BinaryExpression() {
162 delete mLhs;
163 delete mRhs;
164 }
165
166 virtual Type getType(const AST &ast) {
167 return coalesceTypes(mLhs->getType(ast), mRhs->getType(ast));
168 }
Yifan Hong42009032016-09-29 09:38:15 -0700169 virtual std::string toString(StringHelper::Case atomCase) {
170 return mLhs->toString(atomCase) + " " + mOp + " " + mRhs->toString(atomCase);
Steven Morelandf1a35f72016-08-17 08:41:49 -0700171 }
172
173private:
174 Expression* mLhs;
175 std::string mOp;
176 Expression* mRhs;
177
178 DISALLOW_COPY_AND_ASSIGN(BinaryExpression);
179};
180
181struct TernaryExpression : Expression {
182 TernaryExpression(Expression *lhs, Expression *mhs, Expression* rhs)
183 : mLhs(lhs), mMhs(mhs), mRhs(rhs)
184 {}
185 ~TernaryExpression() {
186 delete mLhs;
187 delete mMhs;
188 delete mRhs;
189 }
190
191 virtual Type getType(const AST &ast) {
192 return coalesceTypes(mMhs->getType(ast), mRhs->getType(ast));
193 }
Yifan Hong42009032016-09-29 09:38:15 -0700194 virtual std::string toString(StringHelper::Case atomCase) {
195 return mLhs->toString(atomCase) + " ? " + mMhs->toString(atomCase) + " : " + mRhs->toString(atomCase);
Steven Morelandf1a35f72016-08-17 08:41:49 -0700196 }
197
198private:
199 Expression* mLhs;
200 Expression* mMhs;
201 Expression* mRhs;
202
203 DISALLOW_COPY_AND_ASSIGN(TernaryExpression);
204};
205
206struct ArraySubscript : Expression {
207 ArraySubscript(std::string id, Expression* subscript)
208 : mId(id), mSubscript(subscript)
209 {}
210 ~ArraySubscript() {
211 delete mSubscript;
212 }
213
214 virtual Type getType(const AST &) {
Yifan Hongb33b5ee2016-09-29 13:48:54 -0700215 return Type::UNKNOWN;
Steven Morelandf1a35f72016-08-17 08:41:49 -0700216 }
Yifan Hong42009032016-09-29 09:38:15 -0700217 virtual std::string toString(StringHelper::Case atomCase) {
218 return mId + "[" + mSubscript->toString(atomCase) + "]";
Steven Morelandf1a35f72016-08-17 08:41:49 -0700219 }
220
221private:
222 std::string mId;
223 Expression* mSubscript;
224
225 DISALLOW_COPY_AND_ASSIGN(ArraySubscript);
226};
227
228struct FunctionCall : Expression {
229 FunctionCall(std::string id, std::vector<Expression *> *args)
230 : mId(id), mArgs(args)
231 {}
232 ~FunctionCall() {
233 if(mArgs != NULL) {
234 for(auto* args : *mArgs) {
235 delete args;
236 }
237 }
238 delete mArgs;
239 }
240
241 virtual Type getType(const AST &) {
Yifan Hongb33b5ee2016-09-29 13:48:54 -0700242 return Type::UNKNOWN;
Steven Morelandf1a35f72016-08-17 08:41:49 -0700243 }
Yifan Hong42009032016-09-29 09:38:15 -0700244 virtual std::string toString(StringHelper::Case atomCase) {
Steven Morelandf1a35f72016-08-17 08:41:49 -0700245 std::string out = mId + "(";
246
247 for (auto it = mArgs->begin(); it != mArgs->end(); ++it) {
248 if (it != mArgs->begin()) {
249 out += ", ";
250 }
251
Yifan Hong42009032016-09-29 09:38:15 -0700252 out += (*it)->toString(atomCase);
Steven Morelandf1a35f72016-08-17 08:41:49 -0700253 }
254
Yifan Hong01c9b8b2016-09-28 17:20:35 -0700255 out += ")";
256
Steven Morelandf1a35f72016-08-17 08:41:49 -0700257 return out;
258 }
259
260private:
261 std::string mId;
262 std::vector<Expression *> *mArgs;
263
264 DISALLOW_COPY_AND_ASSIGN(FunctionCall);
265};
266
267// static
268Expression *Expression::parenthesize(Expression *inner) {
269 return new ParenthesizedExpression(inner);
270}
271
272// static
Yifan Hong42009032016-09-29 09:38:15 -0700273Expression *Expression::atom(Type type, const std::string &value, bool isId) {
274 return new AtomExpression(type, value, isId);
Steven Morelandf1a35f72016-08-17 08:41:49 -0700275}
276
277// static
278Expression *Expression::unary(std::string op, Expression *rhs) {
279 return new UnaryExpression(op, rhs);
280}
281
282// static
283Expression *Expression::binary(Expression *lhs, std::string op, Expression *rhs) {
284 return new BinaryExpression(lhs, op, rhs);
285}
286
287// static
288Expression *Expression::ternary(Expression *lhs, Expression *mhs, Expression *rhs) {
289 return new TernaryExpression(lhs, mhs, rhs);
290}
291
292// static
293Expression *Expression::arraySubscript(std::string id, Expression *subscript) {
294 return new ArraySubscript(id, subscript);
295}
296
297// static
298Expression *Expression::functionCall(std::string id, std::vector<Expression *> *args) {
299 return new FunctionCall(id, args);
300}
301
302
Yifan Hong01c9b8b2016-09-28 17:20:35 -0700303} //namespace android