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    }								\
95    else								\
96    {								\
97       (Current).first_line   = (Current).last_line =		\
98 	 YYRHSLOC(Rhs, 0).last_line;				\
99       (Current).first_column = (Current).last_column =		\
100 	 YYRHSLOC(Rhs, 0).last_column;				\
101    }								\
102    (Current).source = 0;					\
103 } while (0)
104 
105 struct token {
106 	int type;
107 	YYSTYPE value;
108 	YYLTYPE location;
109 };
110 
111 typedef struct token_node {
112 	token_t *token;
113 	struct token_node *next;
114 } token_node_t;
115 
116 struct token_list {
117 	token_node_t *head;
118 	token_node_t *tail;
119 	token_node_t *non_space_tail;
120 };
121 
122 typedef struct argument_node {
123 	token_list_t *argument;
124 	struct argument_node *next;
125 } argument_node_t;
126 
127 typedef struct argument_list {
128 	argument_node_t *head;
129 	argument_node_t *tail;
130 } argument_list_t;
131 
132 typedef struct glcpp_parser glcpp_parser_t;
133 
134 typedef enum {
135 	TOKEN_CLASS_IDENTIFIER,
136 	TOKEN_CLASS_IDENTIFIER_FINALIZED,
137 	TOKEN_CLASS_FUNC_MACRO,
138 	TOKEN_CLASS_OBJ_MACRO
139 } token_class_t;
140 
141 token_class_t
142 glcpp_parser_classify_token (glcpp_parser_t *parser,
143 			     const char *identifier,
144 			     int *parameter_index);
145 
146 typedef struct {
147 	int is_function;
148 	string_list_t *parameters;
149 	const char *identifier;
150 	token_list_t *replacements;
151 } macro_t;
152 
153 typedef struct expansion_node {
154 	macro_t *macro;
155 	token_node_t *replacements;
156 	struct expansion_node *next;
157 } expansion_node_t;
158 
159 typedef enum skip_type {
160 	SKIP_NO_SKIP,
161 	SKIP_TO_ELSE,
162 	SKIP_TO_ENDIF
163 } skip_type_t;
164 
165 typedef struct skip_node {
166 	skip_type_t type;
167 	bool has_else;
168 	YYLTYPE loc; /* location of the initial #if/#elif/... */
169 	struct skip_node *next;
170 } skip_node_t;
171 
172 typedef struct active_list {
173 	const char *identifier;
174 	token_node_t *marker;
175 	struct active_list *next;
176 } active_list_t;
177 
178 struct _mesa_glsl_parse_state;
179 
180 typedef void (*glcpp_extension_iterator)(
181 		struct _mesa_glsl_parse_state *state,
182 		void (*add_builtin_define)(glcpp_parser_t *, const char *, int),
183 		glcpp_parser_t *data,
184 		unsigned version,
185 		bool es);
186 
187 struct glcpp_parser {
188 	void *linalloc;
189 	yyscan_t scanner;
190 	struct hash_table *defines;
191 	active_list_t *active;
192 	int lexing_directive;
193 	int lexing_version_directive;
194 	int space_tokens;
195 	int last_token_was_newline;
196 	int last_token_was_space;
197 	int first_non_space_token_this_line;
198 	int newline_as_space;
199 	int in_control_line;
200 	bool in_define;
201 	int paren_count;
202 	int commented_newlines;
203 	skip_node_t *skip_stack;
204 	int skipping;
205 	token_list_t *lex_from_list;
206 	token_node_t *lex_from_node;
207 	struct _mesa_string_buffer *output;
208 	struct _mesa_string_buffer *info_log;
209 	int error;
210 	glcpp_extension_iterator extensions;
211 	const struct gl_extensions *extension_list;
212 	void *state;
213 	gl_api api;
214 	struct gl_context *gl_ctx;
215 	unsigned version;
216 
217 	/**
218 	 * Has the #version been set?
219 	 *
220 	 * A separate flag is used because any possible sentinel value in
221 	 * \c ::version could also be set by a #version line.
222 	 */
223 	bool version_set;
224 
225 	bool has_new_line_number;
226 	int new_line_number;
227 	bool has_new_source_number;
228 	int new_source_number;
229 	bool is_gles;
230 };
231 
232 glcpp_parser_t *
233 glcpp_parser_create(struct gl_context *gl_ctx,
234                     glcpp_extension_iterator extensions, void *state);
235 
236 int
237 glcpp_parser_parse (glcpp_parser_t *parser);
238 
239 void
240 glcpp_parser_destroy (glcpp_parser_t *parser);
241 
242 void
243 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser);
244 
245 int
246 glcpp_preprocess(void *ralloc_ctx, const char **shader, char **info_log,
247 		 glcpp_extension_iterator extensions, void *state,
248 		 struct gl_context *g_ctx);
249 
250 /* Functions for writing to the info log */
251 
252 void
253 glcpp_error (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
254 
255 void
256 glcpp_warning (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
257 
258 /* Generated by glcpp-lex.l to glcpp-lex.c */
259 
260 int
261 glcpp_lex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner);
262 
263 void
264 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader);
265 
266 int
267 glcpp_lex (YYSTYPE *lvalp, YYLTYPE *llocp, yyscan_t scanner);
268 
269 int
270 glcpp_lex_destroy (yyscan_t scanner);
271 
272 /* Generated by glcpp-parse.y to glcpp-parse.c */
273 
274 int
275 yyparse (glcpp_parser_t *parser);
276 
277 #endif
278