1 /** 2 * Copyright (c) 2021-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 PANDA_VERIFIER_VALUE_ORIGIN_H_ 17 #define PANDA_VERIFIER_VALUE_ORIGIN_H_ 18 19 #include "verification/util/enum_tag.h" 20 #include "verification/util/tagged_index.h" 21 22 #include "macros.h" 23 24 #include <cstdint> 25 #include <limits> 26 27 namespace ark::verifier { 28 enum class OriginType { START, INSTRUCTION }; 29 30 using OriginTypeTag = TagForEnum<OriginType, OriginType::START, OriginType::INSTRUCTION>; 31 32 template <typename BytecodeInstruction> 33 class Origin : public TaggedIndex<OriginTypeTag> { 34 using Base = TaggedIndex<OriginTypeTag>; 35 36 public: Origin(const BytecodeInstruction & inst)37 explicit Origin(const BytecodeInstruction &inst) 38 { 39 Base::SetTag<0>(OriginType::INSTRUCTION); 40 Base::SetInt(inst.GetOffset()); 41 } 42 Origin(OriginType t,size_t val)43 Origin(OriginType t, size_t val) 44 { 45 Base::SetTag<0>(t); 46 Base::SetInt(val); 47 } 48 AtStart()49 bool AtStart() const 50 { 51 ASSERT(Base::IsValid()); 52 return Base::GetTag<0>() == OriginType::START; 53 } 54 GetOffset()55 uint32_t GetOffset() const 56 { 57 ASSERT(Base::IsValid()); 58 return static_cast<uint32_t>(Base::GetInt()); 59 }; 60 61 Origin() = default; 62 Origin(const Origin &) = default; 63 Origin(Origin &&) = default; 64 ~Origin() = default; 65 Origin &operator=(const Origin &) = default; 66 Origin &operator=(Origin &&) = default; 67 }; 68 } // namespace ark::verifier 69 70 #endif // PANDA_VERIFIER_VALUE_ORIGIN_H_ 71