File Coverage

blib/lib/ExtUtils/XSpp/Node/Type.pm
Criterion Covered Total %
statement 46 46 100.0
branch 17 18 94.4
condition 9 11 81.8
subroutine 14 14 100.0
pod 2 11 18.1
total 88 100 88.0


line stmt bran cond sub pod time code
1             package ExtUtils::XSpp::Node::Type;
2 21     21   136 use strict;
  21         47  
  21         623  
3 21     21   68 use warnings;
  21         30  
  21         786  
4 21     21   99 use base 'ExtUtils::XSpp::Node';
  21         35  
  21         1438  
5              
6             # TODO: Document...
7              
8             # normalized names for some integral C types
9             my %normalize =
10             ( 'unsigned' => 'unsigned int',
11             'long int' => 'long',
12             'unsigned long int' => 'unsigned long',
13             'short int' => 'short',
14             'unsigned short int' => 'unsigned short',
15             );
16              
17             sub init {
18 3468     3468 1 3330 my $this = shift;
19 3468         5164 my %args = @_;
20              
21 3468   66     8634 $this->{BASE} = $normalize{$args{base}} || $args{base};
22 3468 100       5042 $this->{POINTER} = $args{pointer} ? 1 : 0;
23 3468 100       4590 $this->{REFERENCE} = $args{reference} ? 1 : 0;
24 3468 100       4539 $this->{CONST} = $args{const} ? 1 : 0;
25 3468   100     8392 $this->{TEMPLATE_ARGS} = $args{template_args} || [];
26             }
27              
28             sub clone {
29 3     3 0 4 my $this = shift;
30 3         10 my $clone = bless {%$this} => ref($this);
31 3         5 $clone->{TEMPLATE_ARGS} = [map $_->clone, @{$clone->template_args}];
  3         4  
32 3         5 return $clone;
33             }
34              
35 5135     5135 0 7316 sub is_const { $_[0]->{CONST} }
36 4737     4737 0 7802 sub is_reference { $_[0]->{REFERENCE} }
37 4741     4741 0 7542 sub is_pointer { $_[0]->{POINTER} }
38 4301     4301 0 8387 sub base_type { $_[0]->{BASE} }
39 7612     7612 0 9898 sub template_args { $_[0]->{TEMPLATE_ARGS} }
40              
41             sub equals {
42 2414     2414 0 2617 my( $f, $s ) = @_;
43              
44 2414 100       2134 return 0 if @{$f->template_args} != @{$s->template_args};
  2414         2630  
  2414         2529  
45              
46 2396         2482 for( my $i = 0; $i < @{$f->template_args}; ++$i ) {
  2403         2608  
47 7 50       14 return 0
48             unless $f->template_args->[$i]->equals( $s->template_args->[$i] );
49             }
50              
51 2396   100     2821 return $f->is_const == $s->is_const
52             && $f->is_reference == $s->is_reference
53             && $f->is_pointer == $s->is_pointer
54             && $f->base_type eq $s->base_type;
55             }
56              
57 106   66 106 0 207 sub is_void { return $_[0]->base_type eq 'void' &&
58             !$_[0]->is_pointer && !$_[0]->is_reference }
59              
60             sub print_tmpl_args {
61 359     359 0 397 my $this = shift;
62 359         371 my $state = shift;
63 359         353 my $tmpl_args = '';
64 359 100       345 if( @{$this->template_args} ) {
  359         471  
65             $tmpl_args = '< '
66             . join( ', ',
67 5         7 map $_->print( $state ), @{$this->template_args} )
  5         8  
68             . ' >';
69             }
70 359         590 return $tmpl_args;
71             }
72              
73             sub print {
74 343     343 1 366 my $this = shift;
75 343         355 my $state = shift;
76              
77 343 100       472 return join( '',
    100          
    100          
78             ( $this->is_const ? 'const ' : '' ),
79             $this->base_type,
80             $this->print_tmpl_args,
81             ( $this->is_pointer ? ( '*' x $this->is_pointer ) :
82             $this->is_reference ? '&' : '' ) );
83             }
84              
85              
86             1;