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 | |
| 589 | static void |
| 590 | _token_list_print (token_list_t *list) |
| 591 | { |
| 592 | token_node_t *node; |
| 593 | |
| 594 | if (list == NULL) |
| 595 | return; |
| 596 | |
| 597 | for (node = list->head; node; node = node->next) |
| 598 | _token_print (node->token); |
| 599 | } |
| 600 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 601 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 602 | yyerror (void *scanner, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 603 | { |
| 604 | fprintf (stderr, "Parse error: %s\n", error); |
| 605 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 606 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 607 | glcpp_parser_t * |
| 608 | glcpp_parser_create (void) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 609 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 610 | glcpp_parser_t *parser; |
| 611 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 612 | parser = xtalloc (NULL, glcpp_parser_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 613 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 614 | glcpp_lex_init_extra (parser, &parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 615 | parser->defines = hash_table_ctor (32, hash_table_string_hash, |
| 616 | hash_table_string_compare); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 617 | parser->active = _string_list_create (parser); |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 618 | parser->space_tokens = 1; |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame] | 619 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 620 | parser->skip_stack = NULL; |
| 621 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 622 | return parser; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | int |
| 626 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 627 | { |
| 628 | return yyparse (parser); |
| 629 | } |
| 630 | |
| 631 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 632 | glcpp_parser_destroy (glcpp_parser_t *parser) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 633 | { |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 634 | if (parser->skip_stack) |
| 635 | fprintf (stderr, "Error: Unterminated #if\n"); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 636 | glcpp_lex_destroy (parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 637 | hash_table_dtor (parser->defines); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 638 | talloc_free (parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 639 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 640 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 641 | /* Appends onto 'expansion' a non-macro token or the expansion of an |
| 642 | * object-like macro. |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 643 | * |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 644 | * Returns 0 if this token is completely processed. |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 645 | * |
| 646 | * Returns 1 in the case that 'token' is a function-like macro that |
| 647 | * needs further expansion. |
| 648 | */ |
| 649 | static int |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 650 | _glcpp_parser_expand_token_onto (glcpp_parser_t *parser, |
| 651 | token_t *token, |
| 652 | token_list_t *result) |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 653 | { |
| 654 | const char *identifier; |
| 655 | macro_t *macro; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 656 | token_list_t *expansion; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 657 | |
| 658 | /* We only expand identifiers */ |
| 659 | if (token->type != IDENTIFIER) { |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 660 | _token_list_append (result, token); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 661 | return 0; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* Look up this identifier in the hash table. */ |
| 665 | identifier = token->value.str; |
| 666 | macro = hash_table_find (parser->defines, identifier); |
| 667 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 668 | /* Not a macro, so just append. */ |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 669 | if (macro == NULL) { |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 670 | _token_list_append (result, token); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 671 | return 0; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 674 | /* Finally, don't expand this macro if we're already actively |
| 675 | * expanding it, (to avoid infinite recursion). */ |
Carl Worth | ec4ada0 | 2010-05-26 08:15:49 -0700 | [diff] [blame] | 676 | if (_string_list_contains (parser->active, identifier, NULL)) |
| 677 | { |
| 678 | /* We change the token type here from IDENTIFIER to |
| 679 | * OTHER to prevent any future expansion of this |
| 680 | * unexpanded token. */ |
| 681 | char *str; |
| 682 | token_t *new_token; |
| 683 | |
| 684 | str = xtalloc_strdup (result, token->value.str); |
| 685 | new_token = _token_create_str (result, OTHER, str); |
| 686 | _token_list_append (result, new_token); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 687 | return 0; |
| 688 | } |
| 689 | |
Carl Worth | c0607d5 | 2010-05-26 08:01:42 -0700 | [diff] [blame] | 690 | /* For function-like macros return 1 for further processing. */ |
| 691 | if (macro->is_function) { |
| 692 | return 1; |
| 693 | } |
| 694 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 695 | _string_list_push (parser->active, identifier); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 696 | _glcpp_parser_expand_token_list_onto (parser, |
| 697 | macro->replacements, |
| 698 | result); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 699 | _string_list_pop (parser->active); |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | typedef enum function_status |
| 705 | { |
| 706 | FUNCTION_STATUS_SUCCESS, |
| 707 | FUNCTION_NOT_A_FUNCTION, |
| 708 | FUNCTION_UNBALANCED_PARENTHESES |
| 709 | } function_status_t; |
| 710 | |
| 711 | /* Find a set of function-like macro arguments by looking for a |
| 712 | * balanced set of parentheses. Upon return *node will be the last |
| 713 | * consumed node, such that further processing can continue with |
| 714 | * node->next. |
| 715 | * |
| 716 | * Return values: |
| 717 | * |
| 718 | * FUNCTION_STATUS_SUCCESS: |
| 719 | * |
| 720 | * Successfully parsed a set of function arguments. |
| 721 | * |
| 722 | * FUNCTION_NOT_A_FUNCTION: |
| 723 | * |
| 724 | * Macro name not followed by a '('. This is not an error, but |
| 725 | * simply that the macro name should be treated as a non-macro. |
| 726 | * |
| 727 | * FUNCTION_UNBLANCED_PARENTHESES |
| 728 | * |
| 729 | * Macro name is not followed by a balanced set of parentheses. |
| 730 | */ |
| 731 | static function_status_t |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 732 | _arguments_parse (argument_list_t *arguments, token_node_t **node_ret) |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 733 | { |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 734 | token_list_t *argument; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 735 | token_node_t *node = *node_ret, *last; |
| 736 | int paren_count; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 737 | |
| 738 | last = node; |
| 739 | node = node->next; |
| 740 | |
| 741 | /* Ignore whitespace before first parenthesis. */ |
| 742 | while (node && node->token->type == SPACE) |
| 743 | node = node->next; |
| 744 | |
| 745 | if (node == NULL || node->token->type != '(') |
| 746 | return FUNCTION_NOT_A_FUNCTION; |
| 747 | |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame] | 748 | last = node; |
| 749 | node = node->next; |
| 750 | |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 751 | argument = NULL; |
| 752 | |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame] | 753 | for (paren_count = 1; node; last = node, node = node->next) { |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 754 | if (node->token->type == '(') |
| 755 | { |
| 756 | paren_count++; |
| 757 | } |
| 758 | else if (node->token->type == ')') |
| 759 | { |
| 760 | paren_count--; |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 761 | if (paren_count == 0) { |
| 762 | last = node; |
| 763 | node = node->next; |
| 764 | break; |
| 765 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 766 | } |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame] | 767 | |
| 768 | if (node->token->type == ',' && |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 769 | paren_count == 1) |
| 770 | { |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 771 | if (argument) |
| 772 | _token_list_trim_trailing_space (argument); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 773 | argument = NULL; |
| 774 | } |
| 775 | else { |
| 776 | if (argument == NULL) { |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 777 | /* Don't treat initial whitespace as |
| 778 | * part of the arguement. */ |
| 779 | if (node->token->type == SPACE) |
| 780 | continue; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 781 | argument = _token_list_create (arguments); |
| 782 | _argument_list_append (arguments, argument); |
| 783 | } |
| 784 | _token_list_append (argument, node->token); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 785 | } |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 786 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 787 | |
| 788 | if (node && paren_count) |
| 789 | return FUNCTION_UNBALANCED_PARENTHESES; |
| 790 | |
| 791 | *node_ret = last; |
| 792 | |
| 793 | return FUNCTION_STATUS_SUCCESS; |
| 794 | } |
| 795 | |
| 796 | /* Prints the expansion of *node (consuming further tokens from the |
| 797 | * list as necessary). Upon return *node will be the last consumed |
| 798 | * node, such that further processing can continue with node->next. */ |
| 799 | static void |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 800 | _glcpp_parser_expand_function_onto (glcpp_parser_t *parser, |
| 801 | token_node_t **node_ret, |
| 802 | token_list_t *result) |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 803 | { |
| 804 | macro_t *macro; |
| 805 | token_node_t *node; |
| 806 | const char *identifier; |
| 807 | argument_list_t *arguments; |
| 808 | function_status_t status; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 809 | token_list_t *substituted; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 810 | int parameter_index; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 811 | |
| 812 | node = *node_ret; |
| 813 | identifier = node->token->value.str; |
| 814 | |
| 815 | macro = hash_table_find (parser->defines, identifier); |
| 816 | |
| 817 | assert (macro->is_function); |
| 818 | |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 819 | arguments = _argument_list_create (parser); |
| 820 | status = _arguments_parse (arguments, node_ret); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 821 | |
| 822 | switch (status) { |
| 823 | case FUNCTION_STATUS_SUCCESS: |
| 824 | break; |
| 825 | case FUNCTION_NOT_A_FUNCTION: |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 826 | _token_list_append (result, node->token); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 827 | return; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 828 | case FUNCTION_UNBALANCED_PARENTHESES: |
| 829 | fprintf (stderr, "Error: Macro %s call has unbalanced parentheses\n", |
| 830 | identifier); |
| 831 | exit (1); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 834 | if (macro->replacements == NULL) { |
| 835 | talloc_free (arguments); |
| 836 | return; |
| 837 | } |
| 838 | |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 839 | if (_argument_list_length (arguments) != |
| 840 | _string_list_length (macro->parameters)) |
| 841 | { |
| 842 | fprintf (stderr, |
| 843 | "Error: macro %s invoked with %d arguments (expected %d)\n", |
| 844 | identifier, |
| 845 | _argument_list_length (arguments), |
| 846 | _string_list_length (macro->parameters)); |
| 847 | return; |
| 848 | } |
| 849 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 850 | /* Perform argument substitution on the replacement list. */ |
| 851 | substituted = _token_list_create (arguments); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 852 | |
Carl Worth | ce540f2 | 2010-05-26 08:25:44 -0700 | [diff] [blame^] | 853 | for (node = macro->replacements->head; node; node = node->next) |
| 854 | { |
| 855 | if (node->token->type == IDENTIFIER && |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 856 | _string_list_contains (macro->parameters, |
Carl Worth | ce540f2 | 2010-05-26 08:25:44 -0700 | [diff] [blame^] | 857 | node->token->value.str, |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 858 | ¶meter_index)) |
| 859 | { |
| 860 | token_list_t *argument; |
| 861 | argument = _argument_list_member_at (arguments, |
| 862 | parameter_index); |
Carl Worth | d5cd403 | 2010-05-26 08:09:29 -0700 | [diff] [blame] | 863 | /* Before substituting, we expand the argument |
| 864 | * tokens. */ |
| 865 | _glcpp_parser_expand_token_list_onto (parser, |
| 866 | argument, |
| 867 | substituted); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 868 | } else { |
Carl Worth | ce540f2 | 2010-05-26 08:25:44 -0700 | [diff] [blame^] | 869 | _token_list_append (substituted, node->token); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 870 | } |
| 871 | } |
| 872 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 873 | _string_list_push (parser->active, identifier); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 874 | _glcpp_parser_expand_token_list_onto (parser, substituted, result); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 875 | _string_list_pop (parser->active); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 876 | |
| 877 | talloc_free (arguments); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 880 | static void |
| 881 | _glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser, |
| 882 | token_list_t *list, |
| 883 | token_list_t *result) |
| 884 | { |
| 885 | token_node_t *node; |
| 886 | |
| 887 | if (list == NULL) |
| 888 | return; |
| 889 | |
| 890 | for (node = list->head; node; node = node->next) |
| 891 | { |
| 892 | if (_glcpp_parser_expand_token_onto (parser, node->token, |
| 893 | result)) |
| 894 | { |
| 895 | _glcpp_parser_expand_function_onto (parser, &node, |
| 896 | result); |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 901 | void |
| 902 | _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser, |
| 903 | token_list_t *list) |
| 904 | { |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 905 | token_list_t *expanded; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 906 | token_node_t *node; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 907 | function_status_t function_status; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 908 | |
| 909 | if (list == NULL) |
| 910 | return; |
| 911 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 912 | expanded = _token_list_create (parser); |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 913 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 914 | _glcpp_parser_expand_token_list_onto (parser, list, expanded); |
| 915 | |
| 916 | _token_list_trim_trailing_space (expanded); |
| 917 | |
| 918 | _token_list_print (expanded); |
| 919 | |
| 920 | talloc_free (expanded); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 924 | _define_object_macro (glcpp_parser_t *parser, |
| 925 | const char *identifier, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 926 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 927 | { |
| 928 | macro_t *macro; |
| 929 | |
| 930 | macro = xtalloc (parser, macro_t); |
| 931 | |
| 932 | macro->is_function = 0; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 933 | macro->parameters = NULL; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 934 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 935 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 936 | |
| 937 | hash_table_insert (parser->defines, macro, identifier); |
| 938 | } |
| 939 | |
| 940 | void |
| 941 | _define_function_macro (glcpp_parser_t *parser, |
| 942 | const char *identifier, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 943 | string_list_t *parameters, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 944 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 945 | { |
| 946 | macro_t *macro; |
| 947 | |
| 948 | macro = xtalloc (parser, macro_t); |
| 949 | |
| 950 | macro->is_function = 1; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 951 | macro->parameters = talloc_steal (macro, parameters); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 952 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 953 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 954 | |
| 955 | hash_table_insert (parser->defines, macro, identifier); |
| 956 | } |
| 957 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 958 | static int |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 959 | glcpp_parser_lex (glcpp_parser_t *parser) |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 960 | { |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 961 | return glcpp_lex (parser->scanner); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 962 | } |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 963 | |
| 964 | static void |
| 965 | _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition) |
| 966 | { |
| 967 | skip_type_t current = SKIP_NO_SKIP; |
| 968 | skip_node_t *node; |
| 969 | |
| 970 | if (parser->skip_stack) |
| 971 | current = parser->skip_stack->type; |
| 972 | |
| 973 | node = xtalloc (parser, skip_node_t); |
| 974 | |
| 975 | if (current == SKIP_NO_SKIP) { |
| 976 | if (condition) |
| 977 | node->type = SKIP_NO_SKIP; |
| 978 | else |
| 979 | node->type = SKIP_TO_ELSE; |
| 980 | } else { |
| 981 | node->type = SKIP_TO_ENDIF; |
| 982 | } |
| 983 | |
| 984 | node->next = parser->skip_stack; |
| 985 | parser->skip_stack = node; |
| 986 | } |
| 987 | |
| 988 | static void |
| 989 | _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type, |
| 990 | int condition) |
| 991 | { |
| 992 | if (parser->skip_stack == NULL) { |
| 993 | fprintf (stderr, "Error: %s without #if\n", type); |
| 994 | exit (1); |
| 995 | } |
| 996 | |
| 997 | if (parser->skip_stack->type == SKIP_TO_ELSE) { |
| 998 | if (condition) |
| 999 | parser->skip_stack->type = SKIP_NO_SKIP; |
| 1000 | } else { |
| 1001 | parser->skip_stack->type = SKIP_TO_ENDIF; |
| 1002 | } |
| 1003 | } |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 1004 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1005 | static void |
| 1006 | _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser) |
| 1007 | { |
| 1008 | skip_node_t *node; |
| 1009 | |
| 1010 | if (parser->skip_stack == NULL) { |
| 1011 | fprintf (stderr, "Error: #endif without #if\n"); |
| 1012 | exit (1); |
| 1013 | } |
| 1014 | |
| 1015 | node = parser->skip_stack; |
| 1016 | parser->skip_stack = node->next; |
| 1017 | talloc_free (node); |
| 1018 | } |