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 #include <ustring.h> 24 25 namespace panda::es2panda::compiler { 26 class PandaGen; 27 } // namespace panda::es2panda::compiler 28 29 namespace panda::es2panda::checker { 30 class Checker; 31 class Type; 32 } // namespace panda::es2panda::checker 33 34 namespace panda::es2panda::ir { 35 class Expression; 36 37 class Annotation : public Statement { 38 public: Annotation(Expression * expr)39 explicit Annotation(Expression *expr) : Statement(AstNodeType::ANNOTATION), expr_(expr) 40 { 41 if (expr->IsCallExpression()) { 42 expr = expr->AsCallExpression()->Callee(); 43 } 44 45 while (expr->IsMemberExpression()) { 46 name_.insert(0, expr->AsMemberExpression()->Property()->AsIdentifier()->Name().Utf8()); 47 name_.insert(0, "."); 48 expr = expr->AsMemberExpression()->Object(); 49 } 50 name_.insert(0, expr->AsIdentifier()->Name().Utf8()); 51 nameView_ = util::StringView(name_); 52 } 53 Expr()54 const Expression *Expr() const 55 { 56 return expr_; 57 } 58 Expr()59 Expression *Expr() 60 { 61 return expr_; 62 } 63 Name()64 const util::StringView &Name() const 65 { 66 return nameView_; 67 } 68 IsImported()69 bool IsImported() const 70 { 71 return isImported_; 72 } 73 SetIsImported()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 static constexpr char annotationPrefix[] = "__$$ETS_ANNOTATION$$__"; 87 88 // Conventions with TSC part 89 static constexpr uint8_t EMPTY_LITERAL_ARRAY_WITH_NUMBER_TYPE = 0; 90 static constexpr uint8_t EMPTY_LITERAL_ARRAY_WITH_BOOLEAN_TYPE = 1; 91 static constexpr uint8_t EMPTY_LITERAL_ARRAY_WITH_STRING_TYPE = 2; 92 93 static constexpr uint8_t ENUM_LITERAL_ARRAY_WITHOUT_INITIALIZER_NUMBER_TYPE = 0; 94 static constexpr uint8_t ENUM_LITERAL_ARRAY_WITH_EMPTY_INITIALIZER_NUMBER_TYPE = 1; 95 static constexpr uint8_t ENUM_LITERAL_ARRAY_WITHOUT_INITIALIZER_STRING_TYPE = 2; 96 static constexpr uint8_t ENUM_LITERAL_ARRAY_WITH_EMPTY_INITIALIZER_STRING_TYPE = 3; 97 98 private: 99 Expression *expr_ = nullptr; 100 std::string name_ = ""; 101 util::StringView nameView_; 102 bool isImported_ = false; 103 }; 104 105 } // namespace panda::es2panda::ir 106 107 #endif 108