summaryrefslogtreecommitdiffstats
path: root/kalyptus/kalyptus
blob: 224ac09f91b41f08432ff5be4024edd6051ad5fc (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
#!/usr/bin/perl

# KDOC -- C++ and CORBA IDL interface documentation tool.
# Sirtaj Singh Kang <taj@kde.org>, Jan 1999.
# $Id$

# All files in this project are distributed under the GNU General
# Public License. This is Free Software.

require 5.000;

use Carp;
use Getopt::Long;
use File::Basename;
use strict;

use Ast;

use kdocUtil;
use kdocAstUtil;
use kdocParseDoc;

use vars qw/ %rootNodes $declNodeType @includes_list %options @formats_wanted $allow_k_dcop_accessors
        @includeclasses $includeclasses $skipInternal %defines $defines $match_qt_defines
	$libdir $libname $outputdir @libs $parse_global_space $qt_embedded $qt4 $striphpath $doPrivate $readstdin
	$Version $tquiet $debug $debuggen $parseonly $currentfile $cSourceNode $exe
	%formats %flagnames @allowed_k_dcop_accesors $allowed_k_dcop_accesors_re $rootNode 
	@classStack $cNode $globalSpaceClassName
	$lastLine $docNode @includes $cpp $defcppcmd $cppcmd $docincluded
	$inExtern $inNamespace %stats %definitions @inputqueue @codeqobject @qt4_codeqobject @qte_codeqobject /;
     
## globals

%rootNodes = ();			# root nodes for each file type
$declNodeType = undef;			# last declaration type

@includes_list = ();			# list of files included from the parsed .h

# All options

%options = ();				# hash of options (set getopt below)
@formats_wanted = ();
$libdir = $ENV{KDOCLIBS};
$libname = "";
$outputdir = ".";
@libs = ();				# list of includes
$striphpath = 0;

@includeclasses = ();                     # names of classes to include
$includeclasses = "";

$doPrivate = 0;
$Version = "0.9";

$tquiet = 0;
$debug = 0;
$debuggen = 0;
$parseonly = 0;
$globalSpaceClassName = "TQGlobalSpace";

$currentfile = "";

$cpp = 0;
$defcppcmd = "g++ -Wp,-C -E";
$cppcmd = "";

$exe = basename $0;

@inputqueue = ();
@codeqobject = split "\n", <<CODE;
public:
    virtual TQMetaObject *metaObject() const;
    virtual const char *className() const;
    virtual void* tqt_cast( const char* );
    virtual bool tqt_invoke( int, TQUObject* );
    virtual bool tqt_emit( int, TQUObject* );
    virtual bool tqt_property( int, int, TQVariant* );
    static TQMetaObject* staticMetaObject();
    TQObject* qObject();
    static TQString tr( const char *, const char * = 0 );
    static TQString trUtf8( const char *, const char * = 0 );
private:
CODE

@qt4_codeqobject = split "\n", <<CODE;
public:
    static const TQMetaObject staticMetaObject;
    virtual const TQMetaObject *metaObject() const;
    virtual void *qt_metacast(const char *);
    static inline TQString tr(const char *s, const char *c = 0)
        { return staticMetaObject.tr(s, c); }
    virtual int qt_metacall(TQMetaObject::Call, int, void **);
private:
CODE

@qte_codeqobject = split "\n", <<CODE;
public:
    TQMetaObject *metaObject() const {
         return staticMetaObject();
    }
    const char *className() const;
    static TQMetaObject* staticMetaObject();
    static TQString tr( const char *, const char * = 0 );
protected:
    void initMetaObject();
private:
CODE

# Supported formats
%formats = ( "java" => "kalyptusCxxToJava", "jni" => "kalyptusCxxToJNI", 
	"dcopidl" => "kalyptusCxxToDcopIDL",
	"smoke" => "kalyptusCxxToSmoke", "csharp" => "kalyptusCxxToCSharp", "kimono" => "kalyptusCxxToKimono",
        "ECMA" => "kalyptusCxxToECMA", "swig" => "kalyptusCxxToSwig",
		"KDOMECMA" => "kalyptusKDOMEcma");

# these are for expansion of method flags
%flagnames = ( v => 'virtual', 's' => 'static', p => 'pure',
	c => 'const', l => 'slot', i => 'inline', n => 'signal',
 	d => 'k_dcop', z => 'k_dcop_signals', y => 'k_dcop_hidden' );

@allowed_k_dcop_accesors = qw(k_dcop k_dcop_hidden k_dcop_signals);
$allowed_k_dcop_accesors_re = join("|", @allowed_k_dcop_accesors);
        
%definitions = {
    _STYLE_CDE => '',
    _STYLE_MOTIF => '',
    _STYLE_MOTIF_PLUS => '',
    PLUS => '',
    _STYLE_PLATINUM => '',
    _STYLE_SGI => '',
    _STYLE_WINDOWS => '',
    QT_STATIC_CONST => 'static const',
    Q_EXPORT => '',
    Q_EXPORT_CODECS_BIG5 => '',
    Q_REFCOUNT => '',
    TQM_EXPORT_CANVAS => '',
    TQM_EXPORT_DNS => '',
    TQM_EXPORT_ICONVIEW => '',
    TQM_EXPORT_NETWORK => '',
    TQM_EXPORT_SQL => '',
    TQM_EXPORT_WORKSPACE => '',
    QT_NO_REMOTE => 'QT_NO_REMOTE',
    QT_ACCESSIBILITY_SUPPORT => 'QT_ACCESSIBILITY_SUPPORT',
    Q_WS_X11 => 'Q_WS_X11',
    TQ_DISABLE_COPY => 'TQ_DISABLE_COPY',
    Q_WS_QWS => 'undef',
    Q_WS_MAC => 'undef',
    Q_OBJECT => <<'CODE',
public:
    virtual TQMetaObject *metaObject() const;
    virtual const char *className() const;
    virtual bool tqt_invoke( int, TQUObject* );
    virtual bool tqt_emit( int, TQUObject* );
    static TQString tr( const char *, const char * = 0 );
    static TQString trUtf8( const char *, const char * = 0 );
private:
CODE
};

=head1 KDOC -- Source documentation tool

	Sirtaj Singh Kang <taj@kde.org>, Dec 1998.

=cut

# read options

Getopt::Long::config qw( no_ignore_case permute bundling auto_abbrev );

GetOptions( \%options,
	"format|f=s", \@formats_wanted,
	"url|u=s",
	"skip-internal", \$skipInternal,
	"skip-deprecated|e",
	"document-all|a",
	"compress|z",
        "no-cache",      # do not create $HOME/.kalyptus cache
	# HTML options
	"html-cols=i",
	"html-logo=s",

	"strip-h-path",	\$striphpath,
	"outputdir|d=s", \$outputdir,
	"stdin|i",	\$readstdin,
	"name|n=s",	\$libname,
	"help|h", 	\&show_usage,
	"version|v|V", 	\&show_version,
	"private|p",	\$doPrivate,
	"libdir|L=s",	\$libdir,
	"xref|l=s",	\@libs,
	"classes|c=s",    \@includeclasses,
	"globspace",	\$parse_global_space,
	"qte",	\$qt_embedded,
	"qt4",	\$qt4,
	"allow_k_dcop_accessors", \$allow_k_dcop_accessors,

	"cpp|P",	\$cpp,
	"docincluded",  \$docincluded,
	"cppcmd|C=s",	\$cppcmd,
	"includedir|I=s", \@includes,
	"define=s", \%defines, # define a single preprocessing symbol
	"defines=s", \$defines, # file containing preprocessing symbols, one per line

	"tquiet|q",	\$tquiet,
	"debug|D",	\$debug, # debug the parsing
	"debuggen",	\$debuggen, # debug the file generation
	"parse-only",	\$parseonly )
		|| exit 1;

$| = 1 if $debug or $debuggen;

# preprocessor settings

if ( $cppcmd eq "" ) {
	$cppcmd = $defcppcmd;
}
else {
	$cpp = 1;
}

if ($#includeclasses>=0)
{
    $includeclasses = join (" ", @includeclasses);
    print "Using Classes: $includeclasses\n" unless $tquiet;
}

if ( $#includes >= 0 && !$cpp ) {
	die "$exe: --includedir requires --cpp\n";
}

# Check output formats. HTML is the default
if( $#formats_wanted < 0 ) {
	push @formats_wanted, "java";
}

foreach my $format ( @formats_wanted ) {
	die "$exe: unsupported format '$format'.\n"
		if !defined $formats{$format};
}

if( $defines )
{
    open( DEFS, $defines ) or die "Couldn't open $defines: $!\n";
    my @defs = <DEFS>;
    chomp @defs;
    close DEFS;
    foreach (@defs)
    {
        $defines{ $_ } = 1 unless exists $defines{ $_ };
    }
}

# Check the %defines hash for QT_* symbols and compile the corresponding RE
# Otherwise, compile the default ones. Used for filtering in readCxxLine.
if ( my @qt_defines = map { ($_=~m/^QT_(.*)/)[0] } keys %defines)
{
    my $regexp = "m/^#\\s*ifn?def\\s+QT_(?:" . join('|', map { "\$qt_defines[$_]" } 0..$#qt_defines).")/o";
    $match_qt_defines = eval "sub { my \$s=shift;
                                   \$s=~/^#\\s*if(n)?def\\s+QT_/ || return 0;
                                   if(!\$1) { return \$s=~$regexp ? 0:1 }
                                   else { return \$s=~$regexp ? 1:0 }
                                  }";
    die if $@;
}
else
{
    $match_qt_defines = eval q{
    sub
    {
        my $s = shift;
        $s =~ m/^\#\s*ifndef\s+QT_NO_(?:REMOTE| # not in the default compile options
                                        NIS|    #  ...
                                        XINERAMA|
                                        IMAGEIO_(?:MNG|JPEG)|
                                        STYLE_(?:MAC|INTERLACE|COMPACT)
                                     )/x;
    }
    };
    die if $@;
}
# Check if there any files to process.
# We do it here to prevent the libraries being loaded up first.

checkFileArgs();

# work out libdir. This is created by kdocLib:writeDoc when
# required.
$libdir = $ENV{HOME}."/.kalyptus" unless $libdir ne "";


######
###### main program
######
	readLibraries();
	parseFiles();

	if ( $parseonly ) {
		print "\n\tParse Tree\n\t------------\n\n";
		kdocAstUtil::dumpAst( $rootNode );
	}
	else {
		writeDocumentation();
		writeLibrary() unless $libname eq "";
	}

	kdocAstUtil::printDebugStats() if $debug;

	exit 0;
######

sub checkFileArgs
{
	return unless $#ARGV < 0;

	die "$exe: no input files.\n" unless $readstdin;

	# read filenames from standard input
    	while (<STDIN>) {
		chop;
		$_ =~ s,\\,/,g;	# back to fwd slash (for Windows)
		foreach my $file ( split( /\s+/, $_ ) ) {
			push @ARGV, $file;
		}
	}
}

sub readLibraries
{
	return if $#libs < 0;

	require kdocLib;
	foreach my $lib ( @libs ) {
		print "$exe: reading lib: $lib\n" unless $tquiet;

		my $relpath = exists $options{url} ? 
			$options{url} : $outputdir;
		kdocLib::readLibrary( \&getRoot, $lib, $libdir, $relpath );
	}
}

sub parseFiles
{
	foreach $currentfile ( @ARGV ) {
		my $lang = "CXX";

		if ( $currentfile =~ /\.idl\s*$/ ) {
			# IDL file
			$lang = "IDL";
		}

		# assume cxx file
		if( $cpp ) {
			# pass through preprocessor
			my $cmd = $cppcmd;
			foreach my $dir ( @includes ) {
				$cmd .= " -I $dir ";
			}

			$cmd .= " -DTQOBJECTDEFS_H $currentfile";

			open( INPUT, "$cmd |" )
				|| croak "Can't preprocess $currentfile";
		}
		else {
			open( INPUT, "tqt-replace-stream $currentfile |" )
				|| croak "Can't read from $currentfile";
		}

		print STDERR "$exe: processing $currentfile\n" unless $tquiet;

		# reset vars
		$rootNode = getRoot( $lang );


		# add to file lookup table
		my $showname = $striphpath ? basename( $currentfile )
						: $currentfile;
		$cSourceNode = Ast::New( $showname );
		$cSourceNode->AddProp( "NodeType", "source" );
		$cSourceNode->AddProp( "Path", $currentfile );
		$rootNode->AddPropList( "Sources", $cSourceNode );

		# reset state
		@classStack = ();
		$cNode = $rootNode;
		$inExtern = 0;
		$inNamespace = 0;

		# parse
		my $k = undef;
		while ( defined ($k = readDecl()) ) {
			print "\nDecl: <$k>[$declNodeType]\n" if $debug;
			if( identifyDecl( $k ) && $k =~ /{/ ) {
				readCxxCodeBlock();
			} 
		}
		close INPUT;
	}
}


sub writeDocumentation
{
	foreach my $node ( values %rootNodes ) {
		# postprocess
		kdocAstUtil::linkNamespaces( $node );
		kdocAstUtil::makeInherit( $node, $node );
		kdocAstUtil::linkReferences( $node, $node );
		kdocAstUtil::calcStats( \%stats, $node, $node );

		# write
		no strict "refs";
		foreach my $format ( @formats_wanted ) {
			my $pack = $formats{ $format };
			require $pack.".pm";

			print STDERR "Generating bindings for $format ",
				          "language...\n" unless $tquiet;

			my $f = "$pack\::writeDoc";
			&$f( $libname, $node, $outputdir, \%options );
		}
	}
}

sub writeLibrary
{
	if( $libname ne "" and !exists $options{'no-cache'} ) {
		require kdocLib;
		foreach my $lang ( keys %rootNodes ) {
			my $node = $rootNodes{ $lang };
			kdocLib::writeDoc( $libname, $node, $lang, $libdir,
					$outputdir, $options{url},
					exists $options{compress} ? 1 : 0 );
		}
	}
}

###### Parser routines

=head2 readSourceLine

	Returns a raw line read from the current input file.
	This is used by routines outside main, since I don t know
	how to share fds.

=cut

sub readSourceLine
{
	return <INPUT>;
}

=head2 readCxxLine

	Reads a C++ source line, skipping comments, blank lines,
	preprocessor tokens and the Q_OBJECT macro

=cut

sub readCxxLine
{
	my( $p );
	my( $l );
	
	while( 1 ) {
		$p = shift @inputqueue || <INPUT>;
		return undef if !defined ($p);

		$p =~ s#//.*$##g;			# C++ comment
		$p =~ s#/\*(?!\*).*?\*/##g;		# C comment

		# join all multiline comments
		if( $p =~ m#/\*(?!\*)#s ) {
			# unterminated comment
LOOP:
			while( defined ($l = <INPUT>) ) {
				$l =~ s#//.*$##g;		# C++ comment
				$p .= $l;
				$p =~ s#/\*(?!\*).*?\*/##sg;	# C comment
				last LOOP unless $p =~ m#(/\*(?!\*))|(\*/)#sg;
			}
		}

		if ( $p =~ /^\s*Q_OBJECT/ ) {
			if ($qt_embedded) {
				push @inputqueue, @qte_codeqobject;
			} elsif ($qt4) {
				push @inputqueue, @qt4_codeqobject;
			} else {
				push @inputqueue, @codeqobject;
			}
			next;
		}
		# Hack, waiting for real handling of preprocessor defines
		$p =~ s/QT_MODULE\(\w+\)//;
		$p =~ s/QT_STATIC_CONST/static const/;
		$p =~ s/QT_WEAK_SYMBOL//;
		$p =~ s/QT_MOC_COMPAT//;
		$p =~ s/Q_EXPORT_CODECS_BIG5//;
		$p =~ s/QT_COMPAT / /;
		$p =~ s/TQ_DISABLE_COPY\((\w+)\)/$1(const $1 &);\n$1 &operator=(const $1 &);/;
		$p =~ s/TQWIDGETSIZE_MAX/32767/; # Qt/E uses this #define as an enum value - yuck!
		$p =~ s/Q_SIGNALS/signals/;
		$p =~ s/ASYNC/void/;
		$p =~ s/[A-Z_]*_EXPORT_DEPRECATED//;
		$p =~ s/[A-Z_]*_EXPORT\s/ /;
		$p =~ s/EXPORT_DOCKCLASS//;
		$p =~ s/DLL_IMP_EXP_KMDICLASS//;
		$p =~ s/KSVG_GET/KJS::Value get();/;
		$p =~ s/KSVG_BASECLASS_GET/KJS::Value get();/;
		$p =~ s/KSVG_BRIDGE/KJS::ObjectImp *bridge();/;
		$p =~ s/KSVG_FORWARDGET/KJS::Value getforward();/;
		$p =~ s/KSVG_PUT/bool put();/;
		$p =~ s/KSVG_FORWARDPUT/bool putforward();/;
		$p =~ s/KSVG_BASECLASS/virtual KJS::Value cache();/;
		if ( $p =~ m/KSVG_DEFINE_PROTOTYPE\((\w+)\)/ ) {
			push @inputqueue, split('\n',"namespace KSVG {\nclass $1 {\n};\n};");
		}
		# Bother the same again for KDOM :/
		$p =~ s/KDOM_GET/KJS::Value get();/;
		$p =~ s/KDOM_BASECLASS_GET/KJS::Value get();/;
		$p =~ s/KDOM_FORWARDGET/KJS::Value getforward();/;
		$p =~ s/KDOM_PUT/bool put();/;
		$p =~ s/KDOM_FORWARDPUT/bool putforward();/;
		$p =~ s/KDOM_BASECLASS/virtual KJS::Value cache();/;
		$p =~ s/KDOM_CAST/KJS::Value cast();/;
		if ( $p =~ m/KDOM_DEFINE_PROTOTYPE\((\w+)\)/ ) {
			push @inputqueue, split('\n',"namespace KDOM {\nclass $1 {\n};\n};");
		}


		next if ( $p =~ /^\s*$/s ); 		# blank lines
#			|| $p =~ /^\s*Q_OBJECT/		# TQObject macro
#			);
#

		next if ( $p =~ /^\s*Q_ENUMS/           # ignore Q_ENUMS
                        || $p =~ /^\s*TQ_ENUMS/       # and TQ_ENUMS
                        || $p =~ /^\s*TQ_OBJECT/       # and TQ_OBJECT
                        || $p =~ /^\s*Q_FLAGS/       # and Q_FLAGS
                        || $p =~ /^\s*TQ_FLAGS/       # and TQ_FLAGS
                        || $p =~ /^\s*Q_DECLARE_FLAGS/       # and Q_DECLARE_FLAGS
                        || $p =~ /^\s*TQ_DECLARE_FLAGS/       # and TQ_DECLARE_FLAGS
                        || ( !$qt4 && $p =~ /^\s*Q_PROPERTY/ )      # and Q_PROPERTY
                        || ( !$qt4 && $p =~ /^\s*TQ_PROPERTY/ )      # and TQ_PROPERTY
                        || $p =~ /^\s*TQDOC_PROPERTY/
                        || $p =~ /^\s*Q_GADGET/
                        || $p =~ /^\s*Q_OVERRIDE/       # and Q_OVERRIDE
                        || $p =~ /^\s*TQ_OVERRIDE/       # and TQ_OVERRIDE
                        || $p =~ /^\s*Q_SETS/
                        || $p =~ /^\s*TQ_SETS/
                        || $p =~ /^\s*Q_DUMMY_COMPARISON_OPERATOR/
                        || $p =~ /^\s*K_SYCOCATYPE/     # and K_SYCOCA stuff
                        || $p =~ /^\s*K_SYCOCAFACTORY/  #
                        || $p =~ /^\s*KSVG_/            # and KSVG stuff ;)
						|| $p =~ /^\s*KDOM_/			# and KDOM stuff :(
						|| $p =~ /^\s*Q_DECLARE_FLAGS/
						|| $p =~ /^\s*Q_DECLARE_OPERATORS_FOR_FLAGS/
						|| $p =~ /^\s*Q_DECLARE_PRIVATE/
						|| $p =~ /^\s*Q_DECLARE_TYPEINFO/
						|| $p =~ /^\s*Q_PRIVATE_SLOT/
						|| $p =~ /^\s*Q_DECLARE_SHARED/
			);

		push @includes_list, $1 if $p =~ /^#include\s+<?(.*?)>?\s*$/;

		# remove all preprocessor macros
		if( $p =~ /^\s*#\s*(\w+)/ ) {
			# Handling of preprocessed sources: skip anything included from
			# other files, unless --docincluded was passed.
			if (!$docincluded && $p =~ /^\s*#\s*[0-9]+\s*\".*$/ 
					&& not($p =~ /\"$currentfile\"/)) {
				# include file markers
				while( <INPUT> ) {
					last if(/\"$currentfile\"/);
					print "Overread $_" if $debug;
				};
				print "Cont: $_" if $debug;
			}
			else {
				# Skip platform-specific stuff, or #if 0 stuff
				# or #else of something we parsed (e.g. for TQKeySequence)
				if ( $p =~ m/^#\s*ifdef\s*Q_WS_/ or
				     $p =~ m/^#\s*if\s+defined\(Q_WS_/ or
				     ($p =~ m/^#\s*ifdef\s+_WS_QWS_/ and $qt_embedded) or
				     ($p =~ m/^#\s*ifndef\s+QT_NO_MIMECLIPBOARD/ and $qt_embedded) or
				     ($p =~ m/^#\s*if\s+defined\(_WS_X11_/ and $qt_embedded) or
				     ($p =~ m/^#\s*if\s+defined\(Q_WS_X11_/ and $qt_embedded) or
				     ($p =~ m/^#\s*if\s+defined\(Q_WS_WIN_/ and $qt_embedded) or
				     ($p =~ m/^#\s*if\s+defined\(_WS_MAC_/ and $qt_embedded) or
				     ($p =~ m/^#\s*if\s+defined\(Q_INCOMPATIBLE_3_0_ADDONS/ and $qt_embedded) or
				     $p =~ m/^#\s*ifndef\s+QT_NO_STL/ or
				     $p =~ m/^#\s*if\s+defined\s*\(Q_OS_/ or
				     $p =~ m/^#\s*if\s+defined\(Q_CC_/ or
				     $p =~ m/^#\s*if\s+defined\(QT_THREAD_SUPPORT/ or
				     $p =~ m/^#\s*else/ or
				     $p =~ m/^#\s*if\s+defined\(Q_FULL_TEMPLATE_INSTANTIATION/ or
				     $p =~ m/^#\s*ifdef\s+QT_WORKSPACE_WINDOWMODE/ or
				     $p =~ m/^#\s*ifdef\s+QT_COMPAT/ or
				     $p =~ m/^#\s*if\s+defined\s*\(?QT_COMPAT/ or
				     $p =~ m/^#\s*ifdef\s+CONTAINER_CUSTOM_WIDGETS/ or
				     $p =~ m/^#\s*ifdef\s+QT3_SUPPORT/ or
				     $p =~ m/^#\s*ifdef\s+Q_MOC_RUN/ or
				     $p =~ m/^#\s*if\s+defined\s*\(QT3_SUPPORT/ or
				     $p =~ m/^#\s*if\s+defined\s*\(qdoc/ or
				     $p =~ m/^#\s*ifndef\s+QT_NO_MEMBER_TEMPLATES/ or
				     $p =~ m/^#if\s*!defined\(Q_NO_USING_KEYWORD\)/ or
				     &$match_qt_defines( $p ) or
				     $p =~ m/^#\s*if\s+0\s+/ ) {
				     my $if_depth = 1;
				     while ( defined $p && $if_depth > 0 ) {
					 $p = <INPUT>;
					 last if !defined $p;
					 $if_depth++ if $p =~ m/^#\s*if/;
					 $if_depth-- if $p =~ m/^#\s*endif/;
					 # Exit at #else in the #ifdef QT_NO_ACCEL/#else/#endif case
					 last if $if_depth == 1 && $p =~ m/^#\s*else\s/;
					 #ignore elif for now
					 print "Skipping ifdef'ed line: $p" if $debug;
				     }
				}

				# multiline macros
				while ( defined $p && $p =~ m#\\\s*$# ) {
					$p = <INPUT>;
				}
			}
			next;
		}

		$lastLine = $p;
		return $p;
	}
}

=head2 readCxxCodeBlock

	Reads a C++ code block (recursive curlies), returning the last line
	or undef on error.

	Parameters: none

=cut

sub readCxxCodeBlock
{
# Code: begins in a {, ends in }\s*;?
# In between: cxx source, including {}
	my ( $count ) = 0;
	my $l = undef;
	
	if ( defined $lastLine ) {
		print "lastLine: '$lastLine'" if $debug;

		my $open = kdocUtil::countReg( $lastLine, "{" );
		my $close = kdocUtil::countReg( $lastLine, "}" );
		$count = $open - $close;

		return $lastLine if ( $open || $close) && $count == 0;
	}

	# find opening brace
	if ( $count == 0 ) {
		while( $count == 0 ) {
			$l = readCxxLine();
			return undef if !defined $l;
			$l =~ s/\\.//g;
			$l =~ s/'.?'//g;
			$l =~ s/".*?"//g;

			$count += kdocUtil::countReg( $l, "{" );
			print "c ", $count, " at '$l'" if $debug;
		}
		$count -= kdocUtil::countReg( $l, "}" );
	}

	# find associated closing brace
	while ( $count > 0 ) {
		$l = readCxxLine();
		croak "Confused by unmatched braces" if !defined $l;
		$l =~ s/\\.//g;
		$l =~ s/'.?'//g;
		$l =~ s/".*?"//g;

		my $add = kdocUtil::countReg( $l, "{" );
		my $sub = kdocUtil::countReg( $l, "}" );
		$count += $add - $sub;

		print "o ", $add, " c ", $sub, " at '$l'" if $debug;
	}

	undef $lastLine;
	return $l;
}

=head2 readDecl

	Returns a declaration and sets the $declNodeType variable.

	A decl starts with a type or keyword and ends with [{};]
	The entire decl is returned in a single line, sans newlines.

	declNodeType values: undef for error, "a" for access specifier,
	"c" for doc comment, "d" for other decls.

	readCxxLine is used to read the declaration.

=cut

sub readDecl
{
	undef $declNodeType;
	my $l = readCxxLine();
	my ( $decl ) = "";

	my $allowed_accesors = "private|public|protected|signals";
	$allowed_accesors .= "|$allowed_k_dcop_accesors_re" if $allow_k_dcop_accessors;

	if( !defined $l ) {
		return undef;
	}
	elsif ( $l =~ /^\s*($allowed_accesors)
		       (\s+\w+)?\s*:/x) { # access specifier
		$declNodeType = "a";
		return $l;
	}
        elsif ( $l =~ /K_DCOP/ ) {
		$declNodeType = "k";
		return $l;
        }
	elsif ( $l =~ m#^\s*/\*\*# ) {	# doc comment
		$declNodeType = "c";
		return $l;
	}
	elsif ( $l =~ /Q_PROPERTY/ ) {	# property
		return $l;
	}
	elsif ( $l =~ /TQ_PROPERTY/ ) {  # property
		return $l;
	}

	do {
		$decl .= $l;

		if ( $l =~ /[{};]/ ) {
			$decl =~ s/\n/ /gs;
			$declNodeType = "d";
			return $decl;
		}
		return undef if !defined ($l = readCxxLine());

	} while ( 1 );
}

#### AST Generator Routines

=head2 getRoot

	Return a root node for the given type of input file.

=cut

sub getRoot
{
	my $type = shift;
	carp "getRoot called without type" unless defined $type;

	if ( !exists $rootNodes{ $type } ) {
		my $node = Ast::New( "Global" );	# parent of all nodes
		$node->AddProp( "NodeType", "root" );
		$node->AddProp( "RootType", $type );
		$node->AddProp( "Compound", 1 );
		$node->AddProp( "KidAccess", "public" );

		$rootNodes{ $type } = $node;
	}
	print "getRoot: call for $type\n" if $debug;

	return $rootNodes{ $type };
}

=head2 identifyDecl

	Parameters: decl

	Identifies a declaration returned by readDecl. If a code block
	needs to be skipped, this subroutine returns a 1, or 0 otherwise.

=cut

sub identifyDecl
{
	my( $decl ) = @_;

	my $newNode = undef;
	my $skipBlock = 0;
	my $isDeprecated = 0;
	
	if ( $decl =~ s/KDE_DEPRECATED// ) {
		$isDeprecated = 1;
	}
	# Doc comment
	if ( $declNodeType eq "c" ) {
		$docNode = kdocParseDoc::newDocComment( $decl );

		# if it's the main doc, it is attached to the root node
		if ( defined $docNode->{LibDoc} ) {
			kdocParseDoc::attachDoc( $rootNode, $docNode,
				$rootNode );
			undef $docNode;
		}

	}
	elsif ( $declNodeType eq "a" ) {
		newAccess( $decl );
	}
        elsif ( $declNodeType eq "k" ) {
	        $cNode->AddProp( "DcopExported", 1 );
        }
        # properties
        elsif ( $decl =~ s/Q_PROPERTY// ) {
		print "Property: <$1>\n" if $debug;

		$newNode = newProperty( $decl );
	}
        elsif ( $decl =~ s/TQ_PROPERTY// ) {
                print "Property: <$1>\n" if $debug;

                $newNode = newProperty( $decl );
        }
	# Typedef struct/class
	elsif ( $decl =~ /^\s*typedef
			\s+(struct|union|class|enum)
			\s*([_\w\:]*)
			\s*([;{]) 
			/xs ) {
		my ($type, $name, $endtag, $rest ) = ($1, $2, $3, $' );
		$name = "--" if $name eq "";

		warn "typedef '$type' n:'$name'\n" if $debug;

		if ( $rest =~ /}\s*([\w_]+(?:::[\w_])*)\s*;/ ) {
			# TODO: Doesn't parse members yet!
			$endtag = ";";
			$name = $1;
		}

		$newNode = newTypedefComp( $type, $name, $endtag );
	}

	# Typedef
	elsif ( $decl =~ /^\s*typedef\s+
			(?:typename\s+)?        # `typename' keyword
			(.*?\s*[\*&>]?)		# type
			\s*([-\w_\:]+)		# name
			\s*((?:\[[-\w_\:<>\s]*\])*)	# array
			\s*[{;]\s*$/xs  ) {

		print "Typedef: <$1 $3> <$2>\n" if $debug;
		$newNode = newTypedef( $1." ".$3, $2 );
	}

	# Enum
	elsif ( $decl =~ /^\s*enum\s+([-\w_:]*)?\s*\{(.*)/s  ) {

		print "Enum: <$1>\n" if $debug;
		my $enumname = defined $2 ? $1 : "";

		$newNode = newEnum( $enumname );
	}

	# Class/Struct
	elsif ( $decl =~ /^\s*((?:template\s*<.*>)?)      # 1 template
					\s*(class|struct|union|namespace) # 2 struct type
					(?:\s*TQ[A-Z_]*EXPORT[A-Z_]*)?
					(?:\s*Q[A-Z_]*EXPORT[A-Z_]*)?
					(?:\s*TQ_PACKED)?
					(?:\s*Q_REFCOUNT)?
					\s+([\w_]+                        # 3 name
							(?:<[\w_ :,]+?>)?		  # maybe explicit template
									#    (eat chars between <> non-hungry)
							(?:::[\w_]+)*                 #   maybe nested
					   )
					([^\(]*?)                             # 4 inheritance
					([;{])/xs ) {                     # 5 rest

		print "Class: [$1]\n\t[$2]\n\t[$3]\n\t[$4]\n\t[$5]\n" if $debug;
		my ( $tmpl, $ntype, $name, $rest, $endtag ) =
			( $1, $2, $3, $4, $5 );

		if ($includeclasses)
		{
		    if (! ($includeclasses =~ /$name/) )
		    {
			return 1;
			
		    }
		}
		if ($ntype eq 'namespace') {
			if ($decl =~ /}/) {
				return 0;
			}
			# Set a flag to indicate we're in a multi-line namespace declaration
			$inNamespace = 1;
		}
		
		my @inherits = ();

		$tmpl =~ s/<(.*)>/$1/ if $tmpl ne "";

		if(  $rest =~ /^\s*:\s*/ ) {
			# inheritance 
			$rest = $';
			@inherits = parseInheritance( $rest );
		}

		$newNode = newClass( $tmpl, $ntype, 
			$name, $endtag, $isDeprecated, @inherits );

		if ($decl =~ /};/) {
			# If the declaration was all on one line ending with a '};', 
			# then pop the new node
			$cNode = pop @classStack;
			print "end decl: popped $cNode->{astNodeName}\n" 
				if $debug;
		}
	}
	# IDL compound node
	elsif( $decl =~ /^\s*(module|interface|exception) # struct type
			\s+([-\w_]+)			# name
			(.*?)				# inheritance?
			([;{])/xs ) {
		
		my ( $type, $name, $rest, $fwd, $complete ) 
			= ( $1, $2, $3, $4 eq ";" ? 1 : 0,
				0 );
		my @in = ();
		print "IDL: [$type] [$name] [$rest] [$fwd]\n" if $debug;

		if( $rest =~ /^\s*:\s*/ ) {
			$rest = $';
			$rest =~ s/\s+//g;
			@in = split ",", $rest;
		}
		if( $decl =~ /}\s*;/ ) {
			$complete = 1;
		}

		$newNode = newIDLstruct( $type, $name, $fwd, $complete, @in );
	}
	# Method
	elsif ( $decl =~ /^\s*(?:(?:class|struct)\s*)?([^=]+?(?:operator\s*(?:\(\)|.?=)\s*)?) # ret+nm
		\( (.*?) \)		# parameters
		\s*((?:const)?)\s*
		(?:throw\s*\(.*?\))?
		\s*((?:=\s*0(?:L?))?)\s*	# Pureness. is "0L" allowed?
		\s*[;{]+/xs ) {	# rest

		my $tpn = $1; # type + name
		my $params = $2;
		# Remove constructor initializer, that's not in the params
		if ( $params =~ /\s*\)\s*:\s*/ ) {
			# Hack: first .* made non-greedy for TQSizePolicy using a?(b):c in ctor init
			$params =~ s/(.*?)\s*\)\s*:\s*.*$/$1/;
		}

		my $const = $3 eq "" ? 0 : 1;
		my $pure = $4 eq "" ? 0 : 1;
		$tpn =~ s/\s+/ /g;
		$tpn =~ s/operator\s+([^\w])/operator$1/g;
		$params =~ s/\s+/ /g;

		print "Method: R+N:[$tpn]\n\tP:[$params]\n\t[$const]\n" if $debug;

		if ( $tpn =~ /((?:\w+\s*::\s*)?operator.*?)\s*$/	# operator
					|| $tpn =~ /((?:\w*\s*::\s*~?)?[-\w:]+)\s*$/  ) { # normal
				my $name = $1;
				$tpn = $`;
				$newNode = newMethod( $tpn, $name, 
								$params, $const, $pure, $isDeprecated );
		}

		$skipBlock = 1; # FIXME check end token before doing this!
	}
	# Using: import namespace
	elsif ( $decl =~ /^\s*using\s+namespace\s+(\w+)/ ) {
		newNamespace( $1 );

	}

	# extern block
	elsif ( $decl =~ /^\s*extern\s*"(.*)"\s*{/ ) {
		$inExtern = 1 unless $decl =~ /}/;
	}

	# Single variable
	elsif ( $decl =~ /^
			\s*( (?:[\w_:]+(?:\s+[\w_:]+)*? )# type
				\s*(?:<.+>)?		# template
				\s*(?:[\&\*])?		# ptr or ref
				(?:\s*(?:const|volatile))* )
			\s*([\w_:]+)			# name
			\s*( (?:\[[^\[\]]*\] (?:\s*\[[^\[\]]*\])*)? ) # array
			\s*((?:=.*)?)			# value
			\s*([;{])\s*$/xs ) {
		my $type = $1;
		my $name = $2;
		my $arr  = $3;
		my $val	 = $4;
		my $end	 = $5;

		$type =~ s/\s+/ /g;

		if ( $type !~ /^friend\s+class\s*/ && $type.$name ne "struct" ) {
			print "Var: [$name] type: [$type$arr] val: [$val]\n"
				if $debug;

			$newNode = newVar( $type.$arr, $name, $val );
		}

		$skipBlock = 1 if $end eq '{';
	}

	# Multi variables
	elsif ( $decl =~ m/^
		\s*( (?:[\w_:]+(?:\s+[\w_:]+)*? )	# type
		\s*(?:<.+>)?)						# template

		\s*( (?:\s*(?: [\&\*][\&\*\s]*)? 	# ptr or ref
			[\w_:]+)						# name
		\s*(?:\[[^\[\]]*\] (?:\s*\[[^\[\]]*\])*)? # array
		\s*(?:,								# extra vars
			\s*(?: [\&\*][\&\*\s]*)? 		# ptr or ref
			\s*(?:[\w_:]+)					# name
			\s*(?:\[[^\[\]]*\] (?:\s*\[[^\[\]]*\])*)? # array
			)* 
		\s*(?:=.*)?)						# value
		\s*[;]/xs ) {

		my $type = $1;
		my $names = $2;
		my $end = $3;
		my $doc = $docNode;

		print "Multivar: type: [$type] names: [$names] \n" if $debug;

		foreach my $vardecl ( split( /\s*,\s*/, $names ) ) {
			next unless $vardecl =~ m/
			\s*((?: [\&\*][\&\*\s]*)?) 	# ptr or ref
			\s*([\w_:]+)			# name
			\s*( (?:\[[^\[\]]*\] (?:\s*\[[^\[\]]*\])*)? ) # array
			\s*((?:=.*)?)			# value
				/xs;
			my ($ptr, $name, $arr, $val) = ($1, $2, $3, $4);

			print "Split: type: [$type$ptr$arr] ",
				" name: [$name] val: [$val] \n" if $debug;

			my $node = newVar( $type.$ptr.$arr, $name, $val );

			$docNode = $doc;	# reuse docNode for each
			postInitNode( $node ) unless !defined $node;
		}

		$skipBlock = 1 if $end eq '{';
	}
	# end of an "extern" block
	elsif ( $decl =~ /^\s*}\s*$/ && $inExtern ) {
		$inExtern = 0;
	}
	# end of an in-block declaration
	elsif ( $decl =~ /^\s*}\s*(.*?)\s*;\s*$/ || ($decl =~ /^\s*}\s*$/ && $inNamespace) ) {
		if ( $cNode->{astNodeName} eq "--" ) {
			# structure typedefs should have no name preassigned.
			# If they do, then the name in 
			# "typedef struct <name> { ..." is kept instead.
			# TODO: Buglet. You should fix YOUR code dammit. ;)


			$cNode->{astNodeName} = $1;
			my $siblings = $cNode->{Parent}->{KidHash};
			undef $siblings->{"--"};
			$siblings->{ $1 } = $cNode;
		}

		# C++ namespaces end with a '}', and not '};' like classes
		if ($decl =~ /^\s*}\s*$/ ) {
			$inNamespace = 0;
		}
		
		if ( $#classStack < 0 ) {
			confess "close decl found, but no class in stack!" ;
			$cNode = $rootNode;
		}
		else {
			$cNode = pop @classStack;
			print "end decl: popped $cNode->{astNodeName}\n" 
				if $debug;
		}
	}
	# unidentified block start
	elsif ( $decl =~ /{/ ) {
		print "Unidentified block start: $decl\n" if $debug;
		$skipBlock = 1;
	}
	# explicit template instantiation, or friend template
	elsif ( $decl =~ /(template|friend)\s+class\s+(?:TQ[A-Z_]*EXPORT[A-Z_]*\s*)?\w+\s*<.*>\s*;/x ) {
		# Nothing to be done with those.
	}
	# explicit template instantiation, or friend template
	elsif ( $decl =~ /(template|friend)\s+class\s+(?:Q[A-Z_]*EXPORT[A-Z_]*\s*)?\w+\s*<.*>\s*;/x ) {
		# Nothing to be done with those (same as above, but for QT not TQT).
	}
	else {

		## decl is unidentified.
		warn "Unidentified decl: $decl\n";
	}

	# once we get here, the last doc node is already used.
	# postInitNode should NOT be called for forward decls
	postInitNode( $newNode ) unless !defined $newNode;

	return $skipBlock;
}

sub postInitNode
{
	my $newNode = shift;

	carp "Cannot postinit undef node." if !defined $newNode;

	# The reasoning here:
	# Forward decls never get a source node.
	# Once a source node is defined, don't assign another one.

	if ( $newNode->{NodeType} ne "Forward" && !defined $newNode->{Source}) {
		$newNode->AddProp( "Source", $cSourceNode );
	} elsif ( $newNode->{NodeType} eq "Forward" ) {
		if ($debug) {
			print "postInit: skipping fwd: $newNode->{astNodeName}\n";
		}
		undef $docNode;
		return;
	}

	if( defined $docNode ) {
		kdocParseDoc::attachDoc( $newNode, $docNode, $rootNode );
		undef $docNode;
	}
}


##### Node generators

=head2 newEnum

	Reads the parameters of an enumeration.

	Returns the parameters, or undef on error.

=cut

sub newEnum
{
	my ( $enum ) = @_;
	my $k = undef;
	my $params = "";

	$k = $lastLine if defined $lastLine;

	if( defined $lastLine && $lastLine =~ /{/ ) {
		$params = $';
		if ( $lastLine =~ /}(.*?);/ ) {
			return initEnum( $enum, $1, $params );
		}
	}

	while ( defined ( $k = readCxxLine() ) ) {
		$params .= $k;

		if ( $k =~ /}(.*?);/ ) {
			return initEnum( $enum, $1, $params );
		}
	}

	return undef;
}

=head2 initEnum

	Parameters: name, (ref) params

	Returns an initialized enum node.

=cut

sub initEnum
{
	my( $name, $end, $params ) = @_;

	($name = $end) if $name eq "" && $end ne "";

	$params =~ s#\s+# #sg; # no newlines
	$params =~ s#\s*/\*([^\*]/|\*[^/]|[^\*/])*\*/##g; # strip out comments
	$params = $1 if $params =~ /^\s*{?(.*)}/;
	$params =~ s/,\s*$/ /;
	print "$name params: [$params]\n" if $debug;

	my ( $node ) = Ast::New( $name );
	$node->AddProp( "NodeType", "enum" );
	$node->AddProp( "Params", $params );
	$node->AddProp( "Source", $cSourceNode );
	makeParamList( $node, $params, 1 ); # Adds the ParamList property containing the list of param nodes
	kdocAstUtil::attachChild( $cNode, $node );

	return $node;
}

=head2 newIDLstruct

	Parameters: type, name, forward, complete, inherits...

	Handles an IDL structure definition (ie module, interface,
	exception).

=cut

sub newIDLstruct
{
	my ( $type, $name, $fwd, $complete ) = @_;

	my $node = exists $cNode->{KidHash} ? 
		$cNode->{KidHash}->{ $name } : undef;

	if( !defined $node ) {
		$node = Ast::New( $name );
		$node->AddProp( "NodeType", $fwd ? "Forward" : $type );
		$node->AddProp( "KidAccess", "public" );
		$node->AddProp( "Compound", 1 ) unless $fwd;
		kdocAstUtil::attachChild( $cNode, $node );
	}
	elsif ( $fwd ) {
		# If we have a node already, we ignore forwards.
		return undef;
	}
	elsif ( $node->{NodeType} eq "Forward" ) {
		# we are defining a previously forward node.
		$node->AddProp( "NodeType", $type );
		$node->AddProp( "Compound", 1 );
		$node->AddProp( "Source", $cSourceNode );
	}

	# register ancestors.
	foreach my $ances ( splice ( @_, 4 ) ) {
		my $n = kdocAstUtil::newInherit( $node, $ances );
	}

	if( !( $fwd || $complete) ) {
		print "newIDL: pushing $cNode->{astNodeName},",
			" new is $node->{astNodeName}\n"
				if $debug;
		push @classStack, $cNode;
		$cNode = $node;
	}

	return $node;
}

=head2 newClass

	Parameters: tmplArgs, cNodeType, name, endTag, isDeprecated, @inheritlist

	Handles a class declaration (also fwd decls).

=cut

sub newClass
{
	my( $tmplArgs, $cNodeType, $name, $endTag, $isDeprecated ) = @_;

	my $access = "private";
	$access = "public" if $cNodeType ne "class";

	# try to find an exisiting node, or create a new one
	my $oldnode = kdocAstUtil::findRef( $cNode, $name );
	my $node = defined $oldnode ? $oldnode : Ast::New( $name );

	if ( $endTag ne "{" ) {
		# forward
		if ( !defined $oldnode ) {
			# new forward node
			$node->AddProp( "NodeType", "Forward" );
			$node->AddProp( "KidAccess", $access );
			kdocAstUtil::attachChild( $cNode, $node );
		}
		# Discard any doc comment against a forward decl
		undef $docNode;
		return $node;
	}

	# this is a class declaration

	print "ClassName: $name\n" if $debug;

	$node->AddProp( "NodeType", $cNodeType );
	$node->AddProp( "Compound", 1 );
	$node->AddProp( "Source", $cSourceNode );
	
	if ($cNodeType eq 'namespace') {
		$node->AddPropList( "Sources", $cSourceNode );
	}

	$node->AddProp( "KidAccess", $access );
	$node->AddProp( "Tmpl", $tmplArgs ) unless $tmplArgs eq "";

	$node->AddProp( "Deprecated", $isDeprecated );

	if ( !defined $oldnode ) {
		kdocAstUtil::attachChild( $cNode, $node );
	}

	# inheritance

	foreach my $ances ( splice (@_, 5) ) {
		my $type = "";
		my $name = $ances;
		my $intmpl = undef;

WORD:
		foreach my $word ( split ( /([\w:]+(:?\s*<.*>)?)/, $ances ) ) {
			next WORD unless $word =~ /^[\w:]/;
			if ( $word =~ /(private|public|protected|virtual)/ ) {
				$type .= "$1 ";
			}
			else {
				
				if ( $word =~ /<(.*)>/ ) {
					# FIXME: Handle multiple tmpl args
					$name = $`;
					$intmpl = $1;
				}
				else {
					$name = $word;
				}

				last WORD;
			}
		}

		# set inheritance access specifier if none specified
		if ( $type eq "" ) {
			$type = $cNodeType eq "class" ? "private ":"public ";
		}
		chop $type;

		# attach inheritance information
		my $n = kdocAstUtil::newInherit( $node, $name );
		$n->AddProp( "Type", $type );

		$n->AddProp( "TmplType", $intmpl ) if defined $intmpl;

		print "In: $name type: $type, tmpl: $intmpl\n" if $debug;
	}

	# new current node
	print "newClass: Pushing $cNode->{astNodeName}\n" if $debug;
	push ( @classStack, $cNode );
	$cNode = $node;

	return $node;
}


=head3 parseInheritance

	Param: inheritance decl string
	Returns: list of superclasses (template decls included)

	This will fail if < and > appear in strings in the decl.

=cut

sub parseInheritance
{
	my $instring = shift;
	my @inherits = ();

	my $accum = "";
	foreach $instring ( split (/\s*,\s*/, $instring) ) {
		$accum .= $instring.", ";
		next unless  (kdocUtil::countReg( $accum, "<" )
			- kdocUtil::countReg( $accum, ">" ) ) == 0;

		# matching no. of < and >, so assume the parent is
		# complete
		$accum =~ s/,\s*$//;
		print "Inherits: '$accum'\n" if $debug;
		push @inherits, $accum;
		$accum = "";
	}

	return @inherits;
}


=head2 newNamespace

	Param: namespace name.
	Returns nothing.

	Imports a namespace into the current node, for ref searches etc.
	Triggered by "using namespace ..."

=cut

sub newNamespace
{
	$cNode->AddPropList( "ImpNames", shift );
}



=head2 newTypedef

	Parameters: realtype, name

	Handles a type definition.

=cut

sub newTypedef
{
	my ( $realtype, $name ) = @_;

	my ( $node ) = Ast::New( $name );

	$node->AddProp( "NodeType", "typedef" );
	$node->AddProp( "Type", $realtype );

	kdocAstUtil::attachChild( $cNode, $node );

	return $node;
}

=head2 newTypedefComp

	Params: realtype, name endtoken

	Creates a new compound type definition.

=cut

sub newTypedefComp
{
	my ( $realtype, $name, $endtag ) = @_;

	my ( $node ) = Ast::New( $name );

	$node->AddProp( "NodeType", "typedef" );
	$node->AddProp( "Type", $realtype );

	kdocAstUtil::attachChild( $cNode, $node );

	if ( $endtag eq '{' ) {
		print "newTypedefComp: Pushing $cNode->{astNodeName}\n" 
			if $debug;
		push ( @classStack, $cNode );
		$cNode = $node;
	}

	return $node;
}


=head2 newMethod

	Parameters: retType, name, params, const, pure?, deprecated?

	Handles a new method declaration or definition.

=cut
BEGIN {

my $theSourceNode = $cSourceNode;

sub newMethod
{
	my ( $retType, $name, $params, $const, $pure, $deprecated ) = @_;
	my $parent = $cNode;
	my $class;

	print "Cracked: [$retType] [$name]\n\t[$params]\n\t[$const]\n" 
		if $debug;

	if ( $retType =~ /([\w\s_<>,]+)\s*::\s*$/ ) {
		# check if stuff before :: got into rettype by mistake.
		$retType = $`;
		($name = $1."::".$name);
		$name =~ s/\s+/ /g;
		print "New name = \"$name\" and type = '$retType'\n" if $debug;
	}

	# A 'friend method' declaration isn't a real method declaration
	return undef if ( $retType =~ /^friend\s+/ || $retType =~ /^friend\s+class\s+/ );

	my $isGlobalSpace = 0;

	if( $name =~ /^\s*(.*?)\s*::\s*(.*?)\s*$/ ) {
		# Fully qualified method name.
		$name = $2;
		$class = $1;

		if( $class =~ /^\s*$/ ) {
			$parent = $rootNode;
		}
		elsif ( $class eq $cNode->{astNodeName} ) {
			$parent = $cNode;
		}
		else {
			# ALWAYS IGNORE...
			return undef;
			
			my $node = kdocAstUtil::findRef( $cNode, $class );
			
			if ( !defined $node ) {
				# if we couldn't find the name, try again with
				# all template parameters stripped off:
				my $strippedClass = $class;
				$strippedClass =~ s/<[^<>]*>//g;

				$node = kdocAstUtil::findRef( $cNode, $strippedClass );

				# if still not found: give up
				if ( !defined $node ) {
						warn "$exe: Unidentified class: $class ".
								"in $currentfile\:$.\n";
						return undef;
				}
			}

			$parent = $node;
		}
	}
	# TODO  fix for $retType =~ /template<.*?>/
	elsif( $parse_global_space && $parent->{NodeType} eq "root" && $name !~ /\s*qt_/ && $retType !~ /template\s*<.*?>/ ) {
	    $class = $globalSpaceClassName; # FIXME - sanitize the naming system?
	    $isGlobalSpace = 1;

	    my $opsNode = kdocAstUtil::findRef( $cNode, $class );
	    if (!$opsNode) {
		# manually create a "GlobalSpace" class
		$opsNode = Ast::New( $class );
		$opsNode->AddProp( "NodeType", "class" );
		$opsNode->AddProp( "Compound", 1 );
		$opsNode->AddProp( "Source", $cSourceNode ); # dummy
		$opsNode->AddProp( "KidAccess", "public" );
		kdocAstUtil::attachChild( $cNode, $opsNode );
	    }
		# Add a special 'Source' property for methods in global space
		$cNode->AddProp( "Source", $theSourceNode ); 
		
            unless( $theSourceNode == $cSourceNode ) {
                $theSourceNode = $cSourceNode;
                $opsNode->AddPropList( "Sources", $theSourceNode ); # sources are scattered across Qt
            }
	    $parent = $opsNode;
	} 

	# flags

	my $flags = "";

	if( $retType =~ /static/ || $isGlobalSpace ) {
		$flags .= "s";
		$retType =~ s/static//g;
	}

	if( $const && !$isGlobalSpace ) {
		$flags .= "c";
	}

	if( $pure ) {
		$flags .= "p";
	}

	if( $retType =~ /virtual/ ) {
		$flags .= "v";
		$retType =~ s/virtual//g;
	}

	if( $retType =~ /explicit\s*/ ) {
		$flags .= "t";
		$retType =~ s/explicit\s*//g;
	}

	print "\n" if $flags ne "" && $debug;

	if ( !defined $parent->{KidAccess} ) {
		warn "'", $parent->{astNodeName}, "' has no KidAccess ",
		exists $parent->{Forward} ? "(forward)\n" :"\n";
	}

        # NB, these are =~, so make sure they are listed in correct order
	if ( $parent->{KidAccess} =~ /slot/ ) {
		$flags .= "l";
	}
	elsif ( $parent->{KidAccess} =~ /k_dcop_signals/ ) {
		$flags .= "z";
	}
	elsif ( $parent->{KidAccess} =~ /k_dcop_hidden/ ) {
		$flags .= "y";
	}
	elsif ( $parent->{KidAccess} =~ /k_dcop/ ) {
		$flags .= "d";
	}
	elsif ( $parent->{KidAccess} =~ /signal/ ) {
		$flags .= "n";
	}

	$retType =~ s/TQM?_EXPORT[_A-Z]*\s*//;
	$retType =~ s/QM?_EXPORT[_A-Z]*\s*//;
	$retType =~ s/inline\s+//;
	$retType =~ s/extern\s+//;
	$retType =~ s/^\s*//g;
	$retType =~ s/\s*$//g;
	$retType =~ s/^class\s/ /;  # Remove redundant class forward decln's
	$retType =~ s/<class\s/</;

	# node
	
	my $node = Ast::New( $name );
	$node->AddProp( "NodeType", "method" );
	$node->AddProp( "Flags", $flags );
	$node->AddProp( "ReturnType", $retType );
	$node->AddProp( "Params", $params ); # The raw string with the whole param list
	makeParamList( $node, $params, 0 ); # Adds the ParamList property containing the list of param nodes
	
	$node->AddProp( "Deprecated", $deprecated );

	$parent->AddProp( "Pure", 1 ) if $pure;

	kdocAstUtil::attachChild( $parent, $node );

	return $node;
}

}

=head2 makeParamList

	Parameters:
	* method (or enum) node
	* string containing the whole param list
	* 1 for enums

	Adds a property "ParamList" to the method node.
	This property contains a list of nodes, one for each parameter.

	Each parameter node has the following properties:
	* ArgType the type of the argument, e.g. const TQString&
	* ArgName the name of the argument - optionnal
	* DefaultValue the default value of the argument - optionnal

	For enum values, ArgType is unset, ArgName is the name, DefaultValue its value.

	Author: David Faure <david@mandrakesoft.com>
=cut

sub makeParamList($$$)
{
	my ( $methodNode, $params, $isEnum ) = @_;
	$params =~ s/\s+/ /g; # normalize multiple spaces/tabs into a single one
	$params =~ s/\s*([\*\&])\s*/$1 /g; # normalize spaces before and after *, &
	$params =~ s/\s*(,)([^'\s])\s*/$1 $2/g; # And after ',', but not if inside single quotes 
	$params =~ s/^\s*void\s*$//; # foo(void) ==> foo()
	$params =~ s/^\s*$//;
	# Make sure the property always exists, makes iteration over it easier
        $methodNode->AddProp( "ParamList", [] );

	my @args = kdocUtil::splitUnnested( ',', $params);

    	my $argId = 0;
	foreach my $arg ( @args ) {
		my $argType;
		my $argName;
		my $defaultparam;
		$arg =~ s/\s*([^\s].*[^\s])\s*/$1/; # stripWhiteSpace
		$arg =~ s/(\w+)\[\]/\* $1/; # Turn [] array into *
		$arg =~ s/^class //; # Remove any redundant 'class' forward decln's

		# The RE below reads as: = ( string constant or char or cast to numeric literal
		# or some word/number, with optional bitwise shifts, OR'ed or +'ed flags, and/or function call ).
		if ( $arg =~ s/\s*=\s*(("[^\"]*")|\([^)]*\)\s*[\+-]?\s*[0-9]+|(\'.\')|(([-\w:~]*)\s*([<>\|\+-]*\s*[\w:._]*\s*)*(\([^(]*\))?))// ) {
			$defaultparam = $1;
		}
		
		if (defined $defaultparam && $isEnum) {
			# Remove any casts in enum values, for example this in tdefileitem.h:
			#  'enum { Unknown = (mode_t) - 1 };'
			$defaultparam =~ s/\([^\)]+\)(.*[0-9].*)/$1/;
		}

		# Separate arg type from arg name, if the latter is specified
		if ( $arg =~ /(.*)\s+([\w_]+)\s*$/ || $arg =~ /(.*)\(\s*\*\s([\w_]+)\)\s*\((.*)\)\s*$/ ) {
                    if ( defined $3 ) { # function pointer
                        $argType = $1."(*)($3)";
                        $argName = $2;
                    } else {
		        $argType = $1;
		        $argName = $2;
                    }
		} else { # unnamed arg - or enum value
		    $argType = $arg if (!$isEnum);
		    $argName = $arg if ($isEnum);
		}
		$argId++;
		
		my $node = Ast::New( $argId ); # let's make the arg index the node "name"
		$node->AddProp( "NodeType", "param" );
		$node->AddProp( "ArgType", $argType );
		$node->AddProp( "ArgName", $argName ) if (defined $argName);
		$node->AddProp( "DefaultValue", $defaultparam ) if (defined $defaultparam);
		$methodNode->AddPropList( "ParamList", $node );
		print STDERR "ArgType: $argType ArgName: $argName\n" if ($debug);
	}
}

=head2 newAccess

	Parameters: access

	Sets the default "Access" specifier for the current class node. If
	the access is a "slot" type, "_slots" is appended to the access
	string.

=cut

sub newAccess
{
	my ( $access ) = @_;

	return undef unless ($access =~ /^\s*(\w+)\s*(slots|$allowed_k_dcop_accesors_re)?/);

	print "Access: [$1] [$2]\n" if $debug;

	$access = $1;

	if ( defined $2 && $2 ne "" ) {
		$access .= "_" . $2;
	}

	$cNode->AddProp( "KidAccess", $access );

	return $cNode;
}


=head2 newVar

	Parameters: type, name, value

	New variable. Value is ignored if undef

=cut

sub newVar
{
	my ( $type, $name, $val ) = @_;

	my $node = Ast::New( $name );
	$node->AddProp( "NodeType", "var" );

	my $static = 0;
	if ( $type =~ /static/ ) {
	#	$type =~ s/static//;
		$static = 1;
	}

	$node->AddProp( "Type", $type );
	$node->AddProp( "Flags", 's' ) if $static;
	$node->AddProp( "Value", $val ) if defined $val;
	kdocAstUtil::attachChild( $cNode, $node );

	return $node;
}

=head2 newProperty

	Parameters: property

	Handles a property

=cut

sub newProperty
{
	my ( $property ) = @_;

    $property =~ s/^\s+|\s+$//g;  
    my @items = split(/ /,$property);

    do {
	    my ( $node ) = Ast::New( $items[1] );

	    $node->AddProp( "NodeType", "property" );
        $node->AddProp( "type", $items[0] );
        $node->AddProp( "READ", $items[3] );
        $node->AddProp( "WRITE", $items[5] );
        $node->AddProp( "NOTIFY", $items[7] );

        $cNode->{KidAccess} = "public";
	    kdocAstUtil::attachChild( $cNode, $node );
        return $node;

    } if defined $items[1];
}


=head2 show_usage

	Display usage information and quit.

=cut

sub show_usage
{
print<<EOF;
usage:
	$exe [options] [-f format] [-d outdir] [-n name] files... [-llib..]

See the man page kdoc[1] for more info.
EOF
	exit 1;
}


=head2 show_version

	Display short version information and quit.

=cut

sub show_version
{
	die "$exe: $Version (c) Sirtaj S. Kang <taj\@kde.org>\n";
}