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