1/** 2 * Copyright (c) 2021-2022 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_COMPILER_BASE_LITERALS_H 17#define ES2PANDA_COMPILER_BASE_LITERALS_H 18 19#include <ir/expressions/literal.h> 20#include <util/ustring.h> 21 22#include <variant> 23 24namespace panda::es2panda::ir { 25class Literal; 26} // namespace panda::es2panda::ir 27 28namespace panda::es2panda::checker { 29class Checker; 30class Type; 31} // namespace panda::es2panda::checker 32 33namespace panda::es2panda::compiler { 34 35class PandaGen; 36 37class LiteralBuffer { 38public: 39 explicit LiteralBuffer(ArenaAllocator *allocator) : literals_(allocator->Adapter()) {} 40 ~LiteralBuffer() = default; 41 NO_COPY_SEMANTIC(LiteralBuffer); 42 NO_MOVE_SEMANTIC(LiteralBuffer); 43 44 void Add(const ir::Literal *lit) 45 { 46 literals_.push_back(lit); 47 } 48 49 bool IsEmpty() const 50 { 51 return literals_.empty(); 52 } 53 54 size_t Size() const 55 { 56 return literals_.size(); 57 } 58 59 int32_t Index() const 60 { 61 return index_; 62 } 63 64 void ResetLiteral(size_t index, const ir::Literal *literal) 65 { 66 literals_[index] = literal; 67 } 68 69 const ArenaVector<const ir::Literal *> &Literals() const 70 { 71 return literals_; 72 } 73 74 void Insert(LiteralBuffer *other) 75 { 76 literals_.insert(literals_.end(), other->literals_.begin(), other->literals_.end()); 77 other->literals_.clear(); 78 } 79 80 void SetIndex(int32_t index) 81 { 82 index_ = index; 83 } 84 85private: 86 ArenaVector<const ir::Literal *> literals_; 87 int32_t index_ {}; 88}; 89 90class Literals { 91public: 92 Literals() = delete; 93 94 static void GetTemplateObject(PandaGen *pg, const ir::TaggedTemplateExpression *lit); 95}; 96 97} // namespace panda::es2panda::compiler 98 99#endif 100