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 24 namespace panda::es2panda::ir { 25 class Literal; 26 } // namespace panda::es2panda::ir 27 28 namespace panda::es2panda::checker { 29 class Checker; 30 class Type; 31 } // namespace panda::es2panda::checker 32 33 namespace panda::es2panda::compiler { 34 35 class PandaGen; 36 37 class LiteralBuffer { 38 public: LiteralBuffer(ArenaAllocator * allocator)39 explicit LiteralBuffer(ArenaAllocator *allocator) : literals_(allocator->Adapter()) {} 40 ~LiteralBuffer() = default; 41 NO_COPY_SEMANTIC(LiteralBuffer); 42 NO_MOVE_SEMANTIC(LiteralBuffer); 43 Add(const ir::Literal * lit)44 void Add(const ir::Literal *lit) 45 { 46 literals_.push_back(lit); 47 } 48 IsEmpty()49 bool IsEmpty() const 50 { 51 return literals_.empty(); 52 } 53 Size()54 size_t Size() const 55 { 56 return literals_.size(); 57 } 58 Index()59 int32_t Index() const 60 { 61 return index_; 62 } 63 ResetLiteral(size_t index,const ir::Literal * literal)64 void ResetLiteral(size_t index, const ir::Literal *literal) 65 { 66 literals_[index] = literal; 67 } 68 Literals()69 const ArenaVector<const ir::Literal *> &Literals() const 70 { 71 return literals_; 72 } 73 Insert(LiteralBuffer * other)74 void Insert(LiteralBuffer *other) 75 { 76 literals_.insert(literals_.end(), other->literals_.begin(), other->literals_.end()); 77 other->literals_.clear(); 78 } 79 SetIndex(int32_t index)80 void SetIndex(int32_t index) 81 { 82 index_ = index; 83 } 84 85 private: 86 ArenaVector<const ir::Literal *> literals_; 87 int32_t index_ {}; 88 }; 89 90 class Literals { 91 public: 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