code_signature Function

private function code_signature(src) result(sig)

The code tokens of src as one string that same_code compares when use statements may be reordered: for each token its kind (operators all alike), whether it is glued to the previous token, and its text as key gives it. The signatures of use statements in a run are sorted, and so are those of the items of each only: list.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

source text

Return Value character(len=:), allocatable


Calls

proc~~code_signature~~CallsGraph proc~code_signature code_signature proc~builder_append string_builder_t%builder_append proc~code_signature->proc~builder_append proc~builder_get string_builder_t%builder_get proc~code_signature->proc~builder_get proc~canonical_relational canonical_relational proc~code_signature->proc~canonical_relational proc~folded_length folded_length proc~code_signature->proc~folded_length proc~is_use_statement is_use_statement proc~code_signature->proc~is_use_statement proc~lowercase lowercase proc~code_signature->proc~lowercase proc~only_items only_items proc~code_signature->proc~only_items proc~parse_source parse_source proc~code_signature->proc~parse_source proc~set_view set_view proc~code_signature->proc~set_view proc~sorted_order sorted_order proc~code_signature->proc~sorted_order proc~to_s to_s proc~code_signature->proc~to_s proc~token_lower token_lower proc~code_signature->proc~token_lower proc~token_text token_text proc~code_signature->proc~token_text proc~lower_char lower_char proc~lowercase->proc~lower_char proc~protected_items protected_items proc~parse_source->proc~protected_items proc~split_statements split_statements proc~parse_source->proc~split_statements proc~tokenize tokenize proc~parse_source->proc~tokenize proc~make_lowercase make_lowercase proc~set_view->proc~make_lowercase proc~token_lower->proc~make_lowercase proc~directive directive proc~protected_items->proc~directive proc~ends_with_continuation ends_with_continuation proc~split_statements->proc~ends_with_continuation proc~find_semicolon find_semicolon proc~split_statements->proc~find_semicolon proc~line_end_token line_end_token proc~split_statements->proc~line_end_token proc~line_kind line_kind proc~split_statements->proc~line_kind proc~push statement_list_t%push proc~split_statements->proc~push proc~add add proc~tokenize->proc~add proc~comment_kind comment_kind proc~tokenize->proc~comment_kind proc~dotop_at dotop_at proc~tokenize->proc~dotop_at proc~dotop_end dotop_end proc~tokenize->proc~dotop_end proc~fypp_inline_end fypp_inline_end proc~tokenize->proc~fypp_inline_end proc~is_blank is_blank proc~tokenize->proc~is_blank proc~is_digit is_digit proc~tokenize->proc~is_digit proc~is_preproc_start is_preproc_start proc~tokenize->proc~is_preproc_start proc~is_quote is_quote proc~tokenize->proc~is_quote proc~lex_number lex_number proc~tokenize->proc~lex_number proc~line_end line_end proc~tokenize->proc~line_end proc~name_end name_end proc~tokenize->proc~name_end proc~operator_end operator_end proc~tokenize->proc~operator_end proc~peek peek proc~tokenize->proc~peek proc~preproc_end preproc_end proc~tokenize->proc~preproc_end proc~string_end string_end proc~tokenize->proc~string_end proc~add->proc~is_blank proc~add->proc~peek proc~is_code_char is_code_char proc~add->proc~is_code_char proc~push~2 token_list_t%push proc~add->proc~push~2 proc~comment_kind->proc~lowercase proc~comment_kind->proc~peek proc~is_letter is_letter proc~comment_kind->proc~is_letter proc~directive->proc~token_text proc~dotop_at->proc~peek proc~dotop_at->proc~is_letter proc~dotop_end->proc~lowercase proc~dotop_end->proc~name_end proc~dotop_end->proc~peek proc~is_name_char is_name_char proc~dotop_end->proc~is_name_char proc~fypp_inline_end->proc~peek proc~is_preproc_start->proc~peek proc~lex_number->proc~lower_char proc~lex_number->proc~dotop_at proc~lex_number->proc~is_digit proc~lex_number->proc~name_end proc~lex_number->proc~peek proc~digits_end digits_end proc~lex_number->proc~digits_end proc~lex_number->proc~is_name_char proc~name_end->proc~peek proc~name_end->proc~is_name_char proc~preproc_end->proc~line_end proc~preproc_end->proc~peek proc~last_nonblank last_nonblank proc~preproc_end->proc~last_nonblank proc~string_end->proc~is_blank proc~string_end->proc~line_end proc~string_end->proc~peek proc~only_blanks_to_eol only_blanks_to_eol proc~string_end->proc~only_blanks_to_eol proc~digits_end->proc~is_digit proc~digits_end->proc~peek proc~is_code_char->proc~is_blank proc~is_name_char->proc~is_digit proc~is_name_char->proc~is_letter proc~last_nonblank->proc~is_blank proc~only_blanks_to_eol->proc~line_end proc~only_blanks_to_eol->proc~last_nonblank

