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