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 | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 28 | #include <talloc.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 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 32 | #define YYLEX_PARAM parser->scanner |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 33 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 34 | typedef struct { |
| 35 | int is_function; |
| 36 | list_t *parameter_list; |
| 37 | list_t *replacement_list; |
| 38 | } macro_t; |
| 39 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 40 | struct glcpp_parser { |
| 41 | yyscan_t scanner; |
| 42 | struct hash_table *defines; |
| 43 | }; |
| 44 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 45 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 46 | yyerror (void *scanner, const char *error); |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 47 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 48 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 49 | _define_object_macro (glcpp_parser_t *parser, |
| 50 | const char *macro, |
| 51 | list_t *replacement_list); |
| 52 | |
| 53 | void |
| 54 | _define_function_macro (glcpp_parser_t *parser, |
| 55 | const char *macro, |
| 56 | list_t *parameter_list, |
| 57 | list_t *replacement_list); |
| 58 | |
| 59 | void |
| 60 | _print_expanded_object_macro (glcpp_parser_t *parser, const char *macro); |
| 61 | |
| 62 | void |
| 63 | _print_expanded_function_macro (glcpp_parser_t *parser, |
| 64 | const char *macro, |
| 65 | list_t *arguments); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 66 | |
| 67 | list_t * |
| 68 | _list_create (void *ctx); |
| 69 | |
| 70 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 71 | _list_append_item (list_t *list, const char *str); |
| 72 | |
| 73 | void |
| 74 | _list_append_list (list_t *list, list_t *tail); |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 75 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame^] | 76 | int |
| 77 | _list_contains (list_t *list, const char *member, int *index); |
| 78 | |
| 79 | const char * |
| 80 | _list_member_at (list_t *list, int index); |
| 81 | |
| 82 | int |
| 83 | _list_length (list_t *list); |
| 84 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 85 | %} |
| 86 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 87 | %union { |
| 88 | char *str; |
| 89 | list_t *list; |
| 90 | } |
| 91 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 92 | %parse-param {glcpp_parser_t *parser} |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 93 | %lex-param {void *scanner} |
| 94 | |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 95 | %token DEFINE FUNC_MACRO IDENTIFIER NEWLINE OBJ_MACRO SPACE TOKEN UNDEF |
| 96 | %type <str> FUNC_MACRO IDENTIFIER OBJ_MACRO TOKEN word word_or_symbol |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 97 | %type <list> argument argument_list parameter_list replacement_list |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 98 | |
| 99 | %% |
| 100 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 101 | input: |
| 102 | /* empty */ |
Carl Worth | 8bcb6f1 | 2010-05-12 13:21:20 -0700 | [diff] [blame] | 103 | | input content |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 104 | ; |
| 105 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 106 | content: |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 107 | IDENTIFIER { |
| 108 | printf ("%s", $1); |
| 109 | talloc_free ($1); |
| 110 | } |
| 111 | | TOKEN { |
| 112 | printf ("%s", $1); |
| 113 | talloc_free ($1); |
| 114 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 115 | | macro |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 116 | | directive_with_newline |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 117 | | NEWLINE { printf ("\n"); } |
| 118 | | '(' { printf ("("); } |
| 119 | | ')' { printf (")"); } |
| 120 | | ',' { printf (","); } |
Carl Worth | 48b94da | 2010-05-13 10:46:29 -0700 | [diff] [blame] | 121 | | SPACE { printf (" "); } |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 122 | ; |
| 123 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 124 | macro: |
| 125 | FUNC_MACRO '(' argument_list ')' { |
| 126 | _print_expanded_function_macro (parser, $1, $3); |
| 127 | } |
| 128 | | OBJ_MACRO { |
| 129 | _print_expanded_object_macro (parser, $1); |
| 130 | talloc_free ($1); |
| 131 | } |
| 132 | ; |
| 133 | |
| 134 | argument_list: |
Carl Worth | db35d55 | 2010-05-14 08:47:32 -0700 | [diff] [blame] | 135 | argument { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 136 | $$ = _list_create (parser); |
| 137 | _list_append_list ($$, $1); |
| 138 | } |
| 139 | | argument_list ',' argument { |
| 140 | _list_append_list ($1, $3); |
| 141 | $$ = $1; |
| 142 | } |
| 143 | ; |
| 144 | |
| 145 | argument: |
| 146 | /* empty */ { |
| 147 | $$ = _list_create (parser); |
| 148 | } |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 149 | | argument word { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 150 | _list_append_item ($1, $2); |
| 151 | talloc_free ($2); |
| 152 | } |
| 153 | | argument '(' argument ')' |
| 154 | ; |
| 155 | |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 156 | directive_with_newline: |
| 157 | directive NEWLINE { |
| 158 | printf ("\n"); |
| 159 | } |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 160 | ; |
| 161 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 162 | directive: |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 163 | DEFINE IDENTIFIER { |
| 164 | list_t *list = _list_create (parser); |
| 165 | _define_object_macro (parser, $2, list); |
| 166 | } |
| 167 | | DEFINE IDENTIFIER SPACE replacement_list { |
| 168 | _define_object_macro (parser, $2, $4); |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 169 | } |
Carl Worth | 48b94da | 2010-05-13 10:46:29 -0700 | [diff] [blame] | 170 | | DEFINE IDENTIFIER '(' parameter_list ')' { |
| 171 | list_t *list = _list_create (parser); |
| 172 | _define_function_macro (parser, $2, $4, list); |
| 173 | } |
| 174 | | DEFINE IDENTIFIER '(' parameter_list ')' SPACE replacement_list { |
| 175 | _define_function_macro (parser, $2, $4, $7); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 176 | } |
| 177 | | UNDEF FUNC_MACRO { |
| 178 | list_t *replacement = hash_table_find (parser->defines, $2); |
| 179 | if (replacement) { |
| 180 | /* XXX: Need hash table to support a real way |
| 181 | * to remove an element rather than prefixing |
| 182 | * a new node with data of NULL like this. */ |
| 183 | hash_table_insert (parser->defines, NULL, $2); |
| 184 | talloc_free (replacement); |
| 185 | } |
| 186 | talloc_free ($2); |
| 187 | } |
| 188 | | UNDEF OBJ_MACRO { |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 189 | list_t *replacement = hash_table_find (parser->defines, $2); |
| 190 | if (replacement) { |
| 191 | /* XXX: Need hash table to support a real way |
| 192 | * to remove an element rather than prefixing |
| 193 | * a new node with data of NULL like this. */ |
| 194 | hash_table_insert (parser->defines, NULL, $2); |
| 195 | talloc_free (replacement); |
| 196 | } |
| 197 | talloc_free ($2); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 198 | } |
| 199 | ; |
| 200 | |
| 201 | replacement_list: |
Carl Worth | 48b94da | 2010-05-13 10:46:29 -0700 | [diff] [blame] | 202 | word_or_symbol { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 203 | $$ = _list_create (parser); |
Carl Worth | 48b94da | 2010-05-13 10:46:29 -0700 | [diff] [blame] | 204 | _list_append_item ($$, $1); |
| 205 | talloc_free ($1); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 206 | } |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 207 | | replacement_list word_or_symbol { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 208 | _list_append_item ($1, $2); |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 209 | talloc_free ($2); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 210 | $$ = $1; |
| 211 | } |
| 212 | ; |
| 213 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 214 | parameter_list: |
| 215 | /* empty */ { |
| 216 | $$ = _list_create (parser); |
| 217 | } |
| 218 | | IDENTIFIER { |
| 219 | $$ = _list_create (parser); |
| 220 | _list_append_item ($$, $1); |
| 221 | talloc_free ($1); |
| 222 | } |
| 223 | | parameter_list ',' IDENTIFIER { |
| 224 | _list_append_item ($1, $3); |
| 225 | talloc_free ($3); |
| 226 | $$ = $1; |
| 227 | } |
| 228 | ; |
| 229 | |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 230 | word_or_symbol: |
| 231 | word { $$ = $1; } |
| 232 | | '(' { $$ = xtalloc_strdup (parser, "("); } |
| 233 | | ')' { $$ = xtalloc_strdup (parser, ")"); } |
| 234 | | ',' { $$ = xtalloc_strdup (parser, ","); } |
Carl Worth | 48b94da | 2010-05-13 10:46:29 -0700 | [diff] [blame] | 235 | | SPACE { $$ = xtalloc_strdup (parser, " "); } |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 236 | ; |
| 237 | |
| 238 | word: |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 239 | IDENTIFIER { $$ = $1; } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 240 | | FUNC_MACRO { $$ = $1; } |
| 241 | | OBJ_MACRO { $$ = $1; } |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 242 | | TOKEN { $$ = $1; } |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 243 | ; |
| 244 | |
| 245 | %% |
| 246 | |
| 247 | list_t * |
| 248 | _list_create (void *ctx) |
| 249 | { |
| 250 | list_t *list; |
| 251 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 252 | list = xtalloc (ctx, list_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 253 | list->head = NULL; |
| 254 | list->tail = NULL; |
| 255 | |
| 256 | return list; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 257 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 258 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 259 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 260 | _list_append_list (list_t *list, list_t *tail) |
| 261 | { |
| 262 | if (list->head == NULL) { |
| 263 | list->head = tail->head; |
| 264 | } else { |
| 265 | list->tail->next = tail->head; |
| 266 | } |
| 267 | |
| 268 | list->tail = tail->tail; |
| 269 | } |
| 270 | |
| 271 | void |
| 272 | _list_append_item (list_t *list, const char *str) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 273 | { |
| 274 | node_t *node; |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 275 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 276 | node = xtalloc (list, node_t); |
| 277 | node->str = xtalloc_strdup (node, str); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 278 | |
| 279 | node->next = NULL; |
| 280 | |
| 281 | if (list->head == NULL) { |
| 282 | list->head = node; |
| 283 | } else { |
| 284 | list->tail->next = node; |
| 285 | } |
| 286 | |
| 287 | list->tail = node; |
| 288 | } |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame^] | 289 | |
| 290 | int |
| 291 | _list_contains (list_t *list, const char *member, int *index) |
| 292 | { |
| 293 | node_t *node; |
| 294 | int i; |
| 295 | |
| 296 | if (list == NULL) |
| 297 | return 0; |
| 298 | |
| 299 | for (i = 0, node = list->head; node; i++, node = node->next) { |
| 300 | if (strcmp (node->str, member) == 0) { |
| 301 | *index = i; |
| 302 | return 1; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | int |
| 310 | _list_length (list_t *list) |
| 311 | { |
| 312 | int length = 0; |
| 313 | node_t *node; |
| 314 | |
| 315 | if (list == NULL) |
| 316 | return 0; |
| 317 | |
| 318 | for (node = list->head; node; node = node->next) |
| 319 | length++; |
| 320 | |
| 321 | return length; |
| 322 | } |
| 323 | |
| 324 | const char * |
| 325 | _list_member_at (list_t *list, int index) |
| 326 | { |
| 327 | node_t *node; |
| 328 | int i; |
| 329 | |
| 330 | if (list == NULL) |
| 331 | return NULL; |
| 332 | |
| 333 | node = list->head; |
| 334 | for (i = 0; i < index; i++) { |
| 335 | node = node->next; |
| 336 | if (node == NULL) |
| 337 | break; |
| 338 | } |
| 339 | |
| 340 | if (node) |
| 341 | return node->str; |
| 342 | |
| 343 | return NULL; |
| 344 | } |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 345 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 346 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 347 | yyerror (void *scanner, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 348 | { |
| 349 | fprintf (stderr, "Parse error: %s\n", error); |
| 350 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 351 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 352 | glcpp_parser_t * |
| 353 | glcpp_parser_create (void) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 354 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 355 | glcpp_parser_t *parser; |
| 356 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 357 | parser = xtalloc (NULL, glcpp_parser_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 358 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 359 | yylex_init_extra (parser, &parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 360 | parser->defines = hash_table_ctor (32, hash_table_string_hash, |
| 361 | hash_table_string_compare); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 362 | |
| 363 | return parser; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | int |
| 367 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 368 | { |
| 369 | return yyparse (parser); |
| 370 | } |
| 371 | |
| 372 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 373 | glcpp_parser_destroy (glcpp_parser_t *parser) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 374 | { |
| 375 | yylex_destroy (parser->scanner); |
| 376 | hash_table_dtor (parser->defines); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 377 | talloc_free (parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 378 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 379 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 380 | macro_type_t |
| 381 | glcpp_parser_macro_type (glcpp_parser_t *parser, const char *identifier) |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 382 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 383 | macro_t *macro; |
| 384 | |
| 385 | macro = hash_table_find (parser->defines, identifier); |
| 386 | |
| 387 | if (macro == NULL) |
| 388 | return MACRO_TYPE_UNDEFINED; |
| 389 | |
| 390 | if (macro->is_function) |
| 391 | return MACRO_TYPE_FUNCTION; |
| 392 | else |
| 393 | return MACRO_TYPE_OBJECT; |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 396 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 397 | _define_object_macro (glcpp_parser_t *parser, |
| 398 | const char *identifier, |
| 399 | list_t *replacement_list) |
| 400 | { |
| 401 | macro_t *macro; |
| 402 | |
| 403 | macro = xtalloc (parser, macro_t); |
| 404 | |
| 405 | macro->is_function = 0; |
| 406 | macro->parameter_list = NULL; |
| 407 | macro->replacement_list = talloc_steal (macro, replacement_list); |
| 408 | |
| 409 | hash_table_insert (parser->defines, macro, identifier); |
| 410 | } |
| 411 | |
| 412 | void |
| 413 | _define_function_macro (glcpp_parser_t *parser, |
| 414 | const char *identifier, |
| 415 | list_t *parameter_list, |
| 416 | list_t *replacement_list) |
| 417 | { |
| 418 | macro_t *macro; |
| 419 | |
| 420 | macro = xtalloc (parser, macro_t); |
| 421 | |
| 422 | macro->is_function = 1; |
| 423 | macro->parameter_list = talloc_steal (macro, parameter_list); |
| 424 | macro->replacement_list = talloc_steal (macro, replacement_list); |
| 425 | |
| 426 | hash_table_insert (parser->defines, macro, identifier); |
| 427 | } |
| 428 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame^] | 429 | static void |
| 430 | _print_expanded_macro_recursive (glcpp_parser_t *parser, |
| 431 | const char *token, |
| 432 | const char *orig, |
| 433 | list_t *parameters, |
| 434 | list_t *arguments); |
| 435 | |
| 436 | static void |
| 437 | _print_expanded_list_recursive (glcpp_parser_t *parser, |
| 438 | list_t *list, |
| 439 | const char *orig, |
| 440 | list_t *parameters, |
| 441 | list_t *arguments) |
| 442 | { |
| 443 | const char *token; |
| 444 | node_t *node; |
| 445 | int index; |
| 446 | |
| 447 | for (node = list->head ; node ; node = node->next) { |
| 448 | token = node->str; |
| 449 | |
| 450 | if (strcmp (token, orig) == 0) { |
| 451 | printf ("%s", token); |
| 452 | continue; |
| 453 | } |
| 454 | |
| 455 | if (_list_contains (parameters, token, &index)) { |
| 456 | const char *argument; |
| 457 | |
| 458 | argument = _list_member_at (arguments, index); |
| 459 | _print_expanded_macro_recursive (parser, argument, |
| 460 | orig, parameters, |
| 461 | arguments); |
| 462 | } else { |
| 463 | _print_expanded_macro_recursive (parser, token, |
| 464 | orig, parameters, |
| 465 | arguments); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | |
| 471 | static void |
| 472 | _print_expanded_macro_recursive (glcpp_parser_t *parser, |
| 473 | const char *token, |
| 474 | const char *orig, |
| 475 | list_t *parameters, |
| 476 | list_t *arguments) |
| 477 | { |
| 478 | macro_t *macro; |
| 479 | list_t *replacement_list; |
| 480 | |
| 481 | macro = hash_table_find (parser->defines, token); |
| 482 | if (macro == NULL) { |
| 483 | printf ("%s", token); |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | replacement_list = macro->replacement_list; |
| 488 | |
| 489 | _print_expanded_list_recursive (parser, replacement_list, |
| 490 | orig, parameters, arguments); |
| 491 | } |
| 492 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 493 | void |
| 494 | _print_expanded_object_macro (glcpp_parser_t *parser, const char *identifier) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 495 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 496 | macro_t *macro; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 497 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 498 | macro = hash_table_find (parser->defines, identifier); |
| 499 | assert (! macro->is_function); |
| 500 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame^] | 501 | _print_expanded_macro_recursive (parser, identifier, identifier, |
| 502 | NULL, NULL); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | void |
| 506 | _print_expanded_function_macro (glcpp_parser_t *parser, |
| 507 | const char *identifier, |
| 508 | list_t *arguments) |
| 509 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 510 | macro_t *macro; |
| 511 | |
| 512 | macro = hash_table_find (parser->defines, identifier); |
| 513 | assert (macro->is_function); |
| 514 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame^] | 515 | if (_list_length (arguments) != _list_length (macro->parameter_list)) { |
| 516 | fprintf (stderr, |
| 517 | "Error: macro %s invoked with %d arguments (expected %d)\n", |
| 518 | identifier, |
| 519 | _list_length (arguments), |
| 520 | _list_length (macro->parameter_list)); |
| 521 | return; |
| 522 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 523 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame^] | 524 | _print_expanded_macro_recursive (parser, identifier, identifier, |
| 525 | macro->parameter_list, arguments); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 526 | } |