xref: /linux-6.15/scripts/kconfig/expr.h (revision 95573cac)
10c874100SMasahiro Yamada /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Copyright (C) 2002 Roman Zippel <[email protected]>
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #ifndef EXPR_H
71da177e4SLinus Torvalds #define EXPR_H
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #ifdef __cplusplus
101da177e4SLinus Torvalds extern "C" {
111da177e4SLinus Torvalds #endif
121da177e4SLinus Torvalds 
1337ae2d59SArnaud Lacombe #include <assert.h>
141da177e4SLinus Torvalds #include <stdio.h>
151da177e4SLinus Torvalds #ifndef __cplusplus
161da177e4SLinus Torvalds #include <stdbool.h>
171da177e4SLinus Torvalds #endif
181da177e4SLinus Torvalds 
19fbaf242cSMasahiro Yamada #include <list_types.h>
2091b69454SMasahiro Yamada 
211da177e4SLinus Torvalds typedef enum tristate {
221da177e4SLinus Torvalds 	no, mod, yes
231da177e4SLinus Torvalds } tristate;
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds enum expr_type {
2631847b67SJan Beulich 	E_NONE, E_OR, E_AND, E_NOT,
2731847b67SJan Beulich 	E_EQUAL, E_UNEQUAL, E_LTH, E_LEQ, E_GTH, E_GEQ,
287c9bb07aSMasahiro Yamada 	E_SYMBOL, E_RANGE
291da177e4SLinus Torvalds };
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds union expr_data {
32f93d6bfbSMasahiro Yamada 	struct expr * const expr;
33f93d6bfbSMasahiro Yamada 	struct symbol * const sym;
34f93d6bfbSMasahiro Yamada 	void *_initdata;
351da177e4SLinus Torvalds };
361da177e4SLinus Torvalds 
37f93d6bfbSMasahiro Yamada /**
38f93d6bfbSMasahiro Yamada  * struct expr - expression
39f93d6bfbSMasahiro Yamada  *
40f93d6bfbSMasahiro Yamada  * @node:  link node for the hash table
41f93d6bfbSMasahiro Yamada  * @type:  expressoin type
42*95573cacSMasahiro Yamada  * @val: calculated tristate value
43*95573cacSMasahiro Yamada  * @val_is_valid: indicate whether the value is valid
44f93d6bfbSMasahiro Yamada  * @left:  left node
45f93d6bfbSMasahiro Yamada  * @right: right node
46f93d6bfbSMasahiro Yamada  */
471da177e4SLinus Torvalds struct expr {
48f93d6bfbSMasahiro Yamada 	struct hlist_node node;
491da177e4SLinus Torvalds 	enum expr_type type;
50*95573cacSMasahiro Yamada 	tristate val;
51*95573cacSMasahiro Yamada 	bool val_is_valid;
521da177e4SLinus Torvalds 	union expr_data left, right;
531da177e4SLinus Torvalds };
541da177e4SLinus Torvalds 
55d6ee3576SSam Ravnborg #define EXPR_OR(dep1, dep2)	(((dep1)>(dep2))?(dep1):(dep2))
56d6ee3576SSam Ravnborg #define EXPR_AND(dep1, dep2)	(((dep1)<(dep2))?(dep1):(dep2))
57d6ee3576SSam Ravnborg #define EXPR_NOT(dep)		(2-(dep))
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds struct expr_value {
601da177e4SLinus Torvalds 	struct expr *expr;
611da177e4SLinus Torvalds 	tristate tri;
621da177e4SLinus Torvalds };
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds struct symbol_value {
651da177e4SLinus Torvalds 	void *val;
661da177e4SLinus Torvalds 	tristate tri;
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds enum symbol_type {
702aabbed6SMasahiro Yamada 	S_UNKNOWN, S_BOOLEAN, S_TRISTATE, S_INT, S_HEX, S_STRING
711da177e4SLinus Torvalds };
721da177e4SLinus Torvalds 
73eaa2a874SSam Ravnborg /* enum values are used as index to symbol.def[] */
740c1822e6SRoman Zippel enum {
750c1822e6SRoman Zippel 	S_DEF_USER,		/* main user value */
76eaa2a874SSam Ravnborg 	S_DEF_AUTO,		/* values read from auto.conf */
77eaa2a874SSam Ravnborg 	S_DEF_DEF3,		/* Reserved for UI usage */
78eaa2a874SSam Ravnborg 	S_DEF_DEF4,		/* Reserved for UI usage */
79eaa2a874SSam Ravnborg 	S_DEF_COUNT
800c1822e6SRoman Zippel };
810c1822e6SRoman Zippel 
8252aede4bSUlf Magnusson /*
8352aede4bSUlf Magnusson  * Represents a configuration symbol.
8452aede4bSUlf Magnusson  *
851da251c6SMasahiro Yamada  * Choices are represented as a special kind of symbol with null name.
86f79dc03fSMasahiro Yamada  *
87f79dc03fSMasahiro Yamada  * @choice_link: linked to menu::choice_members
8852aede4bSUlf Magnusson  */
891da177e4SLinus Torvalds struct symbol {
9091b69454SMasahiro Yamada 	/* link node for the hash table */
9191b69454SMasahiro Yamada 	struct hlist_node node;
9252aede4bSUlf Magnusson 
9352aede4bSUlf Magnusson 	/* The name of the symbol, e.g. "FOO" for 'config FOO' */
941da177e4SLinus Torvalds 	char *name;
9552aede4bSUlf Magnusson 
9652aede4bSUlf Magnusson 	/* S_BOOLEAN, S_TRISTATE, ... */
971da177e4SLinus Torvalds 	enum symbol_type type;
9852aede4bSUlf Magnusson 
9952aede4bSUlf Magnusson 	/*
10052aede4bSUlf Magnusson 	 * The calculated value of the symbol. The SYMBOL_VALID bit is set in
10152aede4bSUlf Magnusson 	 * 'flags' when this is up to date. Note that this value might differ
10252aede4bSUlf Magnusson 	 * from the user value set in e.g. a .config file, due to visibility.
10352aede4bSUlf Magnusson 	 */
1040c1822e6SRoman Zippel 	struct symbol_value curr;
10552aede4bSUlf Magnusson 
10652aede4bSUlf Magnusson 	/*
10752aede4bSUlf Magnusson 	 * Values for the symbol provided from outside. def[S_DEF_USER] holds
10852aede4bSUlf Magnusson 	 * the .config value.
10952aede4bSUlf Magnusson 	 */
110eaa2a874SSam Ravnborg 	struct symbol_value def[S_DEF_COUNT];
11152aede4bSUlf Magnusson 
11252aede4bSUlf Magnusson 	/*
11352aede4bSUlf Magnusson 	 * An upper bound on the tristate value the user can set for the symbol
11452aede4bSUlf Magnusson 	 * if it is a boolean or tristate. Calculated from prompt dependencies,
11552aede4bSUlf Magnusson 	 * which also inherit dependencies from enclosing menus, choices, and
11652aede4bSUlf Magnusson 	 * ifs. If 'n', the user value will be ignored.
11752aede4bSUlf Magnusson 	 *
11852aede4bSUlf Magnusson 	 * Symbols lacking prompts always have visibility 'n'.
11952aede4bSUlf Magnusson 	 */
1201da177e4SLinus Torvalds 	tristate visible;
12152aede4bSUlf Magnusson 
122e0492219SMasahiro Yamada 	/* config entries associated with this symbol */
123e0492219SMasahiro Yamada 	struct list_head menus;
124e0492219SMasahiro Yamada 
125f79dc03fSMasahiro Yamada 	struct list_head choice_link;
126f79dc03fSMasahiro Yamada 
12752aede4bSUlf Magnusson 	/* SYMBOL_* flags */
1281da177e4SLinus Torvalds 	int flags;
12952aede4bSUlf Magnusson 
13052aede4bSUlf Magnusson 	/* List of properties. See prop_type. */
1311da177e4SLinus Torvalds 	struct property *prop;
13252aede4bSUlf Magnusson 
13352aede4bSUlf Magnusson 	/* Dependencies from enclosing menus, choices, and ifs */
134246cf9c2SCatalin Marinas 	struct expr_value dir_dep;
13552aede4bSUlf Magnusson 
13652aede4bSUlf Magnusson 	/* Reverse dependencies through being selected by other symbols */
1371da177e4SLinus Torvalds 	struct expr_value rev_dep;
13852aede4bSUlf Magnusson 
13952aede4bSUlf Magnusson 	/*
14052aede4bSUlf Magnusson 	 * "Weak" reverse dependencies through being implied by other symbols
14152aede4bSUlf Magnusson 	 */
142237e3ad0SNicolas Pitre 	struct expr_value implied;
1431da177e4SLinus Torvalds };
1441da177e4SLinus Torvalds 
1455b2cf365SSam Ravnborg #define SYMBOL_CONST      0x0001  /* symbol is const */
1465b2cf365SSam Ravnborg #define SYMBOL_CHECK      0x0008  /* used during dependency checking */
1475b2cf365SSam Ravnborg #define SYMBOL_VALID      0x0080  /* set when symbol.curr is calculated */
14831bfb108SMartin Walch #define SYMBOL_WRITE      0x0200  /* write symbol to file (KCONFIG_CONFIG) */
1498e2442a5SMasahiro Yamada #define SYMBOL_WRITTEN    0x0800  /* track info to avoid double-write to .config */
1505b2cf365SSam Ravnborg #define SYMBOL_CHECKED    0x2000  /* used during dependency checking */
1515b2cf365SSam Ravnborg #define SYMBOL_WARNED     0x8000  /* warning has been issued */
1525b2cf365SSam Ravnborg 
1535b2cf365SSam Ravnborg /* Set when symbol.def[] is used */
1545b2cf365SSam Ravnborg #define SYMBOL_DEF        0x10000  /* First bit of SYMBOL_DEF */
1555b2cf365SSam Ravnborg #define SYMBOL_DEF_USER   0x10000  /* symbol.def[S_DEF_USER] is valid */
1565b2cf365SSam Ravnborg #define SYMBOL_DEF_AUTO   0x20000  /* symbol.def[S_DEF_AUTO] is valid */
1575b2cf365SSam Ravnborg #define SYMBOL_DEF3       0x40000  /* symbol.def[S_DEF_3] is valid */
1585b2cf365SSam Ravnborg #define SYMBOL_DEF4       0x80000  /* symbol.def[S_DEF_4] is valid */
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds #define SYMBOL_MAXLENGTH	256
1611da177e4SLinus Torvalds 
162cf82607aSSam Ravnborg /* A property represent the config options that can be associated
163cf82607aSSam Ravnborg  * with a config "symbol".
164cf82607aSSam Ravnborg  * Sample:
165cf82607aSSam Ravnborg  * config FOO
166cf82607aSSam Ravnborg  *         default y
167cf82607aSSam Ravnborg  *         prompt "foo prompt"
168cf82607aSSam Ravnborg  *         select BAR
169cf82607aSSam Ravnborg  * config BAZ
170cf82607aSSam Ravnborg  *         int "BAZ Value"
171cf82607aSSam Ravnborg  *         range 1..255
172ecd53ac2SDirk Gouders  *
173769a1c02SMasahiro Yamada  * Please, also check parser.y:print_symbol() when modifying the
174ecd53ac2SDirk Gouders  * list of property types!
175cf82607aSSam Ravnborg  */
1761da177e4SLinus Torvalds enum prop_type {
177cf82607aSSam Ravnborg 	P_UNKNOWN,
178cf82607aSSam Ravnborg 	P_PROMPT,   /* prompt "foo prompt" or "BAZ Value" */
179cf82607aSSam Ravnborg 	P_COMMENT,  /* text associated with a comment */
18052aede4bSUlf Magnusson 	P_MENU,     /* prompt associated with a menu or menuconfig symbol */
181cf82607aSSam Ravnborg 	P_DEFAULT,  /* default y */
182cf82607aSSam Ravnborg 	P_SELECT,   /* select BAR */
183237e3ad0SNicolas Pitre 	P_IMPLY,    /* imply BAR */
184cf82607aSSam Ravnborg 	P_RANGE,    /* range 7..100 (for a symbol) */
1851da177e4SLinus Torvalds };
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds struct property {
188cf82607aSSam Ravnborg 	struct property *next;     /* next property - null if last */
189cf82607aSSam Ravnborg 	enum prop_type type;       /* type of property */
190cf82607aSSam Ravnborg 	const char *text;          /* the prompt value - P_PROMPT, P_MENU, P_COMMENT */
1911da177e4SLinus Torvalds 	struct expr_value visible;
192cf82607aSSam Ravnborg 	struct expr *expr;         /* the optional conditional part of the property */
193cf82607aSSam Ravnborg 	struct menu *menu;         /* the menu the property are associated with
194ca4c74baSMasahiro Yamada 	                            * valid for: P_SELECT, P_RANGE,
195cf82607aSSam Ravnborg 	                            * P_PROMPT, P_DEFAULT, P_MENU, P_COMMENT */
1961a90b0cdSMasahiro Yamada 	const char *filename;      /* what file was this property defined */
197cf82607aSSam Ravnborg 	int lineno;                /* what lineno was this property defined */
1981da177e4SLinus Torvalds };
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds #define for_all_properties(sym, st, tok) \
2011da177e4SLinus Torvalds 	for (st = sym->prop; st; st = st->next) \
2021da177e4SLinus Torvalds 		if (st->type == (tok))
2031da177e4SLinus Torvalds #define for_all_defaults(sym, st) for_all_properties(sym, st, P_DEFAULT)
2041da177e4SLinus Torvalds #define for_all_prompts(sym, st) \
2051da177e4SLinus Torvalds 	for (st = sym->prop; st; st = st->next) \
2061da177e4SLinus Torvalds 		if (st->text)
2071da177e4SLinus Torvalds 
20833ca1a24SUlf Magnusson /*
20933ca1a24SUlf Magnusson  * Represents a node in the menu tree, as seen in e.g. menuconfig (though used
21033ca1a24SUlf Magnusson  * for all front ends). Each symbol, menu, etc. defined in the Kconfig files
21133ca1a24SUlf Magnusson  * gets a node. A symbol defined in multiple locations gets one node at each
21233ca1a24SUlf Magnusson  * location.
213f79dc03fSMasahiro Yamada  *
214f79dc03fSMasahiro Yamada  * @choice_members: list of choice members with priority.
21533ca1a24SUlf Magnusson  */
2161da177e4SLinus Torvalds struct menu {
21733ca1a24SUlf Magnusson 	/* The next menu node at the same level */
2181da177e4SLinus Torvalds 	struct menu *next;
21933ca1a24SUlf Magnusson 
22033ca1a24SUlf Magnusson 	/* The parent menu node, corresponding to e.g. a menu or choice */
2211da177e4SLinus Torvalds 	struct menu *parent;
22233ca1a24SUlf Magnusson 
22333ca1a24SUlf Magnusson 	/* The first child menu node, for e.g. menus and choices */
2241da177e4SLinus Torvalds 	struct menu *list;
22533ca1a24SUlf Magnusson 
22633ca1a24SUlf Magnusson 	/*
22733ca1a24SUlf Magnusson 	 * The symbol associated with the menu node. Choices are implemented as
22833ca1a24SUlf Magnusson 	 * a special kind of symbol. NULL for menus, comments, and ifs.
22933ca1a24SUlf Magnusson 	 */
2301da177e4SLinus Torvalds 	struct symbol *sym;
23133ca1a24SUlf Magnusson 
232e0492219SMasahiro Yamada 	struct list_head link;	/* link to symbol::menus */
233e0492219SMasahiro Yamada 
234f79dc03fSMasahiro Yamada 	struct list_head choice_members;
235f79dc03fSMasahiro Yamada 
23633ca1a24SUlf Magnusson 	/*
23733ca1a24SUlf Magnusson 	 * The prompt associated with the node. This holds the prompt for a
23833ca1a24SUlf Magnusson 	 * symbol as well as the text for a menu or comment, along with the
23933ca1a24SUlf Magnusson 	 * type (P_PROMPT, P_MENU, etc.)
24033ca1a24SUlf Magnusson 	 */
2411da177e4SLinus Torvalds 	struct property *prompt;
24233ca1a24SUlf Magnusson 
24333ca1a24SUlf Magnusson 	/*
24433ca1a24SUlf Magnusson 	 * 'visible if' dependencies. If more than one is given, they will be
24533ca1a24SUlf Magnusson 	 * ANDed together.
24633ca1a24SUlf Magnusson 	 */
24786e187ffSArnaud Lacombe 	struct expr *visibility;
24833ca1a24SUlf Magnusson 
24933ca1a24SUlf Magnusson 	/*
25033ca1a24SUlf Magnusson 	 * Ordinary dependencies from e.g. 'depends on' and 'if', ANDed
25133ca1a24SUlf Magnusson 	 * together
25233ca1a24SUlf Magnusson 	 */
2531da177e4SLinus Torvalds 	struct expr *dep;
25433ca1a24SUlf Magnusson 
25533ca1a24SUlf Magnusson 	/* MENU_* flags */
2561da177e4SLinus Torvalds 	unsigned int flags;
25733ca1a24SUlf Magnusson 
25833ca1a24SUlf Magnusson 	/* Any help text associated with the node */
25903d29122SSam Ravnborg 	char *help;
26033ca1a24SUlf Magnusson 
26133ca1a24SUlf Magnusson 	/* The location where the menu node appears in the Kconfig files */
26240bab83aSMasahiro Yamada 	const char *filename;
2631da177e4SLinus Torvalds 	int lineno;
26433ca1a24SUlf Magnusson 
26533ca1a24SUlf Magnusson 	/* For use by front ends that need to store auxiliary data */
2661da177e4SLinus Torvalds 	void *data;
2671da177e4SLinus Torvalds };
2681da177e4SLinus Torvalds 
26933ca1a24SUlf Magnusson /*
27033ca1a24SUlf Magnusson  * Set on a menu node when the corresponding symbol changes state in some way.
27133ca1a24SUlf Magnusson  * Can be checked by front ends.
27233ca1a24SUlf Magnusson  */
2731da177e4SLinus Torvalds #define MENU_CHANGED		0x0001
27433ca1a24SUlf Magnusson 
2751da177e4SLinus Torvalds #define MENU_ROOT		0x0002
2761da177e4SLinus Torvalds 
27795ac9b3bSBenjamin Poirier struct jump_key {
278bad9955dSBenjamin Poirier 	struct list_head entries;
27995ac9b3bSBenjamin Poirier 	size_t offset;
28095ac9b3bSBenjamin Poirier 	struct menu *target;
28195ac9b3bSBenjamin Poirier };
28295ac9b3bSBenjamin Poirier 
2831da177e4SLinus Torvalds extern struct symbol symbol_yes, symbol_no, symbol_mod;
2841da177e4SLinus Torvalds extern struct symbol *modules_sym;
2851da177e4SLinus Torvalds extern int cdebug;
2861da177e4SLinus Torvalds struct expr *expr_alloc_symbol(struct symbol *sym);
2871da177e4SLinus Torvalds struct expr *expr_alloc_one(enum expr_type type, struct expr *ce);
2881da177e4SLinus Torvalds struct expr *expr_alloc_two(enum expr_type type, struct expr *e1, struct expr *e2);
2891da177e4SLinus Torvalds struct expr *expr_alloc_comp(enum expr_type type, struct symbol *s1, struct symbol *s2);
2901da177e4SLinus Torvalds struct expr *expr_alloc_and(struct expr *e1, struct expr *e2);
2911da177e4SLinus Torvalds struct expr *expr_alloc_or(struct expr *e1, struct expr *e2);
2921da177e4SLinus Torvalds void expr_eliminate_eq(struct expr **ep1, struct expr **ep2);
293d607e0e7SMasahiro Yamada bool expr_eq(struct expr *e1, struct expr *e2);
2941da177e4SLinus Torvalds tristate expr_calc_value(struct expr *e);
2951da177e4SLinus Torvalds struct expr *expr_eliminate_dups(struct expr *e);
2961da177e4SLinus Torvalds struct expr *expr_transform(struct expr *e);
297d607e0e7SMasahiro Yamada bool expr_contains_symbol(struct expr *dep, struct symbol *sym);
2981da177e4SLinus Torvalds bool expr_depends_symbol(struct expr *dep, struct symbol *sym);
2991da177e4SLinus Torvalds struct expr *expr_trans_compare(struct expr *e, enum expr_type type, struct symbol *sym);
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds void expr_fprint(struct expr *e, FILE *out);
3021da177e4SLinus Torvalds struct gstr; /* forward */
3036425e3b2SMasahiro Yamada void expr_gstr_print(const struct expr *e, struct gstr *gs);
304d9119b59SEugeniu Rosca void expr_gstr_print_revdep(struct expr *e, struct gstr *gs,
305d9119b59SEugeniu Rosca 			    tristate pr_type, const char *title);
3061da177e4SLinus Torvalds 
expr_is_yes(const struct expr * e)307d607e0e7SMasahiro Yamada static inline bool expr_is_yes(const struct expr *e)
3081da177e4SLinus Torvalds {
3091da177e4SLinus Torvalds 	return !e || (e->type == E_SYMBOL && e->left.sym == &symbol_yes);
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds #ifdef __cplusplus
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds #endif
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds #endif /* EXPR_H */
317