105436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Bison code properties structure and scanner.
205436638acc7c010349a69c3395f1a57c642dc62Ying Wang
305436638acc7c010349a69c3395f1a57c642dc62Ying Wang   Copyright (C) 2006-2007, 2009-2012 Free Software Foundation, Inc.
405436638acc7c010349a69c3395f1a57c642dc62Ying Wang
505436638acc7c010349a69c3395f1a57c642dc62Ying Wang   This file is part of Bison, the GNU Compiler Compiler.
605436638acc7c010349a69c3395f1a57c642dc62Ying Wang
705436638acc7c010349a69c3395f1a57c642dc62Ying Wang   This program is free software: you can redistribute it and/or modify
805436638acc7c010349a69c3395f1a57c642dc62Ying Wang   it under the terms of the GNU General Public License as published by
905436638acc7c010349a69c3395f1a57c642dc62Ying Wang   the Free Software Foundation, either version 3 of the License, or
1005436638acc7c010349a69c3395f1a57c642dc62Ying Wang   (at your option) any later version.
1105436638acc7c010349a69c3395f1a57c642dc62Ying Wang
1205436638acc7c010349a69c3395f1a57c642dc62Ying Wang   This program is distributed in the hope that it will be useful,
1305436638acc7c010349a69c3395f1a57c642dc62Ying Wang   but WITHOUT ANY WARRANTY; without even the implied warranty of
1405436638acc7c010349a69c3395f1a57c642dc62Ying Wang   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1505436638acc7c010349a69c3395f1a57c642dc62Ying Wang   GNU General Public License for more details.
1605436638acc7c010349a69c3395f1a57c642dc62Ying Wang
1705436638acc7c010349a69c3395f1a57c642dc62Ying Wang   You should have received a copy of the GNU General Public License
1805436638acc7c010349a69c3395f1a57c642dc62Ying Wang   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
1905436638acc7c010349a69c3395f1a57c642dc62Ying Wang
2005436638acc7c010349a69c3395f1a57c642dc62Ying Wang#ifndef SCAN_CODE_H_
2105436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define SCAN_CODE_H_
2205436638acc7c010349a69c3395f1a57c642dc62Ying Wang
2305436638acc7c010349a69c3395f1a57c642dc62Ying Wang# include "location.h"
2405436638acc7c010349a69c3395f1a57c642dc62Ying Wang# include "named-ref.h"
2505436638acc7c010349a69c3395f1a57c642dc62Ying Wang
2605436638acc7c010349a69c3395f1a57c642dc62Ying Wangstruct symbol_list;
2705436638acc7c010349a69c3395f1a57c642dc62Ying Wang
2805436638acc7c010349a69c3395f1a57c642dc62Ying Wang/**
2905436638acc7c010349a69c3395f1a57c642dc62Ying Wang * Keeps track of the maximum number of semantic values to the left of a handle
3005436638acc7c010349a69c3395f1a57c642dc62Ying Wang * (those referenced by $0, $-1, etc.) that are required by the semantic
3105436638acc7c010349a69c3395f1a57c642dc62Ying Wang * actions of this grammar.
3205436638acc7c010349a69c3395f1a57c642dc62Ying Wang */
3305436638acc7c010349a69c3395f1a57c642dc62Ying Wangextern int max_left_semantic_context;
3405436638acc7c010349a69c3395f1a57c642dc62Ying Wang
3505436638acc7c010349a69c3395f1a57c642dc62Ying Wang/**
3605436638acc7c010349a69c3395f1a57c642dc62Ying Wang * A code passage captured from the grammar file and possibly translated,
3705436638acc7c010349a69c3395f1a57c642dc62Ying Wang * and/or properties associated with such a code passage.  Don't break
3805436638acc7c010349a69c3395f1a57c642dc62Ying Wang * encapsulation by modifying the fields directly.  Use the provided interface
3905436638acc7c010349a69c3395f1a57c642dc62Ying Wang * functions.
4005436638acc7c010349a69c3395f1a57c642dc62Ying Wang */
4105436638acc7c010349a69c3395f1a57c642dc62Ying Wangtypedef struct code_props {
4205436638acc7c010349a69c3395f1a57c642dc62Ying Wang  /** Set by the init functions.  */
4305436638acc7c010349a69c3395f1a57c642dc62Ying Wang  enum {
4405436638acc7c010349a69c3395f1a57c642dc62Ying Wang    CODE_PROPS_NONE, CODE_PROPS_PLAIN,
4505436638acc7c010349a69c3395f1a57c642dc62Ying Wang    CODE_PROPS_SYMBOL_ACTION, CODE_PROPS_RULE_ACTION
4605436638acc7c010349a69c3395f1a57c642dc62Ying Wang  } kind;
4705436638acc7c010349a69c3395f1a57c642dc62Ying Wang
4805436638acc7c010349a69c3395f1a57c642dc62Ying Wang  /**
4905436638acc7c010349a69c3395f1a57c642dc62Ying Wang   * \c NULL iff \c code_props::kind is \c CODE_PROPS_NONE.
5005436638acc7c010349a69c3395f1a57c642dc62Ying Wang   * Memory is allocated in an obstack freed elsewhere.
5105436638acc7c010349a69c3395f1a57c642dc62Ying Wang   */
5205436638acc7c010349a69c3395f1a57c642dc62Ying Wang  char const *code;
5305436638acc7c010349a69c3395f1a57c642dc62Ying Wang  /** Undefined iff \c code_props::code is \c NULL.  */
5405436638acc7c010349a69c3395f1a57c642dc62Ying Wang  location location;
5505436638acc7c010349a69c3395f1a57c642dc62Ying Wang
5605436638acc7c010349a69c3395f1a57c642dc62Ying Wang  /**
5705436638acc7c010349a69c3395f1a57c642dc62Ying Wang   * \c false iff either:
5805436638acc7c010349a69c3395f1a57c642dc62Ying Wang   *   - \c code_props_translate_code has never previously been invoked for
5905436638acc7c010349a69c3395f1a57c642dc62Ying Wang   *     the \c code_props that would contain the code passage associated
6005436638acc7c010349a69c3395f1a57c642dc62Ying Wang   *     with \c self.  (That \c code_props is not the same as this one if this
6105436638acc7c010349a69c3395f1a57c642dc62Ying Wang   *     one is for a RHS \c symbol_list node.  Instead, it's the \c code_props
6205436638acc7c010349a69c3395f1a57c642dc62Ying Wang   *     for the LHS symbol of the same rule.)
6305436638acc7c010349a69c3395f1a57c642dc62Ying Wang   *   - \c code_props_translate_code has been invoked for that \c code_props,
6405436638acc7c010349a69c3395f1a57c642dc62Ying Wang   *     but the symbol value associated with this \c code_props was not
6505436638acc7c010349a69c3395f1a57c642dc62Ying Wang   *     referenced in the code passage.
6605436638acc7c010349a69c3395f1a57c642dc62Ying Wang   */
6705436638acc7c010349a69c3395f1a57c642dc62Ying Wang  bool is_value_used;
6805436638acc7c010349a69c3395f1a57c642dc62Ying Wang
6905436638acc7c010349a69c3395f1a57c642dc62Ying Wang  /** \c NULL iff \c code_props::kind is not \c CODE_PROPS_RULE_ACTION.  */
7005436638acc7c010349a69c3395f1a57c642dc62Ying Wang  struct symbol_list *rule;
7105436638acc7c010349a69c3395f1a57c642dc62Ying Wang
7205436638acc7c010349a69c3395f1a57c642dc62Ying Wang  /* Named reference. */
7305436638acc7c010349a69c3395f1a57c642dc62Ying Wang  named_ref *named_ref;
7405436638acc7c010349a69c3395f1a57c642dc62Ying Wang} code_props;
7505436638acc7c010349a69c3395f1a57c642dc62Ying Wang
7605436638acc7c010349a69c3395f1a57c642dc62Ying Wang/**
7705436638acc7c010349a69c3395f1a57c642dc62Ying Wang * \pre
7805436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - <tt>self != NULL</tt>.
7905436638acc7c010349a69c3395f1a57c642dc62Ying Wang * \post
8005436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - \c self has been overwritten to contain no code.
8105436638acc7c010349a69c3395f1a57c642dc62Ying Wang */
8205436638acc7c010349a69c3395f1a57c642dc62Ying Wangvoid code_props_none_init (code_props *self);
8305436638acc7c010349a69c3395f1a57c642dc62Ying Wang
8405436638acc7c010349a69c3395f1a57c642dc62Ying Wang/** Equivalent to \c code_props_none_init.  */
8505436638acc7c010349a69c3395f1a57c642dc62Ying Wang#define CODE_PROPS_NONE_INIT \
8605436638acc7c010349a69c3395f1a57c642dc62Ying Wang  {CODE_PROPS_NONE, NULL, EMPTY_LOCATION_INIT, false, NULL, NULL}
8705436638acc7c010349a69c3395f1a57c642dc62Ying Wang
8805436638acc7c010349a69c3395f1a57c642dc62Ying Wang/** Initialized by \c CODE_PROPS_NONE_INIT with no further modification.  */
8905436638acc7c010349a69c3395f1a57c642dc62Ying Wangextern code_props const code_props_none;
9005436638acc7c010349a69c3395f1a57c642dc62Ying Wang
9105436638acc7c010349a69c3395f1a57c642dc62Ying Wang/**
9205436638acc7c010349a69c3395f1a57c642dc62Ying Wang * \pre
9305436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - <tt>self != NULL</tt>.
9405436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - <tt>code != NULL</tt>.
9505436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - \c code is an untranslated code passage containing no Bison escapes.
9605436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - \c code was extracted from the grammar file at \c code_loc.
9705436638acc7c010349a69c3395f1a57c642dc62Ying Wang * \post
9805436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - \c self has been overwritten to represent the specified plain code
9905436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     passage.
10005436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - \c self will become invalid if the caller frees \c code before invoking
10105436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     \c code_props_translate_code on \c self.
10205436638acc7c010349a69c3395f1a57c642dc62Ying Wang */
10305436638acc7c010349a69c3395f1a57c642dc62Ying Wangvoid code_props_plain_init (code_props *self, char const *code,
10405436638acc7c010349a69c3395f1a57c642dc62Ying Wang                            location code_loc);
10505436638acc7c010349a69c3395f1a57c642dc62Ying Wang
10605436638acc7c010349a69c3395f1a57c642dc62Ying Wang/**
10705436638acc7c010349a69c3395f1a57c642dc62Ying Wang * \pre
10805436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - <tt>self != NULL</tt>.
10905436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - <tt>code != NULL</tt>.
11005436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - \c code is an untranslated code passage.  The only Bison escapes it
11105436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     might contain are $$ and \@$, referring to a single symbol.
11205436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - \c code was extracted from the grammar file at \c code_loc.
11305436638acc7c010349a69c3395f1a57c642dc62Ying Wang * \post
11405436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - \c self has been overwritten to represent the specified symbol action.
11505436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - \c self will become invalid if the caller frees \c code before invoking
11605436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     \c code_props_translate_code on \c self.
11705436638acc7c010349a69c3395f1a57c642dc62Ying Wang */
11805436638acc7c010349a69c3395f1a57c642dc62Ying Wangvoid code_props_symbol_action_init (code_props *self, char const *code,
11905436638acc7c010349a69c3395f1a57c642dc62Ying Wang                                    location code_loc);
12005436638acc7c010349a69c3395f1a57c642dc62Ying Wang
12105436638acc7c010349a69c3395f1a57c642dc62Ying Wang/**
12205436638acc7c010349a69c3395f1a57c642dc62Ying Wang * \pre
12305436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - <tt>self != NULL</tt>.
12405436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - <tt>code != NULL</tt>.
12505436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - <tt>rule != NULL</tt>.
12605436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - \c code is the untranslated action of the rule for which \c rule is the
12705436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     LHS node.  Thus, \c code possibly contains Bison escapes such as $$, $1,
12805436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     $2, etc referring to the values of the rule.
12905436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - \c code was extracted from the grammar file at \c code_loc.
13005436638acc7c010349a69c3395f1a57c642dc62Ying Wang * \post
13105436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - \c self has been overwritten to represent the specified rule action.
13205436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - \c self does not claim responsibility for the memory of \c rule.
13305436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - \c self will become invalid if:
13405436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     - The caller frees \c code before invoking \c code_props_translate_code
13505436638acc7c010349a69c3395f1a57c642dc62Ying Wang *       on \c self.
13605436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     - The caller frees \c rule.
13705436638acc7c010349a69c3395f1a57c642dc62Ying Wang */
13805436638acc7c010349a69c3395f1a57c642dc62Ying Wangvoid code_props_rule_action_init (code_props *self, char const *code,
13905436638acc7c010349a69c3395f1a57c642dc62Ying Wang                                  location code_loc, struct symbol_list *rule,
14005436638acc7c010349a69c3395f1a57c642dc62Ying Wang                                  named_ref *name);
14105436638acc7c010349a69c3395f1a57c642dc62Ying Wang
14205436638acc7c010349a69c3395f1a57c642dc62Ying Wang/**
14305436638acc7c010349a69c3395f1a57c642dc62Ying Wang * \pre
14405436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - If there's a code passage contained in \c self and it contains Bison
14505436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     escapes, all grammar declarations have already been parsed as they may
14605436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     affect warnings and complaints issued here.
14705436638acc7c010349a69c3395f1a57c642dc62Ying Wang * \post
14805436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - All M4-special symbols and Bison escapes have been translated in
14905436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     \c self->code.
15005436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - <tt>self->code != self->code\@pre</tt> unless
15105436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     <tt>self->code\@pre = NULL</tt>.
15205436638acc7c010349a69c3395f1a57c642dc62Ying Wang */
15305436638acc7c010349a69c3395f1a57c642dc62Ying Wangvoid code_props_translate_code (code_props *self);
15405436638acc7c010349a69c3395f1a57c642dc62Ying Wang
15505436638acc7c010349a69c3395f1a57c642dc62Ying Wang/**
15605436638acc7c010349a69c3395f1a57c642dc62Ying Wang * \pre
15705436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - None.
15805436638acc7c010349a69c3395f1a57c642dc62Ying Wang * \post
15905436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - The dynamic memory allocated by the previous invocation of
16005436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     \c code_props_translate_code (if any) was freed.  The \c code_props
16105436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     instance for which \c code_props_translate_code was invoked is now
16205436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     invalid.
16305436638acc7c010349a69c3395f1a57c642dc62Ying Wang */
16405436638acc7c010349a69c3395f1a57c642dc62Ying Wangvoid code_scanner_last_string_free (void);
16505436638acc7c010349a69c3395f1a57c642dc62Ying Wang
16605436638acc7c010349a69c3395f1a57c642dc62Ying Wang/**
16705436638acc7c010349a69c3395f1a57c642dc62Ying Wang * \pre
16805436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - None.
16905436638acc7c010349a69c3395f1a57c642dc62Ying Wang * \post
17005436638acc7c010349a69c3395f1a57c642dc62Ying Wang *   - All dynamic memory allocated during invocations of
17105436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     \c code_props_translate_code (if any) has been freed.  All \c code_props
17205436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     instances may now be invalid.
17305436638acc7c010349a69c3395f1a57c642dc62Ying Wang */
17405436638acc7c010349a69c3395f1a57c642dc62Ying Wangvoid code_scanner_free (void);
17505436638acc7c010349a69c3395f1a57c642dc62Ying Wang
17605436638acc7c010349a69c3395f1a57c642dc62Ying Wang#endif /* !SCAN_CODE_H_ */
177