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 | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 28 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 29 | #include "glcpp.h" |
| 30 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 31 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 32 | yyerror (void *scanner, const char *error); |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 33 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 34 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 35 | _define_object_macro (glcpp_parser_t *parser, |
| 36 | const char *macro, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 37 | token_list_t *replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 38 | |
| 39 | void |
| 40 | _define_function_macro (glcpp_parser_t *parser, |
| 41 | const char *macro, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 42 | string_list_t *parameters, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 43 | token_list_t *replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 44 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 45 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 46 | _expand_object_macro (glcpp_parser_t *parser, const char *identifier); |
| 47 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 48 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 49 | _expand_function_macro (glcpp_parser_t *parser, |
| 50 | const char *identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 51 | argument_list_t *arguments); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 52 | |
| 53 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 54 | _print_string_list (string_list_t *list); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 55 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 56 | string_list_t * |
| 57 | _string_list_create (void *ctx); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 58 | |
| 59 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 60 | _string_list_append_item (string_list_t *list, const char *str); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 61 | |
| 62 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 63 | _string_list_append_list (string_list_t *list, string_list_t *tail); |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 64 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 65 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 66 | _string_list_contains (string_list_t *list, const char *member, int *index); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 67 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 68 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 69 | _string_list_length (string_list_t *list); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 70 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 71 | argument_list_t * |
| 72 | _argument_list_create (void *ctx); |
| 73 | |
| 74 | void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 75 | _argument_list_append (argument_list_t *list, token_list_t *argument); |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 76 | |
| 77 | int |
| 78 | _argument_list_length (argument_list_t *list); |
| 79 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 80 | token_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 81 | _argument_list_member_at (argument_list_t *list, int index); |
| 82 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 83 | token_list_t * |
| 84 | _token_list_create (void *ctx); |
| 85 | |
| 86 | void |
| 87 | _token_list_append (token_list_t *list, int type, const char *value); |
| 88 | |
| 89 | void |
| 90 | _token_list_append_list (token_list_t *list, token_list_t *tail); |
| 91 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 92 | static void |
| 93 | glcpp_parser_push_expansion_macro (glcpp_parser_t *parser, |
| 94 | macro_t *macro, |
| 95 | argument_list_t *arguments); |
| 96 | |
| 97 | static void |
| 98 | glcpp_parser_pop_expansion (glcpp_parser_t *parser); |
| 99 | |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 100 | #define yylex glcpp_parser_lex |
| 101 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 102 | static int |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 103 | glcpp_parser_lex (glcpp_parser_t *parser); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 104 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 105 | %} |
| 106 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 107 | %union { |
| 108 | char *str; |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 109 | argument_list_t *argument_list; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 110 | string_list_t *string_list; |
| 111 | token_list_t *token_list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 114 | %parse-param {glcpp_parser_t *parser} |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 115 | %lex-param {glcpp_parser_t *parser} |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 116 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 117 | %token DEFINE FUNC_MACRO IDENTIFIER OBJ_MACRO NEWLINE SPACE TOKEN UNDEF |
| 118 | %type <str> argument_word FUNC_MACRO IDENTIFIER OBJ_MACRO TOKEN |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 119 | %type <argument_list> argument_list |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 120 | %type <string_list> macro parameter_list |
| 121 | %type <token_list> argument replacement_list pp_tokens |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 122 | |
Carl Worth | 796e1f0 | 2010-05-17 12:45:16 -0700 | [diff] [blame] | 123 | /* Hard to remove shift/reduce conflicts documented as follows: |
| 124 | * |
| 125 | * 1. '(' after FUNC_MACRO name which is correctly resolved to shift |
| 126 | * to form macro invocation rather than reducing directly to |
| 127 | * content. |
Carl Worth | 69f390d | 2010-05-19 07:42:42 -0700 | [diff] [blame] | 128 | * |
| 129 | * 2. Similarly, '(' after FUNC_MACRO which is correctly resolved to |
| 130 | * shift to form macro invocation rather than reducing directly to |
| 131 | * argument. |
Carl Worth | 796e1f0 | 2010-05-17 12:45:16 -0700 | [diff] [blame] | 132 | */ |
Carl Worth | 69f390d | 2010-05-19 07:42:42 -0700 | [diff] [blame] | 133 | %expect 2 |
Carl Worth | 796e1f0 | 2010-05-17 12:45:16 -0700 | [diff] [blame] | 134 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 135 | %% |
| 136 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 137 | input: |
| 138 | /* empty */ |
Carl Worth | 8bcb6f1 | 2010-05-12 13:21:20 -0700 | [diff] [blame] | 139 | | input content |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 140 | ; |
| 141 | |
Carl Worth | 04af135 | 2010-05-14 10:17:38 -0700 | [diff] [blame] | 142 | /* We do all printing at the content level */ |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 143 | content: |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 144 | IDENTIFIER { |
| 145 | printf ("%s", $1); |
| 146 | talloc_free ($1); |
| 147 | } |
| 148 | | TOKEN { |
| 149 | printf ("%s", $1); |
| 150 | talloc_free ($1); |
| 151 | } |
Carl Worth | acf87bc | 2010-05-17 10:34:29 -0700 | [diff] [blame] | 152 | | FUNC_MACRO { |
| 153 | printf ("%s", $1); |
| 154 | talloc_free ($1); |
| 155 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 156 | | directive { |
| 157 | printf ("\n"); |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 158 | } |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 159 | | '(' { printf ("("); } |
| 160 | | ')' { printf (")"); } |
| 161 | | ',' { printf (","); } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 162 | | macro |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 163 | ; |
| 164 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 165 | macro: |
| 166 | FUNC_MACRO '(' argument_list ')' { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 167 | _expand_function_macro (parser, $1, $3); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 168 | } |
| 169 | | OBJ_MACRO { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 170 | _expand_object_macro (parser, $1); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 171 | talloc_free ($1); |
| 172 | } |
| 173 | ; |
| 174 | |
| 175 | argument_list: |
Carl Worth | ac070e8 | 2010-05-14 11:33:00 -0700 | [diff] [blame] | 176 | /* empty */ { |
| 177 | $$ = _argument_list_create (parser); |
| 178 | } |
| 179 | | argument { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 180 | $$ = _argument_list_create (parser); |
| 181 | _argument_list_append ($$, $1); |
| 182 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 183 | | argument_list ',' argument { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 184 | _argument_list_append ($1, $3); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 185 | $$ = $1; |
| 186 | } |
| 187 | ; |
| 188 | |
| 189 | argument: |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 190 | argument_word { |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 191 | $$ = _token_list_create (parser); |
| 192 | _token_list_append ($$, IDENTIFIER, $1); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 193 | } |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 194 | | argument argument_word { |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 195 | _token_list_append ($1, IDENTIFIER, $2); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 196 | talloc_free ($2); |
Carl Worth | 3596bb1 | 2010-05-14 16:53:52 -0700 | [diff] [blame] | 197 | $$ = $1; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 198 | } |
Carl Worth | 3596bb1 | 2010-05-14 16:53:52 -0700 | [diff] [blame] | 199 | | argument '(' argument ')' { |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 200 | _token_list_append ($1, '(', "("); |
| 201 | _token_list_append_list ($1, $3); |
| 202 | _token_list_append ($1, ')', ")"); |
Carl Worth | 3596bb1 | 2010-05-14 16:53:52 -0700 | [diff] [blame] | 203 | $$ = $1; |
| 204 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 205 | ; |
| 206 | |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 207 | argument_word: |
| 208 | IDENTIFIER { $$ = $1; } |
| 209 | | TOKEN { $$ = $1; } |
| 210 | | FUNC_MACRO { $$ = $1; } |
Carl Worth | 5d21142 | 2010-05-19 07:57:03 -0700 | [diff] [blame] | 211 | | macro { $$ = xtalloc_strdup (parser, ""); } |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 212 | ; |
| 213 | |
| 214 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 215 | directive: |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 216 | DEFINE IDENTIFIER NEWLINE { |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 217 | token_list_t *list = _token_list_create (parser); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 218 | _define_object_macro (parser, $2, list); |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 219 | } |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 220 | | DEFINE IDENTIFIER SPACE replacement_list NEWLINE { |
| 221 | _define_object_macro (parser, $2, $4); |
| 222 | } |
| 223 | | DEFINE IDENTIFIER '(' parameter_list ')' replacement_list NEWLINE { |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 224 | _define_function_macro (parser, $2, $4, $6); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 225 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 226 | | UNDEF IDENTIFIER { |
| 227 | string_list_t *macro = hash_table_find (parser->defines, $2); |
| 228 | if (macro) { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 229 | /* XXX: Need hash table to support a real way |
| 230 | * to remove an element rather than prefixing |
| 231 | * a new node with data of NULL like this. */ |
| 232 | hash_table_insert (parser->defines, NULL, $2); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 233 | talloc_free (macro); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 234 | } |
| 235 | talloc_free ($2); |
| 236 | } |
Carl Worth | 38bd27b | 2010-05-14 12:05:37 -0700 | [diff] [blame] | 237 | ; |
| 238 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 239 | parameter_list: |
| 240 | /* empty */ { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 241 | $$ = _string_list_create (parser); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 242 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 243 | | IDENTIFIER { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 244 | $$ = _string_list_create (parser); |
| 245 | _string_list_append_item ($$, $1); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 246 | talloc_free ($1); |
| 247 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 248 | | parameter_list ',' IDENTIFIER { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 249 | _string_list_append_item ($1, $3); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 250 | talloc_free ($3); |
| 251 | $$ = $1; |
| 252 | } |
| 253 | ; |
| 254 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 255 | replacement_list: |
| 256 | /* empty */ { |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 257 | $$ = _token_list_create (parser); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 258 | } |
| 259 | | pp_tokens { |
| 260 | $$ = $1; |
| 261 | } |
| 262 | ; |
| 263 | |
| 264 | |
| 265 | pp_tokens: |
| 266 | TOKEN { |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 267 | $$ = _token_list_create (parser); |
| 268 | _token_list_append ($$, TOKEN, $1); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 269 | } |
| 270 | | pp_tokens TOKEN { |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 271 | _token_list_append ($1, TOKEN, $2); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 272 | $$ = $1; |
| 273 | } |
| 274 | ; |
| 275 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 276 | %% |
| 277 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 278 | string_list_t * |
| 279 | _string_list_create (void *ctx) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 280 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 281 | string_list_t *list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 282 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 283 | list = xtalloc (ctx, string_list_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 284 | list->head = NULL; |
| 285 | list->tail = NULL; |
| 286 | |
| 287 | return list; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 288 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 289 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 290 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 291 | _string_list_append_list (string_list_t *list, string_list_t *tail) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 292 | { |
| 293 | if (list->head == NULL) { |
| 294 | list->head = tail->head; |
| 295 | } else { |
| 296 | list->tail->next = tail->head; |
| 297 | } |
| 298 | |
| 299 | list->tail = tail->tail; |
| 300 | } |
| 301 | |
| 302 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 303 | _string_list_append_item (string_list_t *list, const char *str) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 304 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 305 | string_node_t *node; |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 306 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 307 | node = xtalloc (list, string_node_t); |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 308 | node->str = xtalloc_strdup (node, str); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 309 | |
| 310 | node->next = NULL; |
| 311 | |
| 312 | if (list->head == NULL) { |
| 313 | list->head = node; |
| 314 | } else { |
| 315 | list->tail->next = node; |
| 316 | } |
| 317 | |
| 318 | list->tail = node; |
| 319 | } |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 320 | |
| 321 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 322 | _string_list_contains (string_list_t *list, const char *member, int *index) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 323 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 324 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 325 | int i; |
| 326 | |
| 327 | if (list == NULL) |
| 328 | return 0; |
| 329 | |
| 330 | for (i = 0, node = list->head; node; i++, node = node->next) { |
| 331 | if (strcmp (node->str, member) == 0) { |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 332 | if (index) |
| 333 | *index = i; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 334 | return 1; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 342 | _string_list_length (string_list_t *list) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 343 | { |
| 344 | int length = 0; |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 345 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 346 | |
| 347 | if (list == NULL) |
| 348 | return 0; |
| 349 | |
| 350 | for (node = list->head; node; node = node->next) |
| 351 | length++; |
| 352 | |
| 353 | return length; |
| 354 | } |
| 355 | |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 356 | void |
| 357 | _print_string_list (string_list_t *list) |
| 358 | { |
| 359 | string_node_t *node; |
| 360 | |
| 361 | if (list == NULL) |
| 362 | return; |
| 363 | |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 364 | for (node = list->head; node; node = node->next) { |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 365 | printf ("%s", node->str); |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 366 | if (node->next) |
| 367 | printf (" "); |
| 368 | } |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 371 | argument_list_t * |
| 372 | _argument_list_create (void *ctx) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 373 | { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 374 | argument_list_t *list; |
| 375 | |
| 376 | list = xtalloc (ctx, argument_list_t); |
| 377 | list->head = NULL; |
| 378 | list->tail = NULL; |
| 379 | |
| 380 | return list; |
| 381 | } |
| 382 | |
| 383 | void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 384 | _argument_list_append (argument_list_t *list, token_list_t *argument) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 385 | { |
| 386 | argument_node_t *node; |
| 387 | |
| 388 | if (argument == NULL || argument->head == NULL) |
| 389 | return; |
| 390 | |
| 391 | node = xtalloc (list, argument_node_t); |
| 392 | node->argument = argument; |
| 393 | |
| 394 | node->next = NULL; |
| 395 | |
| 396 | if (list->head == NULL) { |
| 397 | list->head = node; |
| 398 | } else { |
| 399 | list->tail->next = node; |
| 400 | } |
| 401 | |
| 402 | list->tail = node; |
| 403 | } |
| 404 | |
| 405 | int |
| 406 | _argument_list_length (argument_list_t *list) |
| 407 | { |
| 408 | int length = 0; |
| 409 | argument_node_t *node; |
| 410 | |
| 411 | if (list == NULL) |
| 412 | return 0; |
| 413 | |
| 414 | for (node = list->head; node; node = node->next) |
| 415 | length++; |
| 416 | |
| 417 | return length; |
| 418 | } |
| 419 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 420 | token_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 421 | _argument_list_member_at (argument_list_t *list, int index) |
| 422 | { |
| 423 | argument_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 424 | int i; |
| 425 | |
| 426 | if (list == NULL) |
| 427 | return NULL; |
| 428 | |
| 429 | node = list->head; |
| 430 | for (i = 0; i < index; i++) { |
| 431 | node = node->next; |
| 432 | if (node == NULL) |
| 433 | break; |
| 434 | } |
| 435 | |
| 436 | if (node) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 437 | return node->argument; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 438 | |
| 439 | return NULL; |
| 440 | } |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 441 | |
| 442 | token_list_t * |
| 443 | _token_list_create (void *ctx) |
| 444 | { |
| 445 | token_list_t *list; |
| 446 | |
| 447 | list = xtalloc (ctx, token_list_t); |
| 448 | list->head = NULL; |
| 449 | list->tail = NULL; |
| 450 | |
| 451 | return list; |
| 452 | } |
| 453 | |
| 454 | void |
| 455 | _token_list_append (token_list_t *list, int type, const char *value) |
| 456 | { |
| 457 | token_node_t *node; |
| 458 | |
| 459 | node = xtalloc (list, token_node_t); |
| 460 | node->type = type; |
| 461 | node->value = xtalloc_strdup (list, value); |
| 462 | |
| 463 | node->next = NULL; |
| 464 | |
| 465 | if (list->head == NULL) { |
| 466 | list->head = node; |
| 467 | } else { |
| 468 | list->tail->next = node; |
| 469 | } |
| 470 | |
| 471 | list->tail = node; |
| 472 | } |
| 473 | |
| 474 | void |
| 475 | _token_list_append_list (token_list_t *list, token_list_t *tail) |
| 476 | { |
| 477 | if (list->head == NULL) { |
| 478 | list->head = tail->head; |
| 479 | } else { |
| 480 | list->tail->next = tail->head; |
| 481 | } |
| 482 | |
| 483 | list->tail = tail->tail; |
| 484 | } |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 485 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 486 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 487 | yyerror (void *scanner, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 488 | { |
| 489 | fprintf (stderr, "Parse error: %s\n", error); |
| 490 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 491 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 492 | glcpp_parser_t * |
| 493 | glcpp_parser_create (void) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 494 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 495 | glcpp_parser_t *parser; |
| 496 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 497 | parser = xtalloc (NULL, glcpp_parser_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 498 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 499 | glcpp_lex_init_extra (parser, &parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 500 | parser->defines = hash_table_ctor (32, hash_table_string_hash, |
| 501 | hash_table_string_compare); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 502 | parser->expansions = NULL; |
| 503 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 504 | return parser; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | int |
| 508 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 509 | { |
| 510 | return yyparse (parser); |
| 511 | } |
| 512 | |
| 513 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 514 | glcpp_parser_destroy (glcpp_parser_t *parser) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 515 | { |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 516 | glcpp_lex_destroy (parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 517 | hash_table_dtor (parser->defines); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 518 | talloc_free (parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 519 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 520 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 521 | static int |
| 522 | glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member) |
| 523 | { |
| 524 | expansion_node_t *node; |
| 525 | |
| 526 | for (node = parser->expansions; node; node = node->next) { |
| 527 | if (node->macro && |
| 528 | strcmp (node->macro->identifier, member) == 0) |
| 529 | { |
| 530 | return 1; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 537 | token_class_t |
| 538 | glcpp_parser_classify_token (glcpp_parser_t *parser, |
| 539 | const char *identifier, |
| 540 | int *parameter_index) |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 541 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 542 | macro_t *macro; |
| 543 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 544 | /* First we check if we are currently expanding a |
| 545 | * function-like macro, and if so, whether the parameter list |
| 546 | * contains a parameter matching this token name. */ |
| 547 | if (parser->expansions && |
| 548 | parser->expansions->macro && |
| 549 | parser->expansions->macro->parameters) |
| 550 | { |
| 551 | string_list_t *list; |
| 552 | |
| 553 | list = parser->expansions->macro->parameters; |
| 554 | |
| 555 | if (_string_list_contains (list, identifier, parameter_index)) |
| 556 | return TOKEN_CLASS_ARGUMENT; |
| 557 | } |
| 558 | |
| 559 | /* If not a function-like macro parameter, we next check if |
| 560 | * this token is a macro itself. */ |
| 561 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 562 | macro = hash_table_find (parser->defines, identifier); |
| 563 | |
| 564 | if (macro == NULL) |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 565 | return TOKEN_CLASS_IDENTIFIER; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 566 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 567 | /* Don't consider this a macro if we are already actively |
| 568 | * expanding this macro. */ |
| 569 | if (glcpp_parser_is_expanding (parser, identifier)) |
| 570 | return TOKEN_CLASS_IDENTIFIER; |
| 571 | |
| 572 | /* Definitely a macro. Just need to check if it's function-like. */ |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 573 | if (macro->is_function) |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 574 | return TOKEN_CLASS_FUNC_MACRO; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 575 | else |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 576 | return TOKEN_CLASS_OBJ_MACRO; |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 579 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 580 | _define_object_macro (glcpp_parser_t *parser, |
| 581 | const char *identifier, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 582 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 583 | { |
| 584 | macro_t *macro; |
| 585 | |
| 586 | macro = xtalloc (parser, macro_t); |
| 587 | |
| 588 | macro->is_function = 0; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 589 | macro->parameters = NULL; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 590 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 591 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 592 | |
| 593 | hash_table_insert (parser->defines, macro, identifier); |
| 594 | } |
| 595 | |
| 596 | void |
| 597 | _define_function_macro (glcpp_parser_t *parser, |
| 598 | const char *identifier, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 599 | string_list_t *parameters, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 600 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 601 | { |
| 602 | macro_t *macro; |
| 603 | |
| 604 | macro = xtalloc (parser, macro_t); |
| 605 | |
| 606 | macro->is_function = 1; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 607 | macro->parameters = talloc_steal (macro, parameters); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 608 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 609 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 610 | |
| 611 | hash_table_insert (parser->defines, macro, identifier); |
| 612 | } |
| 613 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 614 | static void |
| 615 | _glcpp_parser_push_expansion_internal (glcpp_parser_t *parser, |
| 616 | macro_t *macro, |
| 617 | argument_list_t *arguments, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 618 | token_node_t *replacements) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 619 | { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 620 | expansion_node_t *node; |
| 621 | |
| 622 | node = xtalloc (parser, expansion_node_t); |
| 623 | |
| 624 | node->macro = macro; |
| 625 | node->arguments = arguments; |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 626 | node->replacements = replacements; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 627 | |
| 628 | node->next = parser->expansions; |
| 629 | parser->expansions = node; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 632 | static void |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 633 | glcpp_parser_push_expansion_macro (glcpp_parser_t *parser, |
| 634 | macro_t *macro, |
| 635 | argument_list_t *arguments) |
| 636 | { |
| 637 | _glcpp_parser_push_expansion_internal (parser, macro, arguments, |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 638 | macro->replacements->head); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | void |
| 642 | glcpp_parser_push_expansion_argument (glcpp_parser_t *parser, |
| 643 | int argument_index) |
| 644 | { |
| 645 | argument_list_t *arguments; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 646 | token_list_t *argument; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 647 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 648 | arguments = parser->expansions->arguments; |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 649 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 650 | argument = _argument_list_member_at (arguments, argument_index); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 651 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 652 | _glcpp_parser_push_expansion_internal (parser, NULL, NULL, |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 653 | argument->head); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 656 | static void |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 657 | glcpp_parser_pop_expansion (glcpp_parser_t *parser) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 658 | { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 659 | expansion_node_t *node; |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 660 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 661 | node = parser->expansions; |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 662 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 663 | if (node == NULL) { |
| 664 | fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n"); |
| 665 | exit (1); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 668 | parser->expansions = node->next; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 669 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 670 | talloc_free (node); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 673 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 674 | _expand_object_macro (glcpp_parser_t *parser, const char *identifier) |
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_t *macro; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 677 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 678 | macro = hash_table_find (parser->defines, identifier); |
| 679 | assert (! macro->is_function); |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 680 | assert (! glcpp_parser_is_expanding (parser, identifier)); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 681 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 682 | glcpp_parser_push_expansion_macro (parser, macro, NULL); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 685 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 686 | _expand_function_macro (glcpp_parser_t *parser, |
| 687 | const char *identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 688 | argument_list_t *arguments) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 689 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 690 | macro_t *macro; |
| 691 | |
| 692 | macro = hash_table_find (parser->defines, identifier); |
| 693 | assert (macro->is_function); |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 694 | assert (! glcpp_parser_is_expanding (parser, identifier)); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 695 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 696 | if (_argument_list_length (arguments) != |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 697 | _string_list_length (macro->parameters)) |
| 698 | { |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 699 | fprintf (stderr, |
| 700 | "Error: macro %s invoked with %d arguments (expected %d)\n", |
| 701 | identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 702 | _argument_list_length (arguments), |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 703 | _string_list_length (macro->parameters)); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 704 | return; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 705 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 706 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 707 | glcpp_parser_push_expansion_macro (parser, macro, arguments); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 708 | } |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 709 | |
| 710 | static int |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 711 | glcpp_parser_lex (glcpp_parser_t *parser) |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 712 | { |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 713 | expansion_node_t *expansion; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 714 | token_node_t *replacements; |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 715 | int parameter_index; |
| 716 | |
| 717 | /* Who says C can't do efficient tail recursion? */ |
| 718 | RECURSE: |
| 719 | |
| 720 | expansion = parser->expansions; |
| 721 | |
| 722 | if (expansion == NULL) |
| 723 | return glcpp_lex (parser->scanner); |
| 724 | |
| 725 | replacements = expansion->replacements; |
| 726 | |
| 727 | /* Pop expansion when replacements is exhausted. */ |
| 728 | if (replacements == NULL) { |
| 729 | glcpp_parser_pop_expansion (parser); |
| 730 | goto RECURSE; |
| 731 | } |
| 732 | |
| 733 | expansion->replacements = replacements->next; |
| 734 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 735 | if (strcmp (replacements->value, "(") == 0) |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 736 | return '('; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 737 | else if (strcmp (replacements->value, ")") == 0) |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 738 | return ')'; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 739 | else if (strcmp (replacements->value, ",") == 0) |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 740 | return ','; |
| 741 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame^] | 742 | yylval.str = xtalloc_strdup (parser, replacements->value); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 743 | |
| 744 | switch (glcpp_parser_classify_token (parser, yylval.str, |
| 745 | ¶meter_index)) |
| 746 | { |
| 747 | case TOKEN_CLASS_ARGUMENT: |
| 748 | talloc_free (yylval.str); |
| 749 | glcpp_parser_push_expansion_argument (parser, |
| 750 | parameter_index); |
| 751 | goto RECURSE; |
| 752 | break; |
| 753 | case TOKEN_CLASS_IDENTIFIER: |
| 754 | return IDENTIFIER; |
| 755 | break; |
| 756 | case TOKEN_CLASS_FUNC_MACRO: |
| 757 | return FUNC_MACRO; |
| 758 | break; |
| 759 | default: |
| 760 | case TOKEN_CLASS_OBJ_MACRO: |
| 761 | return OBJ_MACRO; |
| 762 | break; |
| 763 | } |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 764 | } |