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