[clang] [clang-bindings] Add strict typing to clang Python bindings (#76664) (PR #78114)

Jannick Kremer via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 14 13:31:54 PST 2024


https://github.com/DeinAlptraum created https://github.com/llvm/llvm-project/pull/78114

This PR adds type annotations to the clang Python bindings, to the point that they pass a strict MyPy check.
This utilizes Python features up to 3.7, thus ending support of the bindings for versions <= 3.6 (see also discussion here: https://discourse.llvm.org/t/type-annotations-for-libclang-python-bindings/70644). I thus removed some compatibility stuff for older Python versions.

I tried to change as little as possible, but sometimes passing the type check without changing semantics would have been impossible. Such changes mostly come down to e.g. returning an empty string instead of `None` and similar.
Some changes might look unrelated at first glance, but everything was done specifically to resolve typing errors.

I've left comments in some other places where I'm unsure about my changes, especially where they affect semantics.



>From 9730ffcd4f9b2d10e948a768fdf4b176bd069f5d Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick-kremer at gmx.de>
Date: Sun, 14 Jan 2024 21:20:23 +0000
Subject: [PATCH] [clang-bindings] Add strict typing to Python bindings

---
 clang/bindings/python/clang/cindex.py | 2934 +++++++++++++++----------
 1 file changed, 1733 insertions(+), 1201 deletions(-)

diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index d780ee353a133c..809527e712acde 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -43,7 +43,7 @@
 Most object information is exposed using properties, when the underlying API
 call is efficient.
 """
-from __future__ import absolute_import, division, print_function
+from __future__ import annotations
 
 # TODO
 # ====
@@ -67,89 +67,690 @@
 import clang.enumerations
 
 import os
-import sys
-
-if sys.version_info[0] == 3:
-    # Python 3 strings are unicode, translate them to/from utf8 for C-interop.
-    class c_interop_string(c_char_p):
-        def __init__(self, p=None):
-            if p is None:
-                p = ""
-            if isinstance(p, str):
-                p = p.encode("utf8")
-            super(c_char_p, self).__init__(p)
-
-        def __str__(self):
-            return self.value
-
-        @property
-        def value(self):
-            if super(c_char_p, self).value is None:
-                return None
-            return super(c_char_p, self).value.decode("utf8")
-
-        @classmethod
-        def from_param(cls, param):
-            if isinstance(param, str):
-                return cls(param)
-            if isinstance(param, bytes):
-                return cls(param)
-            if param is None:
-                # Support passing null to C functions expecting char arrays
-                return None
-            raise TypeError(
-                "Cannot convert '{}' to '{}'".format(type(param).__name__, cls.__name__)
-            )
+from enum import Enum
+
+from typing import (
+    Any,
+    Callable,
+    cast as Tcast,
+    Generic,
+    Iterable,
+    Iterator,
+    Optional,
+    Sequence,
+    Type as TType,
+    TypeVar,
+    TYPE_CHECKING,
+    Union as TUnion,
+)
+from typing_extensions import Protocol, TypeAlias
+
+if TYPE_CHECKING:
+    from ctypes import _Pointer, _FuncPointer, _CArgObject
+    from io import TextIOWrapper
+
+    StrPath: TypeAlias = TUnion[str, os.PathLike[str]]
+    InMemoryFile: TypeAlias = (
+        "tuple[TUnion[str, os.PathLike[Any]], TUnion[str, TextIOWrapper]]"
+    )
+    LibFunc: TypeAlias = TUnion[
+        "tuple[str, Optional[list[Any]]]",
+        "tuple[str, Optional[list[Any]], Any]",
+        "tuple[str, Optional[list[Any]], Any, Callable[..., Any]]",
+    ]
+    CObjP: TypeAlias = _Pointer[Any]
 
-        @staticmethod
-        def to_python_string(x, *args):
-            return x.value
+    TSeq = TypeVar("TSeq", covariant=True)
 
-    def b(x):
-        if isinstance(x, bytes):
-            return x
-        return x.encode("utf8")
+    class NoSliceSequence(Protocol[TSeq]):
+        def __len__(self) -> int:
+            ...
 
-elif sys.version_info[0] == 2:
-    # Python 2 strings are utf8 byte strings, no translation is needed for
-    # C-interop.
-    c_interop_string = c_char_p
+        def __getitem__(self, key: int) -> TSeq:
+            ...
 
-    def _to_python_string(x, *args):
-        return x
 
-    c_interop_string.to_python_string = staticmethod(_to_python_string)
+class ClangLib(Protocol):
+    def clang_annotateTokens(
+        self, tu: TranslationUnit, token: _CArgObject, num: int, cursor: _CArgObject
+    ) -> None:
+        ...
 
-    def b(x):
-        return x
+    def clang_CompilationDatabase_dispose(self, cdb: CompilationDatabase) -> None:
+        ...
+
+    def clang_CompilationDatabase_fromDirectory(
+        self, buildDir: str, errorCode: _CArgObject
+    ) -> CompilationDatabase:
+        ...
+
+    def clang_CompilationDatabase_getAllCompileCommands(
+        self, cdb: CompilationDatabase
+    ) -> CompileCommands:
+        ...
+
+    def clang_CompilationDatabase_getCompileCommands(
+        self, cdb: CompilationDatabase, filename: str
+    ) -> CompileCommands:
+        ...
+
+    def clang_CompileCommands_dispose(self, ccmds: CObjP) -> None:
+        ...
+
+    def clang_CompileCommands_getCommand(self, ccmds: CObjP, key: int) -> CObjP:
+        ...
+
+    def clang_CompileCommands_getSize(self, ccmds: CObjP) -> c_uint:
+        ...
+
+    def clang_CompileCommand_getArg(self, cmd: CObjP, key: int) -> str:
+        ...
+
+    def clang_CompileCommand_getDirectory(self, cmd: CObjP) -> str:
+        ...
+
+    def clang_CompileCommand_getFilename(self, cmd: CObjP) -> str:
+        ...
+
+    def clang_CompileCommand_getNumArgs(self, cmd: CObjP) -> int:
+        ...
+
+    def clang_codeCompleteAt(
+        self,
+        tu: TranslationUnit,
+        filename: str,
+        line: int,
+        column: int,
+        unsaved_files: TUnion[int, Array[_CXUnsavedFile]],
+        num_unsaved_files: int,
+        options: int,
+    ) -> _Pointer[CCRStructure]:
+        ...
+
+    def clang_codeCompleteGetDiagnostic(
+        self, ccrs: CodeCompletionResults, key: int
+    ) -> Diagnostic:
+        ...
+
+    def clang_codeCompleteGetNumDiagnostics(self, ccrs: CodeCompletionResults) -> c_int:
+        ...
+
+    def clang_createIndex(self, excludeDecls: int, displayDiagnostics: int) -> CObjP:
+        ...
+
+    def clang_createTranslationUnit(self, index: Index, filename: str) -> CObjP:
+        ...
+
+    def clang_CXXConstructor_isConvertingConstructor(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_CXXConstructor_isCopyConstructor(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_CXXConstructor_isDefaultConstructor(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_CXXConstructor_isMoveConstructor(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_CXXField_isMutable(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_CXXMethod_isConst(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_CXXMethod_isDefaulted(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_CXXMethod_isDeleted(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_CXXMethod_isCopyAssignmentOperator(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_CXXMethod_isMoveAssignmentOperator(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_CXXMethod_isExplicit(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_CXXMethod_isPureVirtual(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_CXXMethod_isStatic(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_CXXMethod_isVirtual(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_CXXRecord_isAbstract(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_EnumDecl_isScoped(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_defaultDiagnosticDisplayOptions(self) -> int:
+        ...
+
+    def clang_defaultSaveOptions(self, tu: TranslationUnit) -> c_uint:
+        ...
+
+    def clang_disposeCodeCompleteResults(self, tu: CodeCompletionResults) -> None:
+        ...
+
+    def clang_disposeDiagnostic(self, diag: Diagnostic) -> None:
+        ...
+
+    def clang_disposeIndex(self, index: Index) -> None:
+        ...
+
+    def clang_disposeString(self, string: _CXString) -> None:
+        ...
+
+    def clang_disposeTokens(
+        self, tu: TranslationUnit, tokens: _Pointer[Token], count: c_uint
+    ) -> None:
+        ...
+
+    def clang_disposeTranslationUnit(self, tu: TranslationUnit) -> None:
+        ...
+
+    def clang_equalCursors(self, self_c: Cursor, other_c: Cursor) -> bool:
+        ...
+
+    def clang_equalLocations(
+        self, self_loc: SourceLocation, other_loc: SourceLocation
+    ) -> bool:
+        ...
+
+    def clang_equalRanges(self, self_r: SourceRange, other_r: SourceRange) -> bool:
+        ...
+
+    def clang_equalTypes(self, self_t: Type, other_t: Type) -> bool:
+        ...
+
+    def clang_formatDiagnostic(self, diag: Diagnostic, options: int) -> str:
+        ...
+
+    def clang_getAddressSpace(self, type: Type) -> int:
+        ...
+
+    def clang_getArgType(self, parent: Type, key: int) -> Type:
+        ...
+
+    def clang_getArrayElementType(self, type: Type) -> Type:
+        ...
+
+    def clang_getArraySize(self, type: Type) -> int:
+        ...
+
+    def clang_getFieldDeclBitWidth(self, cursor: Cursor) -> int:
+        ...
+
+    def clang_getCanonicalCursor(self, cursor: Cursor) -> Cursor:
+        ...
+
+    def clang_getCanonicalType(self, type: Type) -> Type:
+        ...
+
+    def clang_getChildDiagnostics(self, diag: Diagnostic) -> CObjP:
+        ...
+
+    def clang_getCompletionAvailability(self, cs_obj: CObjP) -> int:
+        ...
+
+    def clang_getCompletionBriefComment(self, cs_obj: CObjP) -> str:
+        ...
+
+    def clang_getCompletionChunkCompletionString(
+        self, cs_obj: CObjP, key: int
+    ) -> CObjP:
+        ...
+
+    def clang_getCompletionChunkKind(self, cs_obj: CObjP, key: int) -> int:
+        ...
+
+    def clang_getCompletionChunkText(self, cs_obj: CObjP, key: int) -> str:
+        ...
+
+    def clang_getCompletionPriority(self, cs_obj: CObjP) -> int:
+        ...
+
+    def clang_getCString(self, string: _CXString) -> str | None:
+        ...
+
+    def clang_getCursor(self, tu: TranslationUnit, loc: SourceLocation) -> Cursor:
+        ...
+
+    def clang_getCursorAvailability(self, cursor: Cursor) -> int:
+        ...
+
+    def clang_getCursorDefinition(self, cursor: Cursor) -> Cursor:
+        ...
+
+    def clang_getCursorDisplayName(self, cursor: Cursor) -> str:
+        ...
+
+    def clang_getCursorExceptionSpecificationType(self, cursor: Cursor) -> int:
+        ...
+
+    def clang_getCursorExtent(self, cursor: Cursor) -> SourceRange:
+        ...
+
+    def clang_getCursorLexicalParent(self, cursor: Cursor) -> Cursor:
+        ...
+
+    def clang_getCursorLinkage(self, cursor: Cursor) -> int:
+        ...
+
+    def clang_getCursorLocation(self, cursor: Cursor) -> SourceLocation:
+        ...
+
+    def clang_getCursorReferenced(self, cursor: Cursor) -> Cursor:
+        ...
+
+    def clang_getCursorResultType(self, cursor: Cursor) -> Type:
+        ...
+
+    def clang_getCursorSemanticParent(self, cursor: Cursor) -> Cursor:
+        ...
+
+    def clang_getCursorSpelling(self, cursor: Cursor) -> str:
+        ...
+
+    def clang_getCursorTLSKind(self, cursor: Cursor) -> int:
+        ...
+
+    def clang_getCursorType(self, cursor: Cursor) -> Type:
+        ...
+
+    def clang_getCursorUSR(self, cursor: Cursor) -> str:
+        ...
+
+    def clang_Cursor_getMangling(self, cursor: Cursor) -> str:
+        ...
+
+    def clang_getCXXAccessSpecifier(self, cursor: Cursor) -> int:
+        ...
+
+    def clang_getDeclObjCTypeEncoding(self, cursor: Cursor) -> str:
+        ...
+
+    def clang_getDiagnostic(self, tu: TranslationUnit, key: int) -> CObjP:
+        ...
+
+    def clang_getDiagnosticCategory(self, diag: Diagnostic) -> int:
+        ...
+
+    def clang_getDiagnosticCategoryText(self, diag: Diagnostic) -> str:
+        ...
+
+    def clang_getDiagnosticFixIt(
+        self, diag: Diagnostic, key: int, sr: _CArgObject
+    ) -> str:
+        ...
+
+    def clang_getDiagnosticInSet(self, diag_set: CObjP, key: int) -> CObjP:
+        ...
+
+    def clang_getDiagnosticLocation(self, diag: Diagnostic) -> SourceLocation:
+        ...
+
+    def clang_getDiagnosticNumFixIts(self, diag: Diagnostic) -> c_uint:
+        ...
+
+    def clang_getDiagnosticNumRanges(self, diag: Diagnostic) -> c_uint:
+        ...
+
+    def clang_getDiagnosticOption(
+        self, diag: Diagnostic, disable: _CArgObject | None
+    ) -> str:
+        ...
+
+    def clang_getDiagnosticRange(self, diag: Diagnostic, key: int) -> SourceRange:
+        ...
+
+    def clang_getDiagnosticSeverity(self, diag: Diagnostic) -> int:
+        ...
+
+    def clang_getDiagnosticSpelling(self, diag: Diagnostic) -> str:
+        ...
+
+    def clang_getElementType(self, type: Type) -> Type:
+        ...
+
+    def clang_getEnumConstantDeclUnsignedValue(self, cursor: Cursor) -> int:
+        ...
+
+    def clang_getEnumConstantDeclValue(self, cursor: Cursor) -> int:
+        ...
+
+    def clang_getEnumDeclIntegerType(self, cursor: Cursor) -> Type:
+        ...
+
+    def clang_getExceptionSpecificationType(self, type: Type) -> int:
+        ...
+
+    def clang_getFile(self, tu: TranslationUnit, filename: str) -> CObjP:
+        ...
+
+    def clang_getFileName(self, file: File) -> str:
+        ...
+
+    def clang_getFileTime(self, file: File) -> int:
+        ...
+
+    def clang_getIncludedFile(self, cursor: Cursor) -> File:
+        ...
+
+    def clang_getInclusions(
+        self,
+        tu: TranslationUnit,
+        callback: _FuncPointer,
+        inclusions: list[FileInclusion],
+    ) -> None:
+        ...
+
+    def clang_getInstantiationLocation(
+        self,
+        loc: SourceLocation,
+        file: _CArgObject,
+        line: _CArgObject,
+        column: _CArgObject,
+        offset: _CArgObject,
+    ) -> None:
+        ...
+
+    def clang_getLocation(
+        self, tu: TranslationUnit, file: File, line: int, column: int
+    ) -> SourceLocation:
+        ...
+
+    def clang_getLocationForOffset(
+        self, tu: TranslationUnit, file: File, offset: int
+    ) -> SourceLocation:
+        ...
+
+    def clang_getNullCursor(self) -> Cursor:
+        ...
+
+    def clang_getNumArgTypes(self, type: Type) -> int:
+        ...
+
+    def clang_getNumCompletionChunks(self, cs_obj: CObjP) -> int:
+        ...
+
+    def clang_getNumDiagnostics(self, tu: TranslationUnit) -> c_uint:
+        ...
+
+    def clang_getNumDiagnosticsInSet(self, diag_set: CObjP) -> c_uint:
+        ...
+
+    def clang_getNumElements(self, type: Type) -> int:
+        ...
+
+    def clang_getPointeeType(self, type: Type) -> Type:
+        ...
+
+    def clang_getRange(self, start: SourceLocation, end: SourceLocation) -> SourceRange:
+        ...
+
+    def clang_getRangeEnd(self, range: SourceRange) -> SourceLocation:
+        ...
+
+    def clang_getRangeStart(self, range: SourceRange) -> SourceLocation:
+        ...
+
+    def clang_getResultType(self, type: Type) -> Type:
+        ...
+
+    def clang_getTokenExtent(self, tu: TranslationUnit, token: Token) -> SourceRange:
+        ...
+
+    def clang_getTokenKind(self, token: Token) -> int:
+        ...
+
+    def clang_getTokenLocation(
+        self, tu: TranslationUnit, token: Token
+    ) -> SourceLocation:
+        ...
+
+    def clang_getTokenSpelling(self, tu: TranslationUnit, token: Token) -> str:
+        ...
+
+    def clang_getTranslationUnitCursor(self, tu: TranslationUnit) -> Cursor:
+        ...
+
+    def clang_getTranslationUnitSpelling(self, tu: TranslationUnit) -> str:
+        ...
+
+    def clang_getTypeDeclaration(self, type: Type) -> Cursor:
+        ...
+
+    def clang_getTypedefDeclUnderlyingType(self, cursor: Cursor) -> Type:
+        ...
+
+    def clang_getTypedefName(self, type: Type) -> str:
+        ...
+
+    def clang_getTypeKindSpelling(self, kind: int) -> str:
+        ...
+
+    def clang_getTypeSpelling(self, type: Type) -> str:
+        ...
+
+    def clang_hashCursor(self, cursor: Cursor) -> int:
+        ...
+
+    def clang_isAttribute(self, kind: CursorKind) -> bool:
+        ...
+
+    def clang_isConstQualifiedType(self, type: Type) -> bool:
+        ...
+
+    def clang_isCursorDefinition(self, cursor: Cursor) -> bool:
+        ...
 
+    def clang_isDeclaration(self, kind: CursorKind) -> bool:
+        ...
 
-# Importing ABC-s directly from collections is deprecated since Python 3.7,
-# will stop working in Python 3.8.
-# See: https://docs.python.org/dev/whatsnew/3.7.html#id3
-if sys.version_info[:2] >= (3, 7):
-    from collections import abc as collections_abc
-else:
-    import collections as collections_abc
+    def clang_isExpression(self, kind: CursorKind) -> bool:
+        ...
 
-# We only support PathLike objects on Python version with os.fspath present
-# to be consistent with the Python standard library. On older Python versions
-# we only support strings and we have dummy fspath to just pass them through.
-try:
-    fspath = os.fspath
-except AttributeError:
+    def clang_isFunctionTypeVariadic(self, type: Type) -> bool:
+        ...
 
-    def fspath(x):
+    def clang_isInvalid(self, kind: CursorKind) -> bool:
+        ...
+
+    def clang_isPODType(self, type: Type) -> bool:
+        ...
+
+    def clang_isPreprocessing(self, kind: CursorKind) -> bool:
+        ...
+
+    def clang_isReference(self, kind: CursorKind) -> bool:
+        ...
+
+    def clang_isRestrictQualifiedType(self, type: Type) -> bool:
+        ...
+
+    def clang_isStatement(self, kind: CursorKind) -> bool:
+        ...
+
+    def clang_isTranslationUnit(self, kind: CursorKind) -> bool:
+        ...
+
+    def clang_isUnexposed(self, kind: CursorKind) -> bool:
+        ...
+
+    def clang_isVolatileQualifiedType(self, type: Type) -> bool:
+        ...
+
+    def clang_parseTranslationUnit(
+        self,
+        index: Index,
+        filename: str | None,
+        args_array: Array[c_char_p] | None,
+        num_args: int,
+        unsaved_files: Array[_CXUnsavedFile] | None,
+        num_unsaved: int,
+        options: int,
+    ) -> CObjP:
+        ...
+
+    def clang_reparseTranslationUnit(
+        self,
+        tu: TranslationUnit,
+        num_unsaved: int,
+        unsabed_files: Array[_CXUnsavedFile] | int,
+        options: int,
+    ) -> c_int:
+        ...
+
+    def clang_saveTranslationUnit(
+        self, tu: TranslationUnit, filename: str, options: c_uint
+    ) -> int:
+        ...
+
+    def clang_tokenize(
+        self,
+        tu: TranslationUnit,
+        range: SourceRange,
+        tokens_memory: _CArgObject,
+        tokens_count: _CArgObject,
+    ) -> None:
+        ...
+
+    def clang_visitChildren(
+        self, cursor: Cursor, callback: _FuncPointer, children: list[Cursor]
+    ) -> c_uint:
+        ...
+
+    def clang_Cursor_getNumArguments(self, cursor: Cursor) -> int:
+        ...
+
+    def clang_Cursor_getArgument(self, cursor: Cursor, key: int) -> Cursor:
+        ...
+
+    def clang_Cursor_getNumTemplateArguments(self, cursor: Cursor) -> int:
+        ...
+
+    def clang_Cursor_getStorageClass(self, cursor: Cursor) -> int:
+        ...
+
+    def clang_Cursor_getTemplateArgumentKind(
+        self, cursor: Cursor, key: int
+    ) -> TemplateArgumentKind:
+        ...
+
+    def clang_Cursor_getTemplateArgumentType(self, cursor: Cursor, key: int) -> Type:
+        ...
+
+    def clang_Cursor_getTemplateArgumentValue(self, cursor: Cursor, key: int) -> int:
+        ...
+
+    def clang_Cursor_getTemplateArgumentUnsignedValue(
+        self, cursor: Cursor, key: int
+    ) -> int:
+        ...
+
+    def clang_Cursor_isAnonymous(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_Cursor_isBitField(self, cursor: Cursor) -> bool:
+        ...
+
+    def clang_Cursor_getBriefCommentText(self, cursor: Cursor) -> str:
+        ...
+
+    def clang_Cursor_getRawCommentText(self, cursor: Cursor) -> str:
+        ...
+
+    def clang_Cursor_getOffsetOfField(self, cursor: Cursor) -> int:
+        ...
+
+    def clang_Location_isInSystemHeader(self, loc: SourceLocation) -> bool:
+        ...
+
+    def clang_Type_getAlignOf(self, type: Type) -> int:
+        ...
+
+    def clang_Type_getClassType(self, type: Type) -> Type:
+        ...
+
+    def clang_Type_getNumTemplateArguments(self, type: Type) -> int:
+        ...
+
+    def clang_Type_getTemplateArgumentAsType(self, type: Type, key: int) -> Type:
+        ...
+
+    def clang_Type_getOffsetOf(self, type: Type, fieldname: str) -> int:
+        ...
+
+    def clang_Type_getSizeOf(self, type: Type) -> int:
+        ...
+
+    def clang_Type_getCXXRefQualifier(self, type: Type) -> int:
+        ...
+
+    def clang_Type_getNamedType(self, type: Type) -> Type:
+        ...
+
+    def clang_Type_visitFields(
+        self, type: Type, callback: _FuncPointer, fields: list[Cursor]
+    ) -> c_uint:
+        ...
+
+
+# Python 3 strings are unicode, translate them to/from utf8 for C-interop.
+class c_interop_string(c_char_p):
+    def __init__(self, p: str | bytes | None = None):
+        if p is None:
+            p = ""
+        if isinstance(p, str):
+            p = p.encode("utf8")
+        super(c_char_p, self).__init__(p)
+
+    def __str__(self) -> str:
+        return self.value or ""
+
+    @property
+    def value(self) -> str | None:  # type: ignore
+        val = super(c_char_p, self).value
+        if val is None:
+            return None
+        return val.decode("utf8")
+
+    @classmethod
+    def from_param(cls, param: str | bytes | None) -> c_interop_string:
+        if isinstance(param, str):
+            return cls(param)
+        if isinstance(param, bytes):
+            return cls(param)
+        if param is None:
+            # Support passing null to C functions expecting char arrays
+            return cls(param)
+        raise TypeError(
+            "Cannot convert '{}' to '{}'".format(type(param).__name__, cls.__name__)
+        )
+
+    @staticmethod
+    def to_python_string(x: c_interop_string, *args: Any) -> str | None:
+        return x.value
+
+
+def b(x: str | bytes) -> bytes:
+    if isinstance(x, bytes):
         return x
+    return x.encode("utf8")
 
 
 # ctypes doesn't implicitly convert c_void_p to the appropriate wrapper
 # object. This is a problem, because it means that from_parameter will see an
 # integer and pass the wrong value on platforms where int != void*. Work around
 # this by marshalling object arguments as void**.
-c_object_p = POINTER(c_void_p)
-
-callbacks = {}
+c_object_p: TType[_Pointer[Any]] = POINTER(c_void_p)
 
 ### Exception Classes ###
 
@@ -185,7 +786,7 @@ class TranslationUnitSaveError(Exception):
     # Indicates that the translation unit was somehow invalid.
     ERROR_INVALID_TU = 3
 
-    def __init__(self, enumeration, message):
+    def __init__(self, enumeration: int, message: str):
         assert isinstance(enumeration, int)
 
         if enumeration < 1 or enumeration > 3:
@@ -201,8 +802,11 @@ def __init__(self, enumeration, message):
 
 ### Structures and Utility Classes ###
 
+TInstance = TypeVar("TInstance")
+TResult = TypeVar("TResult")
 
-class CachedProperty(object):
+
+class CachedProperty(Generic[TInstance, TResult]):
     """Decorator that lazy-loads the value of a property.
 
     The first time the property is accessed, the original property function is
@@ -210,16 +814,20 @@ class CachedProperty(object):
     property, replacing the original method.
     """
 
-    def __init__(self, wrapped):
+    def __init__(self, wrapped: Callable[[TInstance], TResult]):
         self.wrapped = wrapped
         try:
             self.__doc__ = wrapped.__doc__
         except:
             pass
 
-    def __get__(self, instance, instance_type=None):
+    def __get__(self, instance: TInstance, instance_type: Any = None) -> TResult:
         if instance is None:
-            return self
+            property_name = self.wrapped.__name__
+            class_name = instance_type.__name__
+            raise TypeError(
+                f"{property_name} is not a static attribute of {class_name}"
+            )
 
         value = self.wrapped(instance)
         setattr(instance, self.wrapped.__name__, value)
@@ -232,13 +840,16 @@ class _CXString(Structure):
 
     _fields_ = [("spelling", c_char_p), ("free", c_int)]
 
-    def __del__(self):
+    def __del__(self) -> None:
         conf.lib.clang_disposeString(self)
 
     @staticmethod
-    def from_result(res, fn=None, args=None):
+    def from_result(res: _CXString, fn: Any = None, args: Any = None) -> str:
         assert isinstance(res, _CXString)
-        return conf.lib.clang_getCString(res)
+        pystr = conf.lib.clang_getCString(res)
+        if pystr is None:
+            return ""
+        return pystr
 
 
 class SourceLocation(Structure):
@@ -247,23 +858,25 @@ class SourceLocation(Structure):
     """
 
     _fields_ = [("ptr_data", c_void_p * 2), ("int_data", c_uint)]
-    _data = None
+    _data: tuple[File | None, int, int, int] | None = None
 
-    def _get_instantiation(self):
+    def _get_instantiation(self) -> tuple[File | None, int, int, int]:
         if self._data is None:
             f, l, c, o = c_object_p(), c_uint(), c_uint(), c_uint()
             conf.lib.clang_getInstantiationLocation(
                 self, byref(f), byref(l), byref(c), byref(o)
             )
             if f:
-                f = File(f)
+                file = File(f)
             else:
-                f = None
-            self._data = (f, int(l.value), int(c.value), int(o.value))
+                file = None
+            self._data = (file, int(l.value), int(c.value), int(o.value))
         return self._data
 
     @staticmethod
-    def from_position(tu, file, line, column):
+    def from_position(
+        tu: TranslationUnit, file: File, line: int, column: int
+    ) -> SourceLocation:
         """
         Retrieve the source location associated with a given file/line/column in
         a particular translation unit.
@@ -271,7 +884,7 @@ def from_position(tu, file, line, column):
         return conf.lib.clang_getLocation(tu, file, line, column)
 
     @staticmethod
-    def from_offset(tu, file, offset):
+    def from_offset(tu: TranslationUnit, file: File, offset: int) -> SourceLocation:
         """Retrieve a SourceLocation from a given character offset.
 
         tu -- TranslationUnit file belongs to
@@ -281,37 +894,39 @@ def from_offset(tu, file, offset):
         return conf.lib.clang_getLocationForOffset(tu, file, offset)
 
     @property
-    def file(self):
+    def file(self) -> File | None:
         """Get the file represented by this source location."""
         return self._get_instantiation()[0]
 
     @property
-    def line(self):
+    def line(self) -> int:
         """Get the line represented by this source location."""
         return self._get_instantiation()[1]
 
     @property
-    def column(self):
+    def column(self) -> int:
         """Get the column represented by this source location."""
         return self._get_instantiation()[2]
 
     @property
-    def offset(self):
+    def offset(self) -> int:
         """Get the file offset represented by this source location."""
         return self._get_instantiation()[3]
 
     @property
-    def is_in_system_header(self):
+    def is_in_system_header(self) -> bool:
         """Returns true if the given source location is in a system header."""
         return conf.lib.clang_Location_isInSystemHeader(self)
 
-    def __eq__(self, other):
+    def __eq__(self, other: object) -> bool:
+        if not isinstance(other, SourceLocation):
+            return False
         return conf.lib.clang_equalLocations(self, other)
 
-    def __ne__(self, other):
+    def __ne__(self, other: object) -> bool:
         return not self.__eq__(other)
 
-    def __repr__(self):
+    def __repr__(self) -> str:
         if self.file:
             filename = self.file.name
         else:
@@ -338,11 +953,11 @@ class SourceRange(Structure):
     # FIXME: Eliminate this and make normal constructor? Requires hiding ctypes
     # object.
     @staticmethod
-    def from_locations(start, end):
+    def from_locations(start: SourceLocation, end: SourceLocation) -> SourceRange:
         return conf.lib.clang_getRange(start, end)
 
     @property
-    def start(self):
+    def start(self) -> SourceLocation:
         """
         Return a SourceLocation representing the first character within a
         source range.
@@ -350,25 +965,27 @@ def start(self):
         return conf.lib.clang_getRangeStart(self)
 
     @property
-    def end(self):
+    def end(self) -> SourceLocation:
         """
         Return a SourceLocation representing the last character within a
         source range.
         """
         return conf.lib.clang_getRangeEnd(self)
 
-    def __eq__(self, other):
+    def __eq__(self, other: object) -> bool:
+        if not isinstance(other, SourceRange):
+            return False
         return conf.lib.clang_equalRanges(self, other)
 
-    def __ne__(self, other):
+    def __ne__(self, other: object) -> bool:
         return not self.__eq__(other)
 
-    def __contains__(self, other):
+    def __contains__(self, other: object) -> bool:
         """Useful to detect the Token/Lexer bug"""
         if not isinstance(other, SourceLocation):
             return False
-        if other.file is None and self.start.file is None:
-            pass
+        if other.file is None or self.start.file is None or self.end.file is None:
+            return False
         elif (
             self.start.file.name != other.file.name
             or other.file.name != self.end.file.name
@@ -388,11 +1005,11 @@ def __contains__(self, other):
                 return True
         return False
 
-    def __repr__(self):
+    def __repr__(self) -> str:
         return "<SourceRange start %r, end %r>" % (self.start, self.end)
 
 
-class Diagnostic(object):
+class Diagnostic:
     """
     A Diagnostic is a single instance of a Clang diagnostic. It includes the
     diagnostic severity, the message, the location the diagnostic occurred, as
@@ -413,34 +1030,34 @@ class Diagnostic(object):
     DisplayCategoryName = 0x20
     _FormatOptionsMask = 0x3F
 
-    def __init__(self, ptr):
+    def __init__(self, ptr: CObjP):
         self.ptr = ptr
 
-    def __del__(self):
+    def __del__(self) -> None:
         conf.lib.clang_disposeDiagnostic(self)
 
     @property
-    def severity(self):
+    def severity(self) -> int:
         return conf.lib.clang_getDiagnosticSeverity(self)
 
     @property
-    def location(self):
+    def location(self) -> SourceLocation:
         return conf.lib.clang_getDiagnosticLocation(self)
 
     @property
-    def spelling(self):
+    def spelling(self) -> str:
         return conf.lib.clang_getDiagnosticSpelling(self)
 
     @property
-    def ranges(self):
-        class RangeIterator(object):
-            def __init__(self, diag):
+    def ranges(self) -> NoSliceSequence[SourceRange]:
+        class RangeIterator:
+            def __init__(self, diag: Diagnostic):
                 self.diag = diag
 
-            def __len__(self):
+            def __len__(self) -> int:
                 return int(conf.lib.clang_getDiagnosticNumRanges(self.diag))
 
-            def __getitem__(self, key):
+            def __getitem__(self, key: int) -> SourceRange:
                 if key >= len(self):
                     raise IndexError
                 return conf.lib.clang_getDiagnosticRange(self.diag, key)
@@ -448,15 +1065,15 @@ def __getitem__(self, key):
         return RangeIterator(self)
 
     @property
-    def fixits(self):
-        class FixItIterator(object):
-            def __init__(self, diag):
+    def fixits(self) -> NoSliceSequence[FixIt]:
+        class FixItIterator:
+            def __init__(self, diag: Diagnostic):
                 self.diag = diag
 
-            def __len__(self):
+            def __len__(self) -> int:
                 return int(conf.lib.clang_getDiagnosticNumFixIts(self.diag))
 
-            def __getitem__(self, key):
+            def __getitem__(self, key: int) -> FixIt:
                 range = SourceRange()
                 value = conf.lib.clang_getDiagnosticFixIt(self.diag, key, byref(range))
                 if len(value) == 0:
@@ -467,15 +1084,15 @@ def __getitem__(self, key):
         return FixItIterator(self)
 
     @property
-    def children(self):
-        class ChildDiagnosticsIterator(object):
-            def __init__(self, diag):
+    def children(self) -> NoSliceSequence[Diagnostic]:
+        class ChildDiagnosticsIterator:
+            def __init__(self, diag: Diagnostic):
                 self.diag_set = conf.lib.clang_getChildDiagnostics(diag)
 
-            def __len__(self):
+            def __len__(self) -> int:
                 return int(conf.lib.clang_getNumDiagnosticsInSet(self.diag_set))
 
-            def __getitem__(self, key):
+            def __getitem__(self, key: int) -> Diagnostic:
                 diag = conf.lib.clang_getDiagnosticInSet(self.diag_set, key)
                 if not diag:
                     raise IndexError
@@ -484,28 +1101,28 @@ def __getitem__(self, key):
         return ChildDiagnosticsIterator(self)
 
     @property
-    def category_number(self):
+    def category_number(self) -> int:
         """The category number for this diagnostic or 0 if unavailable."""
         return conf.lib.clang_getDiagnosticCategory(self)
 
     @property
-    def category_name(self):
+    def category_name(self) -> str:
         """The string name of the category for this diagnostic."""
         return conf.lib.clang_getDiagnosticCategoryText(self)
 
     @property
-    def option(self):
+    def option(self) -> str:
         """The command-line option that enables this diagnostic."""
         return conf.lib.clang_getDiagnosticOption(self, None)
 
     @property
-    def disable_option(self):
+    def disable_option(self) -> str:
         """The command-line option that disables this diagnostic."""
         disable = _CXString()
         conf.lib.clang_getDiagnosticOption(self, byref(disable))
         return _CXString.from_result(disable)
 
-    def format(self, options=None):
+    def format(self, options: int | None = None) -> str:
         """
         Format this diagnostic for display. The options argument takes
         Diagnostic.Display* flags, which can be combined using bitwise OR. If
@@ -518,36 +1135,36 @@ def format(self, options=None):
             raise ValueError("Invalid format options")
         return conf.lib.clang_formatDiagnostic(self, options)
 
-    def __repr__(self):
+    def __repr__(self) -> str:
         return "<Diagnostic severity %r, location %r, spelling %r>" % (
             self.severity,
             self.location,
             self.spelling,
         )
 
-    def __str__(self):
+    def __str__(self) -> str:
         return self.format()
 
-    def from_param(self):
+    def from_param(self) -> CObjP:
         return self.ptr
 
 
-class FixIt(object):
+class FixIt:
     """
     A FixIt represents a transformation to be applied to the source to
     "fix-it". The fix-it should be applied by replacing the given source range
     with the given value.
     """
 
-    def __init__(self, range, value):
+    def __init__(self, range: SourceRange, value: str):
         self.range = range
         self.value = value
 
-    def __repr__(self):
+    def __repr__(self) -> str:
         return "<FixIt range %r, value %r>" % (self.range, self.value)
 
 
-class TokenGroup(object):
+class TokenGroup:
     """Helper class to facilitate token management.
 
     Tokens are allocated from libclang in chunks. They must be disposed of as a
@@ -562,16 +1179,16 @@ class TokenGroup(object):
     You should not instantiate this class outside of this module.
     """
 
-    def __init__(self, tu, memory, count):
+    def __init__(self, tu: TranslationUnit, memory: _Pointer[Token], count: c_uint):
         self._tu = tu
         self._memory = memory
         self._count = count
 
-    def __del__(self):
+    def __del__(self) -> None:
         conf.lib.clang_disposeTokens(self._tu, self._memory, self._count)
 
     @staticmethod
-    def get_tokens(tu, extent):
+    def get_tokens(tu: TranslationUnit, extent: SourceRange) -> Iterator[Token]:
         """Helper method to return all tokens in an extent.
 
         This functionality is needed multiple places in this module. We define
@@ -603,21 +1220,21 @@ def get_tokens(tu, extent):
             yield token
 
 
-class TokenKind(object):
+class TokenKind:
     """Describes a specific type of a Token."""
 
-    _value_map = {}  # int -> TokenKind
+    _value_map: dict[int, TokenKind] = {}
 
-    def __init__(self, value, name):
+    def __init__(self, value: int, name: str):
         """Create a new TokenKind instance from a numeric value and a name."""
         self.value = value
         self.name = name
 
-    def __repr__(self):
+    def __repr__(self) -> str:
         return "TokenKind.%s" % (self.name,)
 
     @staticmethod
-    def from_value(value):
+    def from_value(value: int) -> TokenKind:
         """Obtain a registered TokenKind instance from its value."""
         result = TokenKind._value_map.get(value, None)
 
@@ -627,7 +1244,7 @@ def from_value(value):
         return result
 
     @staticmethod
-    def register(value, name):
+    def register(value: int, name: str) -> None:
         """Register a new TokenKind enumeration.
 
         This should only be called at module load time by code within this
@@ -642,51 +1259,29 @@ def register(value, name):
 
 
 ### Cursor Kinds ###
-class BaseEnumeration(object):
-    """
-    Common base class for named enumerations held in sync with Index.h values.
+TEnum = TypeVar("TEnum", bound="BaseEnumeration")
 
-    Subclasses must define their own _kinds and _name_map members, as:
-    _kinds = []
-    _name_map = None
-    These values hold the per-subclass instances and value-to-name mappings,
-    respectively.
 
+class BaseEnumeration(Enum):
+    """
+    Common base class for named enumerations held in sync with Index.h values.
     """
 
-    def __init__(self, value):
-        if value >= len(self.__class__._kinds):
-            self.__class__._kinds += [None] * (value - len(self.__class__._kinds) + 1)
-        if self.__class__._kinds[value] is not None:
-            raise ValueError(
-                "{0} value {1} already loaded".format(str(self.__class__), value)
-            )
-        self.value = value
-        self.__class__._kinds[value] = self
-        self.__class__._name_map = None
+    value: int
 
-    def from_param(self):
+    def from_param(self) -> int:
         return self.value
 
-    @property
-    def name(self):
-        """Get the enumeration name of this cursor kind."""
-        if self._name_map is None:
-            self._name_map = {}
-            for key, value in self.__class__.__dict__.items():
-                if isinstance(value, self.__class__):
-                    self._name_map[value] = key
-        return self._name_map[self]
-
     @classmethod
-    def from_id(cls, id):
-        if id >= len(cls._kinds) or cls._kinds[id] is None:
-            raise ValueError("Unknown template argument kind %d" % id)
-        return cls._kinds[id]
+    def from_id(cls: type[TEnum], id: int) -> TEnum:
+        try:
+            return cls(id)
+        except ValueError:
+            raise ValueError("Unknown %s %d" % (cls.__class__, id))
 
-    def __repr__(self):
+    def __repr__(self) -> str:
         return "%s.%s" % (
-            self.__class__,
+            self.__class__.__name__,
             self.name,
         )
 
@@ -696,718 +1291,708 @@ class CursorKind(BaseEnumeration):
     A CursorKind describes the kind of entity that a cursor points to.
     """
 
-    # The required BaseEnumeration declarations.
-    _kinds = []
-    _name_map = None
-
     @staticmethod
-    def get_all_kinds():
+    def get_all_kinds() -> list[CursorKind]:
         """Return all CursorKind enumeration instances."""
-        return [x for x in CursorKind._kinds if not x is None]
+        return [x for x in CursorKind]
 
-    def is_declaration(self):
+    def is_declaration(self) -> bool:
         """Test if this is a declaration kind."""
         return conf.lib.clang_isDeclaration(self)
 
-    def is_reference(self):
+    def is_reference(self) -> bool:
         """Test if this is a reference kind."""
         return conf.lib.clang_isReference(self)
 
-    def is_expression(self):
+    def is_expression(self) -> bool:
         """Test if this is an expression kind."""
         return conf.lib.clang_isExpression(self)
 
-    def is_statement(self):
+    def is_statement(self) -> bool:
         """Test if this is a statement kind."""
         return conf.lib.clang_isStatement(self)
 
-    def is_attribute(self):
+    def is_attribute(self) -> bool:
         """Test if this is an attribute kind."""
         return conf.lib.clang_isAttribute(self)
 
-    def is_invalid(self):
+    def is_invalid(self) -> bool:
         """Test if this is an invalid kind."""
         return conf.lib.clang_isInvalid(self)
 
-    def is_translation_unit(self):
+    def is_translation_unit(self) -> bool:
         """Test if this is a translation unit kind."""
         return conf.lib.clang_isTranslationUnit(self)
 
-    def is_preprocessing(self):
+    def is_preprocessing(self) -> bool:
         """Test if this is a preprocessing kind."""
         return conf.lib.clang_isPreprocessing(self)
 
-    def is_unexposed(self):
+    def is_unexposed(self) -> bool:
         """Test if this is an unexposed kind."""
         return conf.lib.clang_isUnexposed(self)
 
-    def __repr__(self):
-        return "CursorKind.%s" % (self.name,)
+    ###
+    # Declaration Kinds
 
+    # A declaration whose specific kind is not exposed via this interface.
+    #
+    # Unexposed declarations have the same operations as any other kind of
+    # declaration; one can extract their location information, spelling, find their
+    # definitions, etc. However, the specific kind of the declaration is not
+    # reported.
+    UNEXPOSED_DECL = 1
 
-###
-# Declaration Kinds
+    # A C or C++ struct.
+    STRUCT_DECL = 2
 
-# A declaration whose specific kind is not exposed via this interface.
-#
-# Unexposed declarations have the same operations as any other kind of
-# declaration; one can extract their location information, spelling, find their
-# definitions, etc. However, the specific kind of the declaration is not
-# reported.
-CursorKind.UNEXPOSED_DECL = CursorKind(1)
+    # A C or C++ union.
+    UNION_DECL = 3
 
-# A C or C++ struct.
-CursorKind.STRUCT_DECL = CursorKind(2)
+    # A C++ class.
+    CLASS_DECL = 4
 
-# A C or C++ union.
-CursorKind.UNION_DECL = CursorKind(3)
+    # An enumeration.
+    ENUM_DECL = 5
 
-# A C++ class.
-CursorKind.CLASS_DECL = CursorKind(4)
+    # A field (in C) or non-static data member (in C++) in a struct, union, or C++
+    # class.
+    FIELD_DECL = 6
 
-# An enumeration.
-CursorKind.ENUM_DECL = CursorKind(5)
+    # An enumerator constant.
+    ENUM_CONSTANT_DECL = 7
 
-# A field (in C) or non-static data member (in C++) in a struct, union, or C++
-# class.
-CursorKind.FIELD_DECL = CursorKind(6)
+    # A function.
+    FUNCTION_DECL = 8
 
-# An enumerator constant.
-CursorKind.ENUM_CONSTANT_DECL = CursorKind(7)
+    # A variable.
+    VAR_DECL = 9
 
-# A function.
-CursorKind.FUNCTION_DECL = CursorKind(8)
+    # A function or method parameter.
+    PARM_DECL = 10
 
-# A variable.
-CursorKind.VAR_DECL = CursorKind(9)
+    # An Objective-C @interface.
+    OBJC_INTERFACE_DECL = 11
 
-# A function or method parameter.
-CursorKind.PARM_DECL = CursorKind(10)
+    # An Objective-C @interface for a category.
+    OBJC_CATEGORY_DECL = 12
 
-# An Objective-C @interface.
-CursorKind.OBJC_INTERFACE_DECL = CursorKind(11)
+    # An Objective-C @protocol declaration.
+    OBJC_PROTOCOL_DECL = 13
 
-# An Objective-C @interface for a category.
-CursorKind.OBJC_CATEGORY_DECL = CursorKind(12)
+    # An Objective-C @property declaration.
+    OBJC_PROPERTY_DECL = 14
 
-# An Objective-C @protocol declaration.
-CursorKind.OBJC_PROTOCOL_DECL = CursorKind(13)
+    # An Objective-C instance variable.
+    OBJC_IVAR_DECL = 15
 
-# An Objective-C @property declaration.
-CursorKind.OBJC_PROPERTY_DECL = CursorKind(14)
+    # An Objective-C instance method.
+    OBJC_INSTANCE_METHOD_DECL = 16
 
-# An Objective-C instance variable.
-CursorKind.OBJC_IVAR_DECL = CursorKind(15)
+    # An Objective-C class method.
+    OBJC_CLASS_METHOD_DECL = 17
 
-# An Objective-C instance method.
-CursorKind.OBJC_INSTANCE_METHOD_DECL = CursorKind(16)
+    # An Objective-C @implementation.
+    OBJC_IMPLEMENTATION_DECL = 18
 
-# An Objective-C class method.
-CursorKind.OBJC_CLASS_METHOD_DECL = CursorKind(17)
+    # An Objective-C @implementation for a category.
+    OBJC_CATEGORY_IMPL_DECL = 19
 
-# An Objective-C @implementation.
-CursorKind.OBJC_IMPLEMENTATION_DECL = CursorKind(18)
+    # A typedef.
+    TYPEDEF_DECL = 20
 
-# An Objective-C @implementation for a category.
-CursorKind.OBJC_CATEGORY_IMPL_DECL = CursorKind(19)
+    # A C++ class method.
+    CXX_METHOD = 21
 
-# A typedef.
-CursorKind.TYPEDEF_DECL = CursorKind(20)
+    # A C++ namespace.
+    NAMESPACE = 22
 
-# A C++ class method.
-CursorKind.CXX_METHOD = CursorKind(21)
+    # A linkage specification, e.g. 'extern "C"'.
+    LINKAGE_SPEC = 23
 
-# A C++ namespace.
-CursorKind.NAMESPACE = CursorKind(22)
+    # A C++ constructor.
+    CONSTRUCTOR = 24
 
-# A linkage specification, e.g. 'extern "C"'.
-CursorKind.LINKAGE_SPEC = CursorKind(23)
+    # A C++ destructor.
+    DESTRUCTOR = 25
 
-# A C++ constructor.
-CursorKind.CONSTRUCTOR = CursorKind(24)
+    # A C++ conversion function.
+    CONVERSION_FUNCTION = 26
 
-# A C++ destructor.
-CursorKind.DESTRUCTOR = CursorKind(25)
+    # A C++ template type parameter
+    TEMPLATE_TYPE_PARAMETER = 27
 
-# A C++ conversion function.
-CursorKind.CONVERSION_FUNCTION = CursorKind(26)
+    # A C++ non-type template parameter.
+    TEMPLATE_NON_TYPE_PARAMETER = 28
 
-# A C++ template type parameter
-CursorKind.TEMPLATE_TYPE_PARAMETER = CursorKind(27)
+    # A C++ template template parameter.
+    TEMPLATE_TEMPLATE_PARAMETER = 29
 
-# A C++ non-type template parameter.
-CursorKind.TEMPLATE_NON_TYPE_PARAMETER = CursorKind(28)
+    # A C++ function template.
+    FUNCTION_TEMPLATE = 30
 
-# A C++ template template parameter.
-CursorKind.TEMPLATE_TEMPLATE_PARAMETER = CursorKind(29)
+    # A C++ class template.
+    CLASS_TEMPLATE = 31
 
-# A C++ function template.
-CursorKind.FUNCTION_TEMPLATE = CursorKind(30)
+    # A C++ class template partial specialization.
+    CLASS_TEMPLATE_PARTIAL_SPECIALIZATION = 32
 
-# A C++ class template.
-CursorKind.CLASS_TEMPLATE = CursorKind(31)
+    # A C++ namespace alias declaration.
+    NAMESPACE_ALIAS = 33
 
-# A C++ class template partial specialization.
-CursorKind.CLASS_TEMPLATE_PARTIAL_SPECIALIZATION = CursorKind(32)
+    # A C++ using directive
+    USING_DIRECTIVE = 34
 
-# A C++ namespace alias declaration.
-CursorKind.NAMESPACE_ALIAS = CursorKind(33)
+    # A C++ using declaration
+    USING_DECLARATION = 35
 
-# A C++ using directive
-CursorKind.USING_DIRECTIVE = CursorKind(34)
+    # A Type alias decl.
+    TYPE_ALIAS_DECL = 36
 
-# A C++ using declaration
-CursorKind.USING_DECLARATION = CursorKind(35)
+    # A Objective-C synthesize decl
+    OBJC_SYNTHESIZE_DECL = 37
 
-# A Type alias decl.
-CursorKind.TYPE_ALIAS_DECL = CursorKind(36)
+    # A Objective-C dynamic decl
+    OBJC_DYNAMIC_DECL = 38
 
-# A Objective-C synthesize decl
-CursorKind.OBJC_SYNTHESIZE_DECL = CursorKind(37)
+    # A C++ access specifier decl.
+    CXX_ACCESS_SPEC_DECL = 39
 
-# A Objective-C dynamic decl
-CursorKind.OBJC_DYNAMIC_DECL = CursorKind(38)
+    ###
+    # Reference Kinds
 
-# A C++ access specifier decl.
-CursorKind.CXX_ACCESS_SPEC_DECL = CursorKind(39)
+    OBJC_SUPER_CLASS_REF = 40
+    OBJC_PROTOCOL_REF = 41
+    OBJC_CLASS_REF = 42
 
+    # A reference to a type declaration.
+    #
+    # A type reference occurs anywhere where a type is named but not
+    # declared. For example, given:
+    #   typedef unsigned size_type;
+    #   size_type size;
+    #
+    # The typedef is a declaration of size_type (CXCursor_TypedefDecl),
+    # while the type of the variable "size" is referenced. The cursor
+    # referenced by the type of size is the typedef for size_type.
+    TYPE_REF = 43
+    CXX_BASE_SPECIFIER = 44
 
-###
-# Reference Kinds
+    # A reference to a class template, function template, template
+    # template parameter, or class template partial specialization.
+    TEMPLATE_REF = 45
 
-CursorKind.OBJC_SUPER_CLASS_REF = CursorKind(40)
-CursorKind.OBJC_PROTOCOL_REF = CursorKind(41)
-CursorKind.OBJC_CLASS_REF = CursorKind(42)
+    # A reference to a namespace or namepsace alias.
+    NAMESPACE_REF = 46
 
-# A reference to a type declaration.
-#
-# A type reference occurs anywhere where a type is named but not
-# declared. For example, given:
-#   typedef unsigned size_type;
-#   size_type size;
-#
-# The typedef is a declaration of size_type (CXCursor_TypedefDecl),
-# while the type of the variable "size" is referenced. The cursor
-# referenced by the type of size is the typedef for size_type.
-CursorKind.TYPE_REF = CursorKind(43)
-CursorKind.CXX_BASE_SPECIFIER = CursorKind(44)
+    # A reference to a member of a struct, union, or class that occurs in
+    # some non-expression context, e.g., a designated initializer.
+    MEMBER_REF = 47
 
-# A reference to a class template, function template, template
-# template parameter, or class template partial specialization.
-CursorKind.TEMPLATE_REF = CursorKind(45)
+    # A reference to a labeled statement.
+    LABEL_REF = 48
 
-# A reference to a namespace or namepsace alias.
-CursorKind.NAMESPACE_REF = CursorKind(46)
+    # A reference to a set of overloaded functions or function templates
+    # that has not yet been resolved to a specific function or function template.
+    OVERLOADED_DECL_REF = 49
 
-# A reference to a member of a struct, union, or class that occurs in
-# some non-expression context, e.g., a designated initializer.
-CursorKind.MEMBER_REF = CursorKind(47)
+    # A reference to a variable that occurs in some non-expression
+    # context, e.g., a C++ lambda capture list.
+    VARIABLE_REF = 50
 
-# A reference to a labeled statement.
-CursorKind.LABEL_REF = CursorKind(48)
+    ###
+    # Invalid/Error Kinds
 
-# A reference to a set of overloaded functions or function templates
-# that has not yet been resolved to a specific function or function template.
-CursorKind.OVERLOADED_DECL_REF = CursorKind(49)
+    INVALID_FILE = 70
+    NO_DECL_FOUND = 71
+    NOT_IMPLEMENTED = 72
+    INVALID_CODE = 73
 
-# A reference to a variable that occurs in some non-expression
-# context, e.g., a C++ lambda capture list.
-CursorKind.VARIABLE_REF = CursorKind(50)
+    ###
+    # Expression Kinds
 
-###
-# Invalid/Error Kinds
+    # An expression whose specific kind is not exposed via this interface.
+    #
+    # Unexposed expressions have the same operations as any other kind of
+    # expression; one can extract their location information, spelling, children,
+    # etc. However, the specific kind of the expression is not reported.
+    UNEXPOSED_EXPR = 100
 
-CursorKind.INVALID_FILE = CursorKind(70)
-CursorKind.NO_DECL_FOUND = CursorKind(71)
-CursorKind.NOT_IMPLEMENTED = CursorKind(72)
-CursorKind.INVALID_CODE = CursorKind(73)
+    # An expression that refers to some value declaration, such as a function,
+    # variable, or enumerator.
+    DECL_REF_EXPR = 101
 
-###
-# Expression Kinds
+    # An expression that refers to a member of a struct, union, class, Objective-C
+    # class, etc.
+    MEMBER_REF_EXPR = 102
 
-# An expression whose specific kind is not exposed via this interface.
-#
-# Unexposed expressions have the same operations as any other kind of
-# expression; one can extract their location information, spelling, children,
-# etc. However, the specific kind of the expression is not reported.
-CursorKind.UNEXPOSED_EXPR = CursorKind(100)
+    # An expression that calls a function.
+    CALL_EXPR = 103
 
-# An expression that refers to some value declaration, such as a function,
-# variable, or enumerator.
-CursorKind.DECL_REF_EXPR = CursorKind(101)
+    # An expression that sends a message to an Objective-C object or class.
+    OBJC_MESSAGE_EXPR = 104
 
-# An expression that refers to a member of a struct, union, class, Objective-C
-# class, etc.
-CursorKind.MEMBER_REF_EXPR = CursorKind(102)
+    # An expression that represents a block literal.
+    BLOCK_EXPR = 105
 
-# An expression that calls a function.
-CursorKind.CALL_EXPR = CursorKind(103)
+    # An integer literal.
+    INTEGER_LITERAL = 106
 
-# An expression that sends a message to an Objective-C object or class.
-CursorKind.OBJC_MESSAGE_EXPR = CursorKind(104)
+    # A floating point number literal.
+    FLOATING_LITERAL = 107
 
-# An expression that represents a block literal.
-CursorKind.BLOCK_EXPR = CursorKind(105)
+    # An imaginary number literal.
+    IMAGINARY_LITERAL = 108
 
-# An integer literal.
-CursorKind.INTEGER_LITERAL = CursorKind(106)
+    # A string literal.
+    STRING_LITERAL = 109
 
-# A floating point number literal.
-CursorKind.FLOATING_LITERAL = CursorKind(107)
+    # A character literal.
+    CHARACTER_LITERAL = 110
 
-# An imaginary number literal.
-CursorKind.IMAGINARY_LITERAL = CursorKind(108)
+    # A parenthesized expression, e.g. "(1)".
+    #
+    # This AST node is only formed if full location information is requested.
+    PAREN_EXPR = 111
 
-# A string literal.
-CursorKind.STRING_LITERAL = CursorKind(109)
+    # This represents the unary-expression's (except sizeof and
+    # alignof).
+    UNARY_OPERATOR = 112
 
-# A character literal.
-CursorKind.CHARACTER_LITERAL = CursorKind(110)
+    # [C99 6.5.2.1] Array Subscripting.
+    ARRAY_SUBSCRIPT_EXPR = 113
 
-# A parenthesized expression, e.g. "(1)".
-#
-# This AST node is only formed if full location information is requested.
-CursorKind.PAREN_EXPR = CursorKind(111)
+    # A builtin binary operation expression such as "x + y" or "x <= y".
+    BINARY_OPERATOR = 114
 
-# This represents the unary-expression's (except sizeof and
-# alignof).
-CursorKind.UNARY_OPERATOR = CursorKind(112)
+    # Compound assignment such as "+=".
+    COMPOUND_ASSIGNMENT_OPERATOR = 115
 
-# [C99 6.5.2.1] Array Subscripting.
-CursorKind.ARRAY_SUBSCRIPT_EXPR = CursorKind(113)
+    # The ?: ternary operator.
+    CONDITIONAL_OPERATOR = 116
 
-# A builtin binary operation expression such as "x + y" or
-# "x <= y".
-CursorKind.BINARY_OPERATOR = CursorKind(114)
+    # An explicit cast in C (C99 6.5.4) or a C-style cast in C++
+    # (C++ [expr.cast]), which uses the syntax (Type)expr.
+    #
+    # For example: (int)f.
+    CSTYLE_CAST_EXPR = 117
 
-# Compound assignment such as "+=".
-CursorKind.COMPOUND_ASSIGNMENT_OPERATOR = CursorKind(115)
+    # [C99 6.5.2.5]
+    COMPOUND_LITERAL_EXPR = 118
 
-# The ?: ternary operator.
-CursorKind.CONDITIONAL_OPERATOR = CursorKind(116)
+    # Describes an C or C++ initializer list.
+    INIT_LIST_EXPR = 119
 
-# An explicit cast in C (C99 6.5.4) or a C-style cast in C++
-# (C++ [expr.cast]), which uses the syntax (Type)expr.
-#
-# For example: (int)f.
-CursorKind.CSTYLE_CAST_EXPR = CursorKind(117)
+    # The GNU address of label extension, representing &&label.
+    ADDR_LABEL_EXPR = 120
 
-# [C99 6.5.2.5]
-CursorKind.COMPOUND_LITERAL_EXPR = CursorKind(118)
+    # This is the GNU Statement Expression extension: ({int X=4; X;})
+    StmtExpr = 121
 
-# Describes an C or C++ initializer list.
-CursorKind.INIT_LIST_EXPR = CursorKind(119)
+    # Represents a C11 generic selection.
+    GENERIC_SELECTION_EXPR = 122
 
-# The GNU address of label extension, representing &&label.
-CursorKind.ADDR_LABEL_EXPR = CursorKind(120)
+    # Implements the GNU __null extension, which is a name for a null
+    # pointer constant that has integral type (e.g., int or long) and is the same
+    # size and alignment as a pointer.
+    #
+    # The __null extension is typically only used by system headers, which define
+    # NULL as __null in C++ rather than using 0 (which is an integer that may not
+    # match the size of a pointer).
+    GNU_NULL_EXPR = 123
 
-# This is the GNU Statement Expression extension: ({int X=4; X;})
-CursorKind.StmtExpr = CursorKind(121)
+    # C++'s static_cast<> expression.
+    CXX_STATIC_CAST_EXPR = 124
 
-# Represents a C11 generic selection.
-CursorKind.GENERIC_SELECTION_EXPR = CursorKind(122)
+    # C++'s dynamic_cast<> expression.
+    CXX_DYNAMIC_CAST_EXPR = 125
 
-# Implements the GNU __null extension, which is a name for a null
-# pointer constant that has integral type (e.g., int or long) and is the same
-# size and alignment as a pointer.
-#
-# The __null extension is typically only used by system headers, which define
-# NULL as __null in C++ rather than using 0 (which is an integer that may not
-# match the size of a pointer).
-CursorKind.GNU_NULL_EXPR = CursorKind(123)
+    # C++'s reinterpret_cast<> expression.
+    CXX_REINTERPRET_CAST_EXPR = 126
 
-# C++'s static_cast<> expression.
-CursorKind.CXX_STATIC_CAST_EXPR = CursorKind(124)
+    # C++'s const_cast<> expression.
+    CXX_CONST_CAST_EXPR = 127
 
-# C++'s dynamic_cast<> expression.
-CursorKind.CXX_DYNAMIC_CAST_EXPR = CursorKind(125)
+    # Represents an explicit C++ type conversion that uses "functional"
+    # notion (C++ [expr.type.conv]).
+    #
+    # Example:
+    # \code
+    #   x = int(0.5);
+    # \endcode
+    CXX_FUNCTIONAL_CAST_EXPR = 128
 
-# C++'s reinterpret_cast<> expression.
-CursorKind.CXX_REINTERPRET_CAST_EXPR = CursorKind(126)
+    # A C++ typeid expression (C++ [expr.typeid]).
+    CXX_TYPEID_EXPR = 129
 
-# C++'s const_cast<> expression.
-CursorKind.CXX_CONST_CAST_EXPR = CursorKind(127)
+    # [C++ 2.13.5] C++ Boolean Literal.
+    CXX_BOOL_LITERAL_EXPR = 130
 
-# Represents an explicit C++ type conversion that uses "functional"
-# notion (C++ [expr.type.conv]).
-#
-# Example:
-# \code
-#   x = int(0.5);
-# \endcode
-CursorKind.CXX_FUNCTIONAL_CAST_EXPR = CursorKind(128)
+    # [C++0x 2.14.7] C++ Pointer Literal.
+    CXX_NULL_PTR_LITERAL_EXPR = 131
 
-# A C++ typeid expression (C++ [expr.typeid]).
-CursorKind.CXX_TYPEID_EXPR = CursorKind(129)
+    # Represents the "this" expression in C++
+    CXX_THIS_EXPR = 132
 
-# [C++ 2.13.5] C++ Boolean Literal.
-CursorKind.CXX_BOOL_LITERAL_EXPR = CursorKind(130)
+    # [C++ 15] C++ Throw Expression.
+    #
+    # This handles 'throw' and 'throw' assignment-expression. When
+    # assignment-expression isn't present, Op will be null.
+    CXX_THROW_EXPR = 133
 
-# [C++0x 2.14.7] C++ Pointer Literal.
-CursorKind.CXX_NULL_PTR_LITERAL_EXPR = CursorKind(131)
+    # A new expression for memory allocation and constructor calls, e.g:
+    # "new CXXNewExpr(foo)".
+    CXX_NEW_EXPR = 134
 
-# Represents the "this" expression in C++
-CursorKind.CXX_THIS_EXPR = CursorKind(132)
+    # A delete expression for memory deallocation and destructor calls,
+    # e.g. "delete[] pArray".
+    CXX_DELETE_EXPR = 135
 
-# [C++ 15] C++ Throw Expression.
-#
-# This handles 'throw' and 'throw' assignment-expression. When
-# assignment-expression isn't present, Op will be null.
-CursorKind.CXX_THROW_EXPR = CursorKind(133)
+    # Represents a unary expression.
+    CXX_UNARY_EXPR = 136
 
-# A new expression for memory allocation and constructor calls, e.g:
-# "new CXXNewExpr(foo)".
-CursorKind.CXX_NEW_EXPR = CursorKind(134)
+    # ObjCStringLiteral, used for Objective-C string literals i.e. "foo".
+    OBJC_STRING_LITERAL = 137
 
-# A delete expression for memory deallocation and destructor calls,
-# e.g. "delete[] pArray".
-CursorKind.CXX_DELETE_EXPR = CursorKind(135)
+    # ObjCEncodeExpr, used for in Objective-C.
+    OBJC_ENCODE_EXPR = 138
 
-# Represents a unary expression.
-CursorKind.CXX_UNARY_EXPR = CursorKind(136)
+    # ObjCSelectorExpr used for in Objective-C.
+    OBJC_SELECTOR_EXPR = 139
 
-# ObjCStringLiteral, used for Objective-C string literals i.e. "foo".
-CursorKind.OBJC_STRING_LITERAL = CursorKind(137)
+    # Objective-C's protocol expression.
+    OBJC_PROTOCOL_EXPR = 140
 
-# ObjCEncodeExpr, used for in Objective-C.
-CursorKind.OBJC_ENCODE_EXPR = CursorKind(138)
+    # An Objective-C "bridged" cast expression, which casts between
+    # Objective-C pointers and C pointers, transferring ownership in the process.
+    #
+    # \code
+    #   NSString *str = (__bridge_transfer NSString *)CFCreateString();
+    # \endcode
+    OBJC_BRIDGE_CAST_EXPR = 141
 
-# ObjCSelectorExpr used for in Objective-C.
-CursorKind.OBJC_SELECTOR_EXPR = CursorKind(139)
+    # Represents a C++0x pack expansion that produces a sequence of
+    # expressions.
+    #
+    # A pack expansion expression contains a pattern (which itself is an
+    # expression) followed by an ellipsis. For example:
+    PACK_EXPANSION_EXPR = 142
 
-# Objective-C's protocol expression.
-CursorKind.OBJC_PROTOCOL_EXPR = CursorKind(140)
+    # Represents an expression that computes the length of a parameter
+    # pack.
+    SIZE_OF_PACK_EXPR = 143
 
-# An Objective-C "bridged" cast expression, which casts between
-# Objective-C pointers and C pointers, transferring ownership in the process.
-#
-# \code
-#   NSString *str = (__bridge_transfer NSString *)CFCreateString();
-# \endcode
-CursorKind.OBJC_BRIDGE_CAST_EXPR = CursorKind(141)
+    # Represents a C++ lambda expression that produces a local function
+    # object.
+    #
+    #  \code
+    #  void abssort(float *x, unsigned N) {
+    #    std::sort(x, x + N,
+    #              [](float a, float b) {
+    #                return std::abs(a) < std::abs(b);
+    #              });
+    #  }
+    #  \endcode
+    LAMBDA_EXPR = 144
 
-# Represents a C++0x pack expansion that produces a sequence of
-# expressions.
-#
-# A pack expansion expression contains a pattern (which itself is an
-# expression) followed by an ellipsis. For example:
-CursorKind.PACK_EXPANSION_EXPR = CursorKind(142)
+    # Objective-c Boolean Literal.
+    OBJ_BOOL_LITERAL_EXPR = 145
 
-# Represents an expression that computes the length of a parameter
-# pack.
-CursorKind.SIZE_OF_PACK_EXPR = CursorKind(143)
+    # Represents the "self" expression in a ObjC method.
+    OBJ_SELF_EXPR = 146
 
-# Represents a C++ lambda expression that produces a local function
-# object.
-#
-#  \code
-#  void abssort(float *x, unsigned N) {
-#    std::sort(x, x + N,
-#              [](float a, float b) {
-#                return std::abs(a) < std::abs(b);
-#              });
-#  }
-#  \endcode
-CursorKind.LAMBDA_EXPR = CursorKind(144)
+    # OpenMP 4.0 [2.4, Array Section].
+    OMP_ARRAY_SECTION_EXPR = 147
 
-# Objective-c Boolean Literal.
-CursorKind.OBJ_BOOL_LITERAL_EXPR = CursorKind(145)
+    # Represents an @available(...) check.
+    OBJC_AVAILABILITY_CHECK_EXPR = 148
 
-# Represents the "self" expression in a ObjC method.
-CursorKind.OBJ_SELF_EXPR = CursorKind(146)
+    # A statement whose specific kind is not exposed via this interface.
+    #
+    # Unexposed statements have the same operations as any other kind of statement;
+    # one can extract their location information, spelling, children, etc. However,
+    # the specific kind of the statement is not reported.
+    UNEXPOSED_STMT = 200
 
-# OpenMP 4.0 [2.4, Array Section].
-CursorKind.OMP_ARRAY_SECTION_EXPR = CursorKind(147)
+    # A labelled statement in a function.
+    LABEL_STMT = 201
 
-# Represents an @available(...) check.
-CursorKind.OBJC_AVAILABILITY_CHECK_EXPR = CursorKind(148)
+    # A compound statement
+    COMPOUND_STMT = 202
 
+    # A case statement.
+    CASE_STMT = 203
 
-# A statement whose specific kind is not exposed via this interface.
-#
-# Unexposed statements have the same operations as any other kind of statement;
-# one can extract their location information, spelling, children, etc. However,
-# the specific kind of the statement is not reported.
-CursorKind.UNEXPOSED_STMT = CursorKind(200)
+    # A default statement.
+    DEFAULT_STMT = 204
 
-# A labelled statement in a function.
-CursorKind.LABEL_STMT = CursorKind(201)
+    # An if statement.
+    IF_STMT = 205
 
-# A compound statement
-CursorKind.COMPOUND_STMT = CursorKind(202)
+    # A switch statement.
+    SWITCH_STMT = 206
 
-# A case statement.
-CursorKind.CASE_STMT = CursorKind(203)
+    # A while statement.
+    WHILE_STMT = 207
 
-# A default statement.
-CursorKind.DEFAULT_STMT = CursorKind(204)
+    # A do statement.
+    DO_STMT = 208
 
-# An if statement.
-CursorKind.IF_STMT = CursorKind(205)
+    # A for statement.
+    FOR_STMT = 209
 
-# A switch statement.
-CursorKind.SWITCH_STMT = CursorKind(206)
+    # A goto statement.
+    GOTO_STMT = 210
 
-# A while statement.
-CursorKind.WHILE_STMT = CursorKind(207)
+    # An indirect goto statement.
+    INDIRECT_GOTO_STMT = 211
 
-# A do statement.
-CursorKind.DO_STMT = CursorKind(208)
+    # A continue statement.
+    CONTINUE_STMT = 212
 
-# A for statement.
-CursorKind.FOR_STMT = CursorKind(209)
+    # A break statement.
+    BREAK_STMT = 213
 
-# A goto statement.
-CursorKind.GOTO_STMT = CursorKind(210)
+    # A return statement.
+    RETURN_STMT = 214
 
-# An indirect goto statement.
-CursorKind.INDIRECT_GOTO_STMT = CursorKind(211)
+    # A GNU-style inline assembler statement.
+    ASM_STMT = 215
 
-# A continue statement.
-CursorKind.CONTINUE_STMT = CursorKind(212)
+    # Objective-C's overall @try- at catch-@finally statement.
+    OBJC_AT_TRY_STMT = 216
 
-# A break statement.
-CursorKind.BREAK_STMT = CursorKind(213)
+    # Objective-C's @catch statement.
+    OBJC_AT_CATCH_STMT = 217
 
-# A return statement.
-CursorKind.RETURN_STMT = CursorKind(214)
+    # Objective-C's @finally statement.
+    OBJC_AT_FINALLY_STMT = 218
 
-# A GNU-style inline assembler statement.
-CursorKind.ASM_STMT = CursorKind(215)
+    # Objective-C's @throw statement.
+    OBJC_AT_THROW_STMT = 219
 
-# Objective-C's overall @try- at catch-@finally statement.
-CursorKind.OBJC_AT_TRY_STMT = CursorKind(216)
+    # Objective-C's @synchronized statement.
+    OBJC_AT_SYNCHRONIZED_STMT = 220
 
-# Objective-C's @catch statement.
-CursorKind.OBJC_AT_CATCH_STMT = CursorKind(217)
+    # Objective-C's autorelease pool statement.
+    OBJC_AUTORELEASE_POOL_STMT = 221
 
-# Objective-C's @finally statement.
-CursorKind.OBJC_AT_FINALLY_STMT = CursorKind(218)
+    # Objective-C's for collection statement.
+    OBJC_FOR_COLLECTION_STMT = 222
 
-# Objective-C's @throw statement.
-CursorKind.OBJC_AT_THROW_STMT = CursorKind(219)
+    # C++'s catch statement.
+    CXX_CATCH_STMT = 223
 
-# Objective-C's @synchronized statement.
-CursorKind.OBJC_AT_SYNCHRONIZED_STMT = CursorKind(220)
+    # C++'s try statement.
+    CXX_TRY_STMT = 224
 
-# Objective-C's autorelease pool statement.
-CursorKind.OBJC_AUTORELEASE_POOL_STMT = CursorKind(221)
+    # C++'s for (* : *) statement.
+    CXX_FOR_RANGE_STMT = 225
 
-# Objective-C's for collection statement.
-CursorKind.OBJC_FOR_COLLECTION_STMT = CursorKind(222)
+    # Windows Structured Exception Handling's try statement.
+    SEH_TRY_STMT = 226
 
-# C++'s catch statement.
-CursorKind.CXX_CATCH_STMT = CursorKind(223)
+    # Windows Structured Exception Handling's except statement.
+    SEH_EXCEPT_STMT = 227
 
-# C++'s try statement.
-CursorKind.CXX_TRY_STMT = CursorKind(224)
+    # Windows Structured Exception Handling's finally statement.
+    SEH_FINALLY_STMT = 228
 
-# C++'s for (* : *) statement.
-CursorKind.CXX_FOR_RANGE_STMT = CursorKind(225)
+    # A MS inline assembly statement extension.
+    MS_ASM_STMT = 229
 
-# Windows Structured Exception Handling's try statement.
-CursorKind.SEH_TRY_STMT = CursorKind(226)
+    # The null statement.
+    NULL_STMT = 230
 
-# Windows Structured Exception Handling's except statement.
-CursorKind.SEH_EXCEPT_STMT = CursorKind(227)
+    # Adaptor class for mixing declarations with statements and expressions.
+    DECL_STMT = 231
 
-# Windows Structured Exception Handling's finally statement.
-CursorKind.SEH_FINALLY_STMT = CursorKind(228)
+    # OpenMP parallel directive.
+    OMP_PARALLEL_DIRECTIVE = 232
 
-# A MS inline assembly statement extension.
-CursorKind.MS_ASM_STMT = CursorKind(229)
+    # OpenMP SIMD directive.
+    OMP_SIMD_DIRECTIVE = 233
 
-# The null statement.
-CursorKind.NULL_STMT = CursorKind(230)
+    # OpenMP for directive.
+    OMP_FOR_DIRECTIVE = 234
 
-# Adaptor class for mixing declarations with statements and expressions.
-CursorKind.DECL_STMT = CursorKind(231)
+    # OpenMP sections directive.
+    OMP_SECTIONS_DIRECTIVE = 235
 
-# OpenMP parallel directive.
-CursorKind.OMP_PARALLEL_DIRECTIVE = CursorKind(232)
+    # OpenMP section directive.
+    OMP_SECTION_DIRECTIVE = 236
 
-# OpenMP SIMD directive.
-CursorKind.OMP_SIMD_DIRECTIVE = CursorKind(233)
+    # OpenMP single directive.
+    OMP_SINGLE_DIRECTIVE = 237
 
-# OpenMP for directive.
-CursorKind.OMP_FOR_DIRECTIVE = CursorKind(234)
+    # OpenMP parallel for directive.
+    OMP_PARALLEL_FOR_DIRECTIVE = 238
 
-# OpenMP sections directive.
-CursorKind.OMP_SECTIONS_DIRECTIVE = CursorKind(235)
+    # OpenMP parallel sections directive.
+    OMP_PARALLEL_SECTIONS_DIRECTIVE = 239
 
-# OpenMP section directive.
-CursorKind.OMP_SECTION_DIRECTIVE = CursorKind(236)
+    # OpenMP task directive.
+    OMP_TASK_DIRECTIVE = 240
 
-# OpenMP single directive.
-CursorKind.OMP_SINGLE_DIRECTIVE = CursorKind(237)
+    # OpenMP master directive.
+    OMP_MASTER_DIRECTIVE = 241
 
-# OpenMP parallel for directive.
-CursorKind.OMP_PARALLEL_FOR_DIRECTIVE = CursorKind(238)
+    # OpenMP critical directive.
+    OMP_CRITICAL_DIRECTIVE = 242
 
-# OpenMP parallel sections directive.
-CursorKind.OMP_PARALLEL_SECTIONS_DIRECTIVE = CursorKind(239)
+    # OpenMP taskyield directive.
+    OMP_TASKYIELD_DIRECTIVE = 243
 
-# OpenMP task directive.
-CursorKind.OMP_TASK_DIRECTIVE = CursorKind(240)
+    # OpenMP barrier directive.
+    OMP_BARRIER_DIRECTIVE = 244
 
-# OpenMP master directive.
-CursorKind.OMP_MASTER_DIRECTIVE = CursorKind(241)
+    # OpenMP taskwait directive.
+    OMP_TASKWAIT_DIRECTIVE = 245
 
-# OpenMP critical directive.
-CursorKind.OMP_CRITICAL_DIRECTIVE = CursorKind(242)
+    # OpenMP flush directive.
+    OMP_FLUSH_DIRECTIVE = 246
 
-# OpenMP taskyield directive.
-CursorKind.OMP_TASKYIELD_DIRECTIVE = CursorKind(243)
+    # Windows Structured Exception Handling's leave statement.
+    SEH_LEAVE_STMT = 247
 
-# OpenMP barrier directive.
-CursorKind.OMP_BARRIER_DIRECTIVE = CursorKind(244)
+    # OpenMP ordered directive.
+    OMP_ORDERED_DIRECTIVE = 248
 
-# OpenMP taskwait directive.
-CursorKind.OMP_TASKWAIT_DIRECTIVE = CursorKind(245)
+    # OpenMP atomic directive.
+    OMP_ATOMIC_DIRECTIVE = 249
 
-# OpenMP flush directive.
-CursorKind.OMP_FLUSH_DIRECTIVE = CursorKind(246)
+    # OpenMP for SIMD directive.
+    OMP_FOR_SIMD_DIRECTIVE = 250
 
-# Windows Structured Exception Handling's leave statement.
-CursorKind.SEH_LEAVE_STMT = CursorKind(247)
+    # OpenMP parallel for SIMD directive.
+    OMP_PARALLELFORSIMD_DIRECTIVE = 251
 
-# OpenMP ordered directive.
-CursorKind.OMP_ORDERED_DIRECTIVE = CursorKind(248)
+    # OpenMP target directive.
+    OMP_TARGET_DIRECTIVE = 252
 
-# OpenMP atomic directive.
-CursorKind.OMP_ATOMIC_DIRECTIVE = CursorKind(249)
+    # OpenMP teams directive.
+    OMP_TEAMS_DIRECTIVE = 253
 
-# OpenMP for SIMD directive.
-CursorKind.OMP_FOR_SIMD_DIRECTIVE = CursorKind(250)
+    # OpenMP taskgroup directive.
+    OMP_TASKGROUP_DIRECTIVE = 254
 
-# OpenMP parallel for SIMD directive.
-CursorKind.OMP_PARALLELFORSIMD_DIRECTIVE = CursorKind(251)
+    # OpenMP cancellation point directive.
+    OMP_CANCELLATION_POINT_DIRECTIVE = 255
 
-# OpenMP target directive.
-CursorKind.OMP_TARGET_DIRECTIVE = CursorKind(252)
+    # OpenMP cancel directive.
+    OMP_CANCEL_DIRECTIVE = 256
 
-# OpenMP teams directive.
-CursorKind.OMP_TEAMS_DIRECTIVE = CursorKind(253)
+    # OpenMP target data directive.
+    OMP_TARGET_DATA_DIRECTIVE = 257
 
-# OpenMP taskgroup directive.
-CursorKind.OMP_TASKGROUP_DIRECTIVE = CursorKind(254)
+    # OpenMP taskloop directive.
+    OMP_TASK_LOOP_DIRECTIVE = 258
 
-# OpenMP cancellation point directive.
-CursorKind.OMP_CANCELLATION_POINT_DIRECTIVE = CursorKind(255)
+    # OpenMP taskloop simd directive.
+    OMP_TASK_LOOP_SIMD_DIRECTIVE = 259
 
-# OpenMP cancel directive.
-CursorKind.OMP_CANCEL_DIRECTIVE = CursorKind(256)
+    # OpenMP distribute directive.
+    OMP_DISTRIBUTE_DIRECTIVE = 260
 
-# OpenMP target data directive.
-CursorKind.OMP_TARGET_DATA_DIRECTIVE = CursorKind(257)
+    # OpenMP target enter data directive.
+    OMP_TARGET_ENTER_DATA_DIRECTIVE = 261
 
-# OpenMP taskloop directive.
-CursorKind.OMP_TASK_LOOP_DIRECTIVE = CursorKind(258)
+    # OpenMP target exit data directive.
+    OMP_TARGET_EXIT_DATA_DIRECTIVE = 262
 
-# OpenMP taskloop simd directive.
-CursorKind.OMP_TASK_LOOP_SIMD_DIRECTIVE = CursorKind(259)
+    # OpenMP target parallel directive.
+    OMP_TARGET_PARALLEL_DIRECTIVE = 263
 
-# OpenMP distribute directive.
-CursorKind.OMP_DISTRIBUTE_DIRECTIVE = CursorKind(260)
+    # OpenMP target parallel for directive.
+    OMP_TARGET_PARALLELFOR_DIRECTIVE = 264
 
-# OpenMP target enter data directive.
-CursorKind.OMP_TARGET_ENTER_DATA_DIRECTIVE = CursorKind(261)
+    # OpenMP target update directive.
+    OMP_TARGET_UPDATE_DIRECTIVE = 265
 
-# OpenMP target exit data directive.
-CursorKind.OMP_TARGET_EXIT_DATA_DIRECTIVE = CursorKind(262)
+    # OpenMP distribute parallel for directive.
+    OMP_DISTRIBUTE_PARALLELFOR_DIRECTIVE = 266
 
-# OpenMP target parallel directive.
-CursorKind.OMP_TARGET_PARALLEL_DIRECTIVE = CursorKind(263)
+    # OpenMP distribute parallel for simd directive.
+    OMP_DISTRIBUTE_PARALLEL_FOR_SIMD_DIRECTIVE = 267
 
-# OpenMP target parallel for directive.
-CursorKind.OMP_TARGET_PARALLELFOR_DIRECTIVE = CursorKind(264)
+    # OpenMP distribute simd directive.
+    OMP_DISTRIBUTE_SIMD_DIRECTIVE = 268
 
-# OpenMP target update directive.
-CursorKind.OMP_TARGET_UPDATE_DIRECTIVE = CursorKind(265)
+    # OpenMP target parallel for simd directive.
+    OMP_TARGET_PARALLEL_FOR_SIMD_DIRECTIVE = 269
 
-# OpenMP distribute parallel for directive.
-CursorKind.OMP_DISTRIBUTE_PARALLELFOR_DIRECTIVE = CursorKind(266)
+    # OpenMP target simd directive.
+    OMP_TARGET_SIMD_DIRECTIVE = 270
 
-# OpenMP distribute parallel for simd directive.
-CursorKind.OMP_DISTRIBUTE_PARALLEL_FOR_SIMD_DIRECTIVE = CursorKind(267)
+    # OpenMP teams distribute directive.
+    OMP_TEAMS_DISTRIBUTE_DIRECTIVE = 271
 
-# OpenMP distribute simd directive.
-CursorKind.OMP_DISTRIBUTE_SIMD_DIRECTIVE = CursorKind(268)
+    ###
+    # Other Kinds
 
-# OpenMP target parallel for simd directive.
-CursorKind.OMP_TARGET_PARALLEL_FOR_SIMD_DIRECTIVE = CursorKind(269)
+    # Cursor that represents the translation unit itself.
+    #
+    # The translation unit cursor exists primarily to act as the root cursor for
+    # traversing the contents of a translation unit.
+    TRANSLATION_UNIT = 350
 
-# OpenMP target simd directive.
-CursorKind.OMP_TARGET_SIMD_DIRECTIVE = CursorKind(270)
+    ###
+    # Attributes
 
-# OpenMP teams distribute directive.
-CursorKind.OMP_TEAMS_DISTRIBUTE_DIRECTIVE = CursorKind(271)
+    # An attribute whoe specific kind is note exposed via this interface
+    UNEXPOSED_ATTR = 400
 
-###
-# Other Kinds
+    IB_ACTION_ATTR = 401
+    IB_OUTLET_ATTR = 402
+    IB_OUTLET_COLLECTION_ATTR = 403
+
+    CXX_FINAL_ATTR = 404
+    CXX_OVERRIDE_ATTR = 405
+    ANNOTATE_ATTR = 406
+    ASM_LABEL_ATTR = 407
+    PACKED_ATTR = 408
+    PURE_ATTR = 409
+    CONST_ATTR = 410
+    NODUPLICATE_ATTR = 411
+    CUDACONSTANT_ATTR = 412
+    CUDADEVICE_ATTR = 413
+    CUDAGLOBAL_ATTR = 414
+    CUDAHOST_ATTR = 415
+    CUDASHARED_ATTR = 416
+
+    VISIBILITY_ATTR = 417
+
+    DLLEXPORT_ATTR = 418
+    DLLIMPORT_ATTR = 419
+    CONVERGENT_ATTR = 438
+    WARN_UNUSED_ATTR = 439
+    WARN_UNUSED_RESULT_ATTR = 440
+    ALIGNED_ATTR = 441
+
+    ###
+    # Preprocessing
+    PREPROCESSING_DIRECTIVE = 500
+    MACRO_DEFINITION = 501
+    MACRO_INSTANTIATION = 502
+    INCLUSION_DIRECTIVE = 503
+
+    ###
+    # Extra declaration
+
+    # A module import declaration.
+    MODULE_IMPORT_DECL = 600
+    # A type alias template declaration
+    TYPE_ALIAS_TEMPLATE_DECL = 601
+    # A static_assert or _Static_assert node
+    STATIC_ASSERT = 602
+    # A friend declaration
+    FRIEND_DECL = 603
+    # A concept declaration
+    CONCEPT_DECL = 604
+
+    # A code completion overload candidate.
+    OVERLOAD_CANDIDATE = 700
 
-# Cursor that represents the translation unit itself.
-#
-# The translation unit cursor exists primarily to act as the root cursor for
-# traversing the contents of a translation unit.
-CursorKind.TRANSLATION_UNIT = CursorKind(350)
-
-###
-# Attributes
-
-# An attribute whoe specific kind is note exposed via this interface
-CursorKind.UNEXPOSED_ATTR = CursorKind(400)
-
-CursorKind.IB_ACTION_ATTR = CursorKind(401)
-CursorKind.IB_OUTLET_ATTR = CursorKind(402)
-CursorKind.IB_OUTLET_COLLECTION_ATTR = CursorKind(403)
-
-CursorKind.CXX_FINAL_ATTR = CursorKind(404)
-CursorKind.CXX_OVERRIDE_ATTR = CursorKind(405)
-CursorKind.ANNOTATE_ATTR = CursorKind(406)
-CursorKind.ASM_LABEL_ATTR = CursorKind(407)
-CursorKind.PACKED_ATTR = CursorKind(408)
-CursorKind.PURE_ATTR = CursorKind(409)
-CursorKind.CONST_ATTR = CursorKind(410)
-CursorKind.NODUPLICATE_ATTR = CursorKind(411)
-CursorKind.CUDACONSTANT_ATTR = CursorKind(412)
-CursorKind.CUDADEVICE_ATTR = CursorKind(413)
-CursorKind.CUDAGLOBAL_ATTR = CursorKind(414)
-CursorKind.CUDAHOST_ATTR = CursorKind(415)
-CursorKind.CUDASHARED_ATTR = CursorKind(416)
-
-CursorKind.VISIBILITY_ATTR = CursorKind(417)
-
-CursorKind.DLLEXPORT_ATTR = CursorKind(418)
-CursorKind.DLLIMPORT_ATTR = CursorKind(419)
-CursorKind.CONVERGENT_ATTR = CursorKind(438)
-CursorKind.WARN_UNUSED_ATTR = CursorKind(439)
-CursorKind.WARN_UNUSED_RESULT_ATTR = CursorKind(440)
-CursorKind.ALIGNED_ATTR = CursorKind(441)
-
-###
-# Preprocessing
-CursorKind.PREPROCESSING_DIRECTIVE = CursorKind(500)
-CursorKind.MACRO_DEFINITION = CursorKind(501)
-CursorKind.MACRO_INSTANTIATION = CursorKind(502)
-CursorKind.INCLUSION_DIRECTIVE = CursorKind(503)
-
-###
-# Extra declaration
-
-# A module import declaration.
-CursorKind.MODULE_IMPORT_DECL = CursorKind(600)
-# A type alias template declaration
-CursorKind.TYPE_ALIAS_TEMPLATE_DECL = CursorKind(601)
-# A static_assert or _Static_assert node
-CursorKind.STATIC_ASSERT = CursorKind(602)
-# A friend declaration
-CursorKind.FRIEND_DECL = CursorKind(603)
-# A concept declaration
-CursorKind.CONCEPT_DECL = CursorKind(604)
-
-# A code completion overload candidate.
-CursorKind.OVERLOAD_CANDIDATE = CursorKind(700)
 
 ### Template Argument Kinds ###
 class TemplateArgumentKind(BaseEnumeration):
@@ -1416,17 +2001,13 @@ class TemplateArgumentKind(BaseEnumeration):
     represents.
     """
 
-    # The required BaseEnumeration declarations.
-    _kinds = []
-    _name_map = None
+    NULL = 0
+    TYPE = 1
+    DECLARATION = 2
+    NULLPTR = 3
+    INTEGRAL = 4
 
 
-TemplateArgumentKind.NULL = TemplateArgumentKind(0)
-TemplateArgumentKind.TYPE = TemplateArgumentKind(1)
-TemplateArgumentKind.DECLARATION = TemplateArgumentKind(2)
-TemplateArgumentKind.NULLPTR = TemplateArgumentKind(3)
-TemplateArgumentKind.INTEGRAL = TemplateArgumentKind(4)
-
 ### Exception Specification Kinds ###
 class ExceptionSpecificationKind(BaseEnumeration):
     """
@@ -1434,24 +2015,17 @@ class ExceptionSpecificationKind(BaseEnumeration):
     that a function has.
     """
 
-    # The required BaseEnumeration declarations.
-    _kinds = []
-    _name_map = None
-
-    def __repr__(self):
-        return "ExceptionSpecificationKind.{}".format(self.name)
+    NONE = 0
+    DYNAMIC_NONE = 1
+    DYNAMIC = 2
+    MS_ANY = 3
+    BASIC_NOEXCEPT = 4
+    COMPUTED_NOEXCEPT = 5
+    UNEVALUATED = 6
+    UNINSTANTIATED = 7
+    UNPARSED = 8
 
 
-ExceptionSpecificationKind.NONE = ExceptionSpecificationKind(0)
-ExceptionSpecificationKind.DYNAMIC_NONE = ExceptionSpecificationKind(1)
-ExceptionSpecificationKind.DYNAMIC = ExceptionSpecificationKind(2)
-ExceptionSpecificationKind.MS_ANY = ExceptionSpecificationKind(3)
-ExceptionSpecificationKind.BASIC_NOEXCEPT = ExceptionSpecificationKind(4)
-ExceptionSpecificationKind.COMPUTED_NOEXCEPT = ExceptionSpecificationKind(5)
-ExceptionSpecificationKind.UNEVALUATED = ExceptionSpecificationKind(6)
-ExceptionSpecificationKind.UNINSTANTIATED = ExceptionSpecificationKind(7)
-ExceptionSpecificationKind.UNPARSED = ExceptionSpecificationKind(8)
-
 ### Cursors ###
 
 
@@ -1463,8 +2037,10 @@ class Cursor(Structure):
 
     _fields_ = [("_kind_id", c_int), ("xdata", c_int), ("data", c_void_p * 3)]
 
+    _tu: TranslationUnit
+
     @staticmethod
-    def from_location(tu, location):
+    def from_location(tu: TranslationUnit, location: SourceLocation) -> Cursor:
         # We store a reference to the TU in the instance so the TU won't get
         # collected before the cursor.
         cursor = conf.lib.clang_getCursor(tu, location)
@@ -1472,54 +2048,56 @@ def from_location(tu, location):
 
         return cursor
 
-    def __eq__(self, other):
+    def __eq__(self, other: object) -> bool:
+        if not isinstance(other, Cursor):
+            return False
         return conf.lib.clang_equalCursors(self, other)
 
-    def __ne__(self, other):
+    def __ne__(self, other: object) -> bool:
         return not self.__eq__(other)
 
-    def is_definition(self):
+    def is_definition(self) -> bool:
         """
         Returns true if the declaration pointed at by the cursor is also a
         definition of that entity.
         """
         return conf.lib.clang_isCursorDefinition(self)
 
-    def is_const_method(self):
+    def is_const_method(self) -> bool:
         """Returns True if the cursor refers to a C++ member function or member
         function template that is declared 'const'.
         """
         return conf.lib.clang_CXXMethod_isConst(self)
 
-    def is_converting_constructor(self):
+    def is_converting_constructor(self) -> bool:
         """Returns True if the cursor refers to a C++ converting constructor."""
         return conf.lib.clang_CXXConstructor_isConvertingConstructor(self)
 
-    def is_copy_constructor(self):
+    def is_copy_constructor(self) -> bool:
         """Returns True if the cursor refers to a C++ copy constructor."""
         return conf.lib.clang_CXXConstructor_isCopyConstructor(self)
 
-    def is_default_constructor(self):
+    def is_default_constructor(self) -> bool:
         """Returns True if the cursor refers to a C++ default constructor."""
         return conf.lib.clang_CXXConstructor_isDefaultConstructor(self)
 
-    def is_move_constructor(self):
+    def is_move_constructor(self) -> bool:
         """Returns True if the cursor refers to a C++ move constructor."""
         return conf.lib.clang_CXXConstructor_isMoveConstructor(self)
 
-    def is_default_method(self):
+    def is_default_method(self) -> bool:
         """Returns True if the cursor refers to a C++ member function or member
         function template that is declared '= default'.
         """
         return conf.lib.clang_CXXMethod_isDefaulted(self)
 
-    def is_deleted_method(self):
+    def is_deleted_method(self) -> bool:
         """Returns True if the cursor refers to a C++ member function or member
         function template that is declared '= delete'.
         """
         return conf.lib.clang_CXXMethod_isDeleted(self)
 
-    def is_copy_assignment_operator_method(self):
+    def is_copy_assignment_operator_method(self) -> bool:
         """Returnrs True if the cursor refers to a copy-assignment operator.
 
         A copy-assignment operator `X::operator=` is a non-static,
@@ -1544,7 +2122,7 @@ class Bar {
         """
         return conf.lib.clang_CXXMethod_isCopyAssignmentOperator(self)
 
-    def is_move_assignment_operator_method(self):
+    def is_move_assignment_operator_method(self) -> bool:
         """Returnrs True if the cursor refers to a move-assignment operator.
 
         A move-assignment operator `X::operator=` is a non-static,
@@ -1569,7 +2147,7 @@ class Bar {
         """
         return conf.lib.clang_CXXMethod_isMoveAssignmentOperator(self)
 
-    def is_explicit_method(self):
+    def is_explicit_method(self) -> bool:
         """Determines if a C++ constructor or conversion function is
         explicit, returning 1 if such is the case and 0 otherwise.
 
@@ -1614,41 +2192,41 @@ class Foo {
         """
         return conf.lib.clang_CXXMethod_isExplicit(self)
 
-    def is_mutable_field(self):
+    def is_mutable_field(self) -> bool:
         """Returns True if the cursor refers to a C++ field that is declared
         'mutable'.
         """
         return conf.lib.clang_CXXField_isMutable(self)
 
-    def is_pure_virtual_method(self):
+    def is_pure_virtual_method(self) -> bool:
         """Returns True if the cursor refers to a C++ member function or member
         function template that is declared pure virtual.
         """
         return conf.lib.clang_CXXMethod_isPureVirtual(self)
 
-    def is_static_method(self):
+    def is_static_method(self) -> bool:
         """Returns True if the cursor refers to a C++ member function or member
         function template that is declared 'static'.
         """
         return conf.lib.clang_CXXMethod_isStatic(self)
 
-    def is_virtual_method(self):
+    def is_virtual_method(self) -> bool:
         """Returns True if the cursor refers to a C++ member function or member
         function template that is declared 'virtual'.
         """
         return conf.lib.clang_CXXMethod_isVirtual(self)
 
-    def is_abstract_record(self):
+    def is_abstract_record(self) -> bool:
         """Returns True if the cursor refers to a C++ record declaration
         that has pure virtual member functions.
         """
         return conf.lib.clang_CXXRecord_isAbstract(self)
 
-    def is_scoped_enum(self):
+    def is_scoped_enum(self) -> bool:
         """Returns True if the cursor refers to a scoped enum declaration."""
         return conf.lib.clang_EnumDecl_isScoped(self)
 
-    def get_definition(self):
+    def get_definition(self) -> Cursor:
         """
         If the cursor is a reference to a declaration or a declaration of
         some entity, return a cursor that points to the definition of that
@@ -1658,9 +2236,9 @@ def get_definition(self):
         # declaration prior to issuing the lookup.
         return conf.lib.clang_getCursorDefinition(self)
 
-    def get_usr(self):
+    def get_usr(self) -> str:
         """Return the Unified Symbol Resolution (USR) for the entity referenced
-        by the given cursor (or None).
+        by the given cursor.
 
         A Unified Symbol Resolution (USR) is a string that identifies a
         particular entity (function, class, variable, etc.) within a
@@ -1669,19 +2247,19 @@ def get_usr(self):
         another translation unit."""
         return conf.lib.clang_getCursorUSR(self)
 
-    def get_included_file(self):
+    def get_included_file(self) -> File:
         """Returns the File that is included by the current inclusion cursor."""
         assert self.kind == CursorKind.INCLUSION_DIRECTIVE
 
         return conf.lib.clang_getIncludedFile(self)
 
     @property
-    def kind(self):
+    def kind(self) -> CursorKind:
         """Return the kind of this cursor."""
         return CursorKind.from_id(self._kind_id)
 
     @property
-    def spelling(self):
+    def spelling(self) -> str:
         """Return the spelling of the entity pointed at by the cursor."""
         if not hasattr(self, "_spelling"):
             self._spelling = conf.lib.clang_getCursorSpelling(self)
@@ -1689,7 +2267,7 @@ def spelling(self):
         return self._spelling
 
     @property
-    def displayname(self):
+    def displayname(self) -> str:
         """
         Return the display name for the entity referenced by this cursor.
 
@@ -1703,7 +2281,7 @@ def displayname(self):
         return self._displayname
 
     @property
-    def mangled_name(self):
+    def mangled_name(self) -> str:
         """Return the mangled name for the entity referenced by this cursor."""
         if not hasattr(self, "_mangled_name"):
             self._mangled_name = conf.lib.clang_Cursor_getMangling(self)
@@ -1711,7 +2289,7 @@ def mangled_name(self):
         return self._mangled_name
 
     @property
-    def location(self):
+    def location(self) -> SourceLocation:
         """
         Return the source location (the starting character) of the entity
         pointed at by the cursor.
@@ -1722,7 +2300,7 @@ def location(self):
         return self._loc
 
     @property
-    def linkage(self):
+    def linkage(self) -> LinkageKind:
         """Return the linkage of this cursor."""
         if not hasattr(self, "_linkage"):
             self._linkage = conf.lib.clang_getCursorLinkage(self)
@@ -1730,7 +2308,7 @@ def linkage(self):
         return LinkageKind.from_id(self._linkage)
 
     @property
-    def tls_kind(self):
+    def tls_kind(self) -> TLSKind:
         """Return the thread-local storage (TLS) kind of this cursor."""
         if not hasattr(self, "_tls_kind"):
             self._tls_kind = conf.lib.clang_getCursorTLSKind(self)
@@ -1738,7 +2316,7 @@ def tls_kind(self):
         return TLSKind.from_id(self._tls_kind)
 
     @property
-    def extent(self):
+    def extent(self) -> SourceRange:
         """
         Return the source range (the range of text) occupied by the entity
         pointed at by the cursor.
@@ -1749,7 +2327,7 @@ def extent(self):
         return self._extent
 
     @property
-    def storage_class(self):
+    def storage_class(self) -> StorageClass:
         """
         Retrieves the storage class (if any) of the entity pointed at by the
         cursor.
@@ -1760,7 +2338,7 @@ def storage_class(self):
         return StorageClass.from_id(self._storage_class)
 
     @property
-    def availability(self):
+    def availability(self) -> AvailabilityKind:
         """
         Retrieves the availability of the entity pointed at by the cursor.
         """
@@ -1770,7 +2348,7 @@ def availability(self):
         return AvailabilityKind.from_id(self._availability)
 
     @property
-    def access_specifier(self):
+    def access_specifier(self) -> AccessSpecifier:
         """
         Retrieves the access specifier (if any) of the entity pointed at by the
         cursor.
@@ -1781,7 +2359,7 @@ def access_specifier(self):
         return AccessSpecifier.from_id(self._access_specifier)
 
     @property
-    def type(self):
+    def type(self) -> Type:
         """
         Retrieve the Type (if any) of the entity pointed at by the cursor.
         """
@@ -1791,7 +2369,7 @@ def type(self):
         return self._type
 
     @property
-    def canonical(self):
+    def canonical(self) -> Cursor:
         """Return the canonical Cursor corresponding to this Cursor.
 
         The canonical cursor is the cursor which is representative for the
@@ -1805,7 +2383,7 @@ def canonical(self):
         return self._canonical
 
     @property
-    def result_type(self):
+    def result_type(self) -> Type:
         """Retrieve the Type of the result for this Cursor."""
         if not hasattr(self, "_result_type"):
             self._result_type = conf.lib.clang_getCursorResultType(self)
@@ -1813,7 +2391,7 @@ def result_type(self):
         return self._result_type
 
     @property
-    def exception_specification_kind(self):
+    def exception_specification_kind(self) -> ExceptionSpecificationKind:
         """
         Retrieve the exception specification kind, which is one of the values
         from the ExceptionSpecificationKind enumeration.
@@ -1827,7 +2405,7 @@ def exception_specification_kind(self):
         return self._exception_specification_kind
 
     @property
-    def underlying_typedef_type(self):
+    def underlying_typedef_type(self) -> Type:
         """Return the underlying type of a typedef declaration.
 
         Returns a Type for the typedef this cursor is a declaration for. If
@@ -1840,7 +2418,7 @@ def underlying_typedef_type(self):
         return self._underlying_type
 
     @property
-    def enum_type(self):
+    def enum_type(self) -> Type:
         """Return the integer type of an enum declaration.
 
         Returns a Type corresponding to an integer. If the cursor is not for an
@@ -1853,7 +2431,7 @@ def enum_type(self):
         return self._enum_type
 
     @property
-    def enum_value(self):
+    def enum_value(self) -> int:
         """Return the value of an enum constant."""
         if not hasattr(self, "_enum_value"):
             assert self.kind == CursorKind.ENUM_CONSTANT_DECL
@@ -1879,7 +2457,7 @@ def enum_value(self):
         return self._enum_value
 
     @property
-    def objc_type_encoding(self):
+    def objc_type_encoding(self) -> str:
         """Return the Objective-C type encoding as a str."""
         if not hasattr(self, "_objc_type_encoding"):
             self._objc_type_encoding = conf.lib.clang_getDeclObjCTypeEncoding(self)
@@ -1887,7 +2465,7 @@ def objc_type_encoding(self):
         return self._objc_type_encoding
 
     @property
-    def hash(self):
+    def hash(self) -> int:
         """Returns a hash of the cursor as an int."""
         if not hasattr(self, "_hash"):
             self._hash = conf.lib.clang_hashCursor(self)
@@ -1895,7 +2473,7 @@ def hash(self):
         return self._hash
 
     @property
-    def semantic_parent(self):
+    def semantic_parent(self) -> Cursor:
         """Return the semantic parent for this cursor."""
         if not hasattr(self, "_semantic_parent"):
             self._semantic_parent = conf.lib.clang_getCursorSemanticParent(self)
@@ -1903,7 +2481,7 @@ def semantic_parent(self):
         return self._semantic_parent
 
     @property
-    def lexical_parent(self):
+    def lexical_parent(self) -> Cursor:
         """Return the lexical parent for this cursor."""
         if not hasattr(self, "_lexical_parent"):
             self._lexical_parent = conf.lib.clang_getCursorLexicalParent(self)
@@ -1911,14 +2489,14 @@ def lexical_parent(self):
         return self._lexical_parent
 
     @property
-    def translation_unit(self):
+    def translation_unit(self) -> TranslationUnit:
         """Returns the TranslationUnit to which this Cursor belongs."""
         # If this triggers an AttributeError, the instance was not properly
         # created.
         return self._tu
 
     @property
-    def referenced(self):
+    def referenced(self) -> Cursor:
         """
         For a cursor that is a reference, returns a cursor
         representing the entity that it references.
@@ -1929,47 +2507,47 @@ def referenced(self):
         return self._referenced
 
     @property
-    def brief_comment(self):
+    def brief_comment(self) -> str:
         """Returns the brief comment text associated with that Cursor"""
         return conf.lib.clang_Cursor_getBriefCommentText(self)
 
     @property
-    def raw_comment(self):
+    def raw_comment(self) -> str:
         """Returns the raw comment text associated with that Cursor"""
         return conf.lib.clang_Cursor_getRawCommentText(self)
 
-    def get_arguments(self):
+    def get_arguments(self) -> Iterator[Cursor]:
         """Return an iterator for accessing the arguments of this cursor."""
         num_args = conf.lib.clang_Cursor_getNumArguments(self)
         for i in range(0, num_args):
             yield conf.lib.clang_Cursor_getArgument(self, i)
 
-    def get_num_template_arguments(self):
+    def get_num_template_arguments(self) -> int:
         """Returns the number of template args associated with this cursor."""
         return conf.lib.clang_Cursor_getNumTemplateArguments(self)
 
-    def get_template_argument_kind(self, num):
+    def get_template_argument_kind(self, num: int) -> TemplateArgumentKind:
         """Returns the TemplateArgumentKind for the indicated template
         argument."""
         return conf.lib.clang_Cursor_getTemplateArgumentKind(self, num)
 
-    def get_template_argument_type(self, num):
+    def get_template_argument_type(self, num: int) -> Type:
         """Returns the CXType for the indicated template argument."""
         return conf.lib.clang_Cursor_getTemplateArgumentType(self, num)
 
-    def get_template_argument_value(self, num):
+    def get_template_argument_value(self, num: int) -> int:
         """Returns the value of the indicated arg as a signed 64b integer."""
         return conf.lib.clang_Cursor_getTemplateArgumentValue(self, num)
 
-    def get_template_argument_unsigned_value(self, num):
+    def get_template_argument_unsigned_value(self, num: int) -> int:
         """Returns the value of the indicated arg as an unsigned 64b integer."""
         return conf.lib.clang_Cursor_getTemplateArgumentUnsignedValue(self, num)
 
-    def get_children(self):
+    def get_children(self) -> Iterable[Cursor]:
         """Return an iterator for accessing the children of this cursor."""
 
         # FIXME: Expose iteration from CIndex, PR6125.
-        def visitor(child, parent, children):
+        def visitor(child: Cursor, parent: Any, children: list[Cursor]) -> int:
             # FIXME: Document this assertion in API.
             # FIXME: There should just be an isNull method.
             assert child != conf.lib.clang_getNullCursor()
@@ -1979,11 +2557,11 @@ def visitor(child, parent, children):
             children.append(child)
             return 1  # continue
 
-        children = []
-        conf.lib.clang_visitChildren(self, callbacks["cursor_visit"](visitor), children)
+        children: list[Cursor] = []
+        conf.lib.clang_visitChildren(self, cursor_visit_callback(visitor), children)
         return iter(children)
 
-    def walk_preorder(self):
+    def walk_preorder(self) -> Iterator[Cursor]:
         """Depth-first preorder walk over the cursor and its descendants.
 
         Yields cursors.
@@ -1993,7 +2571,7 @@ def walk_preorder(self):
             for descendant in child.walk_preorder():
                 yield descendant
 
-    def get_tokens(self):
+    def get_tokens(self) -> Iterator[Token]:
         """Obtain Token instances formulating that compose this Cursor.
 
         This is a generator for Token instances. It returns all tokens which
@@ -2001,11 +2579,11 @@ def get_tokens(self):
         """
         return TokenGroup.get_tokens(self._tu, self.extent)
 
-    def get_field_offsetof(self):
+    def get_field_offsetof(self) -> int:
         """Returns the offsetof the FIELD_DECL pointed by this Cursor."""
         return conf.lib.clang_Cursor_getOffsetOfField(self)
 
-    def is_anonymous(self):
+    def is_anonymous(self) -> bool:
         """
         Check if the record is anonymous.
         """
@@ -2013,20 +2591,22 @@ def is_anonymous(self):
             return self.type.get_declaration().is_anonymous()
         return conf.lib.clang_Cursor_isAnonymous(self)
 
-    def is_bitfield(self):
+    def is_bitfield(self) -> bool:
         """
         Check if the field is a bitfield.
         """
         return conf.lib.clang_Cursor_isBitField(self)
 
-    def get_bitfield_width(self):
+    def get_bitfield_width(self) -> int:
         """
         Retrieve the width of a bitfield.
         """
         return conf.lib.clang_getFieldDeclBitWidth(self)
 
     @staticmethod
-    def from_result(res, fn, args):
+    def from_result(
+        res: Cursor, fn: Any, args: Sequence[Cursor | TranslationUnit | Type]
+    ) -> Cursor | None:
         assert isinstance(res, Cursor)
         # FIXME: There should just be an isNull method.
         if res == conf.lib.clang_getNullCursor():
@@ -2050,7 +2630,9 @@ def from_result(res, fn, args):
         return res
 
     @staticmethod
-    def from_cursor_result(res, fn, args):
+    def from_cursor_result(
+        res: Cursor, fn: Any, args: Sequence[Cursor]
+    ) -> Cursor | None:
         assert isinstance(res, Cursor)
         if res == conf.lib.clang_getNullCursor():
             return None
@@ -2059,56 +2641,21 @@ def from_cursor_result(res, fn, args):
         return res
 
 
-class StorageClass(object):
+class StorageClass(BaseEnumeration):
     """
     Describes the storage class of a declaration
     """
 
-    # The unique kind objects, index by id.
-    _kinds = []
-    _name_map = None
-
-    def __init__(self, value):
-        if value >= len(StorageClass._kinds):
-            StorageClass._kinds += [None] * (value - len(StorageClass._kinds) + 1)
-        if StorageClass._kinds[value] is not None:
-            raise ValueError("StorageClass already loaded")
-        self.value = value
-        StorageClass._kinds[value] = self
-        StorageClass._name_map = None
-
-    def from_param(self):
-        return self.value
-
-    @property
-    def name(self):
-        """Get the enumeration name of this storage class."""
-        if self._name_map is None:
-            self._name_map = {}
-            for key, value in StorageClass.__dict__.items():
-                if isinstance(value, StorageClass):
-                    self._name_map[value] = key
-        return self._name_map[self]
-
-    @staticmethod
-    def from_id(id):
-        if id >= len(StorageClass._kinds) or not StorageClass._kinds[id]:
-            raise ValueError("Unknown storage class %d" % id)
-        return StorageClass._kinds[id]
-
-    def __repr__(self):
-        return "StorageClass.%s" % (self.name,)
+    INVALID = 0
+    NONE = 1
+    EXTERN = 2
+    STATIC = 3
+    PRIVATEEXTERN = 4
+    OPENCLWORKGROUPLOCAL = 5
+    AUTO = 6
+    REGISTER = 7
 
 
-StorageClass.INVALID = StorageClass(0)
-StorageClass.NONE = StorageClass(1)
-StorageClass.EXTERN = StorageClass(2)
-StorageClass.STATIC = StorageClass(3)
-StorageClass.PRIVATEEXTERN = StorageClass(4)
-StorageClass.OPENCLWORKGROUPLOCAL = StorageClass(5)
-StorageClass.AUTO = StorageClass(6)
-StorageClass.REGISTER = StorageClass(7)
-
 ### Availability Kinds ###
 
 
@@ -2117,18 +2664,11 @@ class AvailabilityKind(BaseEnumeration):
     Describes the availability of an entity.
     """
 
-    # The unique kind objects, indexed by id.
-    _kinds = []
-    _name_map = None
-
-    def __repr__(self):
-        return "AvailabilityKind.%s" % (self.name,)
-
+    AVAILABLE = 0
+    DEPRECATED = 1
+    NOT_AVAILABLE = 2
+    NOT_ACCESSIBLE = 3
 
-AvailabilityKind.AVAILABLE = AvailabilityKind(0)
-AvailabilityKind.DEPRECATED = AvailabilityKind(1)
-AvailabilityKind.NOT_AVAILABLE = AvailabilityKind(2)
-AvailabilityKind.NOT_ACCESSIBLE = AvailabilityKind(3)
 
 ### C++ access specifiers ###
 
@@ -2138,22 +2678,12 @@ class AccessSpecifier(BaseEnumeration):
     Describes the access of a C++ class member
     """
 
-    # The unique kind objects, index by id.
-    _kinds = []
-    _name_map = None
-
-    def from_param(self):
-        return self.value
+    INVALID = 0
+    PUBLIC = 1
+    PROTECTED = 2
+    PRIVATE = 3
+    NONE = 4
 
-    def __repr__(self):
-        return "AccessSpecifier.%s" % (self.name,)
-
-
-AccessSpecifier.INVALID = AccessSpecifier(0)
-AccessSpecifier.PUBLIC = AccessSpecifier(1)
-AccessSpecifier.PROTECTED = AccessSpecifier(2)
-AccessSpecifier.PRIVATE = AccessSpecifier(3)
-AccessSpecifier.NONE = AccessSpecifier(4)
 
 ### Type Kinds ###
 
@@ -2163,175 +2693,134 @@ class TypeKind(BaseEnumeration):
     Describes the kind of type.
     """
 
-    # The unique kind objects, indexed by id.
-    _kinds = []
-    _name_map = None
-
     @property
-    def spelling(self):
+    def spelling(self) -> str:
         """Retrieve the spelling of this TypeKind."""
         return conf.lib.clang_getTypeKindSpelling(self.value)
 
-    def __repr__(self):
-        return "TypeKind.%s" % (self.name,)
-
-
-TypeKind.INVALID = TypeKind(0)
-TypeKind.UNEXPOSED = TypeKind(1)
-TypeKind.VOID = TypeKind(2)
-TypeKind.BOOL = TypeKind(3)
-TypeKind.CHAR_U = TypeKind(4)
-TypeKind.UCHAR = TypeKind(5)
-TypeKind.CHAR16 = TypeKind(6)
-TypeKind.CHAR32 = TypeKind(7)
-TypeKind.USHORT = TypeKind(8)
-TypeKind.UINT = TypeKind(9)
-TypeKind.ULONG = TypeKind(10)
-TypeKind.ULONGLONG = TypeKind(11)
-TypeKind.UINT128 = TypeKind(12)
-TypeKind.CHAR_S = TypeKind(13)
-TypeKind.SCHAR = TypeKind(14)
-TypeKind.WCHAR = TypeKind(15)
-TypeKind.SHORT = TypeKind(16)
-TypeKind.INT = TypeKind(17)
-TypeKind.LONG = TypeKind(18)
-TypeKind.LONGLONG = TypeKind(19)
-TypeKind.INT128 = TypeKind(20)
-TypeKind.FLOAT = TypeKind(21)
-TypeKind.DOUBLE = TypeKind(22)
-TypeKind.LONGDOUBLE = TypeKind(23)
-TypeKind.NULLPTR = TypeKind(24)
-TypeKind.OVERLOAD = TypeKind(25)
-TypeKind.DEPENDENT = TypeKind(26)
-TypeKind.OBJCID = TypeKind(27)
-TypeKind.OBJCCLASS = TypeKind(28)
-TypeKind.OBJCSEL = TypeKind(29)
-TypeKind.FLOAT128 = TypeKind(30)
-TypeKind.HALF = TypeKind(31)
-TypeKind.IBM128 = TypeKind(40)
-TypeKind.COMPLEX = TypeKind(100)
-TypeKind.POINTER = TypeKind(101)
-TypeKind.BLOCKPOINTER = TypeKind(102)
-TypeKind.LVALUEREFERENCE = TypeKind(103)
-TypeKind.RVALUEREFERENCE = TypeKind(104)
-TypeKind.RECORD = TypeKind(105)
-TypeKind.ENUM = TypeKind(106)
-TypeKind.TYPEDEF = TypeKind(107)
-TypeKind.OBJCINTERFACE = TypeKind(108)
-TypeKind.OBJCOBJECTPOINTER = TypeKind(109)
-TypeKind.FUNCTIONNOPROTO = TypeKind(110)
-TypeKind.FUNCTIONPROTO = TypeKind(111)
-TypeKind.CONSTANTARRAY = TypeKind(112)
-TypeKind.VECTOR = TypeKind(113)
-TypeKind.INCOMPLETEARRAY = TypeKind(114)
-TypeKind.VARIABLEARRAY = TypeKind(115)
-TypeKind.DEPENDENTSIZEDARRAY = TypeKind(116)
-TypeKind.MEMBERPOINTER = TypeKind(117)
-TypeKind.AUTO = TypeKind(118)
-TypeKind.ELABORATED = TypeKind(119)
-TypeKind.PIPE = TypeKind(120)
-TypeKind.OCLIMAGE1DRO = TypeKind(121)
-TypeKind.OCLIMAGE1DARRAYRO = TypeKind(122)
-TypeKind.OCLIMAGE1DBUFFERRO = TypeKind(123)
-TypeKind.OCLIMAGE2DRO = TypeKind(124)
-TypeKind.OCLIMAGE2DARRAYRO = TypeKind(125)
-TypeKind.OCLIMAGE2DDEPTHRO = TypeKind(126)
-TypeKind.OCLIMAGE2DARRAYDEPTHRO = TypeKind(127)
-TypeKind.OCLIMAGE2DMSAARO = TypeKind(128)
-TypeKind.OCLIMAGE2DARRAYMSAARO = TypeKind(129)
-TypeKind.OCLIMAGE2DMSAADEPTHRO = TypeKind(130)
-TypeKind.OCLIMAGE2DARRAYMSAADEPTHRO = TypeKind(131)
-TypeKind.OCLIMAGE3DRO = TypeKind(132)
-TypeKind.OCLIMAGE1DWO = TypeKind(133)
-TypeKind.OCLIMAGE1DARRAYWO = TypeKind(134)
-TypeKind.OCLIMAGE1DBUFFERWO = TypeKind(135)
-TypeKind.OCLIMAGE2DWO = TypeKind(136)
-TypeKind.OCLIMAGE2DARRAYWO = TypeKind(137)
-TypeKind.OCLIMAGE2DDEPTHWO = TypeKind(138)
-TypeKind.OCLIMAGE2DARRAYDEPTHWO = TypeKind(139)
-TypeKind.OCLIMAGE2DMSAAWO = TypeKind(140)
-TypeKind.OCLIMAGE2DARRAYMSAAWO = TypeKind(141)
-TypeKind.OCLIMAGE2DMSAADEPTHWO = TypeKind(142)
-TypeKind.OCLIMAGE2DARRAYMSAADEPTHWO = TypeKind(143)
-TypeKind.OCLIMAGE3DWO = TypeKind(144)
-TypeKind.OCLIMAGE1DRW = TypeKind(145)
-TypeKind.OCLIMAGE1DARRAYRW = TypeKind(146)
-TypeKind.OCLIMAGE1DBUFFERRW = TypeKind(147)
-TypeKind.OCLIMAGE2DRW = TypeKind(148)
-TypeKind.OCLIMAGE2DARRAYRW = TypeKind(149)
-TypeKind.OCLIMAGE2DDEPTHRW = TypeKind(150)
-TypeKind.OCLIMAGE2DARRAYDEPTHRW = TypeKind(151)
-TypeKind.OCLIMAGE2DMSAARW = TypeKind(152)
-TypeKind.OCLIMAGE2DARRAYMSAARW = TypeKind(153)
-TypeKind.OCLIMAGE2DMSAADEPTHRW = TypeKind(154)
-TypeKind.OCLIMAGE2DARRAYMSAADEPTHRW = TypeKind(155)
-TypeKind.OCLIMAGE3DRW = TypeKind(156)
-TypeKind.OCLSAMPLER = TypeKind(157)
-TypeKind.OCLEVENT = TypeKind(158)
-TypeKind.OCLQUEUE = TypeKind(159)
-TypeKind.OCLRESERVEID = TypeKind(160)
-
-TypeKind.EXTVECTOR = TypeKind(176)
-TypeKind.ATOMIC = TypeKind(177)
+    INVALID = 0
+    UNEXPOSED = 1
+    VOID = 2
+    BOOL = 3
+    CHAR_U = 4
+    UCHAR = 5
+    CHAR16 = 6
+    CHAR32 = 7
+    USHORT = 8
+    UINT = 9
+    ULONG = 10
+    ULONGLONG = 11
+    UINT128 = 12
+    CHAR_S = 13
+    SCHAR = 14
+    WCHAR = 15
+    SHORT = 16
+    INT = 17
+    LONG = 18
+    LONGLONG = 19
+    INT128 = 20
+    FLOAT = 21
+    DOUBLE = 22
+    LONGDOUBLE = 23
+    NULLPTR = 24
+    OVERLOAD = 25
+    DEPENDENT = 26
+    OBJCID = 27
+    OBJCCLASS = 28
+    OBJCSEL = 29
+    FLOAT128 = 30
+    HALF = 31
+    IBM128 = 40
+    COMPLEX = 100
+    POINTER = 101
+    BLOCKPOINTER = 102
+    LVALUEREFERENCE = 103
+    RVALUEREFERENCE = 104
+    RECORD = 105
+    ENUM = 106
+    TYPEDEF = 107
+    OBJCINTERFACE = 108
+    OBJCOBJECTPOINTER = 109
+    FUNCTIONNOPROTO = 110
+    FUNCTIONPROTO = 111
+    CONSTANTARRAY = 112
+    VECTOR = 113
+    INCOMPLETEARRAY = 114
+    VARIABLEARRAY = 115
+    DEPENDENTSIZEDARRAY = 116
+    MEMBERPOINTER = 117
+    AUTO = 118
+    ELABORATED = 119
+    PIPE = 120
+    OCLIMAGE1DRO = 121
+    OCLIMAGE1DARRAYRO = 122
+    OCLIMAGE1DBUFFERRO = 123
+    OCLIMAGE2DRO = 124
+    OCLIMAGE2DARRAYRO = 125
+    OCLIMAGE2DDEPTHRO = 126
+    OCLIMAGE2DARRAYDEPTHRO = 127
+    OCLIMAGE2DMSAARO = 128
+    OCLIMAGE2DARRAYMSAARO = 129
+    OCLIMAGE2DMSAADEPTHRO = 130
+    OCLIMAGE2DARRAYMSAADEPTHRO = 131
+    OCLIMAGE3DRO = 132
+    OCLIMAGE1DWO = 133
+    OCLIMAGE1DARRAYWO = 134
+    OCLIMAGE1DBUFFERWO = 135
+    OCLIMAGE2DWO = 136
+    OCLIMAGE2DARRAYWO = 137
+    OCLIMAGE2DDEPTHWO = 138
+    OCLIMAGE2DARRAYDEPTHWO = 139
+    OCLIMAGE2DMSAAWO = 140
+    OCLIMAGE2DARRAYMSAAWO = 141
+    OCLIMAGE2DMSAADEPTHWO = 142
+    OCLIMAGE2DARRAYMSAADEPTHWO = 143
+    OCLIMAGE3DWO = 144
+    OCLIMAGE1DRW = 145
+    OCLIMAGE1DARRAYRW = 146
+    OCLIMAGE1DBUFFERRW = 147
+    OCLIMAGE2DRW = 148
+    OCLIMAGE2DARRAYRW = 149
+    OCLIMAGE2DDEPTHRW = 150
+    OCLIMAGE2DARRAYDEPTHRW = 151
+    OCLIMAGE2DMSAARW = 152
+    OCLIMAGE2DARRAYMSAARW = 153
+    OCLIMAGE2DMSAADEPTHRW = 154
+    OCLIMAGE2DARRAYMSAADEPTHRW = 155
+    OCLIMAGE3DRW = 156
+    OCLSAMPLER = 157
+    OCLEVENT = 158
+    OCLQUEUE = 159
+    OCLRESERVEID = 160
+
+    EXTVECTOR = 176
+    ATOMIC = 177
 
 
 class RefQualifierKind(BaseEnumeration):
     """Describes a specific ref-qualifier of a type."""
 
-    # The unique kind objects, indexed by id.
-    _kinds = []
-    _name_map = None
-
-    def from_param(self):
-        return self.value
-
-    def __repr__(self):
-        return "RefQualifierKind.%s" % (self.name,)
-
-
-RefQualifierKind.NONE = RefQualifierKind(0)
-RefQualifierKind.LVALUE = RefQualifierKind(1)
-RefQualifierKind.RVALUE = RefQualifierKind(2)
+    NONE = 0
+    LVALUE = 1
+    RVALUE = 2
 
 
 class LinkageKind(BaseEnumeration):
     """Describes the kind of linkage of a cursor."""
 
-    # The unique kind objects, indexed by id.
-    _kinds = []
-    _name_map = None
-
-    def from_param(self):
-        return self.value
-
-    def __repr__(self):
-        return "LinkageKind.%s" % (self.name,)
-
-
-LinkageKind.INVALID = LinkageKind(0)
-LinkageKind.NO_LINKAGE = LinkageKind(1)
-LinkageKind.INTERNAL = LinkageKind(2)
-LinkageKind.UNIQUE_EXTERNAL = LinkageKind(3)
-LinkageKind.EXTERNAL = LinkageKind(4)
+    INVALID = 0
+    NO_LINKAGE = 1
+    INTERNAL = 2
+    UNIQUE_EXTERNAL = 3
+    EXTERNAL = 4
 
 
 class TLSKind(BaseEnumeration):
     """Describes the kind of thread-local storage (TLS) of a cursor."""
 
-    # The unique kind objects, indexed by id.
-    _kinds = []
-    _name_map = None
-
-    def from_param(self):
-        return self.value
-
-    def __repr__(self):
-        return "TLSKind.%s" % (self.name,)
-
-
-TLSKind.NONE = TLSKind(0)
-TLSKind.DYNAMIC = TLSKind(1)
-TLSKind.STATIC = TLSKind(2)
+    NONE = 0
+    DYNAMIC = 1
+    STATIC = 2
 
 
 class Type(Structure):
@@ -2341,30 +2830,32 @@ class Type(Structure):
 
     _fields_ = [("_kind_id", c_int), ("data", c_void_p * 2)]
 
+    _tu: TranslationUnit
+
     @property
-    def kind(self):
+    def kind(self) -> TypeKind:
         """Return the kind of this type."""
         return TypeKind.from_id(self._kind_id)
 
-    def argument_types(self):
+    def argument_types(self) -> NoSliceSequence[Type]:
         """Retrieve a container for the non-variadic arguments for this type.
 
         The returned object is iterable and indexable. Each item in the
         container is a Type instance.
         """
 
-        class ArgumentsIterator(collections_abc.Sequence):
-            def __init__(self, parent):
+        class ArgumentsIterator:
+            def __init__(self, parent: Type):
                 self.parent = parent
-                self.length = None
+                self.length: int | None = None
 
-            def __len__(self):
+            def __len__(self) -> int:
                 if self.length is None:
                     self.length = conf.lib.clang_getNumArgTypes(self.parent)
 
                 return self.length
 
-            def __getitem__(self, key):
+            def __getitem__(self, key: int) -> Type:
                 # FIXME Support slice objects.
                 if not isinstance(key, int):
                     raise TypeError("Must supply a non-negative int.")
@@ -2388,7 +2879,7 @@ def __getitem__(self, key):
         return ArgumentsIterator(self)
 
     @property
-    def element_type(self):
+    def element_type(self) -> Type:
         """Retrieve the Type of elements within this Type.
 
         If accessed on a type that is not an array, complex, or vector type, an
@@ -2401,7 +2892,7 @@ def element_type(self):
         return result
 
     @property
-    def element_count(self):
+    def element_count(self) -> int:
         """Retrieve the number of elements in this type.
 
         Returns an int.
@@ -2415,14 +2906,14 @@ def element_count(self):
         return result
 
     @property
-    def translation_unit(self):
+    def translation_unit(self) -> TranslationUnit:
         """The TranslationUnit to which this Type is associated."""
         # If this triggers an AttributeError, the instance was not properly
         # instantiated.
         return self._tu
 
     @staticmethod
-    def from_result(res, fn, args):
+    def from_result(res: Type, fn: Any, args: Sequence[Cursor | Type]) -> Type:
         assert isinstance(res, Type)
 
         tu = None
@@ -2436,13 +2927,13 @@ def from_result(res, fn, args):
 
         return res
 
-    def get_num_template_arguments(self):
+    def get_num_template_arguments(self) -> int:
         return conf.lib.clang_Type_getNumTemplateArguments(self)
 
-    def get_template_argument_type(self, num):
+    def get_template_argument_type(self, num: int) -> Type:
         return conf.lib.clang_Type_getTemplateArgumentAsType(self, num)
 
-    def get_canonical(self):
+    def get_canonical(self) -> Type:
         """
         Return the canonical type for a Type.
 
@@ -2454,7 +2945,7 @@ def get_canonical(self):
         """
         return conf.lib.clang_getCanonicalType(self)
 
-    def is_const_qualified(self):
+    def is_const_qualified(self) -> bool:
         """Determine whether a Type has the "const" qualifier set.
 
         This does not look through typedefs that may have added "const"
@@ -2462,7 +2953,7 @@ def is_const_qualified(self):
         """
         return conf.lib.clang_isConstQualifiedType(self)
 
-    def is_volatile_qualified(self):
+    def is_volatile_qualified(self) -> bool:
         """Determine whether a Type has the "volatile" qualifier set.
 
         This does not look through typedefs that may have added "volatile"
@@ -2470,7 +2961,7 @@ def is_volatile_qualified(self):
         """
         return conf.lib.clang_isVolatileQualifiedType(self)
 
-    def is_restrict_qualified(self):
+    def is_restrict_qualified(self) -> bool:
         """Determine whether a Type has the "restrict" qualifier set.
 
         This does not look through typedefs that may have added "restrict" at
@@ -2478,92 +2969,92 @@ def is_restrict_qualified(self):
         """
         return conf.lib.clang_isRestrictQualifiedType(self)
 
-    def is_function_variadic(self):
+    def is_function_variadic(self) -> bool:
         """Determine whether this function Type is a variadic function type."""
         assert self.kind == TypeKind.FUNCTIONPROTO
 
         return conf.lib.clang_isFunctionTypeVariadic(self)
 
-    def get_address_space(self):
+    def get_address_space(self) -> int:
         return conf.lib.clang_getAddressSpace(self)
 
-    def get_typedef_name(self):
+    def get_typedef_name(self) -> str:
         return conf.lib.clang_getTypedefName(self)
 
-    def is_pod(self):
+    def is_pod(self) -> bool:
         """Determine whether this Type represents plain old data (POD)."""
         return conf.lib.clang_isPODType(self)
 
-    def get_pointee(self):
+    def get_pointee(self) -> Type:
         """
         For pointer types, returns the type of the pointee.
         """
         return conf.lib.clang_getPointeeType(self)
 
-    def get_declaration(self):
+    def get_declaration(self) -> Cursor:
         """
         Return the cursor for the declaration of the given type.
         """
         return conf.lib.clang_getTypeDeclaration(self)
 
-    def get_result(self):
+    def get_result(self) -> Type:
         """
         Retrieve the result type associated with a function type.
         """
         return conf.lib.clang_getResultType(self)
 
-    def get_array_element_type(self):
+    def get_array_element_type(self) -> Type:
         """
         Retrieve the type of the elements of the array type.
         """
         return conf.lib.clang_getArrayElementType(self)
 
-    def get_array_size(self):
+    def get_array_size(self) -> int:
         """
         Retrieve the size of the constant array.
         """
         return conf.lib.clang_getArraySize(self)
 
-    def get_class_type(self):
+    def get_class_type(self) -> Type:
         """
         Retrieve the class type of the member pointer type.
         """
         return conf.lib.clang_Type_getClassType(self)
 
-    def get_named_type(self):
+    def get_named_type(self) -> Type:
         """
         Retrieve the type named by the qualified-id.
         """
         return conf.lib.clang_Type_getNamedType(self)
 
-    def get_align(self):
+    def get_align(self) -> int:
         """
         Retrieve the alignment of the record.
         """
         return conf.lib.clang_Type_getAlignOf(self)
 
-    def get_size(self):
+    def get_size(self) -> int:
         """
         Retrieve the size of the record.
         """
         return conf.lib.clang_Type_getSizeOf(self)
 
-    def get_offset(self, fieldname):
+    def get_offset(self, fieldname: str) -> int:
         """
         Retrieve the offset of a field in the record.
         """
         return conf.lib.clang_Type_getOffsetOf(self, fieldname)
 
-    def get_ref_qualifier(self):
+    def get_ref_qualifier(self) -> RefQualifierKind:
         """
         Retrieve the ref-qualifier of the type.
         """
         return RefQualifierKind.from_id(conf.lib.clang_Type_getCXXRefQualifier(self))
 
-    def get_fields(self):
+    def get_fields(self) -> Iterable[Cursor]:
         """Return an iterator for accessing the fields of this type."""
 
-        def visitor(field, children):
+        def visitor(field: Cursor, children: Any) -> int:
             assert field != conf.lib.clang_getNullCursor()
 
             # Create reference to TU so it isn't GC'd before Cursor.
@@ -2571,33 +3062,31 @@ def visitor(field, children):
             fields.append(field)
             return 1  # continue
 
-        fields = []
-        conf.lib.clang_Type_visitFields(
-            self, callbacks["fields_visit"](visitor), fields
-        )
+        fields: list[Cursor] = []
+        conf.lib.clang_Type_visitFields(self, fields_visit_callback(visitor), fields)
         return iter(fields)
 
-    def get_exception_specification_kind(self):
+    def get_exception_specification_kind(self) -> ExceptionSpecificationKind:
         """
         Return the kind of the exception specification; a value from
         the ExceptionSpecificationKind enumeration.
         """
         return ExceptionSpecificationKind.from_id(
-            conf.lib.clang.getExceptionSpecificationType(self)
+            conf.lib.clang_getExceptionSpecificationType(self)
         )
 
     @property
-    def spelling(self):
+    def spelling(self) -> str:
         """Retrieve the spelling of this Type."""
         return conf.lib.clang_getTypeSpelling(self)
 
-    def __eq__(self, other):
-        if type(other) != type(self):
+    def __eq__(self, other: object) -> bool:
+        if not isinstance(other, Type):
             return False
 
         return conf.lib.clang_equalTypes(self, other)
 
-    def __ne__(self, other):
+    def __ne__(self, other: object) -> bool:
         return not self.__eq__(other)
 
 
@@ -2608,17 +3097,17 @@ def __ne__(self, other):
 # a void*.
 
 
-class ClangObject(object):
+class ClangObject:
     """
     A helper for Clang objects. This class helps act as an intermediary for
     the ctypes library and the Clang CIndex library.
     """
 
-    def __init__(self, obj):
+    def __init__(self, obj: CObjP):
         assert isinstance(obj, c_object_p) and obj
         self.obj = self._as_parameter_ = obj
 
-    def from_param(self):
+    def from_param(self) -> CObjP:
         return self._as_parameter_
 
 
@@ -2656,27 +3145,27 @@ class _CXUnsavedFile(Structure):
 }
 
 
-class CompletionChunk(object):
-    class Kind(object):
-        def __init__(self, name):
+class CompletionChunk:
+    class Kind:
+        def __init__(self, name: str):
             self.name = name
 
-        def __str__(self):
+        def __str__(self) -> str:
             return self.name
 
-        def __repr__(self):
+        def __repr__(self) -> str:
             return "<ChunkKind: %s>" % self
 
-    def __init__(self, completionString, key):
+    def __init__(self, completionString: CObjP, key: int):
         self.cs = completionString
         self.key = key
         self.__kindNumberCache = -1
 
-    def __repr__(self):
+    def __repr__(self) -> str:
         return "{'" + self.spelling + "', " + str(self.kind) + "}"
 
     @CachedProperty
-    def spelling(self):
+    def spelling(self) -> str:
         if self.__kindNumber in SpellingCache:
             return SpellingCache[self.__kindNumber]
         return conf.lib.clang_getCompletionChunkText(self.cs, self.key)
@@ -2685,7 +3174,7 @@ def spelling(self):
     # apparently still significantly faster. Please profile carefully if you
     # would like to add CachedProperty back.
     @property
-    def __kindNumber(self):
+    def __kindNumber(self) -> int:
         if self.__kindNumberCache == -1:
             self.__kindNumberCache = conf.lib.clang_getCompletionChunkKind(
                 self.cs, self.key
@@ -2693,31 +3182,31 @@ def __kindNumber(self):
         return self.__kindNumberCache
 
     @CachedProperty
-    def kind(self):
+    def kind(self) -> Kind:
         return completionChunkKindMap[self.__kindNumber]
 
     @CachedProperty
-    def string(self):
+    def string(self) -> CompletionString | None:
         res = conf.lib.clang_getCompletionChunkCompletionString(self.cs, self.key)
 
         if res:
             return CompletionString(res)
         else:
-            None
+            return None
 
-    def isKindOptional(self):
+    def isKindOptional(self) -> bool:
         return self.__kindNumber == 0
 
-    def isKindTypedText(self):
+    def isKindTypedText(self) -> bool:
         return self.__kindNumber == 1
 
-    def isKindPlaceHolder(self):
+    def isKindPlaceHolder(self) -> bool:
         return self.__kindNumber == 3
 
-    def isKindInformative(self):
+    def isKindInformative(self) -> bool:
         return self.__kindNumber == 4
 
-    def isKindResultType(self):
+    def isKindResultType(self) -> bool:
         return self.__kindNumber == 15
 
 
@@ -2747,44 +3236,48 @@ def isKindResultType(self):
 
 
 class CompletionString(ClangObject):
-    class Availability(object):
-        def __init__(self, name):
+    class Availability:
+        def __init__(self, name: str):
             self.name = name
 
-        def __str__(self):
+        def __str__(self) -> str:
             return self.name
 
-        def __repr__(self):
+        def __repr__(self) -> str:
             return "<Availability: %s>" % self
 
-    def __len__(self):
+    def __len__(self) -> int:
         return self.num_chunks
 
     @CachedProperty
-    def num_chunks(self):
+    def num_chunks(self) -> int:
         return conf.lib.clang_getNumCompletionChunks(self.obj)
 
-    def __getitem__(self, key):
+    def __getitem__(self, key: int) -> CompletionChunk:
         if self.num_chunks <= key:
             raise IndexError
         return CompletionChunk(self.obj, key)
 
+    def __iter__(self) -> Iterator[CompletionChunk]:
+        for i in range(len(self)):
+            yield self[i]
+
     @property
-    def priority(self):
+    def priority(self) -> int:
         return conf.lib.clang_getCompletionPriority(self.obj)
 
     @property
-    def availability(self):
+    def availability(self) -> CompletionChunk.Kind:
         res = conf.lib.clang_getCompletionAvailability(self.obj)
         return availabilityKinds[res]
 
     @property
-    def briefComment(self):
+    def briefComment(self) -> str:
         if conf.function_exists("clang_getCompletionBriefComment"):
             return conf.lib.clang_getCompletionBriefComment(self.obj)
-        return _CXString()
+        return ""
 
-    def __repr__(self):
+    def __repr__(self) -> str:
         return (
             " | ".join([str(a) for a in self])
             + " || Priority: "
@@ -2807,25 +3300,28 @@ def __repr__(self):
 class CodeCompletionResult(Structure):
     _fields_ = [("cursorKind", c_int), ("completionString", c_object_p)]
 
-    def __repr__(self):
+    def __repr__(self) -> str:
         return str(CompletionString(self.completionString))
 
     @property
-    def kind(self):
+    def kind(self) -> CursorKind:
         return CursorKind.from_id(self.cursorKind)
 
     @property
-    def string(self):
+    def string(self) -> CompletionString:
         return CompletionString(self.completionString)
 
 
 class CCRStructure(Structure):
     _fields_ = [("results", POINTER(CodeCompletionResult)), ("numResults", c_int)]
 
-    def __len__(self):
+    results: list[CodeCompletionResult]
+    numResults: int
+
+    def __len__(self) -> int:
         return self.numResults
 
-    def __getitem__(self, key):
+    def __getitem__(self, key: int) -> CodeCompletionResult:
         if len(self) <= key:
             raise IndexError
 
@@ -2833,30 +3329,30 @@ def __getitem__(self, key):
 
 
 class CodeCompletionResults(ClangObject):
-    def __init__(self, ptr):
+    def __init__(self, ptr: _Pointer[CCRStructure]):
         assert isinstance(ptr, POINTER(CCRStructure)) and ptr
         self.ptr = self._as_parameter_ = ptr
 
-    def from_param(self):
+    def from_param(self) -> _Pointer[CCRStructure]:
         return self._as_parameter_
 
-    def __del__(self):
+    def __del__(self) -> None:
         conf.lib.clang_disposeCodeCompleteResults(self)
 
     @property
-    def results(self):
+    def results(self) -> CCRStructure:
         return self.ptr.contents
 
     @property
-    def diagnostics(self):
-        class DiagnosticsItr(object):
-            def __init__(self, ccr):
+    def diagnostics(self) -> NoSliceSequence[Diagnostic]:
+        class DiagnosticsItr:
+            def __init__(self, ccr: CodeCompletionResults):
                 self.ccr = ccr
 
-            def __len__(self):
+            def __len__(self) -> int:
                 return int(conf.lib.clang_codeCompleteGetNumDiagnostics(self.ccr))
 
-            def __getitem__(self, key):
+            def __getitem__(self, key: int) -> Diagnostic:
                 return conf.lib.clang_codeCompleteGetDiagnostic(self.ccr, key)
 
         return DiagnosticsItr(self)
@@ -2870,7 +3366,7 @@ class Index(ClangObject):
     """
 
     @staticmethod
-    def create(excludeDecls=False):
+    def create(excludeDecls: bool = False) -> Index:
         """
         Create a new Index.
         Parameters:
@@ -2878,14 +3374,20 @@ def create(excludeDecls=False):
         """
         return Index(conf.lib.clang_createIndex(excludeDecls, 0))
 
-    def __del__(self):
+    def __del__(self) -> None:
         conf.lib.clang_disposeIndex(self)
 
-    def read(self, path):
+    def read(self, path: StrPath) -> TranslationUnit:
         """Load a TranslationUnit from the given AST file."""
         return TranslationUnit.from_ast_file(path, self)
 
-    def parse(self, path, args=None, unsaved_files=None, options=0):
+    def parse(
+        self,
+        path: StrPath | None,
+        args: list[str] | None = None,
+        unsaved_files: list[InMemoryFile] | None = None,
+        options: int = 0,
+    ) -> TranslationUnit:
         """Load the translation unit from the given source code file by running
         clang and generating the AST before loading. Additional command line
         parameters can be passed to clang via the args parameter.
@@ -2942,8 +3444,13 @@ class TranslationUnit(ClangObject):
 
     @classmethod
     def from_source(
-        cls, filename, args=None, unsaved_files=None, options=0, index=None
-    ):
+        cls,
+        filename: StrPath | None,
+        args: list[str] | None = None,
+        unsaved_files: list[InMemoryFile] | None = None,
+        options: int = 0,
+        index: Index | None = None,
+    ) -> TranslationUnit:
         """Create a TranslationUnit by parsing source.
 
         This is capable of processing source code both from files on the
@@ -2953,8 +3460,8 @@ def from_source(
         a list via args. These can be used to specify include paths, warnings,
         etc. e.g. ["-Wall", "-I/path/to/include"].
 
-        In-memory file content can be provided via unsaved_files. This is an
-        iterable of 2-tuples. The first element is the filename (str or
+        In-memory file content can be provided via unsaved_files. This is a
+        list of 2-tuples. The first element is the filename (str or
         PathLike). The second element defines the content. Content can be
         provided as str source code or as file objects (anything with a read()
         method). If a file object is being used, content will be read until EOF
@@ -3002,14 +3509,14 @@ def from_source(
             for i, (name, contents) in enumerate(unsaved_files):
                 if hasattr(contents, "read"):
                     contents = contents.read()
-                contents = b(contents)
-                unsaved_array[i].name = b(fspath(name))
-                unsaved_array[i].contents = contents
-                unsaved_array[i].length = len(contents)
+                binary_contents = b(contents)
+                unsaved_array[i].name = b(os.fspath(name))
+                unsaved_array[i].contents = binary_contents
+                unsaved_array[i].length = len(binary_contents)
 
         ptr = conf.lib.clang_parseTranslationUnit(
             index,
-            fspath(filename) if filename is not None else None,
+            os.fspath(filename) if filename is not None else None,
             args_array,
             len(args),
             unsaved_array,
@@ -3023,7 +3530,9 @@ def from_source(
         return cls(ptr, index=index)
 
     @classmethod
-    def from_ast_file(cls, filename, index=None):
+    def from_ast_file(
+        cls, filename: StrPath, index: Index | None = None
+    ) -> TranslationUnit:
         """Create a TranslationUnit instance from a saved AST file.
 
         A previously-saved AST file (provided with -emit-ast or
@@ -3040,13 +3549,13 @@ def from_ast_file(cls, filename, index=None):
         if index is None:
             index = Index.create()
 
-        ptr = conf.lib.clang_createTranslationUnit(index, fspath(filename))
+        ptr = conf.lib.clang_createTranslationUnit(index, os.fspath(filename))
         if not ptr:
             raise TranslationUnitLoadError(filename)
 
         return cls(ptr=ptr, index=index)
 
-    def __init__(self, ptr, index):
+    def __init__(self, ptr: CObjP, index: Index) -> None:
         """Create a TranslationUnit instance.
 
         TranslationUnits should be created using one of the from_* @classmethod
@@ -3056,20 +3565,20 @@ def __init__(self, ptr, index):
         self.index = index
         ClangObject.__init__(self, ptr)
 
-    def __del__(self):
+    def __del__(self) -> None:
         conf.lib.clang_disposeTranslationUnit(self)
 
     @property
-    def cursor(self):
+    def cursor(self) -> Cursor:
         """Retrieve the cursor that represents the given translation unit."""
         return conf.lib.clang_getTranslationUnitCursor(self)
 
     @property
-    def spelling(self):
+    def spelling(self) -> str:
         """Get the original translation unit source file name."""
         return conf.lib.clang_getTranslationUnitSpelling(self)
 
-    def get_includes(self):
+    def get_includes(self) -> Iterable[FileInclusion]:
         """
         Return an iterable sequence of FileInclusion objects that describe the
         sequence of inclusions in a translation unit. The first object in
@@ -3078,25 +3587,32 @@ def get_includes(self):
         headers.
         """
 
-        def visitor(fobj, lptr, depth, includes):
+        def visitor(
+            fobj: CObjP,
+            lptr: _Pointer[SourceLocation],
+            depth: int,
+            includes: list[FileInclusion],
+        ) -> None:
             if depth > 0:
                 loc = lptr.contents
                 includes.append(FileInclusion(loc.file, File(fobj), loc, depth))
 
         # Automatically adapt CIndex/ctype pointers to python objects
-        includes = []
+        includes: list[FileInclusion] = []
         conf.lib.clang_getInclusions(
-            self, callbacks["translation_unit_includes"](visitor), includes
+            self, translation_unit_includes_callback(visitor), includes
         )
 
         return iter(includes)
 
-    def get_file(self, filename):
+    def get_file(self, filename: StrPath) -> File:
         """Obtain a File from this translation unit."""
 
         return File.from_name(self, filename)
 
-    def get_location(self, filename, position):
+    def get_location(
+        self, filename: StrPath, position: int | tuple[int, int]
+    ) -> SourceLocation:
         """Obtain a SourceLocation for a file in this translation unit.
 
         The position can be specified by passing:
@@ -3112,7 +3628,11 @@ def get_location(self, filename, position):
 
         return SourceLocation.from_position(self, f, position[0], position[1])
 
-    def get_extent(self, filename, locations):
+    def get_extent(
+        self,
+        filename: StrPath,
+        locations: Sequence[SourceLocation] | Sequence[int] | Sequence[Sequence[int]],
+    ) -> SourceRange:
         """Obtain a SourceRange from this translation unit.
 
         The bounds of the SourceRange must ultimately be defined by a start and
@@ -3135,6 +3655,7 @@ def get_extent(self, filename, locations):
         start_location, end_location = locations
 
         if hasattr(start_location, "__len__"):
+            start_location = Tcast(Sequence[int], start_location)
             start_location = SourceLocation.from_position(
                 self, f, start_location[0], start_location[1]
             )
@@ -3142,6 +3663,7 @@ def get_extent(self, filename, locations):
             start_location = SourceLocation.from_offset(self, f, start_location)
 
         if hasattr(end_location, "__len__"):
+            end_location = Tcast(Sequence[int], end_location)
             end_location = SourceLocation.from_position(
                 self, f, end_location[0], end_location[1]
             )
@@ -3154,19 +3676,19 @@ def get_extent(self, filename, locations):
         return SourceRange.from_locations(start_location, end_location)
 
     @property
-    def diagnostics(self):
+    def diagnostics(self) -> NoSliceSequence[Diagnostic]:
         """
         Return an iterable (and indexable) object containing the diagnostics.
         """
 
-        class DiagIterator(object):
-            def __init__(self, tu):
+        class DiagIterator:
+            def __init__(self, tu: TranslationUnit):
                 self.tu = tu
 
-            def __len__(self):
+            def __len__(self) -> int:
                 return int(conf.lib.clang_getNumDiagnostics(self.tu))
 
-            def __getitem__(self, key):
+            def __getitem__(self, key: int) -> Diagnostic:
                 diag = conf.lib.clang_getDiagnostic(self.tu, key)
                 if not diag:
                     raise IndexError
@@ -3174,7 +3696,9 @@ def __getitem__(self, key):
 
         return DiagIterator(self)
 
-    def reparse(self, unsaved_files=None, options=0):
+    def reparse(
+        self, unsaved_files: list[InMemoryFile] | None = None, options: int = 0
+    ) -> None:
         """
         Reparse an already parsed translation unit.
 
@@ -3186,21 +3710,21 @@ def reparse(self, unsaved_files=None, options=0):
         if unsaved_files is None:
             unsaved_files = []
 
-        unsaved_files_array = 0
+        unsaved_files_array: int | Array[_CXUnsavedFile] = 0
         if len(unsaved_files):
             unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))()
             for i, (name, contents) in enumerate(unsaved_files):
                 if hasattr(contents, "read"):
                     contents = contents.read()
-                contents = b(contents)
-                unsaved_files_array[i].name = b(fspath(name))
-                unsaved_files_array[i].contents = contents
-                unsaved_files_array[i].length = len(contents)
+                binary_contents = b(contents)
+                unsaved_files_array[i].name = b(os.fspath(name))
+                unsaved_files_array[i].contents = binary_contents
+                unsaved_files_array[i].length = len(binary_contents)
         ptr = conf.lib.clang_reparseTranslationUnit(
             self, len(unsaved_files), unsaved_files_array, options
         )
 
-    def save(self, filename):
+    def save(self, filename: StrPath) -> None:
         """Saves the TranslationUnit to a file.
 
         This is equivalent to passing -emit-ast to the clang frontend. The
@@ -3217,21 +3741,21 @@ def save(self, filename):
         """
         options = conf.lib.clang_defaultSaveOptions(self)
         result = int(
-            conf.lib.clang_saveTranslationUnit(self, fspath(filename), options)
+            conf.lib.clang_saveTranslationUnit(self, os.fspath(filename), options)
         )
         if result != 0:
             raise TranslationUnitSaveError(result, "Error saving TranslationUnit.")
 
     def codeComplete(
         self,
-        path,
-        line,
-        column,
-        unsaved_files=None,
-        include_macros=False,
-        include_code_patterns=False,
-        include_brief_comments=False,
-    ):
+        path: StrPath,
+        line: int,
+        column: int,
+        unsaved_files: list[InMemoryFile] | None = None,
+        include_macros: bool = False,
+        include_code_patterns: bool = False,
+        include_brief_comments: bool = False,
+    ) -> CodeCompletionResults | None:
         """
         Code complete in this translation unit.
 
@@ -3254,19 +3778,19 @@ def codeComplete(
         if unsaved_files is None:
             unsaved_files = []
 
-        unsaved_files_array = 0
+        unsaved_files_array: int | Array[_CXUnsavedFile] = 0
         if len(unsaved_files):
             unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))()
             for i, (name, contents) in enumerate(unsaved_files):
                 if hasattr(contents, "read"):
                     contents = contents.read()
-                contents = b(contents)
-                unsaved_files_array[i].name = b(fspath(name))
-                unsaved_files_array[i].contents = contents
-                unsaved_files_array[i].length = len(contents)
+                binary_contents = b(contents)
+                unsaved_files_array[i].name = b(os.fspath(name))
+                unsaved_files_array[i].contents = binary_contents
+                unsaved_files_array[i].length = len(binary_contents)
         ptr = conf.lib.clang_codeCompleteAt(
             self,
-            fspath(path),
+            os.fspath(path),
             line,
             column,
             unsaved_files_array,
@@ -3277,7 +3801,11 @@ def codeComplete(
             return CodeCompletionResults(ptr)
         return None
 
-    def get_tokens(self, locations=None, extent=None):
+    def get_tokens(
+        self,
+        locations: tuple[SourceLocation, SourceLocation] | None = None,
+        extent: SourceRange | None = None,
+    ) -> Iterable[Token]:
         """Obtain tokens in this translation unit.
 
         This is a generator for Token instances. The caller specifies a range
@@ -3287,6 +3815,8 @@ def get_tokens(self, locations=None, extent=None):
         """
         if locations is not None:
             extent = SourceRange(start=locations[0], end=locations[1])
+        if extent is None:
+            raise TypeError("get_tokens() requires at least one argument")
 
         return TokenGroup.get_tokens(self, extent)
 
@@ -3297,38 +3827,40 @@ class File(ClangObject):
     translation unit.
     """
 
+    _tu: TranslationUnit
+
     @staticmethod
-    def from_name(translation_unit, file_name):
+    def from_name(translation_unit: TranslationUnit, file_name: StrPath) -> File:
         """Retrieve a file handle within the given translation unit."""
-        return File(conf.lib.clang_getFile(translation_unit, fspath(file_name)))
+        return File(conf.lib.clang_getFile(translation_unit, os.fspath(file_name)))
 
     @property
-    def name(self):
+    def name(self) -> str:
         """Return the complete file and path name of the file."""
         return conf.lib.clang_getFileName(self)
 
     @property
-    def time(self):
+    def time(self) -> int:
         """Return the last modification time of the file."""
         return conf.lib.clang_getFileTime(self)
 
-    def __str__(self):
+    def __str__(self) -> str:
         return self.name
 
-    def __repr__(self):
+    def __repr__(self) -> str:
         return "<File: %s>" % (self.name)
 
     @staticmethod
-    def from_result(res, fn, args):
+    def from_result(res: CObjP, fn: Any, args: Sequence[Cursor]) -> File:
         assert isinstance(res, c_object_p)
-        res = File(res)
+        ret = File(res)
 
         # Copy a reference to the TranslationUnit to prevent premature GC.
-        res._tu = args[0]._tu
-        return res
+        ret._tu = args[0]._tu
+        return ret
 
 
-class FileInclusion(object):
+class FileInclusion:
     """
     The FileInclusion class represents the inclusion of one source file by
     another via a '#include' directive or as the input file for the translation
@@ -3337,14 +3869,14 @@ class FileInclusion(object):
     file in the stack. Note that the input file has depth 0.
     """
 
-    def __init__(self, src, tgt, loc, depth):
+    def __init__(self, src: File | None, tgt: File, loc: SourceLocation, depth: int):
         self.source = src
         self.include = tgt
         self.location = loc
         self.depth = depth
 
     @property
-    def is_input_file(self):
+    def is_input_file(self) -> bool:
         """True if the included file is the input file."""
         return self.depth == 0
 
@@ -3363,7 +3895,7 @@ class CompilationDatabaseError(Exception):
     # The database could not be loaded
     ERROR_CANNOTLOADDATABASE = 1
 
-    def __init__(self, enumeration, message):
+    def __init__(self, enumeration: int, message: str):
         assert isinstance(enumeration, int)
 
         if enumeration > 1:
@@ -3377,30 +3909,30 @@ def __init__(self, enumeration, message):
         Exception.__init__(self, "Error %d: %s" % (enumeration, message))
 
 
-class CompileCommand(object):
+class CompileCommand:
     """Represents the compile command used to build a file"""
 
-    def __init__(self, cmd, ccmds):
+    def __init__(self, cmd: CObjP, ccmds: CompileCommands):
         self.cmd = cmd
         # Keep a reference to the originating CompileCommands
         # to prevent garbage collection
         self.ccmds = ccmds
 
     @property
-    def directory(self):
+    def directory(self) -> str:
         """Get the working directory for this CompileCommand"""
         return conf.lib.clang_CompileCommand_getDirectory(self.cmd)
 
     @property
-    def filename(self):
+    def filename(self) -> str:
         """Get the working filename for this CompileCommand"""
         return conf.lib.clang_CompileCommand_getFilename(self.cmd)
 
     @property
-    def arguments(self):
+    def arguments(self) -> Iterator[str]:
         """
         Get an iterable object providing each argument in the
-        command line for the compiler invocation as a _CXString.
+        command line for the compiler invocation as a string.
 
         Invariant : the first argument is the compiler executable
         """
@@ -3409,29 +3941,29 @@ def arguments(self):
             yield conf.lib.clang_CompileCommand_getArg(self.cmd, i)
 
 
-class CompileCommands(object):
+class CompileCommands:
     """
     CompileCommands is an iterable object containing all CompileCommand
     that can be used for building a specific file.
     """
 
-    def __init__(self, ccmds):
+    def __init__(self, ccmds: CObjP):
         self.ccmds = ccmds
 
-    def __del__(self):
+    def __del__(self) -> None:
         conf.lib.clang_CompileCommands_dispose(self.ccmds)
 
-    def __len__(self):
+    def __len__(self) -> int:
         return int(conf.lib.clang_CompileCommands_getSize(self.ccmds))
 
-    def __getitem__(self, i):
+    def __getitem__(self, i: int) -> CompileCommand:
         cc = conf.lib.clang_CompileCommands_getCommand(self.ccmds, i)
         if not cc:
             raise IndexError
         return CompileCommand(cc, self)
 
     @staticmethod
-    def from_result(res, fn, args):
+    def from_result(res: CObjP, fn: Any, args: Any) -> CompileCommands | None:
         if not res:
             return None
         return CompileCommands(res)
@@ -3445,22 +3977,22 @@ class CompilationDatabase(ClangObject):
     It enables querying how a specific source file can be built.
     """
 
-    def __del__(self):
+    def __del__(self) -> None:
         conf.lib.clang_CompilationDatabase_dispose(self)
 
     @staticmethod
-    def from_result(res, fn, args):
+    def from_result(res: CObjP, fn: Any, args: Any) -> CompilationDatabase:
         if not res:
             raise CompilationDatabaseError(0, "CompilationDatabase loading failed")
         return CompilationDatabase(res)
 
     @staticmethod
-    def fromDirectory(buildDir):
+    def fromDirectory(buildDir: StrPath) -> CompilationDatabase:
         """Builds a CompilationDatabase from the database found in buildDir"""
         errorCode = c_uint()
         try:
             cdb = conf.lib.clang_CompilationDatabase_fromDirectory(
-                fspath(buildDir), byref(errorCode)
+                os.fspath(buildDir), byref(errorCode)
             )
         except CompilationDatabaseError as e:
             raise CompilationDatabaseError(
@@ -3468,16 +4000,16 @@ def fromDirectory(buildDir):
             )
         return cdb
 
-    def getCompileCommands(self, filename):
+    def getCompileCommands(self, filename: StrPath) -> CompileCommands:
         """
         Get an iterable object providing all the CompileCommands available to
         build filename. Returns None if filename is not found in the database.
         """
         return conf.lib.clang_CompilationDatabase_getCompileCommands(
-            self, fspath(filename)
+            self, os.fspath(filename)
         )
 
-    def getAllCompileCommands(self):
+    def getAllCompileCommands(self) -> CompileCommands:
         """
         Get an iterable object providing all the CompileCommands available from
         the database.
@@ -3498,7 +4030,7 @@ class Token(Structure):
     _fields_ = [("int_data", c_uint * 4), ("ptr_data", c_void_p)]
 
     @property
-    def spelling(self):
+    def spelling(self) -> str:
         """The spelling of this token.
 
         This is the textual representation of the token in source.
@@ -3506,22 +4038,22 @@ def spelling(self):
         return conf.lib.clang_getTokenSpelling(self._tu, self)
 
     @property
-    def kind(self):
+    def kind(self) -> TokenKind:
         """Obtain the TokenKind of the current token."""
         return TokenKind.from_value(conf.lib.clang_getTokenKind(self))
 
     @property
-    def location(self):
+    def location(self) -> SourceLocation:
         """The SourceLocation this Token occurs at."""
         return conf.lib.clang_getTokenLocation(self._tu, self)
 
     @property
-    def extent(self):
+    def extent(self) -> SourceRange:
         """The SourceRange this Token occupies."""
         return conf.lib.clang_getTokenExtent(self._tu, self)
 
     @property
-    def cursor(self):
+    def cursor(self) -> Cursor:
         """The Cursor this Token corresponds to."""
         cursor = Cursor()
         cursor._tu = self._tu
@@ -3533,15 +4065,15 @@ def cursor(self):
 
 # Now comes the plumbing to hook up the C library.
 
-# Register callback types in common container.
-callbacks["translation_unit_includes"] = CFUNCTYPE(
+# Register callback types
+translation_unit_includes_callback = CFUNCTYPE(
     None, c_object_p, POINTER(SourceLocation), c_uint, py_object
 )
-callbacks["cursor_visit"] = CFUNCTYPE(c_int, Cursor, Cursor, py_object)
-callbacks["fields_visit"] = CFUNCTYPE(c_int, Cursor, py_object)
+cursor_visit_callback = CFUNCTYPE(c_int, Cursor, Cursor, py_object)
+fields_visit_callback = CFUNCTYPE(c_int, Cursor, py_object)
 
 # Functions strictly alphabetical order.
-functionList = [
+functionList: list[LibFunc] = [
     (
         "clang_annotateTokens",
         [TranslationUnit, POINTER(Token), c_uint, POINTER(Cursor)],
@@ -3704,7 +4236,7 @@ def cursor(self):
     ("clang_getIncludedFile", [Cursor], c_object_p, File.from_result),
     (
         "clang_getInclusions",
-        [TranslationUnit, callbacks["translation_unit_includes"], py_object],
+        [TranslationUnit, translation_unit_includes_callback, py_object],
     ),
     (
         "clang_getInstantiationLocation",
@@ -3789,7 +4321,7 @@ def cursor(self):
         "clang_tokenize",
         [TranslationUnit, SourceRange, POINTER(POINTER(Token)), POINTER(c_uint)],
     ),
-    ("clang_visitChildren", [Cursor, callbacks["cursor_visit"], py_object], c_uint),
+    ("clang_visitChildren", [Cursor, cursor_visit_callback, py_object], c_uint),
     ("clang_Cursor_getNumArguments", [Cursor], c_int),
     ("clang_Cursor_getArgument", [Cursor, c_uint], Cursor, Cursor.from_result),
     ("clang_Cursor_getNumTemplateArguments", [Cursor], c_int),
@@ -3815,19 +4347,19 @@ def cursor(self):
     ("clang_Type_getSizeOf", [Type], c_longlong),
     ("clang_Type_getCXXRefQualifier", [Type], c_uint),
     ("clang_Type_getNamedType", [Type], Type, Type.from_result),
-    ("clang_Type_visitFields", [Type, callbacks["fields_visit"], py_object], c_uint),
+    ("clang_Type_visitFields", [Type, fields_visit_callback, py_object], c_uint),
 ]
 
 
 class LibclangError(Exception):
-    def __init__(self, message):
+    def __init__(self, message: str):
         self.m = message
 
-    def __str__(self):
+    def __str__(self) -> str:
         return self.m
 
 
-def register_function(lib, item, ignore_errors):
+def register_function(lib: ClangLib, item: LibFunc, ignore_errors: bool) -> None:
     # A function may not exist, if these bindings are used with an older or
     # incompatible version of libclang.so.
     try:
@@ -3851,28 +4383,28 @@ def register_function(lib, item, ignore_errors):
         func.errcheck = item[3]
 
 
-def register_functions(lib, ignore_errors):
+def register_functions(lib: ClangLib, ignore_errors: bool) -> None:
     """Register function prototypes with a libclang library instance.
 
     This must be called as part of library instantiation so Python knows how
     to call out to the shared library.
     """
 
-    def register(item):
-        return register_function(lib, item, ignore_errors)
+    def register(item: LibFunc) -> None:
+        register_function(lib, item, ignore_errors)
 
     for f in functionList:
         register(f)
 
 
-class Config(object):
+class Config:
     library_path = None
-    library_file = None
+    library_file: str | None = None
     compatibility_check = True
     loaded = False
 
     @staticmethod
-    def set_library_path(path):
+    def set_library_path(path: StrPath) -> None:
         """Set the path in which to search for libclang"""
         if Config.loaded:
             raise Exception(
@@ -3880,10 +4412,10 @@ def set_library_path(path):
                 "any other functionalities in libclang."
             )
 
-        Config.library_path = fspath(path)
+        Config.library_path = os.fspath(path)
 
     @staticmethod
-    def set_library_file(filename):
+    def set_library_file(filename: StrPath) -> None:
         """Set the exact location of libclang"""
         if Config.loaded:
             raise Exception(
@@ -3891,10 +4423,10 @@ def set_library_file(filename):
                 "any other functionalities in libclang."
             )
 
-        Config.library_file = fspath(filename)
+        Config.library_file = os.fspath(filename)
 
     @staticmethod
-    def set_compatibility_check(check_status):
+    def set_compatibility_check(check_status: bool) -> None:
         """Perform compatibility check when loading libclang
 
         The python bindings are only tested and evaluated with the version of
@@ -3920,13 +4452,13 @@ def set_compatibility_check(check_status):
         Config.compatibility_check = check_status
 
     @CachedProperty
-    def lib(self):
+    def lib(self) -> ClangLib:
         lib = self.get_cindex_library()
         register_functions(lib, not Config.compatibility_check)
         Config.loaded = True
         return lib
 
-    def get_filename(self):
+    def get_filename(self) -> str:
         if Config.library_file:
             return Config.library_file
 
@@ -3946,7 +4478,7 @@ def get_filename(self):
 
         return file
 
-    def get_cindex_library(self):
+    def get_cindex_library(self) -> ClangLib:
         try:
             library = cdll.LoadLibrary(self.get_filename())
         except OSError as e:
@@ -3959,7 +4491,7 @@ def get_cindex_library(self):
 
         return library
 
-    def function_exists(self, name):
+    def function_exists(self, name: str) -> bool:
         try:
             getattr(self.lib, name)
         except AttributeError:
@@ -3968,7 +4500,7 @@ def function_exists(self, name):
         return True
 
 
-def register_enumerations():
+def register_enumerations() -> None:
     for name, value in clang.enumerations.TokenKinds:
         TokenKind.register(value, name)
 



More information about the cfe-commits mailing list