GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
symrec.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1993-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_symrec_h)
27 #define octave_symrec_h 1
28 
29 #include "octave-config.h"
30 
31 #include <deque>
32 #include <list>
33 #include <memory>
34 #include <string>
35 
37 
38 #include "ov.h"
39 #include "ovl.h"
40 
42 
43 class symbol_scope_rep;
44 
46 {
47 public:
48 
49  typedef std::size_t context_id;
50 
51  enum symrec_t : unsigned char
52  {
53  // generic variable
54  LOCAL = 1,
55  // formal parameter
56  FORMAL = 2,
57  // this symbol may NOT become a variable.
58  // (symbol added to a static workspace)
60  // this symbol was recognized as a variable from syntax
61  VARIABLE = 8
62  };
63 
64 private:
65 
66  class symbol_record_rep
67  {
68  public:
69 
70  symbol_record_rep () = delete;
71 
72  symbol_record_rep (const std::string& nm, symrec_t sc)
73  : m_frame_offset (0), m_data_offset (0), m_storage_class (sc),
74  m_name (nm)
75  { }
76 
77  OCTAVE_DEFAULT_COPY_MOVE_DELETE (symbol_record_rep)
78 
79  // FIXME: use special storage class instead?
80  bool is_valid () const { return ! m_name.empty (); }
81 
82  void set_frame_offset (std::size_t offset) { m_frame_offset = offset; }
83 
84  std::size_t frame_offset () const { return m_frame_offset; }
85 
86  void set_data_offset (std::size_t offset) { m_data_offset = offset; }
87 
88  std::size_t data_offset () const { return m_data_offset; }
89 
90  bool is_local () const
91  {
92  return m_storage_class & LOCAL;
93  }
94 
95  bool is_formal () const
96  {
97  return m_storage_class & FORMAL;
98  }
99 
100  bool is_added_static () const
101  {
102  return m_storage_class & ADDED_STATIC;
103  }
104 
105  bool is_variable () const
106  {
107  return m_storage_class & VARIABLE;
108  }
109 
110  void mark_local ()
111  {
112  m_storage_class = static_cast<symrec_t> (m_storage_class | LOCAL);
113  }
114 
115  void mark_formal ()
116  {
117  // Formal parameters are also variables.
118  m_storage_class = static_cast<symrec_t> (m_storage_class
119  | FORMAL | VARIABLE);
120  }
121 
122  void mark_added_static ()
123  {
124  m_storage_class = static_cast<symrec_t> (m_storage_class
125  | ADDED_STATIC);
126  }
127 
128  void mark_as_variable ()
129  {
130  m_storage_class = static_cast<symrec_t> (m_storage_class | VARIABLE);
131  }
132 
133  void unmark_local ()
134  {
135  m_storage_class = static_cast<symrec_t> (m_storage_class & ~LOCAL);
136  }
137 
138  void unmark_formal ()
139  {
140  m_storage_class = static_cast<symrec_t> (m_storage_class & ~FORMAL);
141  }
142 
143  void unmark_added_static ()
144  {
145  m_storage_class = static_cast<symrec_t> (m_storage_class
146  & ~ADDED_STATIC);
147  }
148 
149  void unmark_as_variable ()
150  {
151  m_storage_class = static_cast<symrec_t> (m_storage_class & ~VARIABLE);
152  }
153 
154  unsigned int storage_class () const { return m_storage_class; }
155 
156  std::shared_ptr<symbol_record_rep> dup () const;
157 
158  octave_value dump () const;
159 
160  std::string name () const { return m_name; }
161 
162  void rename (const std::string& new_name) { m_name = new_name; }
163 
164  private:
165 
166  std::size_t m_frame_offset;
167  std::size_t m_data_offset;
168 
169  symrec_t m_storage_class;
170 
171  std::string m_name;
172  };
173 
174 public:
175 
176  symbol_record (const std::string& nm = "", symrec_t sc = LOCAL)
177  : m_rep (new symbol_record_rep (nm, sc))
178  { }
179 
180  symbol_record (const std::string& nm, const octave_value&,
181  symrec_t sc = LOCAL)
182  : m_rep (new symbol_record_rep (nm, sc))
183  { }
184 
185  symbol_record (const symbol_record&) = default;
186 
188 
189  ~symbol_record () = default;
190 
191  bool is_valid () const { return m_rep->is_valid (); }
192 
193  explicit operator bool () const { return is_valid (); }
194 
195  void set_frame_offset (std::size_t offset)
196  { m_rep->set_frame_offset (offset); }
197 
198  std::size_t frame_offset () const { return m_rep->frame_offset (); }
199 
200  void set_data_offset (std::size_t offset)
201  { m_rep->set_data_offset (offset); }
202 
203  std::size_t data_offset () const { return m_rep->data_offset (); }
204 
205  symbol_record dup () const { return symbol_record (m_rep->dup ()); }
206 
207  std::string name () const { return m_rep->name (); }
208 
209  void rename (const std::string& new_name) { m_rep->rename (new_name); }
210 
211  bool is_local () const { return m_rep->is_local (); }
212  bool is_formal () const { return m_rep->is_formal (); }
213  bool is_added_static () const { return m_rep->is_added_static (); }
214  bool is_variable () const { return m_rep->is_variable (); }
215 
216  void mark_local () { m_rep->mark_local (); }
217  void mark_formal () { m_rep->mark_formal (); }
218  void mark_added_static () { m_rep->mark_added_static (); }
219  void mark_as_variable () { m_rep->mark_as_variable (); }
220 
221  void unmark_local () { m_rep->unmark_local (); }
222  void unmark_formal () { m_rep->unmark_formal (); }
223  void unmark_added_static () { m_rep->unmark_added_static (); }
224  void unmark_as_variable () { m_rep->unmark_as_variable (); }
225 
226  unsigned int storage_class () const { return m_rep->storage_class (); }
227 
228  octave_value dump () const { return m_rep->dump (); }
229 
230 private:
231 
232  std::shared_ptr<symbol_record_rep> m_rep;
233 
234  // NEW_REP must be dynamically allocated or nullptr.
235  symbol_record (const std::shared_ptr<symbol_record_rep>& new_rep)
236  : m_rep (new_rep)
237  { }
238 };
239 
240 OCTAVE_END_NAMESPACE(octave)
241 
242 #endif
void mark_formal()
Definition: symrec.h:217
symbol_record & operator=(const symbol_record &)=default
bool is_local() const
Definition: symrec.h:211
symbol_record(const symbol_record &)=default
bool is_variable() const
Definition: symrec.h:214
symbol_record(const std::string &nm="", symrec_t sc=LOCAL)
Definition: symrec.h:176
unsigned int storage_class() const
Definition: symrec.h:226
bool is_valid() const
Definition: symrec.h:191
symbol_record dup() const
Definition: symrec.h:205
void unmark_as_variable()
Definition: symrec.h:224
void set_data_offset(std::size_t offset)
Definition: symrec.h:200
std::string name() const
Definition: symrec.h:207
std::size_t context_id
Definition: symrec.h:49
void unmark_added_static()
Definition: symrec.h:223
void set_frame_offset(std::size_t offset)
Definition: symrec.h:195
bool is_added_static() const
Definition: symrec.h:213
void unmark_local()
Definition: symrec.h:221
@ ADDED_STATIC
Definition: symrec.h:59
void rename(const std::string &new_name)
Definition: symrec.h:209
void mark_added_static()
Definition: symrec.h:218
std::size_t frame_offset() const
Definition: symrec.h:198
void unmark_formal()
Definition: symrec.h:222
std::size_t data_offset() const
Definition: symrec.h:203
bool is_formal() const
Definition: symrec.h:212
symbol_record(const std::string &nm, const octave_value &, symrec_t sc=LOCAL)
Definition: symrec.h:180
octave_value dump() const
Definition: symrec.h:228
void mark_as_variable()
Definition: symrec.h:219
void mark_local()
Definition: symrec.h:216
~symbol_record()=default
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn