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 | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 28 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 29 | #include "glcpp.h" |
| 30 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 31 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 32 | yyerror (void *scanner, const char *error); |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 33 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 34 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 35 | _define_object_macro (glcpp_parser_t *parser, |
| 36 | const char *macro, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 37 | token_list_t *replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 38 | |
| 39 | void |
| 40 | _define_function_macro (glcpp_parser_t *parser, |
| 41 | const char *macro, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 42 | string_list_t *parameters, |
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 | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 45 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 46 | _expand_object_macro (glcpp_parser_t *parser, const char *identifier); |
| 47 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 48 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 49 | _expand_function_macro (glcpp_parser_t *parser, |
| 50 | const char *identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 51 | argument_list_t *arguments); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 52 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 53 | string_list_t * |
| 54 | _string_list_create (void *ctx); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 55 | |
| 56 | 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 | |
| 59 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 60 | _string_list_append_list (string_list_t *list, string_list_t *tail); |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 61 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 62 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 63 | _string_list_contains (string_list_t *list, const char *member, int *index); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 64 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 65 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 66 | _string_list_length (string_list_t *list); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 67 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 68 | argument_list_t * |
| 69 | _argument_list_create (void *ctx); |
| 70 | |
| 71 | void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 72 | _argument_list_append (argument_list_t *list, token_list_t *argument); |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 73 | |
| 74 | int |
| 75 | _argument_list_length (argument_list_t *list); |
| 76 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 77 | token_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 78 | _argument_list_member_at (argument_list_t *list, int index); |
| 79 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 80 | token_list_t * |
| 81 | _token_list_create (void *ctx); |
| 82 | |
| 83 | void |
| 84 | _token_list_append (token_list_t *list, int type, const char *value); |
| 85 | |
| 86 | void |
| 87 | _token_list_append_list (token_list_t *list, token_list_t *tail); |
| 88 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 89 | static void |
| 90 | glcpp_parser_push_expansion_macro (glcpp_parser_t *parser, |
| 91 | macro_t *macro, |
| 92 | argument_list_t *arguments); |
| 93 | |
| 94 | static void |
| 95 | glcpp_parser_pop_expansion (glcpp_parser_t *parser); |
| 96 | |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 97 | #define yylex glcpp_parser_lex |
| 98 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 99 | static int |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 100 | glcpp_parser_lex (glcpp_parser_t *parser); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 101 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 102 | %} |
| 103 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 104 | %union { |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 105 | int ival; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 106 | char *str; |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 107 | argument_list_t *argument_list; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 108 | string_list_t *string_list; |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 109 | token_t token; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 110 | token_list_t *token_list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 113 | %parse-param {glcpp_parser_t *parser} |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 114 | %lex-param {glcpp_parser_t *parser} |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 115 | |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 116 | %token DEFINE FUNC_MACRO IDENTIFIER IDENTIFIER_FINALIZED OBJ_MACRO NEWLINE SEPARATOR SPACE TOKEN UNDEF |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame^] | 117 | %type <ival> punctuator |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 118 | %type <str> content FUNC_MACRO IDENTIFIER IDENTIFIER_FINALIZED OBJ_MACRO |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 119 | %type <argument_list> argument_list |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 120 | %type <string_list> macro parameter_list |
Carl Worth | 9f3d2c4 | 2010-05-20 08:42:02 -0700 | [diff] [blame] | 121 | %type <token> TOKEN argument_word argument_word_or_comma |
| 122 | %type <token_list> argument argument_or_comma replacement_list pp_tokens |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 123 | |
Carl Worth | 796e1f0 | 2010-05-17 12:45:16 -0700 | [diff] [blame] | 124 | /* Hard to remove shift/reduce conflicts documented as follows: |
| 125 | * |
| 126 | * 1. '(' after FUNC_MACRO name which is correctly resolved to shift |
| 127 | * to form macro invocation rather than reducing directly to |
| 128 | * content. |
Carl Worth | 69f390d | 2010-05-19 07:42:42 -0700 | [diff] [blame] | 129 | * |
| 130 | * 2. Similarly, '(' after FUNC_MACRO which is correctly resolved to |
| 131 | * shift to form macro invocation rather than reducing directly to |
| 132 | * argument. |
Carl Worth | 9f3d2c4 | 2010-05-20 08:42:02 -0700 | [diff] [blame] | 133 | * |
| 134 | * 3. Similarly again now that we added argument_or_comma as well. |
Carl Worth | 796e1f0 | 2010-05-17 12:45:16 -0700 | [diff] [blame] | 135 | */ |
Carl Worth | 9f3d2c4 | 2010-05-20 08:42:02 -0700 | [diff] [blame] | 136 | %expect 3 |
Carl Worth | 796e1f0 | 2010-05-17 12:45:16 -0700 | [diff] [blame] | 137 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 138 | %% |
| 139 | |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 140 | /* We do all printing at the input level. |
| 141 | * |
| 142 | * The value for "input" is simply TOKEN or SEPARATOR so we |
| 143 | * can decide whether it's necessary to print a space |
| 144 | * character between any two. */ |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 145 | input: |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 146 | /* empty */ { |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame^] | 147 | parser->just_printed_separator = 1; |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 148 | } |
| 149 | | input content { |
| 150 | int is_token; |
| 151 | |
| 152 | if ($2 && strlen ($2)) { |
| 153 | int c = $2[0]; |
| 154 | int is_not_separator = ((c >= 'a' && c <= 'z') || |
| 155 | (c >= 'A' && c <= 'Z') || |
| 156 | (c >= 'A' && c <= 'Z') || |
| 157 | (c >= '0' && c <= '9') || |
| 158 | (c == '_')); |
| 159 | |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame^] | 160 | if (! parser->just_printed_separator && is_not_separator) |
| 161 | { |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 162 | printf (" "); |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame^] | 163 | } |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 164 | printf ("%s", $2); |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame^] | 165 | |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 166 | if (is_not_separator) |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame^] | 167 | parser->just_printed_separator = 0; |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 168 | else |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame^] | 169 | parser->just_printed_separator = 1; |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 170 | } |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame^] | 171 | |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 172 | if ($2) |
| 173 | talloc_free ($2); |
| 174 | } |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 175 | ; |
| 176 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 177 | content: |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 178 | IDENTIFIER { |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 179 | $$ = $1; |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 180 | } |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 181 | | IDENTIFIER_FINALIZED { |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 182 | $$ = $1; |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 183 | } |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 184 | | TOKEN { |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 185 | $$ = $1.value; |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 186 | } |
Carl Worth | acf87bc | 2010-05-17 10:34:29 -0700 | [diff] [blame] | 187 | | FUNC_MACRO { |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 188 | $$ = $1; |
Carl Worth | acf87bc | 2010-05-17 10:34:29 -0700 | [diff] [blame] | 189 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 190 | | directive { |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 191 | $$ = talloc_strdup (parser, "\n"); |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 192 | } |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 193 | | punctuator { |
| 194 | $$ = talloc_asprintf (parser, "%c", $1); |
| 195 | } |
| 196 | | macro { |
| 197 | $$ = NULL; |
| 198 | } |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 199 | ; |
| 200 | |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 201 | punctuator: |
| 202 | '(' { $$ = '('; } |
| 203 | | ')' { $$ = ')'; } |
| 204 | | ',' { $$ = ','; } |
| 205 | ; |
| 206 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 207 | macro: |
| 208 | FUNC_MACRO '(' argument_list ')' { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 209 | _expand_function_macro (parser, $1, $3); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 210 | } |
| 211 | | OBJ_MACRO { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 212 | _expand_object_macro (parser, $1); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 213 | talloc_free ($1); |
| 214 | } |
| 215 | ; |
| 216 | |
| 217 | argument_list: |
Carl Worth | ac070e8 | 2010-05-14 11:33:00 -0700 | [diff] [blame] | 218 | /* empty */ { |
| 219 | $$ = _argument_list_create (parser); |
| 220 | } |
| 221 | | argument { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 222 | $$ = _argument_list_create (parser); |
| 223 | _argument_list_append ($$, $1); |
| 224 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 225 | | argument_list ',' argument { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 226 | _argument_list_append ($1, $3); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 227 | $$ = $1; |
| 228 | } |
| 229 | ; |
| 230 | |
| 231 | argument: |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 232 | argument_word { |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 233 | $$ = _token_list_create (parser); |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 234 | _token_list_append ($$, $1.type, $1.value); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 235 | } |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 236 | | argument argument_word { |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 237 | _token_list_append ($1, $2.type, $2.value); |
| 238 | talloc_free ($2.value); |
Carl Worth | 3596bb1 | 2010-05-14 16:53:52 -0700 | [diff] [blame] | 239 | $$ = $1; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 240 | } |
Carl Worth | 9f3d2c4 | 2010-05-20 08:42:02 -0700 | [diff] [blame] | 241 | | argument '(' argument_or_comma ')' { |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 242 | _token_list_append ($1, '(', "("); |
| 243 | _token_list_append_list ($1, $3); |
| 244 | _token_list_append ($1, ')', ")"); |
Carl Worth | 3596bb1 | 2010-05-14 16:53:52 -0700 | [diff] [blame] | 245 | $$ = $1; |
| 246 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 247 | ; |
| 248 | |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 249 | argument_word: |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 250 | IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; } |
| 251 | | IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; } |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 252 | | TOKEN { $$ = $1; } |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 253 | | FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; } |
| 254 | | macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); } |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 255 | ; |
| 256 | |
Carl Worth | 9f3d2c4 | 2010-05-20 08:42:02 -0700 | [diff] [blame] | 257 | /* XXX: The body of argument_or_comma is the same as the body |
| 258 | * of argument, but with "argument" and "argument_word" |
| 259 | * changed to "argument_or_comma" and |
| 260 | * "argument_word_or_comma". It would be nice to have less |
| 261 | * redundancy here, but I'm not sure how. |
| 262 | * |
| 263 | * It would also be nice to have a less ugly grammar to have |
| 264 | * to implement, but such is the C preprocessor. |
| 265 | */ |
| 266 | argument_or_comma: |
| 267 | argument_word_or_comma { |
| 268 | $$ = _token_list_create (parser); |
| 269 | _token_list_append ($$, $1.type, $1.value); |
| 270 | } |
| 271 | | argument_or_comma argument_word_or_comma { |
| 272 | _token_list_append ($1, $2.type, $2.value); |
| 273 | $$ = $1; |
| 274 | } |
| 275 | | argument_or_comma '(' argument_or_comma ')' { |
| 276 | _token_list_append ($1, '(', "("); |
| 277 | _token_list_append_list ($1, $3); |
| 278 | _token_list_append ($1, ')', ")"); |
| 279 | $$ = $1; |
| 280 | } |
| 281 | ; |
| 282 | |
| 283 | argument_word_or_comma: |
| 284 | IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; } |
| 285 | | IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; } |
| 286 | | TOKEN { $$ = $1; } |
| 287 | | FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; } |
| 288 | | macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); } |
| 289 | | ',' { $$.type = ','; $$.value = xtalloc_strdup (parser, ","); } |
| 290 | ; |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 291 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 292 | directive: |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 293 | DEFINE IDENTIFIER NEWLINE { |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 294 | token_list_t *list = _token_list_create (parser); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 295 | _define_object_macro (parser, $2, list); |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 296 | } |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 297 | | DEFINE IDENTIFIER SPACE replacement_list NEWLINE { |
| 298 | _define_object_macro (parser, $2, $4); |
| 299 | } |
| 300 | | DEFINE IDENTIFIER '(' parameter_list ')' replacement_list NEWLINE { |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 301 | _define_function_macro (parser, $2, $4, $6); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 302 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 303 | | UNDEF IDENTIFIER { |
| 304 | string_list_t *macro = hash_table_find (parser->defines, $2); |
| 305 | if (macro) { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 306 | /* XXX: Need hash table to support a real way |
| 307 | * to remove an element rather than prefixing |
| 308 | * a new node with data of NULL like this. */ |
| 309 | hash_table_insert (parser->defines, NULL, $2); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 310 | talloc_free (macro); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 311 | } |
| 312 | talloc_free ($2); |
| 313 | } |
Carl Worth | 38bd27b | 2010-05-14 12:05:37 -0700 | [diff] [blame] | 314 | ; |
| 315 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 316 | parameter_list: |
| 317 | /* empty */ { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 318 | $$ = _string_list_create (parser); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 319 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 320 | | IDENTIFIER { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 321 | $$ = _string_list_create (parser); |
| 322 | _string_list_append_item ($$, $1); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 323 | talloc_free ($1); |
| 324 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 325 | | parameter_list ',' IDENTIFIER { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 326 | _string_list_append_item ($1, $3); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 327 | talloc_free ($3); |
| 328 | $$ = $1; |
| 329 | } |
| 330 | ; |
| 331 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 332 | replacement_list: |
| 333 | /* empty */ { |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 334 | $$ = _token_list_create (parser); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 335 | } |
| 336 | | pp_tokens { |
| 337 | $$ = $1; |
| 338 | } |
| 339 | ; |
| 340 | |
| 341 | |
| 342 | pp_tokens: |
| 343 | TOKEN { |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 344 | $$ = _token_list_create (parser); |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 345 | _token_list_append ($$, $1.type, $1.value); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 346 | } |
| 347 | | pp_tokens TOKEN { |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 348 | _token_list_append ($1, $2.type, $2.value); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 349 | $$ = $1; |
| 350 | } |
| 351 | ; |
| 352 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 353 | %% |
| 354 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 355 | string_list_t * |
| 356 | _string_list_create (void *ctx) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 357 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 358 | string_list_t *list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 359 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 360 | list = xtalloc (ctx, string_list_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 361 | list->head = NULL; |
| 362 | list->tail = NULL; |
| 363 | |
| 364 | return list; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 365 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 366 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 367 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 368 | _string_list_append_list (string_list_t *list, string_list_t *tail) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 369 | { |
| 370 | if (list->head == NULL) { |
| 371 | list->head = tail->head; |
| 372 | } else { |
| 373 | list->tail->next = tail->head; |
| 374 | } |
| 375 | |
| 376 | list->tail = tail->tail; |
| 377 | } |
| 378 | |
| 379 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 380 | _string_list_append_item (string_list_t *list, const char *str) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 381 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 382 | string_node_t *node; |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 383 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 384 | node = xtalloc (list, string_node_t); |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 385 | node->str = xtalloc_strdup (node, str); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 386 | |
| 387 | node->next = NULL; |
| 388 | |
| 389 | if (list->head == NULL) { |
| 390 | list->head = node; |
| 391 | } else { |
| 392 | list->tail->next = node; |
| 393 | } |
| 394 | |
| 395 | list->tail = node; |
| 396 | } |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 397 | |
| 398 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 399 | _string_list_contains (string_list_t *list, const char *member, int *index) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 400 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 401 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 402 | int i; |
| 403 | |
| 404 | if (list == NULL) |
| 405 | return 0; |
| 406 | |
| 407 | for (i = 0, node = list->head; node; i++, node = node->next) { |
| 408 | if (strcmp (node->str, member) == 0) { |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 409 | if (index) |
| 410 | *index = i; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 411 | return 1; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 419 | _string_list_length (string_list_t *list) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 420 | { |
| 421 | int length = 0; |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 422 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 423 | |
| 424 | if (list == NULL) |
| 425 | return 0; |
| 426 | |
| 427 | for (node = list->head; node; node = node->next) |
| 428 | length++; |
| 429 | |
| 430 | return length; |
| 431 | } |
| 432 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 433 | argument_list_t * |
| 434 | _argument_list_create (void *ctx) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 435 | { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 436 | argument_list_t *list; |
| 437 | |
| 438 | list = xtalloc (ctx, argument_list_t); |
| 439 | list->head = NULL; |
| 440 | list->tail = NULL; |
| 441 | |
| 442 | return list; |
| 443 | } |
| 444 | |
| 445 | void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 446 | _argument_list_append (argument_list_t *list, token_list_t *argument) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 447 | { |
| 448 | argument_node_t *node; |
| 449 | |
| 450 | if (argument == NULL || argument->head == NULL) |
| 451 | return; |
| 452 | |
| 453 | node = xtalloc (list, argument_node_t); |
| 454 | node->argument = argument; |
| 455 | |
| 456 | node->next = NULL; |
| 457 | |
| 458 | if (list->head == NULL) { |
| 459 | list->head = node; |
| 460 | } else { |
| 461 | list->tail->next = node; |
| 462 | } |
| 463 | |
| 464 | list->tail = node; |
| 465 | } |
| 466 | |
| 467 | int |
| 468 | _argument_list_length (argument_list_t *list) |
| 469 | { |
| 470 | int length = 0; |
| 471 | argument_node_t *node; |
| 472 | |
| 473 | if (list == NULL) |
| 474 | return 0; |
| 475 | |
| 476 | for (node = list->head; node; node = node->next) |
| 477 | length++; |
| 478 | |
| 479 | return length; |
| 480 | } |
| 481 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 482 | token_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 483 | _argument_list_member_at (argument_list_t *list, int index) |
| 484 | { |
| 485 | argument_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 486 | int i; |
| 487 | |
| 488 | if (list == NULL) |
| 489 | return NULL; |
| 490 | |
| 491 | node = list->head; |
| 492 | for (i = 0; i < index; i++) { |
| 493 | node = node->next; |
| 494 | if (node == NULL) |
| 495 | break; |
| 496 | } |
| 497 | |
| 498 | if (node) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 499 | return node->argument; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 500 | |
| 501 | return NULL; |
| 502 | } |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 503 | |
| 504 | token_list_t * |
| 505 | _token_list_create (void *ctx) |
| 506 | { |
| 507 | token_list_t *list; |
| 508 | |
| 509 | list = xtalloc (ctx, token_list_t); |
| 510 | list->head = NULL; |
| 511 | list->tail = NULL; |
| 512 | |
| 513 | return list; |
| 514 | } |
| 515 | |
| 516 | void |
| 517 | _token_list_append (token_list_t *list, int type, const char *value) |
| 518 | { |
| 519 | token_node_t *node; |
| 520 | |
| 521 | node = xtalloc (list, token_node_t); |
| 522 | node->type = type; |
| 523 | node->value = xtalloc_strdup (list, value); |
| 524 | |
| 525 | node->next = NULL; |
| 526 | |
| 527 | if (list->head == NULL) { |
| 528 | list->head = node; |
| 529 | } else { |
| 530 | list->tail->next = node; |
| 531 | } |
| 532 | |
| 533 | list->tail = node; |
| 534 | } |
| 535 | |
| 536 | void |
| 537 | _token_list_append_list (token_list_t *list, token_list_t *tail) |
| 538 | { |
| 539 | if (list->head == NULL) { |
| 540 | list->head = tail->head; |
| 541 | } else { |
| 542 | list->tail->next = tail->head; |
| 543 | } |
| 544 | |
| 545 | list->tail = tail->tail; |
| 546 | } |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 547 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 548 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 549 | yyerror (void *scanner, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 550 | { |
| 551 | fprintf (stderr, "Parse error: %s\n", error); |
| 552 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 553 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 554 | glcpp_parser_t * |
| 555 | glcpp_parser_create (void) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 556 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 557 | glcpp_parser_t *parser; |
| 558 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 559 | parser = xtalloc (NULL, glcpp_parser_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 560 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 561 | glcpp_lex_init_extra (parser, &parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 562 | parser->defines = hash_table_ctor (32, hash_table_string_hash, |
| 563 | hash_table_string_compare); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 564 | parser->expansions = NULL; |
| 565 | |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame^] | 566 | parser->just_printed_separator = 1; |
| 567 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 568 | return parser; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | int |
| 572 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 573 | { |
| 574 | return yyparse (parser); |
| 575 | } |
| 576 | |
| 577 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 578 | glcpp_parser_destroy (glcpp_parser_t *parser) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 579 | { |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 580 | glcpp_lex_destroy (parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 581 | hash_table_dtor (parser->defines); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 582 | talloc_free (parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 583 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 584 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 585 | static int |
| 586 | glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member) |
| 587 | { |
| 588 | expansion_node_t *node; |
| 589 | |
| 590 | for (node = parser->expansions; node; node = node->next) { |
| 591 | if (node->macro && |
| 592 | strcmp (node->macro->identifier, member) == 0) |
| 593 | { |
| 594 | return 1; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 601 | token_class_t |
| 602 | glcpp_parser_classify_token (glcpp_parser_t *parser, |
| 603 | const char *identifier, |
| 604 | int *parameter_index) |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 605 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 606 | macro_t *macro; |
| 607 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 608 | /* First we check if we are currently expanding a |
| 609 | * function-like macro, and if so, whether the parameter list |
| 610 | * contains a parameter matching this token name. */ |
| 611 | if (parser->expansions && |
| 612 | parser->expansions->macro && |
| 613 | parser->expansions->macro->parameters) |
| 614 | { |
| 615 | string_list_t *list; |
| 616 | |
| 617 | list = parser->expansions->macro->parameters; |
| 618 | |
| 619 | if (_string_list_contains (list, identifier, parameter_index)) |
| 620 | return TOKEN_CLASS_ARGUMENT; |
| 621 | } |
| 622 | |
| 623 | /* If not a function-like macro parameter, we next check if |
| 624 | * this token is a macro itself. */ |
| 625 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 626 | macro = hash_table_find (parser->defines, identifier); |
| 627 | |
| 628 | if (macro == NULL) |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 629 | return TOKEN_CLASS_IDENTIFIER; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 630 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 631 | /* Don't consider this a macro if we are already actively |
| 632 | * expanding this macro. */ |
| 633 | if (glcpp_parser_is_expanding (parser, identifier)) |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 634 | return TOKEN_CLASS_IDENTIFIER_FINALIZED; |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 635 | |
| 636 | /* Definitely a macro. Just need to check if it's function-like. */ |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 637 | if (macro->is_function) |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 638 | return TOKEN_CLASS_FUNC_MACRO; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 639 | else |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 640 | return TOKEN_CLASS_OBJ_MACRO; |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 643 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 644 | _define_object_macro (glcpp_parser_t *parser, |
| 645 | const char *identifier, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 646 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 647 | { |
| 648 | macro_t *macro; |
| 649 | |
| 650 | macro = xtalloc (parser, macro_t); |
| 651 | |
| 652 | macro->is_function = 0; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 653 | macro->parameters = NULL; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 654 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 655 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 656 | |
| 657 | hash_table_insert (parser->defines, macro, identifier); |
| 658 | } |
| 659 | |
| 660 | void |
| 661 | _define_function_macro (glcpp_parser_t *parser, |
| 662 | const char *identifier, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 663 | string_list_t *parameters, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 664 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 665 | { |
| 666 | macro_t *macro; |
| 667 | |
| 668 | macro = xtalloc (parser, macro_t); |
| 669 | |
| 670 | macro->is_function = 1; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 671 | macro->parameters = talloc_steal (macro, parameters); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 672 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 673 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 674 | |
| 675 | hash_table_insert (parser->defines, macro, identifier); |
| 676 | } |
| 677 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 678 | static void |
| 679 | _glcpp_parser_push_expansion_internal (glcpp_parser_t *parser, |
| 680 | macro_t *macro, |
| 681 | argument_list_t *arguments, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 682 | token_node_t *replacements) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 683 | { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 684 | expansion_node_t *node; |
| 685 | |
| 686 | node = xtalloc (parser, expansion_node_t); |
| 687 | |
| 688 | node->macro = macro; |
| 689 | node->arguments = arguments; |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 690 | node->replacements = replacements; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 691 | |
| 692 | node->next = parser->expansions; |
| 693 | parser->expansions = node; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 696 | static void |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 697 | glcpp_parser_push_expansion_macro (glcpp_parser_t *parser, |
| 698 | macro_t *macro, |
| 699 | argument_list_t *arguments) |
| 700 | { |
| 701 | _glcpp_parser_push_expansion_internal (parser, macro, arguments, |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 702 | macro->replacements->head); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | void |
| 706 | glcpp_parser_push_expansion_argument (glcpp_parser_t *parser, |
| 707 | int argument_index) |
| 708 | { |
| 709 | argument_list_t *arguments; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 710 | token_list_t *argument; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 711 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 712 | arguments = parser->expansions->arguments; |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 713 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 714 | argument = _argument_list_member_at (arguments, argument_index); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 715 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 716 | _glcpp_parser_push_expansion_internal (parser, NULL, NULL, |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 717 | argument->head); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 720 | static void |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 721 | glcpp_parser_pop_expansion (glcpp_parser_t *parser) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 722 | { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 723 | expansion_node_t *node; |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 724 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 725 | node = parser->expansions; |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 726 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 727 | if (node == NULL) { |
| 728 | fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n"); |
| 729 | exit (1); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 732 | parser->expansions = node->next; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 733 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 734 | talloc_free (node); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 737 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 738 | _expand_object_macro (glcpp_parser_t *parser, const char *identifier) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 739 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 740 | macro_t *macro; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 741 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 742 | macro = hash_table_find (parser->defines, identifier); |
| 743 | assert (! macro->is_function); |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 744 | assert (! glcpp_parser_is_expanding (parser, identifier)); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 745 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 746 | glcpp_parser_push_expansion_macro (parser, macro, NULL); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 749 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 750 | _expand_function_macro (glcpp_parser_t *parser, |
| 751 | const char *identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 752 | argument_list_t *arguments) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 753 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 754 | macro_t *macro; |
| 755 | |
| 756 | macro = hash_table_find (parser->defines, identifier); |
| 757 | assert (macro->is_function); |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 758 | assert (! glcpp_parser_is_expanding (parser, identifier)); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 759 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 760 | if (_argument_list_length (arguments) != |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 761 | _string_list_length (macro->parameters)) |
| 762 | { |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 763 | fprintf (stderr, |
| 764 | "Error: macro %s invoked with %d arguments (expected %d)\n", |
| 765 | identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 766 | _argument_list_length (arguments), |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 767 | _string_list_length (macro->parameters)); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 768 | return; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 769 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 770 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 771 | glcpp_parser_push_expansion_macro (parser, macro, arguments); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 772 | } |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 773 | |
| 774 | static int |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 775 | glcpp_parser_lex (glcpp_parser_t *parser) |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 776 | { |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 777 | expansion_node_t *expansion; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 778 | token_node_t *replacements; |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 779 | int parameter_index; |
| 780 | |
| 781 | /* Who says C can't do efficient tail recursion? */ |
| 782 | RECURSE: |
| 783 | |
| 784 | expansion = parser->expansions; |
| 785 | |
| 786 | if (expansion == NULL) |
| 787 | return glcpp_lex (parser->scanner); |
| 788 | |
| 789 | replacements = expansion->replacements; |
| 790 | |
| 791 | /* Pop expansion when replacements is exhausted. */ |
| 792 | if (replacements == NULL) { |
| 793 | glcpp_parser_pop_expansion (parser); |
| 794 | goto RECURSE; |
| 795 | } |
| 796 | |
| 797 | expansion->replacements = replacements->next; |
| 798 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 799 | if (strcmp (replacements->value, "(") == 0) |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 800 | return '('; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 801 | else if (strcmp (replacements->value, ")") == 0) |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 802 | return ')'; |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 803 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 804 | yylval.str = xtalloc_strdup (parser, replacements->value); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 805 | |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 806 | /* Carefully refuse to expand any finalized identifier. */ |
| 807 | if (replacements->type == IDENTIFIER_FINALIZED) |
| 808 | return IDENTIFIER_FINALIZED; |
| 809 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 810 | switch (glcpp_parser_classify_token (parser, yylval.str, |
| 811 | ¶meter_index)) |
| 812 | { |
| 813 | case TOKEN_CLASS_ARGUMENT: |
| 814 | talloc_free (yylval.str); |
| 815 | glcpp_parser_push_expansion_argument (parser, |
| 816 | parameter_index); |
| 817 | goto RECURSE; |
| 818 | break; |
| 819 | case TOKEN_CLASS_IDENTIFIER: |
| 820 | return IDENTIFIER; |
| 821 | break; |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 822 | case TOKEN_CLASS_IDENTIFIER_FINALIZED: |
| 823 | return IDENTIFIER_FINALIZED; |
| 824 | break; |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 825 | case TOKEN_CLASS_FUNC_MACRO: |
| 826 | return FUNC_MACRO; |
| 827 | break; |
| 828 | default: |
| 829 | case TOKEN_CLASS_OBJ_MACRO: |
| 830 | return OBJ_MACRO; |
| 831 | break; |
| 832 | } |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 833 | } |