11553Srgrimes/* Various declarations for language-independent pretty-print subroutines.
21553Srgrimes   Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
31553Srgrimes   Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
41553Srgrimes
51553SrgrimesThis file is part of GCC.
61553Srgrimes
71553SrgrimesGCC is free software; you can redistribute it and/or modify it under
81553Srgrimesthe terms of the GNU General Public License as published by the Free
91553SrgrimesSoftware Foundation; either version 2, or (at your option) any later
101553Srgrimesversion.
111553Srgrimes
121553SrgrimesGCC is distributed in the hope that it will be useful, but WITHOUT ANY
131553SrgrimesWARRANTY; without even the implied warranty of MERCHANTABILITY or
141553SrgrimesFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
151553Srgrimesfor more details.
161553Srgrimes
171553SrgrimesYou should have received a copy of the GNU General Public License
181553Srgrimesalong with GCC; see the file COPYING.  If not, write to the Free
191553SrgrimesSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
201553Srgrimes02110-1301, USA.  */
211553Srgrimes
221553Srgrimes#ifndef GCC_PRETTY_PRINT_H
231553Srgrimes#define GCC_PRETTY_PRINT_H
241553Srgrimes
251553Srgrimes#include "obstack.h"
261553Srgrimes#include "input.h"
271553Srgrimes
281553Srgrimes/* Maximum number of format string arguments.  */
291553Srgrimes#define PP_NL_ARGMAX   30
301553Srgrimes
3111448Swollman/* The type of a text to be formatted according a format specification
321553Srgrimes   along with a list of things.  */
331553Srgrimestypedef struct
3430603Scharnier{
3530603Scharnier  const char *format_spec;
3630603Scharnier  va_list *args_ptr;
3730603Scharnier  int err_no;  /* for %m */
381553Srgrimes  location_t *locus;
3930603Scharnier} text_info;
401553Srgrimes
411553Srgrimes/* How often diagnostics are prefixed by their locations:
4293078Sdes   o DIAGNOSTICS_SHOW_PREFIX_NEVER: never - not yet supported;
4393078Sdes   o DIAGNOSTICS_SHOW_PREFIX_ONCE: emit only once;
4493078Sdes   o DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE: emit each time a physical
451553Srgrimes   line is started.  */
461553Srgrimestypedef enum
471553Srgrimes{
481553Srgrimes  DIAGNOSTICS_SHOW_PREFIX_ONCE       = 0x0,
491553Srgrimes  DIAGNOSTICS_SHOW_PREFIX_NEVER      = 0x1,
501553Srgrimes  DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE = 0x2
511553Srgrimes} diagnostic_prefixing_rule_t;
521553Srgrimes
531553Srgrimes/* The chunk_info data structure forms a stack of the results from the
541553Srgrimes   first phase of formatting (pp_base_format) which have not yet been
551553Srgrimes   output (pp_base_output_formatted_text).  A stack is necessary because
561553Srgrimes   the diagnostic starter may decide to generate its own output by way
571553Srgrimes   of the formatter.  */
58129851Sdwmalonestruct chunk_info
591553Srgrimes{
601553Srgrimes  /* Pointer to previous chunk on the stack.  */
611553Srgrimes  struct chunk_info *prev;
621553Srgrimes
631553Srgrimes  /* Array of chunks to output.  Each chunk is a NUL-terminated string.
641553Srgrimes     In the first phase of formatting, even-numbered chunks are
655278Swollman     to be output verbatim, odd-numbered chunks are format specifiers.
665278Swollman     The second phase replaces all odd-numbered chunks with formatted
6737820Sphk     text, and the third phase simply emits all the chunks in sequence
6837820Sphk     with appropriate line-wrapping.  */
691553Srgrimes  const char *args[PP_NL_ARGMAX * 2];
701553Srgrimes};
711553Srgrimes
721553Srgrimes/* The output buffer datatype.  This is best seen as an abstract datatype
73174316Sobrien   whose fields should not be accessed directly by clients.  */
74174316Sobrientypedef struct
75174316Sobrien{
76174316Sobrien  /* Obstack where the text is built up.  */
77259368Sian  struct obstack formatted_obstack;
781553Srgrimes
791553Srgrimes  /* Obstack containing a chunked representation of the format
801553Srgrimes     specification plus arguments.  */
81199804Sattilio  struct obstack chunk_obstack;
821553Srgrimes
831553Srgrimes  /* Currently active obstack: one of the above two.  This is used so
841553Srgrimes     that the text formatters don't need to know which phase we're in.  */
8522984Sjoerg  struct obstack *obstack;
861553Srgrimes
871553Srgrimes  /* Stack of chunk arrays.  These come from the chunk_obstack.  */
881553Srgrimes  struct chunk_info *cur_chunk_array;
891553Srgrimes
905278Swollman  /* Where to output formatted text.  */
91115108Sgshapiro  FILE *stream;
921553Srgrimes
931553Srgrimes  /* The amount of characters output so far.  */
941553Srgrimes  int line_length;
951553Srgrimes
961553Srgrimes  /* This must be large enough to hold any printed integer or
971553Srgrimes     floating-point value.  */
9830603Scharnier  char digit_buffer[128];
991553Srgrimes} output_buffer;
1001553Srgrimes
101115108Sgshapiro/* The type of pretty-printer flags passed to clients.  */
102106054Swollmantypedef unsigned int pp_flags;
10393078Sdes
1041553Srgrimestypedef enum
1051553Srgrimes{
1061553Srgrimes  pp_none, pp_before, pp_after
1071553Srgrimes} pp_padding;
10825437Sjoerg
1091553Srgrimes/* Structure for switching in and out of verbatim mode in a convenient
110202206Sed   manner.  */
11183243Sddtypedef struct
1121553Srgrimes{
11383243Sdd  /* Current prefixing rule.  */
1141553Srgrimes  diagnostic_prefixing_rule_t rule;
1151553Srgrimes
1161553Srgrimes  /* The ideal upper bound of number of characters per line, as suggested
11719224Sjoerg     by front-end.  */
11811448Swollman  int line_cutoff;
11911448Swollman} pp_wrapping_mode_t;
12011448Swollman
1211553Srgrimes/* Maximum characters per line in automatic line wrapping mode.
1221553Srgrimes   Zero means don't wrap lines.  */
1231553Srgrimes#define pp_line_cutoff(PP)  pp_base (PP)->wrapping.line_cutoff
124174316Sobrien
1251553Srgrimes/* Prefixing rule used in formatting a diagnostic message.  */
126137232Sglebius#define pp_prefixing_rule(PP)  pp_base (PP)->wrapping.rule
127137232Sglebius
128137233Sglebius/* Get or set the wrapping mode as a single entity.  */
129137857Skeramida#define pp_wrapping_mode(PP) pp_base (PP)->wrapping
130137232Sglebius
131137232Sglebius/* The type of a hook that formats client-specific data onto a pretty_pinter.
132137232Sglebius   A client-supplied formatter returns true if everything goes well,
133186331Sdelphij   otherwise it returns false.  */
134137232Sglebiustypedef struct pretty_print_info pretty_printer;
135137232Sglebiustypedef bool (*printer_fn) (pretty_printer *, text_info *, const char *,
136137232Sglebius			    int, bool, bool, bool);
137137233Sglebius
138137233Sglebius/* Client supplied function used to decode formats.  */
139137232Sglebius#define pp_format_decoder(PP) pp_base (PP)->format_decoder
140137233Sglebius
14137156Sguido/* TRUE if a newline character needs to be added before further
142137232Sglebius   formatting.  */
143137233Sglebius#define pp_needs_newline(PP)  pp_base (PP)->need_newline
14437156Sguido
1451553Srgrimes/* True if PRETTY-PTINTER is in line-wrapping mode.  */
1461553Srgrimes#define pp_is_wrapping_line(PP) (pp_line_cutoff (PP) > 0)
1471553Srgrimes
1481553Srgrimes/* The amount of whitespace to be emitted when starting a new line.  */
149174316Sobrien#define pp_indentation(PP) pp_base (PP)->indent_skip
150174316Sobrien
151174316Sobrien/* The data structure that contains the bare minimum required to do
152174316Sobrien   proper pretty-printing.  Clients may derived from this structure
153174316Sobrien   and add additional fields they need.  */
1541553Srgrimesstruct pretty_print_info
1551553Srgrimes{
1561553Srgrimes  /* Where we print external representation of ENTITY.  */
1571553Srgrimes  output_buffer *buffer;
158129855Sdwmalone
159129855Sdwmalone  /* The prefix for each new line.  */
1601553Srgrimes  const char *prefix;
1611553Srgrimes
1621553Srgrimes  /* Where to put whitespace around the entity being formatted.  */
1631553Srgrimes  pp_padding padding;
1641553Srgrimes
1651553Srgrimes  /* The real upper bound of number of characters per line, taking into
1661553Srgrimes     account the case of a very very looong prefix.  */
16763795Sdwmalone  int maximum_length;
1681553Srgrimes
16937820Sphk  /* Indentation count.  */
17037820Sphk  int indent_skip;
17137820Sphk
17237820Sphk  /* Current wrapping mode.  */
1735278Swollman  pp_wrapping_mode_t wrapping;
1741553Srgrimes
175200954Sed  /* If non-NULL, this function formats a TEXT into the BUFFER.  When called,
1761553Srgrimes     TEXT->format_spec points to a format code.  FORMAT_DECODER should call
17774053Sbrian     pp_string (and related functions) to add data to the BUFFER.
17870099Sume     FORMAT_DECODER can read arguments from *TEXT->args_pts using VA_ARG.
17970099Sume     If the BUFFER needs additional characters from the format string, it
1801553Srgrimes     should advance the TEXT->format_spec as it goes.  When FORMAT_DECODER
1811553Srgrimes     returns, TEXT->format_spec should point to the last character processed.
18222984Sjoerg  */
18322984Sjoerg  printer_fn format_decoder;
18422984Sjoerg
18522984Sjoerg  /* Nonzero if current PREFIX was emitted at least once.  */
1861553Srgrimes  bool emitted_prefix;
1871553Srgrimes
1881553Srgrimes  /* Nonzero means one should emit a newline before outputting anything.  */
18974053Sbrian  bool need_newline;
1901553Srgrimes};
1911553Srgrimes
1921553Srgrimes#define pp_set_line_maximum_length(PP, L) \
19383243Sdd   pp_base_set_line_maximum_length (pp_base (PP), L)
194129865Sdwmalone#define pp_set_prefix(PP, P)    pp_base_set_prefix (pp_base (PP), P)
195174316Sobrien#define pp_destroy_prefix(PP)   pp_base_destroy_prefix (pp_base (PP))
196174316Sobrien#define pp_remaining_character_count_for_line(PP) \
1971553Srgrimes  pp_base_remaining_character_count_for_line (pp_base (PP))
1981553Srgrimes#define pp_clear_output_area(PP) \
1991553Srgrimes  pp_base_clear_output_area (pp_base (PP))
20022984Sjoerg#define pp_formatted_text(PP)   pp_base_formatted_text (pp_base (PP))
20122984Sjoerg#define pp_last_position_in_text(PP) \
20222984Sjoerg  pp_base_last_position_in_text (pp_base (PP))
20360938Sjake#define pp_emit_prefix(PP)      pp_base_emit_prefix (pp_base (PP))
20422984Sjoerg#define pp_append_text(PP, B, E) \
20522984Sjoerg  pp_base_append_text (pp_base (PP), B, E)
20622984Sjoerg#define pp_flush(PP)            pp_base_flush (pp_base (PP))
20722984Sjoerg#define pp_format(PP, TI)       pp_base_format (pp_base (PP), TI)
20822984Sjoerg#define pp_output_formatted_text(PP) \
20960938Sjake  pp_base_output_formatted_text (pp_base (PP))
21022984Sjoerg#define pp_format_verbatim(PP, TI) \
21122984Sjoerg  pp_base_format_verbatim (pp_base (PP), TI)
21222984Sjoerg
21322984Sjoerg#define pp_character(PP, C)     pp_base_character (pp_base (PP), C)
21422984Sjoerg#define pp_string(PP, S)        pp_base_string (pp_base (PP), S)
21522984Sjoerg#define pp_newline(PP)          pp_base_newline (pp_base (PP))
21622984Sjoerg
21722984Sjoerg#define pp_space(PP)            pp_character (PP, ' ')
218174316Sobrien#define pp_left_paren(PP)       pp_character (PP, '(')
21922984Sjoerg#define pp_right_paren(PP)      pp_character (PP, ')')
22022984Sjoerg#define pp_left_bracket(PP)     pp_character (PP, '[')
22122984Sjoerg#define pp_right_bracket(PP)    pp_character (PP, ']')
22222984Sjoerg#define pp_left_brace(PP)       pp_character (PP, '{')
22322984Sjoerg#define pp_right_brace(PP)      pp_character (PP, '}')
22425437Sjoerg#define pp_semicolon(PP)        pp_character (PP, ';')
22525437Sjoerg#define pp_comma(PP)            pp_string (PP, ", ")
22625437Sjoerg#define pp_dot(PP)              pp_character (PP, '.')
22725437Sjoerg#define pp_colon(PP)            pp_character (PP, ':')
22825437Sjoerg#define pp_colon_colon(PP)      pp_string (PP, "::")
22925437Sjoerg#define pp_arrow(PP)            pp_string (PP, "->")
23025437Sjoerg#define pp_equal(PP)            pp_character (PP, '=')
23125437Sjoerg#define pp_question(PP)         pp_character (PP, '?')
23270099Sume#define pp_bar(PP)              pp_character (PP, '|')
23370099Sume#define pp_carret(PP)           pp_character (PP, '^')
23425437Sjoerg#define pp_ampersand(PP)        pp_character (PP, '&')
23525437Sjoerg#define pp_less(PP)             pp_character (PP, '<')
23625437Sjoerg#define pp_greater(PP)          pp_character (PP, '>')
23725437Sjoerg#define pp_plus(PP)             pp_character (PP, '+')
23825437Sjoerg#define pp_minus(PP)            pp_character (PP, '-')
23925437Sjoerg#define pp_star(PP)             pp_character (PP, '*')
24025437Sjoerg#define pp_slash(PP)            pp_character (PP, '/')
24125437Sjoerg#define pp_modulo(PP)           pp_character (PP, '%')
24225437Sjoerg#define pp_exclamation(PP)      pp_character (PP, '!')
24325437Sjoerg#define pp_complement(PP)       pp_character (PP, '~')
2441553Srgrimes#define pp_quote(PP)            pp_character (PP, '\'')
2451553Srgrimes#define pp_backquote(PP)        pp_character (PP, '`')
2461553Srgrimes#define pp_doublequote(PP)      pp_character (PP, '"')
2471553Srgrimes#define pp_newline_and_indent(PP, N) \
2481553Srgrimes  do {                               \
2491553Srgrimes    pp_indentation (PP) += N;        \
2501553Srgrimes    pp_newline (PP);                 \
2511553Srgrimes    pp_base_indent (pp_base (PP));   \
2521553Srgrimes    pp_needs_newline (PP) = false;   \
2531553Srgrimes  } while (0)
2541553Srgrimes#define pp_maybe_newline_and_indent(PP, N) \
2551553Srgrimes  if (pp_needs_newline (PP)) pp_newline_and_indent (PP, N)
2561553Srgrimes#define pp_maybe_space(PP)   pp_base_maybe_space (pp_base (PP))
2571553Srgrimes#define pp_separate_with(PP, C)     \
2581553Srgrimes   do {                             \
2591553Srgrimes     pp_character (PP, C);          \
2601553Srgrimes     pp_space (PP);                 \
2611553Srgrimes   } while (0)
2621553Srgrimes#define pp_scalar(PP, FORMAT, SCALAR)	                      \
26322984Sjoerg  do					        	      \
2641553Srgrimes    {			         			      \
26583243Sdd      sprintf (pp_buffer (PP)->digit_buffer, FORMAT, SCALAR); \
2661553Srgrimes      pp_string (PP, pp_buffer (PP)->digit_buffer);           \
26722984Sjoerg    }						              \
2681553Srgrimes  while (0)
2691553Srgrimes#define pp_decimal_int(PP, I)  pp_scalar (PP, "%d", I)
27093078Sdes#define pp_wide_integer(PP, I) \
27193078Sdes   pp_scalar (PP, HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT) I)
2721553Srgrimes#define pp_widest_integer(PP, I) \
27393078Sdes   pp_scalar (PP, HOST_WIDEST_INT_PRINT_DEC, (HOST_WIDEST_INT) I)
27493078Sdes#define pp_pointer(PP, P)      pp_scalar (PP, "%p", P)
27593078Sdes
276129756Sdwmalone#define pp_identifier(PP, ID)  pp_string (PP, ID)
27793078Sdes#define pp_tree_identifier(PP, T)                      \
27893078Sdes  pp_append_text(PP, IDENTIFIER_POINTER (T), \
27993078Sdes                 IDENTIFIER_POINTER (T) + IDENTIFIER_LENGTH (T))
28093078Sdes
28193078Sdes#define pp_unsupported_tree(PP, T)                         \
282224002Sdelphij  pp_verbatim (pp_base (PP), "#%qs not supported by %s#", \
28393078Sdes               tree_code_name[(int) TREE_CODE (T)], __FUNCTION__)
28470099Sume
28593078Sdes
28670099Sume#define pp_buffer(PP) pp_base (PP)->buffer
28793078Sdes/* Clients that directly derive from pretty_printer need to override
28870099Sume   this macro to return a pointer to the base pretty_printer structure.  */
289178986Sbrian#define pp_base(PP) (PP)
29093078Sdes
29193078Sdesextern void pp_construct (pretty_printer *, const char *, int);
29293078Sdesextern void pp_base_set_line_maximum_length (pretty_printer *, int);
293156339Spjdextern void pp_base_set_prefix (pretty_printer *, const char *);
29417245Spstextern void pp_base_destroy_prefix (pretty_printer *);
29593078Sdesextern int pp_base_remaining_character_count_for_line (pretty_printer *);
2961553Srgrimesextern void pp_base_clear_output_area (pretty_printer *);
29793078Sdesextern const char *pp_base_formatted_text (pretty_printer *);
29893078Sdesextern const char *pp_base_last_position_in_text (const pretty_printer *);
299183347Sdwmaloneextern void pp_base_emit_prefix (pretty_printer *);
30025437Sjoergextern void pp_base_append_text (pretty_printer *, const char *, const char *);
30193078Sdes
30293078Sdes/* This header may be included before toplev.h, hence the duplicate
30337820Sphk   definitions to allow for GCC-specific formats.  */
30493078Sdes#if GCC_VERSION >= 3005
305131589Scperciva#define ATTRIBUTE_GCC_PPDIAG(m, n) __attribute__ ((__format__ (__gcc_diag__, m ,n))) ATTRIBUTE_NONNULL(m)
306149425Spjd#else
30737820Sphk#define ATTRIBUTE_GCC_PPDIAG(m, n) ATTRIBUTE_NONNULL(m)
30882724Skris#endif
30982724Skrisextern void pp_printf (pretty_printer *, const char *, ...)
31093078Sdes     ATTRIBUTE_GCC_PPDIAG(2,3);
31193078Sdes
31293078Sdesextern void pp_verbatim (pretty_printer *, const char *, ...)
31393078Sdes     ATTRIBUTE_GCC_PPDIAG(2,3);
31493078Sdesextern void pp_base_flush (pretty_printer *);
31593078Sdesextern void pp_base_format (pretty_printer *, text_info *);
316241736Sedextern void pp_base_output_formatted_text (pretty_printer *);
31793078Sdesextern void pp_base_format_verbatim (pretty_printer *, text_info *);
31893078Sdes
319131591Scpercivaextern void pp_base_indent (pretty_printer *);
32093078Sdesextern void pp_base_newline (pretty_printer *);
32193078Sdesextern void pp_base_character (pretty_printer *, int);
322211023Solliextern void pp_base_string (pretty_printer *, const char *);
32393078Sdesextern void pp_write_text_to_stream (pretty_printer *pp);
32493078Sdesextern void pp_base_maybe_space (pretty_printer *);
32593078Sdes
32693078Sdes/* Switch into verbatim mode and return the old mode.  */
32793078Sdesstatic inline pp_wrapping_mode_t
328114676Sgshapiropp_set_verbatim_wrapping_ (pretty_printer *pp)
329183347Sdwmalone{
33093078Sdes  pp_wrapping_mode_t oldmode = pp_wrapping_mode (pp);
33193078Sdes  pp_line_cutoff (pp) = 0;
33293078Sdes  pp_prefixing_rule (pp) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
33393078Sdes  return oldmode;
33493077Sdes}
33593078Sdes#define pp_set_verbatim_wrapping(PP) pp_set_verbatim_wrapping_ (pp_base (PP))
33693077Sdes
337186234Sobrien#endif /* GCC_PRETTY_PRINT_H */
33893078Sdes