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