LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
mips/Operand.hpp
Go to the documentation of this file.
1/* Copyright 2022 - 2026 R. Thomas
2 *
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#ifndef LIEF_ASM_MIPS_OPERAND_H
16#define LIEF_ASM_MIPS_OPERAND_H
17#include "LIEF/iterators.hpp"
18#include "LIEF/visibility.h"
19
20#include <cassert>
21#include <memory>
22#include <string>
23
24#include <ostream>
25
26
27namespace LIEF::assembly::mips {
28
29namespace details {
30class Operand;
31class OperandIt;
32}
33
35class LIEF_API Operand {
36 public:
38 class Iterator final
39 : public iterator_facade_base<Iterator, std::forward_iterator_tag, Operand,
40 std::ptrdiff_t, const Operand*, const Operand&> {
41 public:
42 using implementation = details::OperandIt;
43 using iterator_facade_base::operator++;
44
46
47 LIEF_API Iterator(std::unique_ptr<details::OperandIt> impl);
50
52 LIEF_API Iterator& operator=(Iterator&&) noexcept;
53
55
56 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
58
59 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS);
60
61 friend bool operator!=(const Iterator& LHS, const Iterator& RHS) {
62 return !(LHS == RHS);
63 }
64
65 LIEF_API const Operand& operator*() const LIEF_LIFETIMEBOUND;
66
67 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
68 LIEF_API const Operand* operator->() const LIEF_LIFETIMEBOUND;
69
72 LIEF_API std::unique_ptr<Operand> yield();
73
74 private:
75 void load() const;
76
77 std::unique_ptr<details::OperandIt> impl_;
78 mutable std::unique_ptr<Operand> cached_;
79 };
80
82 std::string to_string() const;
83
92 template<class T>
93 const T* as() const LIEF_LIFETIMEBOUND {
94 static_assert(std::is_base_of_v<Operand, T>, "Require Operand inheritance");
95 if (T::classof(this)) {
96 return static_cast<const T*>(this);
97 }
98 return nullptr;
99 }
100
101 virtual ~Operand();
102
104 static LIEF_LOCAL std::unique_ptr<Operand>
105 create(std::unique_ptr<details::Operand> impl);
106
108 LIEF_LOCAL const details::Operand& impl() const LIEF_LIFETIMEBOUND {
109 assert(impl_ != nullptr);
110 return *impl_;
111 }
112
114 LIEF_LOCAL details::Operand& impl() LIEF_LIFETIMEBOUND {
115 assert(impl_ != nullptr);
116 return *impl_;
117 }
118
119 friend LIEF_API std::ostream& operator<<(std::ostream& os, const Operand& op) {
120 os << op.to_string();
121 return os;
122 }
123
124 protected:
125 LIEF_LOCAL Operand(std::unique_ptr<details::Operand> impl);
126 std::unique_ptr<details::Operand> impl_;
127};
128
129}
130
131
132#endif
std::unique_ptr< Operand > yield()
Transfer ownership of the operand at the current position to the caller. Returns nullptr if the itera...
details::OperandIt implementation
Definition mips/Operand.hpp:42
Iterator(std::unique_ptr< details::OperandIt > impl)
Iterator & operator=(const Iterator &)
This class represents an operand for a Mips instruction.
Definition mips/Operand.hpp:35
const T * as() const
This function can be used to down cast an Operand instance:
Definition mips/Operand.hpp:93
friend std::ostream & operator<<(std::ostream &os, const Operand &op)
Definition mips/Operand.hpp:119
std::string to_string() const
Pretty representation of the operand.
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition iterators.hpp:738
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition mips/Operand.hpp:29
Mips architecture-related namespace.
Definition mips/Instruction.hpp:26
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46