GNU Octave  8.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-2023 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
45  line (), column ());
46 
47  new_scr->copy_base (*this);
48 
49  return new_scr;
50 }
51 
54 {
55  octave_value tmp
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
82 
83  new_mcq->copy_base (*this);
84 
85  return new_mcq;
86 }
87 
90 {
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 check_for_doc_string (comment_list *comments)
125 {
126  // If the comment list ends in a block comment or full-line comment,
127  // then it is the doc string for this property.
128 
129  if (comments)
130  {
131  comment_elt last_elt = comments->back ();
132 
133  if (last_elt.is_block () || last_elt.is_full_line ())
134  return last_elt.text ();
135  }
136 
137  return "";
138 }
139 
141  comment_list *comments)
142  : m_av (av), m_comments (comments),
143  m_doc_string (check_for_doc_string (m_comments))
144 { }
145 
147 {
148  delete m_av;
149 }
150 
152 {
154 
155  return dynamic_cast<tree_identifier *> (id_expr);
156 }
157 
159 {
160  return m_av->initializer_expression ();
161 }
162 
163 // Classdef property_list
164 
166 {
167  while (! empty ())
168  {
169  auto p = begin ();
170  delete *p;
171  erase (p);
172  }
173 }
174 
175 // Classdef properties_block
176 
177 // Classdef methods_list
178 
179 // Classdef methods_block
180 
181 // Classdef event
182 
184  comment_list *comments)
185  : m_id (i), m_comments (comments),
186  m_doc_string (check_for_doc_string (m_comments))
187 { }
188 
189 // Classdef events_list
190 
192 {
193  while (! empty ())
194  {
195  auto p = begin ();
196  delete *p;
197  erase (p);
198  }
199 }
200 
201 // Classdef events_block
202 
203 // Classdef enum
204 
206  tree_expression *e,
207  comment_list *comments)
208  : m_id (i), m_expr (e), m_comments (comments),
209  m_doc_string (check_for_doc_string (m_comments))
210 { }
211 
212 // Classdef enum_list
213 
215 {
216  while (! empty ())
217  {
218  auto p = begin ();
219  delete *p;
220  erase (p);
221  }
222 }
223 
224 // Classdef enum_block
225 
226 // Classdef body
227 
229  : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst ()
230 { }
231 
233  : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst (),
234  m_doc_string (pb ? get_doc_string (pb->leading_comment ()) : "")
235 {
236  append (pb);
237 }
238 
240  : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst (),
241  m_doc_string (mb ? get_doc_string (mb->leading_comment ()) : "")
242 {
243  append (mb);
244 }
245 
247  : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst (),
248  m_doc_string (evb ? get_doc_string (evb->leading_comment ()) : "")
249 {
250  append (evb);
251 }
252 
254  : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst (),
255  m_doc_string (enb ? get_doc_string (enb->leading_comment ()) : "")
256 {
257  append (enb);
258 }
259 
261 {
262  while (! m_properties_lst.empty ())
263  {
264  auto p = m_properties_lst.begin ();
265  delete *p;
266  m_properties_lst.erase (p);
267  }
268 
269  while (! m_methods_lst.empty ())
270  {
271  auto p = m_methods_lst.begin ();
272  delete *p;
273  m_methods_lst.erase (p);
274  }
275 
276  while (! m_events_lst.empty ())
277  {
278  auto p = m_events_lst.begin ();
279  delete *p;
280  m_events_lst.erase (p);
281  }
282 
283  while (! m_enum_lst.empty ())
284  {
285  auto p = m_enum_lst.begin ();
286  delete *p;
287  m_enum_lst.erase (p);
288  }
289 }
290 
291 std::string
293 {
294  // Grab the first comment from the list and use it as the doc string
295  // for this classdef body.
296 
297  if (comments)
298  {
299  comment_elt first_elt = comments->front ();
300 
301  return first_elt.text ();
302  }
303 
304  return "";
305 }
306 
307 // Classdef
308 
310 tree_classdef::make_meta_class (interpreter& interp, bool is_at_folder)
311 {
312  cdef_class cls = cdef_class::make_meta_class (interp, this, is_at_folder);
313 
314  if (cls.ok ())
315  return cls.get_constructor_function ();
316 
317  return octave_value ();
318 }
319 
OCTAVE_END_NAMESPACE(octave)
elt_type & back(void)
Definition: base-list.h:80
elt_type & front(void)
Definition: base-list.h:79
iterator erase(iterator pos)
Definition: base-list.h:55
octave_value get_constructor_function(void)
Definition: cdef-class.h:362
static OCTINTERP_API 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:846
bool ok(void) const
Definition: cdef-object.h:312
std::string text(void) const
Definition: comment-list.h:75
bool is_full_line(void) const
Definition: comment-list.h:80
bool is_block(void) const
Definition: comment-list.h:79
static OCTINTERP_API octave_value metaclass_query(const std::string &cls)
Definition: ov-classdef.cc:400
static OCTINTERP_API octave_value superclass_ref(const std::string &meth, const std::string &cls)
Definition: ov-classdef.cc:393
OCTINTERP_API octave_function * function_value(bool silent=false) const
tree_expression * initializer_expression(void)
tree_expression * identifier_expression(void)
std::list< tree_classdef_events_block * > m_events_lst
Definition: pt-classdef.h:753
std::list< tree_classdef_methods_block * > m_methods_lst
Definition: pt-classdef.h:751
std::list< tree_classdef_enum_block * > m_enum_lst
Definition: pt-classdef.h:755
std::list< tree_classdef_properties_block * > m_properties_lst
Definition: pt-classdef.h:749
void append(tree_classdef_properties_block *pb)
Definition: pt-classdef.h:694
std::string get_doc_string(comment_list *comment) const
Definition: pt-classdef.cc:292
tree_classdef_enum(tree_identifier *i, tree_expression *e, comment_list *comments)
Definition: pt-classdef.cc:205
tree_classdef_event(tree_identifier *i=nullptr, comment_list *comments=nullptr)
Definition: pt-classdef.cc:183
tree_identifier * ident(void)
Definition: pt-classdef.cc:151
tree_arg_validation * m_av
Definition: pt-classdef.h:352
tree_classdef_property(tree_arg_validation *av, comment_list *comments=nullptr)
Definition: pt-classdef.cc:140
tree_expression * expression(void)
Definition: pt-classdef.cc:158
octave_value make_meta_class(interpreter &interp, bool is_at_folder=false)
Definition: pt-classdef.cc:310
virtual void copy_base(const tree_expression &e)
Definition: pt-exp.h:132
bool is_postfix_indexed(void) const
Definition: pt-exp.h:94
tree_metaclass_query * dup(symbol_scope &scope) const
Definition: pt-classdef.cc:78
std::string m_class_name
Definition: pt-classdef.h:134
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-classdef.cc:89
tree_metaclass_query(void)=delete
tree_superclass_ref(void)=delete
std::string m_class_name
Definition: pt-classdef.h:97
std::string m_method_name
Definition: pt-classdef.h:93
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(void) const
Definition: pt.h:62
virtual int line(void) const
Definition: pt.h:60
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void panic_unless(bool cond)
Definition: error.h:526
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:211
std::string check_for_doc_string(comment_list *comments)
Definition: pt-classdef.cc:124