xref: /openbmc/linux/scripts/kconfig/expr.h (revision d6b6592ac6d11eab91e6758d224eac35f4122aca)
10c874100SMasahiro Yamada /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
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>
15bad9955dSBenjamin Poirier #include "list.h"
161da177e4SLinus Torvalds #ifndef __cplusplus
171da177e4SLinus Torvalds #include <stdbool.h>
181da177e4SLinus Torvalds #endif
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds struct file {
211da177e4SLinus Torvalds 	struct file *next;
221da177e4SLinus Torvalds 	struct file *parent;
232e7a0918SArnaud Lacombe 	const char *name;
241da177e4SLinus Torvalds 	int lineno;
251da177e4SLinus Torvalds };
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds typedef enum tristate {
281da177e4SLinus Torvalds 	no, mod, yes
291da177e4SLinus Torvalds } tristate;
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds enum expr_type {
3231847b67SJan Beulich 	E_NONE, E_OR, E_AND, E_NOT,
3331847b67SJan Beulich 	E_EQUAL, E_UNEQUAL, E_LTH, E_LEQ, E_GTH, E_GEQ,
3431847b67SJan Beulich 	E_LIST, E_SYMBOL, E_RANGE
351da177e4SLinus Torvalds };
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds union expr_data {
381da177e4SLinus Torvalds 	struct expr *expr;
391da177e4SLinus Torvalds 	struct symbol *sym;
401da177e4SLinus Torvalds };
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds struct expr {
431da177e4SLinus Torvalds 	enum expr_type type;
441da177e4SLinus Torvalds 	union expr_data left, right;
451da177e4SLinus Torvalds };
461da177e4SLinus Torvalds 
47d6ee3576SSam Ravnborg #define EXPR_OR(dep1, dep2)	(((dep1)>(dep2))?(dep1):(dep2))
48d6ee3576SSam Ravnborg #define EXPR_AND(dep1, dep2)	(((dep1)<(dep2))?(dep1):(dep2))
49d6ee3576SSam Ravnborg #define EXPR_NOT(dep)		(2-(dep))
501da177e4SLinus Torvalds 
517a962923SRoman Zippel #define expr_list_for_each_sym(l, e, s) \
527a962923SRoman Zippel 	for (e = (l); e && (s = e->right.sym); e = e->left.expr)
537a962923SRoman Zippel 
541da177e4SLinus Torvalds struct expr_value {
551da177e4SLinus Torvalds 	struct expr *expr;
561da177e4SLinus Torvalds 	tristate tri;
571da177e4SLinus Torvalds };
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds struct symbol_value {
601da177e4SLinus Torvalds 	void *val;
611da177e4SLinus Torvalds 	tristate tri;
621da177e4SLinus Torvalds };
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds enum symbol_type {
652aabbed6SMasahiro Yamada 	S_UNKNOWN, S_BOOLEAN, S_TRISTATE, S_INT, S_HEX, S_STRING
661da177e4SLinus Torvalds };
671da177e4SLinus Torvalds 
68eaa2a874SSam Ravnborg /* enum values are used as index to symbol.def[] */
690c1822e6SRoman Zippel enum {
700c1822e6SRoman Zippel 	S_DEF_USER,		/* main user value */
71eaa2a874SSam Ravnborg 	S_DEF_AUTO,		/* values read from auto.conf */
72eaa2a874SSam Ravnborg 	S_DEF_DEF3,		/* Reserved for UI usage */
73eaa2a874SSam Ravnborg 	S_DEF_DEF4,		/* Reserved for UI usage */
74eaa2a874SSam Ravnborg 	S_DEF_COUNT
750c1822e6SRoman Zippel };
760c1822e6SRoman Zippel 
7752aede4bSUlf Magnusson /*
7852aede4bSUlf Magnusson  * Represents a configuration symbol.
7952aede4bSUlf Magnusson  *
8052aede4bSUlf Magnusson  * Choices are represented as a special kind of symbol and have the
8152aede4bSUlf Magnusson  * SYMBOL_CHOICE bit set in 'flags'.
8252aede4bSUlf Magnusson  */
831da177e4SLinus Torvalds struct symbol {
8452aede4bSUlf Magnusson 	/* The next symbol in the same bucket in the symbol hash table */
851da177e4SLinus Torvalds 	struct symbol *next;
8652aede4bSUlf Magnusson 
8752aede4bSUlf Magnusson 	/* The name of the symbol, e.g. "FOO" for 'config FOO' */
881da177e4SLinus Torvalds 	char *name;
8952aede4bSUlf Magnusson 
9052aede4bSUlf Magnusson 	/* S_BOOLEAN, S_TRISTATE, ... */
911da177e4SLinus Torvalds 	enum symbol_type type;
9252aede4bSUlf Magnusson 
9352aede4bSUlf Magnusson 	/*
9452aede4bSUlf Magnusson 	 * The calculated value of the symbol. The SYMBOL_VALID bit is set in
9552aede4bSUlf Magnusson 	 * 'flags' when this is up to date. Note that this value might differ
9652aede4bSUlf Magnusson 	 * from the user value set in e.g. a .config file, due to visibility.
9752aede4bSUlf Magnusson 	 */
980c1822e6SRoman Zippel 	struct symbol_value curr;
9952aede4bSUlf Magnusson 
10052aede4bSUlf Magnusson 	/*
10152aede4bSUlf Magnusson 	 * Values for the symbol provided from outside. def[S_DEF_USER] holds
10252aede4bSUlf Magnusson 	 * the .config value.
10352aede4bSUlf Magnusson 	 */
104eaa2a874SSam Ravnborg 	struct symbol_value def[S_DEF_COUNT];
10552aede4bSUlf Magnusson 
10652aede4bSUlf Magnusson 	/*
10752aede4bSUlf Magnusson 	 * An upper bound on the tristate value the user can set for the symbol
10852aede4bSUlf Magnusson 	 * if it is a boolean or tristate. Calculated from prompt dependencies,
10952aede4bSUlf Magnusson 	 * which also inherit dependencies from enclosing menus, choices, and
11052aede4bSUlf Magnusson 	 * ifs. If 'n', the user value will be ignored.
11152aede4bSUlf Magnusson 	 *
11252aede4bSUlf Magnusson 	 * Symbols lacking prompts always have visibility 'n'.
11352aede4bSUlf Magnusson 	 */
1141da177e4SLinus Torvalds 	tristate visible;
11552aede4bSUlf Magnusson 
11652aede4bSUlf Magnusson 	/* SYMBOL_* flags */
1171da177e4SLinus Torvalds 	int flags;
11852aede4bSUlf Magnusson 
11952aede4bSUlf Magnusson 	/* List of properties. See prop_type. */
1201da177e4SLinus Torvalds 	struct property *prop;
12152aede4bSUlf Magnusson 
12252aede4bSUlf Magnusson 	/* Dependencies from enclosing menus, choices, and ifs */
123246cf9c2SCatalin Marinas 	struct expr_value dir_dep;
12452aede4bSUlf Magnusson 
12552aede4bSUlf Magnusson 	/* Reverse dependencies through being selected by other symbols */
1261da177e4SLinus Torvalds 	struct expr_value rev_dep;
12752aede4bSUlf Magnusson 
12852aede4bSUlf Magnusson 	/*
12952aede4bSUlf Magnusson 	 * "Weak" reverse dependencies through being implied by other symbols
13052aede4bSUlf Magnusson 	 */
131237e3ad0SNicolas Pitre 	struct expr_value implied;
1321da177e4SLinus Torvalds };
1331da177e4SLinus Torvalds 
1342aabbed6SMasahiro Yamada #define for_all_symbols(i, sym) for (i = 0; i < SYMBOL_HASHSIZE; i++) for (sym = symbol_hash[i]; sym; sym = sym->next)
1351da177e4SLinus Torvalds 
1365b2cf365SSam Ravnborg #define SYMBOL_CONST      0x0001  /* symbol is const */
1375b2cf365SSam Ravnborg #define SYMBOL_CHECK      0x0008  /* used during dependency checking */
1385b2cf365SSam Ravnborg #define SYMBOL_CHOICE     0x0010  /* start of a choice block (null name) */
1395b2cf365SSam Ravnborg #define SYMBOL_CHOICEVAL  0x0020  /* used as a value in a choice block */
1405b2cf365SSam Ravnborg #define SYMBOL_VALID      0x0080  /* set when symbol.curr is calculated */
1415b2cf365SSam Ravnborg #define SYMBOL_OPTIONAL   0x0100  /* choice is optional - values can be 'n' */
14231bfb108SMartin Walch #define SYMBOL_WRITE      0x0200  /* write symbol to file (KCONFIG_CONFIG) */
1435b2cf365SSam Ravnborg #define SYMBOL_CHANGED    0x0400  /* ? */
1448e2442a5SMasahiro Yamada #define SYMBOL_WRITTEN    0x0800  /* track info to avoid double-write to .config */
145693359f7SDirk Gouders #define SYMBOL_NO_WRITE   0x1000  /* Symbol for internal use only; it will not be written */
1465b2cf365SSam Ravnborg #define SYMBOL_CHECKED    0x2000  /* used during dependency checking */
1475b2cf365SSam Ravnborg #define SYMBOL_WARNED     0x8000  /* warning has been issued */
1485b2cf365SSam Ravnborg 
1495b2cf365SSam Ravnborg /* Set when symbol.def[] is used */
1505b2cf365SSam Ravnborg #define SYMBOL_DEF        0x10000  /* First bit of SYMBOL_DEF */
1515b2cf365SSam Ravnborg #define SYMBOL_DEF_USER   0x10000  /* symbol.def[S_DEF_USER] is valid */
1525b2cf365SSam Ravnborg #define SYMBOL_DEF_AUTO   0x20000  /* symbol.def[S_DEF_AUTO] is valid */
1535b2cf365SSam Ravnborg #define SYMBOL_DEF3       0x40000  /* symbol.def[S_DEF_3] is valid */
1545b2cf365SSam Ravnborg #define SYMBOL_DEF4       0x80000  /* symbol.def[S_DEF_4] is valid */
1551da177e4SLinus Torvalds 
156fbe98bb9SArve Hjønnevåg /* choice values need to be set before calculating this symbol value */
157fbe98bb9SArve Hjønnevåg #define SYMBOL_NEED_SET_CHOICE_VALUES  0x100000
158fbe98bb9SArve Hjønnevåg 
1591da177e4SLinus Torvalds #define SYMBOL_MAXLENGTH	256
160e66f25d7SAndi Kleen #define SYMBOL_HASHSIZE		9973
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_CHOICE,   /* choice value */
183cf82607aSSam Ravnborg 	P_SELECT,   /* select BAR */
184237e3ad0SNicolas Pitre 	P_IMPLY,    /* imply BAR */
185cf82607aSSam Ravnborg 	P_RANGE,    /* range 7..100 (for a symbol) */
18659e89e3dSSam Ravnborg 	P_SYMBOL,   /* where a symbol is defined */
1871da177e4SLinus Torvalds };
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds struct property {
190cf82607aSSam Ravnborg 	struct property *next;     /* next property - null if last */
191cf82607aSSam Ravnborg 	enum prop_type type;       /* type of property */
192cf82607aSSam Ravnborg 	const char *text;          /* the prompt value - P_PROMPT, P_MENU, P_COMMENT */
1931da177e4SLinus Torvalds 	struct expr_value visible;
194cf82607aSSam Ravnborg 	struct expr *expr;         /* the optional conditional part of the property */
195cf82607aSSam Ravnborg 	struct menu *menu;         /* the menu the property are associated with
196cf82607aSSam Ravnborg 	                            * valid for: P_SELECT, P_RANGE, P_CHOICE,
197cf82607aSSam Ravnborg 	                            * P_PROMPT, P_DEFAULT, P_MENU, P_COMMENT */
198cf82607aSSam Ravnborg 	struct file *file;         /* what file was this property defined */
199cf82607aSSam Ravnborg 	int lineno;                /* what lineno was this property defined */
2001da177e4SLinus Torvalds };
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds #define for_all_properties(sym, st, tok) \
2031da177e4SLinus Torvalds 	for (st = sym->prop; st; st = st->next) \
2041da177e4SLinus Torvalds 		if (st->type == (tok))
2051da177e4SLinus Torvalds #define for_all_defaults(sym, st) for_all_properties(sym, st, P_DEFAULT)
2061da177e4SLinus Torvalds #define for_all_choices(sym, st) for_all_properties(sym, st, P_CHOICE)
2071da177e4SLinus Torvalds #define for_all_prompts(sym, st) \
2081da177e4SLinus Torvalds 	for (st = sym->prop; st; st = st->next) \
2091da177e4SLinus Torvalds 		if (st->text)
2101da177e4SLinus Torvalds 
21133ca1a24SUlf Magnusson /*
21233ca1a24SUlf Magnusson  * Represents a node in the menu tree, as seen in e.g. menuconfig (though used
21333ca1a24SUlf Magnusson  * for all front ends). Each symbol, menu, etc. defined in the Kconfig files
21433ca1a24SUlf Magnusson  * gets a node. A symbol defined in multiple locations gets one node at each
21533ca1a24SUlf Magnusson  * location.
21633ca1a24SUlf Magnusson  */
2171da177e4SLinus Torvalds struct menu {
21833ca1a24SUlf Magnusson 	/* The next menu node at the same level */
2191da177e4SLinus Torvalds 	struct menu *next;
22033ca1a24SUlf Magnusson 
22133ca1a24SUlf Magnusson 	/* The parent menu node, corresponding to e.g. a menu or choice */
2221da177e4SLinus Torvalds 	struct menu *parent;
22333ca1a24SUlf Magnusson 
22433ca1a24SUlf Magnusson 	/* The first child menu node, for e.g. menus and choices */
2251da177e4SLinus Torvalds 	struct menu *list;
22633ca1a24SUlf Magnusson 
22733ca1a24SUlf Magnusson 	/*
22833ca1a24SUlf Magnusson 	 * The symbol associated with the menu node. Choices are implemented as
22933ca1a24SUlf Magnusson 	 * a special kind of symbol. NULL for menus, comments, and ifs.
23033ca1a24SUlf Magnusson 	 */
2311da177e4SLinus Torvalds 	struct symbol *sym;
23233ca1a24SUlf Magnusson 
23333ca1a24SUlf Magnusson 	/*
23433ca1a24SUlf Magnusson 	 * The prompt associated with the node. This holds the prompt for a
23533ca1a24SUlf Magnusson 	 * symbol as well as the text for a menu or comment, along with the
23633ca1a24SUlf Magnusson 	 * type (P_PROMPT, P_MENU, etc.)
23733ca1a24SUlf Magnusson 	 */
2381da177e4SLinus Torvalds 	struct property *prompt;
23933ca1a24SUlf Magnusson 
24033ca1a24SUlf Magnusson 	/*
24133ca1a24SUlf Magnusson 	 * 'visible if' dependencies. If more than one is given, they will be
24233ca1a24SUlf Magnusson 	 * ANDed together.
24333ca1a24SUlf Magnusson 	 */
24486e187ffSArnaud Lacombe 	struct expr *visibility;
24533ca1a24SUlf Magnusson 
24633ca1a24SUlf Magnusson 	/*
24733ca1a24SUlf Magnusson 	 * Ordinary dependencies from e.g. 'depends on' and 'if', ANDed
24833ca1a24SUlf Magnusson 	 * together
24933ca1a24SUlf Magnusson 	 */
2501da177e4SLinus Torvalds 	struct expr *dep;
25133ca1a24SUlf Magnusson 
25233ca1a24SUlf Magnusson 	/* MENU_* flags */
2531da177e4SLinus Torvalds 	unsigned int flags;
25433ca1a24SUlf Magnusson 
25533ca1a24SUlf Magnusson 	/* Any help text associated with the node */
25603d29122SSam Ravnborg 	char *help;
25733ca1a24SUlf Magnusson 
25833ca1a24SUlf Magnusson 	/* The location where the menu node appears in the Kconfig files */
2591da177e4SLinus Torvalds 	struct file *file;
2601da177e4SLinus Torvalds 	int lineno;
26133ca1a24SUlf Magnusson 
26233ca1a24SUlf Magnusson 	/* For use by front ends that need to store auxiliary data */
2631da177e4SLinus Torvalds 	void *data;
2641da177e4SLinus Torvalds };
2651da177e4SLinus Torvalds 
26633ca1a24SUlf Magnusson /*
26733ca1a24SUlf Magnusson  * Set on a menu node when the corresponding symbol changes state in some way.
26833ca1a24SUlf Magnusson  * Can be checked by front ends.
26933ca1a24SUlf Magnusson  */
2701da177e4SLinus Torvalds #define MENU_CHANGED		0x0001
27133ca1a24SUlf Magnusson 
2721da177e4SLinus Torvalds #define MENU_ROOT		0x0002
2731da177e4SLinus Torvalds 
27495ac9b3bSBenjamin Poirier struct jump_key {
275bad9955dSBenjamin Poirier 	struct list_head entries;
27695ac9b3bSBenjamin Poirier 	size_t offset;
27795ac9b3bSBenjamin Poirier 	struct menu *target;
27895ac9b3bSBenjamin Poirier };
27995ac9b3bSBenjamin Poirier 
2801da177e4SLinus Torvalds extern struct file *file_list;
2811da177e4SLinus Torvalds extern struct file *current_file;
2821da177e4SLinus Torvalds struct file *lookup_file(const char *name);
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds extern struct symbol symbol_yes, symbol_no, symbol_mod;
2851da177e4SLinus Torvalds extern struct symbol *modules_sym;
2861da177e4SLinus Torvalds extern int cdebug;
2871da177e4SLinus Torvalds struct expr *expr_alloc_symbol(struct symbol *sym);
2881da177e4SLinus Torvalds struct expr *expr_alloc_one(enum expr_type type, struct expr *ce);
2891da177e4SLinus Torvalds struct expr *expr_alloc_two(enum expr_type type, struct expr *e1, struct expr *e2);
2901da177e4SLinus Torvalds struct expr *expr_alloc_comp(enum expr_type type, struct symbol *s1, struct symbol *s2);
2911da177e4SLinus Torvalds struct expr *expr_alloc_and(struct expr *e1, struct expr *e2);
2921da177e4SLinus Torvalds struct expr *expr_alloc_or(struct expr *e1, struct expr *e2);
29317742dc7SMichal Marek struct expr *expr_copy(const struct expr *org);
2941da177e4SLinus Torvalds void expr_free(struct expr *e);
2951da177e4SLinus Torvalds void expr_eliminate_eq(struct expr **ep1, struct expr **ep2);
296*3460d0bcSThomas Hebb int expr_eq(struct expr *e1, struct expr *e2);
2971da177e4SLinus Torvalds tristate expr_calc_value(struct expr *e);
2981da177e4SLinus Torvalds struct expr *expr_eliminate_dups(struct expr *e);
2991da177e4SLinus Torvalds struct expr *expr_transform(struct expr *e);
3001da177e4SLinus Torvalds int expr_contains_symbol(struct expr *dep, struct symbol *sym);
3011da177e4SLinus Torvalds bool expr_depends_symbol(struct expr *dep, struct symbol *sym);
3021da177e4SLinus Torvalds struct expr *expr_trans_compare(struct expr *e, enum expr_type type, struct symbol *sym);
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds void expr_fprint(struct expr *e, FILE *out);
3051da177e4SLinus Torvalds struct gstr; /* forward */
3061da177e4SLinus Torvalds void expr_gstr_print(struct expr *e, struct gstr *gs);
307d9119b59SEugeniu Rosca void expr_gstr_print_revdep(struct expr *e, struct gstr *gs,
308d9119b59SEugeniu Rosca 			    tristate pr_type, const char *title);
3091da177e4SLinus Torvalds 
expr_is_yes(struct expr * e)3101da177e4SLinus Torvalds static inline int expr_is_yes(struct expr *e)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	return !e || (e->type == E_SYMBOL && e->left.sym == &symbol_yes);
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
expr_is_no(struct expr * e)3151da177e4SLinus Torvalds static inline int expr_is_no(struct expr *e)
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds 	return e && (e->type == E_SYMBOL && e->left.sym == &symbol_no);
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds #ifdef __cplusplus
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds #endif
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds #endif /* EXPR_H */
325