577
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
577
|
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 2, or (at your option) any |
|
10 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, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
577
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
577
|
26 #endif |
|
27 |
4466
|
28 #include "defun.h" |
2982
|
29 #include "error.h" |
|
30 #include "ov.h" |
|
31 #include "oct-lvalue.h" |
|
32 #include "pt-id.h" |
|
33 #include "pt-idx.h" |
|
34 #include "pt-misc.h" |
|
35 #include "pt-walk.h" |
4466
|
36 #include "utils.h" |
|
37 |
|
38 // If TRUE, print a warning if a function does not define all the |
|
39 // values in the return list which are expected. |
|
40 static bool Vwarn_undefined_return_values; |
581
|
41 |
577
|
42 // Parameter lists. |
|
43 |
1742
|
44 tree_parameter_list::~tree_parameter_list (void) |
|
45 { |
4219
|
46 while (! empty ()) |
1742
|
47 { |
4219
|
48 iterator p = begin (); |
|
49 delete *p; |
|
50 erase (p); |
1742
|
51 } |
|
52 } |
|
53 |
577
|
54 void |
|
55 tree_parameter_list::mark_as_formal_parameters (void) |
|
56 { |
4219
|
57 for (iterator p = begin (); p != end (); p++) |
577
|
58 { |
4219
|
59 tree_identifier *elt = *p; |
577
|
60 elt->mark_as_formal_parameter (); |
|
61 } |
|
62 } |
|
63 |
|
64 void |
4466
|
65 tree_parameter_list::initialize_undefined_elements (const std::string& warnfor, |
|
66 int nargout, |
|
67 const octave_value& val) |
1093
|
68 { |
4466
|
69 bool warned = false; |
|
70 |
|
71 int count = 0; |
|
72 |
4219
|
73 for (iterator p = begin (); p != end (); p++) |
1093
|
74 { |
4466
|
75 if (++count > nargout) |
|
76 break; |
|
77 |
4219
|
78 tree_identifier *elt = *p; |
2948
|
79 |
1093
|
80 if (! elt->is_defined ()) |
2973
|
81 { |
4466
|
82 if (Vwarn_undefined_return_values && ! warned) |
|
83 { |
|
84 warned = true; |
|
85 warning ("%s: some elements in list of return values are undefined", |
|
86 warnfor.c_str ()); |
|
87 } |
|
88 |
2979
|
89 octave_lvalue tmp = elt->lvalue (); |
2973
|
90 |
3538
|
91 tmp.assign (octave_value::op_asn_eq, val); |
2973
|
92 } |
1093
|
93 } |
|
94 } |
|
95 |
|
96 void |
2086
|
97 tree_parameter_list::define_from_arg_vector (const octave_value_list& args) |
577
|
98 { |
712
|
99 int nargin = args.length (); |
|
100 |
|
101 if (nargin <= 0) |
577
|
102 return; |
|
103 |
712
|
104 int expected_nargin = length (); |
577
|
105 |
4219
|
106 iterator p = begin (); |
577
|
107 |
712
|
108 for (int i = 0; i < expected_nargin; i++) |
577
|
109 { |
4219
|
110 tree_identifier *elt = *p++; |
577
|
111 |
2979
|
112 octave_lvalue ref = elt->lvalue (); |
2959
|
113 |
577
|
114 if (i < nargin) |
|
115 { |
620
|
116 if (args(i).is_defined () && args(i).is_magic_colon ()) |
577
|
117 { |
|
118 ::error ("invalid use of colon in function argument list"); |
|
119 return; |
|
120 } |
2891
|
121 |
3538
|
122 ref.assign (octave_value::op_asn_eq, args(i)); |
577
|
123 } |
2891
|
124 else |
3538
|
125 ref.assign (octave_value::op_asn_eq, octave_value ()); |
577
|
126 } |
|
127 } |
|
128 |
3239
|
129 void |
4219
|
130 tree_parameter_list::undefine (void) |
3239
|
131 { |
|
132 int len = length (); |
|
133 |
4219
|
134 iterator p = begin (); |
3239
|
135 |
|
136 for (int i = 0; i < len; i++) |
|
137 { |
4219
|
138 tree_identifier *elt = *p++; |
3239
|
139 |
|
140 octave_lvalue ref = elt->lvalue (); |
|
141 |
3538
|
142 ref.assign (octave_value::op_asn_eq, octave_value ()); |
3239
|
143 } |
|
144 } |
|
145 |
2086
|
146 octave_value_list |
723
|
147 tree_parameter_list::convert_to_const_vector (tree_va_return_list *vr_list) |
577
|
148 { |
|
149 int nout = length (); |
|
150 |
723
|
151 if (vr_list) |
|
152 nout += vr_list->length (); |
|
153 |
2086
|
154 octave_value_list retval; |
577
|
155 retval.resize (nout); |
|
156 |
|
157 int i = 0; |
|
158 |
4219
|
159 for (iterator p = begin (); p != end (); p++) |
577
|
160 { |
4219
|
161 tree_identifier *elt = *p; |
577
|
162 |
4465
|
163 retval(i++) = elt->is_defined () ? elt->rvalue () : octave_value (); |
577
|
164 } |
|
165 |
723
|
166 if (vr_list) |
|
167 { |
4219
|
168 for (tree_va_return_list::iterator p = vr_list->begin (); |
|
169 p != vr_list->end (); |
|
170 p++) |
723
|
171 { |
4219
|
172 retval(i++) = *p; |
723
|
173 } |
|
174 } |
|
175 |
577
|
176 return retval; |
|
177 } |
|
178 |
1827
|
179 bool |
577
|
180 tree_parameter_list::is_defined (void) |
|
181 { |
1827
|
182 bool status = true; |
577
|
183 |
4219
|
184 for (iterator p = begin (); p != end (); p++) |
577
|
185 { |
4219
|
186 tree_identifier *elt = *p; |
577
|
187 |
|
188 if (! elt->is_defined ()) |
|
189 { |
1827
|
190 status = false; |
577
|
191 break; |
|
192 } |
|
193 } |
|
194 |
|
195 return status; |
|
196 } |
|
197 |
|
198 void |
2124
|
199 tree_parameter_list::accept (tree_walker& tw) |
581
|
200 { |
2124
|
201 tw.visit_parameter_list (*this); |
581
|
202 } |
|
203 |
|
204 // Return lists. |
|
205 |
1742
|
206 tree_return_list::~tree_return_list (void) |
|
207 { |
4219
|
208 while (! empty ()) |
1742
|
209 { |
4219
|
210 iterator p = begin (); |
|
211 delete *p; |
|
212 erase (p); |
1742
|
213 } |
|
214 } |
|
215 |
581
|
216 void |
2124
|
217 tree_return_list::accept (tree_walker& tw) |
581
|
218 { |
2124
|
219 tw.visit_return_list (*this); |
581
|
220 } |
|
221 |
4466
|
222 static int |
|
223 warn_undefined_return_values (void) |
|
224 { |
|
225 Vwarn_undefined_return_values |
|
226 = check_preference ("warn_undefined_return_values"); |
|
227 |
|
228 return 0; |
|
229 } |
|
230 |
|
231 void |
|
232 symbols_of_pt_misc (void) |
|
233 { |
|
234 DEFVAR (warn_undefined_return_values, true, warn_undefined_return_values, |
|
235 "-*- texinfo -*-\n\ |
|
236 @defvr {Built-in Variable} warn_undefined_return_values\n\ |
|
237 If the value of @code{warn_undefined_return_values} is nonzero,\n\ |
|
238 print a warning if a function does not define all the values in\n\ |
|
239 the return list which are expected. The default value is 1.\n\ |
|
240 @end defvr"); |
|
241 } |
|
242 |
577
|
243 /* |
|
244 ;;; Local Variables: *** |
|
245 ;;; mode: C++ *** |
|
246 ;;; End: *** |
|
247 */ |