13af6ab5fSopenharmony_ci/* 23af6ab5fSopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ci#ifndef ETS2PANDA_TEST_UNIT_NODE_CREATOR_H 173af6ab5fSopenharmony_ci#define ETS2PANDA_TEST_UNIT_NODE_CREATOR_H 183af6ab5fSopenharmony_ci 193af6ab5fSopenharmony_ci#include "ir/astNode.h" 203af6ab5fSopenharmony_ci#include "ir/statements/variableDeclarator.h" 213af6ab5fSopenharmony_ci#include "ir/statements/variableDeclaration.h" 223af6ab5fSopenharmony_ci#include "ir/expressions/identifier.h" 233af6ab5fSopenharmony_ci#include "ir/statements/forUpdateStatement.h" 243af6ab5fSopenharmony_ci#include "ir/statements/blockStatement.h" 253af6ab5fSopenharmony_ci#include "ir/statements/whileStatement.h" 263af6ab5fSopenharmony_ci#include "ir/expressions/literals/booleanLiteral.h" 273af6ab5fSopenharmony_ci#include "ir/expressions/literals/numberLiteral.h" 283af6ab5fSopenharmony_ci#include "ir/expressions/binaryExpression.h" 293af6ab5fSopenharmony_ci#include "ir/expressions/updateExpression.h" 303af6ab5fSopenharmony_ci 313af6ab5fSopenharmony_cinamespace ark::es2panda::gtests { 323af6ab5fSopenharmony_ci 333af6ab5fSopenharmony_ciclass NodeGenerator { 343af6ab5fSopenharmony_cipublic: 353af6ab5fSopenharmony_ci explicit NodeGenerator(ArenaAllocator *alloc) : alloc_(alloc) {} 363af6ab5fSopenharmony_ci // x = 1 373af6ab5fSopenharmony_ci ir::VariableDeclaration *CreateVarDecl(bool declare, util::StringView name = "x") 383af6ab5fSopenharmony_ci { 393af6ab5fSopenharmony_ci auto varDecl = alloc_->New<ir::VariableDeclarator>(ir::VariableDeclaratorFlag::LET, CreateId(name)); 403af6ab5fSopenharmony_ci ArenaVector<ir::VariableDeclarator *> tmp {alloc_->Adapter()}; 413af6ab5fSopenharmony_ci tmp.emplace_back(varDecl); 423af6ab5fSopenharmony_ci return alloc_->New<ir::VariableDeclaration>(ir::VariableDeclaration::VariableDeclarationKind::LET, alloc_, 433af6ab5fSopenharmony_ci std::move(tmp), declare); 443af6ab5fSopenharmony_ci } 453af6ab5fSopenharmony_ci 463af6ab5fSopenharmony_ci ir::Identifier *CreateId(util::StringView x) 473af6ab5fSopenharmony_ci { 483af6ab5fSopenharmony_ci return alloc_->New<ir::Identifier>(x, alloc_); 493af6ab5fSopenharmony_ci } 503af6ab5fSopenharmony_ci 513af6ab5fSopenharmony_ci // x = x + 1 523af6ab5fSopenharmony_ci ir::UpdateExpression *CreateIncrement(util::StringView name = "x", bool isPrefix = false) 533af6ab5fSopenharmony_ci { 543af6ab5fSopenharmony_ci return alloc_->New<ir::UpdateExpression>(CreateId(name), lexer::TokenType::PUNCTUATOR_PLUS_PLUS, isPrefix); 553af6ab5fSopenharmony_ci } 563af6ab5fSopenharmony_ci 573af6ab5fSopenharmony_ci // x < 10 583af6ab5fSopenharmony_ci ir::BinaryExpression *CreateLessCmpExpr(util::StringView name = "x") 593af6ab5fSopenharmony_ci { 603af6ab5fSopenharmony_ci const int anyLoopLimit = 10; 613af6ab5fSopenharmony_ci return alloc_->New<ir::BinaryExpression>(CreateId(name), 623af6ab5fSopenharmony_ci alloc_->New<ir::NumberLiteral>(lexer::Number(anyLoopLimit)), 633af6ab5fSopenharmony_ci lexer::TokenType::PUNCTUATOR_LESS_THAN); 643af6ab5fSopenharmony_ci } 653af6ab5fSopenharmony_ci 663af6ab5fSopenharmony_ci ir::BlockStatement *CreateBlockWithDeclare(util::StringView name = "x") 673af6ab5fSopenharmony_ci { 683af6ab5fSopenharmony_ci auto varDecl = CreateVarDecl(true, name); 693af6ab5fSopenharmony_ci ArenaVector<ir::Statement *> tmp {alloc_->Adapter()}; 703af6ab5fSopenharmony_ci tmp.emplace_back(varDecl); 713af6ab5fSopenharmony_ci auto *newBlock = alloc_->New<ir::BlockStatement>(alloc_, std::move(tmp)); 723af6ab5fSopenharmony_ci varDecl->SetParent(newBlock); 733af6ab5fSopenharmony_ci return newBlock; 743af6ab5fSopenharmony_ci } 753af6ab5fSopenharmony_ci 763af6ab5fSopenharmony_ci ir::ForUpdateStatement *CreateForUpdate() 773af6ab5fSopenharmony_ci { 783af6ab5fSopenharmony_ci return alloc_->New<ir::ForUpdateStatement>(CreateVarDecl(true), CreateLessCmpExpr(), CreateIncrement(), 793af6ab5fSopenharmony_ci CreateBlockWithDeclare()); 803af6ab5fSopenharmony_ci } 813af6ab5fSopenharmony_ci 823af6ab5fSopenharmony_ci ir::WhileStatement *CreateWhile() 833af6ab5fSopenharmony_ci { 843af6ab5fSopenharmony_ci return alloc_->New<ir::WhileStatement>(CreateLessCmpExpr(), CreateBlockWithDeclare()); 853af6ab5fSopenharmony_ci } 863af6ab5fSopenharmony_ci 873af6ab5fSopenharmony_ciprivate: 883af6ab5fSopenharmony_ci ArenaAllocator *const alloc_; 893af6ab5fSopenharmony_ci}; 903af6ab5fSopenharmony_ci} // namespace ark::es2panda::gtests 913af6ab5fSopenharmony_ci#endif // ETS2PANDA_TEST_UNIT_NODE_CREATOR_H 92