2 * Copyright 1993, 2000 Christopher Seiwald.
4 * This file is part of Jam - see jam.c for Copyright information.
8 * Copyright 2001-2004 David Abrahams.
9 * Distributed under the Boost Software License, Version 1.0.
10 * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
18 # include "variable.h"
24 # include "hdrmacro.h"
28 # include "builtins.h"
36 * compile.c - compile parsed jam statements
40 * compile_append() - append list results of two statements
41 * compile_eval() - evaluate if to determine which leg to compile
42 * compile_foreach() - compile the "for x in y" statement
43 * compile_if() - compile 'if' rule
44 * compile_while() - compile 'while' rule
45 * compile_include() - support for 'include' - call include() on file
46 * compile_list() - expand and return a list
47 * compile_local() - declare (and set) local variables
48 * compile_null() - do nothing -- a stub for parsing
49 * compile_on() - run rule under influence of on-target variables
50 * compile_rule() - compile a single user defined rule
51 * compile_rules() - compile a chain of rules
52 * compile_set() - compile the "set variable" statement
53 * compile_setcomp() - support for `rule` - save parse tree
54 * compile_setexec() - support for `actions` - save execution string
55 * compile_settings() - compile the "on =" (set variable on exec) statement
56 * compile_switch() - compile 'switch' rule
60 * debug_compile() - printf with indent to show rule expansion.
61 * evaluate_rule() - execute a rule invocation
63 * builtin_depends() - DEPENDS/INCLUDES rule
64 * builtin_echo() - ECHO rule
65 * builtin_exit() - EXIT rule
66 * builtin_flags() - NOCARE, NOTFILE, TEMPORARY rule
68 * 02/03/94 (seiwald) - Changed trace output to read "setting" instead of
69 * the awkward sounding "settings".
70 * 04/12/94 (seiwald) - Combined build_depends() with build_includes().
71 * 04/12/94 (seiwald) - actionlist() now just appends a single action.
72 * 04/13/94 (seiwald) - added shorthand L0 for null list pointer
73 * 05/13/94 (seiwald) - include files are now bound as targets, and thus
74 * can make use of $(SEARCH)
75 * 06/01/94 (seiwald) - new 'actions existing' does existing sources
76 * 08/23/94 (seiwald) - Support for '+=' (append to variable)
77 * 12/20/94 (seiwald) - NOTIME renamed NOTFILE.
78 * 01/22/95 (seiwald) - Exit rule.
79 * 02/02/95 (seiwald) - Always rule; LEAVES rule.
80 * 02/14/95 (seiwald) - NoUpdate rule.
81 * 09/11/00 (seiwald) - new evaluate_rule() for headers().
82 * 09/11/00 (seiwald) - compile_xxx() now return LIST *.
83 * New compile_append() and compile_list() in
84 * support of building lists here, rather than
86 * 01/10/00 (seiwald) - built-ins split out to builtin.c.
89 static void debug_compile( int which, char *s, FRAME* frame );
90 int glob( char *s, char *c );
91 /* Internal functions from builtins.c */
92 void backtrace( FRAME *frame );
93 void backtrace_line( FRAME *frame );
94 void print_source_line( PARSE* p );
96 struct frame * frame_before_python_call;
98 void frame_init( FRAME* frame )
101 frame->prev_user = 0;
102 lol_init(frame->args);
103 frame->module = root_module();
104 frame->rulename = "module scope";
105 frame->procedure = 0;
109 void frame_free( FRAME* frame )
111 lol_free( frame->args );
116 * compile_append() - append list results of two statements
118 * parse->left more compile_append() by left-recursion
119 * parse->right single rule
122 LIST * compile_append( PARSE * parse, FRAME * frame )
124 /* Append right to left. */
126 parse_evaluate( parse->left, frame ),
127 parse_evaluate( parse->right, frame ) );
132 * compile_eval() - evaluate if to determine which leg to compile
135 * list if expression true - compile 'then' clause
136 * L0 if expression false - compile 'else' clause
139 static int lcmp( LIST * t, LIST * s )
143 while ( !status && ( t || s ) )
145 char *st = t ? t->string : "";
146 char *ss = s ? s->string : "";
148 status = strcmp( st, ss );
150 t = t ? list_next( t ) : t;
151 s = s ? list_next( s ) : s;
157 LIST * compile_eval( PARSE * parse, FRAME * frame )
165 /* Short circuit lr eval for &&, ||, and 'in'. */
167 ll = parse_evaluate( parse->left, frame );
170 switch ( parse->num )
173 case EXPR_IN : if ( ll ) goto eval; break;
174 case EXPR_OR : if ( !ll ) goto eval; break;
175 default: eval: lr = parse_evaluate( parse->right, frame );
179 switch ( parse->num )
181 case EXPR_NOT: if ( !ll ) status = 1; break;
182 case EXPR_AND: if ( ll && lr ) status = 1; break;
183 case EXPR_OR : if ( ll || lr ) status = 1; break;
186 /* "a in b": make sure each of ll is equal to something in lr. */
187 for ( t = ll; t; t = list_next( t ) )
189 for ( s = lr; s; s = list_next( s ) )
190 if ( !strcmp( t->string, s->string ) )
194 /* No more ll? Success. */
195 if ( !t ) status = 1;
198 case EXPR_EXISTS: if ( lcmp( ll, L0 ) != 0 ) status = 1; break;
199 case EXPR_EQUALS: if ( lcmp( ll, lr ) == 0 ) status = 1; break;
200 case EXPR_NOTEQ : if ( lcmp( ll, lr ) != 0 ) status = 1; break;
201 case EXPR_LESS : if ( lcmp( ll, lr ) < 0 ) status = 1; break;
202 case EXPR_LESSEQ: if ( lcmp( ll, lr ) <= 0 ) status = 1; break;
203 case EXPR_MORE : if ( lcmp( ll, lr ) > 0 ) status = 1; break;
204 case EXPR_MOREEQ: if ( lcmp( ll, lr ) >= 0 ) status = 1; break;
209 debug_compile( 0, "if", frame );
211 printf( "(%d) ", status );
216 /* Find something to return. */
217 /* In odd circumstances (like "" = "") */
218 /* we'll have to return a new string. */
220 if ( !status ) t = 0;
221 else if ( ll ) t = ll, ll = 0;
222 else if ( lr ) t = lr, lr = 0;
223 else t = list_new( L0, newstr( "1" ) );
225 if ( ll ) list_free( ll );
226 if ( lr ) list_free( lr );
232 * compile_foreach() - compile the "for x in y" statement
234 * Compile_foreach() resets the given variable name to each specified
235 * value, executing the commands enclosed in braces for each iteration.
237 * parse->string index variable
238 * parse->left variable values
239 * parse->right rule to compile
242 LIST * compile_foreach( PARSE * parse, FRAME * frame )
244 LIST * nv = parse_evaluate( parse->left, frame );
250 s = addsettings( s, VAR_SET, parse->string, L0 );
254 /* Call var_set to reset $(parse->string) for each val. */
256 for ( l = nv; l; l = list_next( l ) )
258 LIST * val = list_new( L0, copystr( l->string ) );
259 var_set( parse->string, val, VAR_SET );
260 list_free( parse_evaluate( parse->right, frame ) );
275 * compile_if() - compile 'if' rule
277 * parse->left condition tree
278 * parse->right then tree
279 * parse->third else tree
282 LIST * compile_if( PARSE * p, FRAME * frame )
284 LIST * l = parse_evaluate( p->left, frame );
288 return parse_evaluate( p->right, frame );
290 return parse_evaluate( p->third, frame );
294 LIST * compile_while( PARSE * p, FRAME * frame )
298 while ( ( l = parse_evaluate( p->left, frame ) ) )
301 if ( r ) list_free( r );
302 r = parse_evaluate( p->right, frame );
309 * compile_include() - support for 'include' - call include() on file
311 * parse->left list of files to include (can only do 1)
314 LIST * compile_include( PARSE * parse, FRAME * frame )
316 LIST * nt = parse_evaluate( parse->left, frame );
320 debug_compile( 0, "include", frame);
327 TARGET * t = bindtarget( nt->string );
329 /* DWA 2001/10/22 - Perforce Jam cleared the arguments here, which
330 * prevents an included file from being treated as part of the body of a
331 * rule. I did not see any reason to do that, so I lifted the
335 /* Bind the include file under the influence of */
336 /* "on-target" variables. Though they are targets, */
337 /* include files are not built with make(). */
339 pushsettings( t->settings );
340 /* We don't expect that file to be included is generated by some
341 action. Therefore, pass 0 as third argument.
342 If the name resolves to directory, let it error out. */
343 t->boundname = search( t->name, &t->time, 0, 0 );
344 popsettings( t->settings );
346 parse_file( t->boundname, frame );
354 static LIST* evaluate_in_module ( char* module_name, PARSE * p, FRAME* frame)
358 module_t* outer_module = frame->module;
359 frame->module = module_name ? bindmodule( module_name ) : root_module();
361 if ( outer_module != frame->module )
363 exit_module( outer_module );
364 enter_module( frame->module );
367 result = parse_evaluate( p, frame );
369 if ( outer_module != frame->module )
371 exit_module( frame->module );
372 enter_module( outer_module );
373 frame->module = outer_module;
380 LIST * compile_module( PARSE * p, FRAME * frame )
382 /* Here we are entering a module declaration block. */
383 LIST * module_name = parse_evaluate( p->left, frame );
384 LIST * result = evaluate_in_module( module_name ? module_name->string : 0,
386 list_free( module_name );
391 LIST * compile_class( PARSE * p, FRAME * frame )
393 /** Todo: check for empty class name.
394 Check for class redeclaration. */
396 char * class_module = 0;
398 LIST * name = parse_evaluate( p->left->right, frame );
402 bases = parse_evaluate( p->left->left->right, frame );
404 class_module = make_class_module( name, bases, frame );
405 evaluate_in_module( class_module, p->right, frame );
412 * compile_list() - expand and return a list.
414 * parse->string - character string to expand.
417 LIST * compile_list( PARSE * parse, FRAME * frame )
419 /* s is a copyable string */
420 char * s = parse->string;
421 return var_expand( L0, s, s + strlen( s ), frame->args, 1 );
426 * compile_local() - declare (and set) local variables.
428 * parse->left list of variables
429 * parse->right list of values
430 * parse->third rules to execute
433 LIST * compile_local( PARSE * parse, FRAME * frame )
437 LIST * nt = parse_evaluate( parse->left, frame );
438 LIST * ns = parse_evaluate( parse->right, frame );
443 debug_compile( 0, "local", frame );
450 /* Initial value is ns. */
451 for ( l = nt; l; l = list_next( l ) )
452 s = addsettings( s, VAR_SET, l->string, list_copy( (LIST *)0, ns ) );
457 /* Note that callees of the current context get this "local" variable,
458 * making it not so much local as layered.
462 result = parse_evaluate( parse->third, frame );
472 * compile_null() - do nothing -- a stub for parsing.
475 LIST * compile_null( PARSE * parse, FRAME * frame )
482 * compile_on() - run rule under influence of on-target variables
484 * parse->left list of files to include (can only do 1).
485 * parse->right rule to run.
490 LIST * compile_on( PARSE * parse, FRAME * frame )
492 LIST * nt = parse_evaluate( parse->left, frame );
497 debug_compile( 0, "on", frame );
504 TARGET * t = bindtarget( nt->string );
505 pushsettings( t->settings );
506 result = parse_evaluate( parse->right, frame );
507 popsettings( t->settings );
517 * compile_rule() - compile a single user defined rule.
519 * parse->string name of user defined rule.
520 * parse->left parameters (list of lists) to rule, recursing left.
522 * Wrapped around evaluate_rule() so that headers() can share it.
525 LIST * compile_rule( PARSE * parse, FRAME * frame )
531 /* Build up the list of arg lists. */
534 inner->prev_user = frame->module->user_module ? frame : frame->prev_user;
535 inner->module = frame->module; /* This gets fixed up in evaluate_rule(), below. */
536 inner->procedure = parse;
537 /* Special-case LOL of length 1 where the first list is totally empty.
538 This is created when calling functions with no parameters, due to
539 the way jam grammar is written. This is OK when one jam function
540 calls another, but really not good when Jam function calls Python. */
541 if ( parse->left->left == NULL && parse->left->right->func == compile_null)
544 for ( p = parse->left; p; p = p->left )
545 lol_add( inner->args, parse_evaluate( p->right, frame ) );
547 /* And invoke the rule. */
548 result = evaluate_rule( parse->string, inner );
554 static void argument_error( char * message, RULE * rule, FRAME * frame, LIST* arg )
556 LOL * actual = frame->args;
557 assert( frame->procedure != 0 );
558 backtrace_line( frame->prev );
559 printf( "*** argument error\n* rule %s ( ", frame->rulename );
560 lol_print( rule->arguments->data );
561 printf( " )\n* called with: ( " );
563 printf( " )\n* %s %s\n", message, arg ? arg->string : "" );
564 print_source_line( rule->procedure );
565 printf( "see definition of rule '%s' being called\n", rule->name );
566 backtrace( frame->prev );
571 /* Define delimiters for type check elements in argument lists (and return type
572 * specifications, eventually).
574 # define TYPE_OPEN_DELIM '['
575 # define TYPE_CLOSE_DELIM ']'
578 * is_type_name() - true iff the given string represents a type check
582 static int is_type_name( char * s )
584 return ( s[ 0 ] == TYPE_OPEN_DELIM ) &&
585 ( s[ strlen( s ) - 1 ] == TYPE_CLOSE_DELIM );
590 * arg_modifier - if the next element of formal is a single character, return
591 * that; return 0 otherwise. Used to extract "*+?" modifiers * from argument
595 static char arg_modifier( LIST * formal )
599 char * next = formal->next->string;
600 if ( next && ( next[ 0 ] != 0 ) && ( next[ 1 ] == 0 ) )
608 * type_check() - checks that each element of values satisfies the requirements
611 * caller - the frame of the rule calling the rule whose arguments are
614 * called - the rule being called
616 * arg_name - a list element containing the name of the argument being
620 static void type_check
629 static module_t * typecheck = 0;
631 /* If nothing to check, bail now. */
632 if ( !values || !type_name )
636 typecheck = bindmodule( ".typecheck" );
638 /* If the checking rule can not be found, also bail. */
640 RULE checker_, *checker = &checker_;
642 checker->name = type_name;
643 if ( !typecheck->rules || !hashcheck( typecheck->rules, (HASHDATA * *)&checker ) )
647 exit_module( caller->module );
649 while ( values != 0 )
654 frame->module = typecheck;
655 frame->prev = caller;
656 frame->prev_user = caller->module->user_module ? caller : caller->prev_user;
658 enter_module( typecheck );
659 /* Prepare the argument list */
660 lol_add( frame->args, list_new( L0, values->string ) );
661 error = evaluate_rule( type_name, frame );
663 exit_module( typecheck );
666 argument_error( error->string, called, caller, arg_name );
669 values = values->next;
672 enter_module( caller->module );
676 * collect_arguments() - local argument checking and collection
679 collect_arguments( RULE* rule, FRAME* frame )
681 SETTINGS *locals = 0;
683 LOL * all_actual = frame->args;
684 LOL * all_formal = rule->arguments ? rule->arguments->data : 0;
685 if ( all_formal ) /* Nothing to set; nothing to check */
687 int max = all_formal->count > all_actual->count
692 for ( n = 0; n < max ; ++n )
694 LIST *actual = lol_get( all_actual, n );
698 for ( formal = lol_get( all_formal, n ); formal; formal = formal->next )
700 char* name = formal->string;
702 if ( is_type_name(name) )
705 argument_error( "missing argument name before type name:", rule, frame, formal );
708 argument_error( "missing argument name after type name:", rule, frame, formal );
710 type_name = formal->string;
716 LIST* arg_name = formal; /* hold the argument name for type checking */
719 /* Stop now if a variable number of arguments are specified */
720 if ( name[0] == '*' && name[1] == 0 )
723 modifier = arg_modifier( formal );
725 if ( !actual && modifier != '?' && modifier != '*' )
726 argument_error( "missing argument", rule, frame, formal );
732 value = list_copy( 0, actual );
735 /* skip an extra element for the modifier */
736 formal = formal->next;
739 /* skip an extra element for the modifier */
740 formal = formal->next;
743 if ( actual ) /* in case actual is missing */
745 value = list_new( 0, actual->string );
746 actual = actual->next;
750 locals = addsettings(locals, VAR_SET, name, value);
751 locals->multiple = multiple;
752 type_check( type_name, value, frame, rule, arg_name );
759 argument_error( "extra argument", rule, frame, actual );
767 enter_rule( char *rulename, module_t *target_module );
771 static int python_instance_number = 0;
774 /* Given a Python object, return a string to use in Jam
775 code instead of said object.
776 If the object is string, use the string value
777 If the object implemenets __jam_repr__ method, use that.
780 The result value is newstr-ed. */
781 char *python_to_string(PyObject* value)
783 if (PyString_Check(value))
785 return newstr(PyString_AsString(value));
789 /* See if this is an instance that defines special __jam_repr__
791 if (PyInstance_Check(value)
792 && PyObject_HasAttrString(value, "__jam_repr__"))
794 PyObject* repr = PyObject_GetAttrString(value, "__jam_repr__");
797 PyObject* arguments2 = PyTuple_New(0);
798 PyObject* value2 = PyObject_Call(repr, arguments2, 0);
800 Py_DECREF(arguments2);
801 if (PyString_Check(value2))
803 return newstr(PyString_AsString(value2));
813 call_python_function(RULE* r, FRAME* frame)
816 PyObject * arguments = 0;
817 PyObject * kw = NULL;
819 PyObject * py_result;
825 arguments = PyTuple_New(0);
828 for (args = collect_arguments(r, frame); args; args = args->next)
830 PyObject *key = PyString_FromString(args->symbol);
833 value = list_to_python(args->value);
836 value = PyString_FromString(args->value->string);
840 PyDict_SetItem(kw, key, value);
847 arguments = PyTuple_New( frame->args->count );
848 for ( i = 0; i < frame->args->count; ++i )
850 PyObject * arg = PyList_New(0);
851 LIST* l = lol_get( frame->args, i);
853 for ( ; l; l = l->next )
855 PyObject * v = PyString_FromString(l->string);
856 PyList_Append( arg, v );
859 /* Steals reference to 'arg' */
860 PyTuple_SetItem( arguments, i, arg );
864 frame_before_python_call = frame;
865 py_result = PyObject_Call( r->python_function, arguments, kw );
866 Py_DECREF(arguments);
868 if ( py_result != NULL )
870 if ( PyList_Check( py_result ) )
872 int size = PyList_Size( py_result );
874 for ( i = 0; i < size; ++i )
876 PyObject * item = PyList_GetItem( py_result, i );
877 char *s = python_to_string (item);
879 fprintf( stderr, "Non-string object returned by Python call.\n" );
881 result = list_new (result, s);
885 else if ( py_result == Py_None )
891 char *s = python_to_string(py_result);
893 result = list_new(0, s);
895 /* We have tried all we could. Return empty list. There are
896 cases, e.g. feature.feature function that should return
897 value for the benefit of Python code and which also can be
898 called by Jam code, where no sensible value can be
899 returned. We cannot even emit a warning, since there will
900 be a pile of them. */
904 Py_DECREF( py_result );
909 fprintf(stderr,"Call failed\n");
916 module_t * python_module()
918 static module_t * python = 0;
920 python = bindmodule("__python__");
928 * evaluate_rule() - execute a rule invocation.
938 profile_frame prof[1];
939 module_t * prev_module = frame->module;
943 LOL arg_context_, * arg_context = &arg_context_;
945 lol_init(arg_context);
947 arg_context = frame->prev->args;
948 l = var_expand( L0, rulename, rulename+strlen(rulename), arg_context, 0 );
953 backtrace_line( frame->prev );
954 printf( "warning: rulename %s expands to empty string\n", rulename );
955 backtrace( frame->prev );
959 rulename = l->string;
960 rule = bindrule( l->string, frame->module );
963 if ( rule->python_function )
965 /* The below messing with modules is due to the way modules are
966 * implemented in Jam. Suppose we are in module M1 now. The global
967 * variable map actually holds 'M1' variables, and M1->variables hold
970 * If we call Python right away, Python calls back Jam and then Jam
971 * does 'module M1 { }' then Jam will try to swap the current global
972 * variables with M1->variables. The result will be that global
973 * variables map will hold global variables, and any variable settings
974 * we do will go to the global module, not M1.
976 * By restoring basic state, where the global variable map holds global
977 * variable, we make sure any future 'module M1' entry will work OK.
981 module_t * m = python_module();
985 exit_module( prev_module );
988 result = call_python_function( rule, frame );
991 enter_module ( prev_module );
997 /* Drop the rule name. */
998 l = list_pop_front( l );
1000 /* Tack the rest of the expansion onto the front of the first argument. */
1001 frame->args->list[0] = list_append( l, lol_get( frame->args, 0 ) );
1003 if ( DEBUG_COMPILE )
1005 /* Try hard to indicate in which module the rule is going to execute. */
1006 if ( rule->module != frame->module
1007 && rule->procedure != 0 && strcmp( rulename, rule->procedure->rulename ) )
1010 strncat( buf, rule->module->name, sizeof( buf ) - 1 );
1011 strncat( buf, rule->name, sizeof( buf ) - 1 );
1012 debug_compile( 1, buf, frame );
1016 debug_compile( 1, rulename, frame );
1019 lol_print( frame->args );
1023 if ( rule->procedure && rule->module != prev_module )
1025 /* Propagate current module to nested rule invocations. */
1026 frame->module = rule->module;
1028 /* Swap variables. */
1029 exit_module( prev_module );
1030 enter_module( rule->module );
1033 /* Record current rule name in frame. */
1034 if ( rule->procedure )
1036 frame->rulename = rulename;
1037 /* And enter record profile info. */
1038 if ( DEBUG_PROFILE )
1039 profile_enter( rule->procedure->rulename, prof );
1042 /* Check traditional targets $(<) and sources $(>). */
1043 if ( !rule->actions && !rule->procedure )
1045 backtrace_line( frame->prev );
1046 printf( "rule %s unknown in module %s\n", rule->name, frame->module->name );
1047 backtrace( frame->prev );
1051 /* If this rule will be executed for updating the targets then construct the
1052 * action for make().
1054 if ( rule->actions )
1059 /* The action is associated with this instance of this rule. */
1060 action = (ACTION *)BJAM_MALLOC( sizeof( ACTION ) );
1061 memset( (char *)action, '\0', sizeof( *action ) );
1063 action->rule = rule;
1064 action->targets = targetlist( (TARGETS *)0, lol_get( frame->args, 0 ) );
1065 action->sources = targetlist( (TARGETS *)0, lol_get( frame->args, 1 ) );
1067 /* If we have a group of targets all being built using the same action
1068 * then we must not allow any of them to be used as sources unless they
1069 * had all already been built in the first place or their joined action
1070 * has had a chance to finish its work and build all of them anew.
1072 * Without this it might be possible, in case of a multi-process build,
1073 * for their action, triggered by buiding one of the targets, to still
1074 * be running when another target in the group reports as done in order
1075 * to avoid triggering the same action again and gets used prematurely.
1077 * As a quick-fix to achieve this effect we make all the targets list
1078 * each other as 'included targets'. More precisely, we mark the first
1079 * listed target as including all the other targets in the list and vice
1080 * versa. This makes anyone depending on any of those targets implicitly
1081 * depend on all of them, thus making sure none of those targets can be
1082 * used as sources until all of them have been built. Note that direct
1083 * dependencies could not have been used due to the 'circular
1084 * dependency' issue.
1086 * TODO: Although the current implementation solves the problem of one
1087 * of the targets getting used before its action completes its work it
1088 * also forces the action to run whenever any of the targets in the
1089 * group is not up to date even though some of them might not actually
1090 * be used by the targets being built. We should see how we can
1091 * correctly recognize such cases and use that to avoid running the
1092 * action if possible and not rebuild targets not actually depending on
1093 * targets that are not up to date.
1095 * TODO: Using the 'include' feature might have side-effects due to
1096 * interaction with the actual 'inclusion scanning' system. This should
1099 if ( action->targets )
1101 TARGET * t0 = action->targets->target;
1102 for ( t = action->targets->next; t; t = t->next )
1104 target_include( t->target, t0 );
1105 target_include( t0, t->target );
1109 /* Append this action to the actions of each target. */
1110 for ( t = action->targets; t; t = t->next )
1111 t->target->actions = actionlist( t->target->actions, action );
1114 /* Now recursively compile any parse tree associated with this rule.
1115 * parse_refer()/parse_free() call pair added to ensure rule not freed
1118 if ( rule->procedure )
1120 SETTINGS * local_args = collect_arguments( rule, frame );
1121 PARSE * parse = rule->procedure;
1122 parse_refer( parse );
1124 pushsettings( local_args );
1125 result = parse_evaluate( parse, frame );
1126 popsettings( local_args );
1127 freesettings( local_args );
1129 parse_free( parse );
1132 if ( frame->module != prev_module )
1134 exit_module( frame->module );
1135 enter_module( prev_module );
1138 if ( DEBUG_PROFILE && rule->procedure )
1139 profile_exit( prof );
1141 if ( DEBUG_COMPILE )
1142 debug_compile( -1, 0, frame);
1149 * Call the given rule with the specified parameters. The parameters should be
1150 * of type LIST* and end with a NULL pointer. This differs from 'evaluate_rule'
1151 * in that frame for the called rule is prepared inside 'call_rule'.
1153 * This function is useful when a builtin rule (in C) wants to call another rule
1154 * which might be implemented in Jam.
1157 LIST * call_rule( char * rulename, FRAME * caller_frame, ... )
1163 frame_init( inner );
1164 inner->prev = caller_frame;
1165 inner->prev_user = caller_frame->module->user_module ?
1166 caller_frame : caller_frame->prev_user;
1167 inner->module = caller_frame->module;
1168 inner->procedure = 0;
1170 va_start( va, caller_frame );
1173 LIST * l = va_arg( va, LIST* );
1176 lol_add( inner->args, l );
1180 result = evaluate_rule( rulename, inner );
1182 frame_free( inner );
1189 * compile_rules() - compile a chain of rules
1191 * parse->left single rule
1192 * parse->right more compile_rules() by right-recursion
1195 LIST * compile_rules( PARSE * parse, FRAME * frame )
1197 /* Ignore result from first statement; return the 2nd. */
1198 /* Optimize recursion on the right by looping. */
1199 do list_free( parse_evaluate( parse->left, frame ) );
1200 while ( ( parse = parse->right )->func == compile_rules );
1201 return parse_evaluate( parse, frame );
1206 * assign_var_mode() - convert ASSIGN_XXX compilation flag into corresponding
1207 * VAR_XXX variable set flag.
1210 static int assign_var_mode( int parsenum, char const * * tracetext )
1216 case ASSIGN_SET : setflag = VAR_SET ; trace = "=" ; break;
1217 case ASSIGN_APPEND : setflag = VAR_APPEND ; trace = "+="; break;
1218 case ASSIGN_DEFAULT: setflag = VAR_DEFAULT; trace = "?="; break;
1219 default: setflag = VAR_SET ; trace = "" ; break;
1222 *tracetext = trace ;
1227 * compile_set() - compile the "set variable" statement
1229 * parse->left variable names
1230 * parse->right variable values
1231 * parse->num ASSIGN_SET/APPEND/DEFAULT
1234 LIST * compile_set( PARSE * parse, FRAME * frame )
1236 LIST * nt = parse_evaluate( parse->left, frame );
1237 LIST * ns = parse_evaluate( parse->right, frame );
1240 int setflag = assign_var_mode( parse->num, &trace );
1242 if ( DEBUG_COMPILE )
1244 debug_compile( 0, "set", frame );
1246 printf( " %s ", trace );
1251 /* Call var_set to set variable. var_set keeps ns, so need to copy it. */
1252 for ( l = nt; l; l = list_next( l ) )
1253 var_set( l->string, list_copy( L0, ns ), setflag );
1260 * compile_setcomp() - support for `rule` - save parse tree.
1262 * parse->string rule name
1263 * parse->left rules for rule
1264 * parse->right optional list-of-lists describing arguments
1267 LIST * compile_setcomp( PARSE * parse, FRAME * frame )
1269 argument_list * arg_list = 0;
1271 /* Create new LOL describing argument requirements if supplied. */
1275 arg_list = args_new();
1276 for ( p = parse->right; p; p = p->left )
1277 lol_add( arg_list->data, parse_evaluate( p->right, frame ) );
1280 new_rule_body( frame->module, parse->string, arg_list, parse->left, !parse->num );
1286 * compile_setexec() - support for `actions` - save execution string.
1288 * parse->string rule name
1289 * parse->string1 OS command string
1291 * parse->left `bind` variables
1293 * Note that the parse flags (as defined in compile.h) are transferred directly
1294 * to the rule flags (as defined in rules.h).
1297 LIST * compile_setexec( PARSE * parse, FRAME * frame )
1299 LIST * bindlist = parse_evaluate( parse->left, frame );
1300 new_rule_actions( frame->module, parse->string, parse->string1, bindlist, parse->num );
1306 * compile_settings() - compile the "on =" (set variable on exec) statement.
1308 * parse->left variable names
1309 * parse->right target name
1310 * parse->third variable value
1311 * parse->num ASSIGN_SET/APPEND
1314 LIST * compile_settings( PARSE * parse, FRAME * frame )
1316 LIST * nt = parse_evaluate( parse->left, frame );
1317 LIST * ns = parse_evaluate( parse->third, frame );
1318 LIST * targets = parse_evaluate( parse->right, frame );
1321 int setflag = assign_var_mode( parse->num, &trace );
1323 if ( DEBUG_COMPILE )
1325 debug_compile( 0, "set", frame );
1328 list_print( targets );
1329 printf( " %s ", trace );
1334 /* Call addsettings() to save variable setting. addsettings() keeps ns, so
1335 * need to copy it. Pass append flag to addsettings().
1337 for ( ts = targets; ts; ts = list_next( ts ) )
1339 TARGET * t = bindtarget( ts->string );
1342 for ( l = nt; l; l = list_next( l ) )
1343 t->settings = addsettings( t->settings, setflag, l->string,
1344 list_copy( (LIST *)0, ns ) );
1348 list_free( targets );
1354 * compile_switch() - compile 'switch' rule.
1356 * parse->left switch value (only 1st used)
1357 * parse->right cases
1359 * cases->left 1st case
1360 * cases->right next cases
1362 * case->string argument to match
1363 * case->left parse tree to execute
1366 LIST * compile_switch( PARSE * parse, FRAME * frame )
1368 LIST * nt = parse_evaluate( parse->left, frame );
1371 if ( DEBUG_COMPILE )
1373 debug_compile( 0, "switch", frame );
1378 /* Step through cases. */
1379 for ( parse = parse->right; parse; parse = parse->right )
1381 if ( !glob( parse->left->string, nt ? nt->string : "" ) )
1383 /* Get & exec parse tree for this case. */
1384 parse = parse->left->left;
1385 result = parse_evaluate( parse, frame );
1396 * debug_compile() - printf with indent to show rule expansion.
1399 static void debug_compile( int which, char * s, FRAME * frame )
1401 static int level = 0;
1402 static char indent[36] = ">>>>|>>>>|>>>>|>>>>|>>>>|>>>>|>>>>|";
1408 print_source_line( frame->procedure );
1410 i = ( level + 1 ) * 2;
1413 fputs( indent, stdout );
1417 printf( "%*.*s ", i, i, indent );