Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 1 | %{ |
| 2 | /* |
| 3 | * Copyright © 2010 Intel Corporation |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | * Software is furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice (including the next |
| 13 | * paragraph) shall be included in all copies or substantial portions of the |
| 14 | * Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | * DEALINGS IN THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 27 | #include <assert.h> |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 28 | #include <inttypes.h> |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 29 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 30 | #include "glcpp.h" |
Ian Romanick | 06143ea | 2010-06-30 16:27:22 -0700 | [diff] [blame] | 31 | #include "main/mtypes.h" |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 32 | |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 33 | #define glcpp_print(stream, str) stream = talloc_strdup_append(stream, str) |
| 34 | #define glcpp_printf(stream, fmt, args...) \ |
| 35 | stream = talloc_asprintf_append(stream, fmt, args) |
| 36 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 37 | static void |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 38 | yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error); |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 39 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 40 | static void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 41 | _define_object_macro (glcpp_parser_t *parser, |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 42 | YYLTYPE *loc, |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 43 | const char *macro, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 44 | token_list_t *replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 45 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 46 | static void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 47 | _define_function_macro (glcpp_parser_t *parser, |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 48 | YYLTYPE *loc, |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 49 | const char *macro, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 50 | string_list_t *parameters, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 51 | token_list_t *replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 52 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 53 | static string_list_t * |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 54 | _string_list_create (void *ctx); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 55 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 56 | static void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 57 | _string_list_append_item (string_list_t *list, const char *str); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 58 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 59 | static int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 60 | _string_list_contains (string_list_t *list, const char *member, int *index); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 61 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 62 | static int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 63 | _string_list_length (string_list_t *list); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 64 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 65 | static argument_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 66 | _argument_list_create (void *ctx); |
| 67 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 68 | static void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 69 | _argument_list_append (argument_list_t *list, token_list_t *argument); |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 70 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 71 | static int |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 72 | _argument_list_length (argument_list_t *list); |
| 73 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 74 | static token_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 75 | _argument_list_member_at (argument_list_t *list, int index); |
| 76 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 77 | /* Note: This function talloc_steal()s the str pointer. */ |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 78 | static token_t * |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 79 | _token_create_str (void *ctx, int type, char *str); |
| 80 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 81 | static token_t * |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 82 | _token_create_ival (void *ctx, int type, int ival); |
| 83 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 84 | static token_list_t * |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 85 | _token_list_create (void *ctx); |
| 86 | |
Carl Worth | b1ae61a | 2010-05-26 08:10:38 -0700 | [diff] [blame] | 87 | /* Note: This function adds a talloc_reference() to token. |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 88 | * |
| 89 | * You may want to talloc_unlink any current reference if you no |
| 90 | * longer need it. */ |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 91 | static void |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 92 | _token_list_append (token_list_t *list, token_t *token); |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 93 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 94 | static void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 95 | _token_list_append_list (token_list_t *list, token_list_t *tail); |
| 96 | |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 97 | static active_list_t * |
| 98 | _active_list_push (active_list_t *list, |
| 99 | const char *identifier, |
| 100 | token_node_t *marker); |
| 101 | |
| 102 | static active_list_t * |
| 103 | _active_list_pop (active_list_t *list); |
| 104 | |
| 105 | int |
| 106 | _active_list_contains (active_list_t *list, const char *identifier); |
| 107 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 108 | static void |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 109 | _glcpp_parser_expand_token_list (glcpp_parser_t *parser, |
| 110 | token_list_t *list); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 111 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 112 | static void |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 113 | _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser, |
| 114 | token_list_t *list); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 115 | |
| 116 | static void |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 117 | _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc, |
| 118 | int condition); |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 119 | |
| 120 | static void |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 121 | _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc, |
| 122 | const char *type, int condition); |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 123 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 124 | static void |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 125 | _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc); |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 126 | |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 127 | #define yylex glcpp_parser_lex |
| 128 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 129 | static int |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 130 | glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 131 | |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 132 | static void |
| 133 | glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list); |
| 134 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 135 | %} |
| 136 | |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 137 | %pure-parser |
Kenneth Graunke | f70f607 | 2010-06-17 13:07:13 -0700 | [diff] [blame] | 138 | %error-verbose |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 139 | %locations |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 140 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 141 | %parse-param {glcpp_parser_t *parser} |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 142 | %lex-param {glcpp_parser_t *parser} |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 143 | |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 144 | %token COMMA_FINAL DEFINED ELIF_EXPANDED HASH HASH_DEFINE_FUNC HASH_DEFINE_OBJ HASH_ELIF HASH_ELSE HASH_ENDIF HASH_IF HASH_IFDEF HASH_IFNDEF HASH_UNDEF IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING NEWLINE OTHER PLACEHOLDER SPACE |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 145 | %token PASTE |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 146 | %type <ival> expression INTEGER operator SPACE |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 147 | %type <str> IDENTIFIER INTEGER_STRING OTHER |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 148 | %type <string_list> identifier_list |
Kenneth Graunke | 26e761e | 2010-06-18 23:06:54 -0700 | [diff] [blame] | 149 | %type <token> preprocessing_token conditional_token |
| 150 | %type <token_list> pp_tokens replacement_list text_line conditional_tokens |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 151 | %left OR |
| 152 | %left AND |
| 153 | %left '|' |
| 154 | %left '^' |
| 155 | %left '&' |
| 156 | %left EQUAL NOT_EQUAL |
| 157 | %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL |
| 158 | %left LEFT_SHIFT RIGHT_SHIFT |
| 159 | %left '+' '-' |
| 160 | %left '*' '/' '%' |
| 161 | %right UNARY |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 162 | |
| 163 | %% |
| 164 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 165 | input: |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 166 | /* empty */ |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 167 | | input line |
| 168 | ; |
| 169 | |
| 170 | line: |
| 171 | control_line { |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 172 | glcpp_print(parser->output, "\n"); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 173 | } |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 174 | | text_line { |
Carl Worth | a771a40 | 2010-06-01 11:20:18 -0700 | [diff] [blame] | 175 | _glcpp_parser_print_expanded_token_list (parser, $1); |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 176 | glcpp_print(parser->output, "\n"); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 177 | talloc_free ($1); |
| 178 | } |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 179 | | expanded_line |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 180 | | HASH non_directive |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 181 | ; |
| 182 | |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 183 | expanded_line: |
| 184 | IF_EXPANDED expression NEWLINE { |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 185 | _glcpp_parser_skip_stack_push_if (parser, & @1, $2); |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 186 | } |
| 187 | | ELIF_EXPANDED expression NEWLINE { |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 188 | _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2); |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 189 | } |
| 190 | ; |
| 191 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 192 | control_line: |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 193 | HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE { |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 194 | _define_object_macro (parser, & @2, $2, $3); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 195 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 196 | | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE { |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 197 | _define_function_macro (parser, & @2, $2, NULL, $5); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 198 | } |
| 199 | | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE { |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 200 | _define_function_macro (parser, & @2, $2, $4, $6); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 201 | } |
Carl Worth | e6fb782 | 2010-05-25 15:28:58 -0700 | [diff] [blame] | 202 | | HASH_UNDEF IDENTIFIER NEWLINE { |
Carl Worth | 0324cad | 2010-05-26 15:53:05 -0700 | [diff] [blame] | 203 | macro_t *macro = hash_table_find (parser->defines, $2); |
Carl Worth | e6fb782 | 2010-05-25 15:28:58 -0700 | [diff] [blame] | 204 | if (macro) { |
Carl Worth | 61ebc01 | 2010-07-19 18:02:12 -0700 | [diff] [blame] | 205 | hash_table_remove (parser->defines, $2); |
Carl Worth | e6fb782 | 2010-05-25 15:28:58 -0700 | [diff] [blame] | 206 | talloc_free (macro); |
| 207 | } |
| 208 | talloc_free ($2); |
| 209 | } |
Kenneth Graunke | 26e761e | 2010-06-18 23:06:54 -0700 | [diff] [blame] | 210 | | HASH_IF conditional_tokens NEWLINE { |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 211 | token_list_t *expanded; |
| 212 | token_t *token; |
| 213 | |
| 214 | expanded = _token_list_create (parser); |
| 215 | token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED); |
| 216 | _token_list_append (expanded, token); |
| 217 | talloc_unlink (parser, token); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 218 | _glcpp_parser_expand_token_list (parser, $2); |
| 219 | _token_list_append_list (expanded, $2); |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 220 | glcpp_parser_lex_from (parser, expanded); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 221 | } |
Kenneth Graunke | 6587574 | 2010-06-18 20:08:15 -0700 | [diff] [blame] | 222 | | HASH_IFDEF IDENTIFIER junk NEWLINE { |
Carl Worth | 0324cad | 2010-05-26 15:53:05 -0700 | [diff] [blame] | 223 | macro_t *macro = hash_table_find (parser->defines, $2); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 224 | talloc_free ($2); |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 225 | _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 226 | } |
Kenneth Graunke | 6587574 | 2010-06-18 20:08:15 -0700 | [diff] [blame] | 227 | | HASH_IFNDEF IDENTIFIER junk NEWLINE { |
Carl Worth | 0324cad | 2010-05-26 15:53:05 -0700 | [diff] [blame] | 228 | macro_t *macro = hash_table_find (parser->defines, $2); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 229 | talloc_free ($2); |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 230 | _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 231 | } |
Kenneth Graunke | 26e761e | 2010-06-18 23:06:54 -0700 | [diff] [blame] | 232 | | HASH_ELIF conditional_tokens NEWLINE { |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 233 | token_list_t *expanded; |
| 234 | token_t *token; |
| 235 | |
| 236 | expanded = _token_list_create (parser); |
| 237 | token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED); |
| 238 | _token_list_append (expanded, token); |
| 239 | talloc_unlink (parser, token); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 240 | _glcpp_parser_expand_token_list (parser, $2); |
| 241 | _token_list_append_list (expanded, $2); |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 242 | glcpp_parser_lex_from (parser, expanded); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 243 | } |
Kenneth Graunke | 332fc47 | 2010-06-21 12:20:22 -0700 | [diff] [blame] | 244 | | HASH_ELIF NEWLINE { |
| 245 | /* #elif without an expression results in a warning if the |
| 246 | * condition doesn't matter (we just handled #if 1 or such) |
| 247 | * but an error otherwise. */ |
| 248 | if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP) { |
| 249 | parser->skip_stack->type = SKIP_TO_ENDIF; |
| 250 | glcpp_warning(& @1, parser, "ignoring illegal #elif without expression"); |
| 251 | } else { |
| 252 | glcpp_error(& @1, parser, "#elif needs an expression"); |
| 253 | } |
| 254 | } |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 255 | | HASH_ELSE NEWLINE { |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 256 | _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 257 | } |
| 258 | | HASH_ENDIF NEWLINE { |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 259 | _glcpp_parser_skip_stack_pop (parser, & @1); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 260 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 261 | | HASH NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 262 | ; |
| 263 | |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 264 | expression: |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 265 | INTEGER_STRING { |
| 266 | if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) { |
| 267 | $$ = strtoll ($1 + 2, NULL, 16); |
| 268 | } else if ($1[0] == '0') { |
| 269 | $$ = strtoll ($1, NULL, 8); |
| 270 | } else { |
| 271 | $$ = strtoll ($1, NULL, 10); |
| 272 | } |
| 273 | } |
| 274 | | INTEGER { |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 275 | $$ = $1; |
| 276 | } |
| 277 | | expression OR expression { |
| 278 | $$ = $1 || $3; |
| 279 | } |
| 280 | | expression AND expression { |
| 281 | $$ = $1 && $3; |
| 282 | } |
| 283 | | expression '|' expression { |
| 284 | $$ = $1 | $3; |
| 285 | } |
| 286 | | expression '^' expression { |
| 287 | $$ = $1 ^ $3; |
| 288 | } |
| 289 | | expression '&' expression { |
| 290 | $$ = $1 & $3; |
| 291 | } |
| 292 | | expression NOT_EQUAL expression { |
| 293 | $$ = $1 != $3; |
| 294 | } |
| 295 | | expression EQUAL expression { |
| 296 | $$ = $1 == $3; |
| 297 | } |
| 298 | | expression GREATER_OR_EQUAL expression { |
| 299 | $$ = $1 >= $3; |
| 300 | } |
| 301 | | expression LESS_OR_EQUAL expression { |
| 302 | $$ = $1 <= $3; |
| 303 | } |
| 304 | | expression '>' expression { |
| 305 | $$ = $1 > $3; |
| 306 | } |
| 307 | | expression '<' expression { |
| 308 | $$ = $1 < $3; |
| 309 | } |
| 310 | | expression RIGHT_SHIFT expression { |
| 311 | $$ = $1 >> $3; |
| 312 | } |
| 313 | | expression LEFT_SHIFT expression { |
| 314 | $$ = $1 << $3; |
| 315 | } |
| 316 | | expression '-' expression { |
| 317 | $$ = $1 - $3; |
| 318 | } |
| 319 | | expression '+' expression { |
| 320 | $$ = $1 + $3; |
| 321 | } |
| 322 | | expression '%' expression { |
| 323 | $$ = $1 % $3; |
| 324 | } |
| 325 | | expression '/' expression { |
| 326 | $$ = $1 / $3; |
| 327 | } |
| 328 | | expression '*' expression { |
| 329 | $$ = $1 * $3; |
| 330 | } |
| 331 | | '!' expression %prec UNARY { |
| 332 | $$ = ! $2; |
| 333 | } |
| 334 | | '~' expression %prec UNARY { |
| 335 | $$ = ~ $2; |
| 336 | } |
| 337 | | '-' expression %prec UNARY { |
| 338 | $$ = - $2; |
| 339 | } |
| 340 | | '+' expression %prec UNARY { |
| 341 | $$ = + $2; |
| 342 | } |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 343 | | '(' expression ')' { |
| 344 | $$ = $2; |
| 345 | } |
| 346 | ; |
| 347 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 348 | identifier_list: |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 349 | IDENTIFIER { |
| 350 | $$ = _string_list_create (parser); |
| 351 | _string_list_append_item ($$, $1); |
| 352 | talloc_steal ($$, $1); |
| 353 | } |
| 354 | | identifier_list ',' IDENTIFIER { |
| 355 | $$ = $1; |
| 356 | _string_list_append_item ($$, $3); |
| 357 | talloc_steal ($$, $3); |
| 358 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 359 | ; |
| 360 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 361 | text_line: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 362 | NEWLINE { $$ = NULL; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 363 | | pp_tokens NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 364 | ; |
| 365 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 366 | non_directive: |
Kenneth Graunke | 739ba06 | 2010-06-16 12:41:37 -0700 | [diff] [blame] | 367 | pp_tokens NEWLINE { |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 368 | yyerror (& @1, parser, "Invalid tokens after #"); |
Kenneth Graunke | 739ba06 | 2010-06-16 12:41:37 -0700 | [diff] [blame] | 369 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 370 | ; |
| 371 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 372 | replacement_list: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 373 | /* empty */ { $$ = NULL; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 374 | | pp_tokens |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 375 | ; |
| 376 | |
Kenneth Graunke | 6587574 | 2010-06-18 20:08:15 -0700 | [diff] [blame] | 377 | junk: |
| 378 | /* empty */ |
| 379 | | pp_tokens { |
| 380 | glcpp_warning(&@1, parser, "extra tokens at end of directive"); |
| 381 | } |
Kenneth Graunke | 26e761e | 2010-06-18 23:06:54 -0700 | [diff] [blame] | 382 | ; |
| 383 | |
| 384 | conditional_token: |
| 385 | /* Handle "defined" operator */ |
| 386 | DEFINED IDENTIFIER { |
| 387 | int v = hash_table_find (parser->defines, $2) ? 1 : 0; |
| 388 | $$ = _token_create_ival (parser, INTEGER, v); |
| 389 | } |
| 390 | | DEFINED '(' IDENTIFIER ')' { |
| 391 | int v = hash_table_find (parser->defines, $3) ? 1 : 0; |
| 392 | $$ = _token_create_ival (parser, INTEGER, v); |
| 393 | } |
| 394 | | preprocessing_token |
| 395 | ; |
| 396 | |
| 397 | conditional_tokens: |
| 398 | /* Exactly the same as pp_tokens, but using conditional_token */ |
| 399 | conditional_token { |
| 400 | parser->space_tokens = 1; |
| 401 | $$ = _token_list_create (parser); |
| 402 | _token_list_append ($$, $1); |
| 403 | talloc_unlink (parser, $1); |
| 404 | } |
| 405 | | conditional_tokens conditional_token { |
| 406 | $$ = $1; |
| 407 | _token_list_append ($$, $2); |
| 408 | talloc_unlink (parser, $2); |
| 409 | } |
| 410 | ; |
Kenneth Graunke | 6587574 | 2010-06-18 20:08:15 -0700 | [diff] [blame] | 411 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 412 | pp_tokens: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 413 | preprocessing_token { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 414 | parser->space_tokens = 1; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 415 | $$ = _token_list_create (parser); |
| 416 | _token_list_append ($$, $1); |
| 417 | talloc_unlink (parser, $1); |
| 418 | } |
| 419 | | pp_tokens preprocessing_token { |
| 420 | $$ = $1; |
| 421 | _token_list_append ($$, $2); |
| 422 | talloc_unlink (parser, $2); |
| 423 | } |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 424 | ; |
| 425 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 426 | preprocessing_token: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 427 | IDENTIFIER { |
| 428 | $$ = _token_create_str (parser, IDENTIFIER, $1); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 429 | $$->location = yylloc; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 430 | } |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 431 | | INTEGER_STRING { |
| 432 | $$ = _token_create_str (parser, INTEGER_STRING, $1); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 433 | $$->location = yylloc; |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 434 | } |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 435 | | operator { |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 436 | $$ = _token_create_ival (parser, $1, $1); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 437 | $$->location = yylloc; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 438 | } |
| 439 | | OTHER { |
| 440 | $$ = _token_create_str (parser, OTHER, $1); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 441 | $$->location = yylloc; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 442 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 443 | | SPACE { |
Carl Worth | e939786 | 2010-05-25 17:08:07 -0700 | [diff] [blame] | 444 | $$ = _token_create_ival (parser, SPACE, SPACE); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 445 | $$->location = yylloc; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 446 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 447 | ; |
| 448 | |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 449 | operator: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 450 | '[' { $$ = '['; } |
| 451 | | ']' { $$ = ']'; } |
| 452 | | '(' { $$ = '('; } |
| 453 | | ')' { $$ = ')'; } |
| 454 | | '{' { $$ = '{'; } |
| 455 | | '}' { $$ = '}'; } |
| 456 | | '.' { $$ = '.'; } |
| 457 | | '&' { $$ = '&'; } |
| 458 | | '*' { $$ = '*'; } |
| 459 | | '+' { $$ = '+'; } |
| 460 | | '-' { $$ = '-'; } |
| 461 | | '~' { $$ = '~'; } |
| 462 | | '!' { $$ = '!'; } |
| 463 | | '/' { $$ = '/'; } |
| 464 | | '%' { $$ = '%'; } |
| 465 | | LEFT_SHIFT { $$ = LEFT_SHIFT; } |
| 466 | | RIGHT_SHIFT { $$ = RIGHT_SHIFT; } |
| 467 | | '<' { $$ = '<'; } |
| 468 | | '>' { $$ = '>'; } |
| 469 | | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; } |
| 470 | | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; } |
| 471 | | EQUAL { $$ = EQUAL; } |
| 472 | | NOT_EQUAL { $$ = NOT_EQUAL; } |
| 473 | | '^' { $$ = '^'; } |
| 474 | | '|' { $$ = '|'; } |
| 475 | | AND { $$ = AND; } |
| 476 | | OR { $$ = OR; } |
| 477 | | ';' { $$ = ';'; } |
| 478 | | ',' { $$ = ','; } |
Carl Worth | 6310169 | 2010-05-29 05:07:24 -0700 | [diff] [blame] | 479 | | '=' { $$ = '='; } |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 480 | | PASTE { $$ = PASTE; } |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 481 | | DEFINED { $$ = DEFINED; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 482 | ; |
| 483 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 484 | %% |
| 485 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 486 | string_list_t * |
| 487 | _string_list_create (void *ctx) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 488 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 489 | string_list_t *list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 490 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 491 | list = xtalloc (ctx, string_list_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 492 | list->head = NULL; |
| 493 | list->tail = NULL; |
| 494 | |
| 495 | return list; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 496 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 497 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 498 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 499 | _string_list_append_item (string_list_t *list, const char *str) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 500 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 501 | string_node_t *node; |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 502 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 503 | node = xtalloc (list, string_node_t); |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 504 | node->str = xtalloc_strdup (node, str); |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 505 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 506 | node->next = NULL; |
| 507 | |
| 508 | if (list->head == NULL) { |
| 509 | list->head = node; |
| 510 | } else { |
| 511 | list->tail->next = node; |
| 512 | } |
| 513 | |
| 514 | list->tail = node; |
| 515 | } |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 516 | |
| 517 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 518 | _string_list_contains (string_list_t *list, const char *member, int *index) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 519 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 520 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 521 | int i; |
| 522 | |
| 523 | if (list == NULL) |
| 524 | return 0; |
| 525 | |
| 526 | for (i = 0, node = list->head; node; i++, node = node->next) { |
| 527 | if (strcmp (node->str, member) == 0) { |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 528 | if (index) |
| 529 | *index = i; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 530 | return 1; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 538 | _string_list_length (string_list_t *list) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 539 | { |
| 540 | int length = 0; |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 541 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 542 | |
| 543 | if (list == NULL) |
| 544 | return 0; |
| 545 | |
| 546 | for (node = list->head; node; node = node->next) |
| 547 | length++; |
| 548 | |
| 549 | return length; |
| 550 | } |
| 551 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 552 | argument_list_t * |
| 553 | _argument_list_create (void *ctx) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 554 | { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 555 | argument_list_t *list; |
| 556 | |
| 557 | list = xtalloc (ctx, argument_list_t); |
| 558 | list->head = NULL; |
| 559 | list->tail = NULL; |
| 560 | |
| 561 | return list; |
| 562 | } |
| 563 | |
| 564 | void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 565 | _argument_list_append (argument_list_t *list, token_list_t *argument) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 566 | { |
| 567 | argument_node_t *node; |
| 568 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 569 | node = xtalloc (list, argument_node_t); |
| 570 | node->argument = argument; |
| 571 | |
| 572 | node->next = NULL; |
| 573 | |
| 574 | if (list->head == NULL) { |
| 575 | list->head = node; |
| 576 | } else { |
| 577 | list->tail->next = node; |
| 578 | } |
| 579 | |
| 580 | list->tail = node; |
| 581 | } |
| 582 | |
| 583 | int |
| 584 | _argument_list_length (argument_list_t *list) |
| 585 | { |
| 586 | int length = 0; |
| 587 | argument_node_t *node; |
| 588 | |
| 589 | if (list == NULL) |
| 590 | return 0; |
| 591 | |
| 592 | for (node = list->head; node; node = node->next) |
| 593 | length++; |
| 594 | |
| 595 | return length; |
| 596 | } |
| 597 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 598 | token_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 599 | _argument_list_member_at (argument_list_t *list, int index) |
| 600 | { |
| 601 | argument_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 602 | int i; |
| 603 | |
| 604 | if (list == NULL) |
| 605 | return NULL; |
| 606 | |
| 607 | node = list->head; |
| 608 | for (i = 0; i < index; i++) { |
| 609 | node = node->next; |
| 610 | if (node == NULL) |
| 611 | break; |
| 612 | } |
| 613 | |
| 614 | if (node) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 615 | return node->argument; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 616 | |
| 617 | return NULL; |
| 618 | } |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 619 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 620 | /* Note: This function talloc_steal()s the str pointer. */ |
| 621 | token_t * |
| 622 | _token_create_str (void *ctx, int type, char *str) |
| 623 | { |
| 624 | token_t *token; |
| 625 | |
| 626 | token = xtalloc (ctx, token_t); |
| 627 | token->type = type; |
| 628 | token->value.str = talloc_steal (token, str); |
| 629 | |
| 630 | return token; |
| 631 | } |
| 632 | |
| 633 | token_t * |
| 634 | _token_create_ival (void *ctx, int type, int ival) |
| 635 | { |
| 636 | token_t *token; |
| 637 | |
| 638 | token = xtalloc (ctx, token_t); |
| 639 | token->type = type; |
| 640 | token->value.ival = ival; |
| 641 | |
| 642 | return token; |
| 643 | } |
| 644 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 645 | token_list_t * |
| 646 | _token_list_create (void *ctx) |
| 647 | { |
| 648 | token_list_t *list; |
| 649 | |
| 650 | list = xtalloc (ctx, token_list_t); |
| 651 | list->head = NULL; |
| 652 | list->tail = NULL; |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 653 | list->non_space_tail = NULL; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 654 | |
| 655 | return list; |
| 656 | } |
| 657 | |
| 658 | void |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 659 | _token_list_append (token_list_t *list, token_t *token) |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 660 | { |
| 661 | token_node_t *node; |
| 662 | |
| 663 | node = xtalloc (list, token_node_t); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 664 | node->token = xtalloc_reference (list, token); |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 665 | |
| 666 | node->next = NULL; |
| 667 | |
| 668 | if (list->head == NULL) { |
| 669 | list->head = node; |
| 670 | } else { |
| 671 | list->tail->next = node; |
| 672 | } |
| 673 | |
| 674 | list->tail = node; |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 675 | if (token->type != SPACE) |
| 676 | list->non_space_tail = node; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | void |
| 680 | _token_list_append_list (token_list_t *list, token_list_t *tail) |
| 681 | { |
Carl Worth | a65cf7b | 2010-05-27 11:55:36 -0700 | [diff] [blame] | 682 | if (tail == NULL || tail->head == NULL) |
| 683 | return; |
| 684 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 685 | if (list->head == NULL) { |
| 686 | list->head = tail->head; |
| 687 | } else { |
| 688 | list->tail->next = tail->head; |
| 689 | } |
| 690 | |
| 691 | list->tail = tail->tail; |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 692 | list->non_space_tail = tail->non_space_tail; |
| 693 | } |
| 694 | |
Carl Worth | d80dcaf | 2010-07-20 15:55:21 -0700 | [diff] [blame^] | 695 | static token_list_t * |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 696 | _token_list_copy (void *ctx, token_list_t *other) |
| 697 | { |
| 698 | token_list_t *copy; |
| 699 | token_node_t *node; |
| 700 | |
| 701 | if (other == NULL) |
| 702 | return NULL; |
| 703 | |
| 704 | copy = _token_list_create (ctx); |
| 705 | for (node = other->head; node; node = node->next) |
| 706 | _token_list_append (copy, node->token); |
| 707 | |
| 708 | return copy; |
| 709 | } |
| 710 | |
Carl Worth | d80dcaf | 2010-07-20 15:55:21 -0700 | [diff] [blame^] | 711 | static void |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 712 | _token_list_trim_trailing_space (token_list_t *list) |
| 713 | { |
| 714 | token_node_t *tail, *next; |
| 715 | |
| 716 | if (list->non_space_tail) { |
| 717 | tail = list->non_space_tail->next; |
| 718 | list->non_space_tail->next = NULL; |
| 719 | list->tail = list->non_space_tail; |
| 720 | |
| 721 | while (tail) { |
| 722 | next = tail->next; |
| 723 | talloc_free (tail); |
| 724 | tail = next; |
| 725 | } |
| 726 | } |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 727 | } |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 728 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 729 | static void |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 730 | _token_print (char **out, token_t *token) |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 731 | { |
| 732 | if (token->type < 256) { |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 733 | glcpp_printf (*out, "%c", token->type); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 734 | return; |
| 735 | } |
| 736 | |
| 737 | switch (token->type) { |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 738 | case INTEGER: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 739 | glcpp_printf (*out, "%" PRIxMAX, token->value.ival); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 740 | break; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 741 | case IDENTIFIER: |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 742 | case INTEGER_STRING: |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 743 | case OTHER: |
Kenneth Graunke | ca9e5fc | 2010-06-16 17:31:50 -0700 | [diff] [blame] | 744 | glcpp_print (*out, token->value.str); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 745 | break; |
| 746 | case SPACE: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 747 | glcpp_print (*out, " "); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 748 | break; |
| 749 | case LEFT_SHIFT: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 750 | glcpp_print (*out, "<<"); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 751 | break; |
| 752 | case RIGHT_SHIFT: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 753 | glcpp_print (*out, ">>"); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 754 | break; |
| 755 | case LESS_OR_EQUAL: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 756 | glcpp_print (*out, "<="); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 757 | break; |
| 758 | case GREATER_OR_EQUAL: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 759 | glcpp_print (*out, ">="); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 760 | break; |
| 761 | case EQUAL: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 762 | glcpp_print (*out, "=="); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 763 | break; |
| 764 | case NOT_EQUAL: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 765 | glcpp_print (*out, "!="); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 766 | break; |
| 767 | case AND: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 768 | glcpp_print (*out, "&&"); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 769 | break; |
| 770 | case OR: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 771 | glcpp_print (*out, "||"); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 772 | break; |
| 773 | case PASTE: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 774 | glcpp_print (*out, "##"); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 775 | break; |
Carl Worth | dd74900 | 2010-05-27 10:12:33 -0700 | [diff] [blame] | 776 | case COMMA_FINAL: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 777 | glcpp_print (*out, ","); |
Carl Worth | dd74900 | 2010-05-27 10:12:33 -0700 | [diff] [blame] | 778 | break; |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 779 | case PLACEHOLDER: |
| 780 | /* Nothing to print. */ |
| 781 | break; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 782 | default: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 783 | assert(!"Error: Don't know how to print token."); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 784 | break; |
| 785 | } |
| 786 | } |
| 787 | |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 788 | /* Return a new token (talloc()ed off of 'token') formed by pasting |
| 789 | * 'token' and 'other'. Note that this function may return 'token' or |
| 790 | * 'other' directly rather than allocating anything new. |
| 791 | * |
| 792 | * Caution: Only very cursory error-checking is performed to see if |
| 793 | * the final result is a valid single token. */ |
| 794 | static token_t * |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 795 | _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other) |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 796 | { |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 797 | token_t *combined = NULL; |
| 798 | |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 799 | /* Pasting a placeholder onto anything makes no change. */ |
| 800 | if (other->type == PLACEHOLDER) |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 801 | return token; |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 802 | |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 803 | /* When 'token' is a placeholder, just return 'other'. */ |
| 804 | if (token->type == PLACEHOLDER) |
| 805 | return other; |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 806 | |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 807 | /* A very few single-character punctuators can be combined |
| 808 | * with another to form a multi-character punctuator. */ |
| 809 | switch (token->type) { |
| 810 | case '<': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 811 | if (other->type == '<') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 812 | combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT); |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 813 | else if (other->type == '=') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 814 | combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 815 | break; |
| 816 | case '>': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 817 | if (other->type == '>') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 818 | combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT); |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 819 | else if (other->type == '=') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 820 | combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 821 | break; |
| 822 | case '=': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 823 | if (other->type == '=') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 824 | combined = _token_create_ival (token, EQUAL, EQUAL); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 825 | break; |
| 826 | case '!': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 827 | if (other->type == '=') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 828 | combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 829 | break; |
| 830 | case '&': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 831 | if (other->type == '&') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 832 | combined = _token_create_ival (token, AND, AND); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 833 | break; |
| 834 | case '|': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 835 | if (other->type == '|') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 836 | combined = _token_create_ival (token, OR, OR); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 837 | break; |
| 838 | } |
| 839 | |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 840 | if (combined != NULL) { |
| 841 | /* Inherit the location from the first token */ |
| 842 | combined->location = token->location; |
| 843 | return combined; |
| 844 | } |
| 845 | |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 846 | /* Two string-valued tokens can usually just be mashed |
| 847 | * together. |
| 848 | * |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 849 | * XXX: This isn't actually legitimate. Several things here |
| 850 | * should result in a diagnostic since the result cannot be a |
| 851 | * valid, single pre-processing token. For example, pasting |
| 852 | * "123" and "abc" is not legal, but we don't catch that |
| 853 | * here. */ |
| 854 | if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) && |
| 855 | (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING)) |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 856 | { |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 857 | char *str; |
| 858 | |
| 859 | str = xtalloc_asprintf (token, "%s%s", |
| 860 | token->value.str, other->value.str); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 861 | combined = _token_create_str (token, token->type, str); |
| 862 | combined->location = token->location; |
| 863 | return combined; |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Kenneth Graunke | ca9e5fc | 2010-06-16 17:31:50 -0700 | [diff] [blame] | 866 | glcpp_error (&token->location, parser, ""); |
Kenneth Graunke | 33eaa3e | 2010-06-18 19:52:36 -0700 | [diff] [blame] | 867 | glcpp_print (parser->info_log, "Pasting \""); |
| 868 | _token_print (&parser->info_log, token); |
| 869 | glcpp_print (parser->info_log, "\" and \""); |
| 870 | _token_print (&parser->info_log, other); |
| 871 | glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n"); |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 872 | |
| 873 | return token; |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 876 | static void |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 877 | _token_list_print (glcpp_parser_t *parser, token_list_t *list) |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 878 | { |
| 879 | token_node_t *node; |
| 880 | |
| 881 | if (list == NULL) |
| 882 | return; |
| 883 | |
| 884 | for (node = list->head; node; node = node->next) |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 885 | _token_print (&parser->output, node->token); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 886 | } |
| 887 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 888 | void |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 889 | yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 890 | { |
Kenneth Graunke | f1e6c06 | 2010-06-17 12:03:25 -0700 | [diff] [blame] | 891 | glcpp_error(locp, parser, "%s", error); |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 892 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 893 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 894 | glcpp_parser_t * |
Ian Romanick | 06143ea | 2010-06-30 16:27:22 -0700 | [diff] [blame] | 895 | glcpp_parser_create (const struct gl_extensions *extensions) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 896 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 897 | glcpp_parser_t *parser; |
Ian Romanick | 2d12236 | 2010-06-30 16:03:19 -0700 | [diff] [blame] | 898 | token_t *tok; |
| 899 | token_list_t *list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 900 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 901 | parser = xtalloc (NULL, glcpp_parser_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 902 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 903 | glcpp_lex_init_extra (parser, &parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 904 | parser->defines = hash_table_ctor (32, hash_table_string_hash, |
| 905 | hash_table_string_compare); |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 906 | parser->active = NULL; |
Carl Worth | a771a40 | 2010-06-01 11:20:18 -0700 | [diff] [blame] | 907 | parser->lexing_if = 0; |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 908 | parser->space_tokens = 1; |
Carl Worth | 95951ea | 2010-05-26 15:57:10 -0700 | [diff] [blame] | 909 | parser->newline_as_space = 0; |
| 910 | parser->in_control_line = 0; |
| 911 | parser->paren_count = 0; |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame] | 912 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 913 | parser->skip_stack = NULL; |
| 914 | |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 915 | parser->lex_from_list = NULL; |
| 916 | parser->lex_from_node = NULL; |
| 917 | |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 918 | parser->output = talloc_strdup(parser, ""); |
Kenneth Graunke | 33eaa3e | 2010-06-18 19:52:36 -0700 | [diff] [blame] | 919 | parser->info_log = talloc_strdup(parser, ""); |
Kenneth Graunke | 1b85c46 | 2010-06-21 13:55:12 -0700 | [diff] [blame] | 920 | parser->error = 0; |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 921 | |
Ian Romanick | 2d12236 | 2010-06-30 16:03:19 -0700 | [diff] [blame] | 922 | /* Add pre-defined macros. */ |
| 923 | tok = _token_create_ival (parser, INTEGER, 1); |
| 924 | |
| 925 | list = _token_list_create(parser); |
| 926 | _token_list_append(list, tok); |
| 927 | _define_object_macro(parser, NULL, "GL_ARB_draw_buffers", list); |
| 928 | |
| 929 | list = _token_list_create(parser); |
| 930 | _token_list_append(list, tok); |
| 931 | _define_object_macro(parser, NULL, "GL_ARB_texture_rectangle", list); |
| 932 | |
Ian Romanick | 06143ea | 2010-06-30 16:27:22 -0700 | [diff] [blame] | 933 | if ((extensions != NULL) && extensions->EXT_texture_array) { |
| 934 | list = _token_list_create(parser); |
| 935 | _token_list_append(list, tok); |
| 936 | _define_object_macro(parser, NULL, |
| 937 | "GL_EXT_texture_array", list); |
| 938 | } |
| 939 | |
Ian Romanick | 2d12236 | 2010-06-30 16:03:19 -0700 | [diff] [blame] | 940 | talloc_unlink(parser, tok); |
| 941 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 942 | return parser; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | int |
| 946 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 947 | { |
| 948 | return yyparse (parser); |
| 949 | } |
| 950 | |
| 951 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 952 | glcpp_parser_destroy (glcpp_parser_t *parser) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 953 | { |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 954 | if (parser->skip_stack) |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 955 | glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n"); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 956 | glcpp_lex_destroy (parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 957 | hash_table_dtor (parser->defines); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 958 | talloc_free (parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 959 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 960 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 961 | typedef enum function_status |
| 962 | { |
| 963 | FUNCTION_STATUS_SUCCESS, |
| 964 | FUNCTION_NOT_A_FUNCTION, |
| 965 | FUNCTION_UNBALANCED_PARENTHESES |
| 966 | } function_status_t; |
| 967 | |
| 968 | /* Find a set of function-like macro arguments by looking for a |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 969 | * balanced set of parentheses. |
| 970 | * |
| 971 | * When called, 'node' should be the opening-parenthesis token, (or |
| 972 | * perhaps preceeding SPACE tokens). Upon successful return *last will |
| 973 | * be the last consumed node, (corresponding to the closing right |
| 974 | * parenthesis). |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 975 | * |
| 976 | * Return values: |
| 977 | * |
| 978 | * FUNCTION_STATUS_SUCCESS: |
| 979 | * |
| 980 | * Successfully parsed a set of function arguments. |
| 981 | * |
| 982 | * FUNCTION_NOT_A_FUNCTION: |
| 983 | * |
| 984 | * Macro name not followed by a '('. This is not an error, but |
| 985 | * simply that the macro name should be treated as a non-macro. |
| 986 | * |
Carl Worth | 14c98a5 | 2010-06-02 15:49:54 -0700 | [diff] [blame] | 987 | * FUNCTION_UNBALANCED_PARENTHESES |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 988 | * |
| 989 | * Macro name is not followed by a balanced set of parentheses. |
| 990 | */ |
| 991 | static function_status_t |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 992 | _arguments_parse (argument_list_t *arguments, |
| 993 | token_node_t *node, |
| 994 | token_node_t **last) |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 995 | { |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 996 | token_list_t *argument; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 997 | int paren_count; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 998 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 999 | node = node->next; |
| 1000 | |
| 1001 | /* Ignore whitespace before first parenthesis. */ |
| 1002 | while (node && node->token->type == SPACE) |
| 1003 | node = node->next; |
| 1004 | |
| 1005 | if (node == NULL || node->token->type != '(') |
| 1006 | return FUNCTION_NOT_A_FUNCTION; |
| 1007 | |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame] | 1008 | node = node->next; |
| 1009 | |
Carl Worth | a19297b | 2010-05-27 13:29:19 -0700 | [diff] [blame] | 1010 | argument = _token_list_create (arguments); |
| 1011 | _argument_list_append (arguments, argument); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1012 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1013 | for (paren_count = 1; node; node = node->next) { |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1014 | if (node->token->type == '(') |
| 1015 | { |
| 1016 | paren_count++; |
| 1017 | } |
| 1018 | else if (node->token->type == ')') |
| 1019 | { |
| 1020 | paren_count--; |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1021 | if (paren_count == 0) |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 1022 | break; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1023 | } |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame] | 1024 | |
| 1025 | if (node->token->type == ',' && |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1026 | paren_count == 1) |
| 1027 | { |
Carl Worth | a19297b | 2010-05-27 13:29:19 -0700 | [diff] [blame] | 1028 | _token_list_trim_trailing_space (argument); |
| 1029 | argument = _token_list_create (arguments); |
| 1030 | _argument_list_append (arguments, argument); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1031 | } |
| 1032 | else { |
Carl Worth | a19297b | 2010-05-27 13:29:19 -0700 | [diff] [blame] | 1033 | if (argument->head == NULL) { |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 1034 | /* Don't treat initial whitespace as |
| 1035 | * part of the arguement. */ |
| 1036 | if (node->token->type == SPACE) |
| 1037 | continue; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1038 | } |
| 1039 | _token_list_append (argument, node->token); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1040 | } |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 1041 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1042 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1043 | if (paren_count) |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1044 | return FUNCTION_UNBALANCED_PARENTHESES; |
| 1045 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1046 | *last = node; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1047 | |
| 1048 | return FUNCTION_STATUS_SUCCESS; |
| 1049 | } |
| 1050 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1051 | /* This is a helper function that's essentially part of the |
| 1052 | * implementation of _glcpp_parser_expand_node. It shouldn't be called |
| 1053 | * except for by that function. |
| 1054 | * |
| 1055 | * Returns NULL if node is a simple token with no expansion, (that is, |
| 1056 | * although 'node' corresponds to an identifier defined as a |
| 1057 | * function-like macro, it is not followed with a parenthesized |
| 1058 | * argument list). |
| 1059 | * |
| 1060 | * Compute the complete expansion of node (which is a function-like |
| 1061 | * macro) and subsequent nodes which are arguments. |
| 1062 | * |
| 1063 | * Returns the token list that results from the expansion and sets |
| 1064 | * *last to the last node in the list that was consumed by the |
| 1065 | * expansion. Specificallty, *last will be set as follows: as the |
| 1066 | * token of the closing right parenthesis. |
| 1067 | */ |
| 1068 | static token_list_t * |
| 1069 | _glcpp_parser_expand_function (glcpp_parser_t *parser, |
| 1070 | token_node_t *node, |
| 1071 | token_node_t **last) |
| 1072 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1073 | { |
| 1074 | macro_t *macro; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1075 | const char *identifier; |
| 1076 | argument_list_t *arguments; |
| 1077 | function_status_t status; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1078 | token_list_t *substituted; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1079 | int parameter_index; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1080 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1081 | identifier = node->token->value.str; |
| 1082 | |
| 1083 | macro = hash_table_find (parser->defines, identifier); |
| 1084 | |
| 1085 | assert (macro->is_function); |
| 1086 | |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1087 | arguments = _argument_list_create (parser); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1088 | status = _arguments_parse (arguments, node, last); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1089 | |
| 1090 | switch (status) { |
| 1091 | case FUNCTION_STATUS_SUCCESS: |
| 1092 | break; |
| 1093 | case FUNCTION_NOT_A_FUNCTION: |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1094 | return NULL; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1095 | case FUNCTION_UNBALANCED_PARENTHESES: |
Kenneth Graunke | ca9e5fc | 2010-06-16 17:31:50 -0700 | [diff] [blame] | 1096 | glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1097 | return NULL; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1100 | if (macro->replacements == NULL) { |
| 1101 | talloc_free (arguments); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1102 | return _token_list_create (parser); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
Carl Worth | a19297b | 2010-05-27 13:29:19 -0700 | [diff] [blame] | 1105 | if (! ((_argument_list_length (arguments) == |
| 1106 | _string_list_length (macro->parameters)) || |
| 1107 | (_string_list_length (macro->parameters) == 0 && |
| 1108 | _argument_list_length (arguments) == 1 && |
| 1109 | arguments->head->argument->head == NULL))) |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1110 | { |
Kenneth Graunke | ca9e5fc | 2010-06-16 17:31:50 -0700 | [diff] [blame] | 1111 | glcpp_error (&node->token->location, parser, |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 1112 | "Error: macro %s invoked with %d arguments (expected %d)\n", |
| 1113 | identifier, |
| 1114 | _argument_list_length (arguments), |
| 1115 | _string_list_length (macro->parameters)); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1116 | return NULL; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1119 | /* Perform argument substitution on the replacement list. */ |
| 1120 | substituted = _token_list_create (arguments); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1121 | |
Carl Worth | ce540f2 | 2010-05-26 08:25:44 -0700 | [diff] [blame] | 1122 | for (node = macro->replacements->head; node; node = node->next) |
| 1123 | { |
| 1124 | if (node->token->type == IDENTIFIER && |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1125 | _string_list_contains (macro->parameters, |
Carl Worth | ce540f2 | 2010-05-26 08:25:44 -0700 | [diff] [blame] | 1126 | node->token->value.str, |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1127 | ¶meter_index)) |
| 1128 | { |
| 1129 | token_list_t *argument; |
| 1130 | argument = _argument_list_member_at (arguments, |
| 1131 | parameter_index); |
Carl Worth | d5cd403 | 2010-05-26 08:09:29 -0700 | [diff] [blame] | 1132 | /* Before substituting, we expand the argument |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 1133 | * tokens, or append a placeholder token for |
| 1134 | * an empty argument. */ |
| 1135 | if (argument->head) { |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1136 | _glcpp_parser_expand_token_list (parser, |
| 1137 | argument); |
| 1138 | _token_list_append_list (substituted, argument); |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 1139 | } else { |
| 1140 | token_t *new_token; |
| 1141 | |
| 1142 | new_token = _token_create_ival (substituted, |
| 1143 | PLACEHOLDER, |
| 1144 | PLACEHOLDER); |
| 1145 | _token_list_append (substituted, new_token); |
| 1146 | } |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1147 | } else { |
Carl Worth | ce540f2 | 2010-05-26 08:25:44 -0700 | [diff] [blame] | 1148 | _token_list_append (substituted, node->token); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1152 | /* After argument substitution, and before further expansion |
| 1153 | * below, implement token pasting. */ |
| 1154 | |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 1155 | _token_list_trim_trailing_space (substituted); |
| 1156 | |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1157 | node = substituted->head; |
| 1158 | while (node) |
| 1159 | { |
| 1160 | token_node_t *next_non_space; |
| 1161 | |
| 1162 | /* Look ahead for a PASTE token, skipping space. */ |
| 1163 | next_non_space = node->next; |
| 1164 | while (next_non_space && next_non_space->token->type == SPACE) |
| 1165 | next_non_space = next_non_space->next; |
| 1166 | |
| 1167 | if (next_non_space == NULL) |
| 1168 | break; |
| 1169 | |
| 1170 | if (next_non_space->token->type != PASTE) { |
| 1171 | node = next_non_space; |
| 1172 | continue; |
| 1173 | } |
| 1174 | |
| 1175 | /* Now find the next non-space token after the PASTE. */ |
| 1176 | next_non_space = next_non_space->next; |
| 1177 | while (next_non_space && next_non_space->token->type == SPACE) |
| 1178 | next_non_space = next_non_space->next; |
| 1179 | |
| 1180 | if (next_non_space == NULL) { |
Kenneth Graunke | ca9e5fc | 2010-06-16 17:31:50 -0700 | [diff] [blame] | 1181 | yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n"); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1182 | return NULL; |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 1185 | node->token = _token_paste (parser, node->token, next_non_space->token); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1186 | node->next = next_non_space->next; |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 1187 | if (next_non_space == substituted->tail) |
| 1188 | substituted->tail = node; |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1189 | |
| 1190 | node = node->next; |
| 1191 | } |
| 1192 | |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 1193 | substituted->non_space_tail = substituted->tail; |
| 1194 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1195 | return substituted; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1198 | /* Compute the complete expansion of node, (and subsequent nodes after |
| 1199 | * 'node' in the case that 'node' is a function-like macro and |
| 1200 | * subsequent nodes are arguments). |
| 1201 | * |
| 1202 | * Returns NULL if node is a simple token with no expansion. |
| 1203 | * |
| 1204 | * Otherwise, returns the token list that results from the expansion |
| 1205 | * and sets *last to the last node in the list that was consumed by |
| 1206 | * the expansion. Specificallty, *last will be set as follows: |
| 1207 | * |
| 1208 | * As 'node' in the case of object-like macro expansion. |
| 1209 | * |
| 1210 | * As the token of the closing right parenthesis in the case of |
| 1211 | * function-like macro expansion. |
| 1212 | */ |
| 1213 | static token_list_t * |
| 1214 | _glcpp_parser_expand_node (glcpp_parser_t *parser, |
| 1215 | token_node_t *node, |
| 1216 | token_node_t **last) |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1217 | { |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1218 | token_t *token = node->token; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1219 | const char *identifier; |
| 1220 | macro_t *macro; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1221 | |
| 1222 | /* We only expand identifiers */ |
| 1223 | if (token->type != IDENTIFIER) { |
| 1224 | /* We change any COMMA into a COMMA_FINAL to prevent |
| 1225 | * it being mistaken for an argument separator |
| 1226 | * later. */ |
| 1227 | if (token->type == ',') { |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1228 | token->type = COMMA_FINAL; |
| 1229 | token->value.ival = COMMA_FINAL; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1230 | } |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1231 | |
| 1232 | return NULL; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | /* Look up this identifier in the hash table. */ |
| 1236 | identifier = token->value.str; |
| 1237 | macro = hash_table_find (parser->defines, identifier); |
| 1238 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1239 | /* Not a macro, so no expansion needed. */ |
| 1240 | if (macro == NULL) |
| 1241 | return NULL; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1242 | |
| 1243 | /* Finally, don't expand this macro if we're already actively |
| 1244 | * expanding it, (to avoid infinite recursion). */ |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1245 | if (_active_list_contains (parser->active, identifier)) { |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1246 | /* We change the token type here from IDENTIFIER to |
| 1247 | * OTHER to prevent any future expansion of this |
| 1248 | * unexpanded token. */ |
| 1249 | char *str; |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1250 | token_list_t *expansion; |
| 1251 | token_t *final; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1252 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1253 | str = xtalloc_strdup (parser, token->value.str); |
| 1254 | final = _token_create_str (parser, OTHER, str); |
| 1255 | expansion = _token_list_create (parser); |
| 1256 | _token_list_append (expansion, final); |
| 1257 | *last = node; |
| 1258 | return expansion; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1261 | if (! macro->is_function) |
| 1262 | { |
| 1263 | *last = node; |
| 1264 | |
| 1265 | if (macro->replacements == NULL) |
| 1266 | return _token_list_create (parser); |
| 1267 | |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1268 | return _token_list_copy (parser, macro->replacements); |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1269 | } |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1270 | |
| 1271 | return _glcpp_parser_expand_function (parser, node, last); |
| 1272 | } |
| 1273 | |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1274 | /* Push a new identifier onto the active list, returning the new list. |
| 1275 | * |
| 1276 | * Here, 'marker' is the token node that appears in the list after the |
| 1277 | * expansion of 'identifier'. That is, when the list iterator begins |
| 1278 | * examinging 'marker', then it is time to pop this node from the |
| 1279 | * active stack. |
| 1280 | */ |
| 1281 | active_list_t * |
| 1282 | _active_list_push (active_list_t *list, |
| 1283 | const char *identifier, |
| 1284 | token_node_t *marker) |
| 1285 | { |
| 1286 | active_list_t *node; |
| 1287 | |
| 1288 | node = xtalloc (list, active_list_t); |
| 1289 | node->identifier = xtalloc_strdup (node, identifier); |
| 1290 | node->marker = marker; |
| 1291 | node->next = list; |
| 1292 | |
| 1293 | return node; |
| 1294 | } |
| 1295 | |
| 1296 | active_list_t * |
| 1297 | _active_list_pop (active_list_t *list) |
| 1298 | { |
| 1299 | active_list_t *node = list; |
| 1300 | |
| 1301 | if (node == NULL) |
| 1302 | return NULL; |
| 1303 | |
| 1304 | node = list->next; |
| 1305 | talloc_free (list); |
| 1306 | |
| 1307 | return node; |
| 1308 | } |
| 1309 | |
| 1310 | int |
| 1311 | _active_list_contains (active_list_t *list, const char *identifier) |
| 1312 | { |
| 1313 | active_list_t *node; |
| 1314 | |
| 1315 | if (list == NULL) |
| 1316 | return 0; |
| 1317 | |
| 1318 | for (node = list; node; node = node->next) |
| 1319 | if (strcmp (node->identifier, identifier) == 0) |
| 1320 | return 1; |
| 1321 | |
| 1322 | return 0; |
| 1323 | } |
| 1324 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1325 | /* Walk over the token list replacing nodes with their expansion. |
| 1326 | * Whenever nodes are expanded the walking will walk over the new |
| 1327 | * nodes, continuing to expand as necessary. The results are placed in |
| 1328 | * 'list' itself; |
| 1329 | */ |
| 1330 | static void |
| 1331 | _glcpp_parser_expand_token_list (glcpp_parser_t *parser, |
| 1332 | token_list_t *list) |
| 1333 | { |
| 1334 | token_node_t *node_prev; |
Carl Worth | c42e640 | 2010-06-22 15:51:34 -0700 | [diff] [blame] | 1335 | token_node_t *node, *last = NULL; |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1336 | token_list_t *expansion; |
| 1337 | |
| 1338 | if (list == NULL) |
| 1339 | return; |
| 1340 | |
| 1341 | _token_list_trim_trailing_space (list); |
| 1342 | |
| 1343 | node_prev = NULL; |
| 1344 | node = list->head; |
| 1345 | |
| 1346 | while (node) { |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1347 | |
| 1348 | while (parser->active && parser->active->marker == node) |
| 1349 | parser->active = _active_list_pop (parser->active); |
| 1350 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1351 | /* Find the expansion for node, which will replace all |
| 1352 | * nodes from node to last, inclusive. */ |
| 1353 | expansion = _glcpp_parser_expand_node (parser, node, &last); |
| 1354 | if (expansion) { |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1355 | token_node_t *n; |
| 1356 | |
| 1357 | for (n = node; n != last->next; n = n->next) |
| 1358 | while (parser->active && |
| 1359 | parser->active->marker == n) |
| 1360 | { |
| 1361 | parser->active = _active_list_pop (parser->active); |
| 1362 | } |
| 1363 | |
| 1364 | parser->active = _active_list_push (parser->active, |
| 1365 | node->token->value.str, |
| 1366 | last->next); |
| 1367 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1368 | /* Splice expansion into list, supporting a |
| 1369 | * simple deletion if the expansion is |
| 1370 | * empty. */ |
| 1371 | if (expansion->head) { |
| 1372 | if (node_prev) |
| 1373 | node_prev->next = expansion->head; |
| 1374 | else |
| 1375 | list->head = expansion->head; |
| 1376 | expansion->tail->next = last->next; |
| 1377 | if (last == list->tail) |
| 1378 | list->tail = expansion->tail; |
| 1379 | } else { |
| 1380 | if (node_prev) |
| 1381 | node_prev->next = last->next; |
| 1382 | else |
| 1383 | list->head = last->next; |
| 1384 | if (last == list->tail) |
Kenneth Graunke | 0656f6b | 2010-06-16 11:56:36 -0700 | [diff] [blame] | 1385 | list->tail = NULL; |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1386 | } |
| 1387 | } else { |
| 1388 | node_prev = node; |
| 1389 | } |
| 1390 | node = node_prev ? node_prev->next : list->head; |
| 1391 | } |
| 1392 | |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1393 | while (parser->active) |
| 1394 | parser->active = _active_list_pop (parser->active); |
| 1395 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1396 | list->non_space_tail = list->tail; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1399 | void |
| 1400 | _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser, |
| 1401 | token_list_t *list) |
| 1402 | { |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1403 | if (list == NULL) |
| 1404 | return; |
| 1405 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1406 | _glcpp_parser_expand_token_list (parser, list); |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 1407 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1408 | _token_list_trim_trailing_space (list); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1409 | |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 1410 | _token_list_print (parser, list); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
Carl Worth | d80dcaf | 2010-07-20 15:55:21 -0700 | [diff] [blame^] | 1413 | static void |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1414 | _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc, |
| 1415 | const char *identifier) |
Kenneth Graunke | 2ab0b13 | 2010-06-04 14:53:58 -0700 | [diff] [blame] | 1416 | { |
| 1417 | /* According to the GLSL specification, macro names starting with "__" |
| 1418 | * or "GL_" are reserved for future use. So, don't allow them. |
| 1419 | */ |
| 1420 | if (strncmp(identifier, "__", 2) == 0) { |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1421 | glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n"); |
Kenneth Graunke | 2ab0b13 | 2010-06-04 14:53:58 -0700 | [diff] [blame] | 1422 | } |
| 1423 | if (strncmp(identifier, "GL_", 3) == 0) { |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1424 | glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n"); |
Kenneth Graunke | 2ab0b13 | 2010-06-04 14:53:58 -0700 | [diff] [blame] | 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1429 | _define_object_macro (glcpp_parser_t *parser, |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1430 | YYLTYPE *loc, |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1431 | const char *identifier, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 1432 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1433 | { |
| 1434 | macro_t *macro; |
| 1435 | |
Ian Romanick | 2d12236 | 2010-06-30 16:03:19 -0700 | [diff] [blame] | 1436 | if (loc != NULL) |
| 1437 | _check_for_reserved_macro_name(parser, loc, identifier); |
Kenneth Graunke | 2ab0b13 | 2010-06-04 14:53:58 -0700 | [diff] [blame] | 1438 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1439 | macro = xtalloc (parser, macro_t); |
| 1440 | |
| 1441 | macro->is_function = 0; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 1442 | macro->parameters = NULL; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 1443 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1444 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1445 | |
| 1446 | hash_table_insert (parser->defines, macro, identifier); |
| 1447 | } |
| 1448 | |
| 1449 | void |
| 1450 | _define_function_macro (glcpp_parser_t *parser, |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1451 | YYLTYPE *loc, |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1452 | const char *identifier, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 1453 | string_list_t *parameters, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 1454 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1455 | { |
| 1456 | macro_t *macro; |
| 1457 | |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1458 | _check_for_reserved_macro_name(parser, loc, identifier); |
Kenneth Graunke | 2ab0b13 | 2010-06-04 14:53:58 -0700 | [diff] [blame] | 1459 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1460 | macro = xtalloc (parser, macro_t); |
| 1461 | |
| 1462 | macro->is_function = 1; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 1463 | macro->parameters = talloc_steal (macro, parameters); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 1464 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1465 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1466 | |
| 1467 | hash_table_insert (parser->defines, macro, identifier); |
| 1468 | } |
| 1469 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 1470 | static int |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 1471 | glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser) |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 1472 | { |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 1473 | token_node_t *node; |
| 1474 | int ret; |
| 1475 | |
Carl Worth | 95951ea | 2010-05-26 15:57:10 -0700 | [diff] [blame] | 1476 | if (parser->lex_from_list == NULL) { |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 1477 | ret = glcpp_lex (yylval, yylloc, parser->scanner); |
Carl Worth | 95951ea | 2010-05-26 15:57:10 -0700 | [diff] [blame] | 1478 | |
| 1479 | /* XXX: This ugly block of code exists for the sole |
| 1480 | * purpose of converting a NEWLINE token into a SPACE |
| 1481 | * token, but only in the case where we have seen a |
| 1482 | * function-like macro name, but have not yet seen its |
| 1483 | * closing parenthesis. |
| 1484 | * |
| 1485 | * There's perhaps a more compact way to do this with |
| 1486 | * mid-rule actions in the grammar. |
| 1487 | * |
| 1488 | * I'm definitely not pleased with the complexity of |
| 1489 | * this code here. |
| 1490 | */ |
| 1491 | if (parser->newline_as_space) |
| 1492 | { |
| 1493 | if (ret == '(') { |
| 1494 | parser->paren_count++; |
| 1495 | } else if (ret == ')') { |
| 1496 | parser->paren_count--; |
| 1497 | if (parser->paren_count == 0) |
| 1498 | parser->newline_as_space = 0; |
| 1499 | } else if (ret == NEWLINE) { |
| 1500 | ret = SPACE; |
| 1501 | } else if (ret != SPACE) { |
| 1502 | if (parser->paren_count == 0) |
| 1503 | parser->newline_as_space = 0; |
| 1504 | } |
| 1505 | } |
| 1506 | else if (parser->in_control_line) |
| 1507 | { |
| 1508 | if (ret == NEWLINE) |
| 1509 | parser->in_control_line = 0; |
| 1510 | } |
| 1511 | else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC || |
| 1512 | ret == HASH_UNDEF || ret == HASH_IF || |
| 1513 | ret == HASH_IFDEF || ret == HASH_IFNDEF || |
| 1514 | ret == HASH_ELIF || ret == HASH_ELSE || |
| 1515 | ret == HASH_ENDIF || ret == HASH) |
| 1516 | { |
| 1517 | parser->in_control_line = 1; |
| 1518 | } |
| 1519 | else if (ret == IDENTIFIER) |
| 1520 | { |
| 1521 | macro_t *macro; |
| 1522 | macro = hash_table_find (parser->defines, |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 1523 | yylval->str); |
Carl Worth | 95951ea | 2010-05-26 15:57:10 -0700 | [diff] [blame] | 1524 | if (macro && macro->is_function) { |
| 1525 | parser->newline_as_space = 1; |
| 1526 | parser->paren_count = 0; |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | return ret; |
| 1531 | } |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 1532 | |
| 1533 | node = parser->lex_from_node; |
| 1534 | |
| 1535 | if (node == NULL) { |
| 1536 | talloc_free (parser->lex_from_list); |
| 1537 | parser->lex_from_list = NULL; |
| 1538 | return NEWLINE; |
| 1539 | } |
| 1540 | |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 1541 | *yylval = node->token->value; |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 1542 | ret = node->token->type; |
| 1543 | |
| 1544 | parser->lex_from_node = node->next; |
| 1545 | |
| 1546 | return ret; |
| 1547 | } |
| 1548 | |
| 1549 | static void |
| 1550 | glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list) |
| 1551 | { |
| 1552 | token_node_t *node; |
| 1553 | |
| 1554 | assert (parser->lex_from_list == NULL); |
| 1555 | |
| 1556 | /* Copy list, eliminating any space tokens. */ |
| 1557 | parser->lex_from_list = _token_list_create (parser); |
| 1558 | |
| 1559 | for (node = list->head; node; node = node->next) { |
| 1560 | if (node->token->type == SPACE) |
| 1561 | continue; |
| 1562 | _token_list_append (parser->lex_from_list, node->token); |
| 1563 | } |
| 1564 | |
| 1565 | talloc_free (list); |
| 1566 | |
| 1567 | parser->lex_from_node = parser->lex_from_list->head; |
| 1568 | |
| 1569 | /* It's possible the list consisted of nothing but whitespace. */ |
| 1570 | if (parser->lex_from_node == NULL) { |
| 1571 | talloc_free (parser->lex_from_list); |
| 1572 | parser->lex_from_list = NULL; |
| 1573 | } |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 1574 | } |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1575 | |
| 1576 | static void |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 1577 | _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc, |
| 1578 | int condition) |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1579 | { |
| 1580 | skip_type_t current = SKIP_NO_SKIP; |
| 1581 | skip_node_t *node; |
| 1582 | |
| 1583 | if (parser->skip_stack) |
| 1584 | current = parser->skip_stack->type; |
| 1585 | |
| 1586 | node = xtalloc (parser, skip_node_t); |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 1587 | node->loc = *loc; |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1588 | |
| 1589 | if (current == SKIP_NO_SKIP) { |
| 1590 | if (condition) |
| 1591 | node->type = SKIP_NO_SKIP; |
| 1592 | else |
| 1593 | node->type = SKIP_TO_ELSE; |
| 1594 | } else { |
| 1595 | node->type = SKIP_TO_ENDIF; |
| 1596 | } |
| 1597 | |
| 1598 | node->next = parser->skip_stack; |
| 1599 | parser->skip_stack = node; |
| 1600 | } |
| 1601 | |
| 1602 | static void |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 1603 | _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc, |
| 1604 | const char *type, int condition) |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1605 | { |
| 1606 | if (parser->skip_stack == NULL) { |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 1607 | glcpp_error (loc, parser, "%s without #if\n", type); |
Kenneth Graunke | e8e93a4 | 2010-06-17 12:58:54 -0700 | [diff] [blame] | 1608 | return; |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | if (parser->skip_stack->type == SKIP_TO_ELSE) { |
| 1612 | if (condition) |
| 1613 | parser->skip_stack->type = SKIP_NO_SKIP; |
| 1614 | } else { |
| 1615 | parser->skip_stack->type = SKIP_TO_ENDIF; |
| 1616 | } |
| 1617 | } |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 1618 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1619 | static void |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 1620 | _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc) |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1621 | { |
| 1622 | skip_node_t *node; |
| 1623 | |
| 1624 | if (parser->skip_stack == NULL) { |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 1625 | glcpp_error (loc, parser, "#endif without #if\n"); |
Kenneth Graunke | e8e93a4 | 2010-06-17 12:58:54 -0700 | [diff] [blame] | 1626 | return; |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | node = parser->skip_stack; |
| 1630 | parser->skip_stack = node->next; |
| 1631 | talloc_free (node); |
| 1632 | } |