GNU Octave 11.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-2026 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
48class OCTINTERP_API tree_decl_elt
49{
50public:
51
53 {
56 persistent
57 };
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
68 comment_list leading_comments () const { return m_id->leading_comments (); }
69 comment_list trailing_comments () const { return m_expr ? m_expr->trailing_comments () : m_id->trailing_comments (); }
70
72 {
73 m_id->mark_as_formal_parameter ();
74 }
75
76 bool lvalue_ok () { return m_id->lvalue_ok (); }
77
79 {
80 return m_id->lvalue (tw);
81 }
82
83 void mark_global () { m_type = global; }
84 bool is_global () const { return m_type == global; }
85
86 void mark_persistent () { m_type = persistent; }
87 bool is_persistent () const { return m_type == persistent; }
88
89 tree_identifier * ident () { return m_id; }
90
91 std::string name () const { return m_id->name (); }
92
93 tree_expression * expression () { return m_expr; }
94
95 tree_decl_elt * dup (symbol_scope& scope) const;
96
98 {
99 tw.visit_decl_elt (*this);
100 }
101
102private:
103
104 decl_type m_type;
105
106 // An identifier to tag with the declared property.
107 tree_identifier *m_id;
108
109 // An initializer expression (may be zero);
110 tree_expression *m_expr;
111};
112
113class OCTINTERP_API tree_decl_init_list : public std::list<tree_decl_elt *>
114{
115public:
116
118
119 tree_decl_init_list (tree_decl_elt *t) { push_back (t); }
120
121 OCTAVE_DISABLE_COPY_MOVE (tree_decl_init_list)
122
124 {
125 while (! empty ())
126 {
127 auto p = begin ();
128 delete *p;
129 erase (p);
130 }
131 }
132
134 {
135 if (empty ())
136 return filepos ();
137
138 tree_decl_elt *elt = front ();
139 return elt->beg_pos ();
140 }
141
143 {
144 if (empty ())
145 return filepos ();
146
147 tree_decl_elt *elt = back ();
148 return elt->end_pos ();
149 }
150
152 {
153 if (empty ())
154 return comment_list ();
155
156 tree_decl_elt *elt = front ();
157 return elt->leading_comments ();
158 }
159
161 {
162 if (empty ())
163 return comment_list ();
164
165 tree_decl_elt *elt = back ();
166 return elt->trailing_comments ();
167 }
168
170 {
171 for (tree_decl_elt *elt : *this)
172 elt->mark_global ();
173 }
174
176 {
177 for (tree_decl_elt *elt : *this)
178 elt->mark_persistent ();
179 }
180
181 std::list<std::string> variable_names () const
182 {
183 std::list<std::string> retval;
184
185 for (const tree_decl_elt *elt : *this)
186 {
187 std::string nm = elt->name ();
188
189 if (! nm.empty ())
190 retval.push_back (nm);
191 }
192
193 return retval;
194 }
195
197 {
198 tw.visit_decl_init_list (*this);
199 }
200};
201
202// Base class for declaration commands -- global, static, etc.
203
204class OCTINTERP_API tree_decl_command : public tree_command
205{
206public:
207
208 tree_decl_command (const std::string& n, const token& tok, tree_decl_init_list *t);
209
210 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_decl_command)
211
213
214 filepos beg_pos () const { return m_token.beg_pos (); }
215 filepos end_pos () const { return m_init_list->end_pos (); }
216
217 comment_list leading_comments () const { return m_token.leading_comments (); }
218 comment_list trailing_comments () const { return m_init_list->trailing_comments (); }
219
221 {
222 if (m_init_list)
223 m_init_list->mark_global ();
224 }
225
227 {
228 if (m_init_list)
229 m_init_list->mark_persistent ();
230 }
231
232 token decl_token () const { return m_token; }
233
234 std::string name () const { return m_cmd_name; }
235
236 tree_decl_init_list * initializer_list () { return m_init_list; }
237
239 {
240 tw.visit_decl_command (*this);
241 }
242
243private:
244
245 // The name of this command -- global, static, etc.
246 std::string m_cmd_name;
247
248 token m_token;
249
250 // The list of variables or initializers in this declaration command.
251 tree_decl_init_list *m_init_list;
252};
253
254OCTAVE_END_NAMESPACE(octave)
255
256#endif
Definition token.h:42
void mark_persistent()
Definition pt-decl.h:226
void accept(tree_walker &tw)
Definition pt-decl.h:238
void mark_global()
Definition pt-decl.h:220
filepos end_pos() const
Definition pt-decl.h:215
token decl_token() const
Definition pt-decl.h:232
tree_decl_init_list * initializer_list()
Definition pt-decl.h:236
filepos beg_pos() const
Definition pt-decl.h:214
comment_list trailing_comments() const
Definition pt-decl.h:218
std::string name() const
Definition pt-decl.h:234
comment_list leading_comments() const
Definition pt-decl.h:217
tree_expression * expression()
Definition pt-decl.h:93
comment_list trailing_comments() const
Definition pt-decl.h:69
bool is_global() const
Definition pt-decl.h:84
void mark_persistent()
Definition pt-decl.h:86
filepos end_pos() const
Definition pt-decl.h:66
bool lvalue_ok()
Definition pt-decl.h:76
tree_identifier * ident()
Definition pt-decl.h:89
octave_lvalue lvalue(tree_evaluator &tw)
Definition pt-decl.h:78
void mark_global()
Definition pt-decl.h:83
void accept(tree_walker &tw)
Definition pt-decl.h:97
bool is_persistent() const
Definition pt-decl.h:87
std::string name() const
Definition pt-decl.h:91
comment_list leading_comments() const
Definition pt-decl.h:68
void mark_as_formal_parameter()
Definition pt-decl.h:71
filepos beg_pos() const
Definition pt-decl.h:65
void mark_persistent()
Definition pt-decl.h:175
void accept(tree_walker &tw)
Definition pt-decl.h:196
comment_list leading_comments() const
Definition pt-decl.h:151
filepos end_pos() const
Definition pt-decl.h:142
filepos beg_pos() const
Definition pt-decl.h:133
tree_decl_init_list(tree_decl_elt *t)
Definition pt-decl.h:119
comment_list trailing_comments() const
Definition pt-decl.h:160
std::list< std::string > variable_names() const
Definition pt-decl.h:181
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