1 /* 2 * Copyright (c) 2021 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_VERIFICATION_VALUE_ORIGIN_H_ 17 #define PANDA_VERIFICATION_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, __LAST__ = INSTRUCTION }; 28 29 template <typename BytecodeInstruction> 30 class Origin : public TaggedIndex<OriginType> { 31 using Base = TaggedIndex<OriginType>; 32 33 public: Origin(const BytecodeInstruction & inst)34 Origin(const BytecodeInstruction &inst) : Base {OriginType::INSTRUCTION, inst.GetOffset()} {} 35 Origin(OriginType t,size_t val)36 Origin(OriginType t, size_t val) : Base {t, val} {} 37 AtStart()38 bool AtStart() const 39 { 40 ASSERT(Base::IsValid()); 41 return Base::GetTag() == OriginType::START; 42 } 43 GetOffset()44 uint32_t GetOffset() const 45 { 46 ASSERT(Base::IsValid()); 47 return static_cast<uint32_t>(Base::GetInt()); 48 }; 49 50 Origin() = default; 51 Origin(const Origin &) = default; 52 Origin(Origin &&) = default; 53 ~Origin() = default; 54 Origin &operator=(const Origin &) = default; 55 Origin &operator=(Origin &&) = default; 56 }; 57 } // namespace panda::verifier 58 59 #endif // PANDA_VERIFICATION_VALUE_ORIGIN_H_ 60