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