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