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   3264 use strict;
  21         1442  
  21         813  
3 21     21   115 use warnings;
  21         46  
  21         1989  
4 21     21   123 use base 'ExtUtils::XSpp::Node';
  21         1197  
  21         3250  
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 4562 my $this = shift;
19 3468         9178 my %args = @_;
20              
21 3468   66     18154 $this->{BASE} = $normalize{$args{base}} || $args{base};
22 3468 100       8211 $this->{POINTER} = $args{pointer} ? 1 : 0;
23 3468 100       7870 $this->{REFERENCE} = $args{reference} ? 1 : 0;
24 3468 100       11519 $this->{CONST} = $args{const} ? 1 : 0;
25 3468   100     21070 $this->{TEMPLATE_ARGS} = $args{template_args} || [];
26             }
27              
28             sub clone {
29 3     3 0 4 my $this = shift;
30 3         20 my $clone = bless {%$this} => ref($this);
31 3         4 $clone->{TEMPLATE_ARGS} = [map $_->clone, @{$clone->template_args}];
  3         11  
32 3         32 return $clone;
33             }
34              
35 5135     5135 0 15274 sub is_const { $_[0]->{CONST} }
36 4737     4737 0 16549 sub is_reference { $_[0]->{REFERENCE} }
37 4741     4741 0 16300 sub is_pointer { $_[0]->{POINTER} }
38 4301     4301 0 23418 sub base_type { $_[0]->{BASE} }
39 7612     7612 0 18613 sub template_args { $_[0]->{TEMPLATE_ARGS} }
40              
41             sub equals {
42 2414     2414 0 3298 my( $f, $s ) = @_;
43              
44 2414 100       2515 return 0 if @{$f->template_args} != @{$s->template_args};
  2414         3964  
  2414         3897  
45              
46 2396         3737 for( my $i = 0; $i < @{$f->template_args}; ++$i ) {
  2403         4103  
47 7 50       17 return 0
48             unless $f->template_args->[$i]->equals( $s->template_args->[$i] );
49             }
50              
51 2396   100     4376 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 277 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 471 my $this = shift;
62 359         398 my $state = shift;
63 359         436 my $tmpl_args = '';
64 359 100       382 if( @{$this->template_args} ) {
  359         607  
65 5         11 $tmpl_args = '< '
66             . join( ', ',
67 5         8 map $_->print( $state ), @{$this->template_args} )
68             . ' >';
69             }
70 359         1067 return $tmpl_args;
71             }
72              
73             sub print {
74 343     343 1 517 my $this = shift;
75 343         413 my $state = shift;
76              
77 343 100       714 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;