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_ABSTRACT_TYPED_VALUE_HPP 17 #define _PANDA_VERIFIER_ABSTRACT_TYPED_VALUE_HPP 18 19 #include "verification/value/abstract_type.h" 20 #include "verification/value/abstract_value.h" 21 22 #include "verification/value/origin.h" 23 24 #include "libpandafile/bytecode_instruction.h" 25 26 #include "macros.h" 27 28 namespace panda::verifier { 29 class AbstractTypedValue { 30 using ValueOrigin = Origin<panda::BytecodeInstructionSafe>; 31 32 public: IsNone()33 bool IsNone() const 34 { 35 return Type_.IsNone(); 36 } 37 AbstractTypedValue() = default; 38 AbstractTypedValue(const AbstractTypedValue &) = default; 39 AbstractTypedValue(AbstractTypedValue &&) = default; 40 AbstractTypedValue &operator=(const AbstractTypedValue &) = default; 41 AbstractTypedValue &operator=(AbstractTypedValue &&) = default; 42 ~AbstractTypedValue() = default; AbstractTypedValue(const AbstractType & type,const AbstractValue & value)43 AbstractTypedValue(const AbstractType &type, const AbstractValue &value) : Value_ {value}, Type_ {type} {} AbstractTypedValue(const AbstractTypedValue & atv,const panda::BytecodeInstructionSafe & inst)44 AbstractTypedValue(const AbstractTypedValue &atv, const panda::BytecodeInstructionSafe &inst) 45 : Value_ {atv.Value_}, Type_ {atv.Type_}, Origin_ {inst} 46 { 47 } AbstractTypedValue(const AbstractType & type,const AbstractValue & value,const panda::BytecodeInstructionSafe & inst)48 AbstractTypedValue(const AbstractType &type, const AbstractValue &value, const panda::BytecodeInstructionSafe &inst) 49 : Value_ {value}, Type_ {type}, Origin_ {inst} 50 { 51 } AbstractTypedValue(const AbstractType & type,const AbstractValue & value,const ValueOrigin & origin)52 AbstractTypedValue(const AbstractType &type, const AbstractValue &value, const ValueOrigin &origin) 53 : Value_ {value}, Type_ {type}, Origin_ {origin} 54 { 55 } 56 struct Start { 57 }; AbstractTypedValue(const AbstractType & type,const AbstractValue & value,Start start,size_t n)58 AbstractTypedValue(const AbstractType &type, const AbstractValue &value, [[maybe_unused]] Start start, size_t n) 59 : Value_ {value}, Type_ {type}, Origin_ {OriginType::START, n} 60 { 61 } SetAbstractType(const AbstractType & type)62 AbstractTypedValue &SetAbstractType(const AbstractType &type) 63 { 64 Type_ = type; 65 return *this; 66 } SetAbstractValue(const AbstractValue & value)67 AbstractTypedValue &SetAbstractValue(const AbstractValue &value) 68 { 69 Value_ = value; 70 return *this; 71 } GetAbstractType()72 const AbstractType &GetAbstractType() const 73 { 74 return Type_; 75 } GetAbstractValue()76 const AbstractValue &GetAbstractValue() const 77 { 78 return Value_; 79 } 80 AbstractTypedValue operator&(const AbstractTypedValue &rhs) const 81 { 82 if (Origin_.IsValid() && rhs.Origin_.IsValid() && Origin_ == rhs.Origin_) { 83 return {Type_ & rhs.GetAbstractType(), Value_ & rhs.GetAbstractValue(), Origin_}; 84 } 85 return {Type_ & rhs.GetAbstractType(), Value_ & rhs.GetAbstractValue()}; 86 } IsConsistent()87 bool IsConsistent() const 88 { 89 return Type_.IsConsistent(); 90 } GetOrigin()91 ValueOrigin &GetOrigin() 92 { 93 return Origin_; 94 } GetOrigin()95 const ValueOrigin &GetOrigin() const 96 { 97 return Origin_; 98 } 99 template <typename TypeImageFunc> Image(TypeImageFunc type_img_func)100 PandaString Image(TypeImageFunc type_img_func) const 101 { 102 // currently only types and origin printed 103 PandaString result {GetAbstractType().Image(type_img_func)}; 104 if (Origin_.IsValid()) { 105 if (Origin_.AtStart()) { 106 result += "@start"; 107 } else { 108 result += "@" + OffsetToHexStr(Origin_.GetOffset()); 109 } 110 } 111 return result; 112 } 113 114 private: 115 AbstractValue Value_; 116 AbstractType Type_; 117 ValueOrigin Origin_; 118 }; 119 120 } // namespace panda::verifier 121 122 #endif // !_PANDA_VERIFIER_ABSTRACT_TYPED_VALUE_HPP 123