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