Debian Codegraph @

src/core/chuck_scan.cpp

raw symbols SCIP
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // file: chuck_scan.cpp
28 // desc: chuck type-system / type-checker pre-scan
29 //
30 // author: Ge Wang ([email protected] | [email protected])
31 // date: Summer 2005 - original
32 //-----------------------------------------------------------------------------
33 #include "chuck_scan.h"
34 #include "chuck_errmsg.h"
35 #include "chuck_instr.h"
36 #include "chuck_type.h"
37 #include "chuck_vm.h"
38 #include "util_string.h"
39
40 #include <string>
41 using namespace std;
42
43
44
45
46 //-----------------------------------------------------------------------------
47 // function prototypes
48 //-----------------------------------------------------------------------------
49 t_CKBOOL type_engine_scan0_class_def( Chuck_Env * env, a_Class_Def class_def );
50
51 t_CKBOOL type_engine_scan1_stmt_list( Chuck_Env * env, a_Stmt_List list, t_CKBOOL * retrieveAllControlPathsReturn = NULL );
52 t_CKBOOL type_engine_scan1_stmt( Chuck_Env * env, a_Stmt stmt );
53 t_CKBOOL type_engine_scan1_if( Chuck_Env * env, a_Stmt_If stmt );
54 t_CKBOOL type_engine_scan1_for( Chuck_Env * env, a_Stmt_For stmt );
55 t_CKBOOL type_engine_scan1_foreach( Chuck_Env * env, a_Stmt_ForEach stmt );
56 t_CKBOOL type_engine_scan1_while( Chuck_Env * env, a_Stmt_While stmt );
57 t_CKBOOL type_engine_scan1_until( Chuck_Env * env, a_Stmt_Until stmt );
58 t_CKBOOL type_engine_scan1_loop( Chuck_Env * env, a_Stmt_Loop stmt );
59 t_CKBOOL type_engine_scan1_break( Chuck_Env * env, a_Stmt_Break br );
60 t_CKBOOL type_engine_scan1_continue( Chuck_Env * env, a_Stmt_Continue cont );
61 t_CKBOOL type_engine_scan1_return( Chuck_Env * env, a_Stmt_Return stmt );
62 t_CKBOOL type_engine_scan1_switch( Chuck_Env * env, a_Stmt_Switch stmt );
63 t_CKBOOL type_engine_scan1_exp( Chuck_Env * env, a_Exp exp );
64 t_CKBOOL type_engine_scan1_exp_binary( Chuck_Env * env, a_Exp_Binary binary );
65 t_CKBOOL type_engine_scan1_op( Chuck_Env * env, ae_Operator op, a_Exp lhs, a_Exp rhs, a_Exp_Binary binary );
66 t_CKBOOL type_engine_scan1_op_chuck( Chuck_Env * env, a_Exp lhs, a_Exp rhs, a_Exp_Binary binary );
67 t_CKBOOL type_engine_scan1_op_unchuck( Chuck_Env * env, a_Exp lhs, a_Exp rhs );
68 t_CKBOOL type_engine_scan1_op_upchuck( Chuck_Env * env, a_Exp lhs, a_Exp rhs );
69 t_CKBOOL type_engine_scan1_op_at_chuck( Chuck_Env * env, a_Exp lhs, a_Exp rhs );
70 t_CKBOOL type_engine_scan1_exp_unary( Chuck_Env * env, a_Exp_Unary unary );
71 t_CKBOOL type_engine_scan1_exp_primary( Chuck_Env * env, a_Exp_Primary exp );
72 t_CKBOOL type_engine_scan1_exp_array_lit( Chuck_Env * env, a_Exp_Primary exp );
73 t_CKBOOL type_engine_scan1_exp_cast( Chuck_Env * env, a_Exp_Cast cast );
74 t_CKBOOL type_engine_scan1_exp_postfix( Chuck_Env * env, a_Exp_Postfix postfix );
75 t_CKBOOL type_engine_scan1_exp_dur( Chuck_Env * env, a_Exp_Dur dur );
76 t_CKBOOL type_engine_scan1_exp_array( Chuck_Env * env, a_Exp_Array array );
77 t_CKBOOL type_engine_scan1_exp_func_call( Chuck_Env * env, a_Exp_Func_Call func_call );
78 t_CKBOOL type_engine_scan1_exp_func_call( Chuck_Env * env, a_Exp exp_func, a_Exp args,
79 t_CKFUNC & ck_func, int linepos );
80 t_CKBOOL type_engine_scan1_exp_dot_member( Chuck_Env * env, a_Exp_Dot_Member member );
81 t_CKBOOL type_engine_scan1_exp_if( Chuck_Env * env, a_Exp_If exp_if );
82 t_CKBOOL type_engine_scan1_exp_decl( Chuck_Env * env, a_Exp_Decl decl );
83 t_CKBOOL type_engine_scan1_array_subscripts( Chuck_Env * env, a_Exp exp_list );
84 t_CKBOOL type_engine_scan1_cast_valid( Chuck_Env * env, t_CKTYPE to, t_CKTYPE from );
85 t_CKBOOL type_engine_scan1_code_segment( Chuck_Env * env, a_Stmt_Code stmt, t_CKBOOL push = TRUE );
86 t_CKBOOL type_engine_scan1_func_def( Chuck_Env * env, a_Func_Def func_def );
87 t_CKBOOL type_engine_scan1_class_def( Chuck_Env * env, a_Class_Def class_def );
88
89 t_CKBOOL type_engine_scan2_stmt_list( Chuck_Env * env, a_Stmt_List list );
90 t_CKBOOL type_engine_scan2_stmt( Chuck_Env * env, a_Stmt stmt );
91 t_CKBOOL type_engine_scan2_if( Chuck_Env * env, a_Stmt_If stmt );
92 t_CKBOOL type_engine_scan2_for( Chuck_Env * env, a_Stmt_For stmt );
93 t_CKBOOL type_engine_scan2_foreach( Chuck_Env * env, a_Stmt_ForEach stmt );
94 t_CKBOOL type_engine_scan2_while( Chuck_Env * env, a_Stmt_While stmt );
95 t_CKBOOL type_engine_scan2_loop( Chuck_Env * env, a_Stmt_Loop stmt );
96 t_CKBOOL type_engine_scan2_until( Chuck_Env * env, a_Stmt_Until stmt );
97 t_CKBOOL type_engine_scan2_break( Chuck_Env * env, a_Stmt_Break br );
98 t_CKBOOL type_engine_scan2_continue( Chuck_Env * env, a_Stmt_Continue cont );
99 t_CKBOOL type_engine_scan2_return( Chuck_Env * env, a_Stmt_Return stmt );
100 t_CKBOOL type_engine_scan2_switch( Chuck_Env * env, a_Stmt_Switch stmt );
101 t_CKBOOL type_engine_scan2_exp( Chuck_Env * env, a_Exp exp );
102 t_CKBOOL type_engine_scan2_exp_binary( Chuck_Env * env, a_Exp_Binary binary );
103 t_CKBOOL type_engine_scan2_op( Chuck_Env * env, ae_Operator op, a_Exp lhs, a_Exp rhs, a_Exp_Binary binary );
104 t_CKBOOL type_engine_scan2_op_chuck( Chuck_Env * env, a_Exp lhs, a_Exp rhs, a_Exp_Binary binary );
105 t_CKBOOL type_engine_scan2_op_unchuck( Chuck_Env * env, a_Exp lhs, a_Exp rhs );
106 t_CKBOOL type_engine_scan2_op_upchuck( Chuck_Env * env, a_Exp lhs, a_Exp rhs );
107 t_CKBOOL type_engine_scan2_op_at_chuck( Chuck_Env * env, a_Exp lhs, a_Exp rhs );
108 t_CKBOOL type_engine_scan2_exp_unary( Chuck_Env * env, a_Exp_Unary unary );
109 t_CKBOOL type_engine_scan2_exp_primary( Chuck_Env * env, a_Exp_Primary exp );
110 t_CKBOOL type_engine_scan2_exp_array_lit( Chuck_Env * env, a_Exp_Primary exp );
111 t_CKBOOL type_engine_scan2_exp_cast( Chuck_Env * env, a_Exp_Cast cast );
112 t_CKBOOL type_engine_scan2_exp_postfix( Chuck_Env * env, a_Exp_Postfix postfix );
113 t_CKBOOL type_engine_scan2_exp_dur( Chuck_Env * env, a_Exp_Dur dur );
114 t_CKBOOL type_engine_scan2_exp_array( Chuck_Env * env, a_Exp_Array array );
115 t_CKBOOL type_engine_scan2_exp_func_call( Chuck_Env * env, a_Exp_Func_Call func_call );
116 t_CKBOOL type_engine_scan2_exp_func_call( Chuck_Env * env, a_Exp exp_func, a_Exp args,
117 t_CKFUNC & ck_func, int linepos );
118 t_CKBOOL type_engine_scan2_exp_dot_member( Chuck_Env * env, a_Exp_Dot_Member member );
119 t_CKBOOL type_engine_scan2_exp_if( Chuck_Env * env, a_Exp_If exp_if );
120 t_CKBOOL type_engine_scan2_exp_decl( Chuck_Env * env, a_Exp_Decl decl );
121 t_CKBOOL type_engine_scan2_array_subscripts( Chuck_Env * env, a_Exp exp_list );
122 t_CKBOOL type_engine_scan2_cast_valid( Chuck_Env * env, t_CKTYPE to, t_CKTYPE from );
123 t_CKBOOL type_engine_scan2_code_segment( Chuck_Env * env, a_Stmt_Code stmt, t_CKBOOL push = TRUE );
124 t_CKBOOL type_engine_scan2_func_def( Chuck_Env * env, a_Func_Def func_def );
125 t_CKBOOL type_engine_scan2_class_def( Chuck_Env * env, a_Class_Def class_def );
126
127
128
129
130 //-----------------------------------------------------------------------------
131 // name: type_engine_scan0_prog()
132 // desc: data in env should be ready : type discovery
133 //-----------------------------------------------------------------------------
134 t_CKBOOL type_engine_scan0_prog( Chuck_Env * env, a_Program prog,
135 te_HowMuch how_much )
136 {
137 t_CKBOOL ret = TRUE;
138
139 if( !prog )
140 return FALSE;
141
142 // log
143 EM_log( CK_LOG_FINER, "(pass 0) type discovery scan..." );
144 // env->context->filename.c_str() );
145 // push indent
146 EM_pushlog();
147 // log how much
148 EM_log( CK_LOG_FINEST, "target: %s", howmuch2str( how_much ) );
149
150 // go through each of the program sections
151 while( prog && ret )
152 {
153 switch( prog->section->s_type )
154 {
155 case ae_section_stmt:
156 // do nothing
157 break;
158
159 case ae_section_func:
160 // do nothing
161 break;
162
163 case ae_section_class:
164 // -----------------------------------------
165 // 1.5.2.5 (ge) check the compilation criteria
166 // if( !howMuch_criteria_match( how_much, prog->section->class_def ) ) break;
167 // -----------------------------------------
168 // 1.5.4.0 (ge) commented out above howMuch_criteria_match()
169 // * go ahead and compile all classes (public or non-public) even if import_only
170 // ...there is logic within to further sort out public vs. non-public classes
171 // * this is needed because public classes could make use of non-public classes
172 // declared in the file (e.g., KSChord needing KS) related: `is_public`
173 // -----------------------------------------
174
175 // make global, if marked public
176 if( prog->section->class_def->decl == ae_key_public )
177 {
178 // 1.5.4.0 (ge) removing restriction of one public class per file
179 // ---------------- legacy note ------------
180 // make sure the context has no public class
181 // if( env->context->public_class_def != NULL )
182 //{
183 // EM_error2( prog->section->class_def->where,
184 // "more than one 'public' class defined..." );
185 // ret = FALSE;
186 // continue;
187 //}
188 // remember
189 // env->context->public_class_def = prog->section->class_def;
190 // -----------------------------------------
191
192 // -----------------------------------------
193 // mark this class definition to be added to [user] namespace
194 // which typically sits between [global] namespace and context namesapce
195 prog->section->class_def->home = env->user();
196 // -----------------------------------------
197 // NOTE: for legacy reasons, fallback to [global] if no [user] namespace
198 // this fallback mechanism was moved out of env->user() to here for clarity in 1.5.4.0 (ge)
199 // needs further verification, as this fallback may not be necessary
200 if( !prog->section->class_def->home )
201 { prog->section->class_def->home = env->global(); }
202 // -----------------------------------------
203 }
204 // scan the class definition
205 ret = type_engine_scan0_class_def( env, prog->section->class_def );
206 break;
207
208 default:
209 EM_error2( prog->where,
210 "(internal error) unrecognized program section (%d) in type checker pre-scan (0)...",
211 prog->section->s_type );
212 ret = FALSE;
213 break;
214 }
215
216 // next section
217 prog = prog->next;
218 }
219
220 // pop indent
221 EM_poplog();
222
223 return ret;
224 }
225
226
227
228
229 //-----------------------------------------------------------------------------
230 // name: type_engine_scan0_class_def()
231 // desc: add defined class to env - this is the main goal of scan0
232 //-----------------------------------------------------------------------------
233 t_CKBOOL type_engine_scan0_class_def( Chuck_Env * env, a_Class_Def class_def )
234 {
235 // make new type for class def
236 t_CKTYPE the_class = NULL;
237 // the return type
238 t_CKBOOL ret = TRUE;
239 // the class body
240 a_Class_Body body = class_def->body;
241
242 // log
243 EM_log( CK_LOG_FINER, "scanning class definition '%s'...",
244 S_name(class_def->name->xid) );
245 // push indent
246 EM_pushlog();
247
248 // if nspc is attached to class_def, that means the class_def is to be
249 // put in that namespace. this is usually the case when doing import
250 if( class_def->home != NULL )
251 {
252 // log
253 EM_log( CK_LOG_FINER, "target namespace: '%s'",
254 class_def->home->name.c_str() );
255 // set the new type as current
256 env->nspc_stack.push_back( env->curr );
257 env->curr = class_def->home;
258 }
259
260 // make sure class not already in namespace
261 if( env->curr->lookup_type( class_def->name->xid, FALSE ) )
262 {
263 EM_error2( class_def->name->where,
264 "class/type '%s' is already defined in namespace '%s'",
265 S_name(class_def->name->xid), env->curr->name.c_str() );
266 ret = FALSE; goto done;
267 }
268
269 // check if reserved
270 if( type_engine_check_reserved( env, class_def->name->xid, class_def->name->where ) )
271 {
272 EM_error2( class_def->name->where, "...in class definition '%s'",
273 S_name(class_def->name->xid) );
274 ret = FALSE; goto done;
275 }
276
277 // allocate new type
278 assert( env->context != NULL );
279 the_class = env->context->new_Chuck_Type( env );
280 // add reference
281 CK_SAFE_ADD_REF( the_class );
282 // set the fields
283 the_class->xid = te_user;
284 the_class->base_name = S_name(class_def->name->xid);
285 // the_class->owner = env->curr; CK_SAFE_ADD_REF(the_class->owner);
286 the_class->array_depth = 0;
287 the_class->size = sizeof(void *);
288 the_class->obj_size = 0; // TODO:
289 the_class->nspc = env->context->new_Chuck_Namespace();
290 CK_SAFE_ADD_REF( the_class->nspc );
291 the_class->nspc->name = the_class->base_name;
292 // public? | 1.5.4.0 (ge) added
293 the_class->is_public = (class_def->decl == ae_key_public);
294 // if public class, then set parent to context
295 // ... allowing the class to address current context
296 // TODO: add ref to the parent?
297 if( the_class->is_public )
298 { the_class->nspc->parent = env->context->nspc; }
299 else { the_class->nspc->parent = env->curr; }
300
301 // 1.5.0.5 (ge) commented out; the AST is cleaned up after every compilation;
302 // would need to make deep copy if want to keep around
303 // the_class->def = class_def;
304
305 // add code
306 the_class->nspc->pre_ctor = new Chuck_VM_Code;
307 CK_SAFE_ADD_REF( the_class->nspc->pre_ctor );
308 // name | 1.5.2.0 (ge) added
309 the_class->nspc->pre_ctor->name = string("class ") + the_class->base_name;
310 // add to env
311 env->curr->add_type( the_class->base_name, the_class ); // URGENT: make this global
312 // incomplete
313 the_class->is_complete = FALSE;
314
315 // set the new type as current
316 env->nspc_stack.push_back( env->curr );
317 env->curr = the_class->nspc;
318 // push the class def
319 env->class_stack.push_back( env->class_def );
320 env->class_def = the_class;
321 // reset the nest list
322 env->class_scope = 0;
323
324 // type check the body
325 while( body && ret )
326 {
327 // check the section
328 switch( body->section->s_type )
329 {
330 case ae_section_stmt:
331 break;
332
333 case ae_section_func:
334 break;
335
336 case ae_section_class:
337 // check for public | 1.5.4.0 (ge) added
338 if( body->section->class_def->decl == ae_key_public )
339 {
340 EM_error2( class_def->where,
341 "cannot declare public classes within a class" );
342 ret = FALSE; goto done;
343 }
344 // do the class
345 ret = type_engine_scan0_class_def( env, body->section->class_def );
346 break;
347 }
348
349 // move to the next section
350 body = body->next;
351 }
352
353 // pop the class
354 env->class_def = env->class_stack.back();
355 env->class_stack.pop_back();
356 // pop the namesapce
357 env->curr = env->nspc_stack.back();
358 env->nspc_stack.pop_back();
359
360 // if things checked out
361 if( ret )
362 {
363 Chuck_Value * value = NULL;
364 Chuck_Type * type = NULL;
365
366 // allocate value
367 type = env->ckt_class->copy( env, env->context );
368 // 1.5.4.3 (ge) add reference count to actual_type | was: type->actual_type = the_class;
369 CK_SAFE_REF_ASSIGN(type->actual_type, the_class);
370 value = env->context->new_Chuck_Value( type, the_class->base_name );
371 value->owner = env->curr; CK_SAFE_ADD_REF(value->owner);
372 value->is_const = TRUE;
373 value->is_instance_member = FALSE;
374 // add to env
375 env->curr->add_value( the_class->base_name, value );
376
377 // remember
378 class_def->type = the_class;
379 }
380
381 // if nspc is attached to class_def, that means the class_def is to be
382 // put in that namespace. this is usually the case when doing import
383 // we undo that extra namespace layer here...
384 if( class_def->home != NULL )
385 {
386 // pop the namesapce
387 env->curr = env->nspc_stack.back();
388 env->nspc_stack.pop_back();
389 }
390 else // set the current namespace as home
391 {
392 // set curr as home
393 class_def->home = env->curr;
394 }
395
396 // initialize the Type info object | 1.5.0.0 (ge) added
397 initialize_object( the_class, env->ckt_class, NULL, env->vm() );
398
399 done:
400
401 // pop indent
402 EM_poplog();
403
404 return ret;
405 }
406
407
408
409
410 //-----------------------------------------------------------------------------
411 // name: type_engine_scan1_prog()
412 // desc: data in env should be ready
413 //-----------------------------------------------------------------------------
414 t_CKBOOL type_engine_scan1_prog( Chuck_Env * env, a_Program prog,
415 te_HowMuch how_much )
416 {
417 t_CKBOOL ret = TRUE;
418
419 if( !prog )
420 return FALSE;
421
422 // log
423 EM_log( CK_LOG_FINER, "(pass 1) type resolution scan..." );
424 // env->context->filename.c_str() );
425 // push indent
426 EM_pushlog();
427 // log how much
428 EM_log( CK_LOG_FINEST, "target: %s", howmuch2str( how_much ) );
429
430 // go through each of the program sections
431 while( prog && ret )
432 {
433 switch( prog->section->s_type )
434 {
435 case ae_section_stmt:
436 // if only classes, then skip
437 if( how_much == te_do_import_only ) break;
438 // scan the statements
439 ret = type_engine_scan1_stmt_list( env, prog->section->stmt_list );
440 break;
441
442 case ae_section_func:
443 // check the compilation criteria | 1.5.2.5 (ge)
444 if( !howMuch_criteria_match( how_much, prog->section->func_def ) ) break;
445 // scan the function definition
446 ret = type_engine_scan1_func_def( env, prog->section->func_def );
447 break;
448
449 case ae_section_class:
450 // 1.5.2.5 (ge) check the compilation criteria
451 // if( !howMuch_criteria_match( how_much, prog->section->class_def ) ) break;
452 // 1.5.4.0 (ge) commented out (see type_engine_prog0_scan() for explanation)
453
454 // scan the class definition
455 ret = type_engine_scan1_class_def( env, prog->section->class_def );
456 break;
457
458 default:
459 EM_error2( prog->where,
460 "(internal error) unrecognized program section in type checker pre-scan (1)..." );
461 ret = FALSE;
462 break;
463 }
464
465 // next section
466 prog = prog->next;
467 }
468
469 // pop indent
470 EM_poplog();
471
472 return ret;
473 }
474
475
476
477
478 //-----------------------------------------------------------------------------
479 // name: type_engine_scan1_stmt_list()
480 // desc: check a statement list; retrieveAllControlPathsReturn is pointer
481 // to retrieve whether the statement list control paths all return
482 //-----------------------------------------------------------------------------
483 t_CKBOOL type_engine_scan1_stmt_list( Chuck_Env * env, a_Stmt_List list,
484 t_CKBOOL * retrieveAllControlPathsReturn )
485 {
486 // last non-NULL statement
487 a_Stmt lastStmt = NULL;
488 // set to NULL to begin with
489 if( retrieveAllControlPathsReturn ) *retrieveAllControlPathsReturn = FALSE;
490
491 // type check the stmt_list
492 while( list )
493 {
494 // the current statement
495 if( !type_engine_scan1_stmt( env, list->stmt ) )
496 return FALSE;
497
498 // add if not NULL
499 if( retrieveAllControlPathsReturn && list->stmt != NULL )
500 lastStmt = list->stmt;
501
502 // advance to the next statement
503 list = list->next;
504 }
505
506 // set to the last non-NULL stmt | 1.5.1.0
507 // handle cases with multiple ending ;;;
508 // public class Class { fun void foo(){ samp => now;; } }
509 if( retrieveAllControlPathsReturn && lastStmt )
510 *retrieveAllControlPathsReturn = lastStmt->allControlPathsReturn;
511
512 return TRUE;
513 }
514
515
516
517
518 //-----------------------------------------------------------------------------
519 // name: type_engine_scan1_stmt(()
520 // desc: ...
521 //-----------------------------------------------------------------------------
522 t_CKBOOL type_engine_scan1_stmt( Chuck_Env * env, a_Stmt stmt )
523 {
524 t_CKBOOL ret = FALSE;
525 t_CKBOOL allControlPathsReturn = FALSE; // 1.5.1.0
526
527 if( !stmt )
528 return TRUE;
529
530 // push stmt | 1.5.4.4 (ge) added to scanner
531 env->stmt_stack.push_back( stmt );
532
533 // the type of stmt
534 switch( stmt->s_type )
535 {
536 case ae_stmt_import: // 1.5.4.0 (ge) added
537 // do nothing here (return true to bypass)
538 ret = TRUE;
539 break;
540
541 case ae_stmt_doc: // 1.5.4.4 (ge) added
542 // do nothing here (return true to bypass)
543 ret = TRUE;
544 break;
545
546 case ae_stmt_example: // 1.5.5.8 (ge,nick,alex) added
547 // do nothing here (return true to bypass)
548 ret = TRUE;
549 break;
550
551 case ae_stmt_if:
552 // count scope to help determine class member
553 env->class_scope++;
554 env->curr->value.push();
555 ret = type_engine_scan1_if( env, &stmt->stmt_if );
556 env->curr->value.pop();
557 env->class_scope--;
558
559 // both control paths must end with return for this to be true | 1.5.1.0
560 if( stmt->stmt_if.if_body )
561 {
562 // check if there is ELSE-BODY
563 if( stmt->stmt_if.else_body )
564 {
565 // both IF-BODY and ELSE-BODY should verify all control paths return
566 allControlPathsReturn = stmt->stmt_if.if_body->allControlPathsReturn
567 && stmt->stmt_if.else_body->allControlPathsReturn;
568 }
569 else
570 {
571 // if no ELSE-BODY, then no guarantee all control paths return
572 allControlPathsReturn = FALSE;
573 }
574 }
575 break;
576
577 case ae_stmt_for:
578 env->class_scope++;
579 env->curr->value.push();
580 ret = type_engine_scan1_for( env, &stmt->stmt_for );
581 env->curr->value.pop();
582 env->class_scope--;
583 break;
584
585 case ae_stmt_foreach:
586 env->class_scope++;
587 env->curr->value.push();
588 ret = type_engine_scan1_foreach( env, &stmt->stmt_foreach );
589 env->curr->value.pop();
590 env->class_scope--;
591 break;
592
593 case ae_stmt_while:
594 env->class_scope++;
595 env->curr->value.push();
596 ret = type_engine_scan1_while( env, &stmt->stmt_while );
597 env->curr->value.pop();
598 env->class_scope--;
599 break;
600
601 case ae_stmt_until:
602 env->class_scope++;
603 env->curr->value.push();
604 ret = type_engine_scan1_until( env, &stmt->stmt_until );
605 env->curr->value.pop();
606 env->class_scope--;
607 break;
608
609 case ae_stmt_loop:
610 env->class_scope++;
611 env->curr->value.push();
612 ret = type_engine_scan1_loop( env, &stmt->stmt_loop );
613 env->curr->value.pop();
614 env->class_scope--;
615 break;
616
617 case ae_stmt_exp:
618 ret = type_engine_scan1_exp( env, stmt->stmt_exp );
619 break;
620
621 case ae_stmt_return:
622 ret = type_engine_scan1_return( env, &stmt->stmt_return );
623 // this one should be easy, since this is literal return stmt | 1.5.1.0
624 allControlPathsReturn = TRUE;
625 break;
626
627 case ae_stmt_code:
628 env->class_scope++;
629 ret = type_engine_scan1_code_segment( env, &stmt->stmt_code );
630 env->class_scope--;
631
632 // check if code segment (e.g., { stmt_list }) fulfills this | 1.5.1.0
633 allControlPathsReturn = stmt->stmt_code.allControlPathsReturn;
634 break;
635
636 case ae_stmt_break:
637 ret = type_engine_scan1_break( env, &stmt->stmt_break );
638 break;
639
640 case ae_stmt_continue:
641 ret = type_engine_scan1_continue( env, &stmt->stmt_continue );
642 break;
643
644 case ae_stmt_switch:
645 env->class_scope++;
646 ret = type_engine_scan1_switch( env, &stmt->stmt_switch );
647 env->class_scope--;
648 break;
649
650 case ae_stmt_case:
651 // ret = type_engine_scan1_case( env, &stmt->stmt_case );
652 break;
653
654 case ae_stmt_gotolabel:
655 // ret = type_engine_scan1_gotolabel( env, &stmt->goto_label );
656 break;
657
658 default:
659 EM_error2( stmt->where,
660 "internal compiler error (pre-scan) - no stmt type '%i'!", stmt->s_type );
661 ret = FALSE;
662 break;
663 }
664
665 // set whether all control paths associated with this stmt return | 1.5.1.0
666 stmt->allControlPathsReturn = allControlPathsReturn;
667
668 // pop stmt | 1.5.4.4 (ge) added to scanner
669 env->stmt_stack.pop_back();
670
671 return ret;
672 }
673
674
675
676
677 //-----------------------------------------------------------------------------
678 // name: type_engine_scan1_if()
679 // desc: ...
680 //-----------------------------------------------------------------------------
681 t_CKBOOL type_engine_scan1_if( Chuck_Env * env, a_Stmt_If stmt )
682 {
683 // check the conditional
684 if( !type_engine_scan1_exp( env, stmt->cond ) )
685 return FALSE;
686
687 // TODO: ensure that conditional has valid type
688
689 // check if
690 if( !type_engine_scan1_stmt( env, stmt->if_body ) )
691 return FALSE;
692
693 // check else, if there is one
694 if( stmt->else_body )
695 if( !type_engine_scan1_stmt( env, stmt->else_body ) )
696 return FALSE;
697
698 return TRUE;
699 }
700
701
702
703
704 //-----------------------------------------------------------------------------
705 // name: type_engine_scan1_for()
706 // desc: ...
707 //-----------------------------------------------------------------------------
708 t_CKBOOL type_engine_scan1_for( Chuck_Env * env, a_Stmt_For stmt )
709 {
710 // check the initial
711 if( !type_engine_scan1_stmt( env, stmt->c1 ) )
712 return FALSE;
713
714 // check the conditional
715 if( !type_engine_scan1_stmt( env, stmt->c2 ) )
716 return FALSE;
717
718 // TODO: same as if - check conditional type valid
719
720 // check the post
721 if( stmt->c3 && !type_engine_scan1_exp( env, stmt->c3 ) )
722 return FALSE;
723
724 // check body
725 if( !type_engine_scan1_stmt( env, stmt->body ) )
726 return FALSE;
727
728 return TRUE;
729 }
730
731
732
733
734 //-----------------------------------------------------------------------------
735 // name: type_engine_scan1_foreach()
736 // desc: ...
737 //-----------------------------------------------------------------------------
738 t_CKBOOL type_engine_scan1_foreach( Chuck_Env * env, a_Stmt_ForEach stmt )
739 {
740 // check the iter part
741 if( !type_engine_scan1_exp( env, stmt->theIter ) )
742 return FALSE;
743
744 // check the array part
745 if( !type_engine_scan1_exp( env, stmt->theArray ) )
746 return FALSE;
747
748 // check body
749 if( !type_engine_scan1_stmt( env, stmt->body ) )
750 return FALSE;
751
752 return TRUE;
753 }
754
755
756
757
758 //-----------------------------------------------------------------------------
759 // name: type_engine_scan1_while()
760 // desc: ...
761 //-----------------------------------------------------------------------------
762 t_CKBOOL type_engine_scan1_while( Chuck_Env * env, a_Stmt_While stmt )
763 {
764 // check the conditional
765 if( !type_engine_scan1_exp( env, stmt->cond ) )
766 return FALSE;
767
768 // TODO: same as if - ensure the type in conditional is valid
769
770 // check the body
771 if( !type_engine_scan1_stmt( env, stmt->body ) )
772 return FALSE;
773
774 return TRUE;
775 }
776
777
778
779
780 //-----------------------------------------------------------------------------
781 // name: type_engine_scan1_until()
782 // desc: ...
783 //-----------------------------------------------------------------------------
784 t_CKBOOL type_engine_scan1_until( Chuck_Env * env, a_Stmt_Until stmt )
785 {
786 // check the conditional
787 if( !type_engine_scan1_exp( env, stmt->cond ) )
788 return FALSE;
789
790 // TODO: same as if - ensure the type in conditional is valid
791
792 // check the body
793 if( !type_engine_scan1_stmt( env, stmt->body ) )
794 return FALSE;
795
796 return TRUE;
797 }
798
799
800
801
802 //-----------------------------------------------------------------------------
803 // name: type_engine_scan1_loop()
804 // desc: ...
805 //-----------------------------------------------------------------------------
806 t_CKBOOL type_engine_scan1_loop( Chuck_Env * env, a_Stmt_Loop stmt )
807 {
808 // check the conditional
809 if( !type_engine_scan1_exp( env, stmt->cond ) )
810 return FALSE;
811
812 // TODO: same as if - ensure the type in conditional is valid
813
814 // check the body
815 if( !type_engine_scan1_stmt( env, stmt->body ) )
816 return FALSE;
817
818 return TRUE;
819 }
820
821
822
823
824 //-----------------------------------------------------------------------------
825 // name: type_engine_scan1_switch()
826 // desc: ...
827 //-----------------------------------------------------------------------------
828 t_CKBOOL type_engine_scan1_switch( Chuck_Env * env, a_Stmt_Switch stmt )
829 {
830 // TODO: implement this
831 EM_error2( stmt->where, "switch not implemented..." );
832
833 return FALSE;
834 }
835
836
837
838
839 //-----------------------------------------------------------------------------
840 // name: type_engine_scan1_break()
841 // desc: ...
842 //-----------------------------------------------------------------------------
843 t_CKBOOL type_engine_scan1_break( Chuck_Env * env, a_Stmt_Break br )
844 {
845 return TRUE;
846 }
847
848
849
850
851 //-----------------------------------------------------------------------------
852 // name: type_engine_scan1_continue()
853 // desc: ...
854 //-----------------------------------------------------------------------------
855 t_CKBOOL type_engine_scan1_continue( Chuck_Env * env, a_Stmt_Continue cont )
856 {
857 return TRUE;
858 }
859
860
861
862
863 //-----------------------------------------------------------------------------
864 // name: type_engine_scan1_return()
865 // desc: ...
866 //-----------------------------------------------------------------------------
867 t_CKBOOL type_engine_scan1_return( Chuck_Env * env, a_Stmt_Return stmt )
868 {
869 t_CKBOOL ret = FALSE;
870
871 // check the type of the return
872 if( stmt->val )
873 ret = type_engine_scan1_exp( env, stmt->val );
874 else
875 ret = TRUE;
876
877 return ret;
878 }
879
880
881
882
883 //-----------------------------------------------------------------------------
884 // name: type_engine_scan1_code_segment()
885 // desc: ...
886 //-----------------------------------------------------------------------------
887 t_CKBOOL type_engine_scan1_code_segment( Chuck_Env * env, a_Stmt_Code stmt,
888 t_CKBOOL push )
889 {
890 // class
891 env->class_scope++;
892 // push
893 if( push ) env->curr->value.push(); // env->context->nspc.value.push();
894 // do it
895 t_CKBOOL t = type_engine_scan1_stmt_list( env, stmt->stmt_list, &stmt->allControlPathsReturn );
896 // pop
897 if( push ) env->curr->value.pop(); // env->context->nspc.value.pop();
898 // class
899 env->class_scope--;
900
901 return t;
902 }
903
904
905
906
907 //-----------------------------------------------------------------------------
908 // name: type_engine_scan1_exp()
909 // desc: ...
910 //-----------------------------------------------------------------------------
911 t_CKBOOL type_engine_scan1_exp( Chuck_Env * env, a_Exp exp )
912 {
913 a_Exp curr = exp;
914 t_CKBOOL ret = TRUE;
915
916 // loop through parallel expressions
917 while( curr )
918 {
919 // examine the syntax
920 switch( curr->s_type )
921 {
922 case ae_exp_binary:
923 ret = type_engine_scan1_exp_binary( env, &curr->binary );
924 break;
925
926 case ae_exp_unary:
927 ret = type_engine_scan1_exp_unary( env, &curr->unary );
928 break;
929
930 case ae_exp_cast:
931 ret = type_engine_scan1_exp_cast( env, &curr->cast );
932 break;
933
934 case ae_exp_postfix:
935 ret = type_engine_scan1_exp_postfix( env, &curr->postfix );
936 break;
937
938 case ae_exp_dur:
939 ret = type_engine_scan1_exp_dur( env, &curr->dur );
940 break;
941
942 case ae_exp_primary:
943 ret = type_engine_scan1_exp_primary( env, &curr->primary );
944 break;
945
946 case ae_exp_array:
947 ret = type_engine_scan1_exp_array( env, &curr->array );
948 break;
949
950 case ae_exp_func_call:
951 ret = type_engine_scan1_exp_func_call( env, &curr->func_call );
952 break;
953
954 case ae_exp_dot_member:
955 ret = type_engine_scan1_exp_dot_member( env, &curr->dot_member );
956 break;
957
958 case ae_exp_if:
959 ret = type_engine_scan1_exp_if( env, &curr->exp_if );
960 break;
961
962 case ae_exp_decl:
963 ret = type_engine_scan1_exp_decl( env, &curr->decl );
964 break;
965
966 default:
967 EM_error2( curr->where,
968 "internal compiler error - no expression type '%i'...",
969 curr->s_type );
970 return FALSE;
971 }
972
973 // error
974 if( !ret )
975 return FALSE;
976
977 // advance to next expression
978 curr = curr->next;
979 }
980
981 // return type
982 return ret;
983 }
984
985
986
987
988 //-----------------------------------------------------------------------------
989 // name: type_engine_scan1_exp_binary()
990 // desc: ...
991 //-----------------------------------------------------------------------------
992 t_CKBOOL type_engine_scan1_exp_binary( Chuck_Env * env, a_Exp_Binary binary )
993 {
994 a_Exp cl = binary->lhs, cr = binary->rhs;
995
996 // scan lhs
997 t_CKBOOL left = type_engine_scan1_exp( env, cl );
998 if( !left ) return FALSE;
999 // scan rhs
1000 t_CKBOOL right = type_engine_scan1_exp( env, cr);
1001 if( !right ) return FALSE;
1002
1003 // cross chuck
1004 while( cr )
1005 {
1006 // type check the pair
1007 if( !type_engine_scan1_op( env, binary->op, cl, cr, binary ) )
1008 return FALSE;
1009
1010 cr = cr->next;
1011 }
1012
1013 return TRUE;
1014 }
1015
1016
1017
1018
1019 //-----------------------------------------------------------------------------
1020 // name: type_engine_scan1_op()
1021 // desc: ...
1022 //-----------------------------------------------------------------------------
1023 t_CKBOOL type_engine_scan1_op( Chuck_Env * env, ae_Operator op, a_Exp lhs, a_Exp rhs,
1024 a_Exp_Binary binary )
1025 {
1026 // TODO: check for static here
1027
1028 return TRUE;
1029 }
1030
1031
1032
1033
1034 //-----------------------------------------------------------------------------
1035 // name: type_engine_scan1_op_chuck()
1036 // desc: ...
1037 //-----------------------------------------------------------------------------
1038 t_CKBOOL type_engine_scan1_op_chuck( Chuck_Env * env, a_Exp lhs, a_Exp rhs,
1039 a_Exp_Binary binary )
1040 {
1041 return TRUE;
1042 }
1043
1044
1045
1046
1047 //-----------------------------------------------------------------------------
1048 // name: type_engine_scan1_op_unchuck()
1049 // desc: ...
1050 //-----------------------------------------------------------------------------
1051 t_CKBOOL type_engine_scan1_op_unchuck( Chuck_Env * env, a_Exp lhs, a_Exp rhs )
1052 {
1053 return TRUE;
1054 }
1055
1056
1057
1058
1059 //-----------------------------------------------------------------------------
1060 // name: type_engine_scan1_op_upchuck()
1061 // desc: ...
1062 //-----------------------------------------------------------------------------
1063 t_CKBOOL type_engine_scan1_op_upchuck( Chuck_Env * env, a_Exp lhs, a_Exp rhs )
1064 {
1065 return TRUE;
1066 }
1067
1068
1069
1070
1071 //-----------------------------------------------------------------------------
1072 // name: type_engine_scan1_op_at_chuck()
1073 // desc: ...
1074 //-----------------------------------------------------------------------------
1075 t_CKBOOL type_engine_scan1_op_at_chuck( Chuck_Env * env, a_Exp lhs, a_Exp rhs )
1076 {
1077 return TRUE;
1078 }
1079
1080
1081
1082
1083 //-----------------------------------------------------------------------------
1084 // name: type_engine_scan1_exp_unary()
1085 // desc: ...
1086 //-----------------------------------------------------------------------------
1087 t_CKBOOL type_engine_scan1_exp_unary( Chuck_Env * env, a_Exp_Unary unary )
1088 {
1089 return TRUE;
1090 }
1091
1092
1093
1094
1095 //-----------------------------------------------------------------------------
1096 // name: type_engine_scan1_exp_primary()
1097 // desc: ...
1098 //-----------------------------------------------------------------------------
1099 t_CKBOOL type_engine_scan1_exp_primary( Chuck_Env * env, a_Exp_Primary exp )
1100 {
1101 // check syntax
1102 switch( exp->s_type )
1103 {
1104 // variable
1105 case ae_primary_var:
1106 case ae_primary_num:
1107 case ae_primary_float:
1108 case ae_primary_str:
1109 case ae_primary_char:
1110 break;
1111
1112 // array literal
1113 case ae_primary_array:
1114 type_engine_scan1_exp_array_lit( env, exp );
1115 break;
1116
1117 // complex literal
1118 case ae_primary_complex:
1119 // if( !type_engine_scan1_exp_complex_lit( env, exp ) ) return FALSE;
1120 break;
1121
1122 // polar literal
1123 case ae_primary_polar:
1124 // if( !type_engine_scan1_exp_polar_lit( env, exp ) ) return FALSE;
1125 break;
1126
1127 // vector literal, ge: added 1.3.5.3
1128 case ae_primary_vec:
1129 // if( !type_engine_scan1_exp_vec_lit( env, exp ) ) return FALSE;
1130 break;
1131
1132 // expression
1133 case ae_primary_exp:
1134 if( !type_engine_scan1_exp( env, exp->exp ) ) return FALSE;
1135 break;
1136
1137 // hack
1138 case ae_primary_hack:
1139 // make sure not l-value
1140 if( exp->exp->s_type == ae_exp_decl )
1141 {
1142 EM_error2( exp->where,
1143 "cannot use <<< >>> on variable declarations" );
1144 return FALSE;
1145 }
1146
1147 // scan
1148 if( !type_engine_scan1_exp( env, exp->exp ) ) return FALSE;
1149 break;
1150
1151 // nil (void)
1152 case ae_primary_nil:
1153 break;
1154
1155 // no match
1156 default:
1157 EM_error2( exp->where,
1158 "(internal error) unrecognized primary type '%i'", exp->s_type );
1159 return FALSE;
1160 break;
1161 }
1162
1163 return TRUE;}
1164
1165
1166
1167
1168 //-----------------------------------------------------------------------------
1169 // name: type_engine_scan1_exp_array_lit()
1170 // desc: ...
1171 //-----------------------------------------------------------------------------
1172 t_CKBOOL type_engine_scan1_exp_array_lit( Chuck_Env * env, a_Exp_Primary exp )
1173 {
1174 // verify there are no errors from the parser...
1175 if( !verify_array( exp->array ) )
1176 return FALSE;
1177
1178 return TRUE;
1179 }
1180
1181
1182
1183
1184 //-----------------------------------------------------------------------------
1185 // name: type_engine_scan1_exp_cast()
1186 // desc: ...
1187 //-----------------------------------------------------------------------------
1188 t_CKBOOL type_engine_scan1_exp_cast( Chuck_Env * env, a_Exp_Cast cast )
1189 {
1190 // check the exp
1191 t_CKBOOL t = type_engine_scan1_exp( env, cast->exp );
1192 if( !t ) return FALSE;
1193
1194 return TRUE;
1195 }
1196
1197
1198
1199
1200 //-----------------------------------------------------------------------------
1201 // name: type_engine_scan1_exp_dur()
1202 // desc: ...
1203 //-----------------------------------------------------------------------------
1204 t_CKBOOL type_engine_scan1_exp_dur( Chuck_Env * env, a_Exp_Dur dur )
1205 {
1206 // type check the two components
1207 t_CKBOOL base = type_engine_scan1_exp( env, dur->base );
1208 t_CKBOOL unit = type_engine_scan1_exp( env, dur->unit );
1209
1210 // make sure both type check
1211 if( !base || !unit ) return FALSE;
1212
1213 return TRUE;
1214 }
1215
1216
1217
1218
1219 //-----------------------------------------------------------------------------
1220 // name: type_engine_scan1_exp_postfix()
1221 // desc: ...
1222 //-----------------------------------------------------------------------------
1223 t_CKBOOL type_engine_scan1_exp_postfix( Chuck_Env * env, a_Exp_Postfix postfix )
1224 {
1225 // check the exp
1226 t_CKBOOL t = type_engine_scan1_exp( env, postfix->exp );
1227 if( !t ) return FALSE;
1228
1229 // syntax
1230 // TODO: figure out ++/--
1231 switch( postfix->op )
1232 {
1233 case ae_op_plusplus:
1234 case ae_op_minusminus:
1235 // assignable?
1236 if( postfix->exp->s_meta != ae_meta_var )
1237 {
1238 EM_error2( postfix->exp->where,
1239 "postfix operator '%s' cannot be used on non-mutable data-type...",
1240 op2str( postfix->op ) );
1241 return FALSE;
1242 }
1243
1244 // TODO: mark somewhere we need to post increment
1245
1246 return TRUE;
1247 break;
1248
1249 default:
1250 // no match
1251 EM_error2( postfix->where,
1252 "internal compiler error (pre-scan): unrecognized postfix '%i'", postfix->op );
1253 return FALSE;
1254 }
1255
1256 return TRUE;
1257 }
1258
1259
1260
1261
1262 //-----------------------------------------------------------------------------
1263 // name: type_engine_scan1_exp_if()
1264 // desc: ...
1265 //-----------------------------------------------------------------------------
1266 t_CKBOOL type_engine_scan1_exp_if( Chuck_Env * env, a_Exp_If exp_if )
1267 {
1268 // check the components
1269 t_CKBOOL cond = type_engine_scan1_exp( env, exp_if->cond );
1270 t_CKBOOL if_exp = type_engine_scan1_exp( env, exp_if->if_exp );
1271 t_CKBOOL else_exp = type_engine_scan1_exp( env, exp_if->else_exp );
1272
1273 // make sure everything good
1274 if( !cond || !if_exp || !else_exp ) return FALSE;
1275
1276 return TRUE;
1277 }
1278
1279
1280
1281
1282 //-----------------------------------------------------------------------------
1283 // name: type_engine_scan1_array_subscripts( )
1284 // desc: ...
1285 //-----------------------------------------------------------------------------
1286 t_CKBOOL type_engine_scan1_array_subscripts( Chuck_Env * env, a_Exp exp_list )
1287 {
1288 return TRUE;
1289 }
1290
1291
1292
1293
1294 //-----------------------------------------------------------------------------
1295 // name: type_engine_scan1_exp_decl( )
1296 // desc: ...
1297 //-----------------------------------------------------------------------------
1298 t_CKBOOL type_engine_scan1_exp_decl( Chuck_Env * env, a_Exp_Decl decl )
1299 {
1300 a_Var_Decl_List list = decl->var_decl_list;
1301 a_Var_Decl var_decl = NULL;
1302
1303 // look up the type
1304 // TODO: handle T a, b, c...
1305 // TODO: do we climb?
1306 t_CKTYPE t = type_engine_find_type( env, decl->type->xid );
1307 // if not found, try to resolve
1308 if( !t )
1309 {
1310 // resolve
1311 EM_error2( decl->where, "...in declaration" );
1312 return FALSE;
1313 }
1314
1315 // loop through the variables
1316 while( list != NULL )
1317 {
1318 // get the decl
1319 var_decl = list->var_decl;
1320 // count
1321 decl->num_var_decls++;
1322
1323 // scan constructor args | 1.5.2.0 (ge) added
1324 if( var_decl->ctor.args != NULL )
1325 {
1326 // disallow non-default constructors for 'global' Object decls
1327 if( decl->is_global )
1328 {
1329 // resolve
1330 EM_error2( var_decl->where, "'global' variables cannot invoke non-default constructors..." );
1331 return FALSE;
1332 }
1333 // type check the exp
1334 if( !type_engine_scan1_exp( env, var_decl->ctor.args ) )
1335 return FALSE;
1336 }
1337
1338 // scan if array
1339 if( var_decl->array != NULL )
1340 {
1341 // verify there are no errors from the parser...
1342 if( !verify_array( var_decl->array ) )
1343 return FALSE;
1344
1345 // may be partial and empty []
1346 if( var_decl->array->exp_list )
1347 {
1348 // type check the exp
1349 if( !type_engine_scan1_exp( env, var_decl->array->exp_list ) )
1350 return FALSE;
1351 // make sure types are of int
1352 if( !type_engine_scan1_array_subscripts( env, var_decl->array->exp_list ) )
1353 return FALSE;
1354 }
1355 }
1356
1357 // 1.4.2.0 (ge) added: var_decl->ck_type = t;
1358 CK_SAFE_REF_ASSIGN( var_decl->ck_type, t );
1359
1360 // the next var decl
1361 list = list->next;
1362 }
1363
1364 // remember : decl->ck_type = t;
1365 CK_SAFE_REF_ASSIGN( decl->ck_type, t );
1366
1367 return TRUE;
1368 }
1369
1370
1371
1372
1373 //-----------------------------------------------------------------------------
1374 // name: type_engine_scan1_exp_func_call()
1375 // desc: ...
1376 //-----------------------------------------------------------------------------
1377 t_CKBOOL type_engine_scan1_exp_func_call( Chuck_Env * env, a_Exp exp_func, a_Exp args,
1378 t_CKFUNC & ck_func, int linepos )
1379 {
1380 // Chuck_Func * func = NULL;
1381 // Chuck_Func * up = NULL;
1382
1383 // type check the func
1384 t_CKBOOL f = type_engine_scan1_exp( env, exp_func );
1385 if( !f ) return FALSE;
1386
1387 // check the arguments
1388 if( args )
1389 {
1390 t_CKBOOL a = type_engine_scan1_exp( env, args );
1391 if( !a ) return FALSE;
1392 }
1393
1394 return TRUE;
1395 }
1396
1397
1398
1399
1400 //-----------------------------------------------------------------------------
1401 // name: type_engine_scan1_exp_func_call()
1402 // desc: ...
1403 //-----------------------------------------------------------------------------
1404 t_CKBOOL type_engine_scan1_exp_func_call( Chuck_Env * env, a_Exp_Func_Call func_call )
1405 {
1406 // type check it
1407 return type_engine_scan1_exp_func_call( env, func_call->func, func_call->args,
1408 func_call->ck_func, func_call->where );
1409 }
1410
1411
1412
1413
1414 //-----------------------------------------------------------------------------
1415 // name: type_engine_scan1_exp_dot_member()
1416 // desc: ...
1417 //-----------------------------------------------------------------------------
1418 t_CKBOOL type_engine_scan1_exp_dot_member( Chuck_Env * env, a_Exp_Dot_Member member )
1419 {
1420 // type check the base
1421 t_CKBOOL base = type_engine_scan1_exp( env, member->base );
1422 if( !base ) return FALSE;
1423
1424 return TRUE;
1425 }
1426
1427
1428
1429
1430 //-----------------------------------------------------------------------------
1431 // name: type_engine_scan1_exp_array()
1432 // desc: ...
1433 //-----------------------------------------------------------------------------
1434 t_CKBOOL type_engine_scan1_exp_array( Chuck_Env * env, a_Exp_Array array )
1435 {
1436 // verify there are no errors from the parser...
1437 if( !verify_array( array->indices ) )
1438 return FALSE;
1439
1440 // type check the base
1441 t_CKBOOL base = type_engine_scan1_exp( env, array->base );
1442 if( !base ) return FALSE;
1443
1444 // type check the index
1445 t_CKBOOL index = type_engine_scan1_exp( env, array->indices->exp_list );
1446 if( !index ) return FALSE;
1447
1448 // cycle through each exp
1449 // a_Exp e = array->indices->exp_list;
1450 // count the dimension
1451 // t_CKUINT depth = 0;
1452
1453 return TRUE;
1454 }
1455
1456
1457
1458
1459 //-----------------------------------------------------------------------------
1460 // name: type_engine_scan1_class_def()
1461 // desc: ...
1462 //-----------------------------------------------------------------------------
1463 t_CKBOOL type_engine_scan1_class_def( Chuck_Env * env, a_Class_Def class_def )
1464 {
1465 // the return type
1466 t_CKBOOL ret = TRUE;
1467 // the class body
1468 a_Class_Body body = class_def->body;
1469 // the class
1470 Chuck_Type * the_class = class_def->type;
1471
1472 // set the new type as current
1473 env->nspc_stack.push_back( env->curr );
1474 env->curr = the_class->nspc;
1475 // push the class def
1476 env->class_stack.push_back( env->class_def );
1477 env->class_def = the_class;
1478 // reset the nest list
1479 env->class_scope = 0;
1480
1481 // type check the body
1482 while( body && ret )
1483 {
1484 // check the section
1485 switch( body->section->s_type )
1486 {
1487 case ae_section_stmt:
1488 // do the statements
1489 ret = type_engine_scan1_stmt_list( env, body->section->stmt_list );
1490 break;
1491
1492 case ae_section_func:
1493 // do the function
1494 ret = type_engine_scan1_func_def( env, body->section->func_def );
1495 break;
1496
1497 case ae_section_class:
1498 // do the class
1499 ret = type_engine_scan1_class_def( env, body->section->class_def );
1500 break;
1501 }
1502
1503 // move to the next section
1504 body = body->next;
1505 }
1506
1507 // pop the class
1508 env->class_def = env->class_stack.back();
1509 env->class_stack.pop_back();
1510 // pop the namesapce
1511 env->curr = env->nspc_stack.back();
1512 env->nspc_stack.pop_back();
1513
1514 return ret;
1515 }
1516
1517
1518
1519
1520 //-----------------------------------------------------------------------------
1521 // name: type_engine_scan1_func_def()
1522 // desc: type check scan pass 1 for function definition
1523 //-----------------------------------------------------------------------------
1524 t_CKBOOL type_engine_scan1_func_def( Chuck_Env * env, a_Func_Def f )
1525 {
1526 a_Arg_List arg_list = NULL;
1527 t_CKUINT count = 0;
1528 t_CKBOOL is_static = (env->class_def != NULL) && (f->static_decl == ae_key_static);
1529
1530 // substitute for @construct
1531 if( S_name(f->name) == string("@construct") )
1532 {
1533 // make sure we are in a class
1534 if( !env->class_def )
1535 {
1536 EM_error2( f->where, "@construct() can only be used within class definitions..." );
1537 return FALSE;
1538 }
1539
1540 // substitute class name
1541 f->name = insert_symbol( env->class_def->base_name.c_str() );
1542 }
1543 // verify @destruct
1544 if( S_name(f->name) == string("@destruct") )
1545 {
1546 // make sure we are in a class
1547 if( !env->class_def )
1548 {
1549 EM_error2( f->where, "@destruct() can only be used within class definitions..." );
1550 return FALSE;
1551 }
1552 // make sure there are no arguments
1553 if( f->arg_list != NULL )
1554 {
1555 EM_error2( f->where, "@destruct() cannot accept arguments..." );
1556 return FALSE;
1557 }
1558 // substitute ~[class name]
1559 // f->name = insert_symbol( string("~"+env->class_def->base_name).c_str() );
1560 }
1561 // check if constructor
1562 t_CKBOOL isInCtor = isctor(env,f);
1563 // check if destructor
1564 t_CKBOOL isInDtor = isdtor(env,f);
1565
1566 // check if reserved
1567 if( type_engine_check_reserved( env, f->name, f->where ) )
1568 {
1569 EM_error2( f->where, "...in function definition '%s'",
1570 S_name(f->name) );
1571 return FALSE;
1572 }
1573
1574 // look up the return type
1575 f->ret_type = type_engine_find_type( env, f->type_decl->xid );
1576 // no return type
1577 if( !f->ret_type )
1578 {
1579 // TODO: try to resolve
1580 EM_error2( f->where, "...in return type of function '%s'", S_name(f->name) );
1581 goto error;
1582 }
1583 // add reference | 1.5.0.5
1584 CK_SAFE_ADD_REF( f->ret_type );
1585
1586 // check if we are in a constructor
1587 if( isInCtor )
1588 {
1589 // check static
1590 if( is_static )
1591 {
1592 EM_error2( f->where, "constructor cannot be declared as 'static'..." );
1593 goto error;
1594 }
1595 // check return type (must be void)
1596 if( !isa(f->ret_type, env->ckt_void) )
1597 {
1598 EM_error2( f->where, "constructor must return void..." );
1599 goto error;
1600 }
1601 }
1602
1603 if( isInDtor )
1604 {
1605 // check static
1606 if( is_static )
1607 {
1608 EM_error2( f->where, "destructor cannot be declared as 'static'..." );
1609 goto error;
1610 }
1611 // check return type (must be void)
1612 if( !isa(f->ret_type, env->ckt_void) )
1613 {
1614 EM_error2( f->where, "destructor must return void..." );
1615 goto error;
1616 }
1617 }
1618
1619 // check if array
1620 if( f->type_decl->array != NULL )
1621 {
1622 // verify there are no errors from the parser...
1623 if( !verify_array( f->type_decl->array ) )
1624 return FALSE;
1625
1626 Chuck_Type * t = NULL;
1627 Chuck_Type * t2 = f->ret_type;
1628 // should be partial and empty []
1629 if( f->type_decl->array->exp_list )
1630 {
1631 EM_error2( f->type_decl->array->where, "function '%s':", S_name(f->name) );
1632 EM_error2( f->type_decl->array->where, "return array type must be defined with empty []'s" );
1633 return FALSE;
1634 }
1635
1636 // get (or create) matching array type
1637 t = env->get_array_type(
1638 env->ckt_array, // the array base class
1639 f->type_decl->array->depth, // the depth of the new type
1640 t2 // the 'array_type'
1641 // env->curr // the owner namespace
1642 );
1643
1644 // TODO: verify
1645 // set ref
1646 f->type_decl->ref = TRUE;
1647 // replace type : f->ret_type = t
1648 CK_SAFE_REF_ASSIGN( f->ret_type, t );
1649 }
1650
1651 // look up types for the function arguments
1652 arg_list = f->arg_list;
1653 // count
1654 count = 1;
1655 // loop over arguments
1656 while( arg_list )
1657 {
1658 // look up in type table
1659 arg_list->type = type_engine_find_type( env, arg_list->type_decl->xid );
1660 // if not there, try to resolve
1661 if( !arg_list->type )
1662 {
1663 // TODO: try to resolve
1664 EM_error2( arg_list->where,
1665 "...in argument %i '%s' of function '%s(.)'",
1666 count, S_name(arg_list->var_decl->xid), S_name(f->name) );
1667 goto error;
1668 }
1669
1670 // count
1671 count++;
1672 // next arg
1673 arg_list = arg_list->next;
1674 }
1675
1676 // scan the code for types that need resolution
1677 assert( f->code == NULL || f->code->s_type == ae_stmt_code );
1678 if( f->code && !type_engine_scan1_code_segment( env, &f->code->stmt_code, FALSE ) )
1679 {
1680 EM_error2( 0, "...in function '%s'", S_name(f->name) );
1681 goto error;
1682 }
1683
1684 // propagate from stmt_code to the outer stmt | 1.5.1.0
1685 if( f->code ) f->code->allControlPathsReturn = f->code->stmt_code.allControlPathsReturn;
1686
1687 return TRUE;
1688
1689 error:
1690
1691 return FALSE;
1692 }
1693
1694
1695
1696
1697 //-----------------------------------------------------------------------------
1698 // name: type_engine_scan2_prog()
1699 // desc: data in env should be ready
1700 //-----------------------------------------------------------------------------
1701 t_CKBOOL type_engine_scan2_prog( Chuck_Env * env, a_Program prog,
1702 te_HowMuch how_much )
1703 {
1704 t_CKBOOL ret = TRUE;
1705
1706 if( !prog )
1707 return FALSE;
1708
1709 // log
1710 EM_log( CK_LOG_FINER, "(pass 2) type verification scan..." );
1711 // env->context->filename.c_str() );
1712 // push indent
1713 EM_pushlog();
1714 // log how much
1715 EM_log( CK_LOG_FINEST, "target: %s", howmuch2str( how_much ) );
1716
1717 // go through each of the program sections
1718 while( prog && ret )
1719 {
1720 switch( prog->section->s_type )
1721 {
1722 case ae_section_stmt:
1723 // if classes only, then skip
1724 if( how_much == te_do_import_only ) break;
1725 // scan the statements
1726 ret = type_engine_scan2_stmt_list( env, prog->section->stmt_list );
1727 break;
1728
1729 case ae_section_func:
1730 // check the compilation criteria | 1.5.2.5 (ge)
1731 if( !howMuch_criteria_match( how_much, prog->section->func_def ) ) break;
1732 // scan the function definition
1733 ret = type_engine_scan2_func_def( env, prog->section->func_def );
1734 break;
1735
1736 case ae_section_class:
1737 // 1.5.2.5 (ge) check the compilation criteria
1738 // if( !howMuch_criteria_match( how_much, prog->section->class_def ) ) break;
1739 // 1.5.4.0 (ge) commented out (see type_engine_prog0_scan() for explanation)
1740
1741 // scan the class definition
1742 ret = type_engine_scan2_class_def( env, prog->section->class_def );
1743 break;
1744
1745 default:
1746 EM_error2( prog->where,
1747 "(internal error) unrecognized program section in type checker pre-scan (2)..." );
1748 ret = FALSE;
1749 break;
1750 }
1751
1752 // next section
1753 prog = prog->next;
1754 }
1755
1756 // pop indent
1757 EM_poplog();
1758
1759 return ret;
1760 }
1761
1762
1763
1764
1765 //-----------------------------------------------------------------------------
1766 // name: type_engine_scan2_stmt_list()
1767 // desc: ...
1768 //-----------------------------------------------------------------------------
1769 t_CKBOOL type_engine_scan2_stmt_list( Chuck_Env * env, a_Stmt_List list )
1770 {
1771 // type check the stmt_list
1772 while( list )
1773 {
1774 // the current statement
1775 if( !type_engine_scan2_stmt( env, list->stmt ) )
1776 return FALSE;
1777
1778 // advance to the next statement
1779 list = list->next;
1780 }
1781
1782 return TRUE;
1783 }
1784
1785
1786
1787
1788 //-----------------------------------------------------------------------------
1789 // name: type_engine_scan2_stmt(()
1790 // desc: ...
1791 //-----------------------------------------------------------------------------
1792 t_CKBOOL type_engine_scan2_stmt( Chuck_Env * env, a_Stmt stmt )
1793 {
1794 t_CKBOOL ret = FALSE;
1795
1796 if( !stmt )
1797 return TRUE;
1798
1799 // push stmt | 1.5.4.4 (ge) added to scanner
1800 env->stmt_stack.push_back( stmt );
1801
1802 // the type of stmt
1803 switch( stmt->s_type )
1804 {
1805 case ae_stmt_import: // 1.5.4.0 (ge) added
1806 // do nothing here (return true to bypass)
1807 ret = TRUE;
1808 break;
1809
1810 case ae_stmt_doc: // 1.5.4.4 (ge) added
1811 // do nothing here (return true to bypass)
1812 ret = TRUE;
1813 break;
1814
1815 case ae_stmt_example: // 1.5.5.8 (ge,nick,alex) added
1816 // do nothing here (return true to bypass)
1817 ret = TRUE;
1818 break;
1819
1820 case ae_stmt_if:
1821 // count scope to help determine class member
1822 env->class_scope++;
1823 env->curr->value.push();
1824 ret = type_engine_scan2_if( env, &stmt->stmt_if );
1825 env->curr->value.pop();
1826 env->class_scope--;
1827 break;
1828
1829 case ae_stmt_for:
1830 env->class_scope++;
1831 env->curr->value.push();
1832 ret = type_engine_scan2_for( env, &stmt->stmt_for );
1833 env->curr->value.pop();
1834 env->class_scope--;
1835 break;
1836
1837 case ae_stmt_foreach:
1838 env->class_scope++;
1839 env->curr->value.push();
1840 ret = type_engine_scan2_foreach( env, &stmt->stmt_foreach );
1841 env->curr->value.pop();
1842 env->class_scope--;
1843 break;
1844
1845 case ae_stmt_while:
1846 env->class_scope++;
1847 env->curr->value.push();
1848 ret = type_engine_scan2_while( env, &stmt->stmt_while );
1849 env->curr->value.pop();
1850 env->class_scope--;
1851 break;
1852
1853 case ae_stmt_until:
1854 env->class_scope++;
1855 env->curr->value.push();
1856 ret = type_engine_scan2_until( env, &stmt->stmt_until );
1857 env->curr->value.pop();
1858 env->class_scope--;
1859 break;
1860
1861 case ae_stmt_loop:
1862 env->class_scope++;
1863 env->curr->value.push();
1864 ret = type_engine_scan2_loop( env, &stmt->stmt_loop );
1865 env->curr->value.pop();
1866 env->class_scope--;
1867 break;
1868
1869 case ae_stmt_exp:
1870 ret = type_engine_scan2_exp( env, stmt->stmt_exp );
1871 break;
1872
1873 case ae_stmt_return:
1874 ret = type_engine_scan2_return( env, &stmt->stmt_return );
1875 break;
1876
1877 case ae_stmt_code:
1878 env->class_scope++;
1879 ret = type_engine_scan2_code_segment( env, &stmt->stmt_code );
1880 env->class_scope--;
1881 break;
1882
1883 case ae_stmt_break:
1884 ret = type_engine_scan2_break( env, &stmt->stmt_break );
1885 break;
1886
1887 case ae_stmt_continue:
1888 ret = type_engine_scan2_continue( env, &stmt->stmt_continue );
1889 break;
1890
1891 case ae_stmt_switch:
1892 env->class_scope++;
1893 ret = type_engine_scan2_switch( env, &stmt->stmt_switch );
1894 env->class_scope--;
1895 break;
1896
1897 case ae_stmt_case:
1898 // ret = type_engine_scan2_case( env, &stmt->stmt_case );
1899 break;
1900
1901 case ae_stmt_gotolabel:
1902 // ret = type_engine_scan2_gotolabel( env, &stmt->goto_label );
1903 break;
1904
1905 default:
1906 EM_error2( stmt->where,
1907 "internal compiler error (pre-scan) - no stmt type '%i'!", stmt->s_type );
1908 ret = FALSE;
1909 break;
1910 }
1911
1912 // pop stmt | 1.5.4.4 (ge) added to scanner
1913 env->stmt_stack.pop_back();
1914
1915 return ret;
1916 }
1917
1918
1919
1920
1921 //-----------------------------------------------------------------------------
1922 // name: type_engine_scan2_if()
1923 // desc: ...
1924 //-----------------------------------------------------------------------------
1925 t_CKBOOL type_engine_scan2_if( Chuck_Env * env, a_Stmt_If stmt )
1926 {
1927 // check the conditional
1928 if( !type_engine_scan2_exp( env, stmt->cond ) )
1929 return FALSE;
1930
1931 // TODO: ensure that conditional has valid type
1932
1933 // check if
1934 if( !type_engine_scan2_stmt( env, stmt->if_body ) )
1935 return FALSE;
1936
1937 // check else, if there is one
1938 if( stmt->else_body )
1939 if( !type_engine_scan2_stmt( env, stmt->else_body ) )
1940 return FALSE;
1941
1942 return TRUE;
1943 }
1944
1945
1946
1947
1948 //-----------------------------------------------------------------------------
1949 // name: type_engine_scan2_for()
1950 // desc: ...
1951 //-----------------------------------------------------------------------------
1952 t_CKBOOL type_engine_scan2_for( Chuck_Env * env, a_Stmt_For stmt )
1953 {
1954 // check the initial
1955 if( !type_engine_scan2_stmt( env, stmt->c1 ) )
1956 return FALSE;
1957
1958 // check the conditional
1959 if( !type_engine_scan2_stmt( env, stmt->c2 ) )
1960 return FALSE;
1961
1962 // TODO: same as if - check conditional type valid
1963
1964 // check the post
1965 if( stmt->c3 && !type_engine_scan2_exp( env, stmt->c3 ) )
1966 return FALSE;
1967
1968 // check body
1969 if( !type_engine_scan2_stmt( env, stmt->body ) )
1970 return FALSE;
1971
1972 return TRUE;
1973 }
1974
1975
1976
1977
1978 //-----------------------------------------------------------------------------
1979 // name: type_engine_scan2_foreach()
1980 // desc: ...
1981 //-----------------------------------------------------------------------------
1982 t_CKBOOL type_engine_scan2_foreach( Chuck_Env * env, a_Stmt_ForEach stmt )
1983 {
1984 // check the array part first
1985 if( !type_engine_scan2_exp( env, stmt->theArray ) )
1986 return FALSE;
1987
1988 // check the iter part
1989 if( !type_engine_scan2_exp( env, stmt->theIter ) )
1990 return FALSE;
1991
1992 // check body
1993 if( !type_engine_scan2_stmt( env, stmt->body ) )
1994 return FALSE;
1995
1996 return TRUE;
1997 }
1998
1999
2000
2001
2002 //-----------------------------------------------------------------------------
2003 // name: type_engine_scan2_while()
2004 // desc: ...
2005 //-----------------------------------------------------------------------------
2006 t_CKBOOL type_engine_scan2_while( Chuck_Env * env, a_Stmt_While stmt )
2007 {
2008 // check the conditional
2009 if( !type_engine_scan2_exp( env, stmt->cond ) )
2010 return FALSE;
2011
2012 // TODO: same as if - ensure the type in conditional is valid
2013
2014 // check the body
2015 if( !type_engine_scan2_stmt( env, stmt->body ) )
2016 return FALSE;
2017
2018 return TRUE;
2019 }
2020
2021
2022
2023
2024 //-----------------------------------------------------------------------------
2025 // name: type_engine_scan2_until()
2026 // desc: ...
2027 //-----------------------------------------------------------------------------
2028 t_CKBOOL type_engine_scan2_until( Chuck_Env * env, a_Stmt_Until stmt )
2029 {
2030 // check the conditional
2031 if( !type_engine_scan2_exp( env, stmt->cond ) )
2032 return FALSE;
2033
2034 // TODO: same as if - ensure the type in conditional is valid
2035
2036 // check the body
2037 if( !type_engine_scan2_stmt( env, stmt->body ) )
2038 return FALSE;
2039
2040 return TRUE;
2041 }
2042
2043
2044
2045
2046 //-----------------------------------------------------------------------------
2047 // name: type_engine_scan2_loop()
2048 // desc: ...
2049 //-----------------------------------------------------------------------------
2050 t_CKBOOL type_engine_scan2_loop( Chuck_Env * env, a_Stmt_Loop stmt )
2051 {
2052 // check the conditional
2053 if( !type_engine_scan2_exp( env, stmt->cond ) )
2054 return FALSE;
2055
2056 // TODO: same as if - ensure the type in conditional is valid
2057
2058 // check the body
2059 if( !type_engine_scan2_stmt( env, stmt->body ) )
2060 return FALSE;
2061
2062 return TRUE;
2063 }
2064
2065
2066
2067
2068 //-----------------------------------------------------------------------------
2069 // name: type_engine_scan2_switch()
2070 // desc: ...
2071 //-----------------------------------------------------------------------------
2072 t_CKBOOL type_engine_scan2_switch( Chuck_Env * env, a_Stmt_Switch stmt )
2073 {
2074 // TODO: implement this
2075 EM_error2( stmt->where, "switch not implemented..." );
2076
2077 return FALSE;
2078 }
2079
2080
2081
2082
2083 //-----------------------------------------------------------------------------
2084 // name: type_engine_scan2_break()
2085 // desc: ...
2086 //-----------------------------------------------------------------------------
2087 t_CKBOOL type_engine_scan2_break( Chuck_Env * env, a_Stmt_Break br )
2088 {
2089 return TRUE;
2090 }
2091
2092
2093
2094
2095 //-----------------------------------------------------------------------------
2096 // name: type_engine_scan2_continue()
2097 // desc: ...
2098 //-----------------------------------------------------------------------------
2099 t_CKBOOL type_engine_scan2_continue( Chuck_Env * env, a_Stmt_Continue cont )
2100 {
2101 return TRUE;
2102 }
2103
2104
2105
2106
2107 //-----------------------------------------------------------------------------
2108 // name: type_engine_scan2_return()
2109 // desc: ...
2110 //-----------------------------------------------------------------------------
2111 t_CKBOOL type_engine_scan2_return( Chuck_Env * env, a_Stmt_Return stmt )
2112 {
2113 t_CKBOOL ret = FALSE;
2114
2115 // check the type of the return
2116 if( stmt->val )
2117 ret = type_engine_scan2_exp( env, stmt->val );
2118 else
2119 ret = TRUE;
2120
2121 return ret;
2122 }
2123
2124
2125
2126
2127 //-----------------------------------------------------------------------------
2128 // name: type_engine_scan2_code_segment()
2129 // desc: ...
2130 //-----------------------------------------------------------------------------
2131 t_CKBOOL type_engine_scan2_code_segment( Chuck_Env * env, a_Stmt_Code stmt,
2132 t_CKBOOL push )
2133 {
2134 // class
2135 env->class_scope++;
2136 // push
2137 if( push ) env->curr->value.push(); // env->context->nspc.value.push();
2138 // do it
2139 t_CKBOOL t = type_engine_scan2_stmt_list( env, stmt->stmt_list );
2140 // pop
2141 if( push ) env->curr->value.pop(); // env->context->nspc.value.pop();
2142 // class
2143 env->class_scope--;
2144
2145 return t;
2146 }
2147
2148
2149
2150
2151 //-----------------------------------------------------------------------------
2152 // name: type_engine_scan2_exp()
2153 // desc: ...
2154 //-----------------------------------------------------------------------------
2155 t_CKBOOL type_engine_scan2_exp( Chuck_Env * env, a_Exp exp )
2156 {
2157 a_Exp curr = exp;
2158 t_CKBOOL ret = TRUE;
2159
2160 // loop through parallel expressions
2161 while( curr )
2162 {
2163 // examine the syntax
2164 switch( curr->s_type )
2165 {
2166 case ae_exp_binary:
2167 ret = type_engine_scan2_exp_binary( env, &curr->binary );
2168 break;
2169
2170 case ae_exp_unary:
2171 ret = type_engine_scan2_exp_unary( env, &curr->unary );
2172 break;
2173
2174 case ae_exp_cast:
2175 ret = type_engine_scan2_exp_cast( env, &curr->cast );
2176 break;
2177
2178 case ae_exp_postfix:
2179 ret = type_engine_scan2_exp_postfix( env, &curr->postfix );
2180 break;
2181
2182 case ae_exp_dur:
2183 ret = type_engine_scan2_exp_dur( env, &curr->dur );
2184 break;
2185
2186 case ae_exp_primary:
2187 ret = type_engine_scan2_exp_primary( env, &curr->primary );
2188 break;
2189
2190 case ae_exp_array:
2191 ret = type_engine_scan2_exp_array( env, &curr->array );
2192 break;
2193
2194 case ae_exp_func_call:
2195 ret = type_engine_scan2_exp_func_call( env, &curr->func_call );
2196 break;
2197
2198 case ae_exp_dot_member:
2199 ret = type_engine_scan2_exp_dot_member( env, &curr->dot_member );
2200 break;
2201
2202 case ae_exp_if:
2203 ret = type_engine_scan2_exp_if( env, &curr->exp_if );
2204 break;
2205
2206 case ae_exp_decl:
2207 ret = type_engine_scan2_exp_decl( env, &curr->decl );
2208 break;
2209
2210 default:
2211 EM_error2( curr->where,
2212 "internal compiler error - no expression type '%i'...",
2213 curr->s_type );
2214 return FALSE;
2215 }
2216
2217 // error
2218 if( !ret )
2219 return FALSE;
2220
2221 // advance to next expression
2222 curr = curr->next;
2223 }
2224
2225 // return type
2226 return ret;
2227 }
2228
2229
2230
2231
2232 //-----------------------------------------------------------------------------
2233 // name: type_engine_scan2_exp_binary()
2234 // desc: ...
2235 //-----------------------------------------------------------------------------
2236 t_CKBOOL type_engine_scan2_exp_binary( Chuck_Env * env, a_Exp_Binary binary )
2237 {
2238 a_Exp cl = binary->lhs, cr = binary->rhs;
2239
2240 // scan lhs
2241 t_CKBOOL left = type_engine_scan2_exp( env, cl );
2242 if( !left ) return FALSE;
2243 // scan rhs
2244 t_CKBOOL right = type_engine_scan2_exp( env, cr);
2245 if( !right ) return FALSE;
2246
2247 // cross chuck
2248 while( cr )
2249 {
2250 // type check the pair
2251 if( !type_engine_scan2_op( env, binary->op, cl, cr, binary ) )
2252 return FALSE;
2253
2254 cr = cr->next;
2255 }
2256
2257 return TRUE;
2258 }
2259
2260
2261
2262
2263 //-----------------------------------------------------------------------------
2264 // name: type_engine_scan2_op()
2265 // desc: ...
2266 //-----------------------------------------------------------------------------
2267 t_CKBOOL type_engine_scan2_op( Chuck_Env * env, ae_Operator op, a_Exp lhs, a_Exp rhs,
2268 a_Exp_Binary binary )
2269 {
2270 // TODO: check for static here
2271
2272 return TRUE;
2273 }
2274
2275
2276
2277
2278 //-----------------------------------------------------------------------------
2279 // name: type_engine_scan2_op_chuck()
2280 // desc: ...
2281 //-----------------------------------------------------------------------------
2282 t_CKBOOL type_engine_scan2_op_chuck( Chuck_Env * env, a_Exp lhs, a_Exp rhs,
2283 a_Exp_Binary binary )
2284 {
2285 return TRUE;
2286 }
2287
2288
2289
2290
2291 //-----------------------------------------------------------------------------
2292 // name: type_engine_scan2_op_unchuck()
2293 // desc: ...
2294 //-----------------------------------------------------------------------------
2295 t_CKBOOL type_engine_scan2_op_unchuck( Chuck_Env * env, a_Exp lhs, a_Exp rhs )
2296 {
2297 return TRUE;
2298 }
2299
2300
2301
2302
2303 //-----------------------------------------------------------------------------
2304 // name: type_engine_scan2_op_upchuck()
2305 // desc: ...
2306 //-----------------------------------------------------------------------------
2307 t_CKBOOL type_engine_scan2_op_upchuck( Chuck_Env * env, a_Exp lhs, a_Exp rhs )
2308 {
2309 return TRUE;
2310 }
2311
2312
2313
2314
2315 //-----------------------------------------------------------------------------
2316 // name: type_engine_scan2_op_at_chuck()
2317 // desc: ...
2318 //-----------------------------------------------------------------------------
2319 t_CKBOOL type_engine_scan2_op_at_chuck( Chuck_Env * env, a_Exp lhs, a_Exp rhs )
2320 {
2321 return TRUE;
2322 }
2323
2324
2325
2326
2327 //-----------------------------------------------------------------------------
2328 // name: type_engine_scan2_exp_unary()
2329 // desc: ...
2330 //-----------------------------------------------------------------------------
2331 t_CKBOOL type_engine_scan2_exp_unary( Chuck_Env * env, a_Exp_Unary unary )
2332 {
2333 return TRUE;
2334 }
2335
2336
2337
2338
2339 //-----------------------------------------------------------------------------
2340 // name: type_engine_scan2_exp_primary()
2341 // desc: ...
2342 //-----------------------------------------------------------------------------
2343 t_CKBOOL type_engine_scan2_exp_primary( Chuck_Env * env, a_Exp_Primary exp )
2344 {
2345 // check syntax
2346 switch( exp->s_type )
2347 {
2348 // variable
2349 case ae_primary_var:
2350 case ae_primary_num:
2351 case ae_primary_float:
2352 case ae_primary_str:
2353 case ae_primary_char:
2354 break;
2355
2356 // array literal
2357 case ae_primary_array:
2358 type_engine_scan2_exp_array_lit( env, exp );
2359 break;
2360
2361 // complex literal
2362 case ae_primary_complex:
2363 // if( !type_engine_scan2_exp_complex_lit( env, exp ) ) return FALSE;
2364 break;
2365
2366 // polar literal
2367 case ae_primary_polar:
2368 // if( !type_engine_scan2_exp_polar_lit( env, exp ) ) return FALSE;
2369 break;
2370
2371 // vector literal, ge: added 1.3.5.3
2372 case ae_primary_vec:
2373 // if( !type_engine_scan2_exp_vec_lit( env, exp ) ) return FALSE;
2374 break;
2375
2376 // expression
2377 case ae_primary_exp:
2378 if( !type_engine_scan2_exp( env, exp->exp ) ) return FALSE;
2379 break;
2380
2381 // hack
2382 case ae_primary_hack:
2383 // make sure not l-value (this should be checked in type_engine_scan1_exp_primary()
2384 assert( exp->exp->s_type != ae_exp_decl );
2385 // scan
2386 if( !type_engine_scan2_exp( env, exp->exp ) ) return FALSE;
2387 break;
2388
2389 // nil (void)
2390 case ae_primary_nil:
2391 break;
2392
2393 // no match
2394 default:
2395 EM_error2( exp->where,
2396 "internal error - unrecognized primary type '%i'", exp->s_type );
2397 return FALSE;
2398 break;
2399 }
2400
2401 return TRUE;
2402 }
2403
2404
2405
2406
2407 //-----------------------------------------------------------------------------
2408 // name: type_engine_scan2_exp_array_lit()
2409 // desc: ...
2410 //-----------------------------------------------------------------------------
2411 t_CKBOOL type_engine_scan2_exp_array_lit( Chuck_Env * env, a_Exp_Primary exp )
2412 {
2413 // verify there are no errors from the parser...
2414 if( !verify_array( exp->array ) )
2415 return FALSE;
2416
2417 return TRUE;
2418 }
2419
2420
2421
2422
2423 //-----------------------------------------------------------------------------
2424 // name: type_engine_scan2_exp_cast()
2425 // desc: ...
2426 //-----------------------------------------------------------------------------
2427 t_CKBOOL type_engine_scan2_exp_cast( Chuck_Env * env, a_Exp_Cast cast )
2428 {
2429 // check the exp
2430 t_CKBOOL t = type_engine_scan2_exp( env, cast->exp );
2431 if( !t ) return FALSE;
2432
2433 return TRUE;
2434 }
2435
2436
2437
2438
2439 //-----------------------------------------------------------------------------
2440 // name: type_engine_scan2_exp_dur()
2441 // desc: ...
2442 //-----------------------------------------------------------------------------
2443 t_CKBOOL type_engine_scan2_exp_dur( Chuck_Env * env, a_Exp_Dur dur )
2444 {
2445 // type check the two components
2446 t_CKBOOL base = type_engine_scan2_exp( env, dur->base );
2447 t_CKBOOL unit = type_engine_scan2_exp( env, dur->unit );
2448
2449 // make sure both type check
2450 if( !base || !unit ) return FALSE;
2451
2452 return TRUE;
2453 }
2454
2455
2456
2457
2458 //-----------------------------------------------------------------------------
2459 // name: type_engine_scan2_exp_postfix()
2460 // desc: ...
2461 //-----------------------------------------------------------------------------
2462 t_CKBOOL type_engine_scan2_exp_postfix( Chuck_Env * env, a_Exp_Postfix postfix )
2463 {
2464 // check the exp
2465 t_CKBOOL t = type_engine_scan2_exp( env, postfix->exp );
2466 if( !t ) return FALSE;
2467
2468 // syntax
2469 // TODO: figure out ++/--
2470 switch( postfix->op )
2471 {
2472 case ae_op_plusplus:
2473 case ae_op_minusminus:
2474 // assignable?
2475 if( postfix->exp->s_meta != ae_meta_var )
2476 {
2477 EM_error2( postfix->exp->where,
2478 "postfix operator '%s' cannot be used on non-mutable data-type...",
2479 op2str( postfix->op ) );
2480 return FALSE;
2481 }
2482
2483 // TODO: mark somewhere we need to post increment
2484
2485 return TRUE;
2486 break;
2487
2488 default:
2489 // no match
2490 EM_error2( postfix->where,
2491 "internal compiler error (pre-scan): unrecognized postfix '%i'", postfix->op );
2492 return FALSE;
2493 }
2494
2495 return TRUE;
2496 }
2497
2498
2499
2500
2501 //-----------------------------------------------------------------------------
2502 // name: type_engine_scan2_exp_if()
2503 // desc: ...
2504 //-----------------------------------------------------------------------------
2505 t_CKBOOL type_engine_scan2_exp_if( Chuck_Env * env, a_Exp_If exp_if )
2506 {
2507 // check the components
2508 t_CKBOOL cond = type_engine_scan2_exp( env, exp_if->cond );
2509 t_CKBOOL if_exp = type_engine_scan2_exp( env, exp_if->if_exp );
2510 t_CKBOOL else_exp = type_engine_scan2_exp( env, exp_if->else_exp );
2511
2512 // make sure everything good
2513 if( !cond || !if_exp || !else_exp ) return FALSE;
2514
2515 return TRUE;
2516 }
2517
2518
2519
2520
2521 //-----------------------------------------------------------------------------
2522 // name: type_engine_scan2_array_subscripts( )
2523 // desc: ...
2524 //-----------------------------------------------------------------------------
2525 t_CKBOOL type_engine_scan2_array_subscripts( Chuck_Env * env, a_Exp exp_list )
2526 {
2527 return TRUE;
2528 }
2529
2530
2531
2532
2533 //-----------------------------------------------------------------------------
2534 // name: type_engine_scan2_exp_decl_create() | 1.5.1.1 (ge)
2535 // *** adapted from type_engine_scan2_exp_decl() pre-1.5.0.8
2536 // *** which became type_engine_check_exp_decl_part1() in 1.5.0.8
2537 // reason: 'auto' needs more context before it can be processed | 1.5.0.8 (ge)
2538 // however, class variables are meant to be accessible out-of-order
2539 // and needs decl created in an earlier pass | 1.5.1.1 (ge)
2540 // desc: this can now be called either from type_scan if not 'auto' in order
2541 // to support accessing class member decls from anywhere (e.g., in the
2542 // same file, above the actual decl); or it can be deferred in the case
2543 // of 'auto' from chuck_type | 1.5.1.1 (ge)
2544 // caveat: 'auto' is disallowed for now as a top-level class member decl type
2545 //-----------------------------------------------------------------------------
2546 t_CKBOOL type_engine_scan2_exp_decl_create( Chuck_Env * env, a_Exp_Decl decl )
2547 {
2548 a_Var_Decl_List list = decl->var_decl_list;
2549 a_Var_Decl var_decl = NULL;
2550 Chuck_Type * type = NULL;
2551 Chuck_Value * value = NULL;
2552 t_CKBOOL do_alloc = TRUE;
2553 t_CKBOOL is_first_in_list = TRUE;
2554
2555 // check to see if Part 1 was already processed | 1.5.1.0 (ge)
2556 // NOTE Part 1 (migrated here from scan2b_exp_decl() in 1.5.0.8 to support
2557 // 'auto', can only be run once as it creates and adds values into the
2558 // current scope; yet a decl expression could be checked more than once
2559 // due to chained chuck statements like: `440 => float f => osc.freq;`
2560 if( list && list->var_decl && list->var_decl->value )
2561 return TRUE;
2562
2563 // retrieve the type
2564 type = decl->ck_type;
2565 // make sure it's not NULL
2566 assert( type != NULL );
2567
2568 // check to see type is not void or auto
2569 // by this point, any 'auto' should have been resolved
2570 if( type->size == 0 )
2571 {
2572 // see if auto was declared
2573 if( isa( type, env->ckt_auto ) )
2574 {
2575 EM_error2( decl->type->where,
2576 "cannot use 'auto' type variable declaration here..." );
2577 EM_error2( 0, "(hint: 'auto' requires either initialization or specific contexts)" );
2578 }
2579 else
2580 {
2581 EM_error2( decl->where,
2582 "cannot declare variables of size '0' (i.e. 'void')..." );
2583 }
2584 return FALSE;
2585 }
2586
2587 // T @ foo?
2588 do_alloc = !decl->type->ref;
2589
2590 // make sure complete
2591 if( /*!t->is_complete &&*/ do_alloc )
2592 {
2593 // check to see if class inside itself
2594 if( env->class_def && equals( type, env->class_def ) && env->class_scope == 0 )
2595 {
2596 EM_error2( decl->where,
2597 "...(note: object of type '%s' declared inside itself)",
2598 type->c_name() );
2599 return FALSE;
2600 }
2601 }
2602
2603 // primitive
2604 if( (isprim( env, type ) || isa( type, env->ckt_string )) && decl->type->ref ) // TODO: string
2605 {
2606 // check for string type
2607 t_CKBOOL isaStr = isa( type, env->ckt_string );
2608 // error
2609 EM_error2( decl->where,
2610 "cannot declare references (@) of%s type '%s'...", isaStr ? "" : " primitive",
2611 type->c_name() );
2612 // more info
2613 if( !isaStr )
2614 {
2615 EM_error2( 0,
2616 "...(primitive types: 'int', 'float', 'time', 'dur', 'vec3', etc.)" );
2617 }
2618 else
2619 {
2620 EM_error2( 0,
2621 "...(NOTE 'string' is a special Object whose operational semantics resemble both Object types and primitive types; e.g., instantiation and function argument-passing are like any other Object; however assignment '@=>' and '=>' are carried out by-value, as with primitive types such as 'int', 'float', 'time', 'dur', 'vec3', etc.)" );
2622 }
2623 return FALSE;
2624 }
2625
2626 // loop through the variables
2627 while( list )
2628 {
2629 // 1.4.2.0 (ge) | reset the type variable to the lead type in the decl
2630 // e.g., for cases like int x[2], y;
2631 // ...this is so y would not be associated with x's array type
2632 type = decl->ck_type;
2633
2634 // get the decl
2635 var_decl = list->var_decl;
2636 // 1.4.2.0 (ge) | by default, copy the decl type reference bit
2637 // this could be overwritten later as appropriate, e.g., by array vars
2638 var_decl->ref = decl->type->ref;
2639
2640 // check if reserved
2641 if( type_engine_check_reserved( env, var_decl->xid, var_decl->where ) )
2642 {
2643 EM_error2( var_decl->where,
2644 "...in variable declaration", S_name(var_decl->xid) );
2645 return FALSE;
2646 }
2647
2648 // check if locally defined
2649 if( env->curr->lookup_value( var_decl->xid, FALSE ) )
2650 {
2651 EM_error2( var_decl->where,
2652 "'%s' has already been defined in the same scope",
2653 S_name(var_decl->xid) );
2654 return FALSE;
2655 }
2656
2657 // check for constructor args | 1.5.2.0 (ge) added
2658 if( var_decl->ctor.args != NULL )
2659 {
2660 // type check the exp
2661 if( !type_engine_scan2_exp( env, var_decl->ctor.args ) )
2662 return FALSE;
2663 }
2664
2665 // check if array
2666 if( var_decl->array != NULL )
2667 {
2668 // 1.4.2.0 (ge) was: decl->type->ref;
2669 var_decl->ref = ( var_decl->array->exp_list == NULL );
2670 // the declaration type | 1.4.2.0 (ge) fixed for multiple decl (e.g., int x[1], y[2];)
2671 Chuck_Type * t2 = decl->ck_type; // was: type, which won't work if more than one var declared
2672 // 1.5.4.4 (ge) added base_type and logic in case decl->ck_type is an array
2673 // (typically this can't happen with a decl, except for 'auto' and multidim arrays)
2674 // for example: https://github.com/ccrma/chuck/issues/441
2675 Chuck_Type * base_type = t2->array_depth ? t2->array_type : t2;
2676 // get (or create) matching array type
2677 type = env->get_array_type(
2678 env->ckt_array, // the array base class
2679 var_decl->array->depth, // the depth of the new type
2680 base_type // the 'array_type' | 1.5.4.4 (ge) changed to t2->array_type, in case t2 is an array
2681 // env->curr // the owner namespace
2682 );
2683
2684 // 1.4.2.0 (ge) | assign new array type to current var decl
2685 // for handling the following kind of multi-var declarations
2686 // int x[1], y[2];
2687 // int x, y[1];
2688 // set reference : var_decl->ck_type = type;
2689 CK_SAFE_REF_ASSIGN( var_decl->ck_type, type );
2690
2691 // 1.4.2.0 (ge) | if one and only one variable, then update decl->ck_type
2692 // otherwise, the variables could have different array depths, and therefore different types
2693 // also note: cannot => to a multi-variable declaration (e.g., 5 => int x, y;)
2694 // this is to support array initialization (e.g., [ [1,2], [3,4] ] @=> int x[][];)
2695 if( is_first_in_list && list->next == NULL )
2696 {
2697 // set reference : var_decl->ck_type = type;
2698 CK_SAFE_REF_ASSIGN( decl->ck_type, type );
2699 }
2700 }
2701
2702 // enter into value binding
2703 env->curr->add_value( S_name(var_decl->xid),
2704 value = env->context->new_Chuck_Value( type, S_name(var_decl->xid) ) );
2705
2706 // remember the owner
2707 value->owner = env->curr; CK_SAFE_ADD_REF( value->owner );
2708 value->owner_class = env->func ? NULL : env->class_def;
2709 // set if is instanced member
2710 value->is_instance_member = ( env->class_def != NULL &&
2711 env->class_scope == 0 &&
2712 env->func == NULL && !decl->is_static );
2713 // 1.5.4.4 (ge) moved is_static set here from type_engine_check_exp_decl_part2()
2714 value->is_static = (decl->is_static != 0);
2715 value->is_context_global = ( env->class_def == NULL && env->func == NULL );
2716 value->addr = var_decl->addr;
2717 // flag it until the decl is checked
2718 value->is_decl_checked = FALSE;
2719 // flag as global
2720 value->is_global = decl->is_global;
2721 // flag as const | 1.5.1.3 (ge) added
2722 value->is_const = decl->is_const;
2723
2724 // if we are located inside a statement
2725 if( decl->is_static && env->stmt_stack.size() )
2726 {
2727 // mark containing statement as having static decl | 1.5.4.3 (ge) added as part of #2024-static-init
2728 // 1.5.4.4 (ge) moved this logic from type_engine_check_exp_decl_part2()
2729 env->stmt_stack.back()->hasStaticDecl = TRUE;
2730 }
2731
2732 // dependency tracking: remember the code position of the DECL | 1.5.0.8
2733 // NOTE track if context-global and not global, or class-member, or static (1.5.4.4 added static check)
2734 if( (value->is_context_global && !value->is_global ) || value->is_instance_member || value->is_static )
2735 value->depend_init_where = var_decl->where;
2736
2737 // remember the value
2738 var_decl->value = value;
2739
2740 // the next var decl
2741 list = list->next;
2742 // 1.4.2.0 (ge) | added
2743 is_first_in_list = FALSE;
2744 }
2745
2746 return TRUE;
2747 }
2748
2749
2750
2751
2752 //-----------------------------------------------------------------------------
2753 // name: type_engine_scan2_exp_decl( )
2754 // desc: ...
2755 //-----------------------------------------------------------------------------
2756 t_CKBOOL type_engine_scan2_exp_decl( Chuck_Env * env, a_Exp_Decl decl )
2757 {
2758 a_Var_Decl_List list = decl->var_decl_list;
2759 a_Var_Decl var_decl = NULL;
2760
2761 // loop through the variables
2762 while( list != NULL )
2763 {
2764 // get the decl
2765 var_decl = list->var_decl;
2766
2767 // check if array
2768 if( var_decl->array != NULL )
2769 {
2770 // verify there are no errors from the parser...
2771 if( !verify_array( var_decl->array ) )
2772 return FALSE;
2773
2774 if( var_decl->array->exp_list )
2775 {
2776 // scan2 the exp
2777 if( !type_engine_scan2_exp( env, var_decl->array->exp_list ) )
2778 return FALSE;
2779 // make sure types are of int
2780 if( !type_engine_scan2_array_subscripts( env, var_decl->array->exp_list ) )
2781 return FALSE;
2782 }
2783 }
2784
2785 // advance
2786 list = list->next;
2787 }
2788
2789 // check whether decl type is auto | 1.5.1.1
2790 if( !isa(decl->ck_type, env->ckt_auto) )
2791 {
2792 // not auto? go ahead and create the decl
2793 if( !type_engine_scan2_exp_decl_create( env, decl ) )
2794 return FALSE;
2795 }
2796 else // decl type is auto
2797 {
2798 // disallow 'auto' for top-level class member variables
2799 if( env->class_def && env->class_scope == 0 )
2800 {
2801 EM_error2( decl->type->where,
2802 "cannot use 'auto' type for top-level class variables..." );
2803 return FALSE;
2804 }
2805
2806 // nothing more to do for now
2807 // defer decl creation until chuck_type has a chance to infer the auto type
2808 }
2809
2810 return TRUE;
2811 }
2812
2813
2814
2815
2816 //-----------------------------------------------------------------------------
2817 // name: type_engine_scan2_exp_func_call()
2818 // desc: ...
2819 //-----------------------------------------------------------------------------
2820 t_CKBOOL type_engine_scan2_exp_func_call( Chuck_Env * env, a_Exp exp_func, a_Exp args,
2821 t_CKFUNC & ck_func, int linepos )
2822 {
2823 // Chuck_Func * func = NULL;
2824 // Chuck_Func * up = NULL;
2825
2826 // type check the func
2827 t_CKBOOL f = type_engine_scan2_exp( env, exp_func );
2828 if( !f ) return FALSE;
2829
2830 // check the arguments
2831 if( args )
2832 {
2833 t_CKBOOL a = type_engine_scan2_exp( env, args );
2834 if( !a ) return FALSE;
2835 }
2836
2837 return TRUE;
2838 }
2839
2840
2841
2842
2843 //-----------------------------------------------------------------------------
2844 // name: type_engine_scan2_exp_func_call()
2845 // desc: ...
2846 //-----------------------------------------------------------------------------
2847 t_CKBOOL type_engine_scan2_exp_func_call( Chuck_Env * env, a_Exp_Func_Call func_call )
2848 {
2849 // type check it
2850 return type_engine_scan2_exp_func_call( env, func_call->func, func_call->args,
2851 func_call->ck_func, func_call->where );
2852 }
2853
2854
2855
2856
2857 //-----------------------------------------------------------------------------
2858 // name: type_engine_scan2_exp_dot_member()
2859 // desc: ...
2860 //-----------------------------------------------------------------------------
2861 t_CKBOOL type_engine_scan2_exp_dot_member( Chuck_Env * env, a_Exp_Dot_Member member )
2862 {
2863 // type check the base
2864 t_CKBOOL base = type_engine_scan2_exp( env, member->base );
2865 if( !base ) return FALSE;
2866
2867 return TRUE;
2868 }
2869
2870
2871
2872
2873 //-----------------------------------------------------------------------------
2874 // name: type_engine_scan2_exp_array()
2875 // desc: ...
2876 //-----------------------------------------------------------------------------
2877 t_CKBOOL type_engine_scan2_exp_array( Chuck_Env * env, a_Exp_Array array )
2878 {
2879 // verify there are no errors from the parser...
2880 if( !verify_array( array->indices ) )
2881 return FALSE;
2882
2883 // type check the base
2884 t_CKBOOL base = type_engine_scan2_exp( env, array->base );
2885 if( !base ) return FALSE;
2886
2887 // type check the index
2888 t_CKBOOL index = type_engine_scan2_exp( env, array->indices->exp_list );
2889 if( !index ) return FALSE;
2890
2891 // cycle through each exp
2892 // a_Exp e = array->indices->exp_list;
2893 // count the dimension
2894 // t_CKUINT depth = 0;
2895
2896 return TRUE;
2897 }
2898
2899
2900
2901
2902 //-----------------------------------------------------------------------------
2903 // name: type_engine_scan2_class_def()
2904 // desc: ...
2905 //-----------------------------------------------------------------------------
2906 t_CKBOOL type_engine_scan2_class_def( Chuck_Env * env, a_Class_Def class_def )
2907 {
2908 // the return type
2909 t_CKBOOL ret = TRUE;
2910 // the class body
2911 a_Class_Body body = class_def->body;
2912 // the class
2913 Chuck_Type * the_class = class_def->type;
2914 // make sure
2915 assert( the_class != NULL );
2916
2917 // set the new type as current
2918 env->nspc_stack.push_back( env->curr );
2919 env->curr = the_class->nspc;
2920 // push the class def
2921 env->class_stack.push_back( env->class_def );
2922 env->class_def = the_class;
2923 // reset the nest list
2924 env->class_scope = 0;
2925
2926 // type check the body
2927 while( body && ret )
2928 {
2929 // check the section
2930 switch( body->section->s_type )
2931 {
2932 case ae_section_stmt:
2933 // already flagged as having a constructor or not
2934 ret = type_engine_scan2_stmt_list( env, body->section->stmt_list );
2935 break;
2936
2937 case ae_section_func:
2938 // do function
2939 ret = type_engine_scan2_func_def( env, body->section->func_def );
2940 break;
2941
2942 case ae_section_class:
2943 // do the class
2944 ret = type_engine_scan2_class_def( env, body->section->class_def );
2945 break;
2946 }
2947
2948 // move to the next section
2949 body = body->next;
2950 }
2951
2952 // pop the class
2953 env->class_def = env->class_stack.back();
2954 env->class_stack.pop_back();
2955 // pop the namesapce
2956 env->curr = env->nspc_stack.back();
2957 env->nspc_stack.pop_back();
2958
2959 // the parent class | 1.5.1.5 (ge) moved from chuck_type module for earlier info
2960 t_CKTYPE t_parent = NULL;
2961
2962 // make sure inheritance
2963 // TODO: sort!
2964 if( class_def->ext )
2965 {
2966 // if extend
2967 if( class_def->ext->extend_id )
2968 {
2969 // find the type
2970 t_parent = type_engine_find_type( env, class_def->ext->extend_id );
2971 if( !t_parent )
2972 {
2973 EM_error2( class_def->ext->extend_id->where,
2974 "undefined super class '%s' in definition of class '%s'",
2975 type_path(class_def->ext->extend_id), S_name(class_def->name->xid) );
2976 return FALSE;
2977 }
2978
2979 // must not be primitive
2980 if( isprim( env, t_parent ) )
2981 {
2982 EM_error2( class_def->ext->extend_id->where,
2983 "cannot extend primitive type '%s'",
2984 t_parent->c_name() );
2985 EM_error2( 0, "...(primitive types: 'int', 'float', 'time', 'dur', 'vec3', etc.)" );
2986 return FALSE;
2987 }
2988 }
2989
2990 // TODO: interface
2991 }
2992
2993 // by default object
2994 if( !t_parent ) t_parent = env->ckt_object;
2995
2996 // check for fun
2997 assert( env->context != NULL );
2998 assert( class_def->type != NULL );
2999 assert( class_def->type->nspc != NULL );
3000
3001 // retrieve the new type (created in scan_class_def)
3002 the_class = class_def->type;
3003
3004 // set fields not set in scan
3005 the_class->parent_type = t_parent; CK_SAFE_ADD_REF(t_parent);
3006 // inherit ugen_info data from parent PLD
3007 the_class->ugen_info = t_parent->ugen_info; CK_SAFE_ADD_REF(t_parent->ugen_info);
3008
3009 return ret;
3010 }
3011
3012
3013
3014
3015 //-----------------------------------------------------------------------------
3016 // name: type_engine_scan2_func_def()
3017 // desc: type scan 2 for function definition
3018 //-----------------------------------------------------------------------------
3019 t_CKBOOL type_engine_scan2_func_def( Chuck_Env * env, a_Func_Def f )
3020 {
3021 Chuck_Type * type = NULL;
3022 Chuck_Value * value = NULL;
3023 Chuck_Func * func = NULL;
3024
3025 // Chuck_Type * parent = NULL;
3026 Chuck_Value * overload = NULL;
3027 // Chuck_Value * override = NULL;
3028 Chuck_Value * v = NULL;
3029 // Chuck_Func * parent_func = NULL;
3030 a_Arg_List arg_list = NULL;
3031 // t_CKBOOL parent_match = FALSE;
3032 string func_name = S_name(f->name);
3033 string orig_name = func_name;
3034 vector<Chuck_Value *> values;
3035 vector<a_Arg_List> symbols;
3036 t_CKUINT count = 0;
3037 Chuck_Func * overfunc = NULL;
3038 // t_CKBOOL has_code = FALSE; // use this for both user and imported
3039 t_CKBOOL op_overload = ( f->op2overload != ae_op_none );
3040 // is in constructor? | 1.5.2.0
3041 t_CKBOOL isInCtor = isctor(env,f);
3042 // is in destructor? | 1.5.2.0
3043 t_CKBOOL isInDtor = isdtor(env,f);
3044
3045 // see if we are already in a function definition
3046 if( env->func != NULL )
3047 {
3048 EM_error2( f->where,
3049 "nested function definitions are not (yet) allowed" );
3050 return FALSE;
3051 }
3052
3053 // make sure a code segment is in stmt - else we should push scope
3054 assert( !f->code || f->code->s_type == ae_stmt_code );
3055
3056 // look up the value in the current class (can shadow?)
3057 overload = env->curr->lookup_value( f->name, FALSE );
3058 if( overload )
3059 {
3060 // if value
3061 if( !isa( overload->type, env->ckt_function ) )
3062 {
3063 EM_error2( f->where,
3064 "function name '%s' is already used by another value", S_name(f->name) );
3065 return FALSE;
3066 }
3067 else
3068 {
3069 // overload
3070 if( !overload->func_ref )
3071 {
3072 // error
3073 EM_error2( f->where,
3074 "(internal error) missing function '%s'",
3075 overload->name.c_str() );
3076 return FALSE;
3077 }
3078 }
3079 }
3080
3081 // if overload
3082 if( overload )
3083 {
3084 // make the new name
3085 func_name += "@" + ck_itoa( ++overload->func_num_overloads ) + "@" + env->curr->name;
3086 }
3087 else
3088 {
3089 // make name using 0
3090 func_name += "@0@" + env->curr->name;
3091 }
3092
3093 // make sure a code segment is in stmt - else we should push scope
3094 assert( !f->code || f->code->s_type == ae_stmt_code );
3095
3096 // make a new func object
3097 func = env->context->new_Chuck_Func();
3098 // set the name
3099 func->name = func_name;
3100 // set the base name (i.e., without designations); 1.4.1.0
3101 func->base_name = orig_name;
3102 // reference the function definition
3103 func->funcdef_connect( f ); // 1.5.0.5 (ge) part of new AST cleanup
3104 // note whether the function is marked as member
3105 func->is_member = (f->static_decl != ae_key_static) &&
3106 (env->class_def != NULL);
3107 // note whether the function is marked as static (in class)
3108 func->is_static = (f->static_decl == ae_key_static) &&
3109 (env->class_def != NULL);
3110
3111 // always create the code (will be filled in later on, depending on builtin vs in-language etc.)
3112 // 1.5.2.0 (ge) moved up from ae_func_builtin
3113 func->code = new Chuck_VM_Code;
3114 // add reference
3115 CK_SAFE_ADD_REF( func->code );
3116 // add name | 1.5.2.0
3117 func->code->name = S_name(f->name);
3118 // copy the native code, for imported functions
3119 if( f->s_type == ae_func_builtin )
3120 {
3121 // whether the function needs 'this'
3122 func->code->need_this = func->is_member;
3123 // is static inside
3124 func->code->is_static = func->is_static;
3125 // set the function pointer
3126 func->code->native_func = (t_CKUINT)func->def()->dl_func_ptr;
3127 // set the function pointer kind | 1.5.2.0
3128 func->code->native_func_kind = func->def()->dl_fp_kind;
3129 }
3130
3131 // make a new type for the function
3132 type = env->context->new_Chuck_Type( env );
3133 // type id
3134 type->xid = te_function;
3135 // type size
3136 type->size = sizeof(void *);
3137 // function type name; this will be updated later in this function once we have more info (e.g., value_ref)
3138 type->base_name = "[fun]";
3139 // 1.5.4.3 (ge) add ref; was: type->parent_type = env->ckt_function;
3140 // part of #2024-func-call-update
3141 CK_SAFE_REF_ASSIGN(type->parent_type, env->ckt_function);
3142 // 1.5.4.3 (ge) add ref; was: type->func_bridge = func;
3143 // part of #2024-func-call-update | TODO: break cycle?
3144 CK_SAFE_REF_ASSIGN(type->func_bridge, func);
3145 // 1.5.4.3 (ge) add func to type reference
3146 // part of #2024-func-call-update | TODO: break cycle?
3147 CK_SAFE_REF_ASSIGN(func->type_ref, type);
3148
3149 // make new value, with potential overloaded name
3150 value = env->context->new_Chuck_Value( type, func_name );
3151 // it is const
3152 value->is_const = TRUE;
3153 // remember the owner
3154 value->owner = env->curr; CK_SAFE_ADD_REF( value->owner );
3155 value->owner_class = env->class_def; CK_SAFE_ADD_REF( value->owner_class );
3156 value->is_instance_member = func->is_member;
3157 // is global context
3158 value->is_context_global = env->class_def == NULL;
3159 // remember the func
3160 value->func_ref = func; CK_SAFE_ADD_REF( value->func_ref ); // add reference TODO: break cycle?
3161 // remember the value
3162 func->value_ref = value; CK_SAFE_ADD_REF( func->value_ref ); // add reference TODO: break cycle?
3163
3164 // set the func
3165 f->ck_func = func; func->add_ref(); // add reference
3166
3167 // if overload
3168 if( overload )
3169 {
3170 // add
3171 func->next = overload->func_ref->next;
3172 overload->func_ref->next = func;
3173 }
3174
3175 // make sure
3176 assert( f->ret_type != NULL );
3177
3178 // primitive
3179 if( (isprim( env, f->ret_type ) /*|| isa( f->ret_type, &t_string )*/)
3180 && f->type_decl->ref ) // TODO: string
3181 {
3182 EM_error2( f->type_decl->where,
3183 "cannot declare references (@) of primitive type '%s'...",
3184 f->ret_type->c_name() );
3185 EM_error2( f->type_decl->where,
3186 "...(primitive types: 'int', 'float', 'time', 'dur', 'vec3', etc.)" );
3187 goto error;
3188 }
3189
3190 // look up types for the function arguments
3191 arg_list = f->arg_list;
3192 // count
3193 count = 1;
3194 // make room for 'this'
3195 if( func->is_member )
3196 {
3197 f->stack_depth = sizeof(void *);
3198 }
3199 else if( func->is_static ) // 1.4.1.0 (ge) added
3200 {
3201 // make room for '@type' for static functions inside class def
3202 f->stack_depth = sizeof(void *);
3203 }
3204
3205 // loop over arguments
3206 while( arg_list )
3207 {
3208 // make sure the type is not NULL - it should have been
3209 // set in scan1_func_def()...
3210 assert( arg_list->type != NULL );
3211
3212 // make sure it's not void
3213 if( arg_list->type->size == 0 )
3214 {
3215 EM_error2( arg_list->where,
3216 "cannot declare variables of size '0' (i.e. 'void')..." );
3217 goto error;
3218 }
3219
3220 // check if reserved
3221 if( type_engine_check_reserved( env, arg_list->var_decl->xid, arg_list->where ) )
3222 {
3223 EM_error2( arg_list->where, "in function '%s'", func->signature().c_str() ); // S_name(f->name) );
3224 goto error;
3225 }
3226
3227 // look up in scope: later
3228 //if( env->curr->lookup_value( arg_list->var_decl->xid, FALSE ) )
3229 //{
3230 // EM_error2( arg_list->where, "in function '%s':", S_name(f->name) );
3231 // EM_error2( arg_list->where, "argument %i '%s' is already defined in this scope",
3232 // count, S_name(arg_list->var_decl->xid) );
3233 // goto error;
3234 //}
3235
3236 // primitive
3237 if( (isprim( env, arg_list->type ) /*|| isa( arg_list->type, &t_string )*/)
3238 && arg_list->type_decl->ref ) // TODO: string
3239 {
3240 EM_error2( arg_list->type_decl->where,
3241 "cannot declare references (@) of primitive type '%s'...",
3242 arg_list->type->c_name() );
3243 EM_error2( arg_list->type_decl->where,
3244 "...(primitive types: 'int', 'float', 'time', 'dur', 'vec3', etc.)" );
3245 goto error;
3246 }
3247
3248 // check if array
3249 if( arg_list->var_decl->array != NULL )
3250 {
3251 // verify there are no errors from the parser...
3252 if( !verify_array( arg_list->var_decl->array ) )
3253 return FALSE;
3254
3255 Chuck_Type * t = arg_list->type;
3256 Chuck_Type * t2 = t;
3257 // should be partial and empty []
3258 if( arg_list->var_decl->array->exp_list )
3259 {
3260 EM_error2( arg_list->where, "in function '%s':", func->signature().c_str() ); // S_name(f->name) );
3261 EM_error2( arg_list->where, "argument %i '%s' must be defined with empty []'s",
3262 count, S_name(arg_list->var_decl->xid) );
3263 goto error; // return FALSE;
3264 }
3265
3266 // get (or create) matching array type
3267 t = env->get_array_type(
3268 env->ckt_array, // the array base class
3269 arg_list->var_decl->array->depth, // the depth of the new type
3270 t2 // the 'array_type'
3271 // env->curr // the owner namespace
3272 );
3273
3274 // set ref
3275 arg_list->type_decl->ref = TRUE;
3276 // set type : arg_list->type = t;
3277 CK_SAFE_REF_ASSIGN( arg_list->type, t );
3278 }
3279
3280 // make new value
3281 v = env->context->new_Chuck_Value(
3282 arg_list->type, S_name(arg_list->var_decl->xid) );
3283 // remember the owner
3284 v->owner = env->curr; CK_SAFE_ADD_REF( v->owner );
3285 // function args not owned
3286 v->owner_class = NULL; CK_SAFE_ADD_REF( v->owner_class );
3287 v->is_instance_member = FALSE;
3288 v->is_const = FALSE; // 1.5.1.3 (ge) added explicitly for future reference
3289 // later: add as value
3290 // symbols.push_back( arg_list );
3291 // values.push_back( v );
3292 // later: env->curr->add_value( arg_list->var_decl->xid, v );
3293
3294 // stack
3295 v->offset = f->stack_depth;
3296 f->stack_depth += arg_list->type->size;
3297
3298 // remember
3299 arg_list->var_decl->value = v;
3300
3301 // count
3302 count++;
3303 // next arg
3304 arg_list = arg_list->next;
3305 }
3306
3307 // if overloading
3308 if( overload != NULL )
3309 {
3310 // -----------------------
3311 // 1.5.2.0 (ge) reinstating ability for overloaded funcs
3312 // (including operators) to return different types
3313 // -----------------------
3314 // make sure return types match
3315 // 1.5.0.0 (ge) more precise error reporting
3316 // -----------------------
3317 // if( *(f->ret_type) != *(overload->func_ref->def()->ret_type) )
3318 // {
3319 // EM_error2( f->where, "overloaded functions require matching return types..." );
3320 // // check if in class definition
3321 // if( env->class_def )
3322 // {
3323 // EM_error3( " |- function in question: %s %s.%s(...)",
3324 // func->def()->ret_type->base_name.c_str(), env->class_def->c_name(), S_name(f->name) );
3325 // EM_error3( " |- previous defined as: %s %s.%s(...)",
3326 // overload->func_ref->def()->ret_type->base_name.c_str(), env->class_def->c_name(), S_name(f->name) );
3327 // }
3328 // else
3329 // {
3330 // EM_error3( " |- function in question: %s %s(...)",
3331 // func->def()->ret_type->base_name.c_str(), S_name(f->name) );
3332 // EM_error3( " |- previous defined as: %s %s(...)",
3333 // overload->func_ref->def()->ret_type->base_name.c_str(), S_name(f->name) );
3334 // }
3335 // goto error;
3336 // }
3337
3338 // -----------------------
3339 // make sure not duplicate
3340 // 1.5.0.0 (ge) added
3341 // -----------------------
3342 overfunc = overload->func_ref;
3343 // loop over overloaded functions
3344 while( overfunc != NULL )
3345 {
3346 // one of these could this newly defined function
3347 if( func != overfunc )
3348 {
3349 // compare argument lists
3350 a_Arg_List lhs = func->def()->arg_list;
3351 a_Arg_List rhs = overfunc->def()->arg_list;
3352 // check
3353 if( same_arg_lists(lhs, rhs) )
3354 {
3355 ae_Operator op = f->op2overload;
3356 // check if this is an overloading of both post and pre (e.g., -- and ++)
3357 if( op_overload && env->op_registry.unaryPreOverloadable(op) &&
3358 env->op_registry.unaryPostOverloadable(op) )
3359 {
3360 // check what kind of unary overload
3361 if( (f->overload_post && env->op_registry.lookup_overload(lhs->type,op) == NULL )
3362 || (!f->overload_post && env->op_registry.lookup_overload(op,lhs->type) ) )
3363 {
3364 // next overfunc
3365 overfunc = overfunc->next;
3366 // keep going, this is ok
3367 continue;
3368 }
3369 }
3370
3371 EM_error2( f->where, "cannot overload %s with identical arguments...", isInCtor ? "constructor" : "function" );
3372 if( env->class_def )
3373 {
3374 EM_error3( " |- '%s %s.%s(%s)' already defined elsewhere",
3375 func->def()->ret_type->base_name.c_str(), env->class_def->c_name(),
3376 orig_name.c_str(), arglist2string(func->def()->arg_list).c_str() );
3377 // if a constructor definition
3378 if( isInCtor )
3379 {
3380 EM_error3( " |- (hint: possibly as '@construct(%s)' in class '%s')",
3381 arglist2string(func->def()->arg_list).c_str(), env->class_def->c_name() );
3382 }
3383 }
3384 else
3385 {
3386 EM_error3( " |- '%s %s(%s)' already defined elsewhere",
3387 func->def()->ret_type->base_name.c_str(), orig_name.c_str(),
3388 arglist2string(func->def()->arg_list).c_str() );
3389 }
3390 goto error;
3391 }
3392 }
3393 // next overloaded function
3394 overfunc = overfunc->next;
3395 }
3396 }
3397
3398 // constructor | 1.5.2.0 (ge) added
3399 if( isInCtor )
3400 {
3401 // set constructor flag
3402 func->is_ctor = TRUE;
3403 // set in class as constructor; if not NULL, then a ctor is already set, and this is an overloading
3404 if( env->class_def->ctors_all == NULL )
3405 {
3406 CK_SAFE_REF_ASSIGN( env->class_def->ctors_all, func );
3407 }
3408 // check if default constructor
3409 func->is_default_ctor = (f->arg_list == NULL);
3410 // if no arguments, then set as base constructor
3411 if( func->is_default_ctor )
3412 {
3413 CK_SAFE_REF_ASSIGN( env->class_def->ctor_default, func );
3414 }
3415 }
3416
3417 // destructor | 1.5.2.0 (ge) added
3418 if( isInDtor )
3419 {
3420 // verify
3421 if( env->class_def->dtor_the != NULL )
3422 {
3423 EM_error2( f->where, "(internal error): unexpected destructor found in '%s'...", env->class_def->c_name() );
3424 return FALSE;
3425 }
3426 // set destructor flag
3427 func->is_dtor = TRUE;
3428 // set in class as destructor
3429 CK_SAFE_REF_ASSIGN( env->class_def->dtor_the, func );
3430 // set invoker
3431 env->class_def->dtor_invoker = new Chuck_VM_DtorInvoker;
3432 // init invoker
3433 env->class_def->dtor_invoker->setup( func, env->vm() );
3434 }
3435
3436 // operator overload | 1.5.1.5 (ge) added
3437 if( op_overload )
3438 {
3439 // scan operator overload
3440 if( !type_engine_scan_func_op_overload( env, f ) )
3441 return FALSE;
3442 }
3443
3444 // add as value
3445 env->curr->add_value( value->name, value );
3446 // enter the name into the function table
3447 env->curr->add_func( func->name, func );
3448
3449 // if not overload, add entries for orig name
3450 if( !overload )
3451 {
3452 env->curr->add_value( orig_name, value );
3453 env->curr->add_func( orig_name, func );
3454 }
3455
3456 // set the current function to this
3457 env->func = func;
3458 // push the value stack
3459 env->curr->value.push();
3460
3461 // scan the code
3462 assert( f->code == NULL || f->code->s_type == ae_stmt_code );
3463 if( f->code && !type_engine_scan2_code_segment( env, &f->code->stmt_code, FALSE ) )
3464 {
3465 EM_error2( 0, "...in function '%s'", func->signature(FALSE,FALSE).c_str() ); // S_name(f->name) );
3466 goto error;
3467 }
3468
3469 // update the function-specific type's name | 1.5.4.3 (ge) added
3470 type->base_name = "[fun]" + func->signature(FALSE,FALSE);
3471
3472 // pop the value stack
3473 env->curr->value.pop();
3474 // clear the env's function definition
3475 env->func = NULL;
3476
3477 return TRUE;
3478
3479 error:
3480
3481 // clean up
3482 if( func )
3483 {
3484 // reset
3485 env->func = NULL;
3486 // break func_ref <-> value_ref cycle, at least here
3487 CK_SAFE_RELEASE( func->value_ref );
3488 // value, type, func shouldn't be released here | 1.5.1.3
3489 // should be cleanup with the overall context
3490 // CK_SAFE_RELEASE(value); CK_SAFE_RELEASE(type); CK_SAFE_RELEASE(func);
3491 }
3492
3493 return FALSE;
3494 }