1/**
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef ES2PANDA_IR_EXPRESSION_MEMBER_EXPRESSION_H
17#define ES2PANDA_IR_EXPRESSION_MEMBER_EXPRESSION_H
18
19#include <binder/variable.h>
20#include <ir/expression.h>
21#include <ir/irnode.h>
22
23namespace panda::es2panda::compiler {
24class PandaGen;
25}  // namespace panda::es2panda::compiler
26
27namespace panda::es2panda::checker {
28class Checker;
29class Type;
30}  // namespace panda::es2panda::checker
31
32namespace panda::es2panda::ir {
33
34class MemberExpression : public Expression {
35public:
36    enum class MemberExpressionKind { ELEMENT_ACCESS, PROPERTY_ACCESS };
37
38    explicit MemberExpression(Expression *object, Expression *property, MemberExpressionKind kind,
39                              bool computed, bool optional)
40        : Expression(AstNodeType::MEMBER_EXPRESSION),
41          object_(object),
42          property_(property),
43          kind_(kind),
44          computed_(computed),
45          optional_(optional)
46    {
47    }
48
49    const Expression *Object() const
50    {
51        return object_;
52    }
53
54    Expression *Object()
55    {
56        return object_;
57    }
58
59    const Expression *Property() const
60    {
61        return property_;
62    }
63
64    Expression *Property()
65    {
66        return property_;
67    }
68
69    bool IsComputed() const
70    {
71        return computed_;
72    }
73
74    bool IsOptional() const
75    {
76        return optional_;
77    }
78
79    bool AccessPrivateProperty() const
80    {
81        return property_->IsPrivateIdentifier();
82    }
83
84    MemberExpressionKind Kind() const
85    {
86        return kind_;
87    }
88
89    void Iterate(const NodeTraverser &cb) const override;
90    void Dump(ir::AstDumper *dumper) const override;
91    void Compile(compiler::PandaGen *pg) const override;
92    void Compile(compiler::PandaGen *pg, compiler::VReg objReg) const;
93    void CompileObject(compiler::PandaGen *pg, compiler::VReg dest) const;
94    compiler::Operand CompileKey(compiler::PandaGen *pg) const;
95    checker::Type *Check(checker::Checker *checker) const override;
96    void UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) override;
97
98private:
99    Expression *object_;
100    Expression *property_;
101    MemberExpressionKind kind_;
102    bool computed_;
103    bool optional_;
104};
105
106}  // namespace panda::es2panda::ir
107
108#endif
109