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_PARSER_INCLUDE_AST_SCRIPT_FUNCTION_H 17 #define ES2PANDA_PARSER_INCLUDE_AST_SCRIPT_FUNCTION_H 18 19 #include <ir/astNode.h> 20 #include <ir/expressions/identifier.h> 21 #include <util/enumbitops.h> 22 23 namespace panda::es2panda::compiler { 24 class PandaGen; 25 } // namespace panda::es2panda::compiler 26 27 namespace panda::es2panda::checker { 28 class Checker; 29 class Type; 30 } // namespace panda::es2panda::checker 31 32 namespace panda::es2panda::binder { 33 class FunctionScope; 34 } // namespace panda::es2panda::binder 35 36 namespace panda::es2panda::ir { 37 38 class TSTypeParameterDeclaration; 39 40 class ScriptFunction : public AstNode { 41 public: ScriptFunction(binder::FunctionScope * scope,ArenaVector<Expression * > && params,TSTypeParameterDeclaration * typeParams,AstNode * body,Expression * returnTypeAnnotation,ir::ScriptFunctionFlags flags,bool declare,bool isTsFunction)42 explicit ScriptFunction(binder::FunctionScope *scope, ArenaVector<Expression *> &¶ms, 43 TSTypeParameterDeclaration *typeParams, AstNode *body, Expression *returnTypeAnnotation, 44 ir::ScriptFunctionFlags flags, bool declare, bool isTsFunction) 45 : AstNode(AstNodeType::SCRIPT_FUNCTION), 46 scope_(scope), 47 id_(nullptr), 48 params_(std::move(params)), 49 typeParams_(typeParams), 50 body_(body), 51 returnTypeAnnotation_(returnTypeAnnotation), 52 flags_(flags), 53 declare_(declare), 54 exportDefault_(false) 55 { 56 thisParam_ = nullptr; 57 if (isTsFunction && !params_.empty()) { 58 auto *firstParam = params_.front(); 59 if (firstParam->IsIdentifier() && firstParam->AsIdentifier()->Name().Is(THIS_PARAM)) { 60 thisParam_ = firstParam; 61 params_.erase(params_.begin()); 62 scope_->ParamScope()->RemoveThisParam(); 63 } 64 } 65 } 66 Id()67 const Identifier *Id() const 68 { 69 return id_; 70 } 71 Id()72 Identifier *Id() 73 { 74 return id_; 75 } 76 Params()77 const ArenaVector<Expression *> &Params() const 78 { 79 return params_; 80 } 81 Params()82 ArenaVector<Expression *> &Params() 83 { 84 return params_; 85 } 86 TypeParams()87 const TSTypeParameterDeclaration *TypeParams() const 88 { 89 return typeParams_; 90 } 91 Body()92 const AstNode *Body() const 93 { 94 return body_; 95 } 96 Body()97 AstNode *Body() 98 { 99 return body_; 100 } 101 ReturnTypeAnnotation()102 const Expression *ReturnTypeAnnotation() const 103 { 104 return returnTypeAnnotation_; 105 } 106 ReturnTypeAnnotation()107 Expression *ReturnTypeAnnotation() 108 { 109 return returnTypeAnnotation_; 110 } 111 IsGenerator()112 bool IsGenerator() const 113 { 114 return (flags_ & ir::ScriptFunctionFlags::GENERATOR) != 0; 115 } 116 IsAsync()117 bool IsAsync() const 118 { 119 return (flags_ & ir::ScriptFunctionFlags::ASYNC) != 0; 120 } 121 IsArrow()122 bool IsArrow() const 123 { 124 return (flags_ & ir::ScriptFunctionFlags::ARROW) != 0; 125 } 126 IsOverload()127 bool IsOverload() const 128 { 129 return (flags_ & ir::ScriptFunctionFlags::OVERLOAD) != 0; 130 } 131 IsConstructor()132 bool IsConstructor() const 133 { 134 return (flags_ & ir::ScriptFunctionFlags::CONSTRUCTOR) != 0; 135 } 136 IsMethod()137 bool IsMethod() const 138 { 139 return (flags_ & ir::ScriptFunctionFlags::METHOD) != 0; 140 } 141 Declare()142 bool Declare() const 143 { 144 return declare_; 145 } 146 SetIdent(Identifier * id)147 void SetIdent(Identifier *id) 148 { 149 id_ = id; 150 } 151 SetAsExportDefault()152 void SetAsExportDefault() 153 { 154 exportDefault_ = true; 155 } 156 AddFlag(ir::ScriptFunctionFlags flags)157 void AddFlag(ir::ScriptFunctionFlags flags) 158 { 159 flags_ |= flags; 160 } 161 162 size_t FormalParamsLength() const; 163 util::StringView GetName() const; 164 Scope()165 binder::FunctionScope *Scope() const 166 { 167 return scope_; 168 } 169 IsConcurrent()170 bool IsConcurrent() const 171 { 172 return (flags_ & ir::ScriptFunctionFlags::CONCURRENT) != 0; 173 } 174 CanBeConcurrent()175 bool CanBeConcurrent() const 176 { 177 return !(IsGenerator() || IsArrow() || IsConstructor() || IsMethod()); 178 } 179 180 void Iterate(const NodeTraverser &cb) const override; 181 void Dump(ir::AstDumper *dumper) const override; 182 void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; 183 checker::Type *Check([[maybe_unused]] checker::Checker *checker) const override; 184 void UpdateSelf(const NodeUpdater &cb, binder::Binder *binder) override; 185 186 private: 187 static constexpr std::string_view THIS_PARAM = "this"; 188 189 binder::FunctionScope *scope_; 190 Identifier *id_; 191 Expression *thisParam_; 192 ArenaVector<Expression *> params_; 193 TSTypeParameterDeclaration *typeParams_; 194 AstNode *body_; 195 Expression *returnTypeAnnotation_; 196 ir::ScriptFunctionFlags flags_; 197 bool declare_; 198 bool exportDefault_; 199 }; 200 201 } // namespace panda::es2panda::ir 202 203 #endif 204