GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pt-decl.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-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_pt_decl_h)
27 #define octave_pt_decl_h 1
28 
29 #include "octave-config.h"
30 
31 #include <list>
32 #include <string>
33 
34 #include "base-list.h"
35 #include "oct-lvalue.h"
36 #include "pt-cmd.h"
37 #include "pt-id.h"
38 #include "pt-walk.h"
39 
41 
42 class symbol_scope;
43 class tree_evaluator;
44 class tree_expression;
45 class tree_identifier;
46 
47 // List of expressions that make up a declaration statement.
48 
50 {
51 public:
52 
53  enum decl_type
54  {
58  };
59 
61 
62  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_decl_elt)
63 
64  ~tree_decl_elt ();
65 
67  {
68  m_id->mark_as_formal_parameter ();
69  }
70 
71  bool lvalue_ok () { return m_id->lvalue_ok (); }
72 
74  {
75  return m_id->lvalue (tw);
76  }
77 
78  void mark_global () { m_type = global; }
79  bool is_global () const { return m_type == global; }
80 
81  void mark_persistent () { m_type = persistent; }
82  bool is_persistent () const { return m_type == persistent; }
83 
84  tree_identifier * ident () { return m_id; }
85 
86  std::string name () const { return m_id->name (); }
87 
88  tree_expression * expression () { return m_expr; }
89 
90  tree_decl_elt * dup (symbol_scope& scope) const;
91 
92  void accept (tree_walker& tw)
93  {
94  tw.visit_decl_elt (*this);
95  }
96 
97 private:
98 
99  decl_type m_type;
100 
101  // An identifier to tag with the declared property.
102  tree_identifier *m_id;
103 
104  // An initializer expression (may be zero);
105  tree_expression *m_expr;
106 };
107 
108 class tree_decl_init_list : public base_list<tree_decl_elt *>
109 {
110 public:
111 
113 
115 
116  OCTAVE_DISABLE_COPY_MOVE (tree_decl_init_list)
117 
119  {
120  while (! empty ())
121  {
122  auto p = begin ();
123  delete *p;
124  erase (p);
125  }
126  }
127 
128  void mark_global ()
129  {
130  for (tree_decl_elt *elt : *this)
131  elt->mark_global ();
132  }
133 
135  {
136  for (tree_decl_elt *elt : *this)
137  elt->mark_persistent ();
138  }
139 
140  std::list<std::string> variable_names () const
141  {
142  std::list<std::string> retval;
143 
144  for (const tree_decl_elt *elt : *this)
145  {
146  std::string nm = elt->name ();
147 
148  if (! nm.empty ())
149  retval.push_back (nm);
150  }
151 
152  return retval;
153  }
154 
155  void accept (tree_walker& tw)
156  {
157  tw.visit_decl_init_list (*this);
158  }
159 };
160 
161 // Base class for declaration commands -- global, static, etc.
162 
164 {
165 public:
166 
167  tree_decl_command (const std::string& n, int l = -1, int c = -1)
168  : tree_command (l, c), m_cmd_name (n), m_init_list (nullptr) { }
169 
170  tree_decl_command (const std::string& n, tree_decl_init_list *t,
171  int l = -1, int c = -1);
172 
173  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_decl_command)
174 
176 
177  void mark_global ()
178  {
179  if (m_init_list)
180  m_init_list->mark_global ();
181  }
182 
184  {
185  if (m_init_list)
186  m_init_list->mark_persistent ();
187  }
188 
189  tree_decl_init_list * initializer_list () { return m_init_list; }
190 
191  std::string name () const { return m_cmd_name; }
192 
193  void accept (tree_walker& tw)
194  {
195  tw.visit_decl_command (*this);
196  }
197 
198 private:
199 
200  // The name of this command -- global, static, etc.
201  std::string m_cmd_name;
202 
203  // The list of variables or initializers in this declaration command.
204  tree_decl_init_list *m_init_list;
205 };
206 
207 OCTAVE_END_NAMESPACE(octave)
208 
209 #endif
void append(const tree_decl_elt * &s)
Definition: base-list.h:92
iterator erase(iterator pos)
Definition: base-list.h:55
void mark_persistent()
Definition: pt-decl.h:183
void accept(tree_walker &tw)
Definition: pt-decl.h:193
void mark_global()
Definition: pt-decl.h:177
tree_decl_command(const std::string &n, int l=-1, int c=-1)
Definition: pt-decl.h:167
tree_decl_init_list * initializer_list()
Definition: pt-decl.h:189
std::string name() const
Definition: pt-decl.h:191
bool is_global() const
Definition: pt-decl.h:79
tree_decl_elt * dup(symbol_scope &scope) const
Definition: pt-decl.cc:61
~tree_decl_elt()
Definition: pt-decl.cc:54
void mark_persistent()
Definition: pt-decl.h:81
bool lvalue_ok()
Definition: pt-decl.h:71
octave_lvalue lvalue(tree_evaluator &tw)
Definition: pt-decl.h:73
void mark_global()
Definition: pt-decl.h:78
void accept(tree_walker &tw)
Definition: pt-decl.h:92
tree_identifier * ident()
Definition: pt-decl.h:84
bool is_persistent() const
Definition: pt-decl.h:82
std::string name() const
Definition: pt-decl.h:86
void mark_as_formal_parameter()
Definition: pt-decl.h:66
tree_decl_elt(tree_identifier *i, tree_expression *e=nullptr)
Definition: pt-decl.cc:47
tree_expression * expression()
Definition: pt-decl.h:88
void mark_persistent()
Definition: pt-decl.h:134
void accept(tree_walker &tw)
Definition: pt-decl.h:155
std::list< std::string > variable_names() const
Definition: pt-decl.h:140
tree_decl_init_list(tree_decl_elt *t)
Definition: pt-decl.h:114
void mark_global()
Definition: pt-decl.h:128
void mark_as_formal_parameter()
Definition: pt-id.h:73
std::string name() const
Definition: pt-id.h:69
octave_lvalue lvalue(tree_evaluator &tw)
Definition: pt-id.cc:68
bool lvalue_ok() const
Definition: pt-id.h:78
virtual void visit_decl_command(tree_decl_command &)
Definition: pt-walk.cc:199
virtual void visit_decl_init_list(tree_decl_init_list &)
Definition: pt-walk.cc:222
virtual void visit_decl_elt(tree_decl_elt &)
Definition: pt-walk.cc:208
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
octave_idx_type n
Definition: mx-inlines.cc:761