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