GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pt-check.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include "error.h"
28 #include "input.h"
29 #include "ov-usr-fcn.h"
30 #include "pt-all.h"
31 
32 namespace octave
33 {
34  void
36  {
38 
39  while (p != lst.end ())
40  {
41  tree_expression *elt = *p++;
42 
43  if (elt)
44  {
45  if (m_do_lvalue_check && ! elt->lvalue_ok ())
46  errmsg ("invalid lvalue in multiple assignment", elt->line ());
47  }
48  }
49  }
50 
51  void
53  {
54  tree_expression *op1 = expr.lhs ();
55 
56  if (op1)
57  op1->accept (*this);
58 
59  tree_expression *op2 = expr.rhs ();
60 
61  if (op2)
62  op2->accept (*this);
63  }
64 
65  void
67  { }
68 
69  void
71  {
72  tree_expression *op1 = expr.base ();
73 
74  if (op1)
75  op1->accept (*this);
76 
77  tree_expression *op3 = expr.increment ();
78 
79  if (op3)
80  op3->accept (*this);
81 
82  tree_expression *op2 = expr.limit ();
83 
84  if (op2)
85  op2->accept (*this);
86  }
87 
88  void
90  { }
91 
92  void
94  {
95  tree_decl_init_list *init_list = cmd.initializer_list ();
96 
97  if (init_list)
98  init_list->accept (*this);
99  }
100 
101  void
103  {
105 
106  while (p != lst.end ())
107  {
108  tree_decl_elt *elt = *p++;
109 
110  if (elt)
111  elt->accept (*this);
112  }
113  }
114 
115  void
117  {
118  tree_identifier *id = cmd.ident ();
119 
120  if (id)
121  id->accept (*this);
122 
123  tree_expression *expr = cmd.expression ();
124 
125  if (expr)
126  expr->accept (*this);
127  }
128 
129  void
131  {
132  tree_expression *lhs = cmd.left_hand_side ();
133 
134  if (lhs)
135  {
136  if (! lhs->lvalue_ok ())
137  errmsg ("invalid lvalue in for command", cmd.line ());
138  }
139 
140  tree_expression *expr = cmd.control_expr ();
141 
142  if (expr)
143  expr->accept (*this);
144 
145  tree_expression *maxproc = cmd.maxproc_expr ();
146 
147  if (maxproc)
148  maxproc->accept (*this);
149 
150  tree_statement_list *list = cmd.body ();
151 
152  if (list)
153  list->accept (*this);
154  }
155 
156  void
158  {
159  tree_argument_list *lhs = cmd.left_hand_side ();
160 
161  if (lhs)
162  {
163  int len = lhs->length ();
164 
165  if (len == 0 || len > 2)
166  errmsg ("invalid number of output arguments in for command",
167  cmd.line ());
168 
169  m_do_lvalue_check = true;
170 
171  lhs->accept (*this);
172 
173  m_do_lvalue_check = false;
174  }
175 
176  tree_expression *expr = cmd.control_expr ();
177 
178  if (expr)
179  expr->accept (*this);
180 
181  tree_statement_list *list = cmd.body ();
182 
183  if (list)
184  list->accept (*this);
185  }
186 
187  void
189  {
190  tree_statement_list *cmd_list = fcn.body ();
191 
192  if (cmd_list)
193  cmd_list->accept (*this);
194  }
195 
196  void
198  {
199  tree_statement_list *cmd_list = fcn.body ();
200 
201  if (cmd_list)
202  cmd_list->accept (*this);
203  }
204 
205  void
207  {
208  octave_value fcn = fdef.function ();
209 
211 
212  if (f)
213  f->accept (*this);
214  }
215 
216  void
218  { }
219 
220  void
222  {
223  tree_expression *expr = cmd.condition ();
224 
225  if (expr)
226  expr->accept (*this);
227 
229 
230  if (list)
231  list->accept (*this);
232  }
233 
234  void
236  {
238 
239  if (list)
240  list->accept (*this);
241  }
242 
243  void
245  {
247 
248  while (p != lst.end ())
249  {
250  tree_if_clause *elt = *p++;
251 
252  if (elt)
253  elt->accept (*this);
254  }
255  }
256 
257  void
259  {
260  tree_expression *e = expr.expression ();
261 
262  if (e)
263  e->accept (*this);
264 
265  std::list<tree_argument_list *> lst = expr.arg_lists ();
266 
267  std::list<tree_argument_list *>::iterator p = lst.begin ();
268 
269  while (p != lst.end ())
270  {
271  tree_argument_list *elt = *p++;
272 
273  if (elt)
274  elt->accept (*this);
275  }
276  }
277 
278  void
280  {
281  tree_matrix::iterator p = lst.begin ();
282 
283  while (p != lst.end ())
284  {
285  tree_argument_list *elt = *p++;
286 
287  if (elt)
288  elt->accept (*this);
289  }
290  }
291 
292  void
294  {
295  tree_matrix::iterator p = lst.begin ();
296 
297  while (p != lst.end ())
298  {
299  tree_argument_list *elt = *p++;
300 
301  if (elt)
302  elt->accept (*this);
303  }
304  }
305 
306  void
308  {
309  tree_argument_list *lhs = expr.left_hand_side ();
310 
311  if (lhs)
312  {
313  m_do_lvalue_check = true;
314 
315  lhs->accept (*this);
316 
317  m_do_lvalue_check = false;
318  }
319 
320  tree_expression *rhs = expr.right_hand_side ();
321 
322  if (rhs)
323  rhs->accept (*this);
324  }
325 
326  void
328  { }
329 
330  void
332  { }
333 
334  void
336  { }
337 
338  void
340  { }
341 
342  void
344  { }
345 
346  void
348  {
350 
351  while (p != lst.end ())
352  {
353  tree_decl_elt *elt = *p++;
354 
355  if (elt)
356  elt->accept (*this);
357  }
358  }
359 
360  void
362  {
363  tree_expression *e = expr.operand ();
364 
365  if (e)
366  e->accept (*this);
367  }
368 
369  void
371  {
372  tree_expression *e = expr.operand ();
373 
374  if (e)
375  e->accept (*this);
376  }
377 
378  void
380  { }
381 
382  void
384  {
386 
387  while (p != lst.end ())
388  {
389  tree_index_expression *elt = *p++;
390 
391  if (elt)
392  elt->accept (*this);
393  }
394  }
395 
396  void
398  {
399  tree_expression *lhs = expr.left_hand_side ();
400 
401  if (lhs)
402  {
403  if (! lhs->lvalue_ok ())
404  errmsg ("invalid lvalue in assignment", expr.line ());
405  }
406 
407  tree_expression *rhs = expr.right_hand_side ();
408 
409  if (rhs)
410  rhs->accept (*this);
411  }
412 
413  void
415  {
416  tree_command *cmd = stmt.command ();
417 
418  if (cmd)
419  cmd->accept (*this);
420  else
421  {
422  tree_expression *expr = stmt.expression ();
423 
424  if (expr)
425  expr->accept (*this);
426  }
427  }
428 
429  void
431  {
432  for (tree_statement *elt : lst)
433  {
434  if (elt)
435  elt->accept (*this);
436  }
437  }
438 
439  void
441  {
442  tree_expression *label = cs.case_label ();
443 
444  if (label)
445  label->accept (*this);
446 
447  tree_statement_list *list = cs.commands ();
448 
449  if (list)
450  list->accept (*this);
451  }
452 
453  void
455  {
457 
458  while (p != lst.end ())
459  {
460  tree_switch_case *elt = *p++;
461 
462  if (elt)
463  elt->accept (*this);
464  }
465  }
466 
467  void
469  {
470  tree_expression *expr = cmd.switch_value ();
471 
472  if (expr)
473  expr->accept (*this);
474 
476 
477  if (list)
478  list->accept (*this);
479  }
480 
481  void
483  {
484  tree_statement_list *try_code = cmd.body ();
485 
486  tree_identifier *expr_id = cmd.identifier ();
487 
488  if (expr_id)
489  {
490  if (! expr_id->lvalue_ok ())
491  errmsg ("invalid lvalue used for identifier in try-catch command",
492  cmd.line ());
493  }
494 
495  if (try_code)
496  try_code->accept (*this);
497 
498  tree_statement_list *catch_code = cmd.cleanup ();
499 
500  if (catch_code)
501  catch_code->accept (*this);
502  }
503 
504  void
506  {
507  tree_statement_list *unwind_protect_code = cmd.body ();
508 
509  if (unwind_protect_code)
510  unwind_protect_code->accept (*this);
511 
512  tree_statement_list *cleanup_code = cmd.cleanup ();
513 
514  if (cleanup_code)
515  cleanup_code->accept (*this);
516  }
517 
518  void
520  {
521  tree_expression *expr = cmd.condition ();
522 
523  if (expr)
524  expr->accept (*this);
525 
526  tree_statement_list *list = cmd.body ();
527 
528  if (list)
529  list->accept (*this);
530  }
531 
532  void
534  {
535  tree_statement_list *list = cmd.body ();
536 
537  if (list)
538  list->accept (*this);
539 
540  tree_expression *expr = cmd.condition ();
541 
542  if (expr)
543  expr->accept (*this);
544  }
545 
546  void
548  {
549  if (m_file_name.empty ())
550  error ("%s", msg.c_str ());
551  else
552  error ("%s: %d: %s", m_file_name.c_str (), line, msg.c_str ());
553  }
554 }
void visit_continue_command(tree_continue_command &)
Definition: pt-check.cc:89
virtual int line(void) const
Definition: pt.h:56
tree_expression * right_hand_side(void)
Definition: pt-assign.h:78
std::string m_file_name
Definition: pt-check.h:139
void visit_while_command(tree_while_command &)
Definition: pt-check.cc:519
void visit_simple_for_command(tree_simple_for_command &)
Definition: pt-check.cc:130
OCTAVE_NORETURN void errmsg(const std::string &msg, int line)
Definition: pt-check.cc:547
tree_statement_list * cleanup(void)
Definition: pt-except.h:138
The value of lines which begin with a space character are not saved in the history list A value of all commands are saved on the history list
Definition: oct-hist.cc:734
tree_expression * base(void)
Definition: pt-colon.h:88
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE * f
void visit_if_command_list(tree_if_command_list &)
Definition: pt-check.cc:244
void visit_function_def(tree_function_def &)
Definition: pt-check.cc:206
void visit_identifier(tree_identifier &)
Definition: pt-check.cc:217
void visit_switch_case_list(tree_switch_case_list &)
Definition: pt-check.cc:454
void visit_constant(tree_constant &)
Definition: pt-check.cc:335
virtual void accept(tree_walker &tw)=0
size_t length(void) const
Definition: base-list.h:50
void error(const char *fmt,...)
Definition: error.cc:578
void visit_statement(tree_statement &)
Definition: pt-check.cc:414
tree_decl_init_list * initializer_list(void)
Definition: pt-decl.h:211
tree_statement_list * body(void)
Definition: pt-loop.h:87
std::list< tree_expression * >::iterator iterator
Definition: base-list.h:40
octave_value function(void)
Definition: pt-cmd.h:116
void visit_do_until_command(tree_do_until_command &)
Definition: pt-check.cc:533
void accept(tree_walker &tw)
Definition: pt-select.h:72
void visit_octave_user_script(octave_user_script &)
Definition: pt-check.cc:188
i e
Definition: data.cc:2591
void visit_statement_list(tree_statement_list &)
Definition: pt-check.cc:430
octave_function * fcn
Definition: ov-class.cc:1754
void visit_multi_assignment(tree_multi_assignment &)
Definition: pt-check.cc:307
void visit_binary_expression(tree_binary_expression &)
Definition: pt-check.cc:52
void visit_no_op_command(tree_no_op_command &)
Definition: pt-check.cc:327
tree_statement_list * commands(void)
Definition: pt-select.h:68
octave::call_stack & cs
Definition: ov-class.cc:1752
void accept(tree_walker &tw)
Definition: pt-decl.h:173
iterator end(void)
Definition: base-list.h:86
tree_expression * expression(void)
Definition: pt-idx.h:82
void visit_simple_assignment(tree_simple_assignment &)
Definition: pt-check.cc:397
void visit_unwind_protect_command(tree_unwind_protect_command &)
Definition: pt-check.cc:505
virtual bool lvalue_ok(void) const
Definition: pt-exp.h:83
void visit_return_command(tree_return_command &)
Definition: pt-check.cc:379
void visit_cell(tree_cell &)
Definition: pt-check.cc:293
std::list< tree_argument_list * > arg_lists(void)
Definition: pt-idx.h:84
void visit_anon_fcn_handle(tree_anon_fcn_handle &)
Definition: pt-check.cc:331
tree_expression * control_expr(void)
Definition: pt-loop.h:293
tree_expression * control_expr(void)
Definition: pt-loop.h:207
void accept(tree_walker &tw)
Definition: pt-select.h:197
tree_statement_list * cleanup(void)
Definition: pt-except.h:71
tree_expression * left_hand_side(void)
Definition: pt-assign.h:76
void accept(tree_walker &tw)
Definition: pt-stmt.h:188
void visit_octave_user_function(octave_user_function &)
Definition: pt-check.cc:197
tree_expression * maxproc_expr(void)
Definition: pt-loop.h:209
void visit_switch_command(tree_switch_command &)
Definition: pt-check.cc:468
tree_expression * expression(void)
Definition: pt-decl.h:102
base_list< tree_argument_list * >::iterator iterator
Definition: pt-array-list.h:44
void visit_decl_elt(tree_decl_elt &)
Definition: pt-check.cc:116
tree_expression * increment(void)
Definition: pt-colon.h:92
void visit_if_command(tree_if_command &)
Definition: pt-check.cc:235
tree_command * command(void)
Definition: pt-stmt.h:90
void visit_prefix_expression(tree_prefix_expression &)
Definition: pt-check.cc:370
void visit_complex_for_command(tree_complex_for_command &)
Definition: pt-check.cc:157
void visit_postfix_expression(tree_postfix_expression &)
Definition: pt-check.cc:361
void visit_try_catch_command(tree_try_catch_command &)
Definition: pt-check.cc:482
void accept(tree_walker &tw)
Definition: pt-decl.h:106
void visit_switch_case(tree_switch_case &)
Definition: pt-check.cc:440
tree_expression * switch_value(void)
Definition: pt-select.h:266
tree_expression * right_hand_side(void)
Definition: pt-assign.h:147
void visit_colon_expression(tree_colon_expression &)
Definition: pt-check.cc:70
bool lvalue_ok(void) const
Definition: pt-id.h:118
tree_statement_list * body(void)
Definition: pt-except.h:136
tree_statement_list * body(void)
Definition: pt-loop.h:211
tree_expression * rhs(void)
Definition: pt-binop.h:100
void visit_matrix(tree_matrix &)
Definition: pt-check.cc:279
void visit_funcall(tree_funcall &)
Definition: pt-check.cc:343
void accept(tree_walker &tw)
Definition: pt-idx.h:100
tree_statement_list * body(void)
Definition: pt-loop.h:295
tree_expression * left_hand_side(void)
Definition: pt-loop.h:205
p
Definition: lu.cc:138
void visit_decl_init_list(tree_decl_init_list &)
Definition: pt-check.cc:102
void visit_break_command(tree_break_command &)
Definition: pt-check.cc:66
void visit_return_list(tree_return_list &)
Definition: pt-check.cc:383
tree_argument_list * left_hand_side(void)
Definition: pt-assign.h:145
void accept(tree_walker &tw)
Definition: pt-id.h:132
tree_expression * lhs(void)
Definition: pt-binop.h:99
void visit_parameter_list(tree_parameter_list &)
Definition: pt-check.cc:347
virtual octave_function * function_value(bool silent=false)
Definition: ov-base.cc:871
tree_expression * operand(void)
Definition: pt-unop.h:70
void accept(tree_walker &tw)
Definition: pt-arg-list.h:103
tree_identifier * ident(void)
Definition: pt-decl.h:98
void visit_decl_command(tree_decl_command &)
Definition: pt-check.cc:93
void visit_fcn_handle(tree_fcn_handle &)
Definition: pt-check.cc:339
tree_expression * condition(void)
Definition: pt-loop.h:85
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
void visit_if_clause(tree_if_clause &)
Definition: pt-check.cc:221
void visit_index_expression(tree_index_expression &)
Definition: pt-check.cc:258
tree_statement_list * body(void)
Definition: pt-except.h:69
iterator begin(void)
Definition: base-list.h:83
void visit_argument_list(tree_argument_list &)
Definition: pt-check.cc:35
tree_switch_case_list * case_list(void)
Definition: pt-select.h:268
tree_argument_list * left_hand_side(void)
Definition: pt-loop.h:291
tree_if_command_list * cmd_list(void)
Definition: pt-select.h:140
tree_expression * condition(void)
Definition: pt-select.h:66
tree_identifier * identifier(void)
Definition: pt-except.h:67
tree_expression * limit(void)
Definition: pt-colon.h:90
tree_expression * expression(void)
Definition: pt-stmt.h:92