577
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2003, 2004, |
|
4 2005, 2006, 2007 John W. Eaton |
577
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
577
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
577
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
577
|
26 #endif |
|
27 |
5848
|
28 #include "Cell.h" |
|
29 |
4466
|
30 #include "defun.h" |
2982
|
31 #include "error.h" |
|
32 #include "ov.h" |
|
33 #include "oct-lvalue.h" |
|
34 #include "pt-id.h" |
|
35 #include "pt-idx.h" |
|
36 #include "pt-misc.h" |
|
37 #include "pt-walk.h" |
4466
|
38 #include "utils.h" |
|
39 |
577
|
40 // Parameter lists. |
|
41 |
1742
|
42 tree_parameter_list::~tree_parameter_list (void) |
|
43 { |
4219
|
44 while (! empty ()) |
1742
|
45 { |
4219
|
46 iterator p = begin (); |
|
47 delete *p; |
|
48 erase (p); |
1742
|
49 } |
|
50 } |
|
51 |
577
|
52 void |
|
53 tree_parameter_list::mark_as_formal_parameters (void) |
|
54 { |
4219
|
55 for (iterator p = begin (); p != end (); p++) |
577
|
56 { |
6215
|
57 tree_decl_elt *elt = *p; |
577
|
58 elt->mark_as_formal_parameter (); |
|
59 } |
|
60 } |
|
61 |
|
62 void |
4466
|
63 tree_parameter_list::initialize_undefined_elements (const std::string& warnfor, |
|
64 int nargout, |
|
65 const octave_value& val) |
1093
|
66 { |
4466
|
67 bool warned = false; |
|
68 |
|
69 int count = 0; |
|
70 |
4219
|
71 for (iterator p = begin (); p != end (); p++) |
1093
|
72 { |
4466
|
73 if (++count > nargout) |
|
74 break; |
|
75 |
6215
|
76 tree_decl_elt *elt = *p; |
2948
|
77 |
7336
|
78 if (! elt->is_variable ()) |
2973
|
79 { |
5781
|
80 if (! warned) |
4466
|
81 { |
|
82 warned = true; |
5781
|
83 |
|
84 warning_with_id |
|
85 ("Octave:undefined-return-values", |
|
86 "%s: some elements in list of return values are undefined", |
|
87 warnfor.c_str ()); |
4466
|
88 } |
|
89 |
2979
|
90 octave_lvalue tmp = elt->lvalue (); |
2973
|
91 |
3538
|
92 tmp.assign (octave_value::op_asn_eq, val); |
2973
|
93 } |
1093
|
94 } |
|
95 } |
|
96 |
|
97 void |
2086
|
98 tree_parameter_list::define_from_arg_vector (const octave_value_list& args) |
577
|
99 { |
712
|
100 int nargin = args.length (); |
|
101 |
|
102 int expected_nargin = length (); |
577
|
103 |
4219
|
104 iterator p = begin (); |
577
|
105 |
712
|
106 for (int i = 0; i < expected_nargin; i++) |
577
|
107 { |
6215
|
108 tree_decl_elt *elt = *p++; |
577
|
109 |
2979
|
110 octave_lvalue ref = elt->lvalue (); |
2959
|
111 |
577
|
112 if (i < nargin) |
|
113 { |
620
|
114 if (args(i).is_defined () && args(i).is_magic_colon ()) |
577
|
115 { |
6215
|
116 if (! elt->eval ()) |
|
117 { |
|
118 ::error ("no default value for argument %d\n", i+1); |
|
119 return; |
|
120 } |
577
|
121 } |
6215
|
122 else |
|
123 ref.assign (octave_value::op_asn_eq, args(i)); |
577
|
124 } |
2891
|
125 else |
6215
|
126 elt->eval (); |
577
|
127 } |
|
128 } |
|
129 |
3239
|
130 void |
4219
|
131 tree_parameter_list::undefine (void) |
3239
|
132 { |
|
133 int len = length (); |
|
134 |
4219
|
135 iterator p = begin (); |
3239
|
136 |
|
137 for (int i = 0; i < len; i++) |
|
138 { |
6215
|
139 tree_decl_elt *elt = *p++; |
3239
|
140 |
|
141 octave_lvalue ref = elt->lvalue (); |
|
142 |
3538
|
143 ref.assign (octave_value::op_asn_eq, octave_value ()); |
3239
|
144 } |
|
145 } |
|
146 |
2086
|
147 octave_value_list |
5848
|
148 tree_parameter_list::convert_to_const_vector (const Cell& varargout) |
577
|
149 { |
5848
|
150 octave_idx_type vlen = varargout.numel (); |
577
|
151 |
5848
|
152 int nout = length () + vlen; |
723
|
153 |
5848
|
154 octave_value_list retval (nout, octave_value ()); |
577
|
155 |
|
156 int i = 0; |
|
157 |
4219
|
158 for (iterator p = begin (); p != end (); p++) |
577
|
159 { |
6215
|
160 tree_decl_elt *elt = *p; |
577
|
161 |
4465
|
162 retval(i++) = elt->is_defined () ? elt->rvalue () : octave_value (); |
577
|
163 } |
|
164 |
5848
|
165 for (octave_idx_type j = 0; j < vlen; j++) |
|
166 retval(i++) = varargout(j); |
723
|
167 |
577
|
168 return retval; |
|
169 } |
|
170 |
1827
|
171 bool |
577
|
172 tree_parameter_list::is_defined (void) |
|
173 { |
1827
|
174 bool status = true; |
577
|
175 |
4219
|
176 for (iterator p = begin (); p != end (); p++) |
577
|
177 { |
6215
|
178 tree_decl_elt *elt = *p; |
577
|
179 |
7336
|
180 if (! elt->is_variable ()) |
577
|
181 { |
1827
|
182 status = false; |
577
|
183 break; |
|
184 } |
|
185 } |
|
186 |
|
187 return status; |
|
188 } |
|
189 |
5861
|
190 tree_parameter_list * |
7336
|
191 tree_parameter_list::dup (symbol_table::scope_id scope) |
5861
|
192 { |
|
193 tree_parameter_list *new_list = new tree_parameter_list (); |
|
194 |
|
195 if (takes_varargs ()) |
|
196 new_list->mark_varargs (); |
|
197 |
|
198 for (iterator p = begin (); p != end (); p++) |
|
199 { |
6215
|
200 tree_decl_elt *elt = *p; |
5861
|
201 |
7336
|
202 new_list->append (elt->dup (scope)); |
5861
|
203 } |
|
204 |
|
205 return new_list; |
|
206 } |
|
207 |
577
|
208 void |
2124
|
209 tree_parameter_list::accept (tree_walker& tw) |
581
|
210 { |
2124
|
211 tw.visit_parameter_list (*this); |
581
|
212 } |
|
213 |
|
214 // Return lists. |
|
215 |
1742
|
216 tree_return_list::~tree_return_list (void) |
|
217 { |
4219
|
218 while (! empty ()) |
1742
|
219 { |
4219
|
220 iterator p = begin (); |
|
221 delete *p; |
|
222 erase (p); |
1742
|
223 } |
|
224 } |
|
225 |
5861
|
226 tree_return_list * |
7336
|
227 tree_return_list::dup (symbol_table::scope_id scope) |
5861
|
228 { |
|
229 tree_return_list *new_list = new tree_return_list (); |
|
230 |
|
231 for (iterator p = begin (); p != end (); p++) |
|
232 { |
|
233 tree_index_expression *elt = *p; |
|
234 |
7336
|
235 new_list->append (elt->dup (scope)); |
5861
|
236 } |
|
237 |
|
238 return new_list; |
|
239 } |
|
240 |
581
|
241 void |
2124
|
242 tree_return_list::accept (tree_walker& tw) |
581
|
243 { |
2124
|
244 tw.visit_return_list (*this); |
581
|
245 } |
|
246 |
577
|
247 /* |
|
248 ;;; Local Variables: *** |
|
249 ;;; mode: C++ *** |
|
250 ;;; End: *** |
|
251 */ |