1 /* Bison code properties structure and scanner. 2 3 Copyright (C) 2006-2007, 2009-2015, 2018-2021 Free Software 4 Foundation, 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 <https://www.gnu.org/licenses/>. */ 20 21 #ifndef SCAN_CODE_H_ 22 # define SCAN_CODE_H_ 23 24 # include "location.h" 25 # include "named-ref.h" 26 # include "uniqstr.h" 27 28 struct symbol_list; 29 30 /** 31 * Keeps track of the maximum number of semantic values to the left of a handle 32 * (those referenced by $0, $-1, etc.) that are required by the semantic 33 * actions of this grammar. 34 */ 35 extern int max_left_semantic_context; 36 37 /** 38 * A code passage captured from the grammar file and possibly translated, 39 * and/or properties associated with such a code passage. Don't break 40 * encapsulation by modifying the fields directly. Use the provided interface 41 * functions. 42 */ 43 typedef struct code_props { 44 /** Set by the init functions. */ 45 enum { 46 CODE_PROPS_NONE, CODE_PROPS_PLAIN, 47 CODE_PROPS_SYMBOL_ACTION, CODE_PROPS_RULE_ACTION 48 } kind; 49 50 /** 51 * \c NULL iff \c code_props::kind is \c CODE_PROPS_NONE. 52 * Memory is allocated in an obstack freed elsewhere. 53 */ 54 char const *code; 55 /** Undefined iff \c code_props::code is \c NULL. */ 56 location location; 57 58 /** 59 * \c false iff either: 60 * - \c code_props_translate_code has never previously been invoked for 61 * the \c code_props that would contain the code passage associated 62 * with \c self. (That \c code_props is not the same as this one if this 63 * one is for a RHS \c symbol_list node. Instead, it's the \c code_props 64 * for the LHS symbol of the same rule.) 65 * - \c code_props_translate_code has been invoked for that \c code_props, 66 * but the symbol value associated with this \c code_props was not 67 * referenced in the code passage. 68 */ 69 bool is_value_used; 70 71 /** 72 * \c true iff this code is an action that is not to be deferred in 73 * a non-deterministic parser. 74 */ 75 bool is_predicate; 76 77 /** 78 * Whether this is actually used (i.e., not completely masked by 79 * other code props). */ 80 bool is_used; 81 82 /** \c NULL iff \c code_props::kind is not \c CODE_PROPS_RULE_ACTION. */ 83 struct symbol_list *rule; 84 85 /** Named reference. */ 86 named_ref *named_ref; 87 88 /** Type, for midrule actions. */ 89 uniqstr type; 90 } code_props; 91 92 /** 93 * \pre 94 * - <tt>self != NULL</tt>. 95 * \post 96 * - \c self has been overwritten to contain no code. 97 */ 98 void code_props_none_init (code_props *self); 99 100 /** Equivalent to \c code_props_none_init. */ 101 # define CODE_PROPS_NONE_INIT \ 102 { \ 103 /* .kind = */ CODE_PROPS_NONE, \ 104 /* .code = */ NULL, \ 105 /* .location = */ EMPTY_LOCATION_INIT, \ 106 /* .is_value_used = */ false, \ 107 /* .is_predicate = */ false, \ 108 /* .is_used = */ false, \ 109 /* .rule = */ NULL, \ 110 /* .named_ref = */ NULL, \ 111 /* .type = */ NULL, \ 112 } 113 114 /** Initialized by \c CODE_PROPS_NONE_INIT with no further modification. */ 115 extern code_props code_props_none; 116 117 /** 118 * \pre 119 * - <tt>self != NULL</tt>. 120 * - <tt>code != NULL</tt>. 121 * - \c code is an untranslated code passage containing no Bison escapes. 122 * - \c code was extracted from the grammar file at \c code_loc. 123 * \post 124 * - \c self has been overwritten to represent the specified plain code 125 * passage. 126 * - \c self will become invalid if the caller frees \c code before invoking 127 * \c code_props_translate_code on \c self. 128 */ 129 void code_props_plain_init (code_props *self, char const *code, 130 location code_loc); 131 132 /** 133 * \pre 134 * - <tt>self != NULL</tt>. 135 * - <tt>code != NULL</tt>. 136 * - \c code is an untranslated code passage. The only Bison escapes it 137 * might contain are $$ and \@$, referring to a single symbol. 138 * - \c code was extracted from the grammar file at \c code_loc. 139 * \post 140 * - \c self has been overwritten to represent the specified symbol action. 141 * - \c self will become invalid if the caller frees \c code before invoking 142 * \c code_props_translate_code on \c self. 143 */ 144 void code_props_symbol_action_init (code_props *self, char const *code, 145 location code_loc); 146 147 /** 148 * \param type type for midrule actions 149 * \pre 150 * - <tt>self != NULL</tt>. 151 * - <tt>code != NULL</tt>. 152 * - <tt>rule != NULL</tt>. 153 * - \c code is the untranslated action of the rule for which \c rule is the 154 * LHS node. Thus, \c code possibly contains Bison escapes such as $$, $1, 155 * $2, etc referring to the values of the rule. 156 * - \c code was extracted from the grammar file at \c code_loc. 157 * \post 158 * - \c self has been overwritten to represent the specified rule action. 159 * - \c self does not claim responsibility for the memory of \c rule. 160 * - \c self will become invalid if: 161 * - The caller frees \c code before invoking \c code_props_translate_code 162 * on \c self. 163 * - The caller frees \c rule. 164 */ 165 void code_props_rule_action_init (code_props *self, char const *code, 166 location code_loc, struct symbol_list *rule, 167 named_ref *name, uniqstr type, 168 bool is_predicate); 169 170 /** 171 * \pre 172 * - If there's a code passage contained in \c self and it contains Bison 173 * escapes, all grammar declarations have already been parsed as they may 174 * affect warnings and complaints issued here. 175 * \post 176 * - All M4-special symbols and Bison escapes have been translated in 177 * \c self->code. 178 * - <tt>self->code != self->code\@pre</tt> unless 179 * <tt>self->code\@pre = NULL</tt>. 180 */ 181 void code_props_translate_code (code_props *self); 182 183 /** 184 * \pre 185 * - None. 186 * \post 187 * - The dynamic memory allocated by the previous invocation of 188 * \c code_props_translate_code (if any) was freed. The \c code_props 189 * instance for which \c code_props_translate_code was invoked is now 190 * invalid. 191 */ 192 void code_scanner_last_string_free (void); 193 194 /** 195 * \pre 196 * - None. 197 * \post 198 * - All dynamic memory allocated during invocations of 199 * \c code_props_translate_code (if any) has been freed. All \c code_props 200 * instances may now be invalid. 201 */ 202 void code_scanner_free (void); 203 204 #endif /* !SCAN_CODE_H_ */ 205