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