Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
token_buffer.h
Go to the documentation of this file.
00001 /*
00002 
00003   Copyright (C) 2000, 2001 Silicon Graphics, Inc.  All Rights Reserved.
00004 
00005   This program is free software; you can redistribute it and/or modify it
00006   under the terms of version 2 of the GNU General Public License as
00007   published by the Free Software Foundation.
00008 
00009   This program is distributed in the hope that it would be useful, but
00010   WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013   Further, this software is distributed without any warranty that it is
00014   free of the rightful claim of any third person regarding infringement 
00015   or the like.  Any license provided herein, whether implied or 
00016   otherwise, applies only to this software file.  Patent licenses, if 
00017   any, provided herein do not apply to combinations of this program with 
00018   other software, or any other product whatsoever.  
00019 
00020   You should have received a copy of the GNU General Public License along
00021   with this program; if not, write the Free Software Foundation, Inc., 59
00022   Temple Place - Suite 330, Boston MA 02111-1307, USA.
00023 
00024   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00025   Mountain View, CA 94043, or:
00026 
00027   http://www.sgi.com
00028 
00029   For further information regarding this notice, see:
00030 
00031   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00032 
00033 */
00034 
00035 
00036 #ifndef token_buffer_INCLUDED
00037 #define token_buffer_INCLUDED
00038 /* ====================================================================
00039  * ====================================================================
00040  *
00041  *
00042  * Revision history:
00043  *  07-Oct-94 - Original Version
00044  *
00045  * Description:
00046  *
00047  *     The internals of a TOKEN_BUFFER is hidden within this package,
00048  *     and we export a TOKEN_BUFFER as an abstract data type (AST).
00049  *     To create and initialize a new TOKEN_BUFFER, call 
00050  *     New_Token_Buffer().  The facilities to manipulate TOKEN_BUFFER
00051  *     objects are grouped as follows in this file:
00052  *
00053  *        1) The AST itself (TOKEN_BUFFER) and a means for creating
00054  *           a new instance of a TOKEN_BUFFER.
00055  *
00056  *        2) Inquiries about a TOKEN_BUFFER.
00057  *
00058  *        3) Initialization and finalization of this subsystem, as
00059  *           well as facilities for freeing up unused memory.
00060  *
00061  *        4) Changing the indentation.
00062  *
00063  *        5) Prepending and appending new tokens to the token-list
00064  *           maintained by a TOKEN_BUFFER.
00065  *
00066  *        6) Fortran specific layout functions.
00067  *
00068  *        7) Writing a token-list to file, and reclaiming (not
00069  *           freeing up memory) a TOKEN_BUFFER.
00070  *
00071  *     Most of the formatting must be done explicitly by the user
00072  *     in terms of item 3).  Separators will be automatically
00073  *     inserted between tokens, as follows:
00074  *
00075  *        + Two string tokens are always separated by a space.
00076  *    
00077  *        + A string token and a binary/tertiary special operator 
00078  *          character (i.e. one of: '+', '-', '*', '/', '&', '|',
00079  *          '%', '=', '?', '<', '>', or ':') are always separated
00080  *          by a space.  Note that to write C code, "->" should be 
00081  *          split into special tokens to get "a->b".
00082  *
00083  *        + A '}', ')' or ']' special character, followed by a binary 
00084  *          or tertiary special operator character or a string token,
00085  *          are always separated by a space.
00086  *
00087  *        + A ';' or ',' and any following token except separator 
00088  *          tokens, are always separated by a space.
00089  *
00090  *        + When a limit has been set on the maximum line-length, then
00091  *          a line may be split anywhere except in the middle of the
00092  *          characters belonging to a token.  NB: For this reason, try
00093  *          to use SPECIAL tokens whenever possible and allways put
00094  *          character string constants in a single token.
00095  *
00096  *     There is no limit to the number of token buffers that may be 
00097  *     created, other than those imposed by memory availablity on the
00098  *     host system.
00099  *
00100  * The opening and closing of files must be taken care of in the
00101  * environment using this module.  This way we can support a uniform
00102  * model of token-sequences (TOKEN_BUFFER) without further 
00103  * consideration of how many output files are being used.
00104  * ====================================================================
00105  * ====================================================================
00106  */
00107 
00108 
00109 /* Before using any of the facilities provided through this package, the 
00110  * token buffer subsystem must be initialized.  To free up memory used
00111  * by all token buffers, the sybsystem must be terminated.  
00112  *
00113  * The FORMAT_KIND sets up a default for how many characters may appear 
00114  * on an output-line (unlimited for FREE_FORMAT and F77_TAB_FORMAT;
00115  * 72 characters for F77_ANSI_FORMAT.  This default can be reset
00116  * with a call to Set_Maximum_Linelength subsequent to the call to
00117  * Initialize_Token_Buffer().  The FORMAT_KIND also determines how lines
00118  * are continued when the maximum line size is exceeded.  Note that
00119  * Set_Maximum_Linelength() will reinstate the default maximum linelength,
00120  * as is determoned by the FORMAT_KIND, when given a zero argument.
00121  *
00122  * WARNING: We assume the srcpos_map_file is open when the tokens written
00123  * to file included any Srcpos_Map token.  We do not currently employ
00124  * any error-checking mechanism to ensure that this is so and gracefully
00125  * terminate otherwise.
00126  * ----------------------------------------------------------------------*/
00127 typedef enum Format_Kind 
00128 {
00129    FREE_FORMAT = 0,
00130    F77_TAB_FORMAT = 1,
00131    F77_ANSI_FORMAT = 2, 
00132    NUM_FORMAT_KINDS = F77_ANSI_FORMAT
00133 } FORMAT_KIND;
00134 
00135 extern void Initialize_Token_Buffer(FORMAT_KIND output_format,
00136                                     BOOL        prompf_srcmap_format);
00137 extern void Terminate_Token_Buffer(FILE *srcpos_map_file);
00138 
00139 extern void Set_Maximum_Linelength(UINT32 max_linelength);
00140 extern BOOL Has_Maximum_Linelength(void);
00141 extern UINT32 Get_Maximum_Linelength(void);
00142 
00143 
00144 /* The AST and a means for creating new objects of the type.
00145  * A token_buffer declaration should always be initialized to
00146  * NULL or a New_Token_Buffer() prior to any use.
00147  * ----------------------------------------------------------*/
00148 typedef struct Token_Buffer *TOKEN_BUFFER;
00149 extern TOKEN_BUFFER New_Token_Buffer(void);
00150 extern void Reclaim_Token_Buffer(TOKEN_BUFFER *tokens);
00151 
00152 
00153 /* Some means for inquiring about a token buffer instance.
00154  * -------------------------------------------------------*/
00155 extern BOOL Is_Empty_Token_Buffer(TOKEN_BUFFER tokens);
00156 extern BOOL Identical_Token_Lists(TOKEN_BUFFER tokens1, 
00157                                   TOKEN_BUFFER tokens2);
00158 
00159 
00160 /* Free up buffers that are no longer in use.  Buffers are
00161    reclaimed by adding them to a free-list.  This routine
00162    will free up all buffers on the free-list.
00163    -------------------------------------------------------*/
00164 extern void Free_Token_Buffer_Memory(void);
00165 
00166 
00167 /* Change indentation for the next call to Append_Indented_Newline()
00168  * or prepend_indented_newline().  The default unit of indentation is
00169  * 2 spaces, but this can be changed with set_indentation_step().
00170  * The indentation can be reset to column 1 with a call to 
00171  * Zero_Indentation().
00172  *  ---------------------------------------------------------------*/
00173 extern UINT Current_Indentation(void);
00174 extern void Set_Current_Indentation(UINT ident);
00175 extern void Set_Indentation_Step(UINT num_spaces);
00176 extern void Increment_Indentation(void);
00177 extern void Decrement_Indentation(void);
00178 extern void Zero_Indentation(void);
00179 
00180 
00181 /* Tokens may be appended to the end (right-hand-side) of the 
00182  * current token-list or prepended to the beginning (left-hand
00183  * side) of the current token-list.  We only support the most 
00184  * basic forms of tokens, these being string constants and 
00185  * special characters, where separators other than newlines
00186  * (space characters) are automatically inserted.  The chars
00187  * in a "const char *" argument is copied when adding a 
00188  * token-string to a TOKEN_BUFFER.
00189  * ------------------------------------------------------------*/
00190 extern void Append_Indented_Newline(TOKEN_BUFFER tokens, UINT num_lines);
00191 extern void Append_Token_String(TOKEN_BUFFER tokens, const char *string);
00192 extern void Append_Token_Special(TOKEN_BUFFER tokens, char special);
00193 extern void Append_And_Copy_Token_List(TOKEN_BUFFER result_tokens, 
00194                                        TOKEN_BUFFER copy_tokens);
00195 extern void Append_And_Reclaim_Token_List(TOKEN_BUFFER result_tokens, 
00196                                           TOKEN_BUFFER *reclaim_tokens);
00197 
00198 extern void Prepend_Indented_Newline(TOKEN_BUFFER tokens, UINT num_lines);
00199 extern void Prepend_Token_String(TOKEN_BUFFER tokens, const char *string);
00200 extern void Prepend_Token_Special(TOKEN_BUFFER tokens, char special);
00201 extern void Prepend_And_Copy_Token_List(TOKEN_BUFFER result_tokens, 
00202                                         TOKEN_BUFFER copy_tokens);
00203 extern void Prepend_And_Reclaim_Token_List(TOKEN_BUFFER result_tokens, 
00204                                            TOKEN_BUFFER *reclaim_tokens);
00205 
00206 
00207 /* Fortran specific layout functions.  Do not call these function
00208  * in modes other than F77_TAB_FORMAT and F77_ANSI_FORMAT:
00209  *
00210  *    + Append_F77_Indented_Newline() will add a newline character 
00211  *      followed by an indentation conformant with Fortran rules 
00212  *      (label==NULL implies "no label") to the given token buffer.
00213  *
00214  *    + Append_F77_Indented_Continuation will ass a newline character,
00215  *      add a continuation marker for the next line and ident it.
00216  *
00217  *    + Append_F77_Comment_Newline() will add a sequence of empty
00218  *      comment lines, following the present line, to the token-list.
00219  *      The last line will be indented according to the 
00220  *      Current_Indentation(), if so desired.  The last line may also
00221  *      be given the "*$*" directive prefix before the indentation,
00222  *      if so desired.
00223  *
00224  *    + Append_F77_Directive_Newline() will add a comment line
00225  *      to the token-list, after ending the current line.
00226  *      The new line will contain the directive prefix (e.g. "C*$*").
00227  *
00228  *    + Append_F77_Sequence_No() should only be called for 
00229  *      F77_ANSI_FORMAT and will append spaces to fill the current
00230  *      line up to column 73 followed by a sequence number in columns 
00231  *      73-80.
00232  * ------------------------------------------------------------------*/
00233 extern void Append_F77_Indented_Newline(TOKEN_BUFFER tokens,
00234                                         UINT         num_lines,
00235                                         const char  *label);
00236 extern void Prepend_F77_Indented_Newline(TOKEN_BUFFER tokens,
00237                                          UINT         num_lines, 
00238                                          const char  *label);
00239 
00240 extern void Append_F77_Indented_Continuation(TOKEN_BUFFER tokens);
00241 extern void Prepend_F77_Indented_Continuation(TOKEN_BUFFER tokens);
00242 
00243 extern void Append_F77_Comment_Newline(TOKEN_BUFFER tokens, 
00244                                        UINT         num_lines,
00245                                        BOOL         indent_last_line);
00246 extern void Prepend_F77_Comment_Newline(TOKEN_BUFFER tokens, 
00247                                         UINT         num_lines,
00248                                         BOOL         indent_last_line);
00249 
00250 extern void Append_F77_Directive_Newline(TOKEN_BUFFER tokens, 
00251                                          const char  *directive_prefix);
00252 extern void Prepend_F77_Directive_Newline(TOKEN_BUFFER tokens, 
00253                                           const char  *directive_prefix);
00254 
00255 extern void Append_F77_Sequence_No(TOKEN_BUFFER tokens, 
00256                                    const char  *seq_no);
00257 extern void Prepend_F77_Sequence_No(TOKEN_BUFFER tokens, 
00258                                     const char  *seq_no);
00259 
00260 
00261 /* The following routines are used to create tokens representing
00262  * source position mappings and/or directives.
00263  *
00264  * The srcpos mappings relate the "current" location in the output 
00265  * file, after having written all tokens up to this point, to the 
00266  * given SRCPOS in the original file. These mappings are inserted
00267  * into a separate file (see comments about Write_And_Reclaim_Tokens()
00268  * and Terminate_Token_Buffer()).
00269  *
00270  * Each srcpos directive represents a newline followed by a "#line"
00271  * directive.  The newline to follow the directive must be explicitly
00272  * added by means of one of the Indented_Newline() routines described
00273  * above.  These diretives are inserted inline into the generated
00274  * source code.
00275  * -----------------------------------------------------------------*/
00276 extern void Append_Srcpos_Map(TOKEN_BUFFER tokens, SRCPOS srcpos);
00277 extern void Append_Srcpos_Directive(TOKEN_BUFFER tokens, SRCPOS srcpos);
00278 
00279 extern void Prepend_Srcpos_Map(TOKEN_BUFFER tokens, SRCPOS srcpos);
00280 extern void Prepend_Srcpos_Directive(TOKEN_BUFFER tokens, SRCPOS srcpos);
00281 
00282 
00283 
00284 /* Tokens representing a partial text-line, a full text-line, or 
00285  * multiple text-lines, may be converted to text strings and written 
00286  * to the output file (ofile) in left-to-right order.  Once the 
00287  * TOKEN_BUFFER has been written to file, the TOKEN_BUFFER will be 
00288  * reclaimed by this subsystem (for reuse) and "*tokens" will be set
00289  * to NULL.  When srcpos_map_file==NULL, the advances in the output-
00290  * file current location will be ignored as far as the Srcpos_Map is 
00291  * concerned.
00292  *
00293  * WARNING: We assume the ofile is open for writing without checking
00294  * that this is so!  Similarly, we assume the srcpos_map_file is
00295  * open when the tokens may include a Srcpos_Map token.
00296  *
00297  * An alternative is provided for writing a token buffer into
00298  * a string buffer, by means of Str_Write_And_Reclaim_Tokens().
00299  * No Srcpos_Map is maintained for such output.
00300  * -----------------------------------------------------------------*/
00301 extern void Write_And_Reclaim_Tokens(FILE         *ofile, 
00302                                      FILE         *srcpos_map_file,
00303                                      TOKEN_BUFFER *tokens);
00304 
00305 extern void Str_Write_And_Reclaim_Tokens(char         *strbuf,
00306                                          UINT32        buflen,
00307                                          TOKEN_BUFFER *tokens);
00308 
00309 
00310 /* Write the given string to the output-file.  The increment in
00311  * line/column number for the output-file will be ignored as
00312  * far as the Srcpos_Map is concerned, unless srcpos_map_file!=NULL.
00313  * -----------------------------------------------------------------*/
00314 void Write_String(FILE *ofile, FILE *srcpos_map_file, const char *str);
00315 
00316 
00317 #endif /* token_buffer_INCLUDED */
00318 
00319 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines