# Copyright 2018 Steven Watanabe
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.txt or copy at
# https://www.bfgroup.xyz/b2/LICENSE.txt)

# Named parameters are represented as a list which has the
# argument name as the first element and the value as the
# remaining elements.  This function sorts the parameters
# into the correct variables and removes the parameter names.
#
# Example::
#
#   rule exe ( name : sources * : requirements * )
#   {
#     param.handle-named-params sources requirements ;
#     # At this point $(sources) is test.cpp
#   }
#   exe test : requirements <link>shared : sources test.cpp ;
#
rule handle-named-params ( parameter-names * )
{
    module [ CALLER_MODULE ]
    {
        # Uglify the variable names, because we're executing in an unknown module.
        local found-8bef5c096d06a1b0 ;
        local tmp-8bef5c096d06a1b0.$(1) ;
        for local v-8bef5c096d06a1b0 in $(1)
        {
            if $($(v-8bef5c096d06a1b0)[1]) && $($(v-8bef5c096d06a1b0)[1]) in $(1)
            {
                if $(tmp-8bef5c096d06a1b0.$($(v-8bef5c096d06a1b0)[1]))
                {
                    import errors ;
                    errors.error Parameter '$($(v-8bef5c096d06a1b0)[1])' passed more than once. ;
                }
                found-8bef5c096d06a1b0 = true ;
                tmp-8bef5c096d06a1b0.$($(v-8bef5c096d06a1b0)[1]) = $($(v-8bef5c096d06a1b0)[2-]) ;
            }
            else if $($(v-8bef5c096d06a1b0))-is-defined
            {
                if $(found-8bef5c096d06a1b0)
                {
                    import errors ;
                    errors.error "Positional arguments must appear first." ;
                }
                tmp-8bef5c096d06a1b0.$(v-8bef5c096d06a1b0) = $($(v-8bef5c096d06a1b0)) ;
            }
        }
        for local v-8bef5c096d06a1b0 in $(1)
        {
            $(v-8bef5c096d06a1b0) = $(tmp-8bef5c096d06a1b0.$(v-8bef5c096d06a1b0)) ;
        }
    }
}
