<commit_before>import ctypes
import ctypes.util
import os

try:
    # If possible, get nicer tracebacks from low-level signals.
    import faulthandler
    faulthandler.enable()
except:
    pass

from .v31 import *

try:
    _llvm = ctypes.cdll.LoadLibrary(os.path.join(os.path.dirname(__file__), "_llvm.so"))
    _libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library("c"))

    def _func(func_name, restype, argtypes=[]):
        """Creates ctypes wrapper for an LLVM API function.

        LLVM{name} -> llvm.{name}

        """
        g = globals()

        g[func_name] = getattr(_llvm, "LLVM" + func_name)
        g[func_name].restype = restype
        g[func_name].argtypes = argtypes

except OSError:
    # Allows us to complete the module import when LLMV library
    # is not available; currently used for documentation building.
    def _func(func_name, restype, argtypes=[]):
        def mock(*args, **kwargs):
            pass
        return mock


class owned_c_char_p(ctypes.c_char_p):
    """Char pointer which collects the memory of the return value."""

    def __del__(self):
        _libc.free(self)


Bool = ctypes.c_int
FALSE = 0
TRUE = 1


# Context
class OpaqueContext(ctypes.Structure):
    pass

ContextRef = ctypes.POINTER(OpaqueContext)

_func("GetGlobalContext", ContextRef)


# Module
class OpaqueModule(ctypes.Structure):
    pass

ModuleRef = ctypes.POINTER(OpaqueModule)

_func("ModuleCreateWithName", ModuleRef)
_func("GetModuleName", ctypes.c_char_p, [ModuleRef])
_func("DumpModule", None, [ModuleRef])
_func("DumpModuleToString", owned_c_char_p, [ModuleRef])
_func("DisposeModule", None, [ModuleRef])

_func("DisposeMessage", None, [ctypes.c_char_p])


# Linker
(LinkerDestroySource, LinkerPreserveSource) = range(2)

_func("LinkModules__", Bool,
      [ModuleRef, ModuleRef, ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)])


# Type
class OpaqueType(ctypes.Structure):
    pass

TypeRef = ctypes.POINTER(OpaqueType)

TypeKind = ctypes.c_int

_func("VoidType", TypeRef)
_func("FloatType", TypeRef)
_func("DoubleType", TypeRef)
_func("IntType", TypeRef, [ctypes.c_uint])

_func("GetTypeKind", TypeKind, [TypeRef])
_func("GetIntTypeWidth", ctypes.c_uint, [TypeRef])


# Sequential types
_func("PointerType", TypeRef, [TypeRef, ctypes.c_uint])
_func("VectorType", TypeRef, [TypeRef, ctypes.c_uint])

_func("GetElementType", TypeRef, [TypeRef])


# Value
class OpaqueValue(ctypes.Structure):
    pass

ValueRef = ctypes.POINTER(OpaqueValue)

_func("TypeOf", TypeRef, [ValueRef])
_func("SetValueName", None, [ValueRef, ctypes.c_char_p])
_func("GetValueName", ctypes.c_char_p, [ValueRef])
_func("DumpValue", None, [ValueRef])

# Operations on scalar constants
_func("ConstNull", ValueRef, [TypeRef])
_func("ConstInt", ValueRef, [TypeRef, ctypes.c_ulonglong, Bool])
_func("ConstReal", ValueRef, [TypeRef, ctypes.c_double])

_func("ConstString", ValueRef, [ctypes.c_char_p, ctypes.c_uint, Bool])
_func("ConstArray", ValueRef, [TypeRef, ctypes.POINTER(ValueRef), ctypes.c_uint])

_func("IsATerminatorInst", ValueRef, [ValueRef])


Opcode = ctypes.c_int


# Globals
_func("AddGlobal", ValueRef, [ModuleRef, TypeRef, ctypes.c_char_p])
_func("GetNamedGlobal", ValueRef, [ModuleRef, ctypes.c_char_p])
_func("SetInitializer", None, [ValueRef, ValueRef])
_func("SetGlobalConstant", None, [ValueRef, Bool])


