1f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland/*
2f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * Copyright (C) 2016 The Android Open Source Project
3f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland *
4f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * Licensed under the Apache License, Version 2.0 (the "License");
5f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * you may not use this file except in compliance with the License.
6f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * You may obtain a copy of the License at
7f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland *
8f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland *      http://www.apache.org/licenses/LICENSE-2.0
9f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland *
10f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * Unless required by applicable law or agreed to in writing, software
11f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * distributed under the License is distributed on an "AS IS" BASIS,
12f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * See the License for the specific language governing permissions and
14f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland * limitations under the License.
15f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland */
16f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
17f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#include "Expression.h"
18f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#include "Define.h"
19f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#include "AST.h"
20f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#include "Scope.h"
21f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
22f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#include <vector>
23f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#include <regex>
24f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
25f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandnamespace android {
26f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
27f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandstatic const std::regex RE_S32("[^ul]$");
28f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandstatic const std::regex RE_U32("[^ul]u$");
29f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandstatic const std::regex RE_S64("[^ul](l|ll)$");
30f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandstatic const std::regex RE_U64("[^ul](ul|ull)$");
31f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
32f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland// static
33f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven MorelandExpression::Type Expression::integralType(std::string integer) {
34f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    if (std::regex_search(integer, RE_S32)) {
35f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        return Type::S32;
36f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
37f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
38f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    if (std::regex_search(integer, RE_U32)) {
39f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        return Type::U32;
40f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
41f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
42f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    if (std::regex_search(integer, RE_S64)) {
43f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        return Type::S64;
44f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
45f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
46f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    if (std::regex_search(integer, RE_U64)) {
47f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        return Type::U64;
48f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
49f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
50b33b5ee164d891adc9d2b69390b7220cc6bdcc0aYifan Hong    LOG(WARNING) << "UNKNOWN INTEGER LITERAL: " << integer;
51f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
52b33b5ee164d891adc9d2b69390b7220cc6bdcc0aYifan Hong    return Type::UNKNOWN;
53f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland}
54f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
55f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland// static
56f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven MorelandExpression::Type Expression::coalesceTypes(Type lhs, Type rhs) {
57f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    // because we are reducing everything to two ranks, we can heavily simplify
58f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    // conversion rules
59f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
60f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#define SIGNED(i) (i & 2) // i & 0b10
61f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#define MAX_RANK(i) (i | 1) // i | 0b01
62f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
63f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    if (lhs == rhs) {
64f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        return lhs;
65f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
66f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
67f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    // lhs != rhs
68f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    if (SIGNED(lhs) == SIGNED(rhs)) {
69f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        return (Type)MAX_RANK(lhs);
70f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
71f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
72f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    // lhs != rhs && SIGNED(lhs) != SIGNED(rhs)
73f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    if (lhs == U32 || rhs == U32) {
74f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        return S64;
75f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
76f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
77b33b5ee164d891adc9d2b69390b7220cc6bdcc0aYifan Hong    return Type::UNKNOWN;
78f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
79f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#undef SIGNED
80f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland#undef MAX_RANK
81f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
82f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland}
83f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
84f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandstruct ParenthesizedExpression : Expression {
85f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    ParenthesizedExpression(Expression* inner)
86f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    : mInner(inner) {}
87f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    ~ParenthesizedExpression() {
88f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        delete mInner;
89f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
90f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
91f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    virtual Type getType(const AST &ast) {
92f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        return mInner->getType(ast);
93f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
944200903983e12253942381e2f9331479848c0b94Yifan Hong    virtual std::string toString(StringHelper::Case atomCase) {
954200903983e12253942381e2f9331479848c0b94Yifan Hong        return "(" + mInner->toString(atomCase) + ")";
96f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
97f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
98f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandprivate:
99f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    Expression* mInner;
100f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
101f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    DISALLOW_COPY_AND_ASSIGN(ParenthesizedExpression);
102f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland};
103f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
104f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandstruct AtomExpression : Expression {
1054200903983e12253942381e2f9331479848c0b94Yifan Hong    AtomExpression(Type type, const std::string &value, bool isId)
1064200903983e12253942381e2f9331479848c0b94Yifan Hong    : mType(type), mValue(value), mIsId(isId)
107f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    {}
108f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
109f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    virtual Type getType(const AST &ast) {
110b33b5ee164d891adc9d2b69390b7220cc6bdcc0aYifan Hong        if (mType != Type::UNKNOWN) {
11195b46238e9f0420e2e79ce2bda2cabc24aff4769Steven Moreland            return mType;
112f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        }
113f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
114f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        Define *define = ast.getDefinesScope().lookup(mValue);
115f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
116f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        if (define == NULL) {
117b33b5ee164d891adc9d2b69390b7220cc6bdcc0aYifan Hong            return Type::UNKNOWN;
118f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        }
119f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
120f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        return define->getExpressionType();
121f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
1224200903983e12253942381e2f9331479848c0b94Yifan Hong    virtual std::string toString(StringHelper::Case atomCase) {
1234200903983e12253942381e2f9331479848c0b94Yifan Hong        // do not enforce case if it is not an identifier.
1244200903983e12253942381e2f9331479848c0b94Yifan Hong        return mIsId ? StringHelper::ToCase(atomCase, mValue) : mValue;
125f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
126f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
127f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandprivate:
128f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    Type mType;
129f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    std::string mValue;
1304200903983e12253942381e2f9331479848c0b94Yifan Hong    bool mIsId;
131f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
132f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    DISALLOW_COPY_AND_ASSIGN(AtomExpression);
133f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland};
134f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
135f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandstruct UnaryExpression : Expression {
136f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    UnaryExpression(std::string op, Expression* rhs)
137f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    : mOp(op), mRhs(rhs)
138f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    {}
139f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    ~UnaryExpression() {
140f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        delete mRhs;
141f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
142f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
143f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    virtual Type getType(const AST &ast) {
144f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        return mRhs->getType(ast);
145f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
1464200903983e12253942381e2f9331479848c0b94Yifan Hong    virtual std::string toString(StringHelper::Case atomCase) {
1474200903983e12253942381e2f9331479848c0b94Yifan Hong        return mOp + mRhs->toString(atomCase);
148f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
149f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
150f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandprivate:
151f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    std::string mOp;
152f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    Expression* mRhs;
153f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
154f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    DISALLOW_COPY_AND_ASSIGN(UnaryExpression);
155f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland};
156f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
157f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandstruct BinaryExpression : Expression {
158f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    BinaryExpression(Expression *lhs, std::string op, Expression* rhs)
159f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    : mLhs(lhs), mOp(op), mRhs(rhs)
160f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    {}
161f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    ~BinaryExpression() {
162f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        delete mLhs;
163f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        delete mRhs;
164f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
165f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
166f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    virtual Type getType(const AST &ast) {
167f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        return coalesceTypes(mLhs->getType(ast), mRhs->getType(ast));
168f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
1694200903983e12253942381e2f9331479848c0b94Yifan Hong    virtual std::string toString(StringHelper::Case atomCase) {
1704200903983e12253942381e2f9331479848c0b94Yifan Hong        return mLhs->toString(atomCase) + " " + mOp + " " + mRhs->toString(atomCase);
171f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
172f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
173f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandprivate:
174f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    Expression* mLhs;
175f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    std::string mOp;
176f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    Expression* mRhs;
177f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
178f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    DISALLOW_COPY_AND_ASSIGN(BinaryExpression);
179f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland};
180f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
181f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandstruct TernaryExpression : Expression {
182f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    TernaryExpression(Expression *lhs, Expression *mhs, Expression* rhs)
183f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    : mLhs(lhs), mMhs(mhs), mRhs(rhs)
184f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    {}
185f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    ~TernaryExpression() {
186f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        delete mLhs;
187f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        delete mMhs;
188f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        delete mRhs;
189f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
190f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
191f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    virtual Type getType(const AST &ast) {
192f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        return coalesceTypes(mMhs->getType(ast), mRhs->getType(ast));
193f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
1944200903983e12253942381e2f9331479848c0b94Yifan Hong    virtual std::string toString(StringHelper::Case atomCase) {
1954200903983e12253942381e2f9331479848c0b94Yifan Hong        return mLhs->toString(atomCase) + " ? " + mMhs->toString(atomCase) + " : " + mRhs->toString(atomCase);
196f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
197f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
198f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandprivate:
199f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    Expression* mLhs;
200f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    Expression* mMhs;
201f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    Expression* mRhs;
202f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
203f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    DISALLOW_COPY_AND_ASSIGN(TernaryExpression);
204f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland};
205f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
206f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandstruct ArraySubscript : Expression {
207f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    ArraySubscript(std::string id, Expression* subscript)
208f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    : mId(id), mSubscript(subscript)
209f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    {}
210f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    ~ArraySubscript() {
211f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        delete mSubscript;
212f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
213f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
214f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    virtual Type getType(const AST &) {
215b33b5ee164d891adc9d2b69390b7220cc6bdcc0aYifan Hong        return Type::UNKNOWN;
216f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
2174200903983e12253942381e2f9331479848c0b94Yifan Hong    virtual std::string toString(StringHelper::Case atomCase) {
2184200903983e12253942381e2f9331479848c0b94Yifan Hong        return mId + "[" + mSubscript->toString(atomCase) + "]";
219f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
220f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
221f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandprivate:
222f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    std::string mId;
223f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    Expression* mSubscript;
224f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
225f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    DISALLOW_COPY_AND_ASSIGN(ArraySubscript);
226f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland};
227f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
228f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandstruct FunctionCall : Expression {
229f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    FunctionCall(std::string id, std::vector<Expression *> *args)
230f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    : mId(id), mArgs(args)
231f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    {}
232f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    ~FunctionCall() {
233f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        if(mArgs != NULL) {
234f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland            for(auto* args : *mArgs) {
235f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland                delete args;
236f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland            }
237f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        }
238f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        delete mArgs;
239f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
240f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
241f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    virtual Type getType(const AST &) {
242b33b5ee164d891adc9d2b69390b7220cc6bdcc0aYifan Hong        return Type::UNKNOWN;
243f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
2444200903983e12253942381e2f9331479848c0b94Yifan Hong    virtual std::string toString(StringHelper::Case atomCase) {
245f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        std::string out = mId + "(";
246f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
247f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        for (auto it = mArgs->begin(); it != mArgs->end(); ++it) {
248f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland            if (it != mArgs->begin()) {
249f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland                out += ", ";
250f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland            }
251f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
2524200903983e12253942381e2f9331479848c0b94Yifan Hong            out += (*it)->toString(atomCase);
253f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        }
254f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
25501c9b8b403912605b02002c4a1557ad931af1f67Yifan Hong        out += ")";
25601c9b8b403912605b02002c4a1557ad931af1f67Yifan Hong
257f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland        return out;
258f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    }
259f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
260f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Morelandprivate:
261f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    std::string mId;
262f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    std::vector<Expression *> *mArgs;
263f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
264f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    DISALLOW_COPY_AND_ASSIGN(FunctionCall);
265f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland};
266f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
267f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland// static
268f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven MorelandExpression *Expression::parenthesize(Expression *inner) {
269f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    return new ParenthesizedExpression(inner);
270f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland}
271f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
272f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland// static
2734200903983e12253942381e2f9331479848c0b94Yifan HongExpression *Expression::atom(Type type, const std::string &value, bool isId) {
2744200903983e12253942381e2f9331479848c0b94Yifan Hong    return new AtomExpression(type, value, isId);
275f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland}
276f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
277f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland// static
278f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven MorelandExpression *Expression::unary(std::string op, Expression *rhs) {
279f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    return new UnaryExpression(op, rhs);
280f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland}
281f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
282f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland// static
283f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven MorelandExpression *Expression::binary(Expression *lhs, std::string op, Expression *rhs) {
284f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    return new BinaryExpression(lhs, op, rhs);
285f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland}
286f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
287f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland// static
288f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven MorelandExpression *Expression::ternary(Expression *lhs, Expression *mhs, Expression *rhs) {
289f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    return new TernaryExpression(lhs, mhs, rhs);
290f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland}
291f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
292f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland// static
293f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven MorelandExpression *Expression::arraySubscript(std::string id, Expression *subscript) {
294f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    return new ArraySubscript(id, subscript);
295f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland}
296f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
297f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland// static
298f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven MorelandExpression *Expression::functionCall(std::string id, std::vector<Expression *> *args) {
299f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland    return new FunctionCall(id, args);
300f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland}
301f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
302f1a35f7d73f38e0d30d7a4343e33c3f4777cf87eSteven Moreland
30301c9b8b403912605b02002c4a1557ad931af1f67Yifan Hong} //namespace android
304