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
|
# This file is part of The New Aspell
# Copyright (C) 2001-2002 by Kevin Atkinson under the GNU LGPL
# license version 2.0 or 2.1. You should have received a copy of the
# LGPL license along with this library if you did not you can find it
# at http://www.gnu.org/.
package MkSrc::ProcNative;
BEGIN {
use Exporter;
our @ISA = qw(Exporter);
}
use strict;
use warnings;
no warnings qw(uninitialized);
no locale;
use MkSrc::Util;
use MkSrc::CcHelper;
use MkSrc::Info;
use MkSrc::Create;
use MkSrc::Type;
sub make_native_obj ( $ @ );
$info{forward}{proc}{native} = sub {
my ($type) = @_;
return "$type->{type} ".to_mixed($type->{name}).";\n";
};
$info{group}{proc}{native} = sub {
my ($data) = @_;
return if exists $data->{'no native'};
create_cc_file (type => 'native',
cxx => true,
namespace => 'acommon',
dir => "common",
header => true,
data => $data);
};
$info{enum}{proc}{native} = sub {
my ($data) = @_;
my $n = to_mixed($data->{name});
return ("enum $n {" .
join (',',
map {to_mixed($data->{prefix}).to_mixed($_->{type})}
@{$data->{data}}).
"};\n");
};
$info{struct}{proc}{native} = sub {
return make_native_obj 'struct', @_;
};
$info{union}{proc}{native} = sub {
return make_native_obj 'union', @_;
};
$info{class}{proc}{native} = sub {
return make_native_obj 'class', @_;
};
$info{errors}{proc}{native} = sub {
my ($data, $accum) = @_;
my $ret;
$accum->{types}{"error info"} = finalized_type "error info";
my $p0;
$p0 = sub {
my ($level, $data) = @_;
return unless defined $data;
foreach my $d (@$data) {
$ret .= "extern \"C\" const ErrorInfo * const ";
$ret .= ' 'x$level;
$ret .= "aerror_";
$ret .= to_lower($d->{type});
$ret .= ";\n";
$p0->($level + 2, $d->{data});
}
};
my $p1;
$p1 = sub {
my ($level, $data) = @_;
return unless defined $data;
foreach my $d (@$data) {
$ret .= "static const ErrorInfo * const ";
$ret .= ' 'x$level;
$ret .= to_lower($d->{type});
$ret .= "_error" if defined $d->{data} || $level == 0;
$ret .= " = aerror_";
$ret .= to_lower($d->{type});
$ret .= ";\n";
$p1->($level + 2, $d->{data});
}
};
$p0->(0, $data->{data});
$ret .= "\n\n";
$p1->(0, $data->{data});
return $ret;
};
sub make_cxx_constructor ( $ $ ; \% ) {
my ($class, $p, $accum) = @_;
my $ret;
$ret .= to_mixed($class);
$ret .= "(";
$ret .= join ', ', map {to_type_name $_, {mode=>'native',pos=>'parm'}, %$accum} @$p;
$ret .= ")";
return $ret;
}
sub make_native_obj ( $ @ ) {
my ($t, $data, $accum) = @_;
my $obj = to_mixed($data->{name});
my @defaults;
my @public;
foreach my $d (@{$data->{data}}) {
next unless $d->{type} eq 'public';
next if $d->{name} eq $data->{name};
push @public, to_mixed($d->{name});
my $typ = finalized_type $d->{name};
$accum->{headers}{$typ->{created_in}} = true;
}
my $ret;
$ret .= "$t $obj ";
$ret .= ": ".join(', ', map {"public $_"} @public).' ' if @public;
$ret .= "{\n";
$ret .= " public:\n" if $t eq 'class';
foreach my $d (@{$data->{data}}) {
next if exists $d->{'c only'};
next if one_of $d->{type}, qw(constructor destructor public);
$ret .= " ";
if ($d->{type} eq 'method') {
my $is_vir = $t eq 'class' && !exists $d->{'cxx impl'};
$ret .= "virtual " if $is_vir;
$ret .= make_cxx_method $d, {mode=>'native'}, %$accum;
$ret .= $is_vir ? " = 0;\n"
: exists $d->{'cxx impl'} ? " { $d->{'cxx impl'}; }\n"
: ";\n";
} elsif ($d->{type} eq 'cxx constructor') {
$ret .= make_cxx_constructor $data->{name}, $d->{data}, %$accum;
$ret .= exists $d->{'cxx impl'} ? " $d->{'cxx impl'}\n"
: ";\n";
} else { # is a type
if (exists $d->{default}) {
push @defaults, $d;
}
if ($d->{type} eq 'cxx member') {
foreach (split /\s*,\s*/, $d->{'headers'}) {
$accum->{headers}{$_} = true;
}
$ret .= $d->{what};
} elsif ($t eq 'class') {
$ret .= to_type_name $d, {mode=>'native'}, %$accum;
$ret .= "_";
} else {
$ret .= to_type_name $d, {mode=>'cc_cxx'}, %$accum;
}
$ret .= ";\n";
}
}
if (@defaults || $t eq 'class') {
$ret .= " $obj()";
if (@defaults) {
$ret .= " : ";
$ret .= join ', ', map {to_lower($_->{name}).($t eq 'class'?'_':'')."($_->{default})"} @defaults;
}
$ret .= " {}\n";
}
$ret .= " virtual ~${obj}() {}\n" if $t eq 'class';
$ret .= "};\n";
foreach my $d (@{$data->{data}}) {
next unless $d->{type} eq 'constructor';
$ret .= make_c_method $data->{name}, $d, {mode=>'native',no_aspell=>false}, %$accum;
$ret .= ";\n";
}
return $ret;
}
1;
|