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 |
Carl Worth | 7f9aa36 | 2010-05-13 12:58:49 -0700 | [diff] [blame^] | 96 | %type <str> FUNC_MACRO IDENTIFIER identifier_perhaps_macro 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 | } |
Carl Worth | 7f9aa36 | 2010-05-13 12:58:49 -0700 | [diff] [blame^] | 218 | | identifier_perhaps_macro { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 219 | $$ = _list_create (parser); |
| 220 | _list_append_item ($$, $1); |
| 221 | talloc_free ($1); |
| 222 | } |
Carl Worth | 7f9aa36 | 2010-05-13 12:58:49 -0700 | [diff] [blame^] | 223 | | parameter_list ',' identifier_perhaps_macro { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 224 | _list_append_item ($1, $3); |
| 225 | talloc_free ($3); |
| 226 | $$ = $1; |
| 227 | } |
| 228 | ; |
| 229 | |
Carl Worth | 7f9aa36 | 2010-05-13 12:58:49 -0700 | [diff] [blame^] | 230 | identifier_perhaps_macro: |
| 231 | IDENTIFIER { $$ = $1; } |
| 232 | | FUNC_MACRO { $$ = $1; } |
| 233 | | OBJ_MACRO { $$ = $1; } |
| 234 | ; |
| 235 | |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 236 | word_or_symbol: |
| 237 | word { $$ = $1; } |
| 238 | | '(' { $$ = xtalloc_strdup (parser, "("); } |
| 239 | | ')' { $$ = xtalloc_strdup (parser, ")"); } |
| 240 | | ',' { $$ = xtalloc_strdup (parser, ","); } |
Carl Worth | 48b94da | 2010-05-13 10:46:29 -0700 | [diff] [blame] | 241 | | SPACE { $$ = xtalloc_strdup (parser, " "); } |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 242 | ; |
| 243 | |
| 244 | word: |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 245 | IDENTIFIER { $$ = $1; } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 246 | | FUNC_MACRO { $$ = $1; } |
| 247 | | OBJ_MACRO { $$ = $1; } |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 248 | | TOKEN { $$ = $1; } |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 249 | ; |
| 250 | |
| 251 | %% |
| 252 | |
| 253 | list_t * |
| 254 | _list_create (void *ctx) |
| 255 | { |
| 256 | list_t *list; |
| 257 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 258 | list = xtalloc (ctx, list_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 259 | list->head = NULL; |
| 260 | list->tail = NULL; |
| 261 | |
| 262 | return list; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 263 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 264 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 265 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 266 | _list_append_list (list_t *list, list_t *tail) |
| 267 | { |
| 268 | if (list->head == NULL) { |
| 269 | list->head = tail->head; |
| 270 | } else { |
| 271 | list->tail->next = tail->head; |
| 272 | } |
| 273 | |
| 274 | list->tail = tail->tail; |
| 275 | } |
| 276 | |
| 277 | void |
| 278 | _list_append_item (list_t *list, const char *str) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 279 | { |
| 280 | node_t *node; |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 281 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 282 | node = xtalloc (list, node_t); |
| 283 | node->str = xtalloc_strdup (node, str); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 284 | |
| 285 | node->next = NULL; |
| 286 | |
| 287 | if (list->head == NULL) { |
| 288 | list->head = node; |
| 289 | } else { |
| 290 | list->tail->next = node; |
| 291 | } |
| 292 | |
| 293 | list->tail = node; |
| 294 | } |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 295 | |
| 296 | int |
| 297 | _list_contains (list_t *list, const char *member, int *index) |
| 298 | { |
| 299 | node_t *node; |
| 300 | int i; |
| 301 | |
| 302 | if (list == NULL) |
| 303 | return 0; |
| 304 | |
| 305 | for (i = 0, node = list->head; node; i++, node = node->next) { |
| 306 | if (strcmp (node->str, member) == 0) { |
| 307 | *index = i; |
| 308 | return 1; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | int |
| 316 | _list_length (list_t *list) |
| 317 | { |
| 318 | int length = 0; |
| 319 | node_t *node; |
| 320 | |
| 321 | if (list == NULL) |
| 322 | return 0; |
| 323 | |
| 324 | for (node = list->head; node; node = node->next) |
| 325 | length++; |
| 326 | |
| 327 | return length; |
| 328 | } |
| 329 | |
| 330 | const char * |
| 331 | _list_member_at (list_t *list, int index) |
| 332 | { |
| 333 | node_t *node; |
| 334 | int i; |
| 335 | |
| 336 | if (list == NULL) |
| 337 | return NULL; |
| 338 | |
| 339 | node = list->head; |
| 340 | for (i = 0; i < index; i++) { |
| 341 | node = node->next; |
| 342 | if (node == NULL) |
| 343 | break; |
| 344 | } |
| 345 | |
| 346 | if (node) |
| 347 | return node->str; |
| 348 | |
| 349 | return NULL; |
| 350 | } |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 351 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 352 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 353 | yyerror (void *scanner, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 354 | { |
| 355 | fprintf (stderr, "Parse error: %s\n", error); |
| 356 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 357 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 358 | glcpp_parser_t * |
| 359 | glcpp_parser_create (void) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 360 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 361 | glcpp_parser_t *parser; |
| 362 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 363 | parser = xtalloc (NULL, glcpp_parser_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 364 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 365 | yylex_init_extra (parser, &parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 366 | parser->defines = hash_table_ctor (32, hash_table_string_hash, |
| 367 | hash_table_string_compare); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 368 | |
| 369 | return parser; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | int |
| 373 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 374 | { |
| 375 | return yyparse (parser); |
| 376 | } |
| 377 | |
| 378 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 379 | glcpp_parser_destroy (glcpp_parser_t *parser) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 380 | { |
| 381 | yylex_destroy (parser->scanner); |
| 382 | hash_table_dtor (parser->defines); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 383 | talloc_free (parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 384 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 385 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 386 | macro_type_t |
| 387 | glcpp_parser_macro_type (glcpp_parser_t *parser, const char *identifier) |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 388 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 389 | macro_t *macro; |
| 390 | |
| 391 | macro = hash_table_find (parser->defines, identifier); |
| 392 | |
| 393 | if (macro == NULL) |
| 394 | return MACRO_TYPE_UNDEFINED; |
| 395 | |
| 396 | if (macro->is_function) |
| 397 | return MACRO_TYPE_FUNCTION; |
| 398 | else |
| 399 | return MACRO_TYPE_OBJECT; |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 402 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 403 | _define_object_macro (glcpp_parser_t *parser, |
| 404 | const char *identifier, |
| 405 | list_t *replacement_list) |
| 406 | { |
| 407 | macro_t *macro; |
| 408 | |
| 409 | macro = xtalloc (parser, macro_t); |
| 410 | |
| 411 | macro->is_function = 0; |
| 412 | macro->parameter_list = NULL; |
| 413 | macro->replacement_list = talloc_steal (macro, replacement_list); |
| 414 | |
| 415 | hash_table_insert (parser->defines, macro, identifier); |
| 416 | } |
| 417 | |
| 418 | void |
| 419 | _define_function_macro (glcpp_parser_t *parser, |
| 420 | const char *identifier, |
| 421 | list_t *parameter_list, |
| 422 | list_t *replacement_list) |
| 423 | { |
| 424 | macro_t *macro; |
| 425 | |
| 426 | macro = xtalloc (parser, macro_t); |
| 427 | |
| 428 | macro->is_function = 1; |
| 429 | macro->parameter_list = talloc_steal (macro, parameter_list); |
| 430 | macro->replacement_list = talloc_steal (macro, replacement_list); |
| 431 | |
| 432 | hash_table_insert (parser->defines, macro, identifier); |
| 433 | } |
| 434 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 435 | static void |
| 436 | _print_expanded_macro_recursive (glcpp_parser_t *parser, |
| 437 | const char *token, |
| 438 | const char *orig, |
| 439 | list_t *parameters, |
| 440 | list_t *arguments); |
| 441 | |
| 442 | static void |
| 443 | _print_expanded_list_recursive (glcpp_parser_t *parser, |
| 444 | list_t *list, |
| 445 | const char *orig, |
| 446 | list_t *parameters, |
| 447 | list_t *arguments) |
| 448 | { |
| 449 | const char *token; |
| 450 | node_t *node; |
| 451 | int index; |
| 452 | |
| 453 | for (node = list->head ; node ; node = node->next) { |
| 454 | token = node->str; |
| 455 | |
| 456 | if (strcmp (token, orig) == 0) { |
| 457 | printf ("%s", token); |
| 458 | continue; |
| 459 | } |
| 460 | |
| 461 | if (_list_contains (parameters, token, &index)) { |
| 462 | const char *argument; |
| 463 | |
| 464 | argument = _list_member_at (arguments, index); |
| 465 | _print_expanded_macro_recursive (parser, argument, |
| 466 | orig, parameters, |
| 467 | arguments); |
| 468 | } else { |
| 469 | _print_expanded_macro_recursive (parser, token, |
| 470 | orig, parameters, |
| 471 | arguments); |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | |
| 477 | static void |
| 478 | _print_expanded_macro_recursive (glcpp_parser_t *parser, |
| 479 | const char *token, |
| 480 | const char *orig, |
| 481 | list_t *parameters, |
| 482 | list_t *arguments) |
| 483 | { |
| 484 | macro_t *macro; |
| 485 | list_t *replacement_list; |
| 486 | |
| 487 | macro = hash_table_find (parser->defines, token); |
| 488 | if (macro == NULL) { |
| 489 | printf ("%s", token); |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | replacement_list = macro->replacement_list; |
| 494 | |
| 495 | _print_expanded_list_recursive (parser, replacement_list, |
| 496 | orig, parameters, arguments); |
| 497 | } |
| 498 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 499 | void |
| 500 | _print_expanded_object_macro (glcpp_parser_t *parser, const char *identifier) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 501 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 502 | macro_t *macro; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 503 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 504 | macro = hash_table_find (parser->defines, identifier); |
| 505 | assert (! macro->is_function); |
| 506 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 507 | _print_expanded_macro_recursive (parser, identifier, identifier, |
| 508 | NULL, NULL); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | void |
| 512 | _print_expanded_function_macro (glcpp_parser_t *parser, |
| 513 | const char *identifier, |
| 514 | list_t *arguments) |
| 515 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 516 | macro_t *macro; |
| 517 | |
| 518 | macro = hash_table_find (parser->defines, identifier); |
| 519 | assert (macro->is_function); |
| 520 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 521 | if (_list_length (arguments) != _list_length (macro->parameter_list)) { |
| 522 | fprintf (stderr, |
| 523 | "Error: macro %s invoked with %d arguments (expected %d)\n", |
| 524 | identifier, |
| 525 | _list_length (arguments), |
| 526 | _list_length (macro->parameter_list)); |
| 527 | return; |
| 528 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 529 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 530 | _print_expanded_macro_recursive (parser, identifier, identifier, |
| 531 | macro->parameter_list, arguments); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 532 | } |