1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3*4882a593Smuzhiyun * Released under the terms of the GNU GPL v2.0.
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #ifndef EXPR_H
7*4882a593Smuzhiyun #define EXPR_H
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #ifdef __cplusplus
10*4882a593Smuzhiyun extern "C" {
11*4882a593Smuzhiyun #endif
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <assert.h>
14*4882a593Smuzhiyun #include <stdio.h>
15*4882a593Smuzhiyun #include "list.h"
16*4882a593Smuzhiyun #ifndef __cplusplus
17*4882a593Smuzhiyun #include <stdbool.h>
18*4882a593Smuzhiyun #endif
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun struct file {
21*4882a593Smuzhiyun struct file *next;
22*4882a593Smuzhiyun struct file *parent;
23*4882a593Smuzhiyun const char *name;
24*4882a593Smuzhiyun int lineno;
25*4882a593Smuzhiyun };
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun typedef enum tristate {
28*4882a593Smuzhiyun no, mod, yes
29*4882a593Smuzhiyun } tristate;
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun enum expr_type {
32*4882a593Smuzhiyun E_NONE, E_OR, E_AND, E_NOT,
33*4882a593Smuzhiyun E_EQUAL, E_UNEQUAL, E_LTH, E_LEQ, E_GTH, E_GEQ,
34*4882a593Smuzhiyun E_LIST, E_SYMBOL, E_RANGE
35*4882a593Smuzhiyun };
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun union expr_data {
38*4882a593Smuzhiyun struct expr *expr;
39*4882a593Smuzhiyun struct symbol *sym;
40*4882a593Smuzhiyun };
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun struct expr {
43*4882a593Smuzhiyun enum expr_type type;
44*4882a593Smuzhiyun union expr_data left, right;
45*4882a593Smuzhiyun };
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun #define EXPR_OR(dep1, dep2) (((dep1)>(dep2))?(dep1):(dep2))
48*4882a593Smuzhiyun #define EXPR_AND(dep1, dep2) (((dep1)<(dep2))?(dep1):(dep2))
49*4882a593Smuzhiyun #define EXPR_NOT(dep) (2-(dep))
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun #define expr_list_for_each_sym(l, e, s) \
52*4882a593Smuzhiyun for (e = (l); e && (s = e->right.sym); e = e->left.expr)
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun struct expr_value {
55*4882a593Smuzhiyun struct expr *expr;
56*4882a593Smuzhiyun tristate tri;
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun struct symbol_value {
60*4882a593Smuzhiyun void *val;
61*4882a593Smuzhiyun tristate tri;
62*4882a593Smuzhiyun };
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun enum symbol_type {
65*4882a593Smuzhiyun S_UNKNOWN, S_BOOLEAN, S_TRISTATE, S_INT, S_HEX, S_STRING, S_OTHER
66*4882a593Smuzhiyun };
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /* enum values are used as index to symbol.def[] */
69*4882a593Smuzhiyun enum {
70*4882a593Smuzhiyun S_DEF_USER, /* main user value */
71*4882a593Smuzhiyun S_DEF_AUTO, /* values read from auto.conf */
72*4882a593Smuzhiyun S_DEF_DEF3, /* Reserved for UI usage */
73*4882a593Smuzhiyun S_DEF_DEF4, /* Reserved for UI usage */
74*4882a593Smuzhiyun S_DEF_COUNT
75*4882a593Smuzhiyun };
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /*
78*4882a593Smuzhiyun * Represents a configuration symbol.
79*4882a593Smuzhiyun *
80*4882a593Smuzhiyun * Choices are represented as a special kind of symbol and have the
81*4882a593Smuzhiyun * SYMBOL_CHOICE bit set in 'flags'.
82*4882a593Smuzhiyun */
83*4882a593Smuzhiyun struct symbol {
84*4882a593Smuzhiyun /* The next symbol in the same bucket in the symbol hash table */
85*4882a593Smuzhiyun struct symbol *next;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* The name of the symbol, e.g. "FOO" for 'config FOO' */
88*4882a593Smuzhiyun char *name;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* S_BOOLEAN, S_TRISTATE, ... */
91*4882a593Smuzhiyun enum symbol_type type;
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /*
94*4882a593Smuzhiyun * The calculated value of the symbol. The SYMBOL_VALID bit is set in
95*4882a593Smuzhiyun * 'flags' when this is up to date. Note that this value might differ
96*4882a593Smuzhiyun * from the user value set in e.g. a .config file, due to visibility.
97*4882a593Smuzhiyun */
98*4882a593Smuzhiyun struct symbol_value curr;
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /*
101*4882a593Smuzhiyun * Values for the symbol provided from outside. def[S_DEF_USER] holds
102*4882a593Smuzhiyun * the .config value.
103*4882a593Smuzhiyun */
104*4882a593Smuzhiyun struct symbol_value def[S_DEF_COUNT];
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun /*
107*4882a593Smuzhiyun * An upper bound on the tristate value the user can set for the symbol
108*4882a593Smuzhiyun * if it is a boolean or tristate. Calculated from prompt dependencies,
109*4882a593Smuzhiyun * which also inherit dependencies from enclosing menus, choices, and
110*4882a593Smuzhiyun * ifs. If 'n', the user value will be ignored.
111*4882a593Smuzhiyun *
112*4882a593Smuzhiyun * Symbols lacking prompts always have visibility 'n'.
113*4882a593Smuzhiyun */
114*4882a593Smuzhiyun tristate visible;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun /* SYMBOL_* flags */
117*4882a593Smuzhiyun int flags;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /* List of properties. See prop_type. */
120*4882a593Smuzhiyun struct property *prop;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /* Dependencies from enclosing menus, choices, and ifs */
123*4882a593Smuzhiyun struct expr_value dir_dep;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun /* Reverse dependencies through being selected by other symbols */
126*4882a593Smuzhiyun struct expr_value rev_dep;
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /*
129*4882a593Smuzhiyun * "Weak" reverse dependencies through being implied by other symbols
130*4882a593Smuzhiyun */
131*4882a593Smuzhiyun struct expr_value implied;
132*4882a593Smuzhiyun };
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun #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)
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun #define SYMBOL_CONST 0x0001 /* symbol is const */
137*4882a593Smuzhiyun #define SYMBOL_CHECK 0x0008 /* used during dependency checking */
138*4882a593Smuzhiyun #define SYMBOL_CHOICE 0x0010 /* start of a choice block (null name) */
139*4882a593Smuzhiyun #define SYMBOL_CHOICEVAL 0x0020 /* used as a value in a choice block */
140*4882a593Smuzhiyun #define SYMBOL_VALID 0x0080 /* set when symbol.curr is calculated */
141*4882a593Smuzhiyun #define SYMBOL_OPTIONAL 0x0100 /* choice is optional - values can be 'n' */
142*4882a593Smuzhiyun #define SYMBOL_WRITE 0x0200 /* write symbol to file (KCONFIG_CONFIG) */
143*4882a593Smuzhiyun #define SYMBOL_CHANGED 0x0400 /* ? */
144*4882a593Smuzhiyun #define SYMBOL_AUTO 0x1000 /* value from environment variable */
145*4882a593Smuzhiyun #define SYMBOL_CHECKED 0x2000 /* used during dependency checking */
146*4882a593Smuzhiyun #define SYMBOL_WARNED 0x8000 /* warning has been issued */
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /* Set when symbol.def[] is used */
149*4882a593Smuzhiyun #define SYMBOL_DEF 0x10000 /* First bit of SYMBOL_DEF */
150*4882a593Smuzhiyun #define SYMBOL_DEF_USER 0x10000 /* symbol.def[S_DEF_USER] is valid */
151*4882a593Smuzhiyun #define SYMBOL_DEF_AUTO 0x20000 /* symbol.def[S_DEF_AUTO] is valid */
152*4882a593Smuzhiyun #define SYMBOL_DEF3 0x40000 /* symbol.def[S_DEF_3] is valid */
153*4882a593Smuzhiyun #define SYMBOL_DEF4 0x80000 /* symbol.def[S_DEF_4] is valid */
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /* choice values need to be set before calculating this symbol value */
156*4882a593Smuzhiyun #define SYMBOL_NEED_SET_CHOICE_VALUES 0x100000
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /* Set symbol to y if allnoconfig; used for symbols that hide others */
159*4882a593Smuzhiyun #define SYMBOL_ALLNOCONFIG_Y 0x200000
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun #define SYMBOL_MAXLENGTH 256
162*4882a593Smuzhiyun #define SYMBOL_HASHSIZE 9973
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /* A property represent the config options that can be associated
165*4882a593Smuzhiyun * with a config "symbol".
166*4882a593Smuzhiyun * Sample:
167*4882a593Smuzhiyun * config FOO
168*4882a593Smuzhiyun * default y
169*4882a593Smuzhiyun * prompt "foo prompt"
170*4882a593Smuzhiyun * select BAR
171*4882a593Smuzhiyun * config BAZ
172*4882a593Smuzhiyun * int "BAZ Value"
173*4882a593Smuzhiyun * range 1..255
174*4882a593Smuzhiyun */
175*4882a593Smuzhiyun enum prop_type {
176*4882a593Smuzhiyun P_UNKNOWN,
177*4882a593Smuzhiyun P_PROMPT, /* prompt "foo prompt" or "BAZ Value" */
178*4882a593Smuzhiyun P_COMMENT, /* text associated with a comment */
179*4882a593Smuzhiyun P_MENU, /* prompt associated with a menu or menuconfig symbol */
180*4882a593Smuzhiyun P_DEFAULT, /* default y */
181*4882a593Smuzhiyun P_CHOICE, /* choice value */
182*4882a593Smuzhiyun P_SELECT, /* select BAR */
183*4882a593Smuzhiyun P_IMPLY, /* imply BAR */
184*4882a593Smuzhiyun P_RANGE, /* range 7..100 (for a symbol) */
185*4882a593Smuzhiyun P_ENV, /* value from environment variable */
186*4882a593Smuzhiyun P_SYMBOL, /* where a symbol is defined */
187*4882a593Smuzhiyun };
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun struct property {
190*4882a593Smuzhiyun struct property *next; /* next property - null if last */
191*4882a593Smuzhiyun struct symbol *sym; /* the symbol for which the property is associated */
192*4882a593Smuzhiyun enum prop_type type; /* type of property */
193*4882a593Smuzhiyun const char *text; /* the prompt value - P_PROMPT, P_MENU, P_COMMENT */
194*4882a593Smuzhiyun struct expr_value visible;
195*4882a593Smuzhiyun struct expr *expr; /* the optional conditional part of the property */
196*4882a593Smuzhiyun struct menu *menu; /* the menu the property are associated with
197*4882a593Smuzhiyun * valid for: P_SELECT, P_RANGE, P_CHOICE,
198*4882a593Smuzhiyun * P_PROMPT, P_DEFAULT, P_MENU, P_COMMENT */
199*4882a593Smuzhiyun struct file *file; /* what file was this property defined */
200*4882a593Smuzhiyun int lineno; /* what lineno was this property defined */
201*4882a593Smuzhiyun };
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun #define for_all_properties(sym, st, tok) \
204*4882a593Smuzhiyun for (st = sym->prop; st; st = st->next) \
205*4882a593Smuzhiyun if (st->type == (tok))
206*4882a593Smuzhiyun #define for_all_defaults(sym, st) for_all_properties(sym, st, P_DEFAULT)
207*4882a593Smuzhiyun #define for_all_choices(sym, st) for_all_properties(sym, st, P_CHOICE)
208*4882a593Smuzhiyun #define for_all_prompts(sym, st) \
209*4882a593Smuzhiyun for (st = sym->prop; st; st = st->next) \
210*4882a593Smuzhiyun if (st->text)
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun /*
213*4882a593Smuzhiyun * Represents a node in the menu tree, as seen in e.g. menuconfig (though used
214*4882a593Smuzhiyun * for all front ends). Each symbol, menu, etc. defined in the Kconfig files
215*4882a593Smuzhiyun * gets a node. A symbol defined in multiple locations gets one node at each
216*4882a593Smuzhiyun * location.
217*4882a593Smuzhiyun */
218*4882a593Smuzhiyun struct menu {
219*4882a593Smuzhiyun /* The next menu node at the same level */
220*4882a593Smuzhiyun struct menu *next;
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /* The parent menu node, corresponding to e.g. a menu or choice */
223*4882a593Smuzhiyun struct menu *parent;
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun /* The first child menu node, for e.g. menus and choices */
226*4882a593Smuzhiyun struct menu *list;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun /*
229*4882a593Smuzhiyun * The symbol associated with the menu node. Choices are implemented as
230*4882a593Smuzhiyun * a special kind of symbol. NULL for menus, comments, and ifs.
231*4882a593Smuzhiyun */
232*4882a593Smuzhiyun struct symbol *sym;
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun /*
235*4882a593Smuzhiyun * The prompt associated with the node. This holds the prompt for a
236*4882a593Smuzhiyun * symbol as well as the text for a menu or comment, along with the
237*4882a593Smuzhiyun * type (P_PROMPT, P_MENU, etc.)
238*4882a593Smuzhiyun */
239*4882a593Smuzhiyun struct property *prompt;
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun /*
242*4882a593Smuzhiyun * 'visible if' dependencies. If more than one is given, they will be
243*4882a593Smuzhiyun * ANDed together.
244*4882a593Smuzhiyun */
245*4882a593Smuzhiyun struct expr *visibility;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun /*
248*4882a593Smuzhiyun * Ordinary dependencies from e.g. 'depends on' and 'if', ANDed
249*4882a593Smuzhiyun * together
250*4882a593Smuzhiyun */
251*4882a593Smuzhiyun struct expr *dep;
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun /* MENU_* flags */
254*4882a593Smuzhiyun unsigned int flags;
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun /* Any help text associated with the node */
257*4882a593Smuzhiyun char *help;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun /* The location where the menu node appears in the Kconfig files */
260*4882a593Smuzhiyun struct file *file;
261*4882a593Smuzhiyun int lineno;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun /* For use by front ends that need to store auxiliary data */
264*4882a593Smuzhiyun void *data;
265*4882a593Smuzhiyun };
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun /*
268*4882a593Smuzhiyun * Set on a menu node when the corresponding symbol changes state in some way.
269*4882a593Smuzhiyun * Can be checked by front ends.
270*4882a593Smuzhiyun */
271*4882a593Smuzhiyun #define MENU_CHANGED 0x0001
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun #define MENU_ROOT 0x0002
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun struct jump_key {
276*4882a593Smuzhiyun struct list_head entries;
277*4882a593Smuzhiyun size_t offset;
278*4882a593Smuzhiyun struct menu *target;
279*4882a593Smuzhiyun int index;
280*4882a593Smuzhiyun };
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun #define JUMP_NB 9
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun extern struct file *file_list;
285*4882a593Smuzhiyun extern struct file *current_file;
286*4882a593Smuzhiyun struct file *lookup_file(const char *name);
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun extern struct symbol symbol_yes, symbol_no, symbol_mod;
289*4882a593Smuzhiyun extern struct symbol *modules_sym;
290*4882a593Smuzhiyun extern struct symbol *sym_defconfig_list;
291*4882a593Smuzhiyun extern int cdebug;
292*4882a593Smuzhiyun struct expr *expr_alloc_symbol(struct symbol *sym);
293*4882a593Smuzhiyun struct expr *expr_alloc_one(enum expr_type type, struct expr *ce);
294*4882a593Smuzhiyun struct expr *expr_alloc_two(enum expr_type type, struct expr *e1, struct expr *e2);
295*4882a593Smuzhiyun struct expr *expr_alloc_comp(enum expr_type type, struct symbol *s1, struct symbol *s2);
296*4882a593Smuzhiyun struct expr *expr_alloc_and(struct expr *e1, struct expr *e2);
297*4882a593Smuzhiyun struct expr *expr_alloc_or(struct expr *e1, struct expr *e2);
298*4882a593Smuzhiyun struct expr *expr_copy(const struct expr *org);
299*4882a593Smuzhiyun void expr_free(struct expr *e);
300*4882a593Smuzhiyun void expr_eliminate_eq(struct expr **ep1, struct expr **ep2);
301*4882a593Smuzhiyun tristate expr_calc_value(struct expr *e);
302*4882a593Smuzhiyun struct expr *expr_trans_bool(struct expr *e);
303*4882a593Smuzhiyun struct expr *expr_eliminate_dups(struct expr *e);
304*4882a593Smuzhiyun struct expr *expr_transform(struct expr *e);
305*4882a593Smuzhiyun int expr_contains_symbol(struct expr *dep, struct symbol *sym);
306*4882a593Smuzhiyun bool expr_depends_symbol(struct expr *dep, struct symbol *sym);
307*4882a593Smuzhiyun struct expr *expr_trans_compare(struct expr *e, enum expr_type type, struct symbol *sym);
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun void expr_fprint(struct expr *e, FILE *out);
310*4882a593Smuzhiyun struct gstr; /* forward */
311*4882a593Smuzhiyun void expr_gstr_print(struct expr *e, struct gstr *gs);
312*4882a593Smuzhiyun void expr_gstr_print_revdep(struct expr *e, struct gstr *gs,
313*4882a593Smuzhiyun tristate pr_type, const char *title);
314*4882a593Smuzhiyun
expr_is_yes(struct expr * e)315*4882a593Smuzhiyun static inline int expr_is_yes(struct expr *e)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun return !e || (e->type == E_SYMBOL && e->left.sym == &symbol_yes);
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun
expr_is_no(struct expr * e)320*4882a593Smuzhiyun static inline int expr_is_no(struct expr *e)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun return e && (e->type == E_SYMBOL && e->left.sym == &symbol_no);
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun #ifdef __cplusplus
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun #endif
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun #endif /* EXPR_H */
330