# Functions
_func("FunctionType", TypeRef, [TypeRef, ctypes.POINTER(TypeRef), ctypes.c_uint, ctypes.c_int])
_func("AddFunction", ValueRef, [ModuleRef, ctypes.c_char_p, TypeRef])
_func("GetNamedFunction", ValueRef, [ModuleRef, ctypes.c_char_p])
_func("SetLinkage", None, [ValueRef, ctypes.c_int])
_func("GetParam", ValueRef, [ValueRef, ctypes.c_uint])
_func("GetReturnType", TypeRef, [TypeRef])

_func("AddAttribute", None, [ValueRef, ctypes.c_int]);

_func("AddFunctionAttr", None, [ValueRef, ctypes.c_int])

NoAliasAttribute = 1 << 6
AlwaysInlineAttribute = 1 << 12


def function_return_type(func):
    """Gets the return type directly from a function object."""
    return GetReturnType(GetElementType(TypeOf(func)))

_func("GetIntrinsicDeclaration", ValueRef,
      [ModuleRef, ctypes.c_uint, ctypes.POINTER(TypeRef), ctypes.c_uint])

_func("GetIntrinsicCount__", ctypes.c_uint, [])
_func("GetIntrinsicName__", owned_c_char_p, [ctypes.c_uint])


# This is False if we're generating docs didn't build the LLVM interface
if "GetIntrinsicCount__" in globals():
    INTRINSICS = dict((GetIntrinsicName__(i).value, i) for i in range(GetIntrinsicCount__()))
    """Intrinsic map; from name to intrinsic ID to use with GetIntrinsicDeclaration."""


ExternalLinkage = 0
PrivateLinkage = 8

# Structure Types
_func("StructType", TypeRef, [ctypes.POINTER(TypeRef), ctypes.c_uint, ctypes.c_bool])<commit_msg>Doc: Anoter attempt at making autodoc happy.<commit_after>import ctypes
import ctypes.util
import os

try:
    # If possible, get nicer tracebacks from low-level signals.
    import faulthandler
    faulthandler.enable()
except:
    pass

from .v31 import *

class _Mock(object):
    def __init__(self, *args, **kwargs):
        pass

try:
    _llvm = ctypes.cdll.LoadLibrary(os.path.join(os.path.dirname(__file__), "_llvm.so"))
    _libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library("c"))

    def _func(func_name, restype, argtypes=[]):
        """Creates ctypes wrapper for an LLVM API function.

        LLVM{name} -> llvm.{name}

        """
        g = globals()

        g[func_name] = getattr(_llvm, "LLVM" + func_name)
        g[func_name].restype = restype
        g[func_name].argtypes = argtypes

except OSError:
    # Allows us to complete the module import when LLMV library
    # is not available; currently used for documentation building.
    def _func(func_name, restype, argtypes=[]):
        globals()[func_name] = _Mock


class owned_c_char_p(ctypes.c_char_p):
    """Char pointer which collects the memory of the return value."""

    def __del__(self):
        _libc.free(self)


Bool = ctypes.c_int
FALSE = 0
TRUE = 1


# Context
class OpaqueContext(ctypes.Structure):
    pass

ContextRef = ctypes.POINTER(OpaqueContext)

_func("GetGlobalContext", ContextRef)


# Module
class OpaqueModule(ctypes.Structure):
    pass

ModuleRef = ctypes.POINTER(OpaqueModule)

_func("ModuleCreateWithName", ModuleRef)
_func("GetModuleName", ctypes.c_char_p, [ModuleRef])
_func("DumpModule", None, [ModuleRef])
_func("DumpModuleToString", owned_c_char_p, [ModuleRef])
_func("DisposeModule", None, [ModuleRef])

_func("DisposeMessage", None, [ctypes.c_char_p])


# Linker
(LinkerDestroySource, LinkerPreserveSource) = range(2)

_func("LinkModules__", Bool,
      [ModuleRef, ModuleRef, ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)])


# Type
class OpaqueType(ctypes.Structure):
    pass

TypeRef = ctypes.POINTER(OpaqueType)

