Next Previous Contents

6. Package for work with source code positions

The compilers often use internal representation which stores source code positions. Here package `position' which serves to support information about source positions of compiled files taking into account all included files is suggested.

The strategy of the package usage can be follows. Function `initiate' of class `positions' is called by the first. After that function `start_file' of class `positions' is called when a file is opened for compilation as source or included file. Members `line_number' and `column_number' of variable `current_position' are modified correspondingly during given file compilation. The value of `current_position' can be stored into internal representation for usage for output messages on the following passes. Function `finish_file' of class `positions' is called when a processed file is closed. Function `finish' of class `positions' may be called after all processing a source file.

The package uses packages `vlobject' and `objstack' which use package `allocate'. The interface part of the package is file `position.h'. The implementation part is file `position.cpp'. The interface contains the following objects:

Type `position_t'

is simply `class position' describes a file position. The class has the following members:

Public members `line_number', `column_number'

are source line and column corresponding to given position.

Public function `file_name'

               `const char *file_name (void)'
returns name of file to which given position belongs.

Public function `path'

               `class position *path (void)'
is pointer to another position class object representing position of a include-clause which caused immediately given file compilation.

Public function `path'

               `int file_inclusion_level (void)'
returns number of level of position file inclusion. The level numbers are started with zero for positions corresponding non-included files and for positions which does not correspond to concrete file.

Class `positions'

The class has not public constructors/destructors. The class has the following members:

Static const member `no_position'

has value of type `position_t' has values `line_number', `column_number' equals to zero or `NULL'. The value does not correspond to concrete file position.

Static member `current'

has value which is current file position.

Static public function `initiate'

           `void initiate (void)'
initiates the package. Value of variable `current' becomes equal to `no_position'.

Static public function `finish'

           `void finish (void)'
frees all memory allocated during package work.

Static public function `start_file'

           `void start_file (const char *file_name)'
save position in member variable `current' and sets up new values of members of `current'. Values of members `file_name', `line_number', `column_number', and `path' become equal to the function parameter value, 1, 0, and pointer to the saved position. Values of `current' during different calls of the function must be different (e.g. different columns or lines), i.e. positions of different include-clauses must be different.

Static public function `finish_file'

           `void finish_file (void)'
restores previous value of the member `current', more exactly sets up the member by position to which the member `path' refers.

Static public Function `compare'

           `int compare_positions (position_t position_1,
                                   position_t position_2)'
compares two positions given by parameters of type `position_t' and returns -1 (if the first position is less than the second), 0 (if the first position is equal to the second) or 1 (if the first position is greater than the second). The order of positions is lexicographic.

Next Previous Contents