GNU Octave  6.2.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-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 (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-classdef.h"
35 #include "pt-eval.h"
36 
37 namespace octave
38 {
39  tree_superclass_ref *
41  {
42  tree_superclass_ref *new_scr
44  line (), column ());
45 
46  new_scr->copy_base (*this);
47 
48  return new_scr;
49  }
50 
53  {
54  octave_value tmp
56 
57  if (! is_postfix_indexed ())
58  {
59  // There was no index, so this superclass_ref object is not
60  // part of an index expression. It is also not an identifier in
61  // the syntax tree but we need to handle it as if it were. So
62  // call the function here.
63 
64  octave_function *f = tmp.function_value (true);
65 
66  assert (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_id (i), m_expr (nullptr), m_comments (comments),
143  m_doc_string (check_for_doc_string (m_comments))
144  { }
145 
147  tree_expression *e,
148  comment_list *comments)
149  : m_id (i), m_expr (e), m_comments (comments),
150  m_doc_string (check_for_doc_string (m_comments))
151  { }
152 
153  // Classdef property_list
154 
156  {
157  while (! empty ())
158  {
159  auto p = begin ();
160  delete *p;
161  erase (p);
162  }
163  }
164 
165  // Classdef properties_block
166 
167  // Classdef methods_list
168 
169  // Classdef methods_block
170 
171  // Classdef event
172 
174  comment_list *comments)
175  : m_id (i), m_comments (comments),
176  m_doc_string (check_for_doc_string (m_comments))
177  { }
178 
179  // Classdef events_list
180 
182  {
183  while (! empty ())
184  {
185  auto p = begin ();
186  delete *p;
187  erase (p);
188  }
189  }
190 
191  // Classdef events_block
192 
193  // Classdef enum
194 
196  tree_expression *e,
197  comment_list *comments)
198  : m_id (i), m_expr (e), m_comments (comments),
199  m_doc_string (check_for_doc_string (m_comments))
200  { }
201 
202  // Classdef enum_list
203 
205  {
206  while (! empty ())
207  {
208  auto p = begin ();
209  delete *p;
210  erase (p);
211  }
212  }
213 
214  // Classdef enum_block
215 
216  // Classdef body
217 
219  : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst ()
220  { }
221 
223  : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst (),
224  m_doc_string (pb ? get_doc_string (pb->leading_comment ()) : "")
225  {
226  append (pb);
227  }
228 
230  : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst (),
231  m_doc_string (mb ? get_doc_string (mb->leading_comment ()) : "")
232  {
233  append (mb);
234  }
235 
237  : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst (),
238  m_doc_string (evb ? get_doc_string (evb->leading_comment ()) : "")
239  {
240  append (evb);
241  }
242 
244  : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst (),
245  m_doc_string (enb ? get_doc_string (enb->leading_comment ()) : "")
246  {
247  append (enb);
248  }
249 
251  {
252  while (! m_properties_lst.empty ())
253  {
254  auto p = m_properties_lst.begin ();
255  delete *p;
256  m_properties_lst.erase (p);
257  }
258 
259  while (! m_methods_lst.empty ())
260  {
261  auto p = m_methods_lst.begin ();
262  delete *p;
263  m_methods_lst.erase (p);
264  }
265 
266  while (! m_events_lst.empty ())
267  {
268  auto p = m_events_lst.begin ();
269  delete *p;
270  m_events_lst.erase (p);
271  }
272 
273  while (! m_enum_lst.empty ())
274  {
275  auto p = m_enum_lst.begin ();
276  delete *p;
277  m_enum_lst.erase (p);
278  }
279  }
280 
281  std::string
283  {
284  // Grab the first comment from the list and use it as the doc string
285  // for this classdef body.
286 
287  if (comments)
288  {
289  comment_elt first_elt = comments->front ();
290 
291  return first_elt.text ();
292  }
293 
294  return "";
295  }
296 
297  // Classdef
298 
300  tree_classdef::make_meta_class (interpreter& interp, bool is_at_folder)
301  {
302  cdef_class cls = cdef_class::make_meta_class (interp, this, is_at_folder);
303 
304  if (cls.ok ())
305  return cls.get_constructor_function ();
306 
307  return octave_value ();
308  }
309 }
elt_type & front(void)
Definition: base-list.h:79
elt_type & back(void)
Definition: base-list.h:80
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:846
octave_value get_constructor_function(void)
Definition: cdef-class.h:344
bool ok(void) const
Definition: cdef-object.h:300
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
std::list< tree_classdef_properties_block * > m_properties_lst
Definition: pt-classdef.h:754
std::list< tree_classdef_enum_block * > m_enum_lst
Definition: pt-classdef.h:760
std::list< tree_classdef_methods_block * > m_methods_lst
Definition: pt-classdef.h:756
void append(tree_classdef_properties_block *pb)
Definition: pt-classdef.h:699
std::list< tree_classdef_events_block * > m_events_lst
Definition: pt-classdef.h:758
std::string get_doc_string(comment_list *comment) const
Definition: pt-classdef.cc:282
tree_classdef_enum(tree_identifier *i, tree_expression *e, comment_list *comments)
Definition: pt-classdef.cc:195
tree_classdef_event(tree_identifier *i=nullptr, comment_list *comments=nullptr)
Definition: pt-classdef.cc:173
tree_classdef_property(tree_identifier *i, comment_list *comments=nullptr)
Definition: pt-classdef.cc:140
octave_value make_meta_class(interpreter &interp, bool is_at_folder=false)
Definition: pt-classdef.cc:300
virtual void copy_base(const tree_expression &e)
Definition: pt-exp.h:131
bool is_postfix_indexed(void) const
Definition: pt-exp.h:92
tree_metaclass_query * dup(symbol_scope &scope) const
Definition: pt-classdef.cc:78
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-classdef.cc:89
octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)
Definition: pt-classdef.cc:52
tree_superclass_ref(void)=delete
tree_superclass_ref * dup(symbol_scope &scope) const
Definition: pt-classdef.cc:40
virtual int column(void) const
Definition: pt.h:62
static octave_value metaclass_query(const std::string &cls)
Definition: ov-classdef.cc:399
static octave_value superclass_ref(const std::string &meth, const std::string &cls)
Definition: ov-classdef.cc:392
octave_function * function_value(bool silent=false) const
static double f(double k, double l_nu, double c_pm)
Definition: randpoisson.cc:118
std::string check_for_doc_string(comment_list *comments)
Definition: pt-classdef.cc:124
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