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 | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 130 | %union { |
Carl Worth | 005b320 | 2010-05-20 14:19:57 -0700 | [diff] [blame] | 131 | int ival; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 132 | char *str; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 133 | token_t *token; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 134 | token_list_t *token_list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 137 | %parse-param {glcpp_parser_t *parser} |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 138 | %lex-param {glcpp_parser_t *parser} |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 139 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 140 | %token HASH_DEFINE_FUNC HASH_DEFINE_OBJ HASH IDENTIFIER NEWLINE OTHER HASH_UNDEF |
| 141 | %token LEFT_SHIFT RIGHT_SHIFT LESS_OR_EQUAL GREATER_OR_EQUAL EQUAL NOT_EQUAL AND OR PASTE |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 142 | %type <ival> punctuator |
| 143 | %type <str> IDENTIFIER OTHER |
| 144 | %type <token> preprocessing_token |
| 145 | %type <token_list> pp_tokens replacement_list text_line |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 146 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 147 | /* Stale stuff just to allow code to compile. */ |
| 148 | %token IDENTIFIER_FINALIZED FUNC_MACRO OBJ_MACRO |
Carl Worth | 796e1f0 | 2010-05-17 12:45:16 -0700 | [diff] [blame] | 149 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 150 | %% |
| 151 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 152 | input: |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 153 | /* empty */ |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 154 | | input line { |
| 155 | printf ("\n"); |
| 156 | } |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 157 | ; |
| 158 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 159 | line: |
| 160 | control_line |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 161 | | text_line { |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 162 | _glcpp_parser_print_expanded_token_list (parser, $1); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 163 | talloc_free ($1); |
| 164 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 165 | | HASH non_directive |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 166 | ; |
| 167 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 168 | control_line: |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 169 | HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE { |
| 170 | _define_object_macro (parser, $2, $3); |
| 171 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 172 | | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE |
| 173 | | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE |
Carl Worth | e6fb782 | 2010-05-25 15:28:58 -0700 | [diff] [blame^] | 174 | | HASH_UNDEF IDENTIFIER NEWLINE { |
| 175 | string_list_t *macro = hash_table_find (parser->defines, $2); |
| 176 | if (macro) { |
| 177 | /* XXX: Need hash table to support a real way |
| 178 | * to remove an element rather than prefixing |
| 179 | * a new node with data of NULL like this. */ |
| 180 | hash_table_insert (parser->defines, NULL, $2); |
| 181 | talloc_free (macro); |
| 182 | } |
| 183 | talloc_free ($2); |
| 184 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 185 | | HASH NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 186 | ; |
| 187 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 188 | identifier_list: |
| 189 | IDENTIFIER |
| 190 | | identifier_list ',' IDENTIFIER |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 191 | ; |
| 192 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 193 | text_line: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 194 | NEWLINE { $$ = NULL; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 195 | | pp_tokens NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 196 | ; |
| 197 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 198 | non_directive: |
| 199 | pp_tokens NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 200 | ; |
| 201 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 202 | replacement_list: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 203 | /* empty */ { $$ = NULL; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 204 | | pp_tokens |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 205 | ; |
| 206 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 207 | pp_tokens: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 208 | preprocessing_token { |
| 209 | $$ = _token_list_create (parser); |
| 210 | _token_list_append ($$, $1); |
| 211 | talloc_unlink (parser, $1); |
| 212 | } |
| 213 | | pp_tokens preprocessing_token { |
| 214 | $$ = $1; |
| 215 | _token_list_append ($$, $2); |
| 216 | talloc_unlink (parser, $2); |
| 217 | } |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 218 | ; |
| 219 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 220 | preprocessing_token: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 221 | IDENTIFIER { |
| 222 | $$ = _token_create_str (parser, IDENTIFIER, $1); |
| 223 | } |
| 224 | | punctuator { |
| 225 | $$ = _token_create_ival (parser, $1, $1); |
| 226 | } |
| 227 | | OTHER { |
| 228 | $$ = _token_create_str (parser, OTHER, $1); |
| 229 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 230 | ; |
| 231 | |
| 232 | punctuator: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 233 | '[' { $$ = '['; } |
| 234 | | ']' { $$ = ']'; } |
| 235 | | '(' { $$ = '('; } |
| 236 | | ')' { $$ = ')'; } |
| 237 | | '{' { $$ = '{'; } |
| 238 | | '}' { $$ = '}'; } |
| 239 | | '.' { $$ = '.'; } |
| 240 | | '&' { $$ = '&'; } |
| 241 | | '*' { $$ = '*'; } |
| 242 | | '+' { $$ = '+'; } |
| 243 | | '-' { $$ = '-'; } |
| 244 | | '~' { $$ = '~'; } |
| 245 | | '!' { $$ = '!'; } |
| 246 | | '/' { $$ = '/'; } |
| 247 | | '%' { $$ = '%'; } |
| 248 | | LEFT_SHIFT { $$ = LEFT_SHIFT; } |
| 249 | | RIGHT_SHIFT { $$ = RIGHT_SHIFT; } |
| 250 | | '<' { $$ = '<'; } |
| 251 | | '>' { $$ = '>'; } |
| 252 | | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; } |
| 253 | | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; } |
| 254 | | EQUAL { $$ = EQUAL; } |
| 255 | | NOT_EQUAL { $$ = NOT_EQUAL; } |
| 256 | | '^' { $$ = '^'; } |
| 257 | | '|' { $$ = '|'; } |
| 258 | | AND { $$ = AND; } |
| 259 | | OR { $$ = OR; } |
| 260 | | ';' { $$ = ';'; } |
| 261 | | ',' { $$ = ','; } |
| 262 | | PASTE { $$ = PASTE; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 263 | ; |
| 264 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 265 | %% |
| 266 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 267 | string_list_t * |
| 268 | _string_list_create (void *ctx) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 269 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 270 | string_list_t *list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 271 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 272 | list = xtalloc (ctx, string_list_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 273 | list->head = NULL; |
| 274 | list->tail = NULL; |
| 275 | |
| 276 | return list; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 277 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 278 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 279 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 280 | _string_list_append_list (string_list_t *list, string_list_t *tail) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 281 | { |
| 282 | if (list->head == NULL) { |
| 283 | list->head = tail->head; |
| 284 | } else { |
| 285 | list->tail->next = tail->head; |
| 286 | } |
| 287 | |
| 288 | list->tail = tail->tail; |
| 289 | } |
| 290 | |
| 291 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 292 | _string_list_append_item (string_list_t *list, const char *str) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 293 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 294 | string_node_t *node; |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 295 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 296 | node = xtalloc (list, string_node_t); |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 297 | node->str = xtalloc_strdup (node, str); |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 298 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 299 | node->next = NULL; |
| 300 | |
| 301 | if (list->head == NULL) { |
| 302 | list->head = node; |
| 303 | } else { |
| 304 | list->tail->next = node; |
| 305 | } |
| 306 | |
| 307 | list->tail = node; |
| 308 | } |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 309 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 310 | void |
| 311 | _string_list_push (string_list_t *list, const char *str) |
| 312 | { |
| 313 | string_node_t *node; |
| 314 | |
| 315 | node = xtalloc (list, string_node_t); |
| 316 | node->str = xtalloc_strdup (node, str); |
| 317 | node->next = list->head; |
| 318 | |
| 319 | if (list->tail == NULL) { |
| 320 | list->tail = node; |
| 321 | } |
| 322 | list->head = node; |
| 323 | } |
| 324 | |
| 325 | void |
| 326 | _string_list_pop (string_list_t *list) |
| 327 | { |
| 328 | string_node_t *node; |
| 329 | |
| 330 | node = list->head; |
| 331 | |
| 332 | if (node == NULL) { |
| 333 | fprintf (stderr, "Internal error: _string_list_pop called on an empty list.\n"); |
| 334 | exit (1); |
| 335 | } |
| 336 | |
| 337 | list->head = node->next; |
| 338 | if (list->tail == node) { |
| 339 | assert (node->next == NULL); |
| 340 | list->tail = NULL; |
| 341 | } |
| 342 | |
| 343 | talloc_free (node); |
| 344 | } |
| 345 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 346 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 347 | _string_list_contains (string_list_t *list, const char *member, int *index) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 348 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 349 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 350 | int i; |
| 351 | |
| 352 | if (list == NULL) |
| 353 | return 0; |
| 354 | |
| 355 | for (i = 0, node = list->head; node; i++, node = node->next) { |
| 356 | if (strcmp (node->str, member) == 0) { |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 357 | if (index) |
| 358 | *index = i; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 359 | return 1; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 367 | _string_list_length (string_list_t *list) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 368 | { |
| 369 | int length = 0; |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 370 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 371 | |
| 372 | if (list == NULL) |
| 373 | return 0; |
| 374 | |
| 375 | for (node = list->head; node; node = node->next) |
| 376 | length++; |
| 377 | |
| 378 | return length; |
| 379 | } |
| 380 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 381 | argument_list_t * |
| 382 | _argument_list_create (void *ctx) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 383 | { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 384 | argument_list_t *list; |
| 385 | |
| 386 | list = xtalloc (ctx, argument_list_t); |
| 387 | list->head = NULL; |
| 388 | list->tail = NULL; |
| 389 | |
| 390 | return list; |
| 391 | } |
| 392 | |
| 393 | void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 394 | _argument_list_append (argument_list_t *list, token_list_t *argument) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 395 | { |
| 396 | argument_node_t *node; |
| 397 | |
| 398 | if (argument == NULL || argument->head == NULL) |
| 399 | return; |
| 400 | |
| 401 | node = xtalloc (list, argument_node_t); |
| 402 | node->argument = argument; |
| 403 | |
| 404 | node->next = NULL; |
| 405 | |
| 406 | if (list->head == NULL) { |
| 407 | list->head = node; |
| 408 | } else { |
| 409 | list->tail->next = node; |
| 410 | } |
| 411 | |
| 412 | list->tail = node; |
| 413 | } |
| 414 | |
| 415 | int |
| 416 | _argument_list_length (argument_list_t *list) |
| 417 | { |
| 418 | int length = 0; |
| 419 | argument_node_t *node; |
| 420 | |
| 421 | if (list == NULL) |
| 422 | return 0; |
| 423 | |
| 424 | for (node = list->head; node; node = node->next) |
| 425 | length++; |
| 426 | |
| 427 | return length; |
| 428 | } |
| 429 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 430 | token_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 431 | _argument_list_member_at (argument_list_t *list, int index) |
| 432 | { |
| 433 | argument_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 434 | int i; |
| 435 | |
| 436 | if (list == NULL) |
| 437 | return NULL; |
| 438 | |
| 439 | node = list->head; |
| 440 | for (i = 0; i < index; i++) { |
| 441 | node = node->next; |
| 442 | if (node == NULL) |
| 443 | break; |
| 444 | } |
| 445 | |
| 446 | if (node) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 447 | return node->argument; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 448 | |
| 449 | return NULL; |
| 450 | } |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 451 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 452 | /* Note: This function talloc_steal()s the str pointer. */ |
| 453 | token_t * |
| 454 | _token_create_str (void *ctx, int type, char *str) |
| 455 | { |
| 456 | token_t *token; |
| 457 | |
| 458 | token = xtalloc (ctx, token_t); |
| 459 | token->type = type; |
| 460 | token->value.str = talloc_steal (token, str); |
| 461 | |
| 462 | return token; |
| 463 | } |
| 464 | |
| 465 | token_t * |
| 466 | _token_create_ival (void *ctx, int type, int ival) |
| 467 | { |
| 468 | token_t *token; |
| 469 | |
| 470 | token = xtalloc (ctx, token_t); |
| 471 | token->type = type; |
| 472 | token->value.ival = ival; |
| 473 | |
| 474 | return token; |
| 475 | } |
| 476 | |
| 477 | void |
| 478 | _token_print (token_t *token) |
| 479 | { |
| 480 | if (token->type < 256) { |
| 481 | printf ("%c", token->type); |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | switch (token->type) { |
| 486 | case IDENTIFIER: |
| 487 | case OTHER: |
| 488 | printf ("%s", token->value.str); |
| 489 | break; |
| 490 | case LEFT_SHIFT: |
| 491 | printf ("<<"); |
| 492 | break; |
| 493 | case RIGHT_SHIFT: |
| 494 | printf (">>"); |
| 495 | break; |
| 496 | case LESS_OR_EQUAL: |
| 497 | printf ("<="); |
| 498 | break; |
| 499 | case GREATER_OR_EQUAL: |
| 500 | printf (">="); |
| 501 | break; |
| 502 | case EQUAL: |
| 503 | printf ("=="); |
| 504 | break; |
| 505 | case NOT_EQUAL: |
| 506 | printf ("!="); |
| 507 | break; |
| 508 | case AND: |
| 509 | printf ("&&"); |
| 510 | break; |
| 511 | case OR: |
| 512 | printf ("||"); |
| 513 | break; |
| 514 | case PASTE: |
| 515 | printf ("##"); |
| 516 | break; |
| 517 | default: |
| 518 | fprintf (stderr, "Error: Don't know how to print token type %d\n", token->type); |
| 519 | break; |
| 520 | } |
| 521 | } |
| 522 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 523 | token_list_t * |
| 524 | _token_list_create (void *ctx) |
| 525 | { |
| 526 | token_list_t *list; |
| 527 | |
| 528 | list = xtalloc (ctx, token_list_t); |
| 529 | list->head = NULL; |
| 530 | list->tail = NULL; |
| 531 | |
| 532 | return list; |
| 533 | } |
| 534 | |
| 535 | void |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 536 | _token_list_append (token_list_t *list, token_t *token) |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 537 | { |
| 538 | token_node_t *node; |
| 539 | |
| 540 | node = xtalloc (list, token_node_t); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 541 | node->token = xtalloc_reference (list, token); |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 542 | |
| 543 | node->next = NULL; |
| 544 | |
| 545 | if (list->head == NULL) { |
| 546 | list->head = node; |
| 547 | } else { |
| 548 | list->tail->next = node; |
| 549 | } |
| 550 | |
| 551 | list->tail = node; |
| 552 | } |
| 553 | |
| 554 | void |
| 555 | _token_list_append_list (token_list_t *list, token_list_t *tail) |
| 556 | { |
| 557 | if (list->head == NULL) { |
| 558 | list->head = tail->head; |
| 559 | } else { |
| 560 | list->tail->next = tail->head; |
| 561 | } |
| 562 | |
| 563 | list->tail = tail->tail; |
| 564 | } |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 565 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 566 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 567 | yyerror (void *scanner, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 568 | { |
| 569 | fprintf (stderr, "Parse error: %s\n", error); |
| 570 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 571 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 572 | glcpp_parser_t * |
| 573 | glcpp_parser_create (void) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 574 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 575 | glcpp_parser_t *parser; |
| 576 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 577 | parser = xtalloc (NULL, glcpp_parser_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 578 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 579 | glcpp_lex_init_extra (parser, &parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 580 | parser->defines = hash_table_ctor (32, hash_table_string_hash, |
| 581 | hash_table_string_compare); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 582 | parser->active = _string_list_create (parser); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 583 | parser->expansions = NULL; |
| 584 | |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame] | 585 | parser->just_printed_separator = 1; |
Carl Worth | 876e510 | 2010-05-20 14:38:06 -0700 | [diff] [blame] | 586 | parser->need_newline = 0; |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame] | 587 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 588 | parser->skip_stack = NULL; |
| 589 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 590 | return parser; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | int |
| 594 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 595 | { |
| 596 | return yyparse (parser); |
| 597 | } |
| 598 | |
| 599 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 600 | glcpp_parser_destroy (glcpp_parser_t *parser) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 601 | { |
Carl Worth | 876e510 | 2010-05-20 14:38:06 -0700 | [diff] [blame] | 602 | if (parser->need_newline) |
| 603 | printf ("\n"); |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 604 | if (parser->skip_stack) |
| 605 | fprintf (stderr, "Error: Unterminated #if\n"); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 606 | glcpp_lex_destroy (parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 607 | hash_table_dtor (parser->defines); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 608 | talloc_free (parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 609 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 610 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 611 | static int |
| 612 | glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member) |
| 613 | { |
| 614 | expansion_node_t *node; |
| 615 | |
| 616 | for (node = parser->expansions; node; node = node->next) { |
| 617 | if (node->macro && |
| 618 | strcmp (node->macro->identifier, member) == 0) |
| 619 | { |
| 620 | return 1; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 627 | token_class_t |
| 628 | glcpp_parser_classify_token (glcpp_parser_t *parser, |
| 629 | const char *identifier, |
| 630 | int *parameter_index) |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 631 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 632 | macro_t *macro; |
| 633 | |
Carl Worth | c10a51b | 2010-05-20 15:15:26 -0700 | [diff] [blame] | 634 | /* Is this token a defined macro? */ |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 635 | macro = hash_table_find (parser->defines, identifier); |
| 636 | |
| 637 | if (macro == NULL) |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 638 | return TOKEN_CLASS_IDENTIFIER; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 639 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 640 | /* Don't consider this a macro if we are already actively |
| 641 | * expanding this macro. */ |
| 642 | if (glcpp_parser_is_expanding (parser, identifier)) |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 643 | return TOKEN_CLASS_IDENTIFIER_FINALIZED; |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 644 | |
| 645 | /* Definitely a macro. Just need to check if it's function-like. */ |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 646 | if (macro->is_function) |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 647 | return TOKEN_CLASS_FUNC_MACRO; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 648 | else |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 649 | return TOKEN_CLASS_OBJ_MACRO; |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 652 | void |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 653 | _glcpp_parser_print_expanded_token (glcpp_parser_t *parser, |
| 654 | token_t *token) |
| 655 | { |
| 656 | const char *identifier; |
| 657 | macro_t *macro; |
| 658 | |
| 659 | /* We only expand identifiers */ |
| 660 | if (token->type != IDENTIFIER) { |
| 661 | _token_print (token); |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | /* Look up this identifier in the hash table. */ |
| 666 | identifier = token->value.str; |
| 667 | macro = hash_table_find (parser->defines, identifier); |
| 668 | |
| 669 | /* Not a macro, so just print directly. */ |
| 670 | if (macro == NULL) { |
| 671 | printf ("%s", identifier); |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | /* We're not (yet) supporting function-like macros. */ |
| 676 | if (macro->is_function) { |
| 677 | printf ("%s", identifier); |
| 678 | return; |
| 679 | } |
| 680 | |
| 681 | /* Finally, don't expand this macro if we're already actively |
| 682 | * expanding it, (to avoid infinite recursion). */ |
| 683 | if (_string_list_contains (parser->active, identifier, NULL)) { |
| 684 | printf ("%s", identifier); |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | _string_list_push (parser->active, identifier); |
| 689 | _glcpp_parser_print_expanded_token_list (parser, |
| 690 | macro->replacements); |
| 691 | _string_list_pop (parser->active); |
| 692 | } |
| 693 | |
| 694 | void |
| 695 | _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser, |
| 696 | token_list_t *list) |
| 697 | { |
| 698 | token_node_t *node; |
| 699 | |
| 700 | if (list == NULL) |
| 701 | return; |
| 702 | |
| 703 | for (node = list->head; node; node = node->next) { |
| 704 | _glcpp_parser_print_expanded_token (parser, node->token); |
| 705 | if (node->next) |
| 706 | printf (" "); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 711 | _define_object_macro (glcpp_parser_t *parser, |
| 712 | const char *identifier, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 713 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 714 | { |
| 715 | macro_t *macro; |
| 716 | |
| 717 | macro = xtalloc (parser, macro_t); |
| 718 | |
| 719 | macro->is_function = 0; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 720 | macro->parameters = NULL; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 721 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 722 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 723 | |
| 724 | hash_table_insert (parser->defines, macro, identifier); |
| 725 | } |
| 726 | |
| 727 | void |
| 728 | _define_function_macro (glcpp_parser_t *parser, |
| 729 | const char *identifier, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 730 | string_list_t *parameters, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 731 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 732 | { |
| 733 | macro_t *macro; |
| 734 | |
| 735 | macro = xtalloc (parser, macro_t); |
| 736 | |
| 737 | macro->is_function = 1; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 738 | macro->parameters = talloc_steal (macro, parameters); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 739 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 740 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 741 | |
| 742 | hash_table_insert (parser->defines, macro, identifier); |
| 743 | } |
| 744 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 745 | static void |
Carl Worth | c10a51b | 2010-05-20 15:15:26 -0700 | [diff] [blame] | 746 | _glcpp_parser_push_expansion (glcpp_parser_t *parser, |
| 747 | macro_t *macro, |
| 748 | token_node_t *replacements) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 749 | { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 750 | expansion_node_t *node; |
| 751 | |
| 752 | node = xtalloc (parser, expansion_node_t); |
| 753 | |
| 754 | node->macro = macro; |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 755 | node->replacements = replacements; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 756 | |
| 757 | node->next = parser->expansions; |
| 758 | parser->expansions = node; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 761 | static void |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 762 | glcpp_parser_pop_expansion (glcpp_parser_t *parser) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 763 | { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 764 | expansion_node_t *node; |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 765 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 766 | node = parser->expansions; |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 767 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 768 | if (node == NULL) { |
| 769 | fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n"); |
| 770 | exit (1); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 773 | parser->expansions = node->next; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 774 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 775 | talloc_free (node); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 778 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 779 | _expand_object_macro (glcpp_parser_t *parser, const char *identifier) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 780 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 781 | macro_t *macro; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 782 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 783 | macro = hash_table_find (parser->defines, identifier); |
| 784 | assert (! macro->is_function); |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 785 | assert (! glcpp_parser_is_expanding (parser, identifier)); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 786 | |
Carl Worth | c10a51b | 2010-05-20 15:15:26 -0700 | [diff] [blame] | 787 | _glcpp_parser_push_expansion (parser, macro, macro->replacements->head); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 788 | } |
| 789 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 790 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 791 | _expand_function_macro (glcpp_parser_t *parser, |
| 792 | const char *identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 793 | argument_list_t *arguments) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 794 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 795 | macro_t *macro; |
Carl Worth | c10a51b | 2010-05-20 15:15:26 -0700 | [diff] [blame] | 796 | token_list_t *expanded; |
| 797 | token_node_t *i, *j; |
| 798 | int parameter_index; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 799 | |
| 800 | macro = hash_table_find (parser->defines, identifier); |
| 801 | assert (macro->is_function); |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 802 | assert (! glcpp_parser_is_expanding (parser, identifier)); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 803 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 804 | if (_argument_list_length (arguments) != |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 805 | _string_list_length (macro->parameters)) |
| 806 | { |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 807 | fprintf (stderr, |
| 808 | "Error: macro %s invoked with %d arguments (expected %d)\n", |
| 809 | identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 810 | _argument_list_length (arguments), |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 811 | _string_list_length (macro->parameters)); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 812 | return; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 813 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 814 | |
Carl Worth | c10a51b | 2010-05-20 15:15:26 -0700 | [diff] [blame] | 815 | expanded = _token_list_create (macro); |
| 816 | |
| 817 | for (i = macro->replacements->head; i; i = i->next) { |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 818 | if (_string_list_contains (macro->parameters, |
| 819 | i->token->value.str, |
Carl Worth | c10a51b | 2010-05-20 15:15:26 -0700 | [diff] [blame] | 820 | ¶meter_index)) |
| 821 | { |
| 822 | token_list_t *argument; |
| 823 | argument = _argument_list_member_at (arguments, |
| 824 | parameter_index); |
| 825 | for (j = argument->head; j; j = j->next) |
| 826 | { |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 827 | _token_list_append (expanded, j->token); |
Carl Worth | c10a51b | 2010-05-20 15:15:26 -0700 | [diff] [blame] | 828 | } |
| 829 | } else { |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 830 | _token_list_append (expanded, i->token); |
Carl Worth | c10a51b | 2010-05-20 15:15:26 -0700 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | |
| 834 | _glcpp_parser_push_expansion (parser, macro, expanded->head); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 835 | } |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 836 | |
| 837 | static int |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 838 | glcpp_parser_lex (glcpp_parser_t *parser) |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 839 | { |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 840 | expansion_node_t *expansion; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 841 | token_node_t *replacements; |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 842 | int parameter_index; |
Carl Worth | d8327e5 | 2010-05-20 15:18:54 -0700 | [diff] [blame] | 843 | const char *token; |
| 844 | token_class_t class; |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 845 | |
| 846 | /* Who says C can't do efficient tail recursion? */ |
| 847 | RECURSE: |
| 848 | |
| 849 | expansion = parser->expansions; |
| 850 | |
| 851 | if (expansion == NULL) |
| 852 | return glcpp_lex (parser->scanner); |
| 853 | |
| 854 | replacements = expansion->replacements; |
| 855 | |
| 856 | /* Pop expansion when replacements is exhausted. */ |
| 857 | if (replacements == NULL) { |
| 858 | glcpp_parser_pop_expansion (parser); |
| 859 | goto RECURSE; |
| 860 | } |
| 861 | |
| 862 | expansion->replacements = replacements->next; |
| 863 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 864 | token = replacements->token->value.str; |
Carl Worth | d8327e5 | 2010-05-20 15:18:54 -0700 | [diff] [blame] | 865 | |
| 866 | /* Implement token pasting. */ |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 867 | if (replacements->next && strcmp (replacements->next->token->value.str, "##") == 0) { |
Carl Worth | d8327e5 | 2010-05-20 15:18:54 -0700 | [diff] [blame] | 868 | token_node_t *next_node; |
| 869 | |
| 870 | next_node = replacements->next->next; |
| 871 | |
| 872 | if (next_node == NULL) { |
| 873 | fprintf (stderr, "Error: '##' cannot appear at the end of a macro expansion.\n"); |
| 874 | exit (1); |
| 875 | } |
| 876 | |
| 877 | token = xtalloc_asprintf (parser, "%s%s", |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 878 | token, next_node->token->value.str); |
Carl Worth | d8327e5 | 2010-05-20 15:18:54 -0700 | [diff] [blame] | 879 | expansion->replacements = next_node->next; |
| 880 | } |
| 881 | |
| 882 | |
| 883 | if (strcmp (token, "(") == 0) |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 884 | return '('; |
Carl Worth | d8327e5 | 2010-05-20 15:18:54 -0700 | [diff] [blame] | 885 | else if (strcmp (token, ")") == 0) |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 886 | return ')'; |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 887 | |
Carl Worth | d8327e5 | 2010-05-20 15:18:54 -0700 | [diff] [blame] | 888 | yylval.str = xtalloc_strdup (parser, token); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 889 | |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 890 | /* Carefully refuse to expand any finalized identifier. */ |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 891 | if (replacements->token->type == IDENTIFIER_FINALIZED) |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 892 | return IDENTIFIER_FINALIZED; |
| 893 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 894 | switch (glcpp_parser_classify_token (parser, yylval.str, |
| 895 | ¶meter_index)) |
| 896 | { |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 897 | case TOKEN_CLASS_IDENTIFIER: |
| 898 | return IDENTIFIER; |
| 899 | break; |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 900 | case TOKEN_CLASS_IDENTIFIER_FINALIZED: |
| 901 | return IDENTIFIER_FINALIZED; |
| 902 | break; |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 903 | case TOKEN_CLASS_FUNC_MACRO: |
| 904 | return FUNC_MACRO; |
| 905 | break; |
| 906 | default: |
| 907 | case TOKEN_CLASS_OBJ_MACRO: |
| 908 | return OBJ_MACRO; |
| 909 | break; |
| 910 | } |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 911 | } |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 912 | |
| 913 | static void |
| 914 | _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition) |
| 915 | { |
| 916 | skip_type_t current = SKIP_NO_SKIP; |
| 917 | skip_node_t *node; |
| 918 | |
| 919 | if (parser->skip_stack) |
| 920 | current = parser->skip_stack->type; |
| 921 | |
| 922 | node = xtalloc (parser, skip_node_t); |
| 923 | |
| 924 | if (current == SKIP_NO_SKIP) { |
| 925 | if (condition) |
| 926 | node->type = SKIP_NO_SKIP; |
| 927 | else |
| 928 | node->type = SKIP_TO_ELSE; |
| 929 | } else { |
| 930 | node->type = SKIP_TO_ENDIF; |
| 931 | } |
| 932 | |
| 933 | node->next = parser->skip_stack; |
| 934 | parser->skip_stack = node; |
| 935 | } |
| 936 | |
| 937 | static void |
| 938 | _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type, |
| 939 | int condition) |
| 940 | { |
| 941 | if (parser->skip_stack == NULL) { |
| 942 | fprintf (stderr, "Error: %s without #if\n", type); |
| 943 | exit (1); |
| 944 | } |
| 945 | |
| 946 | if (parser->skip_stack->type == SKIP_TO_ELSE) { |
| 947 | if (condition) |
| 948 | parser->skip_stack->type = SKIP_NO_SKIP; |
| 949 | } else { |
| 950 | parser->skip_stack->type = SKIP_TO_ENDIF; |
| 951 | } |
| 952 | } |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 953 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 954 | static void |
| 955 | _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser) |
| 956 | { |
| 957 | skip_node_t *node; |
| 958 | |
| 959 | if (parser->skip_stack == NULL) { |
| 960 | fprintf (stderr, "Error: #endif without #if\n"); |
| 961 | exit (1); |
| 962 | } |
| 963 | |
| 964 | node = parser->skip_stack; |
| 965 | parser->skip_stack = node->next; |
| 966 | talloc_free (node); |
| 967 | } |