TypeKind = ctypes.c_int

_func("VoidType", TypeRef)
_func("FloatType", TypeRef)
_func("DoubleType", TypeRef)
_func("IntType", TypeRef, [ctypes.c_uint])

_func("GetTypeKind", TypeKind, [TypeRef])
_func("GetIntTypeWidth", ctypes.c_uint, [TypeRef])


# Sequential types
_func("PointerType", TypeRef, [TypeRef, ctypes.c_uint])
_func("VectorType", TypeRef, [TypeRef, ctypes.c_uint])

_func("GetElementType", TypeRef, [TypeRef])


# Value
class OpaqueValue(ctypes.Structure):
    pass

ValueRef = ctypes.POINTER(OpaqueValue)

_func("TypeOf", TypeRef, [ValueRef])
_func("SetValueName", None, [ValueRef, ctypes.c_char_p])
_func("GetValueName", ctypes.c_char_p, [ValueRef])
_func("DumpValue", None, [ValueRef])

# Operations on scalar constants
_func("ConstNull", ValueRef, [TypeRef])
_func("ConstInt", ValueRef, [TypeRef, ctypes.c_ulonglong, Bool])
_func("ConstReal", ValueRef, [TypeRef, ctypes.c_double])

_func("ConstString", ValueRef, [ctypes.c_char_p, ctypes.c_uint, Bool])
_func("ConstArray", ValueRef, [TypeRef, ctypes.POINTER(ValueRef), ctypes.c_uint])

_func("IsATerminatorInst", ValueRef, [ValueRef])


Opcode = ctypes.c_int


# Globals
_func("AddGlobal", ValueRef, [ModuleRef, TypeRef, ctypes.c_char_p])
_func("GetNamedGlobal", ValueRef, [ModuleRef, ctypes.c_char_p])
_func("SetInitializer", None, [ValueRef, ValueRef])
_func("SetGlobalConstant", None, [ValueRef, Bool])


# Functions
_func("FunctionType", TypeRef, [TypeRef, ctypes.POINTER(TypeRef), ctypes.c_uint, ctypes.c_int])
_func("AddFunction", ValueRef, [ModuleRef, ctypes.c_char_p, TypeRef])
_func("GetNamedFunction", ValueRef, [ModuleRef, ctypes.c_char_p])
_func("SetLinkage", None, [ValueRef, ctypes.c_int])
_func("GetParam", ValueRef, [ValueRef, ctypes.c_uint])
_func("GetReturnType", TypeRef, [TypeRef])

_func("AddAttribute", None, [ValueRef, ctypes.c_int]);

_func("AddFunctionAttr", None, [ValueRef, ctypes.c_int])

NoAliasAttribute = 1 << 6
AlwaysInlineAttribute = 1 << 12


def function_return_type(func):
    """Gets the return type directly from a function object."""
    return GetReturnType(GetElementType(TypeOf(func)))

_func("GetIntrinsicDeclaration", ValueRef,
      [ModuleRef, ctypes.c_uint, ctypes.POINTER(TypeRef), ctypes.c_uint])

_func("GetIntrinsicCount__", ctypes.c_uint, [])
_func("GetIntrinsicName__", owned_c_char_p, [ctypes.c_uint])


# This is False if we're generating docs didn't build the LLVM interface
if GetIntrinsicCount__ is not _Mock:
    INTRINSICS = dict((GetIntrinsicName__(i).value, i) for i in range(GetIntrinsicCount__()))
    """Intrinsic map; from name to intrinsic ID to use with GetIntrinsicDeclaration."""


ExternalLinkage = 0
PrivateLinkage = 8

# Structure Types
_func("StructType", TypeRef, [ctypes.POINTER(TypeRef), ctypes.c_uint, ctypes.c_bool])
{'fraction_non_alphanumeric': 0.13520033564086428, 'fraction_numerical': 0.0025173064820641915, 'mean_word_length': 5.612343966712899, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '3916015', 'n_tokens_mistral': 3436, 'n_tokens_neox': 3359, 'n_words': 727}