ov-builtin.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include "error.h"
00028 #include "gripes.h"
00029 #include "oct-obj.h"
00030 #include "ov-builtin.h"
00031 #include "ov.h"
00032 #include "profiler.h"
00033 #include "toplev.h"
00034 #include "unwind-prot.h"
00035 
00036 DEFINE_OCTAVE_ALLOCATOR (octave_builtin);
00037 
00038 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_builtin,
00039                                      "built-in function",
00040                                      "built-in function");
00041 
00042 octave_value_list
00043 octave_builtin::subsref (const std::string& type,
00044                          const std::list<octave_value_list>& idx,
00045                          int nargout)
00046 {
00047   return octave_builtin::subsref (type, idx, nargout, 0);
00048 }
00049 
00050 octave_value_list
00051 octave_builtin::subsref (const std::string& type,
00052                          const std::list<octave_value_list>& idx,
00053                          int nargout, const std::list<octave_lvalue>* lvalue_list)
00054 {
00055   octave_value_list retval;
00056 
00057   switch (type[0])
00058     {
00059     case '(':
00060       {
00061         int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout;
00062 
00063         retval = do_multi_index_op (tmp_nargout, idx.front (),
00064                                     idx.size () == 1 ? lvalue_list : 0);
00065       }
00066       break;
00067 
00068     case '{':
00069     case '.':
00070       {
00071         std::string nm = type_name ();
00072         error ("%s cannot be indexed with %c", nm.c_str (), type[0]);
00073       }
00074       break;
00075 
00076     default:
00077       panic_impossible ();
00078     }
00079 
00080   // FIXME -- perhaps there should be an
00081   // octave_value_list::next_subsref member function?  See also
00082   // octave_user_function::subsref.
00083   //
00084   // FIXME -- Note that if a function call returns multiple
00085   // values, and there is further indexing to perform, then we are
00086   // ignoring all but the first value.  Is this really what we want to
00087   // do?  If it is not, then what should happen for stat("file").size,
00088   // for exmaple?
00089 
00090   if (idx.size () > 1)
00091     retval = retval(0).next_subsref (nargout, type, idx);
00092 
00093   return retval;
00094 }
00095 
00096 octave_value_list
00097 octave_builtin::do_multi_index_op (int nargout, const octave_value_list& args)
00098 {
00099   return octave_builtin::do_multi_index_op (nargout, args, 0);
00100 }
00101 
00102 octave_value_list
00103 octave_builtin::do_multi_index_op (int nargout, const octave_value_list& args,
00104                                    const std::list<octave_lvalue> *lvalue_list)
00105 {
00106   octave_value_list retval;
00107 
00108   if (error_state)
00109     return retval;
00110 
00111   if (args.has_magic_colon ())
00112     ::error ("invalid use of colon in function argument list");
00113   else
00114     {
00115       unwind_protect frame;
00116 
00117       octave_call_stack::push (this);
00118 
00119       frame.add_fcn (octave_call_stack::pop);
00120 
00121       if (lvalue_list || curr_lvalue_list)
00122         {
00123           frame.protect_var (curr_lvalue_list);
00124           curr_lvalue_list = lvalue_list;
00125         }
00126 
00127       try
00128         {
00129           BEGIN_PROFILER_BLOCK (profiler_name ())
00130 
00131           retval = (*f) (args, nargout);
00132           // Do not allow null values to be returned from functions.
00133           // FIXME -- perhaps true builtins should be allowed?
00134           retval.make_storable_values ();
00135           // Fix the case of a single undefined value.
00136           // This happens when a compiled function uses
00137           //   octave_value retval;
00138           // instead of
00139           //   octave_value_list retval;
00140           // the idiom is very common, so we solve that here.
00141           if (retval.length () == 1 && retval.xelem (0).is_undefined ())
00142             retval.clear ();
00143 
00144           END_PROFILER_BLOCK
00145         }
00146       catch (octave_execution_exception)
00147         {
00148           gripe_library_execution_error ();
00149         }
00150     }
00151 
00152   return retval;
00153 }
00154 
00155 
00156 const std::list<octave_lvalue> *octave_builtin::curr_lvalue_list = 0;
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines