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