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