GNU Octave  8.1.0
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-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 "error.h"
31 #include "input.h"
32 #include "ov-usr-fcn.h"
33 #include "pt-all.h"
34 
36 
37 void
39 {
40  auto p = lst.begin ();
41 
42  while (p != lst.end ())
43  {
44  tree_expression *elt = *p++;
45 
46  if (elt)
47  {
48  if (m_do_lvalue_check && ! elt->lvalue_ok ())
49  errmsg ("invalid lvalue in multiple assignment", elt->line ());
50  }
51  }
52 }
53 
54 void
56 {
57  tree_expression *lhs = cmd.left_hand_side ();
58 
59  if (lhs)
60  {
61  if (! lhs->lvalue_ok ())
62  errmsg ("invalid lvalue in for command", cmd.line ());
63  }
64 
65  tree_expression *expr = cmd.control_expr ();
66 
67  if (expr)
68  expr->accept (*this);
69 
70  tree_expression *maxproc = cmd.maxproc_expr ();
71 
72  if (maxproc)
73  maxproc->accept (*this);
74 
75  tree_statement_list *list = cmd.body ();
76 
77  if (list)
78  list->accept (*this);
79 }
80 
81 void
83 {
84  tree_argument_list *lhs = cmd.left_hand_side ();
85 
86  if (lhs)
87  {
88  int len = lhs->length ();
89 
90  if (len == 0 || len > 2)
91  errmsg ("invalid number of output arguments in for command",
92  cmd.line ());
93 
94  m_do_lvalue_check = true;
95 
96  lhs->accept (*this);
97 
98  m_do_lvalue_check = false;
99  }
100 
101  tree_expression *expr = cmd.control_expr ();
102 
103  if (expr)
104  expr->accept (*this);
105 
106  tree_statement_list *list = cmd.body ();
107 
108  if (list)
109  list->accept (*this);
110 }
111 
112 void
114 {
115  tree_argument_list *lhs = expr.left_hand_side ();
116 
117  if (lhs)
118  {
119  m_do_lvalue_check = true;
120 
121  lhs->accept (*this);
122 
123  m_do_lvalue_check = false;
124  }
125 
126  tree_expression *rhs = expr.right_hand_side ();
127 
128  if (rhs)
129  rhs->accept (*this);
130 }
131 
132 void
134 {
135  tree_expression *lhs = expr.left_hand_side ();
136 
137  if (lhs)
138  {
139  if (! lhs->lvalue_ok ())
140  errmsg ("invalid lvalue in assignment", expr.line ());
141  }
142 
143  tree_expression *rhs = expr.right_hand_side ();
144 
145  if (rhs)
146  rhs->accept (*this);
147 }
148 
149 void
151 {
152  tree_statement_list *try_code = cmd.body ();
153 
154  tree_identifier *expr_id = cmd.identifier ();
155 
156  if (expr_id)
157  {
158  if (! expr_id->lvalue_ok ())
159  errmsg ("invalid lvalue used for identifier in try-catch command",
160  cmd.line ());
161  }
162 
163  if (try_code)
164  try_code->accept (*this);
165 
166  tree_statement_list *catch_code = cmd.cleanup ();
167 
168  if (catch_code)
169  catch_code->accept (*this);
170 }
171 
172 void
173 tree_checker::errmsg (const std::string& msg, int line)
174 {
175  if (m_file_name.empty ())
176  error ("%s", msg.c_str ());
177  else
178  error ("%s: %d: %s", m_file_name.c_str (), line, msg.c_str ());
179 }
180 
OCTAVE_END_NAMESPACE(octave)
iterator end(void)
Definition: base-list.h:68
iterator begin(void)
Definition: base-list.h:65
std::size_t length(void) const
Definition: base-list.h:53
void accept(tree_walker &tw)
Definition: pt-arg-list.h:106
void visit_complex_for_command(tree_complex_for_command &)
Definition: pt-check.cc:82
bool m_do_lvalue_check
Definition: pt-check.h:70
OCTAVE_NORETURN void errmsg(const std::string &msg, int line)
Definition: pt-check.cc:173
void visit_simple_for_command(tree_simple_for_command &)
Definition: pt-check.cc:55
void visit_simple_assignment(tree_simple_assignment &)
Definition: pt-check.cc:133
void visit_multi_assignment(tree_multi_assignment &)
Definition: pt-check.cc:113
std::string m_file_name
Definition: pt-check.h:72
void visit_argument_list(tree_argument_list &)
Definition: pt-check.cc:38
void visit_try_catch_command(tree_try_catch_command &)
Definition: pt-check.cc:150
tree_statement_list * body(void)
Definition: pt-loop.h:249
tree_expression * control_expr(void)
Definition: pt-loop.h:247
tree_argument_list * left_hand_side(void)
Definition: pt-loop.h:245
virtual bool lvalue_ok(void) const
Definition: pt-exp.h:86
bool lvalue_ok(void) const
Definition: pt-id.h:82
tree_expression * right_hand_side(void)
Definition: pt-assign.h:150
tree_argument_list * left_hand_side(void)
Definition: pt-assign.h:148
tree_expression * right_hand_side(void)
Definition: pt-assign.h:79
tree_expression * left_hand_side(void)
Definition: pt-assign.h:77
tree_expression * left_hand_side(void)
Definition: pt-loop.h:177
tree_expression * maxproc_expr(void)
Definition: pt-loop.h:181
tree_statement_list * body(void)
Definition: pt-loop.h:183
tree_expression * control_expr(void)
Definition: pt-loop.h:179
void accept(tree_walker &tw)
Definition: pt-stmt.h:199
tree_statement_list * cleanup(void)
Definition: pt-except.h:74
tree_statement_list * body(void)
Definition: pt-except.h:72
tree_identifier * identifier(void)
Definition: pt-except.h:70
virtual int line(void) const
Definition: pt.h:60
virtual void accept(tree_walker &tw)=0
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void error(const char *fmt,...)
Definition: error.cc:979
F77_RET_T len
Definition: xerbla.cc:61