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 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 53 | string_list_t * |
| 54 | _string_list_create (void *ctx); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 55 | |
| 56 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 57 | _string_list_append_item (string_list_t *list, const char *str); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 58 | |
| 59 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 60 | _string_list_append_list (string_list_t *list, string_list_t *tail); |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 61 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 62 | void |
| 63 | _string_list_push (string_list_t *list, const char *str); |
| 64 | |
| 65 | void |
| 66 | _string_list_pop (string_list_t *list); |
| 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_contains (string_list_t *list, const char *member, int *index); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 70 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 71 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 72 | _string_list_length (string_list_t *list); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 73 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 74 | argument_list_t * |
| 75 | _argument_list_create (void *ctx); |
| 76 | |
| 77 | void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 78 | _argument_list_append (argument_list_t *list, token_list_t *argument); |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 79 | |
| 80 | int |
| 81 | _argument_list_length (argument_list_t *list); |
| 82 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 83 | token_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 84 | _argument_list_member_at (argument_list_t *list, int index); |
| 85 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 86 | /* Note: This function talloc_steal()s the str pointer. */ |
| 87 | token_t * |
| 88 | _token_create_str (void *ctx, int type, char *str); |
| 89 | |
| 90 | token_t * |
| 91 | _token_create_ival (void *ctx, int type, int ival); |
| 92 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 93 | token_list_t * |
| 94 | _token_list_create (void *ctx); |
| 95 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 96 | /* Note: This function add a talloc_reference() to token. |
| 97 | * |
| 98 | * You may want to talloc_unlink any current reference if you no |
| 99 | * longer need it. */ |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 100 | void |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 101 | _token_list_append (token_list_t *list, token_t *token); |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 102 | |
| 103 | void |
| 104 | _token_list_append_list (token_list_t *list, token_list_t *tail); |
| 105 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 106 | void |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 107 | _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser, |
| 108 | token_list_t *list); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 109 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 110 | static void |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 111 | glcpp_parser_pop_expansion (glcpp_parser_t *parser); |
| 112 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 113 | static void |
| 114 | _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition); |
| 115 | |
| 116 | static void |
| 117 | _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type, |
| 118 | int condition); |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 119 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 120 | static void |
| 121 | _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser); |
| 122 | |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 123 | #define yylex glcpp_parser_lex |
| 124 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 125 | static int |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 126 | glcpp_parser_lex (glcpp_parser_t *parser); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 127 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 128 | %} |
| 129 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 130 | %parse-param {glcpp_parser_t *parser} |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 131 | %lex-param {glcpp_parser_t *parser} |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 132 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 133 | %token HASH HASH_DEFINE_FUNC HASH_DEFINE_OBJ HASH_UNDEF IDENTIFIER NEWLINE OTHER SPACE |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 134 | %token LEFT_SHIFT RIGHT_SHIFT LESS_OR_EQUAL GREATER_OR_EQUAL EQUAL NOT_EQUAL AND OR PASTE |
Carl Worth | e939786 | 2010-05-25 17:08:07 -0700 | [diff] [blame] | 135 | %type <ival> punctuator SPACE |
| 136 | %type <str> IDENTIFIER OTHER |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 137 | %type <string_list> identifier_list |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 138 | %type <token> preprocessing_token |
| 139 | %type <token_list> pp_tokens replacement_list text_line |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 140 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 141 | /* Stale stuff just to allow code to compile. */ |
| 142 | %token IDENTIFIER_FINALIZED FUNC_MACRO OBJ_MACRO |
Carl Worth | 796e1f0 | 2010-05-17 12:45:16 -0700 | [diff] [blame] | 143 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 144 | %% |
| 145 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 146 | input: |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 147 | /* empty */ |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 148 | | input line { |
| 149 | printf ("\n"); |
| 150 | } |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 151 | ; |
| 152 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 153 | line: |
| 154 | control_line |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 155 | | text_line { |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 156 | _glcpp_parser_print_expanded_token_list (parser, $1); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 157 | talloc_free ($1); |
| 158 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 159 | | HASH non_directive |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 160 | ; |
| 161 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 162 | control_line: |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 163 | HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE { |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 164 | _define_object_macro (parser, $2, $3); |
| 165 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 166 | | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE { |
| 167 | _define_function_macro (parser, $2, NULL, $5); |
| 168 | } |
| 169 | | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE { |
| 170 | _define_function_macro (parser, $2, $4, $6); |
| 171 | } |
Carl Worth | e6fb782 | 2010-05-25 15:28:58 -0700 | [diff] [blame] | 172 | | HASH_UNDEF IDENTIFIER NEWLINE { |
| 173 | string_list_t *macro = hash_table_find (parser->defines, $2); |
| 174 | if (macro) { |
| 175 | /* XXX: Need hash table to support a real way |
| 176 | * to remove an element rather than prefixing |
| 177 | * a new node with data of NULL like this. */ |
| 178 | hash_table_insert (parser->defines, NULL, $2); |
| 179 | talloc_free (macro); |
| 180 | } |
| 181 | talloc_free ($2); |
| 182 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 183 | | HASH NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 184 | ; |
| 185 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 186 | identifier_list: |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 187 | IDENTIFIER { |
| 188 | $$ = _string_list_create (parser); |
| 189 | _string_list_append_item ($$, $1); |
| 190 | talloc_steal ($$, $1); |
| 191 | } |
| 192 | | identifier_list ',' IDENTIFIER { |
| 193 | $$ = $1; |
| 194 | _string_list_append_item ($$, $3); |
| 195 | talloc_steal ($$, $3); |
| 196 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 197 | ; |
| 198 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 199 | text_line: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 200 | NEWLINE { $$ = NULL; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 201 | | pp_tokens NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 202 | ; |
| 203 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 204 | non_directive: |
| 205 | pp_tokens NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 206 | ; |
| 207 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 208 | replacement_list: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 209 | /* empty */ { $$ = NULL; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 210 | | pp_tokens |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 211 | ; |
| 212 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 213 | pp_tokens: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 214 | preprocessing_token { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 215 | parser->space_tokens = 1; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 216 | $$ = _token_list_create (parser); |
| 217 | _token_list_append ($$, $1); |
| 218 | talloc_unlink (parser, $1); |
| 219 | } |
| 220 | | pp_tokens preprocessing_token { |
| 221 | $$ = $1; |
| 222 | _token_list_append ($$, $2); |
| 223 | talloc_unlink (parser, $2); |
| 224 | } |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 225 | ; |
| 226 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 227 | preprocessing_token: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 228 | IDENTIFIER { |
| 229 | $$ = _token_create_str (parser, IDENTIFIER, $1); |
| 230 | } |
| 231 | | punctuator { |
| 232 | $$ = _token_create_ival (parser, $1, $1); |
| 233 | } |
| 234 | | OTHER { |
| 235 | $$ = _token_create_str (parser, OTHER, $1); |
| 236 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 237 | | SPACE { |
Carl Worth | e939786 | 2010-05-25 17:08:07 -0700 | [diff] [blame] | 238 | $$ = _token_create_ival (parser, SPACE, SPACE); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 239 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 240 | ; |
| 241 | |
| 242 | punctuator: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 243 | '[' { $$ = '['; } |
| 244 | | ']' { $$ = ']'; } |
| 245 | | '(' { $$ = '('; } |
| 246 | | ')' { $$ = ')'; } |
| 247 | | '{' { $$ = '{'; } |
| 248 | | '}' { $$ = '}'; } |
| 249 | | '.' { $$ = '.'; } |
| 250 | | '&' { $$ = '&'; } |
| 251 | | '*' { $$ = '*'; } |
| 252 | | '+' { $$ = '+'; } |
| 253 | | '-' { $$ = '-'; } |
| 254 | | '~' { $$ = '~'; } |
| 255 | | '!' { $$ = '!'; } |
| 256 | | '/' { $$ = '/'; } |
| 257 | | '%' { $$ = '%'; } |
| 258 | | LEFT_SHIFT { $$ = LEFT_SHIFT; } |
| 259 | | RIGHT_SHIFT { $$ = RIGHT_SHIFT; } |
| 260 | | '<' { $$ = '<'; } |
| 261 | | '>' { $$ = '>'; } |
| 262 | | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; } |
| 263 | | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; } |
| 264 | | EQUAL { $$ = EQUAL; } |
| 265 | | NOT_EQUAL { $$ = NOT_EQUAL; } |
| 266 | | '^' { $$ = '^'; } |
| 267 | | '|' { $$ = '|'; } |
| 268 | | AND { $$ = AND; } |
| 269 | | OR { $$ = OR; } |
| 270 | | ';' { $$ = ';'; } |
| 271 | | ',' { $$ = ','; } |
| 272 | | PASTE { $$ = PASTE; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 273 | ; |
| 274 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 275 | %% |
| 276 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 277 | string_list_t * |
| 278 | _string_list_create (void *ctx) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 279 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 280 | string_list_t *list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 281 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 282 | list = xtalloc (ctx, string_list_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 283 | list->head = NULL; |
| 284 | list->tail = NULL; |
| 285 | |
| 286 | return list; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 287 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 288 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 289 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 290 | _string_list_append_list (string_list_t *list, string_list_t *tail) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 291 | { |
| 292 | if (list->head == NULL) { |
| 293 | list->head = tail->head; |
| 294 | } else { |
| 295 | list->tail->next = tail->head; |
| 296 | } |
| 297 | |
| 298 | list->tail = tail->tail; |
| 299 | } |
| 300 | |
| 301 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 302 | _string_list_append_item (string_list_t *list, const char *str) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 303 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 304 | string_node_t *node; |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 305 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 306 | node = xtalloc (list, string_node_t); |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 307 | node->str = xtalloc_strdup (node, str); |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 308 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 309 | node->next = NULL; |
| 310 | |
| 311 | if (list->head == NULL) { |
| 312 | list->head = node; |
| 313 | } else { |
| 314 | list->tail->next = node; |
| 315 | } |
| 316 | |
| 317 | list->tail = node; |
| 318 | } |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 319 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 320 | void |
| 321 | _string_list_push (string_list_t *list, const char *str) |
| 322 | { |
| 323 | string_node_t *node; |
| 324 | |
| 325 | node = xtalloc (list, string_node_t); |
| 326 | node->str = xtalloc_strdup (node, str); |
| 327 | node->next = list->head; |
| 328 | |
| 329 | if (list->tail == NULL) { |
| 330 | list->tail = node; |
| 331 | } |
| 332 | list->head = node; |
| 333 | } |
| 334 | |
| 335 | void |
| 336 | _string_list_pop (string_list_t *list) |
| 337 | { |
| 338 | string_node_t *node; |
| 339 | |
| 340 | node = list->head; |
| 341 | |
| 342 | if (node == NULL) { |
| 343 | fprintf (stderr, "Internal error: _string_list_pop called on an empty list.\n"); |
| 344 | exit (1); |
| 345 | } |
| 346 | |
| 347 | list->head = node->next; |
| 348 | if (list->tail == node) { |
| 349 | assert (node->next == NULL); |
| 350 | list->tail = NULL; |
| 351 | } |
| 352 | |
| 353 | talloc_free (node); |
| 354 | } |
| 355 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 356 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 357 | _string_list_contains (string_list_t *list, const char *member, int *index) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 358 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 359 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 360 | int i; |
| 361 | |
| 362 | if (list == NULL) |
| 363 | return 0; |
| 364 | |
| 365 | for (i = 0, node = list->head; node; i++, node = node->next) { |
| 366 | if (strcmp (node->str, member) == 0) { |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 367 | if (index) |
| 368 | *index = i; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 369 | return 1; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 377 | _string_list_length (string_list_t *list) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 378 | { |
| 379 | int length = 0; |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 380 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 381 | |
| 382 | if (list == NULL) |
| 383 | return 0; |
| 384 | |
| 385 | for (node = list->head; node; node = node->next) |
| 386 | length++; |
| 387 | |
| 388 | return length; |
| 389 | } |
| 390 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 391 | argument_list_t * |
| 392 | _argument_list_create (void *ctx) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 393 | { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 394 | argument_list_t *list; |
| 395 | |
| 396 | list = xtalloc (ctx, argument_list_t); |
| 397 | list->head = NULL; |
| 398 | list->tail = NULL; |
| 399 | |
| 400 | return list; |
| 401 | } |
| 402 | |
| 403 | void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 404 | _argument_list_append (argument_list_t *list, token_list_t *argument) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 405 | { |
| 406 | argument_node_t *node; |
| 407 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 408 | node = xtalloc (list, argument_node_t); |
| 409 | node->argument = argument; |
| 410 | |
| 411 | node->next = NULL; |
| 412 | |
| 413 | if (list->head == NULL) { |
| 414 | list->head = node; |
| 415 | } else { |
| 416 | list->tail->next = node; |
| 417 | } |
| 418 | |
| 419 | list->tail = node; |
| 420 | } |
| 421 | |
| 422 | int |
| 423 | _argument_list_length (argument_list_t *list) |
| 424 | { |
| 425 | int length = 0; |
| 426 | argument_node_t *node; |
| 427 | |
| 428 | if (list == NULL) |
| 429 | return 0; |
| 430 | |
| 431 | for (node = list->head; node; node = node->next) |
| 432 | length++; |
| 433 | |
| 434 | return length; |
| 435 | } |
| 436 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 437 | token_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 438 | _argument_list_member_at (argument_list_t *list, int index) |
| 439 | { |
| 440 | argument_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 441 | int i; |
| 442 | |
| 443 | if (list == NULL) |
| 444 | return NULL; |
| 445 | |
| 446 | node = list->head; |
| 447 | for (i = 0; i < index; i++) { |
| 448 | node = node->next; |
| 449 | if (node == NULL) |
| 450 | break; |
| 451 | } |
| 452 | |
| 453 | if (node) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 454 | return node->argument; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 455 | |
| 456 | return NULL; |
| 457 | } |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 458 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 459 | /* Note: This function talloc_steal()s the str pointer. */ |
| 460 | token_t * |
| 461 | _token_create_str (void *ctx, int type, char *str) |
| 462 | { |
| 463 | token_t *token; |
| 464 | |
| 465 | token = xtalloc (ctx, token_t); |
| 466 | token->type = type; |
| 467 | token->value.str = talloc_steal (token, str); |
| 468 | |
| 469 | return token; |
| 470 | } |
| 471 | |
| 472 | token_t * |
| 473 | _token_create_ival (void *ctx, int type, int ival) |
| 474 | { |
| 475 | token_t *token; |
| 476 | |
| 477 | token = xtalloc (ctx, token_t); |
| 478 | token->type = type; |
| 479 | token->value.ival = ival; |
| 480 | |
| 481 | return token; |
| 482 | } |
| 483 | |
| 484 | void |
| 485 | _token_print (token_t *token) |
| 486 | { |
| 487 | if (token->type < 256) { |
| 488 | printf ("%c", token->type); |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | switch (token->type) { |
| 493 | case IDENTIFIER: |
| 494 | case OTHER: |
| 495 | printf ("%s", token->value.str); |
| 496 | break; |
Carl Worth | e939786 | 2010-05-25 17:08:07 -0700 | [diff] [blame] | 497 | case SPACE: |
| 498 | printf (" "); |
| 499 | break; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 500 | case LEFT_SHIFT: |
| 501 | printf ("<<"); |
| 502 | break; |
| 503 | case RIGHT_SHIFT: |
| 504 | printf (">>"); |
| 505 | break; |
| 506 | case LESS_OR_EQUAL: |
| 507 | printf ("<="); |
| 508 | break; |
| 509 | case GREATER_OR_EQUAL: |
| 510 | printf (">="); |
| 511 | break; |
| 512 | case EQUAL: |
| 513 | printf ("=="); |
| 514 | break; |
| 515 | case NOT_EQUAL: |
| 516 | printf ("!="); |
| 517 | break; |
| 518 | case AND: |
| 519 | printf ("&&"); |
| 520 | break; |
| 521 | case OR: |
| 522 | printf ("||"); |
| 523 | break; |
| 524 | case PASTE: |
| 525 | printf ("##"); |
| 526 | break; |
| 527 | default: |
| 528 | fprintf (stderr, "Error: Don't know how to print token type %d\n", token->type); |
| 529 | break; |
| 530 | } |
| 531 | } |
| 532 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 533 | token_list_t * |
| 534 | _token_list_create (void *ctx) |
| 535 | { |
| 536 | token_list_t *list; |
| 537 | |
| 538 | list = xtalloc (ctx, token_list_t); |
| 539 | list->head = NULL; |
| 540 | list->tail = NULL; |
| 541 | |
| 542 | return list; |
| 543 | } |
| 544 | |
| 545 | void |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 546 | _token_list_append (token_list_t *list, token_t *token) |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 547 | { |
| 548 | token_node_t *node; |
| 549 | |
| 550 | node = xtalloc (list, token_node_t); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 551 | node->token = xtalloc_reference (list, token); |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 552 | |
| 553 | node->next = NULL; |
| 554 | |
| 555 | if (list->head == NULL) { |
| 556 | list->head = node; |
| 557 | } else { |
| 558 | list->tail->next = node; |
| 559 | } |
| 560 | |
| 561 | list->tail = node; |
| 562 | } |
| 563 | |
| 564 | void |
| 565 | _token_list_append_list (token_list_t *list, token_list_t *tail) |
| 566 | { |
| 567 | if (list->head == NULL) { |
| 568 | list->head = tail->head; |
| 569 | } else { |
| 570 | list->tail->next = tail->head; |
| 571 | } |
| 572 | |
| 573 | list->tail = tail->tail; |
| 574 | } |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 575 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 576 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 577 | yyerror (void *scanner, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 578 | { |
| 579 | fprintf (stderr, "Parse error: %s\n", error); |
| 580 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 581 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 582 | glcpp_parser_t * |
| 583 | glcpp_parser_create (void) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 584 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 585 | glcpp_parser_t *parser; |
| 586 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 587 | parser = xtalloc (NULL, glcpp_parser_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 588 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 589 | glcpp_lex_init_extra (parser, &parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 590 | parser->defines = hash_table_ctor (32, hash_table_string_hash, |
| 591 | hash_table_string_compare); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 592 | parser->active = _string_list_create (parser); |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 593 | parser->space_tokens = 1; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 594 | parser->expansions = NULL; |
| 595 | |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame] | 596 | parser->just_printed_separator = 1; |
Carl Worth | 876e510 | 2010-05-20 14:38:06 -0700 | [diff] [blame] | 597 | parser->need_newline = 0; |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame] | 598 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 599 | parser->skip_stack = NULL; |
| 600 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 601 | return parser; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | int |
| 605 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 606 | { |
| 607 | return yyparse (parser); |
| 608 | } |
| 609 | |
| 610 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 611 | glcpp_parser_destroy (glcpp_parser_t *parser) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 612 | { |
Carl Worth | 876e510 | 2010-05-20 14:38:06 -0700 | [diff] [blame] | 613 | if (parser->need_newline) |
| 614 | printf ("\n"); |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 615 | if (parser->skip_stack) |
| 616 | fprintf (stderr, "Error: Unterminated #if\n"); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 617 | glcpp_lex_destroy (parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 618 | hash_table_dtor (parser->defines); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 619 | talloc_free (parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 620 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 621 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 622 | static int |
| 623 | glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member) |
| 624 | { |
| 625 | expansion_node_t *node; |
| 626 | |
| 627 | for (node = parser->expansions; node; node = node->next) { |
| 628 | if (node->macro && |
| 629 | strcmp (node->macro->identifier, member) == 0) |
| 630 | { |
| 631 | return 1; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 638 | token_class_t |
| 639 | glcpp_parser_classify_token (glcpp_parser_t *parser, |
| 640 | const char *identifier, |
| 641 | int *parameter_index) |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 642 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 643 | macro_t *macro; |
| 644 | |
Carl Worth | c10a51b | 2010-05-20 15:15:26 -0700 | [diff] [blame] | 645 | /* Is this token a defined macro? */ |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 646 | macro = hash_table_find (parser->defines, identifier); |
| 647 | |
| 648 | if (macro == NULL) |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 649 | return TOKEN_CLASS_IDENTIFIER; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 650 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 651 | /* Don't consider this a macro if we are already actively |
| 652 | * expanding this macro. */ |
| 653 | if (glcpp_parser_is_expanding (parser, identifier)) |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 654 | return TOKEN_CLASS_IDENTIFIER_FINALIZED; |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 655 | |
| 656 | /* Definitely a macro. Just need to check if it's function-like. */ |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 657 | if (macro->is_function) |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 658 | return TOKEN_CLASS_FUNC_MACRO; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 659 | else |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 660 | return TOKEN_CLASS_OBJ_MACRO; |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 663 | /* Print a non-macro token, or the expansion of an object-like macro. |
| 664 | * |
| 665 | * Returns 0 if this token is completely printed. |
| 666 | * |
| 667 | * Returns 1 in the case that 'token' is a function-like macro that |
| 668 | * needs further expansion. |
| 669 | */ |
| 670 | static int |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 671 | _glcpp_parser_print_expanded_token (glcpp_parser_t *parser, |
| 672 | token_t *token) |
| 673 | { |
| 674 | const char *identifier; |
| 675 | macro_t *macro; |
| 676 | |
| 677 | /* We only expand identifiers */ |
| 678 | if (token->type != IDENTIFIER) { |
| 679 | _token_print (token); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 680 | return 0; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* Look up this identifier in the hash table. */ |
| 684 | identifier = token->value.str; |
| 685 | macro = hash_table_find (parser->defines, identifier); |
| 686 | |
| 687 | /* Not a macro, so just print directly. */ |
| 688 | if (macro == NULL) { |
| 689 | printf ("%s", identifier); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 690 | return 0; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 693 | /* For function-like macros return 1 for further processing. */ |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 694 | if (macro->is_function) { |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 695 | return 1; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /* Finally, don't expand this macro if we're already actively |
| 699 | * expanding it, (to avoid infinite recursion). */ |
| 700 | if (_string_list_contains (parser->active, identifier, NULL)) { |
| 701 | printf ("%s", identifier); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | _string_list_push (parser->active, identifier); |
| 706 | _glcpp_parser_print_expanded_token_list (parser, |
| 707 | macro->replacements); |
| 708 | _string_list_pop (parser->active); |
| 709 | |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | typedef enum function_status |
| 714 | { |
| 715 | FUNCTION_STATUS_SUCCESS, |
| 716 | FUNCTION_NOT_A_FUNCTION, |
| 717 | FUNCTION_UNBALANCED_PARENTHESES |
| 718 | } function_status_t; |
| 719 | |
| 720 | /* Find a set of function-like macro arguments by looking for a |
| 721 | * balanced set of parentheses. Upon return *node will be the last |
| 722 | * consumed node, such that further processing can continue with |
| 723 | * node->next. |
| 724 | * |
| 725 | * Return values: |
| 726 | * |
| 727 | * FUNCTION_STATUS_SUCCESS: |
| 728 | * |
| 729 | * Successfully parsed a set of function arguments. |
| 730 | * |
| 731 | * FUNCTION_NOT_A_FUNCTION: |
| 732 | * |
| 733 | * Macro name not followed by a '('. This is not an error, but |
| 734 | * simply that the macro name should be treated as a non-macro. |
| 735 | * |
| 736 | * FUNCTION_UNBLANCED_PARENTHESES |
| 737 | * |
| 738 | * Macro name is not followed by a balanced set of parentheses. |
| 739 | */ |
| 740 | static function_status_t |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 741 | _arguments_parse (argument_list_t *arguments, token_node_t **node_ret) |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 742 | { |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 743 | token_list_t *argument; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 744 | token_node_t *node = *node_ret, *last; |
| 745 | int paren_count; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 746 | |
| 747 | last = node; |
| 748 | node = node->next; |
| 749 | |
| 750 | /* Ignore whitespace before first parenthesis. */ |
| 751 | while (node && node->token->type == SPACE) |
| 752 | node = node->next; |
| 753 | |
| 754 | if (node == NULL || node->token->type != '(') |
| 755 | return FUNCTION_NOT_A_FUNCTION; |
| 756 | |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame^] | 757 | last = node; |
| 758 | node = node->next; |
| 759 | |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 760 | argument = NULL; |
| 761 | |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame^] | 762 | for (paren_count = 1; node; last = node, node = node->next) { |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 763 | if (node->token->type == '(') |
| 764 | { |
| 765 | paren_count++; |
| 766 | } |
| 767 | else if (node->token->type == ')') |
| 768 | { |
| 769 | paren_count--; |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 770 | if (paren_count == 0) { |
| 771 | last = node; |
| 772 | node = node->next; |
| 773 | break; |
| 774 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 775 | } |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame^] | 776 | |
| 777 | if (node->token->type == ',' && |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 778 | paren_count == 1) |
| 779 | { |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 780 | argument = NULL; |
| 781 | } |
| 782 | else { |
| 783 | if (argument == NULL) { |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 784 | /* Don't treat initial whitespace as |
| 785 | * part of the arguement. */ |
| 786 | if (node->token->type == SPACE) |
| 787 | continue; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 788 | argument = _token_list_create (arguments); |
| 789 | _argument_list_append (arguments, argument); |
| 790 | } |
| 791 | _token_list_append (argument, node->token); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 792 | } |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 793 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 794 | |
| 795 | if (node && paren_count) |
| 796 | return FUNCTION_UNBALANCED_PARENTHESES; |
| 797 | |
| 798 | *node_ret = last; |
| 799 | |
| 800 | return FUNCTION_STATUS_SUCCESS; |
| 801 | } |
| 802 | |
| 803 | /* Prints the expansion of *node (consuming further tokens from the |
| 804 | * list as necessary). Upon return *node will be the last consumed |
| 805 | * node, such that further processing can continue with node->next. */ |
| 806 | static void |
| 807 | _glcpp_parser_print_expanded_function (glcpp_parser_t *parser, |
| 808 | token_node_t **node_ret) |
| 809 | { |
| 810 | macro_t *macro; |
| 811 | token_node_t *node; |
| 812 | const char *identifier; |
| 813 | argument_list_t *arguments; |
| 814 | function_status_t status; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 815 | token_list_t *expanded; |
| 816 | token_node_t *i, *j; |
| 817 | int parameter_index; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 818 | |
| 819 | node = *node_ret; |
| 820 | identifier = node->token->value.str; |
| 821 | |
| 822 | macro = hash_table_find (parser->defines, identifier); |
| 823 | |
| 824 | assert (macro->is_function); |
| 825 | |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 826 | arguments = _argument_list_create (parser); |
| 827 | status = _arguments_parse (arguments, node_ret); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 828 | |
| 829 | switch (status) { |
| 830 | case FUNCTION_STATUS_SUCCESS: |
| 831 | break; |
| 832 | case FUNCTION_NOT_A_FUNCTION: |
| 833 | printf ("%s", identifier); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 834 | return; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 835 | case FUNCTION_UNBALANCED_PARENTHESES: |
| 836 | fprintf (stderr, "Error: Macro %s call has unbalanced parentheses\n", |
| 837 | identifier); |
| 838 | exit (1); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 841 | if (macro->replacements == NULL) { |
| 842 | talloc_free (arguments); |
| 843 | return; |
| 844 | } |
| 845 | |
| 846 | |
| 847 | if (_argument_list_length (arguments) != |
| 848 | _string_list_length (macro->parameters)) |
| 849 | { |
| 850 | fprintf (stderr, |
| 851 | "Error: macro %s invoked with %d arguments (expected %d)\n", |
| 852 | identifier, |
| 853 | _argument_list_length (arguments), |
| 854 | _string_list_length (macro->parameters)); |
| 855 | return; |
| 856 | } |
| 857 | |
| 858 | expanded = _token_list_create (arguments); |
| 859 | |
| 860 | for (i = macro->replacements->head; i; i = i->next) { |
| 861 | if (i->token->type == IDENTIFIER && |
| 862 | _string_list_contains (macro->parameters, |
| 863 | i->token->value.str, |
| 864 | ¶meter_index)) |
| 865 | { |
| 866 | token_list_t *argument; |
| 867 | argument = _argument_list_member_at (arguments, |
| 868 | parameter_index); |
| 869 | for (j = argument->head; j; j = j->next) |
| 870 | { |
| 871 | _token_list_append (expanded, j->token); |
| 872 | } |
| 873 | } else { |
| 874 | _token_list_append (expanded, i->token); |
| 875 | } |
| 876 | } |
| 877 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 878 | _string_list_push (parser->active, identifier); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 879 | _glcpp_parser_print_expanded_token_list (parser, expanded); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 880 | _string_list_pop (parser->active); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 881 | |
| 882 | talloc_free (arguments); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | void |
| 886 | _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser, |
| 887 | token_list_t *list) |
| 888 | { |
| 889 | token_node_t *node; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 890 | function_status_t function_status; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 891 | |
| 892 | if (list == NULL) |
| 893 | return; |
| 894 | |
| 895 | for (node = list->head; node; node = node->next) { |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 896 | if (_glcpp_parser_print_expanded_token (parser, node->token)) |
| 897 | _glcpp_parser_print_expanded_function (parser, &node); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
| 901 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 902 | _define_object_macro (glcpp_parser_t *parser, |
| 903 | const char *identifier, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 904 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 905 | { |
| 906 | macro_t *macro; |
| 907 | |
| 908 | macro = xtalloc (parser, macro_t); |
| 909 | |
| 910 | macro->is_function = 0; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 911 | macro->parameters = NULL; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 912 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 913 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 914 | |
| 915 | hash_table_insert (parser->defines, macro, identifier); |
| 916 | } |
| 917 | |
| 918 | void |
| 919 | _define_function_macro (glcpp_parser_t *parser, |
| 920 | const char *identifier, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 921 | string_list_t *parameters, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 922 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 923 | { |
| 924 | macro_t *macro; |
| 925 | |
| 926 | macro = xtalloc (parser, macro_t); |
| 927 | |
| 928 | macro->is_function = 1; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 929 | macro->parameters = talloc_steal (macro, parameters); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 930 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 931 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 932 | |
| 933 | hash_table_insert (parser->defines, macro, identifier); |
| 934 | } |
| 935 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 936 | static void |
Carl Worth | c10a51b | 2010-05-20 15:15:26 -0700 | [diff] [blame] | 937 | _glcpp_parser_push_expansion (glcpp_parser_t *parser, |
| 938 | macro_t *macro, |
| 939 | token_node_t *replacements) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 940 | { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 941 | expansion_node_t *node; |
| 942 | |
| 943 | node = xtalloc (parser, expansion_node_t); |
| 944 | |
| 945 | node->macro = macro; |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 946 | node->replacements = replacements; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 947 | |
| 948 | node->next = parser->expansions; |
| 949 | parser->expansions = node; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 952 | static void |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 953 | glcpp_parser_pop_expansion (glcpp_parser_t *parser) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 954 | { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 955 | expansion_node_t *node; |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 956 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 957 | node = parser->expansions; |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 958 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 959 | if (node == NULL) { |
| 960 | fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n"); |
| 961 | exit (1); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 964 | parser->expansions = node->next; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 965 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 966 | talloc_free (node); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 967 | } |
| 968 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 969 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 970 | _expand_object_macro (glcpp_parser_t *parser, const char *identifier) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 971 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 972 | macro_t *macro; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 973 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 974 | macro = hash_table_find (parser->defines, identifier); |
| 975 | assert (! macro->is_function); |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 976 | assert (! glcpp_parser_is_expanding (parser, identifier)); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 977 | |
Carl Worth | c10a51b | 2010-05-20 15:15:26 -0700 | [diff] [blame] | 978 | _glcpp_parser_push_expansion (parser, macro, macro->replacements->head); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 979 | } |
| 980 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 981 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 982 | _expand_function_macro (glcpp_parser_t *parser, |
| 983 | const char *identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 984 | argument_list_t *arguments) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 985 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 986 | macro_t *macro; |
Carl Worth | c10a51b | 2010-05-20 15:15:26 -0700 | [diff] [blame] | 987 | token_list_t *expanded; |
| 988 | token_node_t *i, *j; |
| 989 | int parameter_index; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 990 | |
| 991 | macro = hash_table_find (parser->defines, identifier); |
| 992 | assert (macro->is_function); |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 993 | assert (! glcpp_parser_is_expanding (parser, identifier)); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 994 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 995 | if (_argument_list_length (arguments) != |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 996 | _string_list_length (macro->parameters)) |
| 997 | { |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 998 | fprintf (stderr, |
| 999 | "Error: macro %s invoked with %d arguments (expected %d)\n", |
| 1000 | identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 1001 | _argument_list_length (arguments), |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 1002 | _string_list_length (macro->parameters)); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 1003 | return; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 1004 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1005 | |
Carl Worth | c10a51b | 2010-05-20 15:15:26 -0700 | [diff] [blame] | 1006 | expanded = _token_list_create (macro); |
| 1007 | |
| 1008 | for (i = macro->replacements->head; i; i = i->next) { |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 1009 | if (_string_list_contains (macro->parameters, |
| 1010 | i->token->value.str, |
Carl Worth | c10a51b | 2010-05-20 15:15:26 -0700 | [diff] [blame] | 1011 | ¶meter_index)) |
| 1012 | { |
| 1013 | token_list_t *argument; |
| 1014 | argument = _argument_list_member_at (arguments, |
| 1015 | parameter_index); |
| 1016 | for (j = argument->head; j; j = j->next) |
| 1017 | { |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 1018 | _token_list_append (expanded, j->token); |
Carl Worth | c10a51b | 2010-05-20 15:15:26 -0700 | [diff] [blame] | 1019 | } |
| 1020 | } else { |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 1021 | _token_list_append (expanded, i->token); |
Carl Worth | c10a51b | 2010-05-20 15:15:26 -0700 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | _glcpp_parser_push_expansion (parser, macro, expanded->head); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 1026 | } |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 1027 | |
| 1028 | static int |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 1029 | glcpp_parser_lex (glcpp_parser_t *parser) |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 1030 | { |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1031 | expansion_node_t *expansion; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 1032 | token_node_t *replacements; |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1033 | int parameter_index; |
Carl Worth | d8327e5 | 2010-05-20 15:18:54 -0700 | [diff] [blame] | 1034 | const char *token; |
| 1035 | token_class_t class; |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1036 | |
| 1037 | /* Who says C can't do efficient tail recursion? */ |
| 1038 | RECURSE: |
| 1039 | |
| 1040 | expansion = parser->expansions; |
| 1041 | |
| 1042 | if (expansion == NULL) |
| 1043 | return glcpp_lex (parser->scanner); |
| 1044 | |
| 1045 | replacements = expansion->replacements; |
| 1046 | |
| 1047 | /* Pop expansion when replacements is exhausted. */ |
| 1048 | if (replacements == NULL) { |
| 1049 | glcpp_parser_pop_expansion (parser); |
| 1050 | goto RECURSE; |
| 1051 | } |
| 1052 | |
| 1053 | expansion->replacements = replacements->next; |
| 1054 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 1055 | token = replacements->token->value.str; |
Carl Worth | d8327e5 | 2010-05-20 15:18:54 -0700 | [diff] [blame] | 1056 | |
| 1057 | /* Implement token pasting. */ |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 1058 | if (replacements->next && strcmp (replacements->next->token->value.str, "##") == 0) { |
Carl Worth | d8327e5 | 2010-05-20 15:18:54 -0700 | [diff] [blame] | 1059 | token_node_t *next_node; |
| 1060 | |
| 1061 | next_node = replacements->next->next; |
| 1062 | |
| 1063 | if (next_node == NULL) { |
| 1064 | fprintf (stderr, "Error: '##' cannot appear at the end of a macro expansion.\n"); |
| 1065 | exit (1); |
| 1066 | } |
| 1067 | |
| 1068 | token = xtalloc_asprintf (parser, "%s%s", |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 1069 | token, next_node->token->value.str); |
Carl Worth | d8327e5 | 2010-05-20 15:18:54 -0700 | [diff] [blame] | 1070 | expansion->replacements = next_node->next; |
| 1071 | } |
| 1072 | |
| 1073 | |
| 1074 | if (strcmp (token, "(") == 0) |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1075 | return '('; |
Carl Worth | d8327e5 | 2010-05-20 15:18:54 -0700 | [diff] [blame] | 1076 | else if (strcmp (token, ")") == 0) |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1077 | return ')'; |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1078 | |
Carl Worth | d8327e5 | 2010-05-20 15:18:54 -0700 | [diff] [blame] | 1079 | yylval.str = xtalloc_strdup (parser, token); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1080 | |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 1081 | /* Carefully refuse to expand any finalized identifier. */ |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 1082 | if (replacements->token->type == IDENTIFIER_FINALIZED) |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 1083 | return IDENTIFIER_FINALIZED; |
| 1084 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1085 | switch (glcpp_parser_classify_token (parser, yylval.str, |
| 1086 | ¶meter_index)) |
| 1087 | { |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1088 | case TOKEN_CLASS_IDENTIFIER: |
| 1089 | return IDENTIFIER; |
| 1090 | break; |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 1091 | case TOKEN_CLASS_IDENTIFIER_FINALIZED: |
| 1092 | return IDENTIFIER_FINALIZED; |
| 1093 | break; |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1094 | case TOKEN_CLASS_FUNC_MACRO: |
| 1095 | return FUNC_MACRO; |
| 1096 | break; |
| 1097 | default: |
| 1098 | case TOKEN_CLASS_OBJ_MACRO: |
| 1099 | return OBJ_MACRO; |
| 1100 | break; |
| 1101 | } |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 1102 | } |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1103 | |
| 1104 | static void |
| 1105 | _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition) |
| 1106 | { |
| 1107 | skip_type_t current = SKIP_NO_SKIP; |
| 1108 | skip_node_t *node; |
| 1109 | |
| 1110 | if (parser->skip_stack) |
| 1111 | current = parser->skip_stack->type; |
| 1112 | |
| 1113 | node = xtalloc (parser, skip_node_t); |
| 1114 | |
| 1115 | if (current == SKIP_NO_SKIP) { |
| 1116 | if (condition) |
| 1117 | node->type = SKIP_NO_SKIP; |
| 1118 | else |
| 1119 | node->type = SKIP_TO_ELSE; |
| 1120 | } else { |
| 1121 | node->type = SKIP_TO_ENDIF; |
| 1122 | } |
| 1123 | |
| 1124 | node->next = parser->skip_stack; |
| 1125 | parser->skip_stack = node; |
| 1126 | } |
| 1127 | |
| 1128 | static void |
| 1129 | _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type, |
| 1130 | int condition) |
| 1131 | { |
| 1132 | if (parser->skip_stack == NULL) { |
| 1133 | fprintf (stderr, "Error: %s without #if\n", type); |
| 1134 | exit (1); |
| 1135 | } |
| 1136 | |
| 1137 | if (parser->skip_stack->type == SKIP_TO_ELSE) { |
| 1138 | if (condition) |
| 1139 | parser->skip_stack->type = SKIP_NO_SKIP; |
| 1140 | } else { |
| 1141 | parser->skip_stack->type = SKIP_TO_ENDIF; |
| 1142 | } |
| 1143 | } |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 1144 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1145 | static void |
| 1146 | _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser) |
| 1147 | { |
| 1148 | skip_node_t *node; |
| 1149 | |
| 1150 | if (parser->skip_stack == NULL) { |
| 1151 | fprintf (stderr, "Error: #endif without #if\n"); |
| 1152 | exit (1); |
| 1153 | } |
| 1154 | |
| 1155 | node = parser->skip_stack; |
| 1156 | parser->skip_stack = node->next; |
| 1157 | talloc_free (node); |
| 1158 | } |