<commit_before>#!python

import json

# comment.

classes = []

def generate_bindings(path):
    
    global classes
    classes = json.load(open(path))
    
    icalls = set()
    
    for c in classes:
        # print c['name']
        used_classes = get_used_classes(c)
        
        header = generate_class_header(used_classes, c)
        
        impl = generate_class_implementation(icalls, used_classes, c)
        
        header_file = open("include/" + strip_name(c["name"]) + ".hpp", "w+")
        header_file.write(header)
        
        source_file = open("src/" + strip_name(c["name"]) + ".cpp", "w+")
        source_file.write(impl)
    
    
    icall_header_file = open("src/__icalls.hpp", "w+")
    icall_header_file.write(generate_icall_header(icalls))
    
    icall_source_file = open("src/__icalls.cpp", "w+")
    icall_source_file.write(generate_icall_implementation(icalls))


def is_reference_type(t):
    for c in classes:
        if c['name'] != t:
            continue
        if c['is_reference']:
            return True
    return False

def make_gdnative_type(t):
    if is_enum(t):
        return remove_enum_prefix(t) + " "
    elif is_class_type(t):
        if is_reference_type(t):
            return "Ref<" + strip_name(t) + "> "
        else:
            return strip_name(t) + " *"
    else:
        if t == "int":
            return "int64_t "
        if t == "float" or t == "real":
            return "double "
        return strip_name(t) + " "

def generate_class_header(used_classes, c):

    source = []
    source.append("#ifndef GODOT_CPP_" + strip_name(c["name"]).upper() + "_HPP")
    source.append("#define GODOT_CPP_" + strip_name(c["name"]).upper() + "_HPP")
    source.append("")
    source.append("")
    
    source.append("#include <gdnative_api_struct.gen.h>")
    source.append("#include <stdint.h>")
    source.append("")
    
    
    source.append("#include <core/CoreTypes.hpp>")

    class_name = strip_name(c["name"])

    # Ref<T> is not included in object.h in Godot either,
    # so don't include it here because it's not needed
    if class_name != "Object" and class_name != "Reference":
        source.append("#include <core/Ref.hpp>")


    included = []

    for used_class in used_classes:
        if is_enum(used_class) and is_nested_type(used_class):
            used_class_name = remove_enum_prefix(extract_nested_type(used_class))
            if used_class_name not in included:
                included.append(used_class_name)
                source.append("#include \"" + used_class_name + ".hpp\"")
        elif is_enum(used_class) and is_nested_type(used_class) and not is_nested_type(used_class, class_name):
            used_class_name = remove_enum_prefix(used_class)
            if used_class_name not in included:
                included.append(used_class_name)
                source.append("#include \"" + used_class_name + ".hpp\"")

    source.append("")
    
    if c["base_class"] != "":
        source.append("#include \"" + strip_name(c["base_class"]) + ".hpp\"")
        
        
    
    source.append("namespace godot {")
    source.append("")


    for used_type in used_classes:
        if is_enum(used_type) or is_nested_type(used_type, class_name):
            continue
        else:
            source.append("class " + strip_name(used_type) + ";")
    
    
    source.append("")
    
    
    # generate the class definition here
    source.append("class " + class_name + ("" if c["base_class"] == "" else (" : public " + strip_name(c["base_class"])) ) + " {")
    
    source.append("public:")
    source.append("")

    # ___get_class_name
    source.append("\tstatic inline char *___get_class_name() { return (char *) \"" + strip_name(c["name"]) + "\"; }")

    source.append("\tstatic inline Object *___get_from_variant(Variant a) { return (Object *) a; }")

    enum_values = []

    source.append("\n\t// enums")
    for enum in c["enums"]:
        source.append("\tenum class " + strip_name(enum["name"]) + " {")
        for value in enum["values"]:
            source.append("\t\t" + remove_nested_type_prefix(value) + " = " + str(enum["values"][value]) + ",")
            enum_values.append(value)
        source.append("\t};")

    source.append("\n\t// constants")
    
    for name in c["constants"]:
        if name not in enum_values:
            source.append("\tconst static int " + name + " = " + str(c["constants"][name]) + ";")

    <commit_msg>disable strict enums<commit_after>#!python

import json

# comment.

classes = []

