GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
pt-decl.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2025 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 "oct-lvalue.h"
35#include "pt-cmd.h"
36#include "pt-id.h"
37#include "pt-walk.h"
38
40
41class symbol_scope;
42class tree_evaluator;
43class tree_expression;
44class tree_identifier;
45
46// List of expressions that make up a declaration statement.
47
49{
50public:
51
58
60
61 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_decl_elt)
62
64
65 filepos beg_pos () const { return m_id->beg_pos (); }
66 filepos end_pos () const { return m_expr ? m_expr->end_pos () : m_id->end_pos (); }
67
69 {
71 }
72
73 bool lvalue_ok () { return m_id->lvalue_ok (); }
74
76 {
77 return m_id->lvalue (tw);
78 }
79
80 void mark_global () { m_type = global; }
81 bool is_global () const { return m_type == global; }
82
83 void mark_persistent () { m_type = persistent; }
84 bool is_persistent () const { return m_type == persistent; }
85
86 tree_identifier * ident () { return m_id; }
87
88 std::string name () const { return m_id->name (); }
89
90 tree_expression * expression () { return m_expr; }
91
92 tree_decl_elt * dup (symbol_scope& scope) const;
93
95 {
96 tw.visit_decl_elt (*this);
97 }
98
99private:
100
101 decl_type m_type;
102
103 // An identifier to tag with the declared property.
104 tree_identifier *m_id;
105
106 // An initializer expression (may be zero);
107 tree_expression *m_expr;
108};
109
110class tree_decl_init_list : public std::list<tree_decl_elt *>
111{
112public:
113
115
116 tree_decl_init_list (tree_decl_elt *t) { push_back (t); }
117
118 OCTAVE_DISABLE_COPY_MOVE (tree_decl_init_list)
119
121 {
122 while (! empty ())
123 {
124 auto p = begin ();
125 delete *p;
126 erase (p);
127 }
128 }
129
131 {
132 if (empty ())
133 return filepos ();
134
135 tree_decl_elt *elt = front ();
136 return elt->beg_pos ();
137 }
138
140 {
141 if (empty ())
142 return filepos ();
143
144 tree_decl_elt *elt = back ();
145 return elt->end_pos ();
146 }
147
149 {
150 for (tree_decl_elt *elt : *this)
151 elt->mark_global ();
152 }
153
155 {
156 for (tree_decl_elt *elt : *this)
157 elt->mark_persistent ();
158 }
159
160 std::list<std::string> variable_names () const
161 {
162 std::list<std::string> retval;
163
164 for (const tree_decl_elt *elt : *this)
165 {
166 std::string nm = elt->name ();
167
168 if (! nm.empty ())
169 retval.push_back (nm);
170 }
171
172 return retval;
173 }
174
176 {
177 tw.visit_decl_init_list (*this);
178 }
179};
180
181// Base class for declaration commands -- global, static, etc.
182
184{
185public:
186
187 tree_decl_command (const std::string& n, const token& tok, tree_decl_init_list *t);
188
189 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_decl_command)
190
192
193 filepos beg_pos () const { return m_token.beg_pos (); }
194 filepos end_pos () const { return m_init_list->end_pos (); }
195
197 {
198 if (m_init_list)
199 m_init_list->mark_global ();
200 }
201
203 {
204 if (m_init_list)
205 m_init_list->mark_persistent ();
206 }
207
208 tree_decl_init_list * initializer_list () { return m_init_list; }
209
210 std::string name () const { return m_cmd_name; }
211
213 {
214 tw.visit_decl_command (*this);
215 }
216
217private:
218
219 // The name of this command -- global, static, etc.
220 std::string m_cmd_name;
221
222 token m_token;
223
224 // The list of variables or initializers in this declaration command.
225 tree_decl_init_list *m_init_list;
226};
227
228OCTAVE_END_NAMESPACE(octave)
229
230#endif
Definition token.h:42
filepos beg_pos() const
Definition token.h:128
void mark_persistent()
Definition pt-decl.h:202
void accept(tree_walker &tw)
Definition pt-decl.h:212
void mark_global()
Definition pt-decl.h:196
filepos end_pos() const
Definition pt-decl.h:194
tree_decl_init_list * initializer_list()
Definition pt-decl.h:208
filepos beg_pos() const
Definition pt-decl.h:193
std::string name() const
Definition pt-decl.h:210
tree_expression * expression()
Definition pt-decl.h:90
bool is_global() const
Definition pt-decl.h:81
tree_decl_elt * dup(symbol_scope &scope) const
Definition pt-decl.cc:61
void mark_persistent()
Definition pt-decl.h:83
filepos end_pos() const
Definition pt-decl.h:66
bool lvalue_ok()
Definition pt-decl.h:73
tree_identifier * ident()
Definition pt-decl.h:86
octave_lvalue lvalue(tree_evaluator &tw)
Definition pt-decl.h:75
void mark_global()
Definition pt-decl.h:80
void accept(tree_walker &tw)
Definition pt-decl.h:94
bool is_persistent() const
Definition pt-decl.h:84
std::string name() const
Definition pt-decl.h:88
void mark_as_formal_parameter()
Definition pt-decl.h:68
filepos beg_pos() const
Definition pt-decl.h:65
void mark_persistent()
Definition pt-decl.h:154
void accept(tree_walker &tw)
Definition pt-decl.h:175
filepos end_pos() const
Definition pt-decl.h:139
filepos beg_pos() const
Definition pt-decl.h:130
tree_decl_init_list(tree_decl_elt *t)
Definition pt-decl.h:116
std::list< std::string > variable_names() const
Definition pt-decl.h:160
void mark_as_formal_parameter()
Definition pt-id.h:80
filepos beg_pos() const
Definition pt-id.h:75
std::string name() const
Definition pt-id.h:71
octave_lvalue lvalue(tree_evaluator &tw)
Definition pt-id.cc:68
bool lvalue_ok() const
Definition pt-id.h:85
filepos end_pos() const
Definition pt-id.h:76
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
virtual filepos end_pos() const =0
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn