glsl: move to compiler/
[mesa.git] / src / compiler / glsl / glcpp / glcpp.h
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/mtypes.h"
31
32 #include "util/ralloc.h"
33
34 #include "program/hash_table.h"
35
36 #define yyscan_t void*
37
38 /* Some data types used for parser values. */
39
40 typedef struct expression_value {
41 intmax_t value;
42 char *undefined_macro;
43 } expression_value_t;
44
45
46 typedef struct string_node {
47 const char *str;
48 struct string_node *next;
49 } string_node_t;
50
51 typedef struct string_list {
52 string_node_t *head;
53 string_node_t *tail;
54 } string_list_t;
55
56 typedef struct token token_t;
57 typedef struct token_list token_list_t;
58
59 typedef union YYSTYPE
60 {
61 intmax_t ival;
62 expression_value_t expression_value;
63 char *str;
64 string_list_t *string_list;
65 token_t *token;
66 token_list_t *token_list;
67 } YYSTYPE;
68
69 # define YYSTYPE_IS_TRIVIAL 1
70 # define YYSTYPE_IS_DECLARED 1
71
72 typedef struct YYLTYPE {
73 int first_line;
74 int first_column;
75 int last_line;
76 int last_column;
77 unsigned source;
78 } YYLTYPE;
79 # define YYLTYPE_IS_DECLARED 1
80 # define YYLTYPE_IS_TRIVIAL 1
81
82 # define YYLLOC_DEFAULT(Current, Rhs, N) \
83 do { \
84 if (N) \
85 { \
86 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
87 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
88 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
89 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
90 } \
91 else \
92 { \
93 (Current).first_line = (Current).last_line = \
94 YYRHSLOC(Rhs, 0).last_line; \
95 (Current).first_column = (Current).last_column = \
96 YYRHSLOC(Rhs, 0).last_column; \
97 } \
98 (Current).source = 0; \
99 } while (0)
100
101 struct token {
102 int type;
103 YYSTYPE value;
104 YYLTYPE location;
105 };
106
107 typedef struct token_node {
108 token_t *token;
109 struct token_node *next;
110 } token_node_t;
111
112 struct token_list {
113 token_node_t *head;
114 token_node_t *tail;
115 token_node_t *non_space_tail;
116 };
117
118 typedef struct argument_node {
119 token_list_t *argument;
120 struct argument_node *next;
121 } argument_node_t;
122
123 typedef struct argument_list {
124 argument_node_t *head;
125 argument_node_t *tail;
126 } argument_list_t;
127
128 typedef struct glcpp_parser glcpp_parser_t;
129
130 typedef enum {
131 TOKEN_CLASS_IDENTIFIER,
132 TOKEN_CLASS_IDENTIFIER_FINALIZED,
133 TOKEN_CLASS_FUNC_MACRO,
134 TOKEN_CLASS_OBJ_MACRO
135 } token_class_t;
136
137 token_class_t
138 glcpp_parser_classify_token (glcpp_parser_t *parser,
139 const char *identifier,
140 int *parameter_index);
141
142 typedef struct {
143 int is_function;
144 string_list_t *parameters;
145 const char *identifier;
146 token_list_t *replacements;
147 } macro_t;
148
149 typedef struct expansion_node {
150 macro_t *macro;
151 token_node_t *replacements;
152 struct expansion_node *next;
153 } expansion_node_t;
154
155 typedef enum skip_type {
156 SKIP_NO_SKIP,
157 SKIP_TO_ELSE,
158 SKIP_TO_ENDIF
159 } skip_type_t;
160
161 typedef struct skip_node {
162 skip_type_t type;
163 bool has_else;
164 YYLTYPE loc; /* location of the initial #if/#elif/... */
165 struct skip_node *next;
166 } skip_node_t;
167
168 typedef struct active_list {
169 const char *identifier;
170 token_node_t *marker;
171 struct active_list *next;
172 } active_list_t;
173
174 struct glcpp_parser {
175 yyscan_t scanner;
176 struct hash_table *defines;
177 active_list_t *active;
178 int lexing_directive;
179 int space_tokens;
180 int last_token_was_newline;
181 int last_token_was_space;
182 int first_non_space_token_this_line;
183 int newline_as_space;
184 int in_control_line;
185 int paren_count;
186 int commented_newlines;
187 skip_node_t *skip_stack;
188 int skipping;
189 token_list_t *lex_from_list;
190 token_node_t *lex_from_node;
191 char *output;
192 char *info_log;
193 size_t output_length;
194 size_t info_log_length;
195 int error;
196 const struct gl_extensions *extensions;
197 gl_api api;
198 bool version_resolved;
199 bool has_new_line_number;
200 int new_line_number;
201 bool has_new_source_number;
202 int new_source_number;
203 bool is_gles;
204 };
205
206 struct gl_extensions;
207
208 glcpp_parser_t *
209 glcpp_parser_create (const struct gl_extensions *extensions, gl_api api);
210
211 int
212 glcpp_parser_parse (glcpp_parser_t *parser);
213
214 void
215 glcpp_parser_destroy (glcpp_parser_t *parser);
216
217 void
218 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser);
219
220 int
221 glcpp_preprocess(void *ralloc_ctx, const char **shader, char **info_log,
222 const struct gl_extensions *extensions, struct gl_context *g_ctx);
223
224 /* Functions for writing to the info log */
225
226 void
227 glcpp_error (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
228
229 void
230 glcpp_warning (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
231
232 /* Generated by glcpp-lex.l to glcpp-lex.c */
233
234 int
235 glcpp_lex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner);
236
237 void
238 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader);
239
240 int
241 glcpp_lex (YYSTYPE *lvalp, YYLTYPE *llocp, yyscan_t scanner);
242
243 int
244 glcpp_lex_destroy (yyscan_t scanner);
245
246 /* Generated by glcpp-parse.y to glcpp-parse.c */
247
248 int
249 yyparse (glcpp_parser_t *parser);
250
251 #endif