10a9064fbSMasahiro Yamada /*
20a9064fbSMasahiro Yamada * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
30a9064fbSMasahiro Yamada * Released under the terms of the GNU GPL v2.0.
40a9064fbSMasahiro Yamada */
50a9064fbSMasahiro Yamada
60a9064fbSMasahiro Yamada #ifndef EXPR_H
70a9064fbSMasahiro Yamada #define EXPR_H
80a9064fbSMasahiro Yamada
90a9064fbSMasahiro Yamada #ifdef __cplusplus
100a9064fbSMasahiro Yamada extern "C" {
110a9064fbSMasahiro Yamada #endif
120a9064fbSMasahiro Yamada
130a9064fbSMasahiro Yamada #include <assert.h>
140a9064fbSMasahiro Yamada #include <stdio.h>
150a9064fbSMasahiro Yamada #include "list.h"
160a9064fbSMasahiro Yamada #ifndef __cplusplus
170a9064fbSMasahiro Yamada #include <stdbool.h>
180a9064fbSMasahiro Yamada #endif
190a9064fbSMasahiro Yamada
200a9064fbSMasahiro Yamada struct file {
210a9064fbSMasahiro Yamada struct file *next;
220a9064fbSMasahiro Yamada struct file *parent;
230a9064fbSMasahiro Yamada const char *name;
240a9064fbSMasahiro Yamada int lineno;
250a9064fbSMasahiro Yamada };
260a9064fbSMasahiro Yamada
270a9064fbSMasahiro Yamada typedef enum tristate {
280a9064fbSMasahiro Yamada no, mod, yes
290a9064fbSMasahiro Yamada } tristate;
300a9064fbSMasahiro Yamada
310a9064fbSMasahiro Yamada enum expr_type {
32bf7ab1e7SMasahiro Yamada E_NONE, E_OR, E_AND, E_NOT,
33bf7ab1e7SMasahiro Yamada E_EQUAL, E_UNEQUAL, E_LTH, E_LEQ, E_GTH, E_GEQ,
34bf7ab1e7SMasahiro Yamada E_LIST, E_SYMBOL, E_RANGE
350a9064fbSMasahiro Yamada };
360a9064fbSMasahiro Yamada
370a9064fbSMasahiro Yamada union expr_data {
380a9064fbSMasahiro Yamada struct expr *expr;
390a9064fbSMasahiro Yamada struct symbol *sym;
400a9064fbSMasahiro Yamada };
410a9064fbSMasahiro Yamada
420a9064fbSMasahiro Yamada struct expr {
430a9064fbSMasahiro Yamada enum expr_type type;
440a9064fbSMasahiro Yamada union expr_data left, right;
450a9064fbSMasahiro Yamada };
460a9064fbSMasahiro Yamada
470a9064fbSMasahiro Yamada #define EXPR_OR(dep1, dep2) (((dep1)>(dep2))?(dep1):(dep2))
480a9064fbSMasahiro Yamada #define EXPR_AND(dep1, dep2) (((dep1)<(dep2))?(dep1):(dep2))
490a9064fbSMasahiro Yamada #define EXPR_NOT(dep) (2-(dep))
500a9064fbSMasahiro Yamada
510a9064fbSMasahiro Yamada #define expr_list_for_each_sym(l, e, s) \
520a9064fbSMasahiro Yamada for (e = (l); e && (s = e->right.sym); e = e->left.expr)
530a9064fbSMasahiro Yamada
540a9064fbSMasahiro Yamada struct expr_value {
550a9064fbSMasahiro Yamada struct expr *expr;
560a9064fbSMasahiro Yamada tristate tri;
570a9064fbSMasahiro Yamada };
580a9064fbSMasahiro Yamada
590a9064fbSMasahiro Yamada struct symbol_value {
600a9064fbSMasahiro Yamada void *val;
610a9064fbSMasahiro Yamada tristate tri;
620a9064fbSMasahiro Yamada };
630a9064fbSMasahiro Yamada
640a9064fbSMasahiro Yamada enum symbol_type {
650a9064fbSMasahiro Yamada S_UNKNOWN, S_BOOLEAN, S_TRISTATE, S_INT, S_HEX, S_STRING, S_OTHER
660a9064fbSMasahiro Yamada };
670a9064fbSMasahiro Yamada
680a9064fbSMasahiro Yamada /* enum values are used as index to symbol.def[] */
690a9064fbSMasahiro Yamada enum {
700a9064fbSMasahiro Yamada S_DEF_USER, /* main user value */
710a9064fbSMasahiro Yamada S_DEF_AUTO, /* values read from auto.conf */
720a9064fbSMasahiro Yamada S_DEF_DEF3, /* Reserved for UI usage */
730a9064fbSMasahiro Yamada S_DEF_DEF4, /* Reserved for UI usage */
740a9064fbSMasahiro Yamada S_DEF_COUNT
750a9064fbSMasahiro Yamada };
760a9064fbSMasahiro Yamada
77*e91610daSEugeniu Rosca /*
78*e91610daSEugeniu Rosca * Represents a configuration symbol.
79*e91610daSEugeniu Rosca *
80*e91610daSEugeniu Rosca * Choices are represented as a special kind of symbol and have the
81*e91610daSEugeniu Rosca * SYMBOL_CHOICE bit set in 'flags'.
82*e91610daSEugeniu Rosca */
830a9064fbSMasahiro Yamada struct symbol {
84*e91610daSEugeniu Rosca /* The next symbol in the same bucket in the symbol hash table */
850a9064fbSMasahiro Yamada struct symbol *next;
86*e91610daSEugeniu Rosca
87*e91610daSEugeniu Rosca /* The name of the symbol, e.g. "FOO" for 'config FOO' */
880a9064fbSMasahiro Yamada char *name;
89*e91610daSEugeniu Rosca
90*e91610daSEugeniu Rosca /* S_BOOLEAN, S_TRISTATE, ... */
910a9064fbSMasahiro Yamada enum symbol_type type;
92*e91610daSEugeniu Rosca
93*e91610daSEugeniu Rosca /*
94*e91610daSEugeniu Rosca * The calculated value of the symbol. The SYMBOL_VALID bit is set in
95*e91610daSEugeniu Rosca * 'flags' when this is up to date. Note that this value might differ
96*e91610daSEugeniu Rosca * from the user value set in e.g. a .config file, due to visibility.
97*e91610daSEugeniu Rosca */
980a9064fbSMasahiro Yamada struct symbol_value curr;
99*e91610daSEugeniu Rosca
100*e91610daSEugeniu Rosca /*
101*e91610daSEugeniu Rosca * Values for the symbol provided from outside. def[S_DEF_USER] holds
102*e91610daSEugeniu Rosca * the .config value.
103*e91610daSEugeniu Rosca */
1040a9064fbSMasahiro Yamada struct symbol_value def[S_DEF_COUNT];
105*e91610daSEugeniu Rosca
106*e91610daSEugeniu Rosca /*
107*e91610daSEugeniu Rosca * An upper bound on the tristate value the user can set for the symbol
108*e91610daSEugeniu Rosca * if it is a boolean or tristate. Calculated from prompt dependencies,
109*e91610daSEugeniu Rosca * which also inherit dependencies from enclosing menus, choices, and
110*e91610daSEugeniu Rosca * ifs. If 'n', the user value will be ignored.
111*e91610daSEugeniu Rosca *
112*e91610daSEugeniu Rosca * Symbols lacking prompts always have visibility 'n'.
113*e91610daSEugeniu Rosca */
1140a9064fbSMasahiro Yamada tristate visible;
115*e91610daSEugeniu Rosca
116*e91610daSEugeniu Rosca /* SYMBOL_* flags */
1170a9064fbSMasahiro Yamada int flags;
118*e91610daSEugeniu Rosca
119*e91610daSEugeniu Rosca /* List of properties. See prop_type. */
1200a9064fbSMasahiro Yamada struct property *prop;
121*e91610daSEugeniu Rosca
122*e91610daSEugeniu Rosca /* Dependencies from enclosing menus, choices, and ifs */
1230a9064fbSMasahiro Yamada struct expr_value dir_dep;
124*e91610daSEugeniu Rosca
125*e91610daSEugeniu Rosca /* Reverse dependencies through being selected by other symbols */
1260a9064fbSMasahiro Yamada struct expr_value rev_dep;
127*e91610daSEugeniu Rosca
128*e91610daSEugeniu Rosca /*
129*e91610daSEugeniu Rosca * "Weak" reverse dependencies through being implied by other symbols
130*e91610daSEugeniu Rosca */
131bf7ab1e7SMasahiro Yamada struct expr_value implied;
1320a9064fbSMasahiro Yamada };
1330a9064fbSMasahiro Yamada
1340a9064fbSMasahiro Yamada #define for_all_symbols(i, sym) for (i = 0; i < SYMBOL_HASHSIZE; i++) for (sym = symbol_hash[i]; sym; sym = sym->next) if (sym->type != S_OTHER)
1350a9064fbSMasahiro Yamada
1360a9064fbSMasahiro Yamada #define SYMBOL_CONST 0x0001 /* symbol is const */
1370a9064fbSMasahiro Yamada #define SYMBOL_CHECK 0x0008 /* used during dependency checking */
1380a9064fbSMasahiro Yamada #define SYMBOL_CHOICE 0x0010 /* start of a choice block (null name) */
1390a9064fbSMasahiro Yamada #define SYMBOL_CHOICEVAL 0x0020 /* used as a value in a choice block */
1400a9064fbSMasahiro Yamada #define SYMBOL_VALID 0x0080 /* set when symbol.curr is calculated */
1410a9064fbSMasahiro Yamada #define SYMBOL_OPTIONAL 0x0100 /* choice is optional - values can be 'n' */
1420a9064fbSMasahiro Yamada #define SYMBOL_WRITE 0x0200 /* write symbol to file (KCONFIG_CONFIG) */
1430a9064fbSMasahiro Yamada #define SYMBOL_CHANGED 0x0400 /* ? */
1440a9064fbSMasahiro Yamada #define SYMBOL_AUTO 0x1000 /* value from environment variable */
1450a9064fbSMasahiro Yamada #define SYMBOL_CHECKED 0x2000 /* used during dependency checking */
1460a9064fbSMasahiro Yamada #define SYMBOL_WARNED 0x8000 /* warning has been issued */
1470a9064fbSMasahiro Yamada
1480a9064fbSMasahiro Yamada /* Set when symbol.def[] is used */
1490a9064fbSMasahiro Yamada #define SYMBOL_DEF 0x10000 /* First bit of SYMBOL_DEF */
1500a9064fbSMasahiro Yamada #define SYMBOL_DEF_USER 0x10000 /* symbol.def[S_DEF_USER] is valid */
1510a9064fbSMasahiro Yamada #define SYMBOL_DEF_AUTO 0x20000 /* symbol.def[S_DEF_AUTO] is valid */
1520a9064fbSMasahiro Yamada #define SYMBOL_DEF3 0x40000 /* symbol.def[S_DEF_3] is valid */
1530a9064fbSMasahiro Yamada #define SYMBOL_DEF4 0x80000 /* symbol.def[S_DEF_4] is valid */
1540a9064fbSMasahiro Yamada
1550a9064fbSMasahiro Yamada /* choice values need to be set before calculating this symbol value */
1560a9064fbSMasahiro Yamada #define SYMBOL_NEED_SET_CHOICE_VALUES 0x100000
1570a9064fbSMasahiro Yamada
1580a9064fbSMasahiro Yamada /* Set symbol to y if allnoconfig; used for symbols that hide others */
1590a9064fbSMasahiro Yamada #define SYMBOL_ALLNOCONFIG_Y 0x200000
1600a9064fbSMasahiro Yamada
1610a9064fbSMasahiro Yamada #define SYMBOL_MAXLENGTH 256
1620a9064fbSMasahiro Yamada #define SYMBOL_HASHSIZE 9973
1630a9064fbSMasahiro Yamada
1640a9064fbSMasahiro Yamada /* A property represent the config options that can be associated
1650a9064fbSMasahiro Yamada * with a config "symbol".
1660a9064fbSMasahiro Yamada * Sample:
1670a9064fbSMasahiro Yamada * config FOO
1680a9064fbSMasahiro Yamada * default y
1690a9064fbSMasahiro Yamada * prompt "foo prompt"
1700a9064fbSMasahiro Yamada * select BAR
1710a9064fbSMasahiro Yamada * config BAZ
1720a9064fbSMasahiro Yamada * int "BAZ Value"
1730a9064fbSMasahiro Yamada * range 1..255
1740a9064fbSMasahiro Yamada */
1750a9064fbSMasahiro Yamada enum prop_type {
1760a9064fbSMasahiro Yamada P_UNKNOWN,
1770a9064fbSMasahiro Yamada P_PROMPT, /* prompt "foo prompt" or "BAZ Value" */
1780a9064fbSMasahiro Yamada P_COMMENT, /* text associated with a comment */
179*e91610daSEugeniu Rosca P_MENU, /* prompt associated with a menu or menuconfig symbol */
1800a9064fbSMasahiro Yamada P_DEFAULT, /* default y */
1810a9064fbSMasahiro Yamada P_CHOICE, /* choice value */
1820a9064fbSMasahiro Yamada P_SELECT, /* select BAR */
183bf7ab1e7SMasahiro Yamada P_IMPLY, /* imply BAR */
1840a9064fbSMasahiro Yamada P_RANGE, /* range 7..100 (for a symbol) */
1850a9064fbSMasahiro Yamada P_ENV, /* value from environment variable */
1860a9064fbSMasahiro Yamada P_SYMBOL, /* where a symbol is defined */
1870a9064fbSMasahiro Yamada };
1880a9064fbSMasahiro Yamada
1890a9064fbSMasahiro Yamada struct property {
1900a9064fbSMasahiro Yamada struct property *next; /* next property - null if last */
1910a9064fbSMasahiro Yamada struct symbol *sym; /* the symbol for which the property is associated */
1920a9064fbSMasahiro Yamada enum prop_type type; /* type of property */
1930a9064fbSMasahiro Yamada const char *text; /* the prompt value - P_PROMPT, P_MENU, P_COMMENT */
1940a9064fbSMasahiro Yamada struct expr_value visible;
1950a9064fbSMasahiro Yamada struct expr *expr; /* the optional conditional part of the property */
1960a9064fbSMasahiro Yamada struct menu *menu; /* the menu the property are associated with
1970a9064fbSMasahiro Yamada * valid for: P_SELECT, P_RANGE, P_CHOICE,
1980a9064fbSMasahiro Yamada * P_PROMPT, P_DEFAULT, P_MENU, P_COMMENT */
1990a9064fbSMasahiro Yamada struct file *file; /* what file was this property defined */
2000a9064fbSMasahiro Yamada int lineno; /* what lineno was this property defined */
2010a9064fbSMasahiro Yamada };
2020a9064fbSMasahiro Yamada
2030a9064fbSMasahiro Yamada #define for_all_properties(sym, st, tok) \
2040a9064fbSMasahiro Yamada for (st = sym->prop; st; st = st->next) \
2050a9064fbSMasahiro Yamada if (st->type == (tok))
2060a9064fbSMasahiro Yamada #define for_all_defaults(sym, st) for_all_properties(sym, st, P_DEFAULT)
2070a9064fbSMasahiro Yamada #define for_all_choices(sym, st) for_all_properties(sym, st, P_CHOICE)
2080a9064fbSMasahiro Yamada #define for_all_prompts(sym, st) \
2090a9064fbSMasahiro Yamada for (st = sym->prop; st; st = st->next) \
2100a9064fbSMasahiro Yamada if (st->text)
2110a9064fbSMasahiro Yamada
212*e91610daSEugeniu Rosca /*
213*e91610daSEugeniu Rosca * Represents a node in the menu tree, as seen in e.g. menuconfig (though used
214*e91610daSEugeniu Rosca * for all front ends). Each symbol, menu, etc. defined in the Kconfig files
215*e91610daSEugeniu Rosca * gets a node. A symbol defined in multiple locations gets one node at each
216*e91610daSEugeniu Rosca * location.
217*e91610daSEugeniu Rosca */
2180a9064fbSMasahiro Yamada struct menu {
219*e91610daSEugeniu Rosca /* The next menu node at the same level */
2200a9064fbSMasahiro Yamada struct menu *next;
221*e91610daSEugeniu Rosca
222*e91610daSEugeniu Rosca /* The parent menu node, corresponding to e.g. a menu or choice */
2230a9064fbSMasahiro Yamada struct menu *parent;
224*e91610daSEugeniu Rosca
225*e91610daSEugeniu Rosca /* The first child menu node, for e.g. menus and choices */
2260a9064fbSMasahiro Yamada struct menu *list;
227*e91610daSEugeniu Rosca
228*e91610daSEugeniu Rosca /*
229*e91610daSEugeniu Rosca * The symbol associated with the menu node. Choices are implemented as
230*e91610daSEugeniu Rosca * a special kind of symbol. NULL for menus, comments, and ifs.
231*e91610daSEugeniu Rosca */
2320a9064fbSMasahiro Yamada struct symbol *sym;
233*e91610daSEugeniu Rosca
234*e91610daSEugeniu Rosca /*
235*e91610daSEugeniu Rosca * The prompt associated with the node. This holds the prompt for a
236*e91610daSEugeniu Rosca * symbol as well as the text for a menu or comment, along with the
237*e91610daSEugeniu Rosca * type (P_PROMPT, P_MENU, etc.)
238*e91610daSEugeniu Rosca */
2390a9064fbSMasahiro Yamada struct property *prompt;
240*e91610daSEugeniu Rosca
241*e91610daSEugeniu Rosca /*
242*e91610daSEugeniu Rosca * 'visible if' dependencies. If more than one is given, they will be
243*e91610daSEugeniu Rosca * ANDed together.
244*e91610daSEugeniu Rosca */
2450a9064fbSMasahiro Yamada struct expr *visibility;
246*e91610daSEugeniu Rosca
247*e91610daSEugeniu Rosca /*
248*e91610daSEugeniu Rosca * Ordinary dependencies from e.g. 'depends on' and 'if', ANDed
249*e91610daSEugeniu Rosca * together
250*e91610daSEugeniu Rosca */
2510a9064fbSMasahiro Yamada struct expr *dep;
252*e91610daSEugeniu Rosca
253*e91610daSEugeniu Rosca /* MENU_* flags */
2540a9064fbSMasahiro Yamada unsigned int flags;
255*e91610daSEugeniu Rosca
256*e91610daSEugeniu Rosca /* Any help text associated with the node */
2570a9064fbSMasahiro Yamada char *help;
258*e91610daSEugeniu Rosca
259*e91610daSEugeniu Rosca /* The location where the menu node appears in the Kconfig files */
2600a9064fbSMasahiro Yamada struct file *file;
2610a9064fbSMasahiro Yamada int lineno;
262*e91610daSEugeniu Rosca
263*e91610daSEugeniu Rosca /* For use by front ends that need to store auxiliary data */
2640a9064fbSMasahiro Yamada void *data;
2650a9064fbSMasahiro Yamada };
2660a9064fbSMasahiro Yamada
267*e91610daSEugeniu Rosca /*
268*e91610daSEugeniu Rosca * Set on a menu node when the corresponding symbol changes state in some way.
269*e91610daSEugeniu Rosca * Can be checked by front ends.
270*e91610daSEugeniu Rosca */
2710a9064fbSMasahiro Yamada #define MENU_CHANGED 0x0001
272*e91610daSEugeniu Rosca
2730a9064fbSMasahiro Yamada #define MENU_ROOT 0x0002
2740a9064fbSMasahiro Yamada
2750a9064fbSMasahiro Yamada struct jump_key {
2760a9064fbSMasahiro Yamada struct list_head entries;
2770a9064fbSMasahiro Yamada size_t offset;
2780a9064fbSMasahiro Yamada struct menu *target;
2790a9064fbSMasahiro Yamada int index;
2800a9064fbSMasahiro Yamada };
2810a9064fbSMasahiro Yamada
2820a9064fbSMasahiro Yamada #define JUMP_NB 9
2830a9064fbSMasahiro Yamada
2840a9064fbSMasahiro Yamada extern struct file *file_list;
2850a9064fbSMasahiro Yamada extern struct file *current_file;
2860a9064fbSMasahiro Yamada struct file *lookup_file(const char *name);
2870a9064fbSMasahiro Yamada
2880a9064fbSMasahiro Yamada extern struct symbol symbol_yes, symbol_no, symbol_mod;
2890a9064fbSMasahiro Yamada extern struct symbol *modules_sym;
2900a9064fbSMasahiro Yamada extern struct symbol *sym_defconfig_list;
2910a9064fbSMasahiro Yamada extern int cdebug;
2920a9064fbSMasahiro Yamada struct expr *expr_alloc_symbol(struct symbol *sym);
2930a9064fbSMasahiro Yamada struct expr *expr_alloc_one(enum expr_type type, struct expr *ce);
2940a9064fbSMasahiro Yamada struct expr *expr_alloc_two(enum expr_type type, struct expr *e1, struct expr *e2);
2950a9064fbSMasahiro Yamada struct expr *expr_alloc_comp(enum expr_type type, struct symbol *s1, struct symbol *s2);
2960a9064fbSMasahiro Yamada struct expr *expr_alloc_and(struct expr *e1, struct expr *e2);
2970a9064fbSMasahiro Yamada struct expr *expr_alloc_or(struct expr *e1, struct expr *e2);
2980a9064fbSMasahiro Yamada struct expr *expr_copy(const struct expr *org);
2990a9064fbSMasahiro Yamada void expr_free(struct expr *e);
3000a9064fbSMasahiro Yamada void expr_eliminate_eq(struct expr **ep1, struct expr **ep2);
3010a9064fbSMasahiro Yamada tristate expr_calc_value(struct expr *e);
3020a9064fbSMasahiro Yamada struct expr *expr_trans_bool(struct expr *e);
3030a9064fbSMasahiro Yamada struct expr *expr_eliminate_dups(struct expr *e);
3040a9064fbSMasahiro Yamada struct expr *expr_transform(struct expr *e);
3050a9064fbSMasahiro Yamada int expr_contains_symbol(struct expr *dep, struct symbol *sym);
3060a9064fbSMasahiro Yamada bool expr_depends_symbol(struct expr *dep, struct symbol *sym);
3070a9064fbSMasahiro Yamada struct expr *expr_trans_compare(struct expr *e, enum expr_type type, struct symbol *sym);
3080a9064fbSMasahiro Yamada
3090a9064fbSMasahiro Yamada void expr_fprint(struct expr *e, FILE *out);
3100a9064fbSMasahiro Yamada struct gstr; /* forward */
3110a9064fbSMasahiro Yamada void expr_gstr_print(struct expr *e, struct gstr *gs);
312*e91610daSEugeniu Rosca void expr_gstr_print_revdep(struct expr *e, struct gstr *gs,
313*e91610daSEugeniu Rosca tristate pr_type, const char *title);
3140a9064fbSMasahiro Yamada
expr_is_yes(struct expr * e)3150a9064fbSMasahiro Yamada static inline int expr_is_yes(struct expr *e)
3160a9064fbSMasahiro Yamada {
3170a9064fbSMasahiro Yamada return !e || (e->type == E_SYMBOL && e->left.sym == &symbol_yes);
3180a9064fbSMasahiro Yamada }
3190a9064fbSMasahiro Yamada
expr_is_no(struct expr * e)3200a9064fbSMasahiro Yamada static inline int expr_is_no(struct expr *e)
3210a9064fbSMasahiro Yamada {
3220a9064fbSMasahiro Yamada return e && (e->type == E_SYMBOL && e->left.sym == &symbol_no);
3230a9064fbSMasahiro Yamada }
3240a9064fbSMasahiro Yamada
3250a9064fbSMasahiro Yamada #ifdef __cplusplus
3260a9064fbSMasahiro Yamada }
3270a9064fbSMasahiro Yamada #endif
3280a9064fbSMasahiro Yamada
3290a9064fbSMasahiro Yamada #endif /* EXPR_H */
330