91
91
cast as Tcast ,
92
92
Generic ,
93
93
Iterator ,
94
+ Literal ,
94
95
Optional ,
95
96
Sequence ,
96
97
Type as TType ,
@@ -1982,7 +1983,7 @@ def type(self) -> Type:
1982
1983
Retrieve the Type (if any) of the entity pointed at by the cursor.
1983
1984
"""
1984
1985
if not hasattr (self , "_type" ):
1985
- self ._type = Type .from_result (conf .lib .clang_getCursorType (self ), ( self ,) )
1986
+ self ._type = Type .from_result (conf .lib .clang_getCursorType (self ), self )
1986
1987
1987
1988
return self ._type
1988
1989
@@ -2009,7 +2010,7 @@ def result_type(self) -> Type:
2009
2010
"""Retrieve the Type of the result for this Cursor."""
2010
2011
if not hasattr (self , "_result_type" ):
2011
2012
self ._result_type = Type .from_result (
2012
- conf .lib .clang_getCursorResultType (self ), ( self ,)
2013
+ conf .lib .clang_getCursorResultType (self ), self
2013
2014
)
2014
2015
2015
2016
return self ._result_type
@@ -2040,7 +2041,7 @@ def underlying_typedef_type(self) -> Type:
2040
2041
if not hasattr (self , "_underlying_type" ):
2041
2042
assert self .kind .is_declaration ()
2042
2043
self ._underlying_type = Type .from_result (
2043
- conf .lib .clang_getTypedefDeclUnderlyingType (self ), ( self ,)
2044
+ conf .lib .clang_getTypedefDeclUnderlyingType (self ), self
2044
2045
)
2045
2046
2046
2047
return self ._underlying_type
@@ -2056,7 +2057,7 @@ def enum_type(self) -> Type:
2056
2057
if not hasattr (self , "_enum_type" ):
2057
2058
assert self .kind == CursorKind .ENUM_DECL
2058
2059
self ._enum_type = Type .from_result (
2059
- conf .lib .clang_getEnumDeclIntegerType (self ), ( self ,)
2060
+ conf .lib .clang_getEnumDeclIntegerType (self ), self
2060
2061
)
2061
2062
2062
2063
return self ._enum_type
@@ -2197,7 +2198,7 @@ def get_template_argument_kind(self, num: int) -> TemplateArgumentKind:
2197
2198
def get_template_argument_type (self , num : int ) -> Type :
2198
2199
"""Returns the CXType for the indicated template argument."""
2199
2200
return Type .from_result (
2200
- conf .lib .clang_Cursor_getTemplateArgumentType (self , num ), ( self , num )
2201
+ conf .lib .clang_Cursor_getTemplateArgumentType (self , num ), self
2201
2202
)
2202
2203
2203
2204
@cursor_null_guard
@@ -2597,8 +2598,10 @@ class Type(Structure):
2597
2598
2598
2599
_fields_ = [("_kind_id" , c_int ), ("data" , c_void_p * 2 )]
2599
2600
2601
+ _tu : TranslationUnit
2602
+
2600
2603
@property
2601
- def kind (self ):
2604
+ def kind (self ) -> TypeKind :
2602
2605
"""Return the kind of this type."""
2603
2606
return TypeKind .from_id (self ._kind_id )
2604
2607
@@ -2635,7 +2638,7 @@ def __getitem__(self, key: int) -> Type:
2635
2638
)
2636
2639
2637
2640
result = Type .from_result (
2638
- conf .lib .clang_getArgType (self .parent , key ), ( self .parent , key )
2641
+ conf .lib .clang_getArgType (self .parent , key ), self .parent
2639
2642
)
2640
2643
if result .kind == TypeKind .INVALID :
2641
2644
raise IndexError ("Argument could not be retrieved." )
@@ -2646,63 +2649,56 @@ def __getitem__(self, key: int) -> Type:
2646
2649
return ArgumentsIterator (self )
2647
2650
2648
2651
@property
2649
- def element_type (self ):
2652
+ def element_type (self ) -> Type :
2650
2653
"""Retrieve the Type of elements within this Type.
2651
2654
2652
2655
If accessed on a type that is not an array, complex, or vector type, an
2653
2656
exception will be raised.
2654
2657
"""
2655
- result = Type .from_result (conf .lib .clang_getElementType (self ), ( self ,) )
2658
+ result = Type .from_result (conf .lib .clang_getElementType (self ), self )
2656
2659
if result .kind == TypeKind .INVALID :
2657
2660
raise Exception ("Element type not available on this type." )
2658
2661
2659
2662
return result
2660
2663
2661
2664
@property
2662
- def element_count (self ):
2665
+ def element_count (self ) -> int :
2663
2666
"""Retrieve the number of elements in this type.
2664
2667
2665
2668
Returns an int.
2666
2669
2667
2670
If the Type is not an array or vector, this raises.
2668
2671
"""
2669
- result = conf .lib .clang_getNumElements (self )
2672
+ result : int = conf .lib .clang_getNumElements (self )
2670
2673
if result < 0 :
2671
2674
raise Exception ("Type does not have elements." )
2672
2675
2673
2676
return result
2674
2677
2675
2678
@property
2676
- def translation_unit (self ):
2679
+ def translation_unit (self ) -> TranslationUnit :
2677
2680
"""The TranslationUnit to which this Type is associated."""
2678
2681
# If this triggers an AttributeError, the instance was not properly
2679
2682
# instantiated.
2680
2683
return self ._tu
2681
2684
2682
2685
@staticmethod
2683
- def from_result (res , args ) :
2686
+ def from_result (res : Type , arg : Cursor | Type ) -> Type :
2684
2687
assert isinstance (res , Type )
2685
-
2686
- tu = None
2687
- for arg in args :
2688
- if hasattr (arg , "translation_unit" ):
2689
- tu = arg .translation_unit
2690
- break
2691
-
2692
- assert tu is not None
2693
- res ._tu = tu
2688
+ assert arg .translation_unit is not None
2689
+ res ._tu = arg .translation_unit
2694
2690
2695
2691
return res
2696
2692
2697
- def get_num_template_arguments (self ):
2693
+ def get_num_template_arguments (self ) -> int :
2698
2694
return conf .lib .clang_Type_getNumTemplateArguments (self ) # type: ignore [no-any-return]
2699
2695
2700
- def get_template_argument_type (self , num ) :
2696
+ def get_template_argument_type (self , num : int ) -> Type :
2701
2697
return Type .from_result (
2702
- conf .lib .clang_Type_getTemplateArgumentAsType (self , num ), ( self , num )
2698
+ conf .lib .clang_Type_getTemplateArgumentAsType (self , num ), self
2703
2699
)
2704
2700
2705
- def get_canonical (self ):
2701
+ def get_canonical (self ) -> Type :
2706
2702
"""
2707
2703
Return the canonical type for a Type.
2708
2704
@@ -2712,9 +2708,11 @@ def get_canonical(self):
2712
2708
example, if 'T' is a typedef for 'int', the canonical type for
2713
2709
'T' would be 'int'.
2714
2710
"""
2715
- return Type .from_result (conf .lib .clang_getCanonicalType (self ), ( self ,) )
2711
+ return Type .from_result (conf .lib .clang_getCanonicalType (self ), self )
2716
2712
2717
- def get_fully_qualified_name (self , policy , with_global_ns_prefix = False ):
2713
+ def get_fully_qualified_name (
2714
+ self , policy : PrintingPolicy , with_global_ns_prefix : bool = False
2715
+ ) -> str :
2718
2716
"""
2719
2717
Get the fully qualified name for a type.
2720
2718
@@ -2727,118 +2725,118 @@ def get_fully_qualified_name(self, policy, with_global_ns_prefix=False):
2727
2725
conf .lib .clang_getFullyQualifiedName (self , policy , with_global_ns_prefix )
2728
2726
)
2729
2727
2730
- def is_const_qualified (self ):
2728
+ def is_const_qualified (self ) -> bool :
2731
2729
"""Determine whether a Type has the "const" qualifier set.
2732
2730
2733
2731
This does not look through typedefs that may have added "const"
2734
2732
at a different level.
2735
2733
"""
2736
2734
return conf .lib .clang_isConstQualifiedType (self ) # type: ignore [no-any-return]
2737
2735
2738
- def is_volatile_qualified (self ):
2736
+ def is_volatile_qualified (self ) -> bool :
2739
2737
"""Determine whether a Type has the "volatile" qualifier set.
2740
2738
2741
2739
This does not look through typedefs that may have added "volatile"
2742
2740
at a different level.
2743
2741
"""
2744
2742
return conf .lib .clang_isVolatileQualifiedType (self ) # type: ignore [no-any-return]
2745
2743
2746
- def is_restrict_qualified (self ):
2744
+ def is_restrict_qualified (self ) -> bool :
2747
2745
"""Determine whether a Type has the "restrict" qualifier set.
2748
2746
2749
2747
This does not look through typedefs that may have added "restrict" at
2750
2748
a different level.
2751
2749
"""
2752
2750
return conf .lib .clang_isRestrictQualifiedType (self ) # type: ignore [no-any-return]
2753
2751
2754
- def is_function_variadic (self ):
2752
+ def is_function_variadic (self ) -> bool :
2755
2753
"""Determine whether this function Type is a variadic function type."""
2756
2754
assert self .kind == TypeKind .FUNCTIONPROTO
2757
2755
2758
2756
return conf .lib .clang_isFunctionTypeVariadic (self ) # type: ignore [no-any-return]
2759
2757
2760
- def get_address_space (self ):
2758
+ def get_address_space (self ) -> int :
2761
2759
return conf .lib .clang_getAddressSpace (self ) # type: ignore [no-any-return]
2762
2760
2763
- def get_typedef_name (self ):
2761
+ def get_typedef_name (self ) -> str :
2764
2762
return _CXString .from_result (conf .lib .clang_getTypedefName (self ))
2765
2763
2766
- def is_pod (self ):
2764
+ def is_pod (self ) -> bool :
2767
2765
"""Determine whether this Type represents plain old data (POD)."""
2768
2766
return conf .lib .clang_isPODType (self ) # type: ignore [no-any-return]
2769
2767
2770
- def get_pointee (self ):
2768
+ def get_pointee (self ) -> Type :
2771
2769
"""
2772
2770
For pointer types, returns the type of the pointee.
2773
2771
"""
2774
- return Type .from_result (conf .lib .clang_getPointeeType (self ), ( self ,) )
2772
+ return Type .from_result (conf .lib .clang_getPointeeType (self ), self )
2775
2773
2776
- def get_declaration (self ):
2774
+ def get_declaration (self ) -> Cursor :
2777
2775
"""
2778
2776
Return the cursor for the declaration of the given type.
2779
2777
"""
2780
2778
return Cursor .from_non_null_cursor_result (
2781
2779
conf .lib .clang_getTypeDeclaration (self ), self
2782
2780
)
2783
2781
2784
- def get_result (self ):
2782
+ def get_result (self ) -> Type :
2785
2783
"""
2786
2784
Retrieve the result type associated with a function type.
2787
2785
"""
2788
- return Type .from_result (conf .lib .clang_getResultType (self ), ( self ,) )
2786
+ return Type .from_result (conf .lib .clang_getResultType (self ), self )
2789
2787
2790
- def get_array_element_type (self ):
2788
+ def get_array_element_type (self ) -> Type :
2791
2789
"""
2792
2790
Retrieve the type of the elements of the array type.
2793
2791
"""
2794
- return Type .from_result (conf .lib .clang_getArrayElementType (self ), ( self ,) )
2792
+ return Type .from_result (conf .lib .clang_getArrayElementType (self ), self )
2795
2793
2796
- def get_array_size (self ):
2794
+ def get_array_size (self ) -> int :
2797
2795
"""
2798
2796
Retrieve the size of the constant array.
2799
2797
"""
2800
2798
return conf .lib .clang_getArraySize (self ) # type: ignore [no-any-return]
2801
2799
2802
- def get_class_type (self ):
2800
+ def get_class_type (self ) -> Type :
2803
2801
"""
2804
2802
Retrieve the class type of the member pointer type.
2805
2803
"""
2806
- return Type .from_result (conf .lib .clang_Type_getClassType (self ), ( self ,) )
2804
+ return Type .from_result (conf .lib .clang_Type_getClassType (self ), self )
2807
2805
2808
- def get_named_type (self ):
2806
+ def get_named_type (self ) -> Type :
2809
2807
"""
2810
2808
Retrieve the type named by the qualified-id.
2811
2809
"""
2812
- return Type .from_result (conf .lib .clang_Type_getNamedType (self ), ( self ,) )
2810
+ return Type .from_result (conf .lib .clang_Type_getNamedType (self ), self )
2813
2811
2814
- def get_align (self ):
2812
+ def get_align (self ) -> int :
2815
2813
"""
2816
2814
Retrieve the alignment of the record.
2817
2815
"""
2818
2816
return conf .lib .clang_Type_getAlignOf (self ) # type: ignore [no-any-return]
2819
2817
2820
- def get_size (self ):
2818
+ def get_size (self ) -> int :
2821
2819
"""
2822
2820
Retrieve the size of the record.
2823
2821
"""
2824
2822
return conf .lib .clang_Type_getSizeOf (self ) # type: ignore [no-any-return]
2825
2823
2826
- def get_offset (self , fieldname ) :
2824
+ def get_offset (self , fieldname : str ) -> int :
2827
2825
"""
2828
2826
Retrieve the offset of a field in the record.
2829
2827
"""
2830
2828
return conf .lib .clang_Type_getOffsetOf (self , fieldname ) # type: ignore [no-any-return]
2831
2829
2832
- def get_ref_qualifier (self ):
2830
+ def get_ref_qualifier (self ) -> RefQualifierKind :
2833
2831
"""
2834
2832
Retrieve the ref-qualifier of the type.
2835
2833
"""
2836
2834
return RefQualifierKind .from_id (conf .lib .clang_Type_getCXXRefQualifier (self ))
2837
2835
2838
- def get_fields (self ):
2836
+ def get_fields (self ) -> Iterator [ Cursor ] :
2839
2837
"""Return an iterator for accessing the fields of this type."""
2840
2838
2841
- def visitor (field , children ) :
2839
+ def visitor (field : Cursor , _ : Any ) -> Literal [ 1 ] :
2842
2840
assert not field .is_null ()
2843
2841
2844
2842
# Create reference to TU so it isn't GC'd before Cursor.
@@ -2850,10 +2848,10 @@ def visitor(field, children):
2850
2848
conf .lib .clang_Type_visitFields (self , fields_visit_callback (visitor ), fields )
2851
2849
return iter (fields )
2852
2850
2853
- def get_bases (self ):
2851
+ def get_bases (self ) -> Iterator [ Cursor ] :
2854
2852
"""Return an iterator for accessing the base classes of this type."""
2855
2853
2856
- def visitor (base , children ) :
2854
+ def visitor (base : Cursor , _ : Any ) -> Literal [ 1 ] :
2857
2855
assert not base .is_null ()
2858
2856
2859
2857
# Create reference to TU so it isn't GC'd before Cursor.
@@ -2865,10 +2863,10 @@ def visitor(base, children):
2865
2863
conf .lib .clang_visitCXXBaseClasses (self , fields_visit_callback (visitor ), bases )
2866
2864
return iter (bases )
2867
2865
2868
- def get_methods (self ):
2866
+ def get_methods (self ) -> Iterator [ Cursor ] :
2869
2867
"""Return an iterator for accessing the methods of this type."""
2870
2868
2871
- def visitor (method , children ) :
2869
+ def visitor (method : Cursor , _ : Any ) -> Literal [ 1 ] :
2872
2870
assert not method .is_null ()
2873
2871
2874
2872
# Create reference to TU so it isn't GC'd before Cursor.
@@ -2880,7 +2878,7 @@ def visitor(method, children):
2880
2878
conf .lib .clang_visitCXXMethods (self , fields_visit_callback (visitor ), methods )
2881
2879
return iter (methods )
2882
2880
2883
- def get_exception_specification_kind (self ):
2881
+ def get_exception_specification_kind (self ) -> ExceptionSpecificationKind :
2884
2882
"""
2885
2883
Return the kind of the exception specification; a value from
2886
2884
the ExceptionSpecificationKind enumeration.
@@ -2890,21 +2888,18 @@ def get_exception_specification_kind(self):
2890
2888
)
2891
2889
2892
2890
@property
2893
- def spelling (self ):
2891
+ def spelling (self ) -> str :
2894
2892
"""Retrieve the spelling of this Type."""
2895
2893
return _CXString .from_result (conf .lib .clang_getTypeSpelling (self ))
2896
2894
2897
- def pretty_printed (self , policy ) :
2895
+ def pretty_printed (self , policy : PrintingPolicy ) -> str :
2898
2896
"""Pretty-prints this Type with the given PrintingPolicy"""
2899
2897
return _CXString .from_result (conf .lib .clang_getTypePrettyPrinted (self , policy ))
2900
2898
2901
- def __eq__ (self , other ):
2902
- if not isinstance (other , Type ):
2903
- return False
2904
-
2905
- return conf .lib .clang_equalTypes (self , other ) # type: ignore [no-any-return]
2899
+ def __eq__ (self , other : object ) -> bool :
2900
+ return isinstance (other , Type ) and conf .lib .clang_equalTypes (self , other )
2906
2901
2907
- def __ne__ (self , other ) :
2902
+ def __ne__ (self , other : object ) -> bool :
2908
2903
return not self .__eq__ (other )
2909
2904
2910
2905
0 commit comments