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