blob: 1d139af064fbb23108909257146fd44cf3d2b57f [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
Carl Worth0b27b5f2010-05-10 16:16:06 -070031#include "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 Worth808401f2010-05-25 14:52:43 -070072struct token {
Carl Worthb5693832010-05-20 08:01:44 -070073 int type;
Carl Worth808401f2010-05-25 14:52:43 -070074 YYSTYPE value;
75};
Carl Worthb5693832010-05-20 08:01:44 -070076
Carl Worth47252442010-05-19 13:54:37 -070077typedef struct token_node {
Carl Worth808401f2010-05-25 14:52:43 -070078 token_t *token;
Carl Worth47252442010-05-19 13:54:37 -070079 struct token_node *next;
80} token_node_t;
81
Carl Worth808401f2010-05-25 14:52:43 -070082struct token_list {
Carl Worth47252442010-05-19 13:54:37 -070083 token_node_t *head;
84 token_node_t *tail;
Carl Worth10ae4382010-05-25 20:35:01 -070085 token_node_t *non_space_tail;
Carl Worth808401f2010-05-25 14:52:43 -070086};
Carl Worth47252442010-05-19 13:54:37 -070087
Carl Worth8f6a8282010-05-14 10:44:19 -070088typedef struct argument_node {
Carl Worth47252442010-05-19 13:54:37 -070089 token_list_t *argument;
Carl Worth8f6a8282010-05-14 10:44:19 -070090 struct argument_node *next;
91} argument_node_t;
92
93typedef struct argument_list {
94 argument_node_t *head;
95 argument_node_t *tail;
96} argument_list_t;
97
Carl Worth33cc4002010-05-12 12:17:10 -070098typedef struct glcpp_parser glcpp_parser_t;
99
Carl Wortha807fb72010-05-18 22:10:04 -0700100typedef enum {
Carl Wortha807fb72010-05-18 22:10:04 -0700101 TOKEN_CLASS_IDENTIFIER,
Carl Worthb5693832010-05-20 08:01:44 -0700102 TOKEN_CLASS_IDENTIFIER_FINALIZED,
Carl Wortha807fb72010-05-18 22:10:04 -0700103 TOKEN_CLASS_FUNC_MACRO,
104 TOKEN_CLASS_OBJ_MACRO
105} token_class_t;
106
107token_class_t
108glcpp_parser_classify_token (glcpp_parser_t *parser,
109 const char *identifier,
110 int *parameter_index);
111
112typedef struct {
113 int is_function;
114 string_list_t *parameters;
115 const char *identifier;
Carl Worth47252442010-05-19 13:54:37 -0700116 token_list_t *replacements;
Carl Wortha807fb72010-05-18 22:10:04 -0700117} macro_t;
118
119typedef struct expansion_node {
120 macro_t *macro;
Carl Worth47252442010-05-19 13:54:37 -0700121 token_node_t *replacements;
Carl Wortha807fb72010-05-18 22:10:04 -0700122 struct expansion_node *next;
123} expansion_node_t;
124
Carl Worthb20d33c2010-05-20 22:27:07 -0700125typedef enum skip_type {
126 SKIP_NO_SKIP,
127 SKIP_TO_ELSE,
128 SKIP_TO_ENDIF
129} skip_type_t;
130
131typedef struct skip_node {
132 skip_type_t type;
133 struct skip_node *next;
134} skip_node_t;
135
Carl Worth22b3ace2010-06-02 15:32:03 -0700136typedef struct active_list {
137 const char *identifier;
138 token_node_t *marker;
139 struct active_list *next;
140} active_list_t;
141
Carl Wortha807fb72010-05-18 22:10:04 -0700142struct glcpp_parser {
143 yyscan_t scanner;
144 struct hash_table *defines;
Carl Worth22b3ace2010-06-02 15:32:03 -0700145 active_list_t *active;
Carl Wortha771a402010-06-01 11:20:18 -0700146 int lexing_if;
Carl Worthf34a0002010-05-25 16:59:02 -0700147 int space_tokens;
Carl Worth95951ea2010-05-26 15:57:10 -0700148 int newline_as_space;
149 int in_control_line;
150 int paren_count;
Carl Worthb20d33c2010-05-20 22:27:07 -0700151 skip_node_t *skip_stack;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700152 token_list_t *lex_from_list;
153 token_node_t *lex_from_node;
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700154 char *output;
155 char *errors;
Carl Wortha807fb72010-05-18 22:10:04 -0700156};
157
Carl Worth33cc4002010-05-12 12:17:10 -0700158glcpp_parser_t *
159glcpp_parser_create (void);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700160
161int
162glcpp_parser_parse (glcpp_parser_t *parser);
163
164void
Carl Worth33cc4002010-05-12 12:17:10 -0700165glcpp_parser_destroy (glcpp_parser_t *parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700166
167/* Generated by glcpp-lex.l to glcpp-lex.c */
168
Carl Wortha1e32bc2010-05-10 13:17:25 -0700169int
Carl Worth8f38aff2010-05-19 10:01:29 -0700170glcpp_lex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700171
Kenneth Graunke1b1f43e2010-06-16 12:01:17 -0700172void
173glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader);
174
Carl Wortha1e32bc2010-05-10 13:17:25 -0700175int
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700176glcpp_lex (YYSTYPE *lvalp, YYLTYPE *llocp, yyscan_t scanner);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700177
178int
Carl Worth8f38aff2010-05-19 10:01:29 -0700179glcpp_lex_destroy (yyscan_t scanner);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700180
181/* Generated by glcpp-parse.y to glcpp-parse.c */
182
183int
Carl Worth0b27b5f2010-05-10 16:16:06 -0700184yyparse (glcpp_parser_t *parser);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700185
Carl Worth5070a202010-05-12 12:45:33 -0700186/* xtalloc - wrappers around talloc to check for out-of-memory */
187
188#define xtalloc(ctx, type) (type *)xtalloc_named_const(ctx, sizeof(type), #type)
189
Carl Wortha807fb72010-05-18 22:10:04 -0700190#define xtalloc_size(ctx, size) xtalloc_named_const(ctx, size, __location__)
191
Carl Worth5070a202010-05-12 12:45:33 -0700192void *
193xtalloc_named_const (const void *context, size_t size, const char *name);
194
195char *
196xtalloc_strdup (const void *t, const char *p);
197
Carl Wortha807fb72010-05-18 22:10:04 -0700198char *
199xtalloc_strndup (const void *t, const char *p, size_t n);
200
Carl Worthb8945832010-05-20 15:02:03 -0700201char *
202xtalloc_asprintf (const void *t, const char *fmt, ...);
203
Carl Worth9bb796f2010-05-25 14:40:47 -0700204void *
205_xtalloc_reference_loc (const void *context,
206 const void *ptr, const char *location);
207
208#define xtalloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_xtalloc_reference_loc((ctx),(ptr), __location__)
209
Carl Wortha1e32bc2010-05-10 13:17:25 -0700210#endif