File: CType.pm

package info (click to toggle)
pdl 2.005-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,200 kB
  • ctags: 3,301
  • sloc: perl: 14,876; ansic: 7,223; fortran: 3,417; makefile: 54; sh: 16
file content (182 lines) | stat: -rw-r--r-- 4,160 bytes parent folder | download | duplicates (12)
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
# Represent any C type.
# Type contains the size of arrays, which is either constant
# or resolved (into an object) from resolveobj.

package C::Type;
use Carp;

# new C::Type(resolveobj,str)

sub new {
	my $this = bless {},shift;
	$this->{Resolve} = shift;
	if(@_) {
		$this->parsefrom(shift);
	}
	return $this;
}

sub stripptrs {
	my($this,$str) = @_;
	if($str =~ /^\s*\w+\s*$/) {
		$str =~ s/\s//g;
		$this->{ProtoName} = $str;
		return [];
	} else {
# Now, recall the different C syntaxes. First priority is a pointer:
		my $decl;
		if($str =~ /^\s*\*(.*)$/) {
			$decl = $this->stripptrs($1);
			unshift @$decl,"PTR";
		} elsif($str =~ /^\s*\(.*\)\s*$/) {
# XXX Should try to see if a funccall.
			return $this->stripptrs($1);
		} elsif($str =~ /^(.*)\[([^]]+)\]\s*$/) {
			my $siz = $2;
			print "ARR($str): ($siz)\n" if $::PP_VERBOSE;
			$decl = $this->stripptrs($1);
			unshift @$decl,"ARR($siz)";
			print "ARR($str): ($siz)\n" if $::PP_VERBOSE;
		} else {
			die("Invalid C type '$str'");
		}
		return $decl;
	}
}

# XXX Correct to *real* parsing. This is only a subset.
sub parsefrom {
	my($this,$str) = @_;
# First, take the words in the beginning
	$str =~ /^\s*((?:\w+\b\s*)+)([^[].*)$/;
	my $base = $1; my $decl = $2;
	my $foo = $this->stripptrs($decl);
	$this->{Base} = $base;
	$this->{Chain} = $foo;
}

sub get_decl {
	my($this,$name,$opts) = @_;
	for(@{$this->{Chain}}) {
		if($_ eq "PTR") {$name = "*$name"}
		elsif($_ =~/^ARR\((.*)\)$/) {
			if($opts->{VarArrays2Ptrs}) {
				$name = "*$name";
			} else {
				$name = "($name)[$1]";
			}
		}
		else { confess("Invalid decl") }
	}
	return "$this->{Base} $name";
}

# Useful when parsing argument decls
sub protoname { return shift->{ProtoName} }

sub get_copy {
	my($this,$from,$to) = @_;
	my ($prev,$close);
	if($#{$this->{Chain}} >= 0) {
		# strdup loses portability :(
		return "($to) = malloc(strlen($from)+1); strcpy($to,$from);"
		 if $this->{Base} =~ /^\s*char\s*$/;
                return "($to) = newSVsv($from);"
                 if $this->{Base} =~ /^\s*SV\s*$/;
		my $code = $this->get_malloc($to,$from);
		my ($deref0,$deref1) = ($from,$to);
		for(@{$this->{Chain}}) {
			if($_ eq "PTR") {confess("Cannot alloc pointer, must be array");}
			elsif($_ =~/^ARR\((.*)\)$/) {
				$no++;
				$prev .= "
				  if(!$deref0) {$deref1=0;}
				  else {int __malloc_ind_$no;
					for(__malloc_ind_$no = 0;
						__malloc_ind_$no < $1;
						__malloc_ind_$no ++) {";
				$deref0 = $deref0."[__malloc_ind_$no]";
				$deref1 = $deref1."[__malloc_ind_$no]";
				$close .= "}}";
			} else { confess("Invalid decl $_") }
		}
		$code .= "$prev $deref1 = $deref0; $close";
		return $code;
	}
	return "($to) = ($from);";
}

sub get_free {
	my($this,$from) = @_;
	my ($prev,$close);
	if($#{$this->{Chain}} >= 0) {
		return "free($from);"
		 if $this->{Base} =~ /^\s*char\s*$/;
                return "SvREFCNT_dec($from);"
                 if $this->{Base} =~ /^\s*SV\s*$/;
		my @mallocs;
		my $str = "{";
		my $deref = "$from";
		my $prev = undef;
		my $close = undef;
		my $no = 0;
		for(@{$this->{Chain}}) {
			$no++;
			if($no > 1) {croak("Can only free one layer!\n");}
#			if($_ eq "PTR") {confess("Cannot free pointer, must be array ;) (FIX CType.pm)");}
			return "free($from);\n ";
		}
	} else {
		"";
	}
}

sub need_malloc {
	my($this) = @_;
	return scalar grep /(ARR|PTR)/,(@{$this->{Chain}})
}

# Just returns with the array string.
sub get_malloc {
	my($this,$assignto) = @_;
	my $str = "{";
	my $deref = "$assignto";
	my $prev = undef;
	my $close = undef;
	my $no = 0;
	for(@{$this->{Chain}}) {
		if($_ eq "PTR") {confess("Cannot alloc pointer, must be array");}
		elsif($_ =~/^ARR\((.*)\)$/) {
			$str .= "$prev $assignto =
				malloc(sizeof(* $assignto) * $1);
				";
			$no++;
			$prev = "{int __malloc_ind_$no;
				for(__malloc_ind_$no = 0;
					__malloc_ind_$no < $1;
					__malloc_ind_$no ++) {";
			$deref = $deref."[__malloc_ind_$no]";
			$close .= "}}";
		} else { confess("Invalid decl $_") }
	}
	$str .= "}";
	return $str;
}

sub getvar {
}

# Determine if everything constant and can just declare
sub need_alloc {
}

sub alloccode {
}

sub copycode {
}

sub freecode {
}

1;