def _provider_text(symbols):
    return """
WRAPPER = provider(
  doc = "Wrapper to hold imported methods",
  fields = [{}]
)
""".format(", ".join(["\"%s\"" % symbol_ for symbol_ in symbols]))

def _getter_text():
    return """
def id_from_file(file_name):
    (before, middle, after) = file_name.partition(".")
    return before

def get(file_name):
    id = id_from_file(file_name)
    return WRAPPER(**_MAPPING[id])
"""

def _mapping_text(ids):
    data_ = []
    for id in ids:
        data_ += ["{id} = wrapper_{id}".format(id = id)]
    return "_MAPPING = dict(\n{data}\n)".format(data = ",\n".join(data_))

def _load_and_wrapper_text(id, file_path, symbols):
    load_list = ", ".join(["{id}_{symbol} = \"{symbol}\"".format(id = id, symbol = symbol_) for symbol_ in symbols])
    load_statement = "load(\":{file}\", {list})".format(file = file_path, list = load_list)
    data = ", ".join(["{symbol} = {id}_{symbol}".format(id = id, symbol = symbol_) for symbol_ in symbols])
    wrapper_statement = "wrapper_{id} = dict({data})".format(id = id, data = data)
    return load_statement + "\n" + wrapper_statement

def id_from_file(file_name):
    (before, middle, after) = file_name.partition(".")
    return before

def get_file_name(file_label):
    (before, separator, after) = file_label.partition(":")
    return id_from_file(after)

def _copy_file(rctx, src):
    src_path = rctx.path(src)
    copy_path = src_path.basename
    rctx.template(copy_path, src_path)
    return copy_path

def _generate_overloads(rctx):
    symbols = rctx.attr.symbols
    ids = []
    lines = ["# Generated overload mappings"]
    for file_ in rctx.attr.files:
        id = id_from_file(file_.name)
        ids += [id]
        copy = _copy_file(rctx, file_)
        lines += [_load_and_wrapper_text(id, copy, symbols)]
    lines += [_mapping_text(ids)]
    lines += [_provider_text(symbols)]
    lines += [_getter_text()]

    rctx.file("toolchain_data_defs.bzl", "\n".join(lines))
    rctx.file("BUILD", "")

generate_overloads = repository_rule(
    implementation = _generate_overloads,
    attrs = {
        "symbols": attr.string_list(),
        "files": attr.label_list(),
    },
)

@@ -1075,31 +1075,65 @@
 urn 
-load_statement + %22%5Cn%22 +
+struct(%0A        load_ = load_statement,%0A        wrapper =
  wra
@@ -1146,16 +1146,24 @@
 tatement
+%0A      )
 %0A%0Adef id
@@ -1655,16 +1655,49 @@
 pings%22%5D%0A
+    loads = %5B%5D%0A    wrappers = %5B%5D%0A
     for 
@@ -1722,16 +1722,16 @@
 .files:%0A
-
         
@@ -1828,25 +1828,34 @@
        l
-ines +
+oad_and_wrapper 
 = 
-%5B
 _load_an
@@ -1879,33 +1879,162 @@
 , copy, symbols)
-%5D
+%0A        loads += %5Bload_and_wrapper.load_%5D%0A        wrappers += %5Bload_and_wrapper.wrapper%5D%0A    lines += loads%0A    lines += wrappers
 %0A    lines += %5B_

Fix generated code to have load statements strictly in the beginning (#231)