xref: /illumos-gate/usr/src/tools/smatch/src/parse.h (revision c85f09cc)
1*1f5207b7SJohn Levon #ifndef PARSE_H
2*1f5207b7SJohn Levon #define PARSE_H
3*1f5207b7SJohn Levon /*
4*1f5207b7SJohn Levon  * Basic parsing data structures. Statements and symbols.
5*1f5207b7SJohn Levon  *
6*1f5207b7SJohn Levon  * Copyright (C) 2003 Transmeta Corp.
7*1f5207b7SJohn Levon  *               2003 Linus Torvalds
8*1f5207b7SJohn Levon  *
9*1f5207b7SJohn Levon  * Permission is hereby granted, free of charge, to any person obtaining a copy
10*1f5207b7SJohn Levon  * of this software and associated documentation files (the "Software"), to deal
11*1f5207b7SJohn Levon  * in the Software without restriction, including without limitation the rights
12*1f5207b7SJohn Levon  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13*1f5207b7SJohn Levon  * copies of the Software, and to permit persons to whom the Software is
14*1f5207b7SJohn Levon  * furnished to do so, subject to the following conditions:
15*1f5207b7SJohn Levon  *
16*1f5207b7SJohn Levon  * The above copyright notice and this permission notice shall be included in
17*1f5207b7SJohn Levon  * all copies or substantial portions of the Software.
18*1f5207b7SJohn Levon  *
19*1f5207b7SJohn Levon  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20*1f5207b7SJohn Levon  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21*1f5207b7SJohn Levon  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22*1f5207b7SJohn Levon  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23*1f5207b7SJohn Levon  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24*1f5207b7SJohn Levon  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25*1f5207b7SJohn Levon  * THE SOFTWARE.
26*1f5207b7SJohn Levon  */
27*1f5207b7SJohn Levon 
28*1f5207b7SJohn Levon #include "symbol.h"
29*1f5207b7SJohn Levon 
30*1f5207b7SJohn Levon enum statement_type {
31*1f5207b7SJohn Levon 	STMT_NONE,
32*1f5207b7SJohn Levon 	STMT_DECLARATION,
33*1f5207b7SJohn Levon 	STMT_EXPRESSION,
34*1f5207b7SJohn Levon 	STMT_COMPOUND,
35*1f5207b7SJohn Levon 	STMT_IF,
36*1f5207b7SJohn Levon 	STMT_RETURN,
37*1f5207b7SJohn Levon 	STMT_CASE,
38*1f5207b7SJohn Levon 	STMT_SWITCH,
39*1f5207b7SJohn Levon 	STMT_ITERATOR,
40*1f5207b7SJohn Levon 	STMT_LABEL,
41*1f5207b7SJohn Levon 	STMT_GOTO,
42*1f5207b7SJohn Levon 	STMT_ASM,
43*1f5207b7SJohn Levon 	STMT_CONTEXT,
44*1f5207b7SJohn Levon 	STMT_RANGE,
45*1f5207b7SJohn Levon };
46*1f5207b7SJohn Levon 
47*1f5207b7SJohn Levon struct statement {
48*1f5207b7SJohn Levon 	enum statement_type type;
49*1f5207b7SJohn Levon 	struct position pos;
50*1f5207b7SJohn Levon 	struct statement *parent;
51*1f5207b7SJohn Levon 	union {
52*1f5207b7SJohn Levon 		struct /* declaration */ {
53*1f5207b7SJohn Levon 			struct symbol_list *declaration;
54*1f5207b7SJohn Levon 		};
55*1f5207b7SJohn Levon 		struct /* context */ {
56*1f5207b7SJohn Levon 			struct expression *expression;
57*1f5207b7SJohn Levon 			struct expression *context;
58*1f5207b7SJohn Levon 		};
59*1f5207b7SJohn Levon 		struct /* return_statement */ {
60*1f5207b7SJohn Levon 			struct expression *ret_value;
61*1f5207b7SJohn Levon 			struct symbol *ret_target;
62*1f5207b7SJohn Levon 		};
63*1f5207b7SJohn Levon 		struct /* if_statement */ {
64*1f5207b7SJohn Levon 			struct expression *if_conditional;
65*1f5207b7SJohn Levon 			struct statement *if_true;
66*1f5207b7SJohn Levon 			struct statement *if_false;
67*1f5207b7SJohn Levon 		};
68*1f5207b7SJohn Levon 		struct /* compound_struct */ {
69*1f5207b7SJohn Levon 			struct statement_list *stmts;
70*1f5207b7SJohn Levon 			struct symbol *ret;
71*1f5207b7SJohn Levon 			struct symbol *inline_fn;
72*1f5207b7SJohn Levon 			struct statement *args;
73*1f5207b7SJohn Levon 		};
74*1f5207b7SJohn Levon 		struct /* labeled_struct */ {
75*1f5207b7SJohn Levon 			struct symbol *label_identifier;
76*1f5207b7SJohn Levon 			struct statement *label_statement;
77*1f5207b7SJohn Levon 		};
78*1f5207b7SJohn Levon 		struct /* case_struct */ {
79*1f5207b7SJohn Levon 			struct expression *case_expression;
80*1f5207b7SJohn Levon 			struct expression *case_to;
81*1f5207b7SJohn Levon 			struct statement *case_statement;
82*1f5207b7SJohn Levon 			struct symbol *case_label;
83*1f5207b7SJohn Levon 		};
84*1f5207b7SJohn Levon 		struct /* switch_struct */ {
85*1f5207b7SJohn Levon 			struct expression *switch_expression;
86*1f5207b7SJohn Levon 			struct statement *switch_statement;
87*1f5207b7SJohn Levon 			struct symbol *switch_break, *switch_case;
88*1f5207b7SJohn Levon 		};
89*1f5207b7SJohn Levon 		struct /* iterator_struct */ {
90*1f5207b7SJohn Levon 			struct symbol *iterator_break;
91*1f5207b7SJohn Levon 			struct symbol *iterator_continue;
92*1f5207b7SJohn Levon 			struct symbol_list *iterator_syms;
93*1f5207b7SJohn Levon 			struct statement  *iterator_pre_statement;
94*1f5207b7SJohn Levon 			struct expression *iterator_pre_condition;
95*1f5207b7SJohn Levon 
96*1f5207b7SJohn Levon 			struct statement  *iterator_statement;
97*1f5207b7SJohn Levon 
98*1f5207b7SJohn Levon 			struct statement  *iterator_post_statement;
99*1f5207b7SJohn Levon 			struct expression *iterator_post_condition;
100*1f5207b7SJohn Levon 		};
101*1f5207b7SJohn Levon 		struct /* goto_struct */ {
102*1f5207b7SJohn Levon 			struct symbol *goto_label;
103*1f5207b7SJohn Levon 
104*1f5207b7SJohn Levon 			/* computed gotos have these: */
105*1f5207b7SJohn Levon 			struct expression *goto_expression;
106*1f5207b7SJohn Levon 			struct symbol_list *target_list;
107*1f5207b7SJohn Levon 		};
108*1f5207b7SJohn Levon 		struct /* asm */ {
109*1f5207b7SJohn Levon 			struct expression *asm_string;
110*1f5207b7SJohn Levon 			struct expression_list *asm_outputs;
111*1f5207b7SJohn Levon 			struct expression_list *asm_inputs;
112*1f5207b7SJohn Levon 			struct expression_list *asm_clobbers;
113*1f5207b7SJohn Levon 			struct symbol_list *asm_labels;
114*1f5207b7SJohn Levon 		};
115*1f5207b7SJohn Levon 		struct /* range */ {
116*1f5207b7SJohn Levon 			struct expression *range_expression;
117*1f5207b7SJohn Levon 			struct expression *range_low;
118*1f5207b7SJohn Levon 			struct expression *range_high;
119*1f5207b7SJohn Levon 		};
120*1f5207b7SJohn Levon 	};
121*1f5207b7SJohn Levon };
122*1f5207b7SJohn Levon 
123*1f5207b7SJohn Levon extern struct symbol_list *function_computed_target_list;
124*1f5207b7SJohn Levon extern struct statement_list *function_computed_goto_list;
125*1f5207b7SJohn Levon 
126*1f5207b7SJohn Levon extern struct token *parse_expression(struct token *, struct expression **);
127*1f5207b7SJohn Levon extern struct symbol *label_symbol(struct token *token);
128*1f5207b7SJohn Levon 
129*1f5207b7SJohn Levon extern int show_statement(struct statement *);
130*1f5207b7SJohn Levon extern void show_statement_list(struct statement_list *, const char *);
131*1f5207b7SJohn Levon extern int show_expression(struct expression *);
132*1f5207b7SJohn Levon 
133*1f5207b7SJohn Levon typedef void (*validate_decl_t)(struct symbol *decl);
134*1f5207b7SJohn Levon extern struct token *external_declaration(struct token *, struct symbol_list **, validate_decl_t);
135*1f5207b7SJohn Levon 
136*1f5207b7SJohn Levon extern struct symbol *ctype_integer(int size, int want_unsigned);
137*1f5207b7SJohn Levon 
138*1f5207b7SJohn Levon extern int inline_function(struct expression *expr, struct symbol *sym);
139*1f5207b7SJohn Levon extern void uninline(struct symbol *sym);
140*1f5207b7SJohn Levon extern void init_parser(int);
141*1f5207b7SJohn Levon 
stmt_set_parent_stmt(struct statement * stmt,struct statement * parent)142*1f5207b7SJohn Levon static inline void stmt_set_parent_stmt(struct statement *stmt, struct statement *parent)
143*1f5207b7SJohn Levon {
144*1f5207b7SJohn Levon 	if (!stmt)
145*1f5207b7SJohn Levon 		return;
146*1f5207b7SJohn Levon 	stmt->parent = parent;
147*1f5207b7SJohn Levon }
148*1f5207b7SJohn Levon 
stmt_get_parent_stmt(struct statement * stmt)149*1f5207b7SJohn Levon static inline struct statement *stmt_get_parent_stmt(struct statement *stmt)
150*1f5207b7SJohn Levon {
151*1f5207b7SJohn Levon 	return stmt->parent;
152*1f5207b7SJohn Levon }
153*1f5207b7SJohn Levon 
154*1f5207b7SJohn Levon #endif /* PARSE_H */
155