xref: /OK3568_Linux_fs/kernel/scripts/kconfig/expr.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
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
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)
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_WRITTEN    0x0800  /* track info to avoid double-write to .config */
145*4882a593Smuzhiyun #define SYMBOL_NO_WRITE   0x1000  /* Symbol for internal use only; it will not be written */
146*4882a593Smuzhiyun #define SYMBOL_CHECKED    0x2000  /* used during dependency checking */
147*4882a593Smuzhiyun #define SYMBOL_WARNED     0x8000  /* warning has been issued */
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun /* Set when symbol.def[] is used */
150*4882a593Smuzhiyun #define SYMBOL_DEF        0x10000  /* First bit of SYMBOL_DEF */
151*4882a593Smuzhiyun #define SYMBOL_DEF_USER   0x10000  /* symbol.def[S_DEF_USER] is valid */
152*4882a593Smuzhiyun #define SYMBOL_DEF_AUTO   0x20000  /* symbol.def[S_DEF_AUTO] is valid */
153*4882a593Smuzhiyun #define SYMBOL_DEF3       0x40000  /* symbol.def[S_DEF_3] is valid */
154*4882a593Smuzhiyun #define SYMBOL_DEF4       0x80000  /* symbol.def[S_DEF_4] is valid */
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun /* choice values need to be set before calculating this symbol value */
157*4882a593Smuzhiyun #define SYMBOL_NEED_SET_CHOICE_VALUES  0x100000
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun /* Set symbol to y if allnoconfig; used for symbols that hide others */
160*4882a593Smuzhiyun #define SYMBOL_ALLNOCONFIG_Y 0x200000
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun #define SYMBOL_MAXLENGTH	256
163*4882a593Smuzhiyun #define SYMBOL_HASHSIZE		9973
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun /* A property represent the config options that can be associated
166*4882a593Smuzhiyun  * with a config "symbol".
167*4882a593Smuzhiyun  * Sample:
168*4882a593Smuzhiyun  * config FOO
169*4882a593Smuzhiyun  *         default y
170*4882a593Smuzhiyun  *         prompt "foo prompt"
171*4882a593Smuzhiyun  *         select BAR
172*4882a593Smuzhiyun  * config BAZ
173*4882a593Smuzhiyun  *         int "BAZ Value"
174*4882a593Smuzhiyun  *         range 1..255
175*4882a593Smuzhiyun  *
176*4882a593Smuzhiyun  * Please, also check parser.y:print_symbol() when modifying the
177*4882a593Smuzhiyun  * list of property types!
178*4882a593Smuzhiyun  */
179*4882a593Smuzhiyun enum prop_type {
180*4882a593Smuzhiyun 	P_UNKNOWN,
181*4882a593Smuzhiyun 	P_PROMPT,   /* prompt "foo prompt" or "BAZ Value" */
182*4882a593Smuzhiyun 	P_COMMENT,  /* text associated with a comment */
183*4882a593Smuzhiyun 	P_MENU,     /* prompt associated with a menu or menuconfig symbol */
184*4882a593Smuzhiyun 	P_DEFAULT,  /* default y */
185*4882a593Smuzhiyun 	P_CHOICE,   /* choice value */
186*4882a593Smuzhiyun 	P_SELECT,   /* select BAR */
187*4882a593Smuzhiyun 	P_IMPLY,    /* imply BAR */
188*4882a593Smuzhiyun 	P_RANGE,    /* range 7..100 (for a symbol) */
189*4882a593Smuzhiyun 	P_SYMBOL,   /* where a symbol is defined */
190*4882a593Smuzhiyun };
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun struct property {
193*4882a593Smuzhiyun 	struct property *next;     /* next property - null if last */
194*4882a593Smuzhiyun 	enum prop_type type;       /* type of property */
195*4882a593Smuzhiyun 	const char *text;          /* the prompt value - P_PROMPT, P_MENU, P_COMMENT */
196*4882a593Smuzhiyun 	struct expr_value visible;
197*4882a593Smuzhiyun 	struct expr *expr;         /* the optional conditional part of the property */
198*4882a593Smuzhiyun 	struct menu *menu;         /* the menu the property are associated with
199*4882a593Smuzhiyun 	                            * valid for: P_SELECT, P_RANGE, P_CHOICE,
200*4882a593Smuzhiyun 	                            * P_PROMPT, P_DEFAULT, P_MENU, P_COMMENT */
201*4882a593Smuzhiyun 	struct file *file;         /* what file was this property defined */
202*4882a593Smuzhiyun 	int lineno;                /* what lineno was this property defined */
203*4882a593Smuzhiyun };
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun #define for_all_properties(sym, st, tok) \
206*4882a593Smuzhiyun 	for (st = sym->prop; st; st = st->next) \
207*4882a593Smuzhiyun 		if (st->type == (tok))
208*4882a593Smuzhiyun #define for_all_defaults(sym, st) for_all_properties(sym, st, P_DEFAULT)
209*4882a593Smuzhiyun #define for_all_choices(sym, st) for_all_properties(sym, st, P_CHOICE)
210*4882a593Smuzhiyun #define for_all_prompts(sym, st) \
211*4882a593Smuzhiyun 	for (st = sym->prop; st; st = st->next) \
212*4882a593Smuzhiyun 		if (st->text)
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun /*
215*4882a593Smuzhiyun  * Represents a node in the menu tree, as seen in e.g. menuconfig (though used
216*4882a593Smuzhiyun  * for all front ends). Each symbol, menu, etc. defined in the Kconfig files
217*4882a593Smuzhiyun  * gets a node. A symbol defined in multiple locations gets one node at each
218*4882a593Smuzhiyun  * location.
219*4882a593Smuzhiyun  */
220*4882a593Smuzhiyun struct menu {
221*4882a593Smuzhiyun 	/* The next menu node at the same level */
222*4882a593Smuzhiyun 	struct menu *next;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	/* The parent menu node, corresponding to e.g. a menu or choice */
225*4882a593Smuzhiyun 	struct menu *parent;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	/* The first child menu node, for e.g. menus and choices */
228*4882a593Smuzhiyun 	struct menu *list;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	/*
231*4882a593Smuzhiyun 	 * The symbol associated with the menu node. Choices are implemented as
232*4882a593Smuzhiyun 	 * a special kind of symbol. NULL for menus, comments, and ifs.
233*4882a593Smuzhiyun 	 */
234*4882a593Smuzhiyun 	struct symbol *sym;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	/*
237*4882a593Smuzhiyun 	 * The prompt associated with the node. This holds the prompt for a
238*4882a593Smuzhiyun 	 * symbol as well as the text for a menu or comment, along with the
239*4882a593Smuzhiyun 	 * type (P_PROMPT, P_MENU, etc.)
240*4882a593Smuzhiyun 	 */
241*4882a593Smuzhiyun 	struct property *prompt;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	/*
244*4882a593Smuzhiyun 	 * 'visible if' dependencies. If more than one is given, they will be
245*4882a593Smuzhiyun 	 * ANDed together.
246*4882a593Smuzhiyun 	 */
247*4882a593Smuzhiyun 	struct expr *visibility;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	/*
250*4882a593Smuzhiyun 	 * Ordinary dependencies from e.g. 'depends on' and 'if', ANDed
251*4882a593Smuzhiyun 	 * together
252*4882a593Smuzhiyun 	 */
253*4882a593Smuzhiyun 	struct expr *dep;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	/* MENU_* flags */
256*4882a593Smuzhiyun 	unsigned int flags;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	/* Any help text associated with the node */
259*4882a593Smuzhiyun 	char *help;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	/* The location where the menu node appears in the Kconfig files */
262*4882a593Smuzhiyun 	struct file *file;
263*4882a593Smuzhiyun 	int lineno;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	/* For use by front ends that need to store auxiliary data */
266*4882a593Smuzhiyun 	void *data;
267*4882a593Smuzhiyun };
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun /*
270*4882a593Smuzhiyun  * Set on a menu node when the corresponding symbol changes state in some way.
271*4882a593Smuzhiyun  * Can be checked by front ends.
272*4882a593Smuzhiyun  */
273*4882a593Smuzhiyun #define MENU_CHANGED		0x0001
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun #define MENU_ROOT		0x0002
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun struct jump_key {
278*4882a593Smuzhiyun 	struct list_head entries;
279*4882a593Smuzhiyun 	size_t offset;
280*4882a593Smuzhiyun 	struct menu *target;
281*4882a593Smuzhiyun 	int index;
282*4882a593Smuzhiyun };
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun #define JUMP_NB			9
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun extern struct file *file_list;
287*4882a593Smuzhiyun extern struct file *current_file;
288*4882a593Smuzhiyun struct file *lookup_file(const char *name);
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun extern struct symbol symbol_yes, symbol_no, symbol_mod;
291*4882a593Smuzhiyun extern struct symbol *modules_sym;
292*4882a593Smuzhiyun extern struct symbol *sym_defconfig_list;
293*4882a593Smuzhiyun extern int cdebug;
294*4882a593Smuzhiyun struct expr *expr_alloc_symbol(struct symbol *sym);
295*4882a593Smuzhiyun struct expr *expr_alloc_one(enum expr_type type, struct expr *ce);
296*4882a593Smuzhiyun struct expr *expr_alloc_two(enum expr_type type, struct expr *e1, struct expr *e2);
297*4882a593Smuzhiyun struct expr *expr_alloc_comp(enum expr_type type, struct symbol *s1, struct symbol *s2);
298*4882a593Smuzhiyun struct expr *expr_alloc_and(struct expr *e1, struct expr *e2);
299*4882a593Smuzhiyun struct expr *expr_alloc_or(struct expr *e1, struct expr *e2);
300*4882a593Smuzhiyun struct expr *expr_copy(const struct expr *org);
301*4882a593Smuzhiyun void expr_free(struct expr *e);
302*4882a593Smuzhiyun void expr_eliminate_eq(struct expr **ep1, struct expr **ep2);
303*4882a593Smuzhiyun int expr_eq(struct expr *e1, struct expr *e2);
304*4882a593Smuzhiyun tristate expr_calc_value(struct expr *e);
305*4882a593Smuzhiyun struct expr *expr_trans_bool(struct expr *e);
306*4882a593Smuzhiyun struct expr *expr_eliminate_dups(struct expr *e);
307*4882a593Smuzhiyun struct expr *expr_transform(struct expr *e);
308*4882a593Smuzhiyun int expr_contains_symbol(struct expr *dep, struct symbol *sym);
309*4882a593Smuzhiyun bool expr_depends_symbol(struct expr *dep, struct symbol *sym);
310*4882a593Smuzhiyun struct expr *expr_trans_compare(struct expr *e, enum expr_type type, struct symbol *sym);
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun void expr_fprint(struct expr *e, FILE *out);
313*4882a593Smuzhiyun struct gstr; /* forward */
314*4882a593Smuzhiyun void expr_gstr_print(struct expr *e, struct gstr *gs);
315*4882a593Smuzhiyun void expr_gstr_print_revdep(struct expr *e, struct gstr *gs,
316*4882a593Smuzhiyun 			    tristate pr_type, const char *title);
317*4882a593Smuzhiyun 
expr_is_yes(struct expr * e)318*4882a593Smuzhiyun static inline int expr_is_yes(struct expr *e)
319*4882a593Smuzhiyun {
320*4882a593Smuzhiyun 	return !e || (e->type == E_SYMBOL && e->left.sym == &symbol_yes);
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun 
expr_is_no(struct expr * e)323*4882a593Smuzhiyun static inline int expr_is_no(struct expr *e)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun 	return e && (e->type == E_SYMBOL && e->left.sym == &symbol_no);
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun #ifdef __cplusplus
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun #endif
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun #endif /* EXPR_H */
333