xref: /aosp_15_r20/external/mesa3d/src/compiler/glsl/glcpp/glcpp.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2010 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #ifndef GLCPP_H
25 #define GLCPP_H
26 
27 #include <stdint.h>
28 #include <stdbool.h>
29 
30 #include "main/menums.h"
31 
32 #include "util/ralloc.h"
33 
34 #include "util/hash_table.h"
35 
36 #include "util/string_buffer.h"
37 
38 struct gl_context;
39 
40 #define yyscan_t void*
41 
42 /* Some data types used for parser values. */
43 
44 typedef struct expression_value {
45 	intmax_t value;
46 	char *undefined_macro;
47 } expression_value_t;
48 
49 
50 typedef struct string_node {
51 	const char *str;
52 	struct string_node *next;
53 } string_node_t;
54 
55 typedef struct string_list {
56 	string_node_t *head;
57 	string_node_t *tail;
58 } string_list_t;
59 
60 typedef struct token token_t;
61 typedef struct token_list token_list_t;
62 
63 typedef union YYSTYPE
64 {
65 	intmax_t ival;
66 	expression_value_t expression_value;
67 	char *str;
68 	string_list_t *string_list;
69 	token_t *token;
70 	token_list_t *token_list;
71 } YYSTYPE;
72 
73 # define YYSTYPE_IS_TRIVIAL 1
74 # define YYSTYPE_IS_DECLARED 1
75 
76 typedef struct YYLTYPE {
77    int first_line;
78    int first_column;
79    int last_line;
80    int last_column;
81    unsigned source;
82 } YYLTYPE;
83 # define YYLTYPE_IS_DECLARED 1
84 # define YYLTYPE_IS_TRIVIAL 1
85 
86 # define YYLLOC_DEFAULT(Current, Rhs, N)			\
87 do {								\
88    if (N)							\
89    {								\
90       (Current).first_line   = YYRHSLOC(Rhs, 1).first_line;	\
91       (Current).first_column = YYRHSLOC(Rhs, 1).first_column;	\
92       (Current).last_line    = YYRHSLOC(Rhs, N).last_line;	\
93       (Current).last_column  = YYRHSLOC(Rhs, N).last_column;	\
94       (Current).source       = YYRHSLOC(Rhs, N).source;		\
95    }								\
96    else								\
97    {								\
98       (Current).first_line   = (Current).last_line =		\
99 	 YYRHSLOC(Rhs, 0).last_line;				\
100       (Current).first_column = (Current).last_column =		\
101 	 YYRHSLOC(Rhs, 0).last_column;				\
102       (Current).source       = YYRHSLOC(Rhs, 0).source;		\
103    }								\
104 } while (0)
105 
106 struct token {
107 	bool expanding;
108 	int type;
109 	YYSTYPE value;
110 	YYLTYPE location;
111 };
112 
113 typedef struct token_node {
114 	token_t *token;
115 	struct token_node *next;
116 } token_node_t;
117 
118 struct token_list {
119 	token_node_t *head;
120 	token_node_t *tail;
121 	token_node_t *non_space_tail;
122 };
123 
124 typedef struct argument_node {
125 	token_list_t *argument;
126 	struct argument_node *next;
127 } argument_node_t;
128 
129 typedef struct argument_list {
130 	argument_node_t *head;
131 	argument_node_t *tail;
132 } argument_list_t;
133 
134 typedef struct glcpp_parser glcpp_parser_t;
135 
136 typedef enum {
137 	TOKEN_CLASS_IDENTIFIER,
138 	TOKEN_CLASS_IDENTIFIER_FINALIZED,
139 	TOKEN_CLASS_FUNC_MACRO,
140 	TOKEN_CLASS_OBJ_MACRO
141 } token_class_t;
142 
143 token_class_t
144 glcpp_parser_classify_token (glcpp_parser_t *parser,
145 			     const char *identifier,
146 			     int *parameter_index);
147 
148 typedef struct {
149 	int is_function;
150 	string_list_t *parameters;
151 	const char *identifier;
152 	token_list_t *replacements;
153 } macro_t;
154 
155 typedef struct expansion_node {
156 	macro_t *macro;
157 	token_node_t *replacements;
158 	struct expansion_node *next;
159 } expansion_node_t;
160 
161 typedef enum skip_type {
162 	SKIP_NO_SKIP,
163 	SKIP_TO_ELSE,
164 	SKIP_TO_ENDIF
165 } skip_type_t;
166 
167 typedef struct skip_node {
168 	skip_type_t type;
169 	bool has_else;
170 	YYLTYPE loc; /* location of the initial #if/#elif/... */
171 	struct skip_node *next;
172 } skip_node_t;
173 
174 typedef struct active_list {
175 	const char *identifier;
176 	token_node_t *marker;
177 	struct active_list *next;
178 } active_list_t;
179 
180 struct _mesa_glsl_parse_state;
181 
182 typedef void (*glcpp_extension_iterator)(
183 		struct _mesa_glsl_parse_state *state,
184 		void (*add_builtin_define)(glcpp_parser_t *, const char *, int),
185 		glcpp_parser_t *data,
186 		unsigned version,
187 		bool es);
188 
189 struct glcpp_parser {
190 	linear_ctx *linalloc;
191 	yyscan_t scanner;
192 	struct hash_table *defines;
193 	active_list_t *active;
194 	int lexing_directive;
195 	int lexing_version_directive;
196 	int space_tokens;
197 	int last_token_was_newline;
198 	int last_token_was_space;
199 	int first_non_space_token_this_line;
200 	int newline_as_space;
201 	int in_control_line;
202 	bool in_define;
203 	int paren_count;
204 	int commented_newlines;
205 	skip_node_t *skip_stack;
206 	int skipping;
207 	token_list_t *lex_from_list;
208 	token_node_t *lex_from_node;
209 	struct _mesa_string_buffer *output;
210 	struct _mesa_string_buffer *info_log;
211 	int error;
212 	glcpp_extension_iterator extensions;
213 	const struct gl_extensions *extension_list;
214 	void *state;
215 	gl_api api;
216 	struct gl_context *gl_ctx;
217 	unsigned version;
218 
219 	/**
220 	 * Has the #version been set?
221 	 *
222 	 * A separate flag is used because any possible sentinel value in
223 	 * \c ::version could also be set by a #version line.
224 	 */
225 	bool version_set;
226 
227 	bool has_new_line_number;
228 	int new_line_number;
229 	bool has_new_source_number;
230 	int new_source_number;
231 	bool is_gles;
232 };
233 
234 glcpp_parser_t *
235 glcpp_parser_create(struct gl_context *gl_ctx,
236                     glcpp_extension_iterator extensions, void *state);
237 
238 int
239 glcpp_parser_parse (glcpp_parser_t *parser);
240 
241 void
242 glcpp_parser_destroy (glcpp_parser_t *parser);
243 
244 void
245 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser);
246 
247 int
248 glcpp_preprocess(void *ralloc_ctx, const char **shader, char **info_log,
249 		 glcpp_extension_iterator extensions, void *state,
250 		 struct gl_context *g_ctx);
251 
252 /* Functions for writing to the info log */
253 
254 void
255 glcpp_error (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
256 
257 void
258 glcpp_warning (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
259 
260 /* Generated by glcpp-lex.l to glcpp-lex.c */
261 
262 int
263 glcpp_lex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner);
264 
265 void
266 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader);
267 
268 int
269 glcpp_lex (YYSTYPE *lvalp, YYLTYPE *llocp, yyscan_t scanner);
270 
271 int
272 glcpp_lex_destroy (yyscan_t scanner);
273 
274 /* Generated by glcpp-parse.y to glcpp-parse.c */
275 
276 int
277 yyparse (glcpp_parser_t *parser);
278 
279 #endif
280