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