1 /* 2 * Copyright (c) 2023-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 ETS2PANDA_TEST_UNIT_NODE_CREATOR_H 17 #define ETS2PANDA_TEST_UNIT_NODE_CREATOR_H 18 19 #include "ir/astNode.h" 20 #include "ir/statements/variableDeclarator.h" 21 #include "ir/statements/variableDeclaration.h" 22 #include "ir/expressions/identifier.h" 23 #include "ir/statements/forUpdateStatement.h" 24 #include "ir/statements/blockStatement.h" 25 #include "ir/statements/whileStatement.h" 26 #include "ir/expressions/literals/booleanLiteral.h" 27 #include "ir/expressions/literals/numberLiteral.h" 28 #include "ir/expressions/binaryExpression.h" 29 #include "ir/expressions/updateExpression.h" 30 31 namespace ark::es2panda::gtests { 32 33 class NodeGenerator { 34 public: NodeGenerator(ArenaAllocator *alloc)35 explicit NodeGenerator(ArenaAllocator *alloc) : alloc_(alloc) {} 36 // x = 1 CreateVarDecl(bool declare, util::StringView name = �)37 ir::VariableDeclaration *CreateVarDecl(bool declare, util::StringView name = "x") 38 { 39 auto varDecl = alloc_->New<ir::VariableDeclarator>(ir::VariableDeclaratorFlag::LET, CreateId(name)); 40 ArenaVector<ir::VariableDeclarator *> tmp {alloc_->Adapter()}; 41 tmp.emplace_back(varDecl); 42 return alloc_->New<ir::VariableDeclaration>(ir::VariableDeclaration::VariableDeclarationKind::LET, alloc_, 43 std::move(tmp), declare); 44 } 45 CreateId(util::StringView x)46 ir::Identifier *CreateId(util::StringView x) 47 { 48 return alloc_->New<ir::Identifier>(x, alloc_); 49 } 50 51 // x = x + 1 CreateIncrement(util::StringView name = �, bool isPrefix = false)52 ir::UpdateExpression *CreateIncrement(util::StringView name = "x", bool isPrefix = false) 53 { 54 return alloc_->New<ir::UpdateExpression>(CreateId(name), lexer::TokenType::PUNCTUATOR_PLUS_PLUS, isPrefix); 55 } 56 57 // x < 10 CreateLessCmpExpr(util::StringView name = �)58 ir::BinaryExpression *CreateLessCmpExpr(util::StringView name = "x") 59 { 60 const int anyLoopLimit = 10; 61 return alloc_->New<ir::BinaryExpression>(CreateId(name), 62 alloc_->New<ir::NumberLiteral>(lexer::Number(anyLoopLimit)), 63 lexer::TokenType::PUNCTUATOR_LESS_THAN); 64 } 65 CreateBlockWithDeclare(util::StringView name = �)66 ir::BlockStatement *CreateBlockWithDeclare(util::StringView name = "x") 67 { 68 auto varDecl = CreateVarDecl(true, name); 69 ArenaVector<ir::Statement *> tmp {alloc_->Adapter()}; 70 tmp.emplace_back(varDecl); 71 auto *newBlock = alloc_->New<ir::BlockStatement>(alloc_, std::move(tmp)); 72 varDecl->SetParent(newBlock); 73 return newBlock; 74 } 75 CreateForUpdate()76 ir::ForUpdateStatement *CreateForUpdate() 77 { 78 return alloc_->New<ir::ForUpdateStatement>(CreateVarDecl(true), CreateLessCmpExpr(), CreateIncrement(), 79 CreateBlockWithDeclare()); 80 } 81 CreateWhile()82 ir::WhileStatement *CreateWhile() 83 { 84 return alloc_->New<ir::WhileStatement>(CreateLessCmpExpr(), CreateBlockWithDeclare()); 85 } 86 87 private: 88 ArenaAllocator *const alloc_; 89 }; 90 } // namespace ark::es2panda::gtests 91 #endif // ETS2PANDA_TEST_UNIT_NODE_CREATOR_H 92