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