GNU Octave 7.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-2022 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
38namespace octave
39{
40 tree_superclass_ref *
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
65 octave_function *f = tmp.function_value (true);
66
67 assert (f);
68
69 return f->call (tw, nargout);
70 }
71
72 // The superclass_ref function object will be indexed as part of the
73 // enclosing index expression.
74
75 return ovl (tmp);
76 }
77
80 {
83
84 new_mcq->copy_base (*this);
85
86 return new_mcq;
87 }
88
91 {
93 }
94
95 // Classdef attribute
96
97 // Classdef attribute_list
98
100 {
101 while (! empty ())
102 {
103 auto p = begin ();
104 delete *p;
105 erase (p);
106 }
107 }
108
109 // Classdef superclass
110
111 // Classdef superclass_list
112
114 {
115 while (! empty ())
116 {
117 auto p = begin ();
118 delete *p;
119 erase (p);
120 }
121 }
122
123 // Classdef property
124
125 std::string check_for_doc_string (comment_list *comments)
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 }
151
153 {
155
156 return dynamic_cast<tree_identifier *> (id_expr);
157 }
158
160 {
161 return m_av->initializer_expression ();
162 }
163
164 // Classdef property_list
165
167 {
168 while (! empty ())
169 {
170 auto p = begin ();
171 delete *p;
172 erase (p);
173 }
174 }
175
176 // Classdef properties_block
177
178 // Classdef methods_list
179
180 // Classdef methods_block
181
182 // Classdef event
183
185 comment_list *comments)
186 : m_id (i), m_comments (comments),
187 m_doc_string (check_for_doc_string (m_comments))
188 { }
189
190 // Classdef events_list
191
193 {
194 while (! empty ())
195 {
196 auto p = begin ();
197 delete *p;
198 erase (p);
199 }
200 }
201
202 // Classdef events_block
203
204 // Classdef enum
205
208 comment_list *comments)
209 : m_id (i), m_expr (e), m_comments (comments),
210 m_doc_string (check_for_doc_string (m_comments))
211 { }
212
213 // Classdef enum_list
214
216 {
217 while (! empty ())
218 {
219 auto p = begin ();
220 delete *p;
221 erase (p);
222 }
223 }
224
225 // Classdef enum_block
226
227 // Classdef body
228
230 : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst ()
231 { }
232
234 : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst (),
235 m_doc_string (pb ? get_doc_string (pb->leading_comment ()) : "")
236 {
237 append (pb);
238 }
239
241 : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst (),
242 m_doc_string (mb ? get_doc_string (mb->leading_comment ()) : "")
243 {
244 append (mb);
245 }
246
248 : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst (),
249 m_doc_string (evb ? get_doc_string (evb->leading_comment ()) : "")
250 {
251 append (evb);
252 }
253
255 : m_properties_lst (), m_methods_lst (), m_events_lst (), m_enum_lst (),
256 m_doc_string (enb ? get_doc_string (enb->leading_comment ()) : "")
257 {
258 append (enb);
259 }
260
262 {
263 while (! m_properties_lst.empty ())
264 {
265 auto p = m_properties_lst.begin ();
266 delete *p;
267 m_properties_lst.erase (p);
268 }
269
270 while (! m_methods_lst.empty ())
271 {
272 auto p = m_methods_lst.begin ();
273 delete *p;
274 m_methods_lst.erase (p);
275 }
276
277 while (! m_events_lst.empty ())
278 {
279 auto p = m_events_lst.begin ();
280 delete *p;
281 m_events_lst.erase (p);
282 }
283
284 while (! m_enum_lst.empty ())
285 {
286 auto p = m_enum_lst.begin ();
287 delete *p;
288 m_enum_lst.erase (p);
289 }
290 }
291
292 std::string
294 {
295 // Grab the first comment from the list and use it as the doc string
296 // for this classdef body.
297
298 if (comments)
299 {
300 comment_elt first_elt = comments->front ();
301
302 return first_elt.text ();
303 }
304
305 return "";
306 }
307
308 // Classdef
309
311 tree_classdef::make_meta_class (interpreter& interp, bool is_at_folder)
312 {
313 cdef_class cls = cdef_class::make_meta_class (interp, this, is_at_folder);
314
315 if (cls.ok ())
316 return cls.get_constructor_function ();
317
318 return octave_value ();
319 }
320}
elt_type & back(void)
Definition: base-list.h:80
elt_type & front(void)
Definition: base-list.h:79
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
octave_value get_constructor_function(void)
Definition: cdef-class.h:361
bool ok(void) const
Definition: cdef-object.h:310
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
tree_expression * identifier_expression(void)
tree_expression * initializer_expression(void)
std::list< tree_classdef_properties_block * > m_properties_lst
Definition: pt-classdef.h:749
std::list< tree_classdef_enum_block * > m_enum_lst
Definition: pt-classdef.h:755
std::list< tree_classdef_methods_block * > m_methods_lst
Definition: pt-classdef.h:751
void append(tree_classdef_properties_block *pb)
Definition: pt-classdef.h:694
std::list< tree_classdef_events_block * > m_events_lst
Definition: pt-classdef.h:753
std::string get_doc_string(comment_list *comment) const
Definition: pt-classdef.cc:293
tree_classdef_enum(tree_identifier *i, tree_expression *e, comment_list *comments)
Definition: pt-classdef.cc:206
tree_classdef_event(tree_identifier *i=nullptr, comment_list *comments=nullptr)
Definition: pt-classdef.cc:184
tree_identifier * ident(void)
Definition: pt-classdef.cc:152
tree_expression * expression(void)
Definition: pt-classdef.cc:159
tree_classdef_property(tree_arg_validation *av, comment_list *comments=nullptr)
Definition: pt-classdef.cc:141
tree_arg_validation * m_av
Definition: pt-classdef.h:352
octave_value make_meta_class(interpreter &interp, bool is_at_folder=false)
Definition: pt-classdef.cc:311
virtual void copy_base(const tree_expression &e)
Definition: pt-exp.h:133
bool is_postfix_indexed(void) const
Definition: pt-exp.h:94
tree_metaclass_query * dup(symbol_scope &scope) const
Definition: pt-classdef.cc:79
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-classdef.cc:90
octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)
Definition: pt-classdef.cc:53
tree_superclass_ref(void)=delete
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
static OCTINTERP_API octave_value metaclass_query(const std::string &cls)
Definition: ov-classdef.cc:399
static OCTINTERP_API octave_value superclass_ref(const std::string &meth, const std::string &cls)
Definition: ov-classdef.cc:392
OCTINTERP_API 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:125
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