Called by

proc~~code_signature~~CalledByGraph proc~code_signature code_signature proc~same_code same_code proc~same_code->proc~code_signature

Source Code

function code_signature(src) result(sig)
    !! The code tokens of `src` as one string that [[same_code]] compares when `use` statements may
    !! be reordered: for each token its kind (operators all alike), whether it is glued to the
    !! previous token, and its text as `key` gives it. The signatures of `use` statements in a run
    !! are sorted, and so are those of the items of each `only:` list.
    character(len=*), intent(in) :: src   !! source text
    character(len=:), allocatable :: sig

    type(source_t) :: source
    type(statement_view_t) :: v
    type(string_builder_t) :: buf
    type(string_t), allocatable :: run(:)
    type(string_t) :: item
    integer :: k, j

    call parse_source(src, source)
    allocate (run(0))
    associate (tokens => source%tokens, stmts => source%stmts)
        do k = 1, stmts%n
            if (stmts%s(k)%kind == SK_PREPROC) call flush_run()
            if (stmts%s(k)%kind /= SK_CODE) cycle
            call set_view(src, tokens, stmts%s(k), v)
            if (is_use_statement(v)) then
                ! through a variable: a gfortran 15 internal compiler error otherwise
                item%s = use_signature(stmts%s(k), v)
                run = [run, item]
            else
                call flush_run()
                do j = stmts%s(k)%first, stmts%s(k)%last
                    if (is_code_kind(tokens%tok(j)%kind)) call buf%append(key(tokens%tok(j)))
                end do
            end if
        end do
    end associate
    call flush_run()
    sig = buf%get()

contains

    subroutine flush_run()
        !! Append the signatures of the pending run of `use` statements, sorted, and empty the run.
        integer, allocatable :: order(:)
        integer :: p
        order = sorted_order(run)
        do p = 1, size(order)
            call buf%append(run(order(p))%s)
        end do
        deallocate (run)
        allocate (run(0))
    end subroutine flush_run

    function use_signature(stmt, v) result(s)
        !! Signature of a `use` statement, with the items of its `only:` list sorted. Any `;` that
        !! ends it comes last.
        type(statement_t), intent(in) :: stmt          !! the `use` statement
        type(statement_view_t), intent(in) :: v        !! its view
        character(len=:), allocatable :: s
        integer, allocatable :: starts(:), ends(:), order(:)
        type(string_t), allocatable :: items(:)
        integer :: p, q, first

        starts = only_items(v)
        first = v%n + 1
        if (size(starts) > 0) first = starts(1)
        s = ''
        do p = 1, first - 1
            s = s//key(source%tokens%tok(v%code(p)))
        end do
        if (size(starts) > 0) then
            ends = [starts(2:) - 2, v%n]
            allocate (items(size(starts)))
            do p = 1, size(starts)
                items(p)%s = ''
                do q = starts(p), ends(p)
                    items(p)%s = items(p)%s//key(source%tokens%tok(v%code(q)))
                end do
            end do
            order = sorted_order(items)
            do p = 1, size(order)
                s = s//items(order(p))%s
                if (p < size(order)) s = s//key(source%tokens%tok(v%code(starts(2) - 1)))   ! the comma
            end do
        end if
        do p = stmt%first, stmt%last
            if (source%tokens%tok(p)%kind == TK_OP) then
                if (token_text(src, source%tokens%tok(p)) == ';') s = s//key(source%tokens%tok(p))
            end if
        end do
    end function use_signature

    function key(t) result(k)
        !! What must match: the kind, the glue, and the text, ignoring case for names and operators,
        !! with relational operators in their symbol spelling. The length is included, since `==`
        !! pads with blanks.
        type(token_t), intent(in) :: t   !! a code token of `src`
        character(len=:), allocatable :: k
        character :: kind, glue
        select case (t%kind)
        case (TK_DOTOP, TK_OP)
            k = canonical_relational(token_lower(src, t))
        case default
            k = token_text(src, t)
            associate (n => folded_length(k, t%kind))
                k(1:n) = lowercase(k(1:n))
            end associate
        end select
        kind = achar(64 + t%kind)
        if (t%kind == TK_DOTOP) kind = achar(64 + TK_OP)
        glue = merge('g', ' ', t%glued)
        k = kind//glue//to_s(len(k))//':'//k
    end function key

end function code_signature