| 1 | package AutoXS::Accessor; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | require Exporter; | ||
| 8 | |||
| 9 | our $VERSION = '0.03'; | ||
| 10 | |||
| 11 | BEGIN { require AutoXS; } | ||
| 12 | use base 'AutoXS'; | ||
| 13 | |||
| 14 | use B qw( svref_2object ); | ||
| 15 | use B::Utils qw( opgrep op_or ); | ||
| 16 | use Class::XSAccessor; | ||
| 17 | |||
| 18 | CHECK { | ||
| 19 | warn "Running AutoXS scanner of " . __PACKAGE__ if $AutoXS::Debug; | ||
| 20 | __PACKAGE__->scan_package_accessor($_) for keys %{$AutoXS::ScanClasses{"".__PACKAGE__}}; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub scan_package_accessor { | ||
| 24 | my $selfclass = shift; | ||
| 25 | my $edit_pkg = shift; | ||
| 26 | warn "Scanning package '$edit_pkg'" if $AutoXS::Debug; | ||
| 27 | |||
| 28 | my $sym = $selfclass->get_symbol($edit_pkg); | ||
| 29 | |||
| 30 | my @to_be_replaced; | ||
| 31 | |||
| 32 | foreach my $function (sort keys %$sym) { | ||
| 33 | next if $function =~ /^BEGIN|END|CHECK|UNITCHECK|INIT|import$/; | ||
| 34 | warn "Scanning function '${edit_pkg}::$function'" if $AutoXS::Debug; | ||
| 35 | |||
| 36 | local *symbol = $sym->{$function}; | ||
| 37 | my $coderef = *symbol{CODE} or next; | ||
| 38 | |||
| 39 | my $codeobj = svref_2object($coderef); | ||
| 40 | next unless ref $codeobj eq 'B::CV'; | ||
| 41 | if ($codeobj->XSUB) { | ||
| 42 | #print *symbol, " is XS\n"; | ||
| 43 | } | ||
| 44 | else { | ||
| 45 | my $r = $codeobj->ROOT; | ||
| 46 | my $glob_array_dereference = { | ||
| 47 | name => 'rv2av', | ||
| 48 | first => { name => 'gv', }, | ||
| 49 | }; | ||
| 50 | |||
| 51 | my $array_hash_access_or_shift = { | ||
| 52 | name => 'helem', | ||
| 53 | first => { | ||
| 54 | name => 'rv2hv', | ||
| 55 | first => op_or( | ||
| 56 | { name => 'aelem', | ||
| 57 | first => $glob_array_dereference, | ||
| 58 | 'last' => { name => 'const', }, | ||
| 59 | }, | ||
| 60 | { name => 'shift', | ||
| 61 | first => $glob_array_dereference, | ||
| 62 | }, | ||
| 63 | ), | ||
| 64 | }, | ||
| 65 | 'last' => { name => 'const', }, | ||
| 66 | }; | ||
| 67 | my $simple_structure = { | ||
| 68 | name => 'lineseq', | ||
| 69 | kids => [ | ||
| 70 | { name => 'nextstate', }, | ||
| 71 | op_or( | ||
| 72 | { name => 'return', | ||
| 73 | first => {name => 'pushmark'}, | ||
| 74 | 'last' => $array_hash_access_or_shift, | ||
| 75 | }, | ||
| 76 | $array_hash_access_or_shift, | ||
| 77 | ), | ||
| 78 | ], | ||
| 79 | }; | ||
| 80 | my $hash_access_pad = { | ||
| 81 | name => 'helem', | ||
| 82 | first => { | ||
| 83 | name => 'rv2hv', | ||
| 84 | first => { name => 'padsv' }, | ||
| 85 | }, | ||
| 86 | last => { | ||
| 87 | name => 'const', | ||
| 88 | capture => 'hash_key', | ||
| 89 | }, | ||
| 90 | }; | ||
| 91 | |||
| 92 | my $self_shift_structure = { | ||
| 93 | name => 'lineseq', | ||
| 94 | kids => [ | ||
| 95 | { name => 'nextstate', }, | ||
| 96 | { name => 'sassign', # optionally match my $self = shift and friends | ||
| 97 | first => { | ||
| 98 | name => 'shift', | ||
| 99 | first => $glob_array_dereference, | ||
| 100 | }, | ||
| 101 | }, | ||
| 102 | { name => 'nextstate', }, | ||
| 103 | op_or( | ||
| 104 | { name => 'return', | ||
| 105 | first => {name => 'pushmark'}, | ||
| 106 | 'last' => $hash_access_pad, | ||
| 107 | }, | ||
| 108 | $hash_access_pad, | ||
| 109 | ), | ||
| 110 | ], | ||
| 111 | }; | ||
| 112 | |||
| 113 | my $self_array_assign_structure = { | ||
| 114 | name => 'lineseq', | ||
| 115 | kids => [ | ||
| 116 | { name => 'nextstate', }, | ||
| 117 | { name => 'aassign', | ||
| 118 | kids => [ | ||
| 119 | { name => 'null', | ||
| 120 | first => { | ||
| 121 | name => 'pushmark', | ||
| 122 | sibling => op_or( | ||
| 123 | $glob_array_dereference, | ||
| 124 | { name => 'shift', | ||
| 125 | first => $glob_array_dereference, | ||
| 126 | }, | ||
| 127 | ), | ||
| 128 | }, | ||
| 129 | }, | ||
| 130 | { name => 'null', | ||
| 131 | first => { | ||
| 132 | name => 'pushmark', | ||
| 133 | sibling => {name => 'padsv'} | ||
| 134 | }, | ||
| 135 | }, | ||
| 136 | ], | ||
| 137 | }, | ||
| 138 | { name => 'nextstate', }, | ||
| 139 | op_or( | ||
| 140 | { name => 'return', | ||
| 141 | first => {name => 'pushmark'}, | ||
| 142 | 'last' => $hash_access_pad, | ||
| 143 | }, | ||
| 144 | $hash_access_pad, | ||
| 145 | ), | ||
| 146 | ], | ||
| 147 | }; | ||
| 148 | B::Utils::walkoptree_filtered( | ||
| 149 | $r, | ||
| 150 | sub { opgrep( { | ||
| 151 | name => 'leavesub', | ||
| 152 | first => op_or( | ||
| 153 | $simple_structure, | ||
| 154 | $self_shift_structure, | ||
| 155 | $self_array_assign_structure, | ||
| 156 | ), | ||
| 157 | }, @_ ) }, | ||
| 158 | sub { | ||
| 159 | my $op = shift; | ||
| 160 | #print $op->name." " .$op->type." " .$op->first->name. "\n"; | ||
| 161 | #my $inner = $op->first->last; | ||
| 162 | #$inner = $inner->last if $inner->name eq 'return'; | ||
| 163 | #$inner = $inner->last; | ||
| 164 | #my $key_string = $inner->sv->PV; | ||
| 165 | my $key_string = $op->{hash_key}->sv->PV; | ||
| 166 | #warn $key; | ||
| 167 | push @to_be_replaced, ["${edit_pkg}::$function", $key_string]; | ||
| 168 | }, | ||
| 169 | ); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | foreach my $struct (@to_be_replaced) { | ||
| 174 | my $function = $struct->[0]; | ||
| 175 | my $key = $struct->[1]; | ||
| 176 | if ($AutoXS::Debug) { | ||
| 177 | warn "Replacing $function with XS accessor for key '$key'.\n"; | ||
| 178 | } | ||
| 179 | Class::XSAccessor->import( | ||
| 180 | replace => 1, | ||
| 181 | getters => { $function => $key }, | ||
| 182 | ); | ||
| 183 | } | ||
| 184 | |||
| 185 | } | ||
| 186 | |||
| 187 | 1; | ||
| 188 | __END__ |