Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/tools/jam/src/compile.c @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 32.9 KB
Line 
1/*
2 * Copyright 1993, 2000 Christopher Seiwald.
3 *
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
6
7/*  This file is ALSO:
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)
11 */
12
13# include "jam.h"
14# include "debug.h"
15
16# include "lists.h"
17# include "parse.h"
18# include "compile.h"
19# include "variable.h"
20# include "expand.h"
21# include "rules.h"
22# include "newstr.h"
23# include "make.h"
24# include "search.h"
25# include "hdrmacro.h"
26# include "hash.h"
27# include "modules.h"
28# include "strings.h"
29# include "builtins.h"
30# include "class.h"
31
32# include <assert.h>
33# include <string.h>
34# include <stdarg.h>
35
36/*
37 * compile.c - compile parsed jam statements
38 *
39 * External routines:
40 *
41 *  compile_append() - append list results of two statements
42 *      compile_eval() - evaluate if to determine which leg to compile
43 *  compile_foreach() - compile the "for x in y" statement
44 *  compile_if() - compile 'if' rule
45 *  compile_while() - compile 'while' rule
46 *  compile_include() - support for 'include' - call include() on file
47 *  compile_list() - expand and return a list
48 *  compile_local() - declare (and set) local variables
49 *  compile_null() - do nothing -- a stub for parsing
50 *  compile_on() - run rule under influence of on-target variables
51 *  compile_rule() - compile a single user defined rule
52 *  compile_rules() - compile a chain of rules
53 *  compile_set() - compile the "set variable" statement
54 *  compile_setcomp() - support for `rule` - save parse tree
55 *  compile_setexec() - support for `actions` - save execution string
56 *  compile_settings() - compile the "on =" (set variable on exec) statement
57 *  compile_switch() - compile 'switch' rule
58 *
59 * Internal routines:
60 *
61 *  debug_compile() - printf with indent to show rule expansion.
62 *  evaluate_rule() - execute a rule invocation
63 *
64 *  builtin_depends() - DEPENDS/INCLUDES rule
65 *  builtin_echo() - ECHO rule
66 *  builtin_exit() - EXIT rule
67 *  builtin_flags() - NOCARE, NOTFILE, TEMPORARY rule
68 *
69 * 02/03/94 (seiwald) - Changed trace output to read "setting" instead of
70 *          the awkward sounding "settings".
71 * 04/12/94 (seiwald) - Combined build_depends() with build_includes().
72 * 04/12/94 (seiwald) - actionlist() now just appends a single action.
73 * 04/13/94 (seiwald) - added shorthand L0 for null list pointer
74 * 05/13/94 (seiwald) - include files are now bound as targets, and thus
75 *          can make use of $(SEARCH)
76 * 06/01/94 (seiwald) - new 'actions existing' does existing sources
77 * 08/23/94 (seiwald) - Support for '+=' (append to variable)
78 * 12/20/94 (seiwald) - NOTIME renamed NOTFILE.
79 * 01/22/95 (seiwald) - Exit rule.
80 * 02/02/95 (seiwald) - Always rule; LEAVES rule.
81 * 02/14/95 (seiwald) - NoUpdate rule.
82 * 09/11/00 (seiwald) - new evaluate_rule() for headers().
83 * 09/11/00 (seiwald) - compile_xxx() now return LIST *.
84 *          New compile_append() and compile_list() in
85 *          support of building lists here, rather than
86 *          in jamgram.yy.
87 * 01/10/00 (seiwald) - built-ins split out to builtin.c.
88 */
89
90static void debug_compile( int which, char *s, FRAME* frame );
91int glob( char *s, char *c );
92/* Internal functions from builtins.c */
93void backtrace( FRAME *frame );
94void backtrace_line( FRAME *frame );
95void print_source_line( PARSE* p );
96
97
98void frame_init( FRAME* frame )
99{
100    frame->prev = 0;
101    frame->prev_user = 0;
102    lol_init(frame->args);
103    frame->module = root_module();
104    frame->rulename = "module scope";
105    frame->procedure = 0;
106}
107
108void frame_free( FRAME* frame )
109{
110    lol_free( frame->args );
111}
112
113/*
114 * compile_append() - append list results of two statements
115 *
116 *  parse->left more compile_append() by left-recursion
117 *  parse->right    single rule
118 */
119
120LIST *
121compile_append(
122    PARSE   *parse,
123    FRAME *frame )
124{
125    /* Append right to left. */
126
127    return list_append( 
128        parse_evaluate( parse->left, frame ),
129        parse_evaluate( parse->right, frame ) );
130}
131
132/*
133 * compile_eval() - evaluate if to determine which leg to compile
134 *
135 * Returns:
136 *      list    if expression true - compile 'then' clause
137 *      L0      if expression false - compile 'else' clause
138 */
139
140static int
141lcmp( LIST *t, LIST *s )
142{
143        int status = 0;
144
145        while( !status && ( t || s ) )
146        {
147            char *st = t ? t->string : "";
148            char *ss = s ? s->string : "";
149
150            status = strcmp( st, ss );
151
152            t = t ? list_next( t ) : t;
153            s = s ? list_next( s ) : s;
154        }
155
156        return status;
157}
158
159LIST *
160compile_eval(
161        PARSE   *parse,
162        FRAME   *frame )
163{
164        LIST *ll, *lr, *s, *t;
165        int status = 0;
166
167        /* Short circuit lr eval for &&, ||, and 'in' */
168
169        ll = parse_evaluate( parse->left, frame );
170        lr = 0;
171
172        switch( parse->num )
173        {
174        case EXPR_AND: 
175        case EXPR_IN:   if( ll ) goto eval; break;
176        case EXPR_OR:   if( !ll ) goto eval; break;
177        default: eval:  lr = parse_evaluate( parse->right, frame );
178        }
179
180        /* Now eval */
181
182        switch( parse->num )
183        {
184        case EXPR_NOT: 
185                if( !ll ) status = 1;
186                break;
187
188        case EXPR_AND:
189                if( ll && lr ) status = 1;
190                break;
191
192        case EXPR_OR:
193                if( ll || lr ) status = 1;
194                break;
195
196        case EXPR_IN:
197                /* "a in b": make sure each of */
198                /* ll is equal to something in lr. */
199
200                for( t = ll; t; t = list_next( t ) )
201                {
202                    for( s = lr; s; s = list_next( s ) )
203                        if( !strcmp( t->string, s->string ) )
204                            break;
205                    if( !s ) break;
206                }
207
208                /* No more ll? Success */
209
210                if( !t ) status = 1;
211
212                break;
213
214        case EXPR_EXISTS:       if( lcmp( ll, L0 ) != 0 ) status = 1; break;
215        case EXPR_EQUALS:       if( lcmp( ll, lr ) == 0 ) status = 1; break;
216        case EXPR_NOTEQ:        if( lcmp( ll, lr ) != 0 ) status = 1; break;
217        case EXPR_LESS:         if( lcmp( ll, lr ) < 0  ) status = 1; break;
218        case EXPR_LESSEQ:       if( lcmp( ll, lr ) <= 0 ) status = 1; break;
219        case EXPR_MORE:         if( lcmp( ll, lr ) > 0  ) status = 1; break;
220        case EXPR_MOREEQ:       if( lcmp( ll, lr ) >= 0 ) status = 1; break;
221
222        }
223
224        if( DEBUG_IF )
225        {
226            debug_compile( 0, "if", frame );
227            list_print( ll );
228            printf( "(%d) ", status );
229            list_print( lr );
230            printf( "\n" );
231        }
232
233        /* Find something to return. */
234        /* In odd circumstances (like "" = "") */
235        /* we'll have to return a new string. */
236
237        if( !status ) t = 0;
238        else if( ll ) t = ll, ll = 0;
239        else if( lr ) t = lr, lr = 0;
240        else t = list_new( L0, newstr( "1" ) );
241
242        if( ll ) list_free( ll );
243        if( lr ) list_free( lr );
244        return t;
245}
246
247
248/*
249 * compile_foreach() - compile the "for x in y" statement
250 *
251 * Compile_foreach() resets the given variable name to each specified
252 * value, executing the commands enclosed in braces for each iteration.
253 *
254 *  parse->string   index variable
255 *  parse->left variable values
256 *  parse->right    rule to compile
257 */
258
259LIST *
260compile_foreach(
261    PARSE   *parse,
262    FRAME *frame )
263{
264    LIST    *nv = parse_evaluate( parse->left, frame );
265    LIST    *l;
266    SETTINGS *s = 0;
267       
268        if ( parse->num )
269        {
270            s = addsettings( s, 0, parse->string, L0 );
271            pushsettings( s );
272        }
273
274    /* Call var_set to reset $(parse->string) for each val. */
275
276    for( l = nv; l; l = list_next( l ) )
277    {
278        LIST *val = list_new( L0, copystr( l->string ) );
279
280        var_set( parse->string, val, VAR_SET );
281
282        list_free( parse_evaluate( parse->right, frame ) );
283    }
284
285        if ( parse->num )
286        {
287            popsettings( s );
288            freesettings( s );
289        }
290
291    list_free( nv );
292
293    return L0;
294}
295
296/*
297 * compile_if() - compile 'if' rule
298 *
299 *  parse->left     condition tree
300 *  parse->right        then tree
301 *  parse->third        else tree
302 */
303
304LIST *
305compile_if(
306    PARSE   *p,
307    FRAME *frame )
308{
309    LIST *l = parse_evaluate( p->left, frame );
310    if( l )
311    {
312        list_free( l );
313        return parse_evaluate( p->right, frame );
314    }
315    else
316    {
317        return parse_evaluate( p->third, frame );
318    }
319}
320
321LIST *
322compile_while(
323    PARSE   *p,
324    FRAME *frame )
325{
326    LIST *r = 0;
327    LIST *l;
328    while ( l = parse_evaluate( p->left, frame ) )
329    {
330        list_free( l );
331        if( r ) list_free( r );
332        r = parse_evaluate( p->right, frame );
333    }
334    return r;
335}
336
337
338/*
339 * compile_include() - support for 'include' - call include() on file
340 *
341 *  parse->left list of files to include (can only do 1)
342 */
343
344LIST *
345compile_include(
346    PARSE   *parse,
347    FRAME *frame )
348{
349    LIST    *nt = parse_evaluate( parse->left, frame );
350
351    if( DEBUG_COMPILE )
352    {
353        debug_compile( 0, "include", frame);
354        list_print( nt );
355        printf( "\n" );
356    }
357
358    if( nt )
359    {
360        TARGET *t = bindtarget( nt->string );
361
362            /* DWA 2001/10/22 - Perforce Jam clears the arguments here, which
363             * prevents an included file from being treated as part of the body
364             * of a rule. I didn't see any reason to do that, so I lifted the
365             * restriction.
366             */
367               
368        /* Bind the include file under the influence of */
369        /* "on-target" variables.  Though they are targets, */
370        /* include files are not built with make(). */
371
372        pushsettings( t->settings );
373        /* We don't expect that file to be included is generated by some
374           action. Therefore, pass 0 as third argument. */
375        t->boundname = search( t->name, &t->time, 0 );
376        popsettings( t->settings );
377
378        parse_file( t->boundname, frame );
379    }
380
381    list_free( nt );
382
383    return L0;
384}
385
386static LIST* evaluate_in_module ( char* module_name, PARSE * p, FRAME* frame)
387{
388    LIST* result;
389
390    module_t* outer_module = frame->module;
391    frame->module = module_name ? bindmodule( module_name ) : root_module();
392
393    if ( outer_module != frame->module )
394    {
395        exit_module( outer_module );
396        enter_module( frame->module );
397    }
398   
399    result = parse_evaluate( p, frame );
400   
401    if ( outer_module != frame->module )
402    {
403        exit_module( frame->module );
404        enter_module( outer_module );
405        frame->module = outer_module;
406    }
407
408    return result;
409}
410
411LIST *
412compile_module(
413    PARSE   *p,
414    FRAME *frame )
415{
416    /* Here we are entering a module declaration block.
417     */
418    LIST* module_name = parse_evaluate( p->left, frame );
419    LIST* result = evaluate_in_module( module_name ? module_name->string : 0, 
420                                       p->right, frame );
421   
422    list_free( module_name );
423    return result;
424}
425
426LIST *
427compile_class( 
428    PARSE *p, 
429    FRAME *frame )
430{
431    /** Todo: check for empty class name.
432        Check for class redeclaration. */
433
434    char* class_module = 0;
435
436    LIST* name = parse_evaluate( p->left->right, frame );
437    LIST* bases = 0;
438
439    if (p->left->left)
440        bases = parse_evaluate( p->left->left->right, frame );
441
442    class_module = make_class_module(name, bases, frame);   
443    evaluate_in_module( class_module, p->right, frame );
444
445    return L0;   
446}
447
448
449/*
450 * compile_list() - expand and return a list
451 *
452 *  parse->string - character string to expand
453 */
454
455LIST *
456compile_list(
457    PARSE   *parse,
458    FRAME *frame )
459{
460    /* voodoo 1 means: s is a copyable string */
461    char *s = parse->string;
462    return var_expand( L0, s, s + strlen( s ), frame->args, 1 );
463}
464
465/*
466 * compile_local() - declare (and set) local variables
467 *
468 *  parse->left list of variables
469 *  parse->right    list of values
470 *  parse->third    rules to execute
471 */
472
473LIST *
474compile_local(
475    PARSE   *parse,
476    FRAME *frame )
477{
478    LIST *l;
479    SETTINGS *s = 0;
480    LIST    *nt = parse_evaluate( parse->left, frame );
481    LIST    *ns = parse_evaluate( parse->right, frame );
482    LIST    *result;
483
484    if( DEBUG_COMPILE )
485    {
486        debug_compile( 0, "local", frame);
487        list_print( nt );
488        printf( " = " );
489        list_print( ns );
490        printf( "\n" );
491    }
492
493    /* Initial value is ns */
494
495    for( l = nt; l; l = list_next( l ) )
496        s = addsettings( s, 0, l->string, list_copy( (LIST*)0, ns ) );
497
498    list_free( ns );
499    list_free( nt );
500
501    /* Note that callees of the current context get this "local" */
502    /* variable, making it not so much local as layered. */
503
504    pushsettings( s );
505    result = parse_evaluate( parse->third, frame );
506    popsettings( s );
507
508    freesettings( s );
509
510    return result;
511}
512
513/*
514 * compile_null() - do nothing -- a stub for parsing
515 */
516
517LIST *
518compile_null(
519    PARSE   *parse,
520    FRAME *frame )
521{
522    return L0;
523}
524
525/*
526 * compile_on() - run rule under influence of on-target variables
527 *
528 *      parse->left     list of files to include (can only do 1)
529 *      parse->right    rule to run
530 *
531 * EXPERIMENTAL!
532 */
533
534LIST *
535compile_on(
536        PARSE   *parse,
537        FRAME   *frame )
538{
539        LIST    *nt = parse_evaluate( parse->left, frame );
540        LIST    *result = 0;
541
542        if( DEBUG_COMPILE )
543        {
544            debug_compile( 0, "on", frame );
545            list_print( nt );
546            printf( "\n" );
547        }
548
549        if( nt )
550        {
551            TARGET *t = bindtarget( nt->string );
552            pushsettings( t->settings );
553
554            result = parse_evaluate( parse->right, frame );
555
556            popsettings( t->settings );
557        }
558
559        list_free( nt );
560
561        return result;
562}
563
564
565/*
566 * compile_rule() - compile a single user defined rule
567 *
568 *  parse->string   name of user defined rule
569 *  parse->left parameters (list of lists) to rule, recursing left
570 *
571 * Wrapped around evaluate_rule() so that headers() can share it.
572 */
573
574LIST *
575compile_rule(
576    PARSE   *parse,
577    FRAME *frame )
578{
579    FRAME       inner[1];
580    LIST    *result;
581    PARSE   *p;
582   
583
584    /* Build up the list of arg lists */
585
586    frame_init( inner );
587    inner->prev = frame;
588    inner->prev_user = frame->module->user_module ? frame : frame->prev_user;
589    inner->module = frame->module; /* This gets fixed up in evaluate_rule(), below */
590    inner->procedure = parse;
591
592    for( p = parse->left; p; p = p->left )
593        lol_add( inner->args, parse_evaluate( p->right, frame ) );
594
595    /* And invoke rule */
596
597    result = evaluate_rule( parse->string, inner );
598
599    frame_free( inner );
600
601    return result;
602}
603
604static void argument_error( char* message, RULE* rule, FRAME* frame, LIST* arg )
605{
606    LOL* actual = frame->args;
607    assert( frame->procedure != 0 );
608    backtrace_line( frame->prev );
609    printf( "*** argument error\n* rule %s ( ", frame->rulename );
610    lol_print( rule->arguments->data );
611    printf( " )\n* called with: ( " );
612    lol_print( actual );
613    printf( " )\n* %s %s\n", message, arg ? arg->string : "" );
614    print_source_line( rule->procedure );
615    printf( "see definition of rule '%s' being called\n", rule->name );
616    backtrace( frame->prev );
617    exit(1);
618}
619
620/* define delimiters for type check elements in argument lists (and
621 * return type specifications, eventually)
622 */
623# define TYPE_OPEN_DELIM '['
624# define TYPE_CLOSE_DELIM ']'
625
626/* is_type_name - true iff the given string represents a type check
627 * specification
628 */
629static int
630is_type_name( char* s )
631{
632    return s[0] == TYPE_OPEN_DELIM
633        && s[strlen(s) - 1] == TYPE_CLOSE_DELIM;
634}
635
636/*
637 * arg_modifier - if the next element of formal is a single character,
638 * return that; return 0 otherwise.  Used to extract "*+?" modifiers
639 * from argument lists.
640 */
641static char
642arg_modifier( LIST* formal )
643{
644    if ( formal->next )
645    {
646        char *next = formal->next->string;
647        if ( next && next[0] != 0 && next[1] == 0 )
648            return next[0];
649    }
650    return 0;
651}
652
653/*
654 * type_check - checks that each element of values satisfies the
655 * requirements of type_name.
656 *
657 *      caller   - the frame of the rule calling the rule whose
658 *                 arguments are being checked
659 *
660 *      called   - the rule being called
661 *
662 *      arg_name - a list element containing the name of the argument
663 *                 being checked
664 */
665static void
666type_check( char* type_name, LIST *values, FRAME* caller, RULE* called, LIST* arg_name )
667{
668    static module_t *typecheck = 0;
669
670    /* if nothing to check, bail now */
671    if ( !values || !type_name )
672        return;
673
674    if ( !typecheck )
675        typecheck = bindmodule(".typecheck");
676
677    /* if the checking rule can't be found, also bail */
678    {
679        RULE checker_, *checker = &checker_;
680
681        checker->name = type_name;
682        if ( !typecheck->rules || !hashcheck( typecheck->rules, (HASHDATA**)&checker ) )
683            return;
684    }
685   
686    exit_module( caller->module );
687   
688    while ( values != 0 )
689    {
690        LIST *error;
691        FRAME frame[1];
692        frame_init( frame );
693        frame->module = typecheck;
694        frame->prev = caller;
695        frame->prev_user = caller->module->user_module ? caller : caller->prev_user;
696
697        enter_module( typecheck );
698        /* Prepare the argument list */
699        lol_add( frame->args, list_new( L0, values->string ) );
700        error = evaluate_rule( type_name, frame );
701       
702        exit_module( typecheck );
703       
704        if ( error )
705            argument_error( error->string, called, caller, arg_name );
706
707        frame_free( frame );
708                values = values->next;
709    }
710
711    enter_module( caller->module );
712}
713
714/*
715 * collect_arguments() - local argument checking and collection
716 */
717static SETTINGS *
718collect_arguments( RULE* rule, FRAME* frame )
719{
720    SETTINGS *locals = 0;
721   
722    LOL* all_actual = frame->args;
723    LOL *all_formal = rule->arguments ? rule->arguments->data : 0;
724    if ( all_formal ) /* Nothing to set; nothing to check */
725    {
726        int max = all_formal->count > all_actual->count
727            ? all_formal->count
728            : all_actual->count;
729       
730        int n;
731        for ( n = 0; n < max ; ++n )
732        {
733            LIST *actual = lol_get( all_actual, n );
734            char *type_name = 0;
735           
736            LIST *formal;
737            for ( formal = lol_get( all_formal, n ); formal; formal = formal->next )
738            {
739                char* name = formal->string;
740
741                if ( is_type_name(name) )
742                {
743                    if ( type_name )
744                        argument_error( "missing argument name before type name:", rule, frame, formal );
745                   
746                    if ( !formal->next )
747                        argument_error( "missing argument name after type name:", rule, frame, formal );
748
749                    type_name = formal->string;
750                }
751                else
752                {
753                    LIST* value = 0;
754                    char modifier;
755                    LIST* arg_name = formal; /* hold the argument name for type checking */
756                   
757                    /* Stop now if a variable number of arguments are specified */
758                    if ( name[0] == '*' && name[1] == 0 )
759                        return locals;
760
761                    modifier = arg_modifier( formal );
762               
763                    if ( !actual && modifier != '?' && modifier != '*' )
764                        argument_error( "missing argument", rule, frame, formal );
765
766                    switch ( modifier )
767                    {
768                    case '+':
769                    case '*':
770                        value = list_copy( 0, actual );
771                        actual = 0;
772                        /* skip an extra element for the modifier */
773                        formal = formal->next; 
774                        break;
775                    case '?':
776                        /* skip an extra element for the modifier */
777                        formal = formal->next; 
778                        /* fall through */
779                    default:
780                        if ( actual ) /* in case actual is missing */
781                        {
782                            value = list_new( 0, actual->string );
783                            actual = actual->next;
784                        }
785                    }
786               
787                    locals = addsettings( locals, 0, name, value );
788                    type_check( type_name, value, frame, rule, arg_name );
789                    type_name = 0;
790                }
791            }
792           
793            if ( actual )
794            {
795                argument_error( "extra argument", rule, frame, actual );
796            }
797        }
798    }
799    return locals;
800}
801
802static int python_instance_number = 0;
803
804RULE *
805enter_rule( char *rulename, module_t *target_module );
806
807#ifdef HAVE_PYTHON
808static LIST*
809call_python_function(RULE* r, FRAME* frame)
810{
811    LIST* result = 0;
812    PyObject* arguments = PyTuple_New(frame->args->count);
813    int i ;
814    PyObject* py_result;
815
816    for(i = 0; i < frame->args->count; ++i)
817    {
818        PyObject* arg = PyList_New(0);
819        LIST* l = lol_get( frame->args, i);
820
821        for(; l; l = l->next)
822        {
823            PyObject* v = PyString_FromString(l->string);
824            /* Steals reference to 'v' */
825            PyList_Append(arg, v);           
826        }
827        /* Steals reference to 'arg' */
828        PyTuple_SetItem(arguments, i, arg);
829    }
830
831    py_result = PyObject_CallObject(r->python_function, arguments);
832    Py_DECREF(arguments);
833    if (py_result != NULL) {
834       
835        if (PyList_Check(py_result)) {
836            int size = PyList_Size(py_result);
837            int i;
838            for(i = 0; i < size; ++i)
839            {
840                PyObject* item = PyList_GetItem(py_result, i);
841                if (PyString_Check(item))
842                {
843                    result = list_new(result, 
844                                      newstr(PyString_AsString(item)));
845                }
846                else
847                {
848                    fprintf(stderr, "Non-string object returned by Python call\n");
849                }
850            }
851        }
852        else if (PyInstance_Check(py_result))
853        {
854            static char instance_name[1000];
855            static char imported_method_name[1000];
856            module_t* m;
857            PyObject* method;
858            PyObject* method_name = PyString_FromString("foo");
859            RULE* r;
860
861            fprintf(stderr, "Got instance!\n");
862
863            snprintf(instance_name, 1000,
864                     "pyinstance%d", python_instance_number);
865            snprintf(imported_method_name, 1000,
866                     "pyinstance%d.foo", python_instance_number);
867            ++python_instance_number;
868           
869            m = bindmodule(instance_name);
870
871            /* This is expected to get bound method. */
872            method = PyObject_GetAttr(py_result, method_name);
873           
874            r = bindrule( imported_method_name, root_module() );
875
876            r->python_function = method;
877
878            result = list_new(0, newstr(instance_name));   
879
880            Py_DECREF(method_name);
881        }
882        else if (py_result == Py_None)
883        {
884            result = L0;
885        }
886        else
887        {
888            fprintf(stderr, "Non-list object returned by Python call\n");
889        }
890
891        Py_DECREF(py_result);
892    }
893    else {
894        PyErr_Print();
895        fprintf(stderr,"Call failed\n");
896    }
897   
898    return result;
899}
900#endif
901
902/*
903 * evaluate_rule() - execute a rule invocation
904 */
905
906LIST *
907evaluate_rule(
908    char    *rulename,
909    FRAME *frame )
910{
911    LIST      *result = L0;
912    RULE          *rule;
913    profile_frame prof[1];
914    module_t    *prev_module = frame->module;
915   
916    LIST      *l;
917    {
918        LOL arg_context_, *arg_context = &arg_context_;
919        if ( !frame->prev )
920            lol_init(arg_context);
921        else
922            arg_context = frame->prev->args;
923       
924        l = var_expand( L0, rulename, rulename+strlen(rulename), arg_context, 0 );
925    }
926
927    if ( !l )
928    {
929        backtrace_line( frame->prev );
930        printf( "warning: rulename %s expands to empty string\n", rulename );
931        backtrace( frame->prev );
932        return result;
933    }
934
935    rulename = l->string;
936    rule = bindrule( l->string, frame->module );
937
938#ifdef HAVE_PYTHON
939    if (rule->python_function)
940    {
941        return call_python_function(rule, frame);
942    }
943#endif
944
945    /* drop the rule name */
946    l = list_pop_front( l );
947
948    /* tack the rest of the expansion onto the front of the first argument */
949    frame->args->list[0] = list_append( l, lol_get( frame->args, 0 ) );
950
951    if ( DEBUG_COMPILE )
952    {
953        /* Try hard to indicate in which module the rule is going to execute */
954        if ( rule->module != frame->module
955             && rule->procedure != 0 && strcmp(rulename, rule->procedure->rulename) )
956        {
957            char buf[256] = "";
958            strncat( buf, rule->module->name, sizeof(buf) - 1 );
959            strncat( buf, rule->name, sizeof(buf) - 1 );
960            debug_compile( 1, buf, frame);
961        }
962        else
963        {
964            debug_compile( 1, rulename, frame);
965        }
966
967        lol_print( frame->args );
968        printf( "\n" );
969    }
970   
971    if ( rule->procedure && rule->module != prev_module )
972    {
973        /* propagate current module to nested rule invocations */
974        frame->module = rule->module;
975       
976        /* swap variables */
977        exit_module( prev_module );
978        enter_module( rule->module );
979    }
980       
981    /* record current rule name in frame */
982    if ( rule->procedure )
983    {
984        frame->rulename = rulename;
985        /* and enter record profile info */
986        if ( DEBUG_PROFILE )
987            profile_enter( rule->procedure->rulename, prof );
988    }
989
990    /* Check traditional targets $(<) and sources $(>) */
991
992    if( !rule->actions && !rule->procedure )
993    {
994        backtrace_line( frame->prev );
995        printf( "rule %s unknown in module %s\n", rule->name, frame->module->name );
996        backtrace( frame->prev );
997        exit(1);
998    }
999
1000    /* If this rule will be executed for updating the targets */
1001    /* then construct the action for make(). */
1002
1003    if( rule->actions )
1004    {
1005        TARGETS *t;
1006        ACTION  *action;
1007
1008        /* The action is associated with this instance of this rule */
1009
1010        action = (ACTION *)malloc( sizeof( ACTION ) );
1011        memset( (char *)action, '\0', sizeof( *action ) );
1012
1013        if ( DEBUG_PROFILE )
1014            profile_memory( sizeof( ACTION ) );
1015
1016        action->rule = rule;
1017        action->targets = targetlist( (TARGETS *)0, lol_get( frame->args, 0 ) );
1018        action->sources = targetlist( (TARGETS *)0, lol_get( frame->args, 1 ) );
1019
1020        /* Append this action to the actions of each target */
1021
1022        for( t = action->targets; t; t = t->next )
1023            t->target->actions = actionlist( t->target->actions, action );
1024    }
1025
1026    /* Now recursively compile any parse tree associated with this rule */
1027    /* refer/free to ensure rule not freed during use */
1028
1029    if( rule->procedure )
1030    {
1031        SETTINGS *local_args = collect_arguments( rule, frame );
1032        PARSE *parse = rule->procedure;
1033        parse_refer( parse );
1034       
1035        pushsettings( local_args );
1036        result = parse_evaluate( parse, frame );
1037        popsettings( local_args );
1038        freesettings( local_args );
1039       
1040        parse_free( parse );
1041    }
1042
1043    if ( frame->module != prev_module )
1044    {
1045        exit_module( frame->module );
1046        enter_module( prev_module );
1047    }
1048
1049    if ( DEBUG_PROFILE && rule->procedure )
1050        profile_exit( prof );
1051
1052    if( DEBUG_COMPILE )
1053        debug_compile( -1, 0, frame);
1054
1055    return result;
1056}
1057
1058/*
1059 * Call the given rule with the specified parameters.
1060 * The parameters should be of LIST* and end with NULL pointer.
1061 * This differs from the 'evaluate_rule' in that frame
1062 * for called rule is prepared in 'call_rule'.
1063 *
1064 * This function is usefull when builtin rule (in C) wants to
1065 * call another rule, which might be implemented in Jam.
1066 */
1067LIST *call_rule( char *rulename, FRAME* caller_frame, ...)
1068{
1069    va_list va;
1070    LIST *result;
1071
1072    FRAME       inner[1];
1073    frame_init( inner );
1074    inner->prev = caller_frame;
1075    inner->prev_user = caller_frame->module->user_module ? 
1076        caller_frame : caller_frame->prev_user;
1077    inner->module = caller_frame->module;   
1078    inner->procedure = 0;
1079
1080    va_start(va, caller_frame);   
1081    for(;;)
1082    {
1083        LIST* l = va_arg(va, LIST*);
1084        if (!l)
1085            break;
1086        lol_add(inner->args, l);
1087    }
1088    va_end(va);
1089               
1090    result = evaluate_rule(rulename, inner);   
1091
1092    frame_free(inner);
1093
1094    return result;
1095}
1096
1097/*
1098 * compile_rules() - compile a chain of rules
1099 *
1100 *      parse->left     single rule
1101 *      parse->right    more compile_rules() by right-recursion
1102 */
1103
1104LIST *
1105compile_rules(
1106    PARSE   *parse,
1107    FRAME *frame )
1108{
1109    /* Ignore result from first statement; return the 2nd. */
1110        /* Optimize recursion on the right by looping. */
1111
1112    do list_free( parse_evaluate( parse->left, frame ) );
1113    while( (parse = parse->right)->func == compile_rules );
1114
1115    return parse_evaluate( parse, frame );
1116}
1117
1118/*
1119 * compile_set() - compile the "set variable" statement
1120 *
1121 *  parse->left variable names
1122 *  parse->right    variable values
1123 *  parse->num  ASSIGN_SET/APPEND/DEFAULT
1124 */
1125
1126LIST *
1127compile_set(
1128    PARSE   *parse,
1129    FRAME *frame )
1130{
1131    LIST    *nt = parse_evaluate( parse->left, frame );
1132    LIST    *ns = parse_evaluate( parse->right, frame );
1133    LIST    *l;
1134    int     setflag;
1135    char    *trace;
1136
1137    switch( parse->num )
1138    {
1139    case ASSIGN_SET:    setflag = VAR_SET; trace = "="; break;
1140    case ASSIGN_APPEND: setflag = VAR_APPEND; trace = "+="; break;
1141    case ASSIGN_DEFAULT:    setflag = VAR_DEFAULT; trace = "?="; break;
1142    default:        setflag = VAR_SET; trace = ""; break;
1143    }
1144
1145    if( DEBUG_COMPILE )
1146    {
1147        debug_compile( 0, "set", frame);
1148        list_print( nt );
1149        printf( " %s ", trace );
1150        list_print( ns );
1151        printf( "\n" );
1152    }
1153
1154    /* Call var_set to set variable */
1155    /* var_set keeps ns, so need to copy it */
1156
1157    for( l = nt; l; l = list_next( l ) )
1158        var_set( l->string, list_copy( L0, ns ), setflag );
1159
1160    list_free( nt );
1161
1162    return ns;
1163}
1164
1165/*
1166 * compile_setcomp() - support for `rule` - save parse tree
1167 *
1168 *  parse->string   rule name
1169 *  parse->left rules for rule
1170 *  parse->right optional list-of-lists describing arguments
1171 */
1172
1173LIST *
1174compile_setcomp(
1175    PARSE   *parse,
1176    FRAME *frame)
1177{
1178    argument_list* arg_list = 0;
1179   
1180    /* Create new LOL describing argument requirements if supplied */
1181    if ( parse->right )
1182    {
1183        PARSE *p;
1184        arg_list = args_new();
1185        for( p = parse->right; p; p = p->left )
1186            lol_add( arg_list->data, parse_evaluate( p->right, frame ) );
1187    }
1188   
1189    new_rule_body( frame->module, parse->string, arg_list, parse->left, !parse->num );
1190    return L0;
1191}
1192
1193/*
1194 * compile_setexec() - support for `actions` - save execution string
1195 *
1196 *  parse->string   rule name
1197 *  parse->string1  OS command string
1198 *  parse->num  flags
1199 *  parse->left `bind` variables
1200 *
1201 * Note that the parse flags (as defined in compile.h) are transfered
1202 * directly to the rule flags (as defined in rules.h).
1203 */
1204
1205LIST *
1206compile_setexec(
1207    PARSE   *parse,
1208    FRAME *frame )
1209{
1210    LIST* bindlist = parse_evaluate( parse->left, frame );
1211
1212    new_rule_actions( frame->module, parse->string, parse->string1, bindlist, parse->num );
1213
1214    return L0;
1215}
1216
1217/*
1218 * compile_settings() - compile the "on =" (set variable on exec) statement
1219 *
1220 *  parse->left variable names
1221 *  parse->right    target name
1222 *  parse->third    variable value
1223 *  parse->num  ASSIGN_SET/APPEND   
1224 */
1225
1226LIST *
1227compile_settings(
1228    PARSE   *parse,
1229    FRAME *frame )
1230{
1231    LIST    *nt = parse_evaluate( parse->left, frame );
1232    LIST    *ns = parse_evaluate( parse->third, frame );
1233    LIST    *targets = parse_evaluate( parse->right, frame );
1234    LIST    *ts;
1235    int append = parse->num == ASSIGN_APPEND;
1236
1237    if( DEBUG_COMPILE )
1238    {
1239        debug_compile( 0, "set", frame);
1240        list_print( nt );
1241        printf( " on " );
1242        list_print( targets );
1243        printf( " %s ", append ? "+=" : "=" );
1244        list_print( ns );
1245        printf( "\n" );
1246    }
1247
1248    /* Call addsettings to save variable setting */
1249    /* addsettings keeps ns, so need to copy it */
1250    /* Pass append flag to addsettings() */
1251
1252    for( ts = targets; ts; ts = list_next( ts ) )
1253    {
1254        TARGET  *t = bindtarget( ts->string );
1255        LIST    *l;
1256
1257        for( l = nt; l; l = list_next( l ) )
1258        t->settings = addsettings( t->settings, append, 
1259                l->string, list_copy( (LIST*)0, ns ) );
1260    }
1261
1262    list_free( nt );
1263    list_free( targets );
1264
1265    return ns;
1266}
1267
1268/*
1269 * compile_switch() - compile 'switch' rule
1270 *
1271 *  parse->left switch value (only 1st used)
1272 *  parse->right    cases
1273 *
1274 *  cases->left 1st case
1275 *  cases->right    next cases
1276 *
1277 *  case->string    argument to match
1278 *  case->left  parse tree to execute
1279 */
1280
1281LIST *
1282compile_switch(
1283    PARSE   *parse,
1284    FRAME *frame )
1285{
1286    LIST    *nt = parse_evaluate( parse->left, frame );
1287    LIST    *result = 0;
1288
1289    if( DEBUG_COMPILE )
1290    {
1291        debug_compile( 0, "switch", frame);
1292        list_print( nt );
1293        printf( "\n" );
1294    }
1295
1296    /* Step through cases */
1297
1298    for( parse = parse->right; parse; parse = parse->right )
1299    {
1300        if( !glob( parse->left->string, nt ? nt->string : "" ) )
1301        {
1302        /* Get & exec parse tree for this case */
1303        parse = parse->left->left;
1304        result = parse_evaluate( parse, frame );
1305        break;
1306        }
1307    }
1308
1309    list_free( nt );
1310
1311    return result;
1312}
1313
1314/*
1315 * debug_compile() - printf with indent to show rule expansion.
1316 */
1317
1318static void
1319debug_compile( int which, char *s, FRAME* frame )
1320{
1321    static int level = 0;
1322    static char indent[36] = ">>>>|>>>>|>>>>|>>>>|>>>>|>>>>|>>>>|";
1323
1324    if ( which >= 0 )
1325    {
1326      int i;
1327     
1328      print_source_line( frame->procedure );
1329     
1330      i = (level+1)*2;
1331      while ( i > 35 )
1332      {
1333        printf( indent );
1334        i -= 35;
1335      }
1336
1337      printf( "%*.*s ", i, i, indent );
1338    }
1339
1340    if( s )
1341        printf( "%s ", s );
1342
1343    level += which;
1344}
Note: See TracBrowser for help on using the repository browser.