blob: 0ccd957eda3c7207f79afcfbda14964c98d20d7f [file] [log] [blame]
Carl Wortha1e32bc2010-05-10 13:17:25 -07001/*
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
Carl Worth35419092010-05-24 11:27:23 -070027#include <stdint.h>
28
Carl Wortha807fb72010-05-18 22:10:04 -070029#include <talloc.h>
30
Aras Pranckevicius31747152010-07-29 12:40:49 +030031#include "program/hash_table.h"
Carl Wortha1e32bc2010-05-10 13:17:25 -070032
33#define yyscan_t void*
34
Carl Wortha807fb72010-05-18 22:10:04 -070035/* Some data types used for parser values. */
Carl Worth0b27b5f2010-05-10 16:16:06 -070036
Carl Worth610053b2010-05-14 10:05:11 -070037typedef struct string_node {
Carl Worth33cc4002010-05-12 12:17:10 -070038 const char *str;
Carl Worth610053b2010-05-14 10:05:11 -070039 struct string_node *next;
40} string_node_t;
Carl Worth33cc4002010-05-12 12:17:10 -070041
Carl Worth610053b2010-05-14 10:05:11 -070042typedef struct string_list {
43 string_node_t *head;
44 string_node_t *tail;
45} string_list_t;
Carl Worth33cc4002010-05-12 12:17:10 -070046
Carl Worth808401f2010-05-25 14:52:43 -070047typedef struct token token_t;
48typedef struct token_list token_list_t;
49
50typedef union YYSTYPE
51{
Carl Worthf6914fd2010-05-26 09:32:57 -070052 intmax_t ival;
Carl Worth808401f2010-05-25 14:52:43 -070053 char *str;
Carl Worthb1854fd2010-05-25 16:28:26 -070054 string_list_t *string_list;
Carl Worth808401f2010-05-25 14:52:43 -070055 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
Kenneth Graunke465e03e2010-06-16 16:35:57 -070062typedef 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
Carl Wortha0cfe8c2010-07-21 13:43:47 -070072# define YYLLOC_DEFAULT(Current, Rhs, N) \
73do { \
74 if (N) \
75 { \
76 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
77 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
78 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
79 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
80 } \
81 else \
82 { \
83 (Current).first_line = (Current).last_line = \
84 YYRHSLOC(Rhs, 0).last_line; \
85 (Current).first_column = (Current).last_column = \
86 YYRHSLOC(Rhs, 0).last_column; \
87 } \
88 (Current).source = 0; \
89} while (0)
90
Carl Worth808401f2010-05-25 14:52:43 -070091struct token {
Carl Worthb5693832010-05-20 08:01:44 -070092 int type;
Carl Worth808401f2010-05-25 14:52:43 -070093 YYSTYPE value;
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -070094 YYLTYPE location;
Carl Worth808401f2010-05-25 14:52:43 -070095};
Carl Worthb5693832010-05-20 08:01:44 -070096
Carl Worth47252442010-05-19 13:54:37 -070097typedef struct token_node {
Carl Worth808401f2010-05-25 14:52:43 -070098 token_t *token;
Carl Worth47252442010-05-19 13:54:37 -070099 struct token_node *next;
100} token_node_t;
101
Carl Worth808401f2010-05-25 14:52:43 -0700102struct token_list {
Carl Worth47252442010-05-19 13:54:37 -0700103 token_node_t *head;
104 token_node_t *tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700105 token_node_t *non_space_tail;
Carl Worth808401f2010-05-25 14:52:43 -0700106};
Carl Worth47252442010-05-19 13:54:37 -0700107
Carl Worth8f6a8282010-05-14 10:44:19 -0700108typedef struct argument_node {
Carl Worth47252442010-05-19 13:54:37 -0700109 token_list_t *argument;
Carl Worth8f6a8282010-05-14 10:44:19 -0700110 struct argument_node *next;
111} argument_node_t;
112
113typedef struct argument_list {
114 argument_node_t *head;
115 argument_node_t *tail;
116} argument_list_t;
117
Carl Worth33cc4002010-05-12 12:17:10 -0700118typedef struct glcpp_parser glcpp_parser_t;
119
Carl Wortha807fb72010-05-18 22:10:04 -0700120typedef enum {
Carl Wortha807fb72010-05-18 22:10:04 -0700121 TOKEN_CLASS_IDENTIFIER,
Carl Worthb5693832010-05-20 08:01:44 -0700122 TOKEN_CLASS_IDENTIFIER_FINALIZED,
Carl Wortha807fb72010-05-18 22:10:04 -0700123 TOKEN_CLASS_FUNC_MACRO,
124 TOKEN_CLASS_OBJ_MACRO
125} token_class_t;
126
127token_class_t
128glcpp_parser_classify_token (glcpp_parser_t *parser,
129 const char *identifier,
130 int *parameter_index);
131
132typedef struct {
133 int is_function;
134 string_list_t *parameters;
135 const char *identifier;
Carl Worth47252442010-05-19 13:54:37 -0700136 token_list_t *replacements;
Carl Wortha807fb72010-05-18 22:10:04 -0700137} macro_t;
138
139typedef struct expansion_node {
140 macro_t *macro;
Carl Worth47252442010-05-19 13:54:37 -0700141 token_node_t *replacements;
Carl Wortha807fb72010-05-18 22:10:04 -0700142 struct expansion_node *next;
143} expansion_node_t;
144
Carl Worthb20d33c2010-05-20 22:27:07 -0700145typedef enum skip_type {
146 SKIP_NO_SKIP,
147 SKIP_TO_ELSE,
148 SKIP_TO_ENDIF
149} skip_type_t;
150
151typedef struct skip_node {
152 skip_type_t type;
Kenneth Graunke07745232010-06-17 12:41:46 -0700153 YYLTYPE loc; /* location of the initial #if/#elif/... */
Carl Worthb20d33c2010-05-20 22:27:07 -0700154 struct skip_node *next;
155} skip_node_t;
156
Carl Worth22b3ace2010-06-02 15:32:03 -0700157typedef struct active_list {
158 const char *identifier;
159 token_node_t *marker;
160 struct active_list *next;
161} active_list_t;
162
Carl Wortha807fb72010-05-18 22:10:04 -0700163struct glcpp_parser {
164 yyscan_t scanner;
165 struct hash_table *defines;
Carl Worth22b3ace2010-06-02 15:32:03 -0700166 active_list_t *active;
Carl Wortha771a402010-06-01 11:20:18 -0700167 int lexing_if;
Carl Worthf34a0002010-05-25 16:59:02 -0700168 int space_tokens;
Carl Worth95951ea2010-05-26 15:57:10 -0700169 int newline_as_space;
170 int in_control_line;
171 int paren_count;
Carl Worthb20d33c2010-05-20 22:27:07 -0700172 skip_node_t *skip_stack;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700173 token_list_t *lex_from_list;
174 token_node_t *lex_from_node;
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700175 char *output;
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700176 char *info_log;
Kenneth Graunke62b4b772010-06-21 12:39:49 -0700177 int error;
Carl Wortha807fb72010-05-18 22:10:04 -0700178};
179
Ian Romanick06143ea2010-06-30 16:27:22 -0700180struct gl_extensions;
181
Carl Worth33cc4002010-05-12 12:17:10 -0700182glcpp_parser_t *
Ian Romanick06143ea2010-06-30 16:27:22 -0700183glcpp_parser_create (const struct gl_extensions *extensions);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700184
185int
186glcpp_parser_parse (glcpp_parser_t *parser);
187
188void
Carl Worth33cc4002010-05-12 12:17:10 -0700189glcpp_parser_destroy (glcpp_parser_t *parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700190
Kenneth Graunke186e2632010-06-23 14:00:27 -0700191int
Ian Romanick06143ea2010-06-30 16:27:22 -0700192preprocess(void *talloc_ctx, const char **shader, char **info_log,
193 const struct gl_extensions *extensions);
Kenneth Graunke186e2632010-06-23 14:00:27 -0700194
Kenneth Graunkec9529c42010-06-18 19:54:25 -0700195/* Functions for writing to the info log */
196
Kenneth Graunkef1e6c062010-06-17 12:03:25 -0700197void
198glcpp_error (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
199
Kenneth Graunkec9529c42010-06-18 19:54:25 -0700200void
201glcpp_warning (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
202
Carl Worth0b27b5f2010-05-10 16:16:06 -0700203/* Generated by glcpp-lex.l to glcpp-lex.c */
204
Carl Wortha1e32bc2010-05-10 13:17:25 -0700205int
Carl Worth8f38aff2010-05-19 10:01:29 -0700206glcpp_lex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700207
Kenneth Graunke1b1f43e2010-06-16 12:01:17 -0700208void
209glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader);
210
Carl Wortha1e32bc2010-05-10 13:17:25 -0700211int
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700212glcpp_lex (YYSTYPE *lvalp, YYLTYPE *llocp, yyscan_t scanner);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700213
214int
Carl Worth8f38aff2010-05-19 10:01:29 -0700215glcpp_lex_destroy (yyscan_t scanner);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700216
217/* Generated by glcpp-parse.y to glcpp-parse.c */
218
219int
Carl Worth0b27b5f2010-05-10 16:16:06 -0700220yyparse (glcpp_parser_t *parser);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700221
Carl Worth5070a202010-05-12 12:45:33 -0700222/* xtalloc - wrappers around talloc to check for out-of-memory */
223
224#define xtalloc(ctx, type) (type *)xtalloc_named_const(ctx, sizeof(type), #type)
225
Carl Wortha807fb72010-05-18 22:10:04 -0700226#define xtalloc_size(ctx, size) xtalloc_named_const(ctx, size, __location__)
227
Carl Worth5070a202010-05-12 12:45:33 -0700228void *
229xtalloc_named_const (const void *context, size_t size, const char *name);
230
231char *
232xtalloc_strdup (const void *t, const char *p);
233
Carl Wortha807fb72010-05-18 22:10:04 -0700234char *
235xtalloc_strndup (const void *t, const char *p, size_t n);
236
Carl Worthb8945832010-05-20 15:02:03 -0700237char *
238xtalloc_asprintf (const void *t, const char *fmt, ...);
239
Carl Worth9bb796f2010-05-25 14:40:47 -0700240void *
241_xtalloc_reference_loc (const void *context,
242 const void *ptr, const char *location);
243
244#define xtalloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_xtalloc_reference_loc((ctx),(ptr), __location__)
245
Carl Wortha1e32bc2010-05-10 13:17:25 -0700246#endif