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 | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 31 | #define YYLEX_PARAM parser->scanner |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 32 | |
| 33 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 34 | yyerror (void *scanner, const char *error); |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 35 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 36 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 37 | _define_object_macro (glcpp_parser_t *parser, |
| 38 | const char *macro, |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 39 | const char *replacement); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 40 | |
| 41 | void |
| 42 | _define_function_macro (glcpp_parser_t *parser, |
| 43 | const char *macro, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 44 | string_list_t *parameters, |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 45 | const char *replacement); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 46 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 47 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 48 | _expand_object_macro (glcpp_parser_t *parser, const char *identifier); |
| 49 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 50 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 51 | _expand_function_macro (glcpp_parser_t *parser, |
| 52 | const char *identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 53 | argument_list_t *arguments); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 54 | |
| 55 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 56 | _print_string_list (string_list_t *list); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 57 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 58 | string_list_t * |
| 59 | _string_list_create (void *ctx); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 60 | |
| 61 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 62 | _string_list_append_item (string_list_t *list, const char *str); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 63 | |
| 64 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 65 | _string_list_append_list (string_list_t *list, string_list_t *tail); |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 66 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 67 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 68 | _string_list_contains (string_list_t *list, const char *member, int *index); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 69 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 70 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 71 | _string_list_length (string_list_t *list); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 72 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 73 | argument_list_t * |
| 74 | _argument_list_create (void *ctx); |
| 75 | |
| 76 | void |
| 77 | _argument_list_append (argument_list_t *list, string_list_t *argument); |
| 78 | |
| 79 | int |
| 80 | _argument_list_length (argument_list_t *list); |
| 81 | |
| 82 | string_list_t * |
| 83 | _argument_list_member_at (argument_list_t *list, int index); |
| 84 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 85 | %} |
| 86 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 87 | %union { |
| 88 | char *str; |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 89 | string_list_t *string_list; |
| 90 | argument_list_t *argument_list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 93 | %parse-param {glcpp_parser_t *parser} |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 94 | %lex-param {void *scanner} |
| 95 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 96 | %token DEFINE FUNC_MACRO IDENTIFIER NEWLINE OBJ_MACRO REPLACEMENT TOKEN UNDEF |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 97 | %type <str> argument_word FUNC_MACRO IDENTIFIER OBJ_MACRO REPLACEMENT TOKEN |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 98 | %type <string_list> argument macro parameter_list |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 99 | %type <argument_list> argument_list |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 100 | |
Carl Worth | 796e1f0 | 2010-05-17 12:45:16 -0700 | [diff] [blame] | 101 | /* Hard to remove shift/reduce conflicts documented as follows: |
| 102 | * |
| 103 | * 1. '(' after FUNC_MACRO name which is correctly resolved to shift |
| 104 | * to form macro invocation rather than reducing directly to |
| 105 | * content. |
Carl Worth | 69f390d | 2010-05-19 07:42:42 -0700 | [diff] [blame] | 106 | * |
| 107 | * 2. Similarly, '(' after FUNC_MACRO which is correctly resolved to |
| 108 | * shift to form macro invocation rather than reducing directly to |
| 109 | * argument. |
Carl Worth | 796e1f0 | 2010-05-17 12:45:16 -0700 | [diff] [blame] | 110 | */ |
Carl Worth | 69f390d | 2010-05-19 07:42:42 -0700 | [diff] [blame] | 111 | %expect 2 |
Carl Worth | 796e1f0 | 2010-05-17 12:45:16 -0700 | [diff] [blame] | 112 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 113 | %% |
| 114 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 115 | input: |
| 116 | /* empty */ |
Carl Worth | 8bcb6f1 | 2010-05-12 13:21:20 -0700 | [diff] [blame] | 117 | | input content |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 118 | ; |
| 119 | |
Carl Worth | 04af135 | 2010-05-14 10:17:38 -0700 | [diff] [blame] | 120 | /* We do all printing at the content level */ |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 121 | content: |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 122 | IDENTIFIER { |
| 123 | printf ("%s", $1); |
| 124 | talloc_free ($1); |
| 125 | } |
| 126 | | TOKEN { |
| 127 | printf ("%s", $1); |
| 128 | talloc_free ($1); |
| 129 | } |
Carl Worth | acf87bc | 2010-05-17 10:34:29 -0700 | [diff] [blame] | 130 | | FUNC_MACRO { |
| 131 | printf ("%s", $1); |
| 132 | talloc_free ($1); |
| 133 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 134 | | directive { |
| 135 | printf ("\n"); |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 136 | } |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 137 | | '(' { printf ("("); } |
| 138 | | ')' { printf (")"); } |
| 139 | | ',' { printf (","); } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 140 | | macro |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 141 | ; |
| 142 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 143 | macro: |
| 144 | FUNC_MACRO '(' argument_list ')' { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 145 | _expand_function_macro (parser, $1, $3); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 146 | } |
| 147 | | OBJ_MACRO { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 148 | _expand_object_macro (parser, $1); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 149 | talloc_free ($1); |
| 150 | } |
| 151 | ; |
| 152 | |
| 153 | argument_list: |
Carl Worth | ac070e8 | 2010-05-14 11:33:00 -0700 | [diff] [blame] | 154 | /* empty */ { |
| 155 | $$ = _argument_list_create (parser); |
| 156 | } |
| 157 | | argument { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 158 | $$ = _argument_list_create (parser); |
| 159 | _argument_list_append ($$, $1); |
| 160 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 161 | | argument_list ',' argument { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 162 | _argument_list_append ($1, $3); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 163 | $$ = $1; |
| 164 | } |
| 165 | ; |
| 166 | |
| 167 | argument: |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 168 | argument_word { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 169 | $$ = _string_list_create (parser); |
Carl Worth | ac070e8 | 2010-05-14 11:33:00 -0700 | [diff] [blame] | 170 | _string_list_append_item ($$, $1); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 171 | } |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 172 | | argument argument_word { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 173 | _string_list_append_item ($1, $2); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 174 | talloc_free ($2); |
Carl Worth | 3596bb1 | 2010-05-14 16:53:52 -0700 | [diff] [blame] | 175 | $$ = $1; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 176 | } |
Carl Worth | 3596bb1 | 2010-05-14 16:53:52 -0700 | [diff] [blame] | 177 | | argument '(' argument ')' { |
| 178 | _string_list_append_item ($1, "("); |
| 179 | _string_list_append_list ($1, $3); |
| 180 | _string_list_append_item ($1, ")"); |
| 181 | $$ = $1; |
| 182 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 183 | ; |
| 184 | |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 185 | argument_word: |
| 186 | IDENTIFIER { $$ = $1; } |
| 187 | | TOKEN { $$ = $1; } |
| 188 | | FUNC_MACRO { $$ = $1; } |
Carl Worth | 5d21142 | 2010-05-19 07:57:03 -0700 | [diff] [blame^] | 189 | | macro { $$ = xtalloc_strdup (parser, ""); } |
Carl Worth | 59ca989 | 2010-05-19 07:49:47 -0700 | [diff] [blame] | 190 | ; |
| 191 | |
| 192 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 193 | directive: |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 194 | DEFINE IDENTIFIER REPLACEMENT { |
| 195 | _define_object_macro (parser, $2, $3); |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 196 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 197 | | DEFINE IDENTIFIER '(' parameter_list ')' REPLACEMENT { |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 198 | _define_function_macro (parser, $2, $4, $6); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 199 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 200 | | UNDEF IDENTIFIER { |
| 201 | string_list_t *macro = hash_table_find (parser->defines, $2); |
| 202 | if (macro) { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 203 | /* XXX: Need hash table to support a real way |
| 204 | * to remove an element rather than prefixing |
| 205 | * a new node with data of NULL like this. */ |
| 206 | hash_table_insert (parser->defines, NULL, $2); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 207 | talloc_free (macro); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 208 | } |
| 209 | talloc_free ($2); |
| 210 | } |
Carl Worth | 38bd27b | 2010-05-14 12:05:37 -0700 | [diff] [blame] | 211 | ; |
| 212 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 213 | parameter_list: |
| 214 | /* empty */ { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 215 | $$ = _string_list_create (parser); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 216 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 217 | | IDENTIFIER { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 218 | $$ = _string_list_create (parser); |
| 219 | _string_list_append_item ($$, $1); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 220 | talloc_free ($1); |
| 221 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 222 | | parameter_list ',' IDENTIFIER { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 223 | _string_list_append_item ($1, $3); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 224 | talloc_free ($3); |
| 225 | $$ = $1; |
| 226 | } |
| 227 | ; |
| 228 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 229 | %% |
| 230 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 231 | string_list_t * |
| 232 | _string_list_create (void *ctx) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 233 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 234 | string_list_t *list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 235 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 236 | list = xtalloc (ctx, string_list_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 237 | list->head = NULL; |
| 238 | list->tail = NULL; |
| 239 | |
| 240 | return list; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 241 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 242 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 243 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 244 | _string_list_append_list (string_list_t *list, string_list_t *tail) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 245 | { |
| 246 | if (list->head == NULL) { |
| 247 | list->head = tail->head; |
| 248 | } else { |
| 249 | list->tail->next = tail->head; |
| 250 | } |
| 251 | |
| 252 | list->tail = tail->tail; |
| 253 | } |
| 254 | |
| 255 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 256 | _string_list_append_item (string_list_t *list, const char *str) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 257 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 258 | string_node_t *node; |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 259 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 260 | node = xtalloc (list, string_node_t); |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 261 | node->str = xtalloc_strdup (node, str); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 262 | |
| 263 | node->next = NULL; |
| 264 | |
| 265 | if (list->head == NULL) { |
| 266 | list->head = node; |
| 267 | } else { |
| 268 | list->tail->next = node; |
| 269 | } |
| 270 | |
| 271 | list->tail = node; |
| 272 | } |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 273 | |
| 274 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 275 | _string_list_contains (string_list_t *list, const char *member, int *index) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 276 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 277 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 278 | int i; |
| 279 | |
| 280 | if (list == NULL) |
| 281 | return 0; |
| 282 | |
| 283 | for (i = 0, node = list->head; node; i++, node = node->next) { |
| 284 | if (strcmp (node->str, member) == 0) { |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 285 | if (index) |
| 286 | *index = i; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 287 | return 1; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 295 | _string_list_length (string_list_t *list) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 296 | { |
| 297 | int length = 0; |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 298 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 299 | |
| 300 | if (list == NULL) |
| 301 | return 0; |
| 302 | |
| 303 | for (node = list->head; node; node = node->next) |
| 304 | length++; |
| 305 | |
| 306 | return length; |
| 307 | } |
| 308 | |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 309 | void |
| 310 | _print_string_list (string_list_t *list) |
| 311 | { |
| 312 | string_node_t *node; |
| 313 | |
| 314 | if (list == NULL) |
| 315 | return; |
| 316 | |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 317 | for (node = list->head; node; node = node->next) { |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 318 | printf ("%s", node->str); |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 319 | if (node->next) |
| 320 | printf (" "); |
| 321 | } |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 324 | argument_list_t * |
| 325 | _argument_list_create (void *ctx) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 326 | { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 327 | argument_list_t *list; |
| 328 | |
| 329 | list = xtalloc (ctx, argument_list_t); |
| 330 | list->head = NULL; |
| 331 | list->tail = NULL; |
| 332 | |
| 333 | return list; |
| 334 | } |
| 335 | |
| 336 | void |
| 337 | _argument_list_append (argument_list_t *list, string_list_t *argument) |
| 338 | { |
| 339 | argument_node_t *node; |
| 340 | |
| 341 | if (argument == NULL || argument->head == NULL) |
| 342 | return; |
| 343 | |
| 344 | node = xtalloc (list, argument_node_t); |
| 345 | node->argument = argument; |
| 346 | |
| 347 | node->next = NULL; |
| 348 | |
| 349 | if (list->head == NULL) { |
| 350 | list->head = node; |
| 351 | } else { |
| 352 | list->tail->next = node; |
| 353 | } |
| 354 | |
| 355 | list->tail = node; |
| 356 | } |
| 357 | |
| 358 | int |
| 359 | _argument_list_length (argument_list_t *list) |
| 360 | { |
| 361 | int length = 0; |
| 362 | argument_node_t *node; |
| 363 | |
| 364 | if (list == NULL) |
| 365 | return 0; |
| 366 | |
| 367 | for (node = list->head; node; node = node->next) |
| 368 | length++; |
| 369 | |
| 370 | return length; |
| 371 | } |
| 372 | |
| 373 | string_list_t * |
| 374 | _argument_list_member_at (argument_list_t *list, int index) |
| 375 | { |
| 376 | argument_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 377 | int i; |
| 378 | |
| 379 | if (list == NULL) |
| 380 | return NULL; |
| 381 | |
| 382 | node = list->head; |
| 383 | for (i = 0; i < index; i++) { |
| 384 | node = node->next; |
| 385 | if (node == NULL) |
| 386 | break; |
| 387 | } |
| 388 | |
| 389 | if (node) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 390 | return node->argument; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 391 | |
| 392 | return NULL; |
| 393 | } |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 394 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 395 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 396 | yyerror (void *scanner, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 397 | { |
| 398 | fprintf (stderr, "Parse error: %s\n", error); |
| 399 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 400 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 401 | glcpp_parser_t * |
| 402 | glcpp_parser_create (void) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 403 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 404 | glcpp_parser_t *parser; |
| 405 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 406 | parser = xtalloc (NULL, glcpp_parser_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 407 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 408 | yylex_init_extra (parser, &parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 409 | parser->defines = hash_table_ctor (32, hash_table_string_hash, |
| 410 | hash_table_string_compare); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 411 | parser->expansions = NULL; |
| 412 | |
| 413 | parser->lex_stack = xtalloc (parser, glcpp_lex_stack_t); |
| 414 | parser->lex_stack->parser = parser; |
| 415 | parser->lex_stack->head = NULL; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 416 | |
| 417 | return parser; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | int |
| 421 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 422 | { |
| 423 | return yyparse (parser); |
| 424 | } |
| 425 | |
| 426 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 427 | glcpp_parser_destroy (glcpp_parser_t *parser) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 428 | { |
| 429 | yylex_destroy (parser->scanner); |
| 430 | hash_table_dtor (parser->defines); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 431 | talloc_free (parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 432 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 433 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 434 | static int |
| 435 | glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member) |
| 436 | { |
| 437 | expansion_node_t *node; |
| 438 | |
| 439 | for (node = parser->expansions; node; node = node->next) { |
| 440 | if (node->macro && |
| 441 | strcmp (node->macro->identifier, member) == 0) |
| 442 | { |
| 443 | return 1; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 450 | token_class_t |
| 451 | glcpp_parser_classify_token (glcpp_parser_t *parser, |
| 452 | const char *identifier, |
| 453 | int *parameter_index) |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 454 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 455 | macro_t *macro; |
| 456 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 457 | /* First we check if we are currently expanding a |
| 458 | * function-like macro, and if so, whether the parameter list |
| 459 | * contains a parameter matching this token name. */ |
| 460 | if (parser->expansions && |
| 461 | parser->expansions->macro && |
| 462 | parser->expansions->macro->parameters) |
| 463 | { |
| 464 | string_list_t *list; |
| 465 | |
| 466 | list = parser->expansions->macro->parameters; |
| 467 | |
| 468 | if (_string_list_contains (list, identifier, parameter_index)) |
| 469 | return TOKEN_CLASS_ARGUMENT; |
| 470 | } |
| 471 | |
| 472 | /* If not a function-like macro parameter, we next check if |
| 473 | * this token is a macro itself. */ |
| 474 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 475 | macro = hash_table_find (parser->defines, identifier); |
| 476 | |
| 477 | if (macro == NULL) |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 478 | return TOKEN_CLASS_IDENTIFIER; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 479 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 480 | /* Don't consider this a macro if we are already actively |
| 481 | * expanding this macro. */ |
| 482 | if (glcpp_parser_is_expanding (parser, identifier)) |
| 483 | return TOKEN_CLASS_IDENTIFIER; |
| 484 | |
| 485 | /* Definitely a macro. Just need to check if it's function-like. */ |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 486 | if (macro->is_function) |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 487 | return TOKEN_CLASS_FUNC_MACRO; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 488 | else |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 489 | return TOKEN_CLASS_OBJ_MACRO; |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 492 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 493 | _define_object_macro (glcpp_parser_t *parser, |
| 494 | const char *identifier, |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 495 | const char *replacement) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 496 | { |
| 497 | macro_t *macro; |
| 498 | |
| 499 | macro = xtalloc (parser, macro_t); |
| 500 | |
| 501 | macro->is_function = 0; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 502 | macro->parameters = NULL; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 503 | macro->identifier = talloc_strdup (macro, identifier); |
| 504 | macro->replacement = talloc_steal (macro, replacement); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 505 | |
| 506 | hash_table_insert (parser->defines, macro, identifier); |
| 507 | } |
| 508 | |
| 509 | void |
| 510 | _define_function_macro (glcpp_parser_t *parser, |
| 511 | const char *identifier, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 512 | string_list_t *parameters, |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 513 | const char *replacement) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 514 | { |
| 515 | macro_t *macro; |
| 516 | |
| 517 | macro = xtalloc (parser, macro_t); |
| 518 | |
| 519 | macro->is_function = 1; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 520 | macro->parameters = talloc_steal (macro, parameters); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 521 | macro->identifier = talloc_strdup (macro, identifier); |
| 522 | macro->replacement = talloc_steal (macro, replacement); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 523 | |
| 524 | hash_table_insert (parser->defines, macro, identifier); |
| 525 | } |
| 526 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 527 | static void |
| 528 | _glcpp_parser_push_expansion_internal (glcpp_parser_t *parser, |
| 529 | macro_t *macro, |
| 530 | argument_list_t *arguments, |
| 531 | const char * replacement) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 532 | { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 533 | expansion_node_t *node; |
| 534 | |
| 535 | node = xtalloc (parser, expansion_node_t); |
| 536 | |
| 537 | node->macro = macro; |
| 538 | node->arguments = arguments; |
| 539 | |
| 540 | node->next = parser->expansions; |
| 541 | parser->expansions = node; |
| 542 | |
| 543 | glcpp_lex_stack_push (parser->lex_stack, replacement); |
| 544 | } |
| 545 | |
| 546 | void |
| 547 | glcpp_parser_push_expansion_macro (glcpp_parser_t *parser, |
| 548 | macro_t *macro, |
| 549 | argument_list_t *arguments) |
| 550 | { |
| 551 | _glcpp_parser_push_expansion_internal (parser, macro, arguments, |
| 552 | macro->replacement); |
| 553 | } |
| 554 | |
| 555 | void |
| 556 | glcpp_parser_push_expansion_argument (glcpp_parser_t *parser, |
| 557 | int argument_index) |
| 558 | { |
| 559 | argument_list_t *arguments; |
| 560 | string_list_t *argument; |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 561 | string_node_t *node; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 562 | char *argument_str, *s; |
| 563 | int length; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 564 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 565 | arguments = parser->expansions->arguments; |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 566 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 567 | argument = _argument_list_member_at (arguments, argument_index); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 568 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 569 | length = 0; |
| 570 | for (node = argument->head; node; node = node->next) |
| 571 | length += strlen (node->str) + 1; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 572 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 573 | argument_str = xtalloc_size (parser, length); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 574 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 575 | *argument_str = '\0'; |
| 576 | s = argument_str; |
| 577 | for (node = argument->head; node; node = node->next) { |
| 578 | strcpy (s, node->str); |
| 579 | s += strlen (node->str); |
| 580 | if (node->next) { |
| 581 | *s = ' '; |
| 582 | s++; |
| 583 | *s = '\0'; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 584 | } |
| 585 | } |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 586 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 587 | _glcpp_parser_push_expansion_internal (parser, NULL, NULL, |
| 588 | argument_str); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 591 | /* The lexer calls this when it exhausts a string. */ |
| 592 | void |
| 593 | glcpp_parser_pop_expansion (glcpp_parser_t *parser) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 594 | { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 595 | expansion_node_t *node; |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 596 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 597 | node = parser->expansions; |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 598 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 599 | if (node == NULL) { |
| 600 | fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n"); |
| 601 | exit (1); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 604 | parser->expansions = node->next; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 605 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 606 | talloc_free (node); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 609 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 610 | _expand_object_macro (glcpp_parser_t *parser, const char *identifier) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 611 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 612 | macro_t *macro; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 613 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 614 | macro = hash_table_find (parser->defines, identifier); |
| 615 | assert (! macro->is_function); |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 616 | assert (! glcpp_parser_is_expanding (parser, identifier)); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 617 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 618 | glcpp_parser_push_expansion_macro (parser, macro, NULL); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 621 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 622 | _expand_function_macro (glcpp_parser_t *parser, |
| 623 | const char *identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 624 | argument_list_t *arguments) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 625 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 626 | macro_t *macro; |
| 627 | |
| 628 | macro = hash_table_find (parser->defines, identifier); |
| 629 | assert (macro->is_function); |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 630 | assert (! glcpp_parser_is_expanding (parser, identifier)); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 631 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 632 | if (_argument_list_length (arguments) != |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 633 | _string_list_length (macro->parameters)) |
| 634 | { |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 635 | fprintf (stderr, |
| 636 | "Error: macro %s invoked with %d arguments (expected %d)\n", |
| 637 | identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 638 | _argument_list_length (arguments), |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 639 | _string_list_length (macro->parameters)); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 640 | return; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 641 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 642 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame] | 643 | glcpp_parser_push_expansion_macro (parser, macro, arguments); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 644 | } |