GNU Octave  8.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-2023 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 
67  {
68  public:
69 
70  symbol_record_rep (const std::string& nm, symrec_t sc)
72  m_name (nm)
73  { }
74 
76 
78 
79  ~symbol_record_rep (void) = default;
80 
81  // FIXME: use special storage class instead?
82  bool is_valid (void) const { return ! m_name.empty (); }
83 
84  void set_frame_offset (std::size_t offset) { m_frame_offset = offset; }
85 
86  std::size_t frame_offset (void) const { return m_frame_offset; }
87 
88  void set_data_offset (std::size_t offset) { m_data_offset = offset; }
89 
90  std::size_t data_offset (void) const { return m_data_offset; }
91 
92  bool is_local (void) const
93  {
94  return m_storage_class & LOCAL;
95  }
96 
97  bool is_formal (void) const
98  {
99  return m_storage_class & FORMAL;
100  }
101 
102  bool is_added_static (void) const
103  {
104  return m_storage_class & ADDED_STATIC;
105  }
106 
107  bool is_variable (void) const
108  {
109  return m_storage_class & VARIABLE;
110  }
111 
112  void mark_local (void)
113  {
114  m_storage_class = static_cast<symrec_t> (m_storage_class | LOCAL);
115  }
116 
117  void mark_formal (void)
118  {
119  // Formal parameters are also variables.
121  | FORMAL | VARIABLE);
122  }
123 
124  void mark_added_static (void)
125  {
127  | ADDED_STATIC);
128  }
129 
130  void mark_as_variable (void)
131  {
133  }
134 
135  void unmark_local (void)
136  {
137  m_storage_class = static_cast<symrec_t> (m_storage_class & ~LOCAL);
138  }
139 
140  void unmark_formal (void)
141  {
142  m_storage_class = static_cast<symrec_t> (m_storage_class & ~FORMAL);
143  }
144 
146  {
148  & ~ADDED_STATIC);
149  }
150 
151  void unmark_as_variable (void)
152  {
153  m_storage_class = static_cast<symrec_t> (m_storage_class & ~VARIABLE);
154  }
155 
156  unsigned int storage_class (void) const { return m_storage_class; }
157 
158  std::shared_ptr<symbol_record_rep> dup (void) const;
159 
160  octave_value dump (void) const;
161 
162  std::string name (void) const { return m_name; }
163 
164  void rename (const std::string& new_name) { m_name = new_name; }
165 
166  private:
167 
168  std::size_t m_frame_offset;
169  std::size_t m_data_offset;
170 
172 
173  std::string m_name;
174  };
175 
176 public:
177 
178  symbol_record (const std::string& nm = "", symrec_t sc = LOCAL)
179  : m_rep (new symbol_record_rep (nm, sc))
180  { }
181 
182  symbol_record (const std::string& nm, const octave_value&,
183  symrec_t sc = LOCAL)
184  : m_rep (new symbol_record_rep (nm, sc))
185  { }
186 
187  symbol_record (const symbol_record&) = default;
188 
190 
191  ~symbol_record (void) = default;
192 
193  bool is_valid (void) const { return m_rep->is_valid (); }
194 
195  explicit operator bool () const { return is_valid (); }
196 
197  void set_frame_offset (std::size_t offset)
198  { m_rep->set_frame_offset (offset); }
199 
200  std::size_t frame_offset (void) const { return m_rep->frame_offset (); }
201 
202  void set_data_offset (std::size_t offset)
203  { m_rep->set_data_offset (offset); }
204 
205  std::size_t data_offset (void) const { return m_rep->data_offset (); }
206 
207  symbol_record dup (void) const { return symbol_record (m_rep->dup ()); }
208 
209  std::string name (void) const { return m_rep->name (); }
210 
211  void rename (const std::string& new_name) { m_rep->rename (new_name); }
212 
213  bool is_local (void) const { return m_rep->is_local (); }
214  bool is_formal (void) const { return m_rep->is_formal (); }
215  bool is_added_static (void) const { return m_rep->is_added_static (); }
216  bool is_variable (void) const { return m_rep->is_variable (); }
217 
218  void mark_local (void) { m_rep->mark_local (); }
219  void mark_formal (void) { m_rep->mark_formal (); }
220  void mark_added_static (void) { m_rep->mark_added_static (); }
221  void mark_as_variable (void) { m_rep->mark_as_variable (); }
222 
223  void unmark_local (void) { m_rep->unmark_local (); }
224  void unmark_formal (void) { m_rep->unmark_formal (); }
225  void unmark_added_static (void) { m_rep->unmark_added_static (); }
226  void unmark_as_variable (void) { m_rep->unmark_as_variable (); }
227 
228  unsigned int storage_class (void) const { return m_rep->storage_class (); }
229 
230  octave_value dump (void) const { return m_rep->dump (); }
231 
232 private:
233 
234  std::shared_ptr<symbol_record_rep> m_rep;
235 
236  // NEW_REP must be dynamically allocated or nullptr.
237  symbol_record (const std::shared_ptr<symbol_record_rep>& new_rep)
238  : m_rep (new_rep)
239  { }
240 };
241 
243 
244 #endif
OCTAVE_END_NAMESPACE(octave)
symbol_record_rep(const symbol_record_rep &)=default
octave_value dump(void) const
Definition: symrec.cc:51
std::size_t data_offset(void) const
Definition: symrec.h:90
bool is_formal(void) const
Definition: symrec.h:97
bool is_variable(void) const
Definition: symrec.h:107
std::string name(void) const
Definition: symrec.h:162
symbol_record_rep & operator=(const symbol_record_rep &)=default
void set_data_offset(std::size_t offset)
Definition: symrec.h:88
std::shared_ptr< symbol_record_rep > dup(void) const
Definition: symrec.cc:45
unsigned int storage_class(void) const
Definition: symrec.h:156
std::size_t frame_offset(void) const
Definition: symrec.h:86
bool is_local(void) const
Definition: symrec.h:92
bool is_added_static(void) const
Definition: symrec.h:102
bool is_valid(void) const
Definition: symrec.h:82
symbol_record_rep(const std::string &nm, symrec_t sc)
Definition: symrec.h:70
void rename(const std::string &new_name)
Definition: symrec.h:164
void set_frame_offset(std::size_t offset)
Definition: symrec.h:84
void mark_as_variable(void)
Definition: symrec.h:221
symbol_record & operator=(const symbol_record &)=default
symbol_record(const symbol_record &)=default
symbol_record(const std::string &nm="", symrec_t sc=LOCAL)
Definition: symrec.h:178
void mark_formal(void)
Definition: symrec.h:219
std::size_t data_offset(void) const
Definition: symrec.h:205
symbol_record(const std::shared_ptr< symbol_record_rep > &new_rep)
Definition: symrec.h:237
std::size_t frame_offset(void) const
Definition: symrec.h:200
void set_data_offset(std::size_t offset)
Definition: symrec.h:202
std::string name(void) const
Definition: symrec.h:209
std::size_t context_id
Definition: symrec.h:49
void set_frame_offset(std::size_t offset)
Definition: symrec.h:197
unsigned int storage_class(void) const
Definition: symrec.h:228
std::shared_ptr< symbol_record_rep > m_rep
Definition: symrec.h:234
bool is_valid(void) const
Definition: symrec.h:193
void unmark_formal(void)
Definition: symrec.h:224
octave_value dump(void) const
Definition: symrec.h:230
symbol_record dup(void) const
Definition: symrec.h:207
@ ADDED_STATIC
Definition: symrec.h:59
void mark_local(void)
Definition: symrec.h:218
void unmark_local(void)
Definition: symrec.h:223
void rename(const std::string &new_name)
Definition: symrec.h:211
bool is_formal(void) const
Definition: symrec.h:214
bool is_added_static(void) const
Definition: symrec.h:215
void mark_added_static(void)
Definition: symrec.h:220
bool is_variable(void) const
Definition: symrec.h:216
void unmark_added_static(void)
Definition: symrec.h:225
bool is_local(void) const
Definition: symrec.h:213
symbol_record(const std::string &nm, const octave_value &, symrec_t sc=LOCAL)
Definition: symrec.h:182
void unmark_as_variable(void)
Definition: symrec.h:226
~symbol_record(void)=default
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn