GNU Octave  9.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-2024 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 
181 OCTAVE_END_NAMESPACE(octave)
std::size_t length() const
Definition: base-list.h:53
iterator end()
Definition: base-list.h:68
iterator begin()
Definition: base-list.h:65
void accept(tree_walker &tw)
Definition: pt-arg-list.h:102
void visit_complex_for_command(tree_complex_for_command &)
Definition: pt-check.cc:82
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
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()
Definition: pt-loop.h:233
tree_argument_list * left_hand_side()
Definition: pt-loop.h:229
tree_expression * control_expr()
Definition: pt-loop.h:231
virtual bool lvalue_ok() const
Definition: pt-exp.h:82
bool lvalue_ok() const
Definition: pt-id.h:78
tree_argument_list * left_hand_side()
Definition: pt-assign.h:140
tree_expression * right_hand_side()
Definition: pt-assign.h:142
tree_expression * left_hand_side()
Definition: pt-assign.h:73
tree_expression * right_hand_side()
Definition: pt-assign.h:75
tree_expression * left_hand_side()
Definition: pt-loop.h:165
tree_expression * control_expr()
Definition: pt-loop.h:167
tree_expression * maxproc_expr()
Definition: pt-loop.h:169
tree_statement_list * body()
Definition: pt-loop.h:171
void accept(tree_walker &tw)
Definition: pt-stmt.h:191
tree_statement_list * cleanup()
Definition: pt-except.h:70
tree_identifier * identifier()
Definition: pt-except.h:66
tree_statement_list * body()
Definition: pt-except.h:68
virtual int line() const
Definition: pt.h:56
virtual void accept(tree_walker &tw)=0
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void() error(const char *fmt,...)
Definition: error.cc:988
F77_RET_T len
Definition: xerbla.cc:61