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