File: Ref.pm

package info (click to toggle)
icheck 0.9.7-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,352 kB
  • sloc: perl: 12,152; makefile: 202; ansic: 100
file content (195 lines) | stat: -rw-r--r-- 3,479 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package CType::Ref;

use 5.6.0;
use strict;
use warnings;

use Carp;
use CType;

no warnings 'recursion';

our @ISA = qw/CType/;

my @refs;

sub new
  {
    my $this = shift;
    my $class = ref($this) || $this;
    my $kind = shift;
    my $name = shift;
    my $namespace = shift;

    my $self = {kind => $kind,
                name => $name,
                type => undef,
               };
    bless $self, $class;

    $self->do_complete($namespace) if $namespace;
    push @refs, $self unless $self->{type};

    return $self;
  }

sub kind
  {
    my $self = shift;
    return $self->{kind};
  }

sub name
  {
    my $self = shift;
    return $self->{name};
  }

sub complete_refs
  {
    my $namespace = shift;

    $_->do_complete($namespace) foreach @refs;
    @refs = ();
  }

sub do_complete
  {
    my $self = shift;
    my $namespace = shift;

    $self->{type} = $namespace->get($self->{kind}, $self->{name});
  }

sub complete
  {
    my $self = shift;
    return $self->{type} ? $self->{type}->complete : 0;
  }

sub best_type_for_comparison
  {
    my $self = shift;
    return $self unless $self->{type};
    if ($self->{kind} eq 'ordinary')
      {
        # We look through typedefs, so that we can compare 'int' and
        # 'foo', where 'typedef int foo'
        return $self->{type}->best_type_for_comparison;
      }
    else
      {
        # We don't look through anything else, because 'int' and
        # 'struct foo' are always a mismatch
        return $self;
      }
  }

sub type
  {
    my $self = shift;
    my $accept_incomplete = shift;

    unless ($self->{type})
      {
        return undef if $accept_incomplete;
        confess "$self->{kind} $self->{name} is incomplete";
      }
    return $self->{type}->type($accept_incomplete);
  }

sub describe
  {
    my $self = shift;
    my $qualifiers = $self->describe_qualifiers;
    $qualifiers .= ' ' if $qualifiers;

    my $str = $qualifiers;
    if ($self->{kind} eq 'ordinary')
      {
        $str .= $self->{name};
      }
    else
      {
        $str .= "$self->{kind} $self->{name}";
      }
    return $str;
  }

sub dump_c
  {
    my $self = shift;
    my $skip_cpp = shift;

    my $qualifiers = $self->dump_c_qualifiers;
    my $str = '';
    if ($self->{kind} eq 'ordinary')
      {
        $str .= $self->{name};
      }
    else
      {
        $str .= "$self->{kind} $self->{name}";
      }
    $str .= ' ' . $qualifiers if $qualifiers;
    return $str;
  }

sub layout
  {
    my $self = shift;
    my $accept_incomplete = shift;
    my $namespace = shift;

    # It's possible for us to get loops here, so we break cycles
    return if $self->{laying_out};
    $self->{laying_out} = 1;

    my $type = $self->type($accept_incomplete);
    $type->layout($accept_incomplete, $namespace) if $type;

    $self->{laying_out} = 0;
  }

sub _check_interface
  {
    my $self = shift;
    my $other = shift;

    return 'both' unless $other->isa('CType::Ref');
    return 'both' if $self->{kind} ne $other->{kind};
    return 'both' if $self->{name} ne $other->{name};
    return 'ok';
  }

sub get_refs
  {
    my $self = shift;
    return ($self);
  }

sub width
  {
    my $self = shift;
    return $self->type->width;
  }

sub alignment
  {
    my $self = shift;
    return $self->type->alignment;
  }

sub alignment_exprs
  {
    my $self = shift;
    return $self->type->alignment_exprs;
  }

sub signed
  {
    my $self = shift;
    return $self->type->signed;
  }

1;