def generate_bindings(path):
    
    global classes
    classes = json.load(open(path))
    
    icalls = set()
    
    for c in classes:
        # print c['name']
        used_classes = get_used_classes(c)
        
        header = generate_class_header(used_classes, c)
        
        impl = generate_class_implementation(icalls, used_classes, c)
        
        header_file = open("include/" + strip_name(c["name"]) + ".hpp", "w+")
        header_file.write(header)
        
        source_file = open("src/" + strip_name(c["name"]) + ".cpp", "w+")
        source_file.write(impl)
    
    
    icall_header_file = open("src/__icalls.hpp", "w+")
    icall_header_file.write(generate_icall_header(icalls))
    
    icall_source_file = open("src/__icalls.cpp", "w+")
    icall_source_file.write(generate_icall_implementation(icalls))


def is_reference_type(t):
    for c in classes:
        if c['name'] != t:
            continue
        if c['is_reference']:
            return True
    return False

def make_gdnative_type(t):
    if is_enum(t):
        return remove_enum_prefix(t) + " "
    elif is_class_type(t):
        if is_reference_type(t):
            return "Ref<" + strip_name(t) + "> "
        else:
            return strip_name(t) + " *"
    else:
        if t == "int":
            return "int64_t "
        if t == "float" or t == "real":
            return "double "
        return strip_name(t) + " "

def generate_class_header(used_classes, c):

    source = []
    source.append("#ifndef GODOT_CPP_" + strip_name(c["name"]).upper() + "_HPP")
    source.append("#define GODOT_CPP_" + strip_name(c["name"]).upper() + "_HPP")
    source.append("")
    source.append("")
    
    source.append("#include <gdnative_api_struct.gen.h>")
    source.append("#include <stdint.h>")
    source.append("")
    
    
    source.append("#include <core/CoreTypes.hpp>")

    class_name = strip_name(c["name"])

    # Ref<T> is not included in object.h in Godot either,
    # so don't include it here because it's not needed
    if class_name != "Object" and class_name != "Reference":
        source.append("#include <core/Ref.hpp>")


    included = []

    for used_class in used_classes:
        if is_enum(used_class) and is_nested_type(used_class):
            used_class_name = remove_enum_prefix(extract_nested_type(used_class))
            if used_class_name not in included:
                included.append(used_class_name)
                source.append("#include \"" + used_class_name + ".hpp\"")
        elif is_enum(used_class) and is_nested_type(used_class) and not is_nested_type(used_class, class_name):
            used_class_name = remove_enum_prefix(used_class)
            if used_class_name not in included:
                included.append(used_class_name)
                source.append("#include \"" + used_class_name + ".hpp\"")

    source.append("")
    
    if c["base_class"] != "":
        source.append("#include \"" + strip_name(c["base_class"]) + ".hpp\"")
        
        
    
    source.append("namespace godot {")
    source.append("")


    for used_type in used_classes:
        if is_enum(used_type) or is_nested_type(used_type, class_name):
            continue
        else:
            source.append("class " + strip_name(used_type) + ";")
    
    
    source.append("")
    
    
    # generate the class definition here
    source.append("class " + class_name + ("" if c["base_class"] == "" else (" : public " + strip_name(c["base_class"])) ) + " {")
    
    source.append("public:")
    source.append("")

    # ___get_class_name
    source.append("\tstatic inline char *___get_class_name() { return (char *) \"" + strip_name(c["name"]) + "\"; }")

    source.append("\tstatic inline Object *___get_from_variant(Variant a) { return (Object *) a; }")

    enum_values = []

    source.append("\n\t// enums")
    for enum in c["enums"]:
        source.append("\tenum " + strip_name(enum["name"]) + " {")
        for value in enum["values"]:
            source.append("\t\t" + remove_nested_type_prefix(value) + " = " + str(enum["values"][value]) + ",")
            enum_values.append(value)
        source.append("\t};")

    source.append("\n\t// constants")
    
    for name in c["constants"]:
        if name not in enum_values:
            source.append("\tconst static int " + name + " = " + str(c["constants"][name]) + ";")

    
{'fraction_non_alphanumeric': 0.14721322461744665, 'fraction_numerical': 0.0004467776164414163, 'mean_word_length': 2.516889238020424, 'pattern_counts': {'":': 8, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5731577', 'n_tokens_mistral': 2905, 'n_tokens_neox': 2850, 'n_words': 545}