cosmetix (added some __attribute__()s)
[k8jam.git] / src / compile.c
blobca961da3ec1b60bb0fb3d8dfb8d9a88c69278837
1 /*
2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
6 /*
7 * compile.c - compile parsed jam statements
9 * External routines:
11 * compile_append() - append list results of two statements
12 * compile_break() - compile 'break/continue/return' rule
13 * compile_eval() - evaluate if to determine which leg to compile
14 * compile_foreach() - compile the "for x in y" statement
15 * compile_if() - compile 'if' rule
16 * compile_include() - support for 'include' - call include() on file
17 * compile_list() - expand and return a list
18 * compile_local() - declare (and set) local variables
19 * compile_null() - do nothing -- a stub for parsing
20 * compile_on() - run rule under influence of on-target variables
21 * compile_rule() - compile a single user defined rule
22 * compile_rules() - compile a chain of rules
23 * compile_set() - compile the "set variable" statement
24 * compile_setcomp() - support for `rule` - save parse tree
25 * compile_setexec() - support for `actions` - save execution string
26 * compile_settings() - compile the "on =" (set variable on exec) statement
27 * compile_switch() - compile 'switch' rule
29 * Internal routines:
31 * debug_compile() - printf with indent to show rule expansion.
32 * evaluate_rule() - execute a rule invocation
34 * 02/03/94 (seiwald) - Changed trace output to read "setting" instead of
35 * the awkward sounding "settings".
36 * 04/12/94 (seiwald) - Combined build_depends() with build_includes().
37 * 04/12/94 (seiwald) - actionlist() now just appends a single action.
38 * 04/13/94 (seiwald) - added shorthand L0 for null list pointer
39 * 05/13/94 (seiwald) - include files are now bound as targets, and thus
40 * can make use of $(SEARCH)
41 * 06/01/94 (seiwald) - new 'actions existing' does existing sources
42 * 08/23/94 (seiwald) - Support for '+=' (append to variable)
43 * 12/20/94 (seiwald) - NOTIME renamed NOTFILE.
44 * 01/22/95 (seiwald) - Exit rule.
45 * 02/02/95 (seiwald) - Always rule; LEAVES rule.
46 * 02/14/95 (seiwald) - NoUpdate rule.
47 * 01/20/00 (seiwald) - Upgraded from K&R to ANSI C
48 * 09/07/00 (seiwald) - stop crashing when a rule redefines itself
49 * 09/11/00 (seiwald) - new evaluate_rule() for headers().
50 * 09/11/00 (seiwald) - rules now return values, accessed via [ rule arg ... ]
51 * 09/12/00 (seiwald) - don't complain about rules invoked without targets
52 * 01/13/01 (seiwald) - fix case where rule is defined within another
53 * 01/10/01 (seiwald) - built-ins split out to builtin.c.
54 * 01/11/01 (seiwald) - optimize compile_rules() for tail recursion
55 * 01/21/01 (seiwald) - replace evaluate_if() with compile_eval()
56 * 01/24/01 (seiwald) - 'while' statement
57 * 03/23/01 (seiwald) - "[ on target rule ]" support
58 * 02/28/02 (seiwald) - merge EXEC_xxx flags in with RULE_xxx
59 * 03/02/02 (seiwald) - rules can be invoked via variable names
60 * 03/12/02 (seiwald) - &&,&,||,|,in now short-circuit again
61 * 03/25/02 (seiwald) - if ( "" a b ) one again returns true
62 * 06/21/02 (seiwald) - support for named parameters
63 * 10/22/02 (seiwald) - list_new() now does its own newstr()/copystr()
64 * 10/22/02 (seiwald) - working return/break/continue statements
65 * 11/04/02 (seiwald) - const-ing for string literals
66 * 11/18/02 (seiwald) - remove bogus search() in 'on' statement.
67 * 12/17/02 (seiwald) - new copysettings() to protect target-specific vars
69 #include "jam.h"
71 #include "lists.h"
72 #include "parse.h"
73 #include "compile.h"
74 #include "variable.h"
75 #include "expand.h"
76 #include "rules.h"
77 #include "newstr.h"
78 #include "search.h"
79 #include "matchglob.h"
82 static const char *set_names[] = { "=", "+=", "-=", "?=" };
83 static void debug_compile (int which, const char *s);
87 * compile_append() - append list results of two statements
89 * parse->left more compile_append() by left-recursion
90 * parse->right single rule
92 LIST *compile_append (PARSE *parse, LOL *args, int *jmp) {
93 /* Append right to left. */
94 return list_append(
95 (*parse->left->func)(parse->left, args, jmp),
96 (*parse->right->func)(parse->right, args, jmp)
102 * compile_break() - compile 'break/continue/return' rule
104 * parse->left results
105 * parse->num JMP_BREAK/CONTINUE/RETURN
107 LIST *compile_break (PARSE *parse, LOL *args, int *jmp) {
108 LIST *lv = (*parse->left->func)(parse->left, args, jmp);
109 *jmp = parse->num;
110 return lv;
115 * compile_eval() - evaluate if to determine which leg to compile
117 * Returns:
118 * list if expression true - compile 'then' clause
119 * L0 if expression false - compile 'else' clause
121 static JAMFA_PURE int lcmp (LIST *t, LIST *s) {
122 int status = 0;
124 while (!status && (t || s)) {
125 const char *st = t?t->string:"";
126 const char *ss = s?s->string:"";
128 status = strcmp(st, ss);
129 t = t?list_next(t):t;
130 s = s?list_next(s):s;
132 return status;
136 LIST *compile_eval (PARSE *parse, LOL *args, int *jmp) {
137 LIST *ll, *lr, *s, *t;
138 int status = 0;
140 /* short circuit lr eval for &&, ||, and 'in' */
141 ll = (*parse->left->func)(parse->left, args, jmp);
142 lr = 0;
143 switch (parse->num) {
144 case EXPR_AND:
145 case EXPR_IN:
146 case EXPR_ANYIN:
147 if (ll) goto eval;
148 break;
149 case EXPR_OR:
150 if (!ll) goto eval;
151 break;
152 default:
153 eval: lr = (*parse->right->func)(parse->right, args, jmp);
154 break;
156 /* now eval */
157 switch (parse->num) {
158 case EXPR_NOT:
159 if (!ll) status = 1;
160 break;
161 case EXPR_AND:
162 if (ll && lr) status = 1;
163 break;
164 case EXPR_OR:
165 if (ll || lr) status = 1;
166 break;
167 case EXPR_IN:
168 /* "a in b": make sure each of ll is equal to something in lr */
169 for (t = ll; t; t = list_next(t)) {
170 for (s = lr; s; s = list_next(s)) if (strcmp(t->string, s->string) == 0) break;
171 if (!s) break;
173 /* No more ll? success */
174 if (!t) status = 1;
175 break;
176 case EXPR_ANYIN:
177 /* "a any-in b": does b contains at least one item from a? */
178 for (t = ll; t && !status; t = list_next(t)) {
179 for (s = lr; s; s = list_next(s)) if (strcmp(t->string, s->string) == 0) { status = 1; break; } /* success */
181 break;
182 case EXPR_EXISTS:
183 if (lcmp(ll, L0) != 0) status = 1;
184 break;
185 case EXPR_EQUALS:
186 if (lcmp(ll, lr) == 0) status = 1;
187 break;
188 case EXPR_NOTEQ:
189 if (lcmp(ll, lr) != 0) status = 1;
190 break;
191 case EXPR_LESS:
192 if (lcmp(ll, lr) < 0) status = 1;
193 break;
194 case EXPR_LESSEQ:
195 if (lcmp(ll, lr) <= 0) status = 1;
196 break;
197 case EXPR_MORE:
198 if (lcmp(ll, lr) > 0) status = 1;
199 break;
200 case EXPR_MOREEQ:
201 if (lcmp(ll, lr) >= 0) status = 1;
202 break;
204 if (DEBUG_IF) {
205 debug_compile(0, "if");
206 list_print(ll);
207 printf("(%d) ", status);
208 list_print(lr);
209 printf("\n");
211 /* find something to return */
212 /* in odd circumstances (like "" = "") we'll have to return a new string */
213 if (!status) t = 0;
214 else if (ll) t = ll, ll = 0;
215 else if (lr) t = lr, lr = 0;
216 else t = list_new(L0, "1", 0);
217 if (ll) list_free(ll);
218 if (lr) list_free(lr);
219 return t;
224 * compile_foreach() - compile the "for x in y" statement
226 * Compile_foreach() resets the given variable name to each specified
227 * value, executing the commands enclosed in braces for each iteration.
229 * parse->string index variable
230 * parse->left variable values
231 * parse->right rule to compile
233 LIST *compile_foreach (PARSE *p, LOL *args, int *jmp) {
234 LIST *nv = (*p->left->func)(p->left, args, jmp);
235 LIST *result = 0;
236 LIST *l;
238 /* for each value for var */
239 for (l = nv; l && *jmp == JMP_NONE; l = list_next(l)) {
240 /* reset $(p->string) for each val */
241 var_set(p->string, list_new(L0, l->string, 1), VAR_SET);
242 /* keep only last result */
243 list_free(result);
244 result = (*p->right->func)(p->right, args, jmp);
245 /* continue loop? */
246 if (*jmp == JMP_CONTINUE) *jmp = JMP_NONE;
248 /* here by break/continue? */
249 if (*jmp == JMP_BREAK || *jmp == JMP_CONTINUE) *jmp = JMP_NONE;
250 list_free(nv);
251 /* returns result of last loop */
252 return result;
257 * compile_if() - compile 'if' rule
259 * parse->left condition tree
260 * parse->right then tree
261 * parse->third else tree
263 LIST *compile_if (PARSE *p, LOL *args, int *jmp) {
264 LIST *l = (*p->left->func)(p->left, args, jmp);
266 p = l?p->right:p->third;
267 list_free(l);
268 return (*p->func)(p, args, jmp);
273 * compile_include() - support for 'include' - call include() on file
275 * parse->left list of files to include (can only do 1)
277 static LIST *compile_include_internal (PARSE *parse, LOL *args, int *jmp, int assoft) {
278 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
280 if (DEBUG_COMPILE) {
281 debug_compile(0, (assoft?"softinclude":"include"));
282 list_print(nt);
283 printf("\n");
285 if (nt) {
286 TARGET *t = bindtarget(nt->string);
287 /* bind the include file under the influence of "on-target" variables */
288 /* though they are targets, include files are not built with make() */
289 /* needn't copysettings(), as search sets no vars */
290 pushsettings(t->settings);
291 t->boundname = search(t->name, &t->time);
292 popsettings(t->settings);
293 /* don't parse missing file if NOCARE set */
294 if (t->time || !(t->flags & T_FLAG_NOCARE)) {
295 int doit = 1;
296 FILE *fl = fopen(t->boundname, "r");
298 if (fl == NULL) {
299 if (assoft) {
300 doit = 0;
301 } else {
302 printf("Failed to include file '%s'\n", t->boundname);
303 exit(EXITBAD);
305 } else {
306 fclose(fl);
308 if (doit) parse_file(t->boundname);
311 list_free(nt);
312 return L0;
317 * compile_include() - support for 'include' - call include() on file
319 * parse->left list of files to include (can only do 1)
321 LIST *compile_include (PARSE *parse, LOL *args, int *jmp) {
322 return compile_include_internal(parse, args, jmp, 0);
327 * compile_softinclude() - support for 'softinclude' - call include() on file if file exists
329 * parse->left list of files to include (can only do 1)
331 LIST *compile_softinclude (PARSE *parse, LOL *args, int *jmp) {
332 return compile_include_internal(parse, args, jmp, 1);
337 * compile_list() - expand and return a list
339 * parse->string - character string to expand
341 LIST *compile_list (PARSE *parse, LOL *args, int *jmp) {
342 /* voodoo 1 means: s is a copyable string */
343 const char *s = parse->string;
344 return var_expand(L0, s, s+strlen(s), args, 1);
349 * compile_local() - declare (and set) local variables
351 * parse->left list of variables
352 * parse->right list of values
353 * parse->third rules to execute
355 LIST *compile_local (PARSE *parse, LOL *args, int *jmp) {
356 LIST *l;
357 SETTINGS *s = 0;
358 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
359 LIST *ns = (*parse->right->func)(parse->right, args, jmp);
360 LIST *result;
362 if (DEBUG_COMPILE) {
363 debug_compile(0, "local");
364 list_print(nt);
365 printf(" = ");
366 list_print(ns);
367 printf("\n");
369 /* initial value is ns */
370 for (l = nt; l; l = list_next(l)) s = addsettings(s, 0, l->string, list_copy((LIST *)0, ns));
371 list_free(ns);
372 list_free(nt);
373 /* note that callees of the current context get this "local" */
374 /* variable, making it not so much local as layered */
375 pushsettings(s);
376 result = (*parse->third->func)(parse->third, args, jmp);
377 popsettings(s);
378 freesettings(s);
379 return result;
384 * compile_null() - do nothing -- a stub for parsing
386 LIST *compile_null (PARSE *parse, LOL *args, int *jmp) {
387 return L0;
392 * compile_on() - run rule under influence of on-target variables
394 * parse->left target list; only first used
395 * parse->right rule to run
397 LIST *compile_on (PARSE *parse, LOL *args, int *jmp) {
398 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
399 LIST *result = 0;
401 if (DEBUG_COMPILE) {
402 debug_compile(0, "on");
403 list_print(nt);
404 printf("\n");
406 /* copy settings, so that 'on target var on target = val' doesn't set var globally */
407 if (nt) {
408 TARGET *t = bindtarget(nt->string);
409 SETTINGS *s = copysettings(t->settings);
411 pushsettings(s);
412 result = (*parse->right->func)(parse->right, args, jmp);
413 popsettings(s);
414 freesettings(s);
416 list_free(nt);
417 return result;
422 * compile_rule() - compile a single user defined rule
424 * parse->left list of rules to run
425 * parse->right parameters (list of lists) to rule, recursing left
427 * Wrapped around evaluate_rule() so that headers() can share it.
429 LIST *compile_rule (PARSE *parse, LOL *args, int *jmp) {
430 LOL nargs[1];
431 LIST *result = 0;
432 LIST *ll, *l;
433 PARSE *p;
435 /* list of rules to run -- normally 1! */
436 ll = (*parse->left->func)(parse->left, args, jmp);
437 /* build up the list of arg lists */
438 lol_init(nargs);
439 for (p = parse->right; p; p = p->left) lol_add(nargs, (*p->right->func)(p->right, args, jmp));
440 /* run rules, appending results from each */
441 for (l = ll; l; l = list_next(l)) result = evaluate_rule(l->string, nargs, result);
442 list_free(ll);
443 lol_free(nargs);
444 return result;
449 * evaluate_rule() - execute a rule invocation
451 LIST *evaluate_rule (const char *rulename, LOL *args, LIST *result) {
452 //#ifdef OPT_EXPAND_RULE_NAMES_EXT
453 RULE *rule;
454 char *expanded, *c;
455 tKString buf;
457 kStringNew(&buf);
458 if (var_string(rulename, &buf, args, ' ') < 0) {
459 kStringFree(&buf);
460 printf("Failed to expand rule %s -- expansion too long\n", rulename);
461 exit(EXITBAD);
463 expanded = kStringCStr(&buf);
464 while (expanded[0] == ' ') ++expanded;
465 while ((c = strrchr(expanded, ' '))) *c = '\0';
466 if (DEBUG_COMPILE) {
467 debug_compile(1, rulename);
468 if (strcmp(rulename, expanded)) printf("-> %s ", expanded);
469 lol_print(args);
470 printf("\n");
472 rule = bindrule(expanded);
473 kStringFree(&buf);
474 //#else
476 RULE *rule = bindrule(rulename);
478 if (DEBUG_COMPILE) {
479 debug_compile(1, rulename);
480 lol_print(args);
481 printf("\n");
484 //#endif
485 /* check traditional targets $(<) and sources $(>) */
486 if (!rule->actions && !rule->procedure) printf("warning: unknown rule %s\n", rule->name);
487 /* if this rule will be executed for updating the targets then construct the action for make() */
488 if (rule->actions) {
489 TARGETS *t;
490 ACTION *action;
492 /* the action is associated with this instance of this rule */
493 action = (ACTION *)malloc(sizeof(ACTION));
494 memset((char *)action, '\0', sizeof(*action));
495 action->rule = rule;
496 action->targets = targetlist((TARGETS *)0, lol_get(args, 0));
497 action->sources = targetlist((TARGETS *)0, lol_get(args, 1));
498 /* append this action to the actions of each target */
499 for (t = action->targets; t; t = t->next) t->target->actions = actionlist(t->target->actions, action);
501 /* now recursively compile any parse tree associated with this rule */
502 if (rule->procedure) {
503 PARSE *parse = rule->procedure;
504 SETTINGS *s = 0;
505 int jmp = JMP_NONE;
506 LIST *l;
507 int i;
509 /* build parameters as local vars */
510 for (l = rule->params, i = 0; l; l = l->next, i++) s = addsettings(s, 0, l->string, list_copy(L0, lol_get(args, i)));
511 /* run rule */
512 /* bring in local params */
513 /* refer/free to ensure rule not freed during use */
514 parse_refer(parse);
515 pushsettings(s);
516 result = list_append(result, (*parse->func)(parse, args, &jmp));
517 popsettings(s);
518 freesettings(s);
519 parse_free(parse);
521 if (DEBUG_COMPILE) debug_compile(-1, 0);
522 //#ifdef OPT_EXPAND_RULE_NAMES_EXT
523 kStringFree(&buf);
524 //#endif
525 return result;
530 * compile_rules() - compile a chain of rules
532 * parse->left single rule
533 * parse->right more compile_rules() by right-recursion
535 LIST *compile_rules (PARSE *parse, LOL *args, int *jmp) {
536 /* ignore result from first statement; return the 2nd */
537 /* optimize recursion on the right by looping */
538 LIST *result = 0;
540 while (*jmp == JMP_NONE && parse->func == compile_rules) {
541 list_free(result);
542 result = (*parse->left->func)(parse->left, args, jmp);
543 parse = parse->right;
545 if (*jmp == JMP_NONE) {
546 list_free(result);
547 result = (*parse->func)(parse, args, jmp);
549 return result;
554 * compile_set() - compile the "set variable" statement
556 * parse->left variable names
557 * parse->right variable values
558 * parse->num VAR_SET/APPEND/DEFAULT
560 LIST *compile_set (PARSE *parse, LOL *args, int *jmp) {
561 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
562 LIST *ns = (*parse->right->func)(parse->right, args, jmp);
563 LIST *l;
565 if (DEBUG_COMPILE) {
566 debug_compile(0, "set");
567 list_print(nt);
568 printf(" %s ", set_names[parse->num]);
569 list_print(ns);
570 printf("\n");
572 /* call var_set to set variable */
573 /* var_set keeps ns, so need to copy it */
574 for (l = nt; l; l = list_next(l)) var_set(l->string, list_copy(L0, ns), parse->num);
575 list_free(nt);
576 return ns;
581 * compile_setcomp() - support for `rule` - save parse tree
583 * parse->string rule name
584 * parse->left list of argument names
585 * parse->right rules for rule
587 LIST *compile_setcomp (PARSE *parse, LOL *args, int *jmp) {
588 RULE *rule = bindrule(parse->string);
589 LIST *params = 0;
590 PARSE *p;
592 /* build param list */
593 for (p = parse->left; p; p = p->left) params = list_new(params, p->string, 1);
594 if (DEBUG_COMPILE) {
595 debug_compile(0, "rule");
596 printf("%s ", parse->string);
597 list_print(params);
598 printf("\n");
600 /* free old one, if present */
601 if (rule->procedure) parse_free(rule->procedure);
602 if (rule->params) list_free(rule->params);
603 rule->procedure = parse->right;
604 rule->params = params;
605 /* we now own this parse tree */
606 /* don't let parse_free() release it */
607 parse_refer(parse->right);
608 return L0;
613 * compile_setexec() - support for `actions` - save execution string
615 * parse->string rule name
616 * parse->string1 OS command string
617 * parse->num flags
618 * parse->left `bind` variables
620 * Note that the parse flags (as defined in compile.h) are transfered
621 * directly to the rule flags (as defined in rules.h).
623 LIST *compile_setexec (PARSE *parse, LOL *args, int *jmp) {
624 RULE *rule = bindrule(parse->string);
625 LIST *bindlist = (*parse->left->func)(parse->left, args, jmp);
627 /* free old one, if present */
628 if (rule->actions) {
629 freestr(rule->actions);
630 list_free(rule->bindlist);
632 rule->actions = copystr(parse->string1);
633 rule->bindlist = bindlist;
634 rule->flags = parse->num;
635 return L0;
640 * compile_settings() - compile the "on =" (set variable on exec) statement
642 * parse->left variable names
643 * parse->right target name
644 * parse->third variable value
645 * parse->num VAR_SET/APPEND/DEFAULT
647 LIST *compile_settings (PARSE *parse, LOL *args, int *jmp) {
648 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
649 LIST *ns = (*parse->third->func)(parse->third, args, jmp);
650 LIST *targets = (*parse->right->func)(parse->right, args, jmp);
651 LIST *ts;
653 if (DEBUG_COMPILE) {
654 debug_compile(0, "set");
655 list_print(nt);
656 printf("on ");
657 list_print(targets);
658 printf(" %s ", set_names[parse->num]);
659 list_print(ns);
660 printf("\n");
662 /* call addsettings to save variable setting addsettings keeps ns, so need to copy it */
663 /* pass append flag to addsettings() */
664 for (ts = targets; ts; ts = list_next(ts)) {
665 TARGET *t = bindtarget(ts->string);
666 LIST *l;
668 for (l = nt; l; l = list_next(l)) {
669 t->settings = addsettings(t->settings, parse->num, l->string, list_copy((LIST *)0, ns));
672 list_free(nt);
673 list_free(targets);
674 return ns;
679 * compile_switch() - compile 'switch' rule
681 * parse->left switch value (only 1st used)
682 * parse->right cases
684 * cases->left 1st case
685 * cases->right next cases
687 * case->string argument to match
688 * case->left parse tree to execute
690 LIST *compile_switch (PARSE *parse, LOL *args, int *jmp) {
691 LIST *nt = (*parse->left->func)(parse->left, args, jmp);
692 LIST *result = 0;
694 if (DEBUG_COMPILE) {
695 debug_compile(0, "switch");
696 list_print(nt);
697 printf("\n");
699 /* step through cases */
700 for (parse = parse->right; parse; parse = parse->right) {
701 if (!matchglob(parse->left->string, nt?nt->string:"")) {
702 /* get and exec parse tree for this case */
703 parse = parse->left->left;
704 result = (*parse->func)(parse, args, jmp);
705 break;
708 list_free(nt);
709 return result;
714 * compile_while() - compile 'while' rule
716 * parse->left condition tree
717 * parse->right execution tree
719 LIST *compile_while (PARSE *p, LOL *args, int *jmp) {
720 LIST *result = 0;
721 LIST *l;
723 /* returns the value from the last execution of the block */
724 while ((*jmp == JMP_NONE ) && (l = (*p->left->func)(p->left, args, jmp))) {
725 /* always toss while's expression */
726 list_free(l);
727 /* keep only last result */
728 list_free(result);
729 result = (*p->right->func)(p->right, args, jmp);
730 /* continue loop? */
731 if (*jmp == JMP_CONTINUE) *jmp = JMP_NONE;
733 /* here by break/continue? */
734 if (*jmp == JMP_BREAK || *jmp == JMP_CONTINUE) *jmp = JMP_NONE;
735 /* returns result of last loop */
736 return result;
741 * debug_compile() - printf with indent to show rule expansion.
743 static void debug_compile (int which, const char *s) {
744 static int level = 0;
745 static const char indent[36] = ">>>>|>>>>|>>>>|>>>>|>>>>|>>>>|>>>>|";
746 int i = ((1+level)*2)%35;
748 if (which >= 0) printf("%*.*s ", i, i, indent);
749 if (s) printf("%s ", s);
750 level += which;