Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 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 | |
Carl Worth | 3541909 | 2010-05-24 11:27:23 -0700 | [diff] [blame] | 27 | #include <stdint.h> |
| 28 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 29 | #include <talloc.h> |
| 30 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 31 | #include "hash_table.h" |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 32 | |
| 33 | #define yyscan_t void* |
| 34 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 35 | /* Some data types used for parser values. */ |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 36 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 37 | typedef struct string_node { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 38 | const char *str; |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 39 | struct string_node *next; |
| 40 | } string_node_t; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 41 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 42 | typedef struct string_list { |
| 43 | string_node_t *head; |
| 44 | string_node_t *tail; |
| 45 | } string_list_t; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 46 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 47 | typedef struct token token_t; |
| 48 | typedef struct token_list token_list_t; |
| 49 | |
| 50 | typedef union YYSTYPE |
| 51 | { |
Carl Worth | f6914fd | 2010-05-26 09:32:57 -0700 | [diff] [blame] | 52 | intmax_t ival; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 53 | char *str; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 54 | string_list_t *string_list; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 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 | |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 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 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 72 | struct token { |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 73 | int type; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 74 | YYSTYPE value; |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 75 | YYLTYPE location; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 76 | }; |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 77 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 78 | typedef struct token_node { |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 79 | token_t *token; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 80 | struct token_node *next; |
| 81 | } token_node_t; |
| 82 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 83 | struct token_list { |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 84 | token_node_t *head; |
| 85 | token_node_t *tail; |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 86 | token_node_t *non_space_tail; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 87 | }; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 88 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 89 | typedef struct argument_node { |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 90 | token_list_t *argument; |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 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 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 99 | typedef struct glcpp_parser glcpp_parser_t; |
| 100 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 101 | typedef enum { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 102 | TOKEN_CLASS_IDENTIFIER, |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 103 | TOKEN_CLASS_IDENTIFIER_FINALIZED, |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 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; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 117 | token_list_t *replacements; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 118 | } macro_t; |
| 119 | |
| 120 | typedef struct expansion_node { |
| 121 | macro_t *macro; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 122 | token_node_t *replacements; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 123 | struct expansion_node *next; |
| 124 | } expansion_node_t; |
| 125 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 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 | struct skip_node *next; |
| 135 | } skip_node_t; |
| 136 | |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 137 | typedef struct active_list { |
| 138 | const char *identifier; |
| 139 | token_node_t *marker; |
| 140 | struct active_list *next; |
| 141 | } active_list_t; |
| 142 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 143 | struct glcpp_parser { |
| 144 | yyscan_t scanner; |
| 145 | struct hash_table *defines; |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 146 | active_list_t *active; |
Carl Worth | a771a40 | 2010-06-01 11:20:18 -0700 | [diff] [blame] | 147 | int lexing_if; |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 148 | int space_tokens; |
Carl Worth | 95951ea | 2010-05-26 15:57:10 -0700 | [diff] [blame] | 149 | int newline_as_space; |
| 150 | int in_control_line; |
| 151 | int paren_count; |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 152 | skip_node_t *skip_stack; |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 153 | token_list_t *lex_from_list; |
| 154 | token_node_t *lex_from_node; |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 155 | char *output; |
| 156 | char *errors; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 159 | glcpp_parser_t * |
| 160 | glcpp_parser_create (void); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 161 | |
| 162 | int |
| 163 | glcpp_parser_parse (glcpp_parser_t *parser); |
| 164 | |
| 165 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 166 | glcpp_parser_destroy (glcpp_parser_t *parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 167 | |
Kenneth Graunke | f1e6c06 | 2010-06-17 12:03:25 -0700 | [diff] [blame^] | 168 | void |
| 169 | glcpp_error (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...); |
| 170 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 171 | /* Generated by glcpp-lex.l to glcpp-lex.c */ |
| 172 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 173 | int |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 174 | glcpp_lex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner); |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 175 | |
Kenneth Graunke | 1b1f43e | 2010-06-16 12:01:17 -0700 | [diff] [blame] | 176 | void |
| 177 | glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader); |
| 178 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 179 | int |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 180 | glcpp_lex (YYSTYPE *lvalp, YYLTYPE *llocp, yyscan_t scanner); |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 181 | |
| 182 | int |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 183 | glcpp_lex_destroy (yyscan_t scanner); |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 184 | |
| 185 | /* Generated by glcpp-parse.y to glcpp-parse.c */ |
| 186 | |
| 187 | int |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 188 | yyparse (glcpp_parser_t *parser); |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 189 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 190 | /* xtalloc - wrappers around talloc to check for out-of-memory */ |
| 191 | |
| 192 | #define xtalloc(ctx, type) (type *)xtalloc_named_const(ctx, sizeof(type), #type) |
| 193 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 194 | #define xtalloc_size(ctx, size) xtalloc_named_const(ctx, size, __location__) |
| 195 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 196 | void * |
| 197 | xtalloc_named_const (const void *context, size_t size, const char *name); |
| 198 | |
| 199 | char * |
| 200 | xtalloc_strdup (const void *t, const char *p); |
| 201 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 202 | char * |
| 203 | xtalloc_strndup (const void *t, const char *p, size_t n); |
| 204 | |
Carl Worth | b894583 | 2010-05-20 15:02:03 -0700 | [diff] [blame] | 205 | char * |
| 206 | xtalloc_asprintf (const void *t, const char *fmt, ...); |
| 207 | |
Carl Worth | 9bb796f | 2010-05-25 14:40:47 -0700 | [diff] [blame] | 208 | void * |
| 209 | _xtalloc_reference_loc (const void *context, |
| 210 | const void *ptr, const char *location); |
| 211 | |
| 212 | #define xtalloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_xtalloc_reference_loc((ctx),(ptr), __location__) |
| 213 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 214 | #endif |