GNU Octave  6.2.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-2021 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 
40 namespace octave
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  // No copying!
63 
64  tree_decl_elt (const tree_decl_elt&) = delete;
65 
67 
68  ~tree_decl_elt (void);
69 
71  {
73  }
74 
75  bool lvalue_ok (void) { return m_id->lvalue_ok (); }
76 
78  {
79  return m_id->lvalue (tw);
80  }
81 
82  void mark_global (void) { type = global; }
83  bool is_global (void) const { return type == global; }
84 
85  void mark_persistent (void) { type = persistent; }
86  bool is_persistent (void) const { return type == persistent; }
87 
88  tree_identifier * ident (void) { return m_id; }
89 
90  std::string name (void) const { return m_id->name (); }
91 
92  tree_expression * expression (void) { return m_expr; }
93 
94  tree_decl_elt * dup (symbol_scope& scope) const;
95 
96  void accept (tree_walker& tw)
97  {
98  tw.visit_decl_elt (*this);
99  }
100 
101  private:
102 
104 
105  // An identifier to tag with the declared property.
107 
108  // An initializer expression (may be zero);
110  };
111 
112  class tree_decl_init_list : public base_list<tree_decl_elt *>
113  {
114  public:
115 
117 
119 
120  // No copying!
121 
123 
125 
127  {
128  while (! empty ())
129  {
130  auto p = begin ();
131  delete *p;
132  erase (p);
133  }
134  }
135 
136  void mark_global (void)
137  {
138  for (tree_decl_elt *elt : *this)
139  elt->mark_global ();
140  }
141 
142  void mark_persistent (void)
143  {
144  for (tree_decl_elt *elt : *this)
145  elt->mark_persistent ();
146  }
147 
148  std::list<std::string> variable_names (void) const
149  {
150  std::list<std::string> retval;
151 
152  for (const tree_decl_elt *elt : *this)
153  {
154  std::string nm = elt->name ();
155 
156  if (! nm.empty ())
157  retval.push_back (nm);
158  }
159 
160  return retval;
161  }
162 
163  void accept (tree_walker& tw)
164  {
165  tw.visit_decl_init_list (*this);
166  }
167  };
168 
169  // Base class for declaration commands -- global, static, etc.
170 
172  {
173  public:
174 
175  tree_decl_command (const std::string& n, int l = -1, int c = -1)
176  : tree_command (l, c), m_cmd_name (n), m_init_list (nullptr) { }
177 
178  tree_decl_command (const std::string& n, tree_decl_init_list *t,
179  int l = -1, int c = -1);
180 
181  // No copying!
182 
184 
186 
187  ~tree_decl_command (void);
188 
189  void mark_global (void)
190  {
191  if (m_init_list)
193  }
194 
195  void mark_persistent (void)
196  {
197  if (m_init_list)
199  }
200 
202 
203  std::string name (void) const { return m_cmd_name; }
204 
205  void accept (tree_walker& tw)
206  {
207  tw.visit_decl_command (*this);
208  }
209 
210  private:
211 
212  // The name of this command -- global, static, etc.
213  std::string m_cmd_name;
214 
215  // The list of variables or initializers in this declaration command.
217  };
218 }
219 
220 #endif
void append(const tree_decl_elt * &s)
Definition: base-list.h:92
iterator erase(iterator pos)
Definition: base-list.h:55
tree_decl_init_list * initializer_list(void)
Definition: pt-decl.h:201
std::string m_cmd_name
Definition: pt-decl.h:213
void mark_persistent(void)
Definition: pt-decl.h:195
tree_decl_command & operator=(const tree_decl_command &)=delete
void accept(tree_walker &tw)
Definition: pt-decl.h:205
tree_decl_init_list * m_init_list
Definition: pt-decl.h:216
tree_decl_command(const tree_decl_command &)=delete
tree_decl_command(const std::string &n, int l=-1, int c=-1)
Definition: pt-decl.h:175
std::string name(void) const
Definition: pt-decl.h:203
void mark_global(void)
Definition: pt-decl.h:189
octave_lvalue lvalue(tree_evaluator &tw)
Definition: pt-decl.h:77
bool is_global(void) const
Definition: pt-decl.h:83
tree_expression * expression(void)
Definition: pt-decl.h:92
tree_expression * m_expr
Definition: pt-decl.h:109
tree_decl_elt(const tree_decl_elt &)=delete
void accept(tree_walker &tw)
Definition: pt-decl.h:96
tree_decl_elt & operator=(const tree_decl_elt &)=delete
std::string name(void) const
Definition: pt-decl.h:90
tree_identifier * m_id
Definition: pt-decl.h:106
void mark_as_formal_parameter(void)
Definition: pt-decl.h:70
tree_decl_elt(tree_identifier *i, tree_expression *e=nullptr)
Definition: pt-decl.cc:47
void mark_global(void)
Definition: pt-decl.h:82
void mark_persistent(void)
Definition: pt-decl.h:85
bool lvalue_ok(void)
Definition: pt-decl.h:75
tree_identifier * ident(void)
Definition: pt-decl.h:88
tree_decl_elt * dup(symbol_scope &scope) const
Definition: pt-decl.cc:61
bool is_persistent(void) const
Definition: pt-decl.h:86
tree_decl_init_list & operator=(const tree_decl_init_list &)=delete
void accept(tree_walker &tw)
Definition: pt-decl.h:163
tree_decl_init_list(tree_decl_elt *t)
Definition: pt-decl.h:118
std::list< std::string > variable_names(void) const
Definition: pt-decl.h:148
tree_decl_init_list(const tree_decl_init_list &)=delete
void mark_persistent(void)
Definition: pt-decl.h:142
octave_lvalue lvalue(tree_evaluator &tw)
Definition: pt-id.cc:69
bool lvalue_ok(void) const
Definition: pt-id.h:82
std::string name(void) const
Definition: pt-id.h:73
void mark_as_formal_parameter(void)
Definition: pt-id.h:77
virtual void visit_decl_init_list(tree_decl_init_list &)
Definition: pt-walk.cc:124
virtual void visit_decl_command(tree_decl_command &)
Definition: pt-walk.cc:103
virtual void visit_decl_elt(tree_decl_elt &)
Definition: pt-walk.cc:111
octave_idx_type n
Definition: mx-inlines.cc:753
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811