1/**
2 * Copyright (c) 2024 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_PARSER_INCLUDE_AST_ANNOTATION_H
17#define ES2PANDA_PARSER_INCLUDE_AST_ANNOTATION_H
18
19#include <ir/expressions/callExpression.h>
20#include <ir/expressions/identifier.h>
21#include <ir/expressions/memberExpression.h>
22#include <ir/statement.h>
23
24namespace panda::es2panda::compiler {
25class PandaGen;
26}  // namespace panda::es2panda::compiler
27
28namespace panda::es2panda::checker {
29class Checker;
30class Type;
31}  // namespace panda::es2panda::checker
32
33namespace panda::es2panda::ir {
34class Expression;
35
36class Annotation : public Statement {
37public:
38    explicit Annotation(Expression *expr) : Statement(AstNodeType::ANNOTATION), expr_(expr)
39    {
40        if (expr->IsCallExpression()) {
41            expr = expr->AsCallExpression()->Callee();
42        }
43
44        while (expr->IsMemberExpression()) {
45            name_.insert(0, expr->AsMemberExpression()->Property()->AsIdentifier()->Name().Utf8());
46            name_.insert(0, ".");
47            expr = expr->AsMemberExpression()->Object();
48        }
49
50        name_.insert(0, expr->AsIdentifier()->Name().Utf8());
51        nameView_ = util::StringView(name_);
52    }
53
54    const Expression *Expr() const
55    {
56        return expr_;
57    }
58
59    Expression *Expr()
60    {
61        return expr_;
62    }
63
64    const util::StringView &Name() const
65    {
66        return nameView_;
67    }
68
69    bool IsImported() const
70    {
71        return isImported_;
72    }
73
74    void SetIsImported()
75    {
76        isImported_ = true;
77    }
78
79    void Iterate(const NodeTraverser &cb) const override;
80    void Dump(ir::AstDumper *dumper) const override;
81    void Compile([[maybe_unused]] compiler::PandaGen *pg) const override;
82    checker::Type *Check([[maybe_unused]] checker::Checker *checker) const override;
83    void UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) override;
84    static constexpr char interfaceString[] = "interface";
85    static constexpr char stringClassName[] = "panda.String";
86
87    // Conventions with TSC part
88    static constexpr uint8_t EMPTY_LITERAL_ARRAY_WITH_NUMBER_TYPE = 0;
89    static constexpr uint8_t EMPTY_LITERAL_ARRAY_WITH_BOOLEAN_TYPE = 1;
90    static constexpr uint8_t EMPTY_LITERAL_ARRAY_WITH_STRING_TYPE = 2;
91
92    static constexpr uint8_t ENUM_LITERAL_ARRAY_WITHOUT_INITIALIZER_NUMBER_TYPE = 0;
93    static constexpr uint8_t ENUM_LITERAL_ARRAY_WITH_EMPTY_INITIALIZER_NUMBER_TYPE = 1;
94    static constexpr uint8_t ENUM_LITERAL_ARRAY_WITHOUT_INITIALIZER_STRING_TYPE = 2;
95    static constexpr uint8_t ENUM_LITERAL_ARRAY_WITH_EMPTY_INITIALIZER_STRING_TYPE = 3;
96
97private:
98    Expression *expr_ = nullptr;
99    std::string name_ = "";
100    util::StringView nameView_;
101    bool isImported_ = false;
102};
103
104}  // namespace panda::es2panda::ir
105
106#endif
107