#!/usr/bin/perl -w

use strict;
use CORBA::IDL;
use CORBA::IDL::parser30;
use CORBA::IDL::symbtab;
use CORBA::Cplusplus;
use CORBA::C;
# visitors
use CORBA::IDL::repos_id;
use CORBA::Cplusplus::literal;
use CORBA::Cplusplus::name;
use CORBA::Cplusplus::type;
use CORBA::Cplusplus::include;
use CORBA::C::include;

my $parser = new Parser;
$parser->YYData->{verbose_error} = 1;		# 0, 1
$parser->YYData->{verbose_warning} = 1;		# 0, 1
$parser->YYData->{verbose_info} = 1;		# 0, 1
$parser->YYData->{verbose_deprecated} = 1;	# 0, 1 (concerns only version '2.4' and upper)
$parser->YYData->{symbtab} = new CORBA::IDL::Symbtab($parser);
my $cflags = '-D__idl2cpp';
if ($Parser::IDL_version lt '3.0') {
	$cflags .= ' -D_PRE_3_0_COMPILER_';
}
if ($^O eq 'MSWin32') {
	$parser->YYData->{preprocessor} = 'cpp -C ' . $cflags;
#	$parser->YYData->{preprocessor} = 'CL /E /C /nologo ' . $cflags;	# Microsoft VC
}
else {
	$parser->YYData->{preprocessor} = 'cpp -C ' . $cflags;
}
$parser->getopts('hi:vx');
if ($parser->YYData->{opt_v}) {
	print "CORBA::Cplusplus $CORBA::Cplusplus::VERSION\n";
	print "CORBA::C $CORBA::C::VERSION\n";
	print "CORBA::IDL $CORBA::IDL::VERSION\n";
	print "IDL $Parser::IDL_version\n";
	print "$0\n";
	print "Perl $] on $^O\n";
	exit;
}
if ($parser->YYData->{opt_h}) {
	use Pod::Usage;
	pod2usage(-verbose => 1);
}
$parser->Run(@ARGV);
$parser->YYData->{symbtab}->CheckForward();
$parser->YYData->{symbtab}->CheckRepositoryID();

if (exists $parser->YYData->{nb_error}) {
	my $nb = $parser->YYData->{nb_error};
	print "$nb error(s).\n"
}
if (        $parser->YYData->{verbose_warning}
		and exists $parser->YYData->{nb_warning} ) {
	my $nb = $parser->YYData->{nb_warning};
	print "$nb warning(s).\n"
}
if (        $parser->YYData->{verbose_info}
		and exists $parser->YYData->{nb_info} ) {
	my $nb = $parser->YYData->{nb_info};
	print "$nb info(s).\n"
}
if (        $parser->YYData->{verbose_deprecated}
		and exists $parser->YYData->{nb_deprecated} ) {
	my $nb = $parser->YYData->{nb_deprecated};
	print "$nb deprecated(s).\n"
}

if (        exists $parser->YYData->{root}
		and ! exists $parser->YYData->{nb_error} ) {
	$parser->YYData->{root}->visit(new CORBA::IDL::repositoryIdVisitor($parser));
	if (        $Parser::IDL_version ge '3.0'
			and $parser->YYData->{opt_x} ) {
		$parser->YYData->{symbtab}->Export();
	}
	$parser->YYData->{root}->visit(new CORBA::Cplusplus::nameVisitor($parser));
	$parser->YYData->{root}->visit(new CORBA::Cplusplus::literalVisitor($parser));
	$parser->YYData->{root}->visit(new CORBA::Cplusplus::lengthVisitor($parser));
	$parser->YYData->{root}->visit(new CORBA::Cplusplus::typeVisitor($parser));
	$parser->YYData->{root}->visit(new CORBA::Cplusplus::includeVisitor($parser));
}

__END__

=head1 NAME

idl2cpp - IDL compiler to language C++ mapping

=head1 SYNOPSIS

idl2cpp [options] I<spec>.idl

=head1 OPTIONS

All options are forwarded to C preprocessor, except -h -i -v -x.

With the GNU C Compatible Compiler Processor, useful options are :

=over 8

=item B<-D> I<name>

=item B<-D> I<name>=I<definition>

=item B<-I> I<directory>

=item B<-I->

=item B<-nostdinc>

=back

Specific options :

=over 8

=item B<-h>

Display help.

=item B<-i> I<directory>

Specify a path for import (only for IDL version 3.0).

=item B<-v>

Display version.

=item B<-x>

Enable export (only for IDL version 3.0).

=back

=head1 DESCRIPTION

B<idl2cpp> parses the given input file (IDL) and generates
a include file following the language C mapping rules.

B<idl2cpp> is a Perl OO application what uses the visitor design pattern.
The parser is generated by Parse::Yapp.

B<idl2cpp> needs Math::BigInt and CORBA::Fixed modules.

B<idl2cpp> needs a B<cpp> executable.

CORBA Specifications, including IDL (Interface Language Definition) and
C++ Language Mapping are available on E<lt>http://www.omg.org/E<gt>.

=head1 SEE ALSO

cpp, perl

=head1 COPYRIGHT

(c) 2002-2007 Francois PERRAD, France. All rights reserved.

This program and all CORBA::Cplusplus modules are distributed
under the terms of the Artistic Licence.

=head1 AUTHOR

Francois PERRAD, francois.perrad@gadz.org

=cut

