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
ov-builtin.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 "gripes.h"
29 #include "oct-obj.h"
30 #include "ov-builtin.h"
31 #include "ov.h"
32 #include "profiler.h"
33 #include "toplev.h"
34 #include "unwind-prot.h"
35 
36 
38  "built-in function",
39  "built-in function");
40 
42 octave_builtin::subsref (const std::string& type,
43  const std::list<octave_value_list>& idx,
44  int nargout)
45 {
46  return octave_builtin::subsref (type, idx, nargout, 0);
47 }
48 
50 octave_builtin::subsref (const std::string& type,
51  const std::list<octave_value_list>& idx,
52  int nargout,
53  const std::list<octave_lvalue>* lvalue_list)
54 {
55  octave_value_list retval;
56 
57  switch (type[0])
58  {
59  case '(':
60  {
61  int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout;
62 
63  retval = do_multi_index_op (tmp_nargout, idx.front (),
64  idx.size () == 1 ? lvalue_list : 0);
65  }
66  break;
67 
68  case '{':
69  case '.':
70  {
71  std::string nm = type_name ();
72  error ("%s cannot be indexed with %c", nm.c_str (), type[0]);
73  }
74  break;
75 
76  default:
78  }
79 
80  // FIXME: perhaps there should be an
81  // octave_value_list::next_subsref member function? See also
82  // octave_user_function::subsref.
83  //
84  // FIXME: Note that if a function call returns multiple
85  // values, and there is further indexing to perform, then we are
86  // ignoring all but the first value. Is this really what we want to
87  // do? If it is not, then what should happen for stat("file").size,
88  // for exmaple?
89 
90  if (idx.size () > 1)
91  retval = retval(0).next_subsref (nargout, type, idx);
92 
93  return retval;
94 }
95 
98 {
99  return octave_builtin::do_multi_index_op (nargout, args, 0);
100 }
101 
104  const std::list<octave_lvalue> *lvalue_list)
105 {
106  octave_value_list retval;
107 
108  if (error_state)
109  return retval;
110 
111  if (args.has_magic_colon ())
112  ::error ("invalid use of colon in function argument list");
113  else
114  {
115  unwind_protect frame;
116 
118 
120 
121  if (lvalue_list || curr_lvalue_list)
122  {
124  curr_lvalue_list = lvalue_list;
125  }
126 
127  try
128  {
130 
131  retval = (*f) (args, nargout);
132  // Do not allow null values to be returned from functions.
133  // FIXME: perhaps true builtins should be allowed?
134  retval.make_storable_values ();
135  // Fix the case of a single undefined value.
136  // This happens when a compiled function uses
137  // octave_value retval;
138  // instead of
139  // octave_value_list retval;
140  // the idiom is very common, so we solve that here.
141  if (retval.length () == 1 && retval.xelem (0).is_undefined ())
142  retval.clear ();
143 
145  }
146  catch (octave_execution_exception)
147  {
149  }
150  }
151 
152  return retval;
153 }
154 
155 jit_type *
157 {
158  return jtype;
159 }
160 
161 void
163 {
164  jtype = &type;
165 }
166 
169 {
170  return f;
171 }
172 
173 const std::list<octave_lvalue> *octave_builtin::curr_lvalue_list = 0;
void stash_jit(jit_type &type)
Definition: ov-builtin.cc:162
void clear(void)
Definition: oct-obj.h:148
octave_value_list(* fcn)(const octave_value_list &, int)
Definition: ov-builtin.h:45
jit_type * to_jit(void) const
Definition: ov-builtin.cc:156
octave_idx_type length(void) const
Definition: oct-obj.h:89
jit_type * jtype
Definition: ov-builtin.h:103
void protect_var(T &var)
void error(const char *fmt,...)
Definition: error.cc:476
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:164
void gripe_library_execution_error(void)
Definition: gripes.cc:208
#define END_PROFILER_BLOCK
Definition: profiler.h:213
void make_storable_values(void)
Definition: oct-obj.cc:272
static const std::list< octave_lvalue > * curr_lvalue_list
Definition: ov-builtin.h:92
void add_fcn(void(*fcn)(void))
fcn function(void) const
Definition: ov-builtin.cc:168
int error_state
Definition: error.cc:101
bool has_magic_colon(void) const
Definition: oct-obj.cc:208
#define panic_impossible()
Definition: error.h:33
OCTAVE_EMPTY_CPP_ARG std::string type_name(void) const
Definition: ov-builtin.h:114
static void push(octave_function *f, symbol_table::scope_id scope=symbol_table::current_scope(), symbol_table::context_id context=symbol_table::current_context())
Definition: toplev.h:233
bool is_undefined(void) const
Definition: ov.h:523
static void pop(void)
Definition: toplev.h:332
octave_value_list do_multi_index_op(int nargout, const octave_value_list &args)
Definition: ov-builtin.cc:97
octave_value & xelem(octave_idx_type i)
Definition: oct-obj.h:143
#define BEGIN_PROFILER_BLOCK(classname)
Definition: profiler.h:209
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-builtin.h:59