GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pt-classdef.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2012-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 (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <iostream>
31 
32 #include "ov.h"
33 #include "ov-classdef.h"
34 #include "pt-args-block.h"
35 #include "pt-classdef.h"
36 #include "pt-eval.h"
37 
39 
42 {
43  tree_superclass_ref *new_scr
44  = new tree_superclass_ref (m_method_name, m_class_name,
45  line (), column ());
46 
47  new_scr->copy_base (*this);
48 
49  return new_scr;
50 }
51 
54 {
55  octave_value tmp
56  = octave_classdef::superclass_ref (m_method_name, m_class_name);
57 
58  if (! is_postfix_indexed ())
59  {
60  // There was no index, so this superclass_ref object is not
61  // part of an index expression. It is also not an identifier in
62  // the syntax tree but we need to handle it as if it were. So
63  // call the function here.
64  octave_function *f = tmp.function_value (true);
65 
66  panic_unless (f);
67 
68  return f->call (tw, nargout);
69  }
70 
71  // The superclass_ref function object will be indexed as part of the
72  // enclosing index expression.
73 
74  return ovl (tmp);
75 }
76 
79 {
80  tree_metaclass_query *new_mcq
81  = new tree_metaclass_query (m_class_name, line (), column ());
82 
83  new_mcq->copy_base (*this);
84 
85  return new_mcq;
86 }
87 
90 {
91  return octave_classdef::metaclass_query (m_class_name);
92 }
93 
94 // Classdef attribute
95 
96 // Classdef attribute_list
97 
99 {
100  while (! empty ())
101  {
102  auto p = begin ();
103  delete *p;
104  erase (p);
105  }
106 }
107 
108 // Classdef superclass
109 
110 // Classdef superclass_list
111 
113 {
114  while (! empty ())
115  {
116  auto p = begin ();
117  delete *p;
118  erase (p);
119  }
120 }
121 
122 // Classdef property
123 
124 std::string
126 {
127  // If the comment list ends in a block comment or full-line comment,
128  // then it is the doc string for this property.
129 
130  if (comments)
131  {
132  comment_elt last_elt = comments->back ();
133 
134  if (last_elt.is_block () || last_elt.is_full_line ())
135  return last_elt.text ();
136  }
137 
138  return "";
139 }
140 
142  comment_list *comments)
143  : m_av (av), m_comments (comments),
144  m_doc_string (check_for_doc_string (m_comments))
145 { }
146 
148 {
149  delete m_av;
150  delete m_comments;
151 }
152 
155 {
156  tree_expression *id_expr = m_av->identifier_expression ();
157 
158  return dynamic_cast<tree_identifier *> (id_expr);
159 }
160 
163 {
164  return m_av->initializer_expression ();
165 }
166 
167 // Classdef property_list
168 
170 {
171  while (! empty ())
172  {
173  auto p = begin ();
174  delete *p;
175  erase (p);
176  }
177 }
178 
179 // Classdef properties_block
180 
181 // Classdef methods_list
182 
183 // Classdef methods_block
184 
185 // Classdef event
186 
188  comment_list *comments)
189  : m_id (i), m_comments (comments),
190  m_doc_string (check_for_doc_string (m_comments))
191 { }
192 
193 // Classdef events_list
194 
196 {
197  while (! empty ())
198  {
199  auto p = begin ();
200  delete *p;
201  erase (p);
202  }
203 }
204 
205 // Classdef events_block
206 
207 // Classdef enum
208 
210  tree_expression *e,
211  comment_list *comments)
212  : m_id (i), m_expr (e), m_comments (comments),
213  m_doc_string (check_for_doc_string (m_comments))
214 { }
215 
216 // Classdef enum_list
217 
219 {
220  while (! empty ())
221  {
222  auto p = begin ();
223  delete *p;
224  erase (p);
225  }
226 }
227 
228 // Classdef enum_block
229 
230 // Classdef body
231 
233  : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst ()
234 { }
235 
237  : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst (),
238  m_doc_string (pb ? get_doc_string (pb->leading_comment ()) : "")
239 {
240  append (pb);
241 }
242 
244  : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst (),
245  m_doc_string (mb ? get_doc_string (mb->leading_comment ()) : "")
246 {
247  append (mb);
248 }
249 
251  : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst (),
252  m_doc_string (evb ? get_doc_string (evb->leading_comment ()) : "")
253 {
254  append (evb);
255 }
256 
258  : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst (),
259  m_doc_string (enb ? get_doc_string (enb->leading_comment ()) : "")
260 {
261  append (enb);
262 }
263 
265 {
266  while (! m_properties_lst.empty ())
267  {
268  auto p = m_properties_lst.begin ();
269  delete *p;
270  m_properties_lst.erase (p);
271  }
272 
273  while (! m_methods_lst.empty ())
274  {
275  auto p = m_methods_lst.begin ();
276  delete *p;
277  m_methods_lst.erase (p);
278  }
279 
280  while (! m_events_lst.empty ())
281  {
282  auto p = m_events_lst.begin ();
283  delete *p;
284  m_events_lst.erase (p);
285  }
286 
287  while (! m_enum_lst.empty ())
288  {
289  auto p = m_enum_lst.begin ();
290  delete *p;
291  m_enum_lst.erase (p);
292  }
293 }
294 
295 std::string
296 tree_classdef_body::get_doc_string (comment_list *comments) const
297 {
298  // Grab the first comment from the list and use it as the doc string
299  // for this classdef body.
300 
301  if (comments)
302  {
303  comment_elt first_elt = comments->front ();
304 
305  return first_elt.text ();
306  }
307 
308  return "";
309 }
310 
311 // Classdef
312 
314 tree_classdef::make_meta_class (interpreter& interp, bool is_at_folder)
315 {
316  cdef_class cls = cdef_class::make_meta_class (interp, this, is_at_folder);
317 
318  if (cls.ok ())
319  return cls.get_constructor_function ();
320 
321  return octave_value ();
322 }
323 
324 OCTAVE_END_NAMESPACE(octave)
elt_type & front()
Definition: base-list.h:79
elt_type & back()
Definition: base-list.h:80
iterator erase(iterator pos)
Definition: base-list.h:55
octave_value get_constructor_function()
Definition: cdef-class.h:363
static cdef_class make_meta_class(interpreter &interp, tree_classdef *t, bool is_at_folder=false)
Analyze the tree_classdef tree and transform it to a cdef_class.
Definition: cdef-class.cc:872
bool ok() const
Definition: cdef-object.h:312
bool is_full_line() const
Definition: comment-list.h:83
std::string text() const
Definition: comment-list.h:78
bool is_block() const
Definition: comment-list.h:82
static octave_value metaclass_query(const std::string &cls)
Definition: ov-classdef.cc:437
static octave_value superclass_ref(const std::string &meth, const std::string &cls)
Definition: ov-classdef.cc:430
octave_function * function_value(bool silent=false) const
tree_expression * identifier_expression()
tree_expression * initializer_expression()
void append(tree_classdef_properties_block *pb)
Definition: pt-classdef.h:610
tree_classdef_enum(tree_identifier *i, tree_expression *e, comment_list *comments)
Definition: pt-classdef.cc:209
tree_classdef_event(tree_identifier *i=nullptr, comment_list *comments=nullptr)
Definition: pt-classdef.cc:187
tree_classdef_property(tree_arg_validation *av, comment_list *comments=nullptr)
Definition: pt-classdef.cc:141
tree_expression * expression()
Definition: pt-classdef.cc:162
tree_identifier * ident()
Definition: pt-classdef.cc:154
octave_value make_meta_class(interpreter &interp, bool is_at_folder=false)
Definition: pt-classdef.cc:314
bool is_postfix_indexed() const
Definition: pt-exp.h:90
virtual void copy_base(const tree_expression &e)
Definition: pt-exp.h:128
tree_metaclass_query * dup(symbol_scope &scope) const
Definition: pt-classdef.cc:78
tree_metaclass_query(const std::string &cls, int l=-1, int c=-1)
Definition: pt-classdef.h:100
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-classdef.cc:89
tree_superclass_ref(const std::string &meth, const std::string &cls, int l=-1, int c=-1)
Definition: pt-classdef.h:53
octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)
Definition: pt-classdef.cc:53
tree_superclass_ref * dup(symbol_scope &scope) const
Definition: pt-classdef.cc:41
virtual int column() const
Definition: pt.h:58
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define panic_unless(cond)
Definition: error.h:515
F77_RET_T const F77_DBLE const F77_DBLE * f
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:219
std::string check_for_doc_string(comment_list *comments)
Definition: pt-classdef.cc:125