GNU Octave  4.2.1
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-misc.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1994-2017 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 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include "Cell.h"
28 
29 #include "defun.h"
30 #include "error.h"
31 #include "ov.h"
32 #include "oct-lvalue.h"
33 #include "pt-id.h"
34 #include "pt-idx.h"
35 #include "pt-misc.h"
36 #include "pt-walk.h"
37 #include "utils.h"
38 
39 // Parameter lists.
40 
42 {
43  while (! empty ())
44  {
45  iterator p = begin ();
46  delete *p;
47  erase (p);
48  }
49 }
50 
51 void
53 {
54  for (iterator p = begin (); p != end (); p++)
55  {
56  tree_decl_elt *elt = *p;
58  }
59 }
60 
61 bool
63 {
64  bool retval = true;
65 
66  std::set<std::string> dict;
67 
68  for (iterator p = begin (); p != end (); p++)
69  {
70  tree_decl_elt *elt = *p;
71 
72  tree_identifier *id = elt->ident ();
73 
74  if (id)
75  {
76  std::string name = id->name ();
77 
78  if (id->is_black_hole ())
79  {
80  if (type != in)
81  error ("invalid use of ~ in output list");
82  }
83  else if (dict.find (name) != dict.end ())
84  error ("'%s' appears more than once in parameter list",
85  name.c_str ());
86  else
87  dict.insert (name);
88  }
89  }
90 
91  std::string va_type = (type == in ? "varargin" : "varargout");
92 
93  size_t len = length ();
94 
95  if (len > 0)
96  {
97  tree_decl_elt *elt = back ();
98 
99  tree_identifier *id = elt->ident ();
100 
101  if (id && id->name () == va_type)
102  {
103  if (len == 1)
105  else
106  mark_varargs ();
107 
108  iterator p = end ();
109  --p;
110  delete *p;
111  erase (p);
112  }
113  }
114 
115  return retval;
116 }
117 
118 void
120  int nargout,
121  const octave_value& val)
122 {
123  bool warned = false;
124 
125  int count = 0;
126 
127  octave_value tmp = symbol_table::varval (".ignored.");
128  const Matrix ignored = tmp.is_defined () ? tmp.matrix_value () : Matrix ();
129 
130  octave_idx_type k = 0;
131 
132  for (iterator p = begin (); p != end (); p++)
133  {
134  if (++count > nargout)
135  break;
136 
137  tree_decl_elt *elt = *p;
138 
139  if (! elt->is_variable ())
140  {
141  if (! warned)
142  {
143  warned = true;
144 
145  while (k < ignored.numel ())
146  {
147  octave_idx_type l = ignored (k);
148  if (l == count)
149  {
150  warned = false;
151  break;
152  }
153  else if (l > count)
154  break;
155  else
156  k++;
157  }
158 
159  if (warned)
160  {
162  ("Octave:undefined-return-values",
163  "%s: some elements in list of return values are undefined",
164  warnfor.c_str ());
165  }
166  }
167 
168  octave_lvalue lval = elt->lvalue ();
169 
170  lval.assign (octave_value::op_asn_eq, val);
171  }
172  }
173 }
174 
175 void
177 {
178  int expected_nargin = length ();
179 
180  iterator p = begin ();
181 
182  for (int i = 0; i < expected_nargin; i++)
183  {
184  tree_decl_elt *elt = *p++;
185 
186  octave_lvalue ref = elt->lvalue ();
187 
188  if (i < args.length ())
189  {
190  if (args(i).is_defined () && args(i).is_magic_colon ())
191  {
192  if (! elt->eval ())
193  error ("no default value for argument %d", i+1);
194  }
195  else
196  ref.define (args(i));
197  }
198  else
199  elt->eval ();
200  }
201 }
202 
203 void
205 {
206  int len = length ();
207 
208  iterator p = begin ();
209 
210  for (int i = 0; i < len; i++)
211  {
212  tree_decl_elt *elt = *p++;
213 
214  octave_lvalue ref = elt->lvalue ();
215 
217  }
218 }
219 
220 std::list<std::string>
222 {
223  std::list<std::string> retval;
224 
225  for (const_iterator p = begin (); p != end (); p++)
226  {
227  tree_decl_elt *elt = *p;
228 
229  retval.push_back (elt->name ());
230  }
231 
232  return retval;
233 }
234 
237  const Cell& varargout)
238 {
239  octave_idx_type vlen = varargout.numel ();
240  int len = length ();
241 
242  // Special case. Will do a shallow copy.
243  if (len == 0)
244  return varargout;
245  else if (nargout <= len)
246  {
247  octave_value_list retval (nargout);
248 
249  int i = 0;
250 
251  for (iterator p = begin (); p != end (); p++)
252  {
253  tree_decl_elt *elt = *p;
254  if (elt->is_defined ())
255  retval(i++) = elt->rvalue1 ();
256  else
257  break;
258  }
259 
260  return retval;
261  }
262  else
263  {
264  octave_value_list retval (len + vlen);
265 
266  int i = 0;
267 
268  for (iterator p = begin (); p != end (); p++)
269  {
270  tree_decl_elt *elt = *p;
271  retval(i++) = elt->rvalue1 ();
272  }
273 
274  for (octave_idx_type j = 0; j < vlen; j++)
275  retval(i++) = varargout(j);
276 
277  return retval;
278  }
279 }
280 
281 bool
283 {
284  bool status = true;
285 
286  for (iterator p = begin (); p != end (); p++)
287  {
288  tree_decl_elt *elt = *p;
289 
290  if (! elt->is_variable ())
291  {
292  status = false;
293  break;
294  }
295  }
296 
297  return status;
298 }
299 
303 {
304  tree_parameter_list *new_list = new tree_parameter_list ();
305 
306  if (takes_varargs ())
307  new_list->mark_varargs ();
308 
309  for (const_iterator p = begin (); p != end (); p++)
310  {
311  const tree_decl_elt *elt = *p;
312 
313  new_list->append (elt->dup (scope, context));
314  }
315 
316  return new_list;
317 }
318 
319 void
321 {
322  tw.visit_parameter_list (*this);
323 }
324 
325 // Return lists.
326 
328 {
329  while (! empty ())
330  {
331  iterator p = begin ();
332  delete *p;
333  erase (p);
334  }
335 }
336 
340 {
341  tree_return_list *new_list = new tree_return_list ();
342 
343  for (const_iterator p = begin (); p != end (); p++)
344  {
345  const tree_index_expression *elt = *p;
346 
347  new_list->append (elt->dup (scope, context));
348  }
349 
350  return new_list;
351 }
352 
353 void
355 {
356  tw.visit_return_list (*this);
357 }
bool is_variable(void)
Definition: pt-decl.h:57
void warning_with_id(const char *id, const char *fmt,...)
Definition: error.cc:803
Definition: Cell.h:37
std::list< std::string > variable_names(void) const
Definition: pt-misc.cc:221
tree_decl_elt * dup(symbol_table::scope_id scope, symbol_table::context_id context) const
Definition: pt-decl.cc:68
bool validate(in_or_out type)
Definition: pt-misc.cc:62
std::string name(void)
Definition: pt-decl.h:89
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:363
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:5068
octave_idx_type length(void) const
Definition: ovl.h:96
bool is_defined(void) const
Definition: ov.h:536
for large enough k
Definition: lu.cc:606
void error(const char *fmt,...)
Definition: error.cc:570
void mark_varargs(void)
Definition: pt-misc.h:100
tree_decl_elt *& back(void)
Definition: base-list.h:98
tree_return_list * dup(symbol_table::scope_id scope, symbol_table::context_id context) const
Definition: pt-misc.cc:338
std::list< tree_decl_elt * >::iterator iterator
Definition: base-list.h:40
iterator erase(iterator pos)
Definition: base-list.h:52
virtual void visit_return_list(tree_return_list &)=0
octave_value rvalue1(int nargout=1)
Definition: pt-decl.h:68
tree_parameter_list * dup(symbol_table::scope_id scope, symbol_table::context_id context) const
Definition: pt-misc.cc:301
static octave_value varval(const std::string &name, scope_id scope=xcurrent_scope, context_id context=xdefault_context)
Definition: symtab.h:1373
tree_identifier * ident(void)
Definition: pt-decl.h:87
~tree_parameter_list(void)
Definition: pt-misc.cc:41
JNIEnv void * args
Definition: ov-java.cc:67
void define_from_arg_vector(const octave_value_list &args)
Definition: pt-misc.cc:176
void define(const octave_value &v)
Definition: oct-lvalue.h:83
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable name
Definition: input.cc:871
bool is_defined(void)
Definition: pt-misc.cc:282
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function xample nargout(@histc)
Definition: ov-usr-fcn.cc:935
virtual void visit_parameter_list(tree_parameter_list &)=0
static llvm::LLVMContext & context
Definition: jit-typeinfo.cc:76
std::string name(void) const
Definition: pt-id.h:67
octave_value_list convert_to_const_vector(int nargout, const Cell &varargout)
Definition: pt-misc.cc:236
double tmp
Definition: data.cc:6300
void mark_as_formal_parameters(void)
Definition: pt-misc.cc:52
octave_value retval
Definition: data.cc:6294
virtual bool is_black_hole(void)
Definition: pt-id.h:73
bool is_defined(void)
Definition: pt-decl.h:55
tree_return_list(void)
Definition: pt-misc.h:119
idx type
Definition: ov.cc:3129
Definition: dMatrix.h:37
tree_parameter_list(void)
Definition: pt-misc.h:58
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:787
~tree_return_list(void)
Definition: pt-misc.cc:327
octave_lvalue lvalue(void)
Definition: pt-decl.h:85
bool takes_varargs(void) const
Definition: pt-misc.h:73
void mark_as_formal_parameter(void)
Definition: pt-decl.h:59
std::list< tree_decl_elt * >::const_iterator const_iterator
Definition: base-list.h:41
void assign(octave_value::assign_op, const octave_value &)
Definition: oct-lvalue.cc:33
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
p
Definition: lu.cc:138
void initialize_undefined_elements(const std::string &warnfor, int nargout, const octave_value &val)
Definition: pt-misc.cc:119
void append(const elt_type &s)
Definition: base-list.h:110
bool eval(void)
Definition: pt-decl.cc:49
void mark_varargs_only(void)
Definition: pt-misc.h:102
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:854
void accept(tree_walker &tw)
Definition: pt-misc.cc:320
void undefine(void)
Definition: pt-misc.cc:204
tree_index_expression * dup(symbol_table::scope_id scope, symbol_table::context_id context) const
Definition: pt-idx.cc:659
void accept(tree_walker &tw)
Definition: pt-misc.cc:354