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 | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 28 | #include <inttypes.h> |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 29 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 30 | #include "glcpp.h" |
Ian Romanick | 06143ea | 2010-06-30 16:27:22 -0700 | [diff] [blame] | 31 | #include "main/mtypes.h" |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 32 | |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 33 | #define glcpp_print(stream, str) stream = talloc_strdup_append(stream, str) |
| 34 | #define glcpp_printf(stream, fmt, args...) \ |
| 35 | stream = talloc_asprintf_append(stream, fmt, args) |
| 36 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 37 | static void |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 38 | yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error); |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 39 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 40 | static void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 41 | _define_object_macro (glcpp_parser_t *parser, |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 42 | YYLTYPE *loc, |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 43 | const char *macro, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 44 | token_list_t *replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 45 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 46 | static void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 47 | _define_function_macro (glcpp_parser_t *parser, |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 48 | YYLTYPE *loc, |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 49 | const char *macro, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 50 | string_list_t *parameters, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 51 | token_list_t *replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 52 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 53 | static string_list_t * |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 54 | _string_list_create (void *ctx); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 55 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 56 | static void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 57 | _string_list_append_item (string_list_t *list, const char *str); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 58 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 59 | static int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 60 | _string_list_contains (string_list_t *list, const char *member, int *index); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 61 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 62 | static int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 63 | _string_list_length (string_list_t *list); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 64 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 65 | static argument_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 66 | _argument_list_create (void *ctx); |
| 67 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 68 | static void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 69 | _argument_list_append (argument_list_t *list, token_list_t *argument); |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 70 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 71 | static int |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 72 | _argument_list_length (argument_list_t *list); |
| 73 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 74 | static token_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 75 | _argument_list_member_at (argument_list_t *list, int index); |
| 76 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 77 | /* Note: This function talloc_steal()s the str pointer. */ |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 78 | static token_t * |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 79 | _token_create_str (void *ctx, int type, char *str); |
| 80 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 81 | static token_t * |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 82 | _token_create_ival (void *ctx, int type, int ival); |
| 83 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 84 | static token_list_t * |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 85 | _token_list_create (void *ctx); |
| 86 | |
Carl Worth | b1ae61a | 2010-05-26 08:10:38 -0700 | [diff] [blame] | 87 | /* Note: This function adds a talloc_reference() to token. |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 88 | * |
| 89 | * You may want to talloc_unlink any current reference if you no |
| 90 | * longer need it. */ |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 91 | static void |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 92 | _token_list_append (token_list_t *list, token_t *token); |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 93 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 94 | static void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 95 | _token_list_append_list (token_list_t *list, token_list_t *tail); |
| 96 | |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 97 | static active_list_t * |
| 98 | _active_list_push (active_list_t *list, |
| 99 | const char *identifier, |
| 100 | token_node_t *marker); |
| 101 | |
| 102 | static active_list_t * |
| 103 | _active_list_pop (active_list_t *list); |
| 104 | |
| 105 | int |
| 106 | _active_list_contains (active_list_t *list, const char *identifier); |
| 107 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 108 | static void |
Kenneth Graunke | 16b4eed | 2010-08-04 16:10:03 -0700 | [diff] [blame^] | 109 | _glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list); |
| 110 | |
| 111 | static void |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 112 | _glcpp_parser_expand_token_list (glcpp_parser_t *parser, |
| 113 | token_list_t *list); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 114 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 115 | static void |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 116 | _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser, |
| 117 | token_list_t *list); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 118 | |
| 119 | static void |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 120 | _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc, |
| 121 | int condition); |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 122 | |
| 123 | static void |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 124 | _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc, |
| 125 | const char *type, int condition); |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 126 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 127 | static void |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 128 | _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc); |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 129 | |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 130 | #define yylex glcpp_parser_lex |
| 131 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 132 | static int |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 133 | glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 134 | |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 135 | static void |
| 136 | glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list); |
| 137 | |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 138 | static void |
| 139 | add_builtin_define(glcpp_parser_t *parser, const char *name, int value); |
| 140 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 141 | %} |
| 142 | |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 143 | %pure-parser |
Kenneth Graunke | f70f607 | 2010-06-17 13:07:13 -0700 | [diff] [blame] | 144 | %error-verbose |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 145 | %locations |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 146 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 147 | %parse-param {glcpp_parser_t *parser} |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 148 | %lex-param {glcpp_parser_t *parser} |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 149 | |
Carl Worth | efef950 | 2010-07-28 11:10:52 -0700 | [diff] [blame] | 150 | %expect 0 |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 151 | %token COMMA_FINAL DEFINED ELIF_EXPANDED HASH HASH_DEFINE_FUNC HASH_DEFINE_OBJ HASH_ELIF HASH_ELSE HASH_ENDIF HASH_IF HASH_IFDEF HASH_IFNDEF HASH_UNDEF HASH_VERSION IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING NEWLINE OTHER PLACEHOLDER SPACE |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 152 | %token PASTE |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 153 | %type <ival> expression INTEGER operator SPACE integer_constant |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 154 | %type <str> IDENTIFIER INTEGER_STRING OTHER |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 155 | %type <string_list> identifier_list |
Kenneth Graunke | 26e761e | 2010-06-18 23:06:54 -0700 | [diff] [blame] | 156 | %type <token> preprocessing_token conditional_token |
| 157 | %type <token_list> pp_tokens replacement_list text_line conditional_tokens |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 158 | %left OR |
| 159 | %left AND |
| 160 | %left '|' |
| 161 | %left '^' |
| 162 | %left '&' |
| 163 | %left EQUAL NOT_EQUAL |
| 164 | %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL |
| 165 | %left LEFT_SHIFT RIGHT_SHIFT |
| 166 | %left '+' '-' |
| 167 | %left '*' '/' '%' |
| 168 | %right UNARY |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 169 | |
| 170 | %% |
| 171 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 172 | input: |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 173 | /* empty */ |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 174 | | input line |
| 175 | ; |
| 176 | |
| 177 | line: |
| 178 | control_line { |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 179 | glcpp_print(parser->output, "\n"); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 180 | } |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 181 | | text_line { |
Carl Worth | a771a40 | 2010-06-01 11:20:18 -0700 | [diff] [blame] | 182 | _glcpp_parser_print_expanded_token_list (parser, $1); |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 183 | glcpp_print(parser->output, "\n"); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 184 | talloc_free ($1); |
| 185 | } |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 186 | | expanded_line |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 187 | | HASH non_directive |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 188 | ; |
| 189 | |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 190 | expanded_line: |
| 191 | IF_EXPANDED expression NEWLINE { |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 192 | _glcpp_parser_skip_stack_push_if (parser, & @1, $2); |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 193 | } |
| 194 | | ELIF_EXPANDED expression NEWLINE { |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 195 | _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2); |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 196 | } |
| 197 | ; |
| 198 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 199 | control_line: |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 200 | HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE { |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 201 | _define_object_macro (parser, & @2, $2, $3); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 202 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 203 | | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE { |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 204 | _define_function_macro (parser, & @2, $2, NULL, $5); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 205 | } |
| 206 | | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE { |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 207 | _define_function_macro (parser, & @2, $2, $4, $6); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 208 | } |
Carl Worth | e6fb782 | 2010-05-25 15:28:58 -0700 | [diff] [blame] | 209 | | HASH_UNDEF IDENTIFIER NEWLINE { |
Carl Worth | 0324cad | 2010-05-26 15:53:05 -0700 | [diff] [blame] | 210 | macro_t *macro = hash_table_find (parser->defines, $2); |
Carl Worth | e6fb782 | 2010-05-25 15:28:58 -0700 | [diff] [blame] | 211 | if (macro) { |
Carl Worth | 61ebc01 | 2010-07-19 18:02:12 -0700 | [diff] [blame] | 212 | hash_table_remove (parser->defines, $2); |
Carl Worth | e6fb782 | 2010-05-25 15:28:58 -0700 | [diff] [blame] | 213 | talloc_free (macro); |
| 214 | } |
| 215 | talloc_free ($2); |
| 216 | } |
Kenneth Graunke | 26e761e | 2010-06-18 23:06:54 -0700 | [diff] [blame] | 217 | | HASH_IF conditional_tokens NEWLINE { |
Kenneth Graunke | 16b4eed | 2010-08-04 16:10:03 -0700 | [diff] [blame^] | 218 | _glcpp_parser_expand_if (parser, IF_EXPANDED, $2); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 219 | } |
Kenneth Graunke | 6587574 | 2010-06-18 20:08:15 -0700 | [diff] [blame] | 220 | | HASH_IFDEF IDENTIFIER junk NEWLINE { |
Carl Worth | 0324cad | 2010-05-26 15:53:05 -0700 | [diff] [blame] | 221 | macro_t *macro = hash_table_find (parser->defines, $2); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 222 | talloc_free ($2); |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 223 | _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 224 | } |
Kenneth Graunke | 6587574 | 2010-06-18 20:08:15 -0700 | [diff] [blame] | 225 | | HASH_IFNDEF IDENTIFIER junk NEWLINE { |
Carl Worth | 0324cad | 2010-05-26 15:53:05 -0700 | [diff] [blame] | 226 | macro_t *macro = hash_table_find (parser->defines, $2); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 227 | talloc_free ($2); |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 228 | _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 229 | } |
Kenneth Graunke | 26e761e | 2010-06-18 23:06:54 -0700 | [diff] [blame] | 230 | | HASH_ELIF conditional_tokens NEWLINE { |
Kenneth Graunke | 16b4eed | 2010-08-04 16:10:03 -0700 | [diff] [blame^] | 231 | _glcpp_parser_expand_if (parser, ELIF_EXPANDED, $2); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 232 | } |
Kenneth Graunke | 332fc47 | 2010-06-21 12:20:22 -0700 | [diff] [blame] | 233 | | HASH_ELIF NEWLINE { |
| 234 | /* #elif without an expression results in a warning if the |
| 235 | * condition doesn't matter (we just handled #if 1 or such) |
| 236 | * but an error otherwise. */ |
| 237 | if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP) { |
| 238 | parser->skip_stack->type = SKIP_TO_ENDIF; |
| 239 | glcpp_warning(& @1, parser, "ignoring illegal #elif without expression"); |
| 240 | } else { |
| 241 | glcpp_error(& @1, parser, "#elif needs an expression"); |
| 242 | } |
| 243 | } |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 244 | | HASH_ELSE NEWLINE { |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 245 | _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 246 | } |
| 247 | | HASH_ENDIF NEWLINE { |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 248 | _glcpp_parser_skip_stack_pop (parser, & @1); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 249 | } |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 250 | | HASH_VERSION integer_constant NEWLINE { |
| 251 | macro_t *macro = hash_table_find (parser->defines, "__VERSION__"); |
| 252 | if (macro) { |
| 253 | hash_table_remove (parser->defines, "__VERSION__"); |
| 254 | talloc_free (macro); |
| 255 | } |
| 256 | add_builtin_define (parser, "__VERSION__", $2); |
| 257 | glcpp_printf(parser->output, "#version %" PRIiMAX "\n", $2); |
| 258 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 259 | | HASH NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 260 | ; |
| 261 | |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 262 | integer_constant: |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 263 | INTEGER_STRING { |
| 264 | if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) { |
| 265 | $$ = strtoll ($1 + 2, NULL, 16); |
| 266 | } else if ($1[0] == '0') { |
| 267 | $$ = strtoll ($1, NULL, 8); |
| 268 | } else { |
| 269 | $$ = strtoll ($1, NULL, 10); |
| 270 | } |
| 271 | } |
| 272 | | INTEGER { |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 273 | $$ = $1; |
| 274 | } |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 275 | |
| 276 | expression: |
| 277 | integer_constant |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 278 | | expression OR expression { |
| 279 | $$ = $1 || $3; |
| 280 | } |
| 281 | | expression AND expression { |
| 282 | $$ = $1 && $3; |
| 283 | } |
| 284 | | expression '|' expression { |
| 285 | $$ = $1 | $3; |
| 286 | } |
| 287 | | expression '^' expression { |
| 288 | $$ = $1 ^ $3; |
| 289 | } |
| 290 | | expression '&' expression { |
| 291 | $$ = $1 & $3; |
| 292 | } |
| 293 | | expression NOT_EQUAL expression { |
| 294 | $$ = $1 != $3; |
| 295 | } |
| 296 | | expression EQUAL expression { |
| 297 | $$ = $1 == $3; |
| 298 | } |
| 299 | | expression GREATER_OR_EQUAL expression { |
| 300 | $$ = $1 >= $3; |
| 301 | } |
| 302 | | expression LESS_OR_EQUAL expression { |
| 303 | $$ = $1 <= $3; |
| 304 | } |
| 305 | | expression '>' expression { |
| 306 | $$ = $1 > $3; |
| 307 | } |
| 308 | | expression '<' expression { |
| 309 | $$ = $1 < $3; |
| 310 | } |
| 311 | | expression RIGHT_SHIFT expression { |
| 312 | $$ = $1 >> $3; |
| 313 | } |
| 314 | | expression LEFT_SHIFT expression { |
| 315 | $$ = $1 << $3; |
| 316 | } |
| 317 | | expression '-' expression { |
| 318 | $$ = $1 - $3; |
| 319 | } |
| 320 | | expression '+' expression { |
| 321 | $$ = $1 + $3; |
| 322 | } |
| 323 | | expression '%' expression { |
| 324 | $$ = $1 % $3; |
| 325 | } |
| 326 | | expression '/' expression { |
| 327 | $$ = $1 / $3; |
| 328 | } |
| 329 | | expression '*' expression { |
| 330 | $$ = $1 * $3; |
| 331 | } |
| 332 | | '!' expression %prec UNARY { |
| 333 | $$ = ! $2; |
| 334 | } |
| 335 | | '~' expression %prec UNARY { |
| 336 | $$ = ~ $2; |
| 337 | } |
| 338 | | '-' expression %prec UNARY { |
| 339 | $$ = - $2; |
| 340 | } |
| 341 | | '+' expression %prec UNARY { |
| 342 | $$ = + $2; |
| 343 | } |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 344 | | '(' expression ')' { |
| 345 | $$ = $2; |
| 346 | } |
| 347 | ; |
| 348 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 349 | identifier_list: |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 350 | IDENTIFIER { |
| 351 | $$ = _string_list_create (parser); |
| 352 | _string_list_append_item ($$, $1); |
| 353 | talloc_steal ($$, $1); |
| 354 | } |
| 355 | | identifier_list ',' IDENTIFIER { |
| 356 | $$ = $1; |
| 357 | _string_list_append_item ($$, $3); |
| 358 | talloc_steal ($$, $3); |
| 359 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 360 | ; |
| 361 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 362 | text_line: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 363 | NEWLINE { $$ = NULL; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 364 | | pp_tokens NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 365 | ; |
| 366 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 367 | non_directive: |
Kenneth Graunke | 739ba06 | 2010-06-16 12:41:37 -0700 | [diff] [blame] | 368 | pp_tokens NEWLINE { |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 369 | yyerror (& @1, parser, "Invalid tokens after #"); |
Kenneth Graunke | 739ba06 | 2010-06-16 12:41:37 -0700 | [diff] [blame] | 370 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 371 | ; |
| 372 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 373 | replacement_list: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 374 | /* empty */ { $$ = NULL; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 375 | | pp_tokens |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 376 | ; |
| 377 | |
Kenneth Graunke | 6587574 | 2010-06-18 20:08:15 -0700 | [diff] [blame] | 378 | junk: |
| 379 | /* empty */ |
| 380 | | pp_tokens { |
| 381 | glcpp_warning(&@1, parser, "extra tokens at end of directive"); |
| 382 | } |
Kenneth Graunke | 26e761e | 2010-06-18 23:06:54 -0700 | [diff] [blame] | 383 | ; |
| 384 | |
| 385 | conditional_token: |
| 386 | /* Handle "defined" operator */ |
| 387 | DEFINED IDENTIFIER { |
| 388 | int v = hash_table_find (parser->defines, $2) ? 1 : 0; |
| 389 | $$ = _token_create_ival (parser, INTEGER, v); |
| 390 | } |
| 391 | | DEFINED '(' IDENTIFIER ')' { |
| 392 | int v = hash_table_find (parser->defines, $3) ? 1 : 0; |
| 393 | $$ = _token_create_ival (parser, INTEGER, v); |
| 394 | } |
| 395 | | preprocessing_token |
| 396 | ; |
| 397 | |
| 398 | conditional_tokens: |
| 399 | /* Exactly the same as pp_tokens, but using conditional_token */ |
| 400 | conditional_token { |
| 401 | parser->space_tokens = 1; |
| 402 | $$ = _token_list_create (parser); |
| 403 | _token_list_append ($$, $1); |
| 404 | talloc_unlink (parser, $1); |
| 405 | } |
| 406 | | conditional_tokens conditional_token { |
| 407 | $$ = $1; |
| 408 | _token_list_append ($$, $2); |
| 409 | talloc_unlink (parser, $2); |
| 410 | } |
| 411 | ; |
Kenneth Graunke | 6587574 | 2010-06-18 20:08:15 -0700 | [diff] [blame] | 412 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 413 | pp_tokens: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 414 | preprocessing_token { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 415 | parser->space_tokens = 1; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 416 | $$ = _token_list_create (parser); |
| 417 | _token_list_append ($$, $1); |
| 418 | talloc_unlink (parser, $1); |
| 419 | } |
| 420 | | pp_tokens preprocessing_token { |
| 421 | $$ = $1; |
| 422 | _token_list_append ($$, $2); |
| 423 | talloc_unlink (parser, $2); |
| 424 | } |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 425 | ; |
| 426 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 427 | preprocessing_token: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 428 | IDENTIFIER { |
| 429 | $$ = _token_create_str (parser, IDENTIFIER, $1); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 430 | $$->location = yylloc; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 431 | } |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 432 | | INTEGER_STRING { |
| 433 | $$ = _token_create_str (parser, INTEGER_STRING, $1); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 434 | $$->location = yylloc; |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 435 | } |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 436 | | operator { |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 437 | $$ = _token_create_ival (parser, $1, $1); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 438 | $$->location = yylloc; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 439 | } |
| 440 | | OTHER { |
| 441 | $$ = _token_create_str (parser, OTHER, $1); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 442 | $$->location = yylloc; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 443 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 444 | | SPACE { |
Carl Worth | e939786 | 2010-05-25 17:08:07 -0700 | [diff] [blame] | 445 | $$ = _token_create_ival (parser, SPACE, SPACE); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 446 | $$->location = yylloc; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 447 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 448 | ; |
| 449 | |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 450 | operator: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 451 | '[' { $$ = '['; } |
| 452 | | ']' { $$ = ']'; } |
| 453 | | '(' { $$ = '('; } |
| 454 | | ')' { $$ = ')'; } |
| 455 | | '{' { $$ = '{'; } |
| 456 | | '}' { $$ = '}'; } |
| 457 | | '.' { $$ = '.'; } |
| 458 | | '&' { $$ = '&'; } |
| 459 | | '*' { $$ = '*'; } |
| 460 | | '+' { $$ = '+'; } |
| 461 | | '-' { $$ = '-'; } |
| 462 | | '~' { $$ = '~'; } |
| 463 | | '!' { $$ = '!'; } |
| 464 | | '/' { $$ = '/'; } |
| 465 | | '%' { $$ = '%'; } |
| 466 | | LEFT_SHIFT { $$ = LEFT_SHIFT; } |
| 467 | | RIGHT_SHIFT { $$ = RIGHT_SHIFT; } |
| 468 | | '<' { $$ = '<'; } |
| 469 | | '>' { $$ = '>'; } |
| 470 | | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; } |
| 471 | | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; } |
| 472 | | EQUAL { $$ = EQUAL; } |
| 473 | | NOT_EQUAL { $$ = NOT_EQUAL; } |
| 474 | | '^' { $$ = '^'; } |
| 475 | | '|' { $$ = '|'; } |
| 476 | | AND { $$ = AND; } |
| 477 | | OR { $$ = OR; } |
| 478 | | ';' { $$ = ';'; } |
| 479 | | ',' { $$ = ','; } |
Carl Worth | 6310169 | 2010-05-29 05:07:24 -0700 | [diff] [blame] | 480 | | '=' { $$ = '='; } |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 481 | | PASTE { $$ = PASTE; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 482 | ; |
| 483 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 484 | %% |
| 485 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 486 | string_list_t * |
| 487 | _string_list_create (void *ctx) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 488 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 489 | string_list_t *list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 490 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 491 | list = talloc (ctx, string_list_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 492 | list->head = NULL; |
| 493 | list->tail = NULL; |
| 494 | |
| 495 | return list; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 496 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 497 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 498 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 499 | _string_list_append_item (string_list_t *list, const char *str) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 500 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 501 | string_node_t *node; |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 502 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 503 | node = talloc (list, string_node_t); |
| 504 | node->str = talloc_strdup (node, str); |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 505 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 506 | node->next = NULL; |
| 507 | |
| 508 | if (list->head == NULL) { |
| 509 | list->head = node; |
| 510 | } else { |
| 511 | list->tail->next = node; |
| 512 | } |
| 513 | |
| 514 | list->tail = node; |
| 515 | } |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 516 | |
| 517 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 518 | _string_list_contains (string_list_t *list, const char *member, int *index) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 519 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 520 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 521 | int i; |
| 522 | |
| 523 | if (list == NULL) |
| 524 | return 0; |
| 525 | |
| 526 | for (i = 0, node = list->head; node; i++, node = node->next) { |
| 527 | if (strcmp (node->str, member) == 0) { |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 528 | if (index) |
| 529 | *index = i; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 530 | return 1; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 538 | _string_list_length (string_list_t *list) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 539 | { |
| 540 | int length = 0; |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 541 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 542 | |
| 543 | if (list == NULL) |
| 544 | return 0; |
| 545 | |
| 546 | for (node = list->head; node; node = node->next) |
| 547 | length++; |
| 548 | |
| 549 | return length; |
| 550 | } |
| 551 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 552 | argument_list_t * |
| 553 | _argument_list_create (void *ctx) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 554 | { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 555 | argument_list_t *list; |
| 556 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 557 | list = talloc (ctx, argument_list_t); |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 558 | list->head = NULL; |
| 559 | list->tail = NULL; |
| 560 | |
| 561 | return list; |
| 562 | } |
| 563 | |
| 564 | void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 565 | _argument_list_append (argument_list_t *list, token_list_t *argument) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 566 | { |
| 567 | argument_node_t *node; |
| 568 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 569 | node = talloc (list, argument_node_t); |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 570 | node->argument = argument; |
| 571 | |
| 572 | node->next = NULL; |
| 573 | |
| 574 | if (list->head == NULL) { |
| 575 | list->head = node; |
| 576 | } else { |
| 577 | list->tail->next = node; |
| 578 | } |
| 579 | |
| 580 | list->tail = node; |
| 581 | } |
| 582 | |
| 583 | int |
| 584 | _argument_list_length (argument_list_t *list) |
| 585 | { |
| 586 | int length = 0; |
| 587 | argument_node_t *node; |
| 588 | |
| 589 | if (list == NULL) |
| 590 | return 0; |
| 591 | |
| 592 | for (node = list->head; node; node = node->next) |
| 593 | length++; |
| 594 | |
| 595 | return length; |
| 596 | } |
| 597 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 598 | token_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 599 | _argument_list_member_at (argument_list_t *list, int index) |
| 600 | { |
| 601 | argument_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 602 | int i; |
| 603 | |
| 604 | if (list == NULL) |
| 605 | return NULL; |
| 606 | |
| 607 | node = list->head; |
| 608 | for (i = 0; i < index; i++) { |
| 609 | node = node->next; |
| 610 | if (node == NULL) |
| 611 | break; |
| 612 | } |
| 613 | |
| 614 | if (node) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 615 | return node->argument; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 616 | |
| 617 | return NULL; |
| 618 | } |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 619 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 620 | /* Note: This function talloc_steal()s the str pointer. */ |
| 621 | token_t * |
| 622 | _token_create_str (void *ctx, int type, char *str) |
| 623 | { |
| 624 | token_t *token; |
| 625 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 626 | token = talloc (ctx, token_t); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 627 | token->type = type; |
| 628 | token->value.str = talloc_steal (token, str); |
| 629 | |
| 630 | return token; |
| 631 | } |
| 632 | |
| 633 | token_t * |
| 634 | _token_create_ival (void *ctx, int type, int ival) |
| 635 | { |
| 636 | token_t *token; |
| 637 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 638 | token = talloc (ctx, token_t); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 639 | token->type = type; |
| 640 | token->value.ival = ival; |
| 641 | |
| 642 | return token; |
| 643 | } |
| 644 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 645 | token_list_t * |
| 646 | _token_list_create (void *ctx) |
| 647 | { |
| 648 | token_list_t *list; |
| 649 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 650 | list = talloc (ctx, token_list_t); |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 651 | list->head = NULL; |
| 652 | list->tail = NULL; |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 653 | list->non_space_tail = NULL; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 654 | |
| 655 | return list; |
| 656 | } |
| 657 | |
| 658 | void |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 659 | _token_list_append (token_list_t *list, token_t *token) |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 660 | { |
| 661 | token_node_t *node; |
| 662 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 663 | node = talloc (list, token_node_t); |
| 664 | node->token = talloc_reference (list, token); |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 665 | |
| 666 | node->next = NULL; |
| 667 | |
| 668 | if (list->head == NULL) { |
| 669 | list->head = node; |
| 670 | } else { |
| 671 | list->tail->next = node; |
| 672 | } |
| 673 | |
| 674 | list->tail = node; |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 675 | if (token->type != SPACE) |
| 676 | list->non_space_tail = node; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | void |
| 680 | _token_list_append_list (token_list_t *list, token_list_t *tail) |
| 681 | { |
Carl Worth | a65cf7b | 2010-05-27 11:55:36 -0700 | [diff] [blame] | 682 | if (tail == NULL || tail->head == NULL) |
| 683 | return; |
| 684 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 685 | if (list->head == NULL) { |
| 686 | list->head = tail->head; |
| 687 | } else { |
| 688 | list->tail->next = tail->head; |
| 689 | } |
| 690 | |
| 691 | list->tail = tail->tail; |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 692 | list->non_space_tail = tail->non_space_tail; |
| 693 | } |
| 694 | |
Carl Worth | d80dcaf | 2010-07-20 15:55:21 -0700 | [diff] [blame] | 695 | static token_list_t * |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 696 | _token_list_copy (void *ctx, token_list_t *other) |
| 697 | { |
| 698 | token_list_t *copy; |
| 699 | token_node_t *node; |
| 700 | |
| 701 | if (other == NULL) |
| 702 | return NULL; |
| 703 | |
| 704 | copy = _token_list_create (ctx); |
| 705 | for (node = other->head; node; node = node->next) |
| 706 | _token_list_append (copy, node->token); |
| 707 | |
| 708 | return copy; |
| 709 | } |
| 710 | |
Carl Worth | d80dcaf | 2010-07-20 15:55:21 -0700 | [diff] [blame] | 711 | static void |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 712 | _token_list_trim_trailing_space (token_list_t *list) |
| 713 | { |
| 714 | token_node_t *tail, *next; |
| 715 | |
| 716 | if (list->non_space_tail) { |
| 717 | tail = list->non_space_tail->next; |
| 718 | list->non_space_tail->next = NULL; |
| 719 | list->tail = list->non_space_tail; |
| 720 | |
| 721 | while (tail) { |
| 722 | next = tail->next; |
| 723 | talloc_free (tail); |
| 724 | tail = next; |
| 725 | } |
| 726 | } |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 727 | } |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 728 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 729 | static void |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 730 | _token_print (char **out, token_t *token) |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 731 | { |
| 732 | if (token->type < 256) { |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 733 | glcpp_printf (*out, "%c", token->type); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 734 | return; |
| 735 | } |
| 736 | |
| 737 | switch (token->type) { |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 738 | case INTEGER: |
Eric Anholt | 8605c29 | 2010-07-28 16:53:51 -0700 | [diff] [blame] | 739 | glcpp_printf (*out, "%" PRIiMAX, token->value.ival); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 740 | break; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 741 | case IDENTIFIER: |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 742 | case INTEGER_STRING: |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 743 | case OTHER: |
Kenneth Graunke | ca9e5fc | 2010-06-16 17:31:50 -0700 | [diff] [blame] | 744 | glcpp_print (*out, token->value.str); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 745 | break; |
| 746 | case SPACE: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 747 | glcpp_print (*out, " "); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 748 | break; |
| 749 | case LEFT_SHIFT: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 750 | glcpp_print (*out, "<<"); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 751 | break; |
| 752 | case RIGHT_SHIFT: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 753 | glcpp_print (*out, ">>"); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 754 | break; |
| 755 | case LESS_OR_EQUAL: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 756 | glcpp_print (*out, "<="); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 757 | break; |
| 758 | case GREATER_OR_EQUAL: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 759 | glcpp_print (*out, ">="); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 760 | break; |
| 761 | case EQUAL: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 762 | glcpp_print (*out, "=="); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 763 | break; |
| 764 | case NOT_EQUAL: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 765 | glcpp_print (*out, "!="); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 766 | break; |
| 767 | case AND: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 768 | glcpp_print (*out, "&&"); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 769 | break; |
| 770 | case OR: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 771 | glcpp_print (*out, "||"); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 772 | break; |
| 773 | case PASTE: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 774 | glcpp_print (*out, "##"); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 775 | break; |
Carl Worth | dd74900 | 2010-05-27 10:12:33 -0700 | [diff] [blame] | 776 | case COMMA_FINAL: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 777 | glcpp_print (*out, ","); |
Carl Worth | dd74900 | 2010-05-27 10:12:33 -0700 | [diff] [blame] | 778 | break; |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 779 | case PLACEHOLDER: |
| 780 | /* Nothing to print. */ |
| 781 | break; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 782 | default: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 783 | assert(!"Error: Don't know how to print token."); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 784 | break; |
| 785 | } |
| 786 | } |
| 787 | |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 788 | /* Return a new token (talloc()ed off of 'token') formed by pasting |
| 789 | * 'token' and 'other'. Note that this function may return 'token' or |
| 790 | * 'other' directly rather than allocating anything new. |
| 791 | * |
| 792 | * Caution: Only very cursory error-checking is performed to see if |
| 793 | * the final result is a valid single token. */ |
| 794 | static token_t * |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 795 | _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other) |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 796 | { |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 797 | token_t *combined = NULL; |
| 798 | |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 799 | /* Pasting a placeholder onto anything makes no change. */ |
| 800 | if (other->type == PLACEHOLDER) |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 801 | return token; |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 802 | |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 803 | /* When 'token' is a placeholder, just return 'other'. */ |
| 804 | if (token->type == PLACEHOLDER) |
| 805 | return other; |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 806 | |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 807 | /* A very few single-character punctuators can be combined |
| 808 | * with another to form a multi-character punctuator. */ |
| 809 | switch (token->type) { |
| 810 | case '<': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 811 | if (other->type == '<') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 812 | combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT); |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 813 | else if (other->type == '=') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 814 | combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 815 | break; |
| 816 | case '>': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 817 | if (other->type == '>') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 818 | combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT); |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 819 | else if (other->type == '=') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 820 | combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 821 | break; |
| 822 | case '=': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 823 | if (other->type == '=') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 824 | combined = _token_create_ival (token, EQUAL, EQUAL); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 825 | break; |
| 826 | case '!': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 827 | if (other->type == '=') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 828 | combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 829 | break; |
| 830 | case '&': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 831 | if (other->type == '&') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 832 | combined = _token_create_ival (token, AND, AND); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 833 | break; |
| 834 | case '|': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 835 | if (other->type == '|') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 836 | combined = _token_create_ival (token, OR, OR); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 837 | break; |
| 838 | } |
| 839 | |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 840 | if (combined != NULL) { |
| 841 | /* Inherit the location from the first token */ |
| 842 | combined->location = token->location; |
| 843 | return combined; |
| 844 | } |
| 845 | |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 846 | /* Two string-valued tokens can usually just be mashed |
| 847 | * together. |
| 848 | * |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 849 | * XXX: This isn't actually legitimate. Several things here |
| 850 | * should result in a diagnostic since the result cannot be a |
| 851 | * valid, single pre-processing token. For example, pasting |
| 852 | * "123" and "abc" is not legal, but we don't catch that |
| 853 | * here. */ |
| 854 | if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) && |
| 855 | (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING)) |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 856 | { |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 857 | char *str; |
| 858 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 859 | str = talloc_asprintf (token, "%s%s", token->value.str, |
| 860 | other->value.str); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 861 | combined = _token_create_str (token, token->type, str); |
| 862 | combined->location = token->location; |
| 863 | return combined; |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Kenneth Graunke | ca9e5fc | 2010-06-16 17:31:50 -0700 | [diff] [blame] | 866 | glcpp_error (&token->location, parser, ""); |
Kenneth Graunke | 33eaa3e | 2010-06-18 19:52:36 -0700 | [diff] [blame] | 867 | glcpp_print (parser->info_log, "Pasting \""); |
| 868 | _token_print (&parser->info_log, token); |
| 869 | glcpp_print (parser->info_log, "\" and \""); |
| 870 | _token_print (&parser->info_log, other); |
| 871 | glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n"); |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 872 | |
| 873 | return token; |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 876 | static void |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 877 | _token_list_print (glcpp_parser_t *parser, token_list_t *list) |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 878 | { |
| 879 | token_node_t *node; |
| 880 | |
| 881 | if (list == NULL) |
| 882 | return; |
| 883 | |
| 884 | for (node = list->head; node; node = node->next) |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 885 | _token_print (&parser->output, node->token); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 886 | } |
| 887 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 888 | void |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 889 | yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 890 | { |
Kenneth Graunke | f1e6c06 | 2010-06-17 12:03:25 -0700 | [diff] [blame] | 891 | glcpp_error(locp, parser, "%s", error); |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 892 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 893 | |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 894 | static void add_builtin_define(glcpp_parser_t *parser, |
| 895 | const char *name, int value) |
| 896 | { |
| 897 | token_t *tok; |
| 898 | token_list_t *list; |
| 899 | |
| 900 | tok = _token_create_ival (parser, INTEGER, value); |
| 901 | |
| 902 | list = _token_list_create(parser); |
| 903 | _token_list_append(list, tok); |
| 904 | _define_object_macro(parser, NULL, name, list); |
| 905 | |
| 906 | talloc_unlink(parser, tok); |
| 907 | } |
| 908 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 909 | glcpp_parser_t * |
Ian Romanick | 06143ea | 2010-06-30 16:27:22 -0700 | [diff] [blame] | 910 | glcpp_parser_create (const struct gl_extensions *extensions) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 911 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 912 | glcpp_parser_t *parser; |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 913 | int language_version; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 914 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 915 | parser = talloc (NULL, glcpp_parser_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 916 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 917 | glcpp_lex_init_extra (parser, &parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 918 | parser->defines = hash_table_ctor (32, hash_table_string_hash, |
| 919 | hash_table_string_compare); |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 920 | parser->active = NULL; |
Carl Worth | a771a40 | 2010-06-01 11:20:18 -0700 | [diff] [blame] | 921 | parser->lexing_if = 0; |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 922 | parser->space_tokens = 1; |
Carl Worth | 95951ea | 2010-05-26 15:57:10 -0700 | [diff] [blame] | 923 | parser->newline_as_space = 0; |
| 924 | parser->in_control_line = 0; |
| 925 | parser->paren_count = 0; |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame] | 926 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 927 | parser->skip_stack = NULL; |
| 928 | |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 929 | parser->lex_from_list = NULL; |
| 930 | parser->lex_from_node = NULL; |
| 931 | |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 932 | parser->output = talloc_strdup(parser, ""); |
Kenneth Graunke | 33eaa3e | 2010-06-18 19:52:36 -0700 | [diff] [blame] | 933 | parser->info_log = talloc_strdup(parser, ""); |
Kenneth Graunke | 1b85c46 | 2010-06-21 13:55:12 -0700 | [diff] [blame] | 934 | parser->error = 0; |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 935 | |
Ian Romanick | 2d12236 | 2010-06-30 16:03:19 -0700 | [diff] [blame] | 936 | /* Add pre-defined macros. */ |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 937 | add_builtin_define(parser, "GL_ARB_draw_buffers", 1); |
| 938 | add_builtin_define(parser, "GL_ARB_texture_rectangle", 1); |
Ian Romanick | 2d12236 | 2010-06-30 16:03:19 -0700 | [diff] [blame] | 939 | |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 940 | if (extensions != NULL) { |
| 941 | if (extensions->EXT_texture_array) { |
| 942 | add_builtin_define(parser, "GL_EXT_texture_array", 1); |
| 943 | } |
Ian Romanick | 2d12236 | 2010-06-30 16:03:19 -0700 | [diff] [blame] | 944 | |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 945 | if (extensions->ARB_fragment_coord_conventions) |
| 946 | add_builtin_define(parser, "GL_ARB_fragment_coord_conventions", |
| 947 | 1); |
Ian Romanick | 06143ea | 2010-06-30 16:27:22 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 950 | language_version = 110; |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 951 | add_builtin_define(parser, "__VERSION__", language_version); |
Ian Romanick | 2d12236 | 2010-06-30 16:03:19 -0700 | [diff] [blame] | 952 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 953 | return parser; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | int |
| 957 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 958 | { |
| 959 | return yyparse (parser); |
| 960 | } |
| 961 | |
| 962 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 963 | glcpp_parser_destroy (glcpp_parser_t *parser) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 964 | { |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 965 | if (parser->skip_stack) |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 966 | glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n"); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 967 | glcpp_lex_destroy (parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 968 | hash_table_dtor (parser->defines); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 969 | talloc_free (parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 970 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 971 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 972 | typedef enum function_status |
| 973 | { |
| 974 | FUNCTION_STATUS_SUCCESS, |
| 975 | FUNCTION_NOT_A_FUNCTION, |
| 976 | FUNCTION_UNBALANCED_PARENTHESES |
| 977 | } function_status_t; |
| 978 | |
| 979 | /* Find a set of function-like macro arguments by looking for a |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 980 | * balanced set of parentheses. |
| 981 | * |
| 982 | * When called, 'node' should be the opening-parenthesis token, (or |
| 983 | * perhaps preceeding SPACE tokens). Upon successful return *last will |
| 984 | * be the last consumed node, (corresponding to the closing right |
| 985 | * parenthesis). |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 986 | * |
| 987 | * Return values: |
| 988 | * |
| 989 | * FUNCTION_STATUS_SUCCESS: |
| 990 | * |
| 991 | * Successfully parsed a set of function arguments. |
| 992 | * |
| 993 | * FUNCTION_NOT_A_FUNCTION: |
| 994 | * |
| 995 | * Macro name not followed by a '('. This is not an error, but |
| 996 | * simply that the macro name should be treated as a non-macro. |
| 997 | * |
Carl Worth | 14c98a5 | 2010-06-02 15:49:54 -0700 | [diff] [blame] | 998 | * FUNCTION_UNBALANCED_PARENTHESES |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 999 | * |
| 1000 | * Macro name is not followed by a balanced set of parentheses. |
| 1001 | */ |
| 1002 | static function_status_t |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1003 | _arguments_parse (argument_list_t *arguments, |
| 1004 | token_node_t *node, |
| 1005 | token_node_t **last) |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1006 | { |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1007 | token_list_t *argument; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1008 | int paren_count; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1009 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1010 | node = node->next; |
| 1011 | |
| 1012 | /* Ignore whitespace before first parenthesis. */ |
| 1013 | while (node && node->token->type == SPACE) |
| 1014 | node = node->next; |
| 1015 | |
| 1016 | if (node == NULL || node->token->type != '(') |
| 1017 | return FUNCTION_NOT_A_FUNCTION; |
| 1018 | |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame] | 1019 | node = node->next; |
| 1020 | |
Carl Worth | a19297b | 2010-05-27 13:29:19 -0700 | [diff] [blame] | 1021 | argument = _token_list_create (arguments); |
| 1022 | _argument_list_append (arguments, argument); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1023 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1024 | for (paren_count = 1; node; node = node->next) { |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1025 | if (node->token->type == '(') |
| 1026 | { |
| 1027 | paren_count++; |
| 1028 | } |
| 1029 | else if (node->token->type == ')') |
| 1030 | { |
| 1031 | paren_count--; |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1032 | if (paren_count == 0) |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 1033 | break; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1034 | } |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame] | 1035 | |
| 1036 | if (node->token->type == ',' && |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1037 | paren_count == 1) |
| 1038 | { |
Carl Worth | a19297b | 2010-05-27 13:29:19 -0700 | [diff] [blame] | 1039 | _token_list_trim_trailing_space (argument); |
| 1040 | argument = _token_list_create (arguments); |
| 1041 | _argument_list_append (arguments, argument); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1042 | } |
| 1043 | else { |
Carl Worth | a19297b | 2010-05-27 13:29:19 -0700 | [diff] [blame] | 1044 | if (argument->head == NULL) { |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 1045 | /* Don't treat initial whitespace as |
| 1046 | * part of the arguement. */ |
| 1047 | if (node->token->type == SPACE) |
| 1048 | continue; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1049 | } |
| 1050 | _token_list_append (argument, node->token); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1051 | } |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 1052 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1053 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1054 | if (paren_count) |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1055 | return FUNCTION_UNBALANCED_PARENTHESES; |
| 1056 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1057 | *last = node; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1058 | |
| 1059 | return FUNCTION_STATUS_SUCCESS; |
| 1060 | } |
| 1061 | |
Carl Worth | e1acbfc | 2010-07-20 16:44:03 -0700 | [diff] [blame] | 1062 | static token_list_t * |
| 1063 | _token_list_create_with_one_space (void *ctx) |
| 1064 | { |
| 1065 | token_list_t *list; |
| 1066 | token_t *space; |
| 1067 | |
| 1068 | list = _token_list_create (ctx); |
| 1069 | space = _token_create_ival (list, SPACE, SPACE); |
| 1070 | _token_list_append (list, space); |
| 1071 | |
| 1072 | return list; |
| 1073 | } |
| 1074 | |
Kenneth Graunke | 16b4eed | 2010-08-04 16:10:03 -0700 | [diff] [blame^] | 1075 | static void |
| 1076 | _glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list) |
| 1077 | { |
| 1078 | token_list_t *expanded; |
| 1079 | token_t *token; |
| 1080 | |
| 1081 | expanded = _token_list_create (parser); |
| 1082 | token = _token_create_ival (parser, type, type); |
| 1083 | _token_list_append (expanded, token); |
| 1084 | _glcpp_parser_expand_token_list (parser, list); |
| 1085 | _token_list_append_list (expanded, list); |
| 1086 | glcpp_parser_lex_from (parser, expanded); |
| 1087 | } |
| 1088 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1089 | /* This is a helper function that's essentially part of the |
| 1090 | * implementation of _glcpp_parser_expand_node. It shouldn't be called |
| 1091 | * except for by that function. |
| 1092 | * |
| 1093 | * Returns NULL if node is a simple token with no expansion, (that is, |
| 1094 | * although 'node' corresponds to an identifier defined as a |
| 1095 | * function-like macro, it is not followed with a parenthesized |
| 1096 | * argument list). |
| 1097 | * |
| 1098 | * Compute the complete expansion of node (which is a function-like |
| 1099 | * macro) and subsequent nodes which are arguments. |
| 1100 | * |
| 1101 | * Returns the token list that results from the expansion and sets |
| 1102 | * *last to the last node in the list that was consumed by the |
Carl Worth | fbe4240 | 2010-07-22 16:36:04 -0700 | [diff] [blame] | 1103 | * expansion. Specifically, *last will be set as follows: as the |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1104 | * token of the closing right parenthesis. |
| 1105 | */ |
| 1106 | static token_list_t * |
| 1107 | _glcpp_parser_expand_function (glcpp_parser_t *parser, |
| 1108 | token_node_t *node, |
| 1109 | token_node_t **last) |
| 1110 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1111 | { |
| 1112 | macro_t *macro; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1113 | const char *identifier; |
| 1114 | argument_list_t *arguments; |
| 1115 | function_status_t status; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1116 | token_list_t *substituted; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1117 | int parameter_index; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1118 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1119 | identifier = node->token->value.str; |
| 1120 | |
| 1121 | macro = hash_table_find (parser->defines, identifier); |
| 1122 | |
| 1123 | assert (macro->is_function); |
| 1124 | |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1125 | arguments = _argument_list_create (parser); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1126 | status = _arguments_parse (arguments, node, last); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1127 | |
| 1128 | switch (status) { |
| 1129 | case FUNCTION_STATUS_SUCCESS: |
| 1130 | break; |
| 1131 | case FUNCTION_NOT_A_FUNCTION: |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1132 | return NULL; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1133 | case FUNCTION_UNBALANCED_PARENTHESES: |
Kenneth Graunke | ca9e5fc | 2010-06-16 17:31:50 -0700 | [diff] [blame] | 1134 | glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1135 | return NULL; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Carl Worth | e1acbfc | 2010-07-20 16:44:03 -0700 | [diff] [blame] | 1138 | /* Replace a macro defined as empty with a SPACE token. */ |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1139 | if (macro->replacements == NULL) { |
| 1140 | talloc_free (arguments); |
Carl Worth | e1acbfc | 2010-07-20 16:44:03 -0700 | [diff] [blame] | 1141 | return _token_list_create_with_one_space (parser); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
Carl Worth | a19297b | 2010-05-27 13:29:19 -0700 | [diff] [blame] | 1144 | if (! ((_argument_list_length (arguments) == |
| 1145 | _string_list_length (macro->parameters)) || |
| 1146 | (_string_list_length (macro->parameters) == 0 && |
| 1147 | _argument_list_length (arguments) == 1 && |
| 1148 | arguments->head->argument->head == NULL))) |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1149 | { |
Kenneth Graunke | ca9e5fc | 2010-06-16 17:31:50 -0700 | [diff] [blame] | 1150 | glcpp_error (&node->token->location, parser, |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 1151 | "Error: macro %s invoked with %d arguments (expected %d)\n", |
| 1152 | identifier, |
| 1153 | _argument_list_length (arguments), |
| 1154 | _string_list_length (macro->parameters)); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1155 | return NULL; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1158 | /* Perform argument substitution on the replacement list. */ |
| 1159 | substituted = _token_list_create (arguments); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1160 | |
Carl Worth | ce540f2 | 2010-05-26 08:25:44 -0700 | [diff] [blame] | 1161 | for (node = macro->replacements->head; node; node = node->next) |
| 1162 | { |
| 1163 | if (node->token->type == IDENTIFIER && |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1164 | _string_list_contains (macro->parameters, |
Carl Worth | ce540f2 | 2010-05-26 08:25:44 -0700 | [diff] [blame] | 1165 | node->token->value.str, |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1166 | ¶meter_index)) |
| 1167 | { |
| 1168 | token_list_t *argument; |
| 1169 | argument = _argument_list_member_at (arguments, |
| 1170 | parameter_index); |
Carl Worth | d5cd403 | 2010-05-26 08:09:29 -0700 | [diff] [blame] | 1171 | /* Before substituting, we expand the argument |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 1172 | * tokens, or append a placeholder token for |
| 1173 | * an empty argument. */ |
| 1174 | if (argument->head) { |
Carl Worth | fbe4240 | 2010-07-22 16:36:04 -0700 | [diff] [blame] | 1175 | token_list_t *expanded_argument; |
| 1176 | expanded_argument = _token_list_copy (parser, |
| 1177 | argument); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1178 | _glcpp_parser_expand_token_list (parser, |
Carl Worth | fbe4240 | 2010-07-22 16:36:04 -0700 | [diff] [blame] | 1179 | expanded_argument); |
| 1180 | _token_list_append_list (substituted, |
| 1181 | expanded_argument); |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 1182 | } else { |
| 1183 | token_t *new_token; |
| 1184 | |
| 1185 | new_token = _token_create_ival (substituted, |
| 1186 | PLACEHOLDER, |
| 1187 | PLACEHOLDER); |
| 1188 | _token_list_append (substituted, new_token); |
| 1189 | } |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1190 | } else { |
Carl Worth | ce540f2 | 2010-05-26 08:25:44 -0700 | [diff] [blame] | 1191 | _token_list_append (substituted, node->token); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1195 | /* After argument substitution, and before further expansion |
| 1196 | * below, implement token pasting. */ |
| 1197 | |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 1198 | _token_list_trim_trailing_space (substituted); |
| 1199 | |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1200 | node = substituted->head; |
| 1201 | while (node) |
| 1202 | { |
| 1203 | token_node_t *next_non_space; |
| 1204 | |
| 1205 | /* Look ahead for a PASTE token, skipping space. */ |
| 1206 | next_non_space = node->next; |
| 1207 | while (next_non_space && next_non_space->token->type == SPACE) |
| 1208 | next_non_space = next_non_space->next; |
| 1209 | |
| 1210 | if (next_non_space == NULL) |
| 1211 | break; |
| 1212 | |
| 1213 | if (next_non_space->token->type != PASTE) { |
| 1214 | node = next_non_space; |
| 1215 | continue; |
| 1216 | } |
| 1217 | |
| 1218 | /* Now find the next non-space token after the PASTE. */ |
| 1219 | next_non_space = next_non_space->next; |
| 1220 | while (next_non_space && next_non_space->token->type == SPACE) |
| 1221 | next_non_space = next_non_space->next; |
| 1222 | |
| 1223 | if (next_non_space == NULL) { |
Kenneth Graunke | ca9e5fc | 2010-06-16 17:31:50 -0700 | [diff] [blame] | 1224 | yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n"); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1225 | return NULL; |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 1228 | node->token = _token_paste (parser, node->token, next_non_space->token); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1229 | node->next = next_non_space->next; |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 1230 | if (next_non_space == substituted->tail) |
| 1231 | substituted->tail = node; |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1232 | |
| 1233 | node = node->next; |
| 1234 | } |
| 1235 | |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 1236 | substituted->non_space_tail = substituted->tail; |
| 1237 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1238 | return substituted; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1241 | /* Compute the complete expansion of node, (and subsequent nodes after |
| 1242 | * 'node' in the case that 'node' is a function-like macro and |
| 1243 | * subsequent nodes are arguments). |
| 1244 | * |
| 1245 | * Returns NULL if node is a simple token with no expansion. |
| 1246 | * |
| 1247 | * Otherwise, returns the token list that results from the expansion |
| 1248 | * and sets *last to the last node in the list that was consumed by |
Carl Worth | e1acbfc | 2010-07-20 16:44:03 -0700 | [diff] [blame] | 1249 | * the expansion. Specifically, *last will be set as follows: |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1250 | * |
| 1251 | * As 'node' in the case of object-like macro expansion. |
| 1252 | * |
| 1253 | * As the token of the closing right parenthesis in the case of |
| 1254 | * function-like macro expansion. |
| 1255 | */ |
| 1256 | static token_list_t * |
| 1257 | _glcpp_parser_expand_node (glcpp_parser_t *parser, |
| 1258 | token_node_t *node, |
| 1259 | token_node_t **last) |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1260 | { |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1261 | token_t *token = node->token; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1262 | const char *identifier; |
| 1263 | macro_t *macro; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1264 | |
| 1265 | /* We only expand identifiers */ |
| 1266 | if (token->type != IDENTIFIER) { |
| 1267 | /* We change any COMMA into a COMMA_FINAL to prevent |
| 1268 | * it being mistaken for an argument separator |
| 1269 | * later. */ |
| 1270 | if (token->type == ',') { |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1271 | token->type = COMMA_FINAL; |
| 1272 | token->value.ival = COMMA_FINAL; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1273 | } |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1274 | |
| 1275 | return NULL; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | /* Look up this identifier in the hash table. */ |
| 1279 | identifier = token->value.str; |
| 1280 | macro = hash_table_find (parser->defines, identifier); |
| 1281 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1282 | /* Not a macro, so no expansion needed. */ |
| 1283 | if (macro == NULL) |
| 1284 | return NULL; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1285 | |
| 1286 | /* Finally, don't expand this macro if we're already actively |
| 1287 | * expanding it, (to avoid infinite recursion). */ |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1288 | if (_active_list_contains (parser->active, identifier)) { |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1289 | /* We change the token type here from IDENTIFIER to |
| 1290 | * OTHER to prevent any future expansion of this |
| 1291 | * unexpanded token. */ |
| 1292 | char *str; |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1293 | token_list_t *expansion; |
| 1294 | token_t *final; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1295 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 1296 | str = talloc_strdup (parser, token->value.str); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1297 | final = _token_create_str (parser, OTHER, str); |
| 1298 | expansion = _token_list_create (parser); |
| 1299 | _token_list_append (expansion, final); |
| 1300 | *last = node; |
| 1301 | return expansion; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1304 | if (! macro->is_function) |
| 1305 | { |
| 1306 | *last = node; |
| 1307 | |
Carl Worth | e1acbfc | 2010-07-20 16:44:03 -0700 | [diff] [blame] | 1308 | /* Replace a macro defined as empty with a SPACE token. */ |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1309 | if (macro->replacements == NULL) |
Carl Worth | e1acbfc | 2010-07-20 16:44:03 -0700 | [diff] [blame] | 1310 | return _token_list_create_with_one_space (parser); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1311 | |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1312 | return _token_list_copy (parser, macro->replacements); |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1313 | } |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1314 | |
| 1315 | return _glcpp_parser_expand_function (parser, node, last); |
| 1316 | } |
| 1317 | |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1318 | /* Push a new identifier onto the active list, returning the new list. |
| 1319 | * |
| 1320 | * Here, 'marker' is the token node that appears in the list after the |
| 1321 | * expansion of 'identifier'. That is, when the list iterator begins |
| 1322 | * examinging 'marker', then it is time to pop this node from the |
| 1323 | * active stack. |
| 1324 | */ |
| 1325 | active_list_t * |
| 1326 | _active_list_push (active_list_t *list, |
| 1327 | const char *identifier, |
| 1328 | token_node_t *marker) |
| 1329 | { |
| 1330 | active_list_t *node; |
| 1331 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 1332 | node = talloc (list, active_list_t); |
| 1333 | node->identifier = talloc_strdup (node, identifier); |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1334 | node->marker = marker; |
| 1335 | node->next = list; |
| 1336 | |
| 1337 | return node; |
| 1338 | } |
| 1339 | |
| 1340 | active_list_t * |
| 1341 | _active_list_pop (active_list_t *list) |
| 1342 | { |
| 1343 | active_list_t *node = list; |
| 1344 | |
| 1345 | if (node == NULL) |
| 1346 | return NULL; |
| 1347 | |
| 1348 | node = list->next; |
| 1349 | talloc_free (list); |
| 1350 | |
| 1351 | return node; |
| 1352 | } |
| 1353 | |
| 1354 | int |
| 1355 | _active_list_contains (active_list_t *list, const char *identifier) |
| 1356 | { |
| 1357 | active_list_t *node; |
| 1358 | |
| 1359 | if (list == NULL) |
| 1360 | return 0; |
| 1361 | |
| 1362 | for (node = list; node; node = node->next) |
| 1363 | if (strcmp (node->identifier, identifier) == 0) |
| 1364 | return 1; |
| 1365 | |
| 1366 | return 0; |
| 1367 | } |
| 1368 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1369 | /* Walk over the token list replacing nodes with their expansion. |
| 1370 | * Whenever nodes are expanded the walking will walk over the new |
| 1371 | * nodes, continuing to expand as necessary. The results are placed in |
| 1372 | * 'list' itself; |
| 1373 | */ |
| 1374 | static void |
| 1375 | _glcpp_parser_expand_token_list (glcpp_parser_t *parser, |
| 1376 | token_list_t *list) |
| 1377 | { |
| 1378 | token_node_t *node_prev; |
Carl Worth | c42e640 | 2010-06-22 15:51:34 -0700 | [diff] [blame] | 1379 | token_node_t *node, *last = NULL; |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1380 | token_list_t *expansion; |
| 1381 | |
| 1382 | if (list == NULL) |
| 1383 | return; |
| 1384 | |
| 1385 | _token_list_trim_trailing_space (list); |
| 1386 | |
| 1387 | node_prev = NULL; |
| 1388 | node = list->head; |
| 1389 | |
| 1390 | while (node) { |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1391 | |
| 1392 | while (parser->active && parser->active->marker == node) |
| 1393 | parser->active = _active_list_pop (parser->active); |
| 1394 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1395 | /* Find the expansion for node, which will replace all |
| 1396 | * nodes from node to last, inclusive. */ |
| 1397 | expansion = _glcpp_parser_expand_node (parser, node, &last); |
| 1398 | if (expansion) { |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1399 | token_node_t *n; |
| 1400 | |
| 1401 | for (n = node; n != last->next; n = n->next) |
| 1402 | while (parser->active && |
| 1403 | parser->active->marker == n) |
| 1404 | { |
| 1405 | parser->active = _active_list_pop (parser->active); |
| 1406 | } |
| 1407 | |
| 1408 | parser->active = _active_list_push (parser->active, |
| 1409 | node->token->value.str, |
| 1410 | last->next); |
| 1411 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1412 | /* Splice expansion into list, supporting a |
| 1413 | * simple deletion if the expansion is |
| 1414 | * empty. */ |
| 1415 | if (expansion->head) { |
| 1416 | if (node_prev) |
| 1417 | node_prev->next = expansion->head; |
| 1418 | else |
| 1419 | list->head = expansion->head; |
| 1420 | expansion->tail->next = last->next; |
| 1421 | if (last == list->tail) |
| 1422 | list->tail = expansion->tail; |
| 1423 | } else { |
| 1424 | if (node_prev) |
| 1425 | node_prev->next = last->next; |
| 1426 | else |
| 1427 | list->head = last->next; |
| 1428 | if (last == list->tail) |
Kenneth Graunke | 0656f6b | 2010-06-16 11:56:36 -0700 | [diff] [blame] | 1429 | list->tail = NULL; |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1430 | } |
| 1431 | } else { |
| 1432 | node_prev = node; |
| 1433 | } |
| 1434 | node = node_prev ? node_prev->next : list->head; |
| 1435 | } |
| 1436 | |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1437 | while (parser->active) |
| 1438 | parser->active = _active_list_pop (parser->active); |
| 1439 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1440 | list->non_space_tail = list->tail; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1443 | void |
| 1444 | _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser, |
| 1445 | token_list_t *list) |
| 1446 | { |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1447 | if (list == NULL) |
| 1448 | return; |
| 1449 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1450 | _glcpp_parser_expand_token_list (parser, list); |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 1451 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1452 | _token_list_trim_trailing_space (list); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1453 | |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 1454 | _token_list_print (parser, list); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1455 | } |
| 1456 | |
Carl Worth | d80dcaf | 2010-07-20 15:55:21 -0700 | [diff] [blame] | 1457 | static void |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1458 | _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc, |
| 1459 | const char *identifier) |
Kenneth Graunke | 2ab0b13 | 2010-06-04 14:53:58 -0700 | [diff] [blame] | 1460 | { |
| 1461 | /* According to the GLSL specification, macro names starting with "__" |
| 1462 | * or "GL_" are reserved for future use. So, don't allow them. |
| 1463 | */ |
| 1464 | if (strncmp(identifier, "__", 2) == 0) { |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1465 | glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n"); |
Kenneth Graunke | 2ab0b13 | 2010-06-04 14:53:58 -0700 | [diff] [blame] | 1466 | } |
| 1467 | if (strncmp(identifier, "GL_", 3) == 0) { |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1468 | glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n"); |
Kenneth Graunke | 2ab0b13 | 2010-06-04 14:53:58 -0700 | [diff] [blame] | 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1473 | _define_object_macro (glcpp_parser_t *parser, |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1474 | YYLTYPE *loc, |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1475 | const char *identifier, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 1476 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1477 | { |
| 1478 | macro_t *macro; |
| 1479 | |
Ian Romanick | 2d12236 | 2010-06-30 16:03:19 -0700 | [diff] [blame] | 1480 | if (loc != NULL) |
| 1481 | _check_for_reserved_macro_name(parser, loc, identifier); |
Kenneth Graunke | 2ab0b13 | 2010-06-04 14:53:58 -0700 | [diff] [blame] | 1482 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 1483 | macro = talloc (parser, macro_t); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1484 | |
| 1485 | macro->is_function = 0; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 1486 | macro->parameters = NULL; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 1487 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1488 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1489 | |
| 1490 | hash_table_insert (parser->defines, macro, identifier); |
| 1491 | } |
| 1492 | |
| 1493 | void |
| 1494 | _define_function_macro (glcpp_parser_t *parser, |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1495 | YYLTYPE *loc, |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1496 | const char *identifier, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 1497 | string_list_t *parameters, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 1498 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1499 | { |
| 1500 | macro_t *macro; |
| 1501 | |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1502 | _check_for_reserved_macro_name(parser, loc, identifier); |
Kenneth Graunke | 2ab0b13 | 2010-06-04 14:53:58 -0700 | [diff] [blame] | 1503 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 1504 | macro = talloc (parser, macro_t); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1505 | |
| 1506 | macro->is_function = 1; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 1507 | macro->parameters = talloc_steal (macro, parameters); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 1508 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1509 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1510 | |
| 1511 | hash_table_insert (parser->defines, macro, identifier); |
| 1512 | } |
| 1513 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 1514 | static int |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 1515 | glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser) |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 1516 | { |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 1517 | token_node_t *node; |
| 1518 | int ret; |
| 1519 | |
Carl Worth | 95951ea | 2010-05-26 15:57:10 -0700 | [diff] [blame] | 1520 | if (parser->lex_from_list == NULL) { |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 1521 | ret = glcpp_lex (yylval, yylloc, parser->scanner); |
Carl Worth | 95951ea | 2010-05-26 15:57:10 -0700 | [diff] [blame] | 1522 | |
| 1523 | /* XXX: This ugly block of code exists for the sole |
| 1524 | * purpose of converting a NEWLINE token into a SPACE |
| 1525 | * token, but only in the case where we have seen a |
| 1526 | * function-like macro name, but have not yet seen its |
| 1527 | * closing parenthesis. |
| 1528 | * |
| 1529 | * There's perhaps a more compact way to do this with |
| 1530 | * mid-rule actions in the grammar. |
| 1531 | * |
| 1532 | * I'm definitely not pleased with the complexity of |
| 1533 | * this code here. |
| 1534 | */ |
| 1535 | if (parser->newline_as_space) |
| 1536 | { |
| 1537 | if (ret == '(') { |
| 1538 | parser->paren_count++; |
| 1539 | } else if (ret == ')') { |
| 1540 | parser->paren_count--; |
| 1541 | if (parser->paren_count == 0) |
| 1542 | parser->newline_as_space = 0; |
| 1543 | } else if (ret == NEWLINE) { |
| 1544 | ret = SPACE; |
| 1545 | } else if (ret != SPACE) { |
| 1546 | if (parser->paren_count == 0) |
| 1547 | parser->newline_as_space = 0; |
| 1548 | } |
| 1549 | } |
| 1550 | else if (parser->in_control_line) |
| 1551 | { |
| 1552 | if (ret == NEWLINE) |
| 1553 | parser->in_control_line = 0; |
| 1554 | } |
| 1555 | else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC || |
| 1556 | ret == HASH_UNDEF || ret == HASH_IF || |
| 1557 | ret == HASH_IFDEF || ret == HASH_IFNDEF || |
| 1558 | ret == HASH_ELIF || ret == HASH_ELSE || |
| 1559 | ret == HASH_ENDIF || ret == HASH) |
| 1560 | { |
| 1561 | parser->in_control_line = 1; |
| 1562 | } |
| 1563 | else if (ret == IDENTIFIER) |
| 1564 | { |
| 1565 | macro_t *macro; |
| 1566 | macro = hash_table_find (parser->defines, |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 1567 | yylval->str); |
Carl Worth | 95951ea | 2010-05-26 15:57:10 -0700 | [diff] [blame] | 1568 | if (macro && macro->is_function) { |
| 1569 | parser->newline_as_space = 1; |
| 1570 | parser->paren_count = 0; |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | return ret; |
| 1575 | } |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 1576 | |
| 1577 | node = parser->lex_from_node; |
| 1578 | |
| 1579 | if (node == NULL) { |
| 1580 | talloc_free (parser->lex_from_list); |
| 1581 | parser->lex_from_list = NULL; |
| 1582 | return NEWLINE; |
| 1583 | } |
| 1584 | |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 1585 | *yylval = node->token->value; |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 1586 | ret = node->token->type; |
| 1587 | |
| 1588 | parser->lex_from_node = node->next; |
| 1589 | |
| 1590 | return ret; |
| 1591 | } |
| 1592 | |
| 1593 | static void |
| 1594 | glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list) |
| 1595 | { |
| 1596 | token_node_t *node; |
| 1597 | |
| 1598 | assert (parser->lex_from_list == NULL); |
| 1599 | |
| 1600 | /* Copy list, eliminating any space tokens. */ |
| 1601 | parser->lex_from_list = _token_list_create (parser); |
| 1602 | |
| 1603 | for (node = list->head; node; node = node->next) { |
| 1604 | if (node->token->type == SPACE) |
| 1605 | continue; |
| 1606 | _token_list_append (parser->lex_from_list, node->token); |
| 1607 | } |
| 1608 | |
| 1609 | talloc_free (list); |
| 1610 | |
| 1611 | parser->lex_from_node = parser->lex_from_list->head; |
| 1612 | |
| 1613 | /* It's possible the list consisted of nothing but whitespace. */ |
| 1614 | if (parser->lex_from_node == NULL) { |
| 1615 | talloc_free (parser->lex_from_list); |
| 1616 | parser->lex_from_list = NULL; |
| 1617 | } |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 1618 | } |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1619 | |
| 1620 | static void |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 1621 | _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc, |
| 1622 | int condition) |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1623 | { |
| 1624 | skip_type_t current = SKIP_NO_SKIP; |
| 1625 | skip_node_t *node; |
| 1626 | |
| 1627 | if (parser->skip_stack) |
| 1628 | current = parser->skip_stack->type; |
| 1629 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 1630 | node = talloc (parser, skip_node_t); |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 1631 | node->loc = *loc; |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1632 | |
| 1633 | if (current == SKIP_NO_SKIP) { |
| 1634 | if (condition) |
| 1635 | node->type = SKIP_NO_SKIP; |
| 1636 | else |
| 1637 | node->type = SKIP_TO_ELSE; |
| 1638 | } else { |
| 1639 | node->type = SKIP_TO_ENDIF; |
| 1640 | } |
| 1641 | |
| 1642 | node->next = parser->skip_stack; |
| 1643 | parser->skip_stack = node; |
| 1644 | } |
| 1645 | |
| 1646 | static void |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 1647 | _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc, |
| 1648 | const char *type, int condition) |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1649 | { |
| 1650 | if (parser->skip_stack == NULL) { |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 1651 | glcpp_error (loc, parser, "%s without #if\n", type); |
Kenneth Graunke | e8e93a4 | 2010-06-17 12:58:54 -0700 | [diff] [blame] | 1652 | return; |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | if (parser->skip_stack->type == SKIP_TO_ELSE) { |
| 1656 | if (condition) |
| 1657 | parser->skip_stack->type = SKIP_NO_SKIP; |
| 1658 | } else { |
| 1659 | parser->skip_stack->type = SKIP_TO_ENDIF; |
| 1660 | } |
| 1661 | } |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 1662 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1663 | static void |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 1664 | _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc) |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1665 | { |
| 1666 | skip_node_t *node; |
| 1667 | |
| 1668 | if (parser->skip_stack == NULL) { |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 1669 | glcpp_error (loc, parser, "#endif without #if\n"); |
Kenneth Graunke | e8e93a4 | 2010-06-17 12:58:54 -0700 | [diff] [blame] | 1670 | return; |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | node = parser->skip_stack; |
| 1674 | parser->skip_stack = node->next; |
| 1675 | talloc_free (node); |
| 1676 | } |