glsl2: Move the compiler to the subdirectory it will live in in Mesa.
[mesa.git] / src / 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
29 #include <talloc.h>
30
31 #include "hash_table.h"
32
33 #define yyscan_t void*
34
35 /* Some data types used for parser values. */
36
37 typedef struct string_node {
38 const char *str;
39 struct string_node *next;
40 } string_node_t;
41
42 typedef struct string_list {
43 string_node_t *head;
44 string_node_t *tail;
45 } string_list_t;
46
47 typedef struct token token_t;
48 typedef struct token_list token_list_t;
49
50 typedef union YYSTYPE
51 {
52 intmax_t ival;
53 char *str;
54 string_list_t *string_list;
55 token_t *token;
56 token_list_t *token_list;
57 } YYSTYPE;
58
59 # define YYSTYPE_IS_TRIVIAL 1
60 # define YYSTYPE_IS_DECLARED 1
61
62 typedef struct YYLTYPE {
63 int first_line;
64 int first_column;
65 int last_line;
66 int last_column;
67 unsigned source;
68 } YYLTYPE;
69 # define YYLTYPE_IS_DECLARED 1
70 # define YYLTYPE_IS_TRIVIAL 1
71
72 struct token {
73 int type;
74 YYSTYPE value;
75 YYLTYPE location;
76 };
77
78 typedef struct token_node {
79 token_t *token;
80 struct token_node *next;
81 } token_node_t;
82
83 struct token_list {
84 token_node_t *head;
85 token_node_t *tail;
86 token_node_t *non_space_tail;
87 };
88
89 typedef struct argument_node {
90 token_list_t *argument;
91 struct argument_node *next;
92 } argument_node_t;
93
94 typedef struct argument_list {
95 argument_node_t *head;
96 argument_node_t *tail;
97 } argument_list_t;
98
99 typedef struct glcpp_parser glcpp_parser_t;
100
101 typedef enum {
102 TOKEN_CLASS_IDENTIFIER,
103 TOKEN_CLASS_IDENTIFIER_FINALIZED,
104 TOKEN_CLASS_FUNC_MACRO,
105 TOKEN_CLASS_OBJ_MACRO
106 } token_class_t;
107
108 token_class_t
109 glcpp_parser_classify_token (glcpp_parser_t *parser,
110 const char *identifier,
111 int *parameter_index);
112
113 typedef struct {
114 int is_function;
115 string_list_t *parameters;
116 const char *identifier;
117 token_list_t *replacements;
118 } macro_t;
119
120 typedef struct expansion_node {
121 macro_t *macro;
122 token_node_t *replacements;
123 struct expansion_node *next;
124 } expansion_node_t;
125
126 typedef enum skip_type {
127 SKIP_NO_SKIP,
128 SKIP_TO_ELSE,
129 SKIP_TO_ENDIF
130 } skip_type_t;
131
132 typedef struct skip_node {
133 skip_type_t type;
134 YYLTYPE loc; /* location of the initial #if/#elif/... */
135 struct skip_node *next;
136 } skip_node_t;
137
138 typedef struct active_list {
139 const char *identifier;
140 token_node_t *marker;
141 struct active_list *next;
142 } active_list_t;
143
144 struct glcpp_parser {
145 yyscan_t scanner;
146 struct hash_table *defines;
147 active_list_t *active;
148 int lexing_if;
149 int space_tokens;
150 int newline_as_space;
151 int in_control_line;
152 int paren_count;
153 skip_node_t *skip_stack;
154 token_list_t *lex_from_list;
155 token_node_t *lex_from_node;
156 char *output;
157 char *info_log;
158 int error;
159 };
160
161 glcpp_parser_t *
162 glcpp_parser_create (void);
163
164 int
165 glcpp_parser_parse (glcpp_parser_t *parser);
166
167 void
168 glcpp_parser_destroy (glcpp_parser_t *parser);
169
170 int
171 preprocess(void *talloc_ctx, const char **shader, char **info_log);
172
173 /* Functions for writing to the info log */
174
175 void
176 glcpp_error (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
177
178 void
179 glcpp_warning (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
180
181 /* Generated by glcpp-lex.l to glcpp-lex.c */
182
183 int
184 glcpp_lex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner);
185
186 void
187 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader);
188
189 int
190 glcpp_lex (YYSTYPE *lvalp, YYLTYPE *llocp, yyscan_t scanner);
191
192 int
193 glcpp_lex_destroy (yyscan_t scanner);
194
195 /* Generated by glcpp-parse.y to glcpp-parse.c */
196
197 int
198 yyparse (glcpp_parser_t *parser);
199
200 /* xtalloc - wrappers around talloc to check for out-of-memory */
201
202 #define xtalloc(ctx, type) (type *)xtalloc_named_const(ctx, sizeof(type), #type)
203
204 #define xtalloc_size(ctx, size) xtalloc_named_const(ctx, size, __location__)
205
206 void *
207 xtalloc_named_const (const void *context, size_t size, const char *name);
208
209 char *
210 xtalloc_strdup (const void *t, const char *p);
211
212 char *
213 xtalloc_strndup (const void *t, const char *p, size_t n);
214
215 char *
216 xtalloc_asprintf (const void *t, const char *fmt, ...);
217
218 void *
219 _xtalloc_reference_loc (const void *context,
220 const void *ptr, const char *location);
221
222 #define xtalloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_xtalloc_reference_loc((ctx),(ptr), __location__)
223
224 #endif