| 1 | /* Lists of symbols for Bison |
| 2 | |
| 3 | Copyright (C) 2002, 2005-2007, 2009-2012 Free Software Foundation, |
| 4 | Inc. |
| 5 | |
| 6 | This file is part of Bison, the GNU Compiler Compiler. |
| 7 | |
| 8 | This program is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #include <config.h> |
| 22 | #include "system.h" |
| 23 | |
| 24 | #include "complain.h" |
| 25 | #include "symlist.h" |
| 26 | |
| 27 | /*--------------------------------------. |
| 28 | | Create a list containing SYM at LOC. | |
| 29 | `--------------------------------------*/ |
| 30 | |
| 31 | symbol_list * |
| 32 | symbol_list_sym_new (symbol *sym, location loc) |
| 33 | { |
| 34 | symbol_list *res = xmalloc (sizeof *res); |
| 35 | |
| 36 | res->content_type = SYMLIST_SYMBOL; |
| 37 | res->content.sym = sym; |
| 38 | res->location = res->sym_loc = loc; |
| 39 | |
| 40 | res->midrule = NULL; |
| 41 | res->midrule_parent_rule = NULL; |
| 42 | res->midrule_parent_rhs_index = 0; |
| 43 | |
| 44 | code_props_none_init (&res->action_props); |
| 45 | |
| 46 | res->ruleprec = NULL; |
| 47 | res->dprec = 0; |
| 48 | res->merger = 0; |
| 49 | |
| 50 | res->named_ref = NULL; |
| 51 | |
| 52 | res->next = NULL; |
| 53 | |
| 54 | return res; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /*--------------------------------------------. |
| 59 | | Create a list containing TYPE_NAME at LOC. | |
| 60 | `--------------------------------------------*/ |
| 61 | |
| 62 | symbol_list * |
| 63 | symbol_list_type_new (uniqstr type_name, location loc) |
| 64 | { |
| 65 | symbol_list *res = xmalloc (sizeof *res); |
| 66 | |
| 67 | res->content_type = SYMLIST_TYPE; |
| 68 | res->content.sem_type = xmalloc (sizeof (semantic_type)); |
| 69 | res->content.sem_type->tag = type_name; |
| 70 | res->content.sem_type->location = loc; |
| 71 | res->content.sem_type->status = undeclared; |
| 72 | |
| 73 | res->location = res->sym_loc = loc; |
| 74 | res->named_ref = NULL; |
| 75 | res->next = NULL; |
| 76 | |
| 77 | return res; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /*-----------------------------------------------------------------------. |
| 82 | | Print this list, for which every content_type must be SYMLIST_SYMBOL. | |
| 83 | `-----------------------------------------------------------------------*/ |
| 84 | |
| 85 | void |
| 86 | symbol_list_syms_print (const symbol_list *l, FILE *f) |
| 87 | { |
| 88 | for (/* Nothing. */; l && l->content.sym; l = l->next) |
| 89 | { |
| 90 | symbol_print (l->content.sym, f); |
| 91 | fprintf (stderr, l->action_props.is_value_used ? " used" : " unused"); |
| 92 | if (l && l->content.sym) |
| 93 | fprintf (f, ", "); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | |
| 98 | /*---------------------------. |
| 99 | | Prepend NODE to the LIST. | |
| 100 | `---------------------------*/ |
| 101 | |
| 102 | symbol_list * |
| 103 | symbol_list_prepend (symbol_list *list, symbol_list *node) |
| 104 | { |
| 105 | node->next = list; |
| 106 | return node; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /*-----------------------------------------------. |
| 111 | | Free the LIST, but not the items it contains. | |
| 112 | `-----------------------------------------------*/ |
| 113 | |
| 114 | void |
| 115 | symbol_list_free (symbol_list *list) |
| 116 | { |
| 117 | symbol_list *node, *next; |
| 118 | for (node = list; node; node = next) |
| 119 | { |
| 120 | next = node->next; |
| 121 | named_ref_free (node->named_ref); |
| 122 | free (node); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /*--------------------. |
| 128 | | Return its length. | |
| 129 | `--------------------*/ |
| 130 | |
| 131 | int |
| 132 | symbol_list_length (symbol_list const *l) |
| 133 | { |
| 134 | int res = 0; |
| 135 | for (/* Nothing. */; |
| 136 | l && !(l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL); |
| 137 | l = l->next) |
| 138 | ++res; |
| 139 | return res; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /*------------------------------. |
| 144 | | Get item N in symbol list L. | |
| 145 | `------------------------------*/ |
| 146 | |
| 147 | symbol_list * |
| 148 | symbol_list_n_get (symbol_list *l, int n) |
| 149 | { |
| 150 | int i; |
| 151 | |
| 152 | if (n < 0) |
| 153 | return NULL; |
| 154 | |
| 155 | for (i = 0; i < n; ++i) |
| 156 | { |
| 157 | l = l->next; |
| 158 | if (l == NULL |
| 159 | || (l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL)) |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | return l; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /*--------------------------------------------------------------. |
| 168 | | Get the data type (alternative in the union) of the value for | |
| 169 | | symbol N in symbol list L. | |
| 170 | `--------------------------------------------------------------*/ |
| 171 | |
| 172 | uniqstr |
| 173 | symbol_list_n_type_name_get (symbol_list *l, location loc, int n) |
| 174 | { |
| 175 | l = symbol_list_n_get (l, n); |
| 176 | if (!l) |
| 177 | { |
| 178 | complain (&loc, complaint, _("invalid $ value: $%d"), n); |
| 179 | return NULL; |
| 180 | } |
| 181 | aver (l->content_type == SYMLIST_SYMBOL); |
| 182 | return l->content.sym->type_name; |
| 183 | } |
| 184 | |
| 185 | bool |
| 186 | symbol_list_null (symbol_list *node) |
| 187 | { |
| 188 | return !node || |
| 189 | (node->content_type == SYMLIST_SYMBOL && !(node->content.sym)); |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | symbol_list_code_props_set (symbol_list *node, code_props_type kind, |
| 194 | code_props const *cprops) |
| 195 | { |
| 196 | switch (node->content_type) |
| 197 | { |
| 198 | case SYMLIST_SYMBOL: |
| 199 | symbol_code_props_set (node->content.sym, kind, cprops); |
| 200 | if (node->content.sym->status == undeclared) |
| 201 | node->content.sym->status = used; |
| 202 | break; |
| 203 | case SYMLIST_TYPE: |
| 204 | semantic_type_code_props_set |
| 205 | (semantic_type_get (node->content.sem_type->tag, |
| 206 | &node->content.sem_type->location), |
| 207 | kind, cprops); |
| 208 | if (node->content.sem_type->status == undeclared) |
| 209 | node->content.sem_type->status = used; |
| 210 | break; |
| 211 | } |
| 212 | } |