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