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 | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 76 | %} |
| 77 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 78 | %union { |
| 79 | char *str; |
| 80 | list_t *list; |
| 81 | } |
| 82 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 83 | %parse-param {glcpp_parser_t *parser} |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 84 | %lex-param {void *scanner} |
| 85 | |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 86 | %token DEFINE FUNC_MACRO IDENTIFIER NEWLINE OBJ_MACRO SPACE TOKEN UNDEF |
| 87 | %type <str> FUNC_MACRO IDENTIFIER OBJ_MACRO TOKEN word word_or_symbol |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 88 | %type <list> argument argument_list parameter_list replacement_list |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 89 | |
| 90 | %% |
| 91 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 92 | input: |
| 93 | /* empty */ |
Carl Worth | 8bcb6f1 | 2010-05-12 13:21:20 -0700 | [diff] [blame] | 94 | | input content |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 95 | ; |
| 96 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 97 | content: |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 98 | IDENTIFIER { |
| 99 | printf ("%s", $1); |
| 100 | talloc_free ($1); |
| 101 | } |
| 102 | | TOKEN { |
| 103 | printf ("%s", $1); |
| 104 | talloc_free ($1); |
| 105 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 106 | | macro |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 107 | | directive_with_newline |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 108 | | NEWLINE { printf ("\n"); } |
| 109 | | '(' { printf ("("); } |
| 110 | | ')' { printf (")"); } |
| 111 | | ',' { printf (","); } |
Carl Worth | 48b94da | 2010-05-13 10:46:29 -0700 | [diff] [blame] | 112 | | SPACE { printf (" "); } |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 113 | ; |
| 114 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 115 | macro: |
| 116 | FUNC_MACRO '(' argument_list ')' { |
| 117 | _print_expanded_function_macro (parser, $1, $3); |
| 118 | } |
| 119 | | OBJ_MACRO { |
| 120 | _print_expanded_object_macro (parser, $1); |
| 121 | talloc_free ($1); |
| 122 | } |
| 123 | ; |
| 124 | |
| 125 | argument_list: |
Carl Worth | db35d55 | 2010-05-14 08:47:32 -0700 | [diff] [blame] | 126 | argument { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 127 | $$ = _list_create (parser); |
| 128 | _list_append_list ($$, $1); |
| 129 | } |
| 130 | | argument_list ',' argument { |
| 131 | _list_append_list ($1, $3); |
| 132 | $$ = $1; |
| 133 | } |
| 134 | ; |
| 135 | |
| 136 | argument: |
| 137 | /* empty */ { |
| 138 | $$ = _list_create (parser); |
| 139 | } |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 140 | | argument word { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 141 | _list_append_item ($1, $2); |
| 142 | talloc_free ($2); |
| 143 | } |
| 144 | | argument '(' argument ')' |
| 145 | ; |
| 146 | |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 147 | directive_with_newline: |
| 148 | directive NEWLINE { |
| 149 | printf ("\n"); |
| 150 | } |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 151 | ; |
| 152 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 153 | directive: |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 154 | DEFINE IDENTIFIER { |
| 155 | list_t *list = _list_create (parser); |
| 156 | _define_object_macro (parser, $2, list); |
| 157 | } |
| 158 | | DEFINE IDENTIFIER SPACE replacement_list { |
| 159 | _define_object_macro (parser, $2, $4); |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 160 | } |
Carl Worth | 48b94da | 2010-05-13 10:46:29 -0700 | [diff] [blame] | 161 | | DEFINE IDENTIFIER '(' parameter_list ')' { |
| 162 | list_t *list = _list_create (parser); |
| 163 | _define_function_macro (parser, $2, $4, list); |
| 164 | } |
| 165 | | DEFINE IDENTIFIER '(' parameter_list ')' SPACE replacement_list { |
| 166 | _define_function_macro (parser, $2, $4, $7); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 167 | } |
| 168 | | UNDEF FUNC_MACRO { |
| 169 | list_t *replacement = hash_table_find (parser->defines, $2); |
| 170 | if (replacement) { |
| 171 | /* XXX: Need hash table to support a real way |
| 172 | * to remove an element rather than prefixing |
| 173 | * a new node with data of NULL like this. */ |
| 174 | hash_table_insert (parser->defines, NULL, $2); |
| 175 | talloc_free (replacement); |
| 176 | } |
| 177 | talloc_free ($2); |
| 178 | } |
| 179 | | UNDEF OBJ_MACRO { |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 180 | list_t *replacement = hash_table_find (parser->defines, $2); |
| 181 | if (replacement) { |
| 182 | /* XXX: Need hash table to support a real way |
| 183 | * to remove an element rather than prefixing |
| 184 | * a new node with data of NULL like this. */ |
| 185 | hash_table_insert (parser->defines, NULL, $2); |
| 186 | talloc_free (replacement); |
| 187 | } |
| 188 | talloc_free ($2); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 189 | } |
| 190 | ; |
| 191 | |
| 192 | replacement_list: |
Carl Worth | 48b94da | 2010-05-13 10:46:29 -0700 | [diff] [blame] | 193 | word_or_symbol { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 194 | $$ = _list_create (parser); |
Carl Worth | 48b94da | 2010-05-13 10:46:29 -0700 | [diff] [blame] | 195 | _list_append_item ($$, $1); |
| 196 | talloc_free ($1); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 197 | } |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 198 | | replacement_list word_or_symbol { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 199 | _list_append_item ($1, $2); |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 200 | talloc_free ($2); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 201 | $$ = $1; |
| 202 | } |
| 203 | ; |
| 204 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 205 | parameter_list: |
| 206 | /* empty */ { |
| 207 | $$ = _list_create (parser); |
| 208 | } |
| 209 | | IDENTIFIER { |
| 210 | $$ = _list_create (parser); |
| 211 | _list_append_item ($$, $1); |
| 212 | talloc_free ($1); |
| 213 | } |
| 214 | | parameter_list ',' IDENTIFIER { |
| 215 | _list_append_item ($1, $3); |
| 216 | talloc_free ($3); |
| 217 | $$ = $1; |
| 218 | } |
| 219 | ; |
| 220 | |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 221 | word_or_symbol: |
| 222 | word { $$ = $1; } |
| 223 | | '(' { $$ = xtalloc_strdup (parser, "("); } |
| 224 | | ')' { $$ = xtalloc_strdup (parser, ")"); } |
| 225 | | ',' { $$ = xtalloc_strdup (parser, ","); } |
Carl Worth | 48b94da | 2010-05-13 10:46:29 -0700 | [diff] [blame] | 226 | | SPACE { $$ = xtalloc_strdup (parser, " "); } |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 227 | ; |
| 228 | |
| 229 | word: |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 230 | IDENTIFIER { $$ = $1; } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 231 | | FUNC_MACRO { $$ = $1; } |
| 232 | | OBJ_MACRO { $$ = $1; } |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 233 | | TOKEN { $$ = $1; } |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 234 | ; |
| 235 | |
| 236 | %% |
| 237 | |
| 238 | list_t * |
| 239 | _list_create (void *ctx) |
| 240 | { |
| 241 | list_t *list; |
| 242 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 243 | list = xtalloc (ctx, list_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 244 | list->head = NULL; |
| 245 | list->tail = NULL; |
| 246 | |
| 247 | return list; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 248 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 249 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 250 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 251 | _list_append_list (list_t *list, list_t *tail) |
| 252 | { |
| 253 | if (list->head == NULL) { |
| 254 | list->head = tail->head; |
| 255 | } else { |
| 256 | list->tail->next = tail->head; |
| 257 | } |
| 258 | |
| 259 | list->tail = tail->tail; |
| 260 | } |
| 261 | |
| 262 | void |
| 263 | _list_append_item (list_t *list, const char *str) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 264 | { |
| 265 | node_t *node; |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 266 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 267 | node = xtalloc (list, node_t); |
| 268 | node->str = xtalloc_strdup (node, str); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 269 | |
| 270 | node->next = NULL; |
| 271 | |
| 272 | if (list->head == NULL) { |
| 273 | list->head = node; |
| 274 | } else { |
| 275 | list->tail->next = node; |
| 276 | } |
| 277 | |
| 278 | list->tail = node; |
| 279 | } |
| 280 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 281 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 282 | yyerror (void *scanner, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 283 | { |
| 284 | fprintf (stderr, "Parse error: %s\n", error); |
| 285 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 286 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 287 | glcpp_parser_t * |
| 288 | glcpp_parser_create (void) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 289 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 290 | glcpp_parser_t *parser; |
| 291 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 292 | parser = xtalloc (NULL, glcpp_parser_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 293 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 294 | yylex_init_extra (parser, &parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 295 | parser->defines = hash_table_ctor (32, hash_table_string_hash, |
| 296 | hash_table_string_compare); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 297 | |
| 298 | return parser; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | int |
| 302 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 303 | { |
| 304 | return yyparse (parser); |
| 305 | } |
| 306 | |
| 307 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 308 | glcpp_parser_destroy (glcpp_parser_t *parser) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 309 | { |
| 310 | yylex_destroy (parser->scanner); |
| 311 | hash_table_dtor (parser->defines); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 312 | talloc_free (parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 313 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 314 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 315 | macro_type_t |
| 316 | glcpp_parser_macro_type (glcpp_parser_t *parser, const char *identifier) |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 317 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 318 | macro_t *macro; |
| 319 | |
| 320 | macro = hash_table_find (parser->defines, identifier); |
| 321 | |
| 322 | if (macro == NULL) |
| 323 | return MACRO_TYPE_UNDEFINED; |
| 324 | |
| 325 | if (macro->is_function) |
| 326 | return MACRO_TYPE_FUNCTION; |
| 327 | else |
| 328 | return MACRO_TYPE_OBJECT; |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 331 | static void |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 332 | _print_expanded_macro_recursive (glcpp_parser_t *parser, |
| 333 | const char *token, |
Carl Worth | 48b94da | 2010-05-13 10:46:29 -0700 | [diff] [blame] | 334 | const char *orig) |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 335 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 336 | macro_t *macro; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 337 | node_t *node; |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 338 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 339 | macro = hash_table_find (parser->defines, token); |
| 340 | if (macro == NULL) { |
Carl Worth | 48b94da | 2010-05-13 10:46:29 -0700 | [diff] [blame] | 341 | printf ("%s", token); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 342 | } else { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 343 | list_t *replacement_list = macro->replacement_list; |
| 344 | |
| 345 | for (node = replacement_list->head ; node ; node = node->next) { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 346 | token = node->str; |
Carl Worth | 48b94da | 2010-05-13 10:46:29 -0700 | [diff] [blame] | 347 | if (strcmp (token, orig) == 0) |
| 348 | printf ("%s", token); |
| 349 | else |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 350 | _print_expanded_macro_recursive (parser, |
Carl Worth | 48b94da | 2010-05-13 10:46:29 -0700 | [diff] [blame] | 351 | token, orig); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 352 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 353 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 356 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 357 | _define_object_macro (glcpp_parser_t *parser, |
| 358 | const char *identifier, |
| 359 | list_t *replacement_list) |
| 360 | { |
| 361 | macro_t *macro; |
| 362 | |
| 363 | macro = xtalloc (parser, macro_t); |
| 364 | |
| 365 | macro->is_function = 0; |
| 366 | macro->parameter_list = NULL; |
| 367 | macro->replacement_list = talloc_steal (macro, replacement_list); |
| 368 | |
| 369 | hash_table_insert (parser->defines, macro, identifier); |
| 370 | } |
| 371 | |
| 372 | void |
| 373 | _define_function_macro (glcpp_parser_t *parser, |
| 374 | const char *identifier, |
| 375 | list_t *parameter_list, |
| 376 | list_t *replacement_list) |
| 377 | { |
| 378 | macro_t *macro; |
| 379 | |
| 380 | macro = xtalloc (parser, macro_t); |
| 381 | |
| 382 | macro->is_function = 1; |
| 383 | macro->parameter_list = talloc_steal (macro, parameter_list); |
| 384 | macro->replacement_list = talloc_steal (macro, replacement_list); |
| 385 | |
| 386 | hash_table_insert (parser->defines, macro, identifier); |
| 387 | } |
| 388 | |
| 389 | void |
| 390 | _print_expanded_object_macro (glcpp_parser_t *parser, const char *identifier) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 391 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 392 | macro_t *macro; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 393 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 394 | macro = hash_table_find (parser->defines, identifier); |
| 395 | assert (! macro->is_function); |
| 396 | |
Carl Worth | 48b94da | 2010-05-13 10:46:29 -0700 | [diff] [blame] | 397 | _print_expanded_macro_recursive (parser, identifier, identifier); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void |
| 401 | _print_expanded_function_macro (glcpp_parser_t *parser, |
| 402 | const char *identifier, |
| 403 | list_t *arguments) |
| 404 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 405 | macro_t *macro; |
| 406 | |
| 407 | macro = hash_table_find (parser->defines, identifier); |
| 408 | assert (macro->is_function); |
| 409 | |
| 410 | /* XXX: Need to use argument list here in the expansion. */ |
| 411 | |
Carl Worth | 48b94da | 2010-05-13 10:46:29 -0700 | [diff] [blame] | 412 | _print_expanded_macro_recursive (parser, identifier, identifier); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 413 | } |