winapi_parser.pm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. #
  2. # Copyright 1999, 2000, 2001 Patrik Stridvall
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2.1 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public
  15. # License along with this library; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  17. #
  18. package winapi_parser;
  19. use strict;
  20. use warnings 'all';
  21. use output qw($output);
  22. use options qw($options);
  23. # Defined a couple common regexp tidbits
  24. my $CALL_CONVENTION="__cdecl|__stdcall|" .
  25. "__RPC_API|__RPC_STUB|__RPC_USER|RPC_ENTRY|" .
  26. "RPC_VAR_ENTRY|STDMETHODCALLTYPE|NET_API_FUNCTION|" .
  27. "CALLBACK|CDECL|NTAPI|PASCAL|APIENTRY|" .
  28. "SEC_ENTRY|VFWAPI|VFWAPIV|WINGDIPAPI|WMIAPI|WINAPI|WINAPIV|";
  29. sub parse_c_file($$) {
  30. my $file = shift;
  31. my $callbacks = shift;
  32. my $empty_callback = sub { };
  33. my $c_comment_found_callback = $$callbacks{c_comment_found} || $empty_callback;
  34. my $cplusplus_comment_found_callback = $$callbacks{cplusplus_comment_found} || $empty_callback;
  35. my $function_create_callback = $$callbacks{function_create} || $empty_callback;
  36. my $function_found_callback = $$callbacks{function_found} || $empty_callback;
  37. my $type_create_callback = $$callbacks{type_create} || $empty_callback;
  38. my $type_found_callback = $$callbacks{type_found} || $empty_callback;
  39. my $preprocessor_found_callback = $$callbacks{preprocessor_found} || $empty_callback;
  40. # global
  41. my $debug_channels = [];
  42. my $in_function = 0;
  43. my $function_begin;
  44. my $function_end;
  45. {
  46. my $documentation_line;
  47. my $documentation;
  48. my $function_line;
  49. my $linkage;
  50. my $return_type;
  51. my $calling_convention;
  52. my $internal_name = "";
  53. my $argument_types;
  54. my $argument_names;
  55. my $argument_documentations;
  56. my $statements_line;
  57. my $statements;
  58. $function_begin = sub {
  59. $documentation_line = shift;
  60. $documentation = shift;
  61. $function_line = shift;
  62. $linkage = shift;
  63. $return_type= shift;
  64. $calling_convention = shift;
  65. $internal_name = shift;
  66. $argument_types = shift;
  67. $argument_names = shift;
  68. $argument_documentations = shift;
  69. if(defined($argument_names) && defined($argument_types) &&
  70. $#$argument_names == -1)
  71. {
  72. foreach my $n (0..$#$argument_types) {
  73. push @$argument_names, "";
  74. }
  75. }
  76. if(defined($argument_documentations) &&
  77. $#$argument_documentations == -1)
  78. {
  79. foreach my $n (0..$#$argument_documentations) {
  80. push @$argument_documentations, "";
  81. }
  82. }
  83. $in_function = 1;
  84. };
  85. $function_end = sub {
  86. $statements_line = shift;
  87. $statements = shift;
  88. my $function = &$function_create_callback();
  89. if(!defined($documentation_line)) {
  90. $documentation_line = 0;
  91. }
  92. $function->file($file);
  93. $function->debug_channels([@$debug_channels]);
  94. $function->documentation_line($documentation_line);
  95. $function->documentation($documentation);
  96. $function->function_line($function_line);
  97. $function->linkage($linkage);
  98. $function->return_type($return_type);
  99. $function->calling_convention($calling_convention);
  100. $function->internal_name($internal_name);
  101. if(defined($argument_types)) {
  102. $function->argument_types([@$argument_types]);
  103. }
  104. if(defined($argument_names)) {
  105. $function->argument_names([@$argument_names]);
  106. }
  107. if(defined($argument_documentations)) {
  108. $function->argument_documentations([@$argument_documentations]);
  109. }
  110. $function->statements_line($statements_line);
  111. $function->statements($statements);
  112. &$function_found_callback($function);
  113. $in_function = 0;
  114. };
  115. }
  116. my $in_type = 0;
  117. my $type_begin;
  118. my $type_end;
  119. {
  120. my $type;
  121. $type_begin = sub {
  122. $type = shift;
  123. $in_type = 1;
  124. };
  125. $type_end = sub {
  126. my $names = shift;
  127. foreach my $name (@$names) {
  128. if($type =~ /^(?:enum|interface|struct|union)/) {
  129. # $output->write("typedef $type {\n");
  130. # $output->write("} $name;\n");
  131. } else {
  132. # $output->write("typedef $type $name;\n");
  133. }
  134. }
  135. $in_type = 0;
  136. };
  137. }
  138. my %regs_entrypoints;
  139. my @comment_lines = ();
  140. my @comments = ();
  141. my $statements_line;
  142. my $statements;
  143. my $level = 0;
  144. my $extern_c = 0;
  145. my $again = 0;
  146. my $lookahead = 0;
  147. my $lookahead_count = 0;
  148. print STDERR "Processing file '$file' ... " if $options->verbose;
  149. open(IN, "< $file") || die "<internal>: $file: $!\n";
  150. local $_ = "";
  151. readmore: while($again || defined(my $line = <IN>)) {
  152. $_ = "" if !defined($_);
  153. if(!$again) {
  154. chomp $line;
  155. if($lookahead) {
  156. $lookahead = 0;
  157. $_ .= "\n" . $line;
  158. $lookahead_count++;
  159. } else {
  160. $_ = $line;
  161. $lookahead_count = 0;
  162. }
  163. $output->write(" $level($lookahead_count): $line\n") if $options->debug >= 2;
  164. $output->write("*** $_\n") if $options->debug >= 3;
  165. } else {
  166. $lookahead_count = 0;
  167. $again = 0;
  168. }
  169. # CVS merge conflicts in file?
  170. if(/^(<<<<<<<|=======|>>>>>>>)/) {
  171. $output->write("$file: merge conflicts in file\n");
  172. last;
  173. }
  174. my $prefix="";
  175. while ($_ ne "")
  176. {
  177. if (s/^([^\"\/]+|\"(?:[^\\\"]*|\\.)*\")//)
  178. {
  179. $prefix.=$1;
  180. }
  181. elsif (/^\/\*/)
  182. {
  183. # remove C comments
  184. if(s/^(\/\*.*?\*\/)//s) {
  185. my @lines = split(/\n/, $1);
  186. push @comment_lines, $.;
  187. push @comments, $1;
  188. &$c_comment_found_callback($. - $#lines, $., $1);
  189. if($#lines <= 0) {
  190. $_ = "$prefix $_";
  191. } else {
  192. $_ = $prefix . ("\n" x $#lines) . $_;
  193. }
  194. $again = 1;
  195. } else {
  196. $_ = "$prefix$_";
  197. $lookahead = 1;
  198. }
  199. next readmore;
  200. }
  201. elsif (s/^(\/\/.*)$//)
  202. {
  203. # remove C++ comments
  204. &$cplusplus_comment_found_callback($., $1);
  205. $again = 1;
  206. }
  207. elsif (s/^(.)//)
  208. {
  209. $prefix.=$1;
  210. }
  211. }
  212. $_=$prefix;
  213. # remove preprocessor directives
  214. if(s/^\s*\#/\#/s) {
  215. if (/^#\s*if\s+0\s*$/ms) {
  216. # Skip #if 0 ... #endif sections entirely.
  217. # They are typically used as 'super comments' and may not
  218. # contain C code. This totally ignores nesting.
  219. if(s/^(\s*#\s*if\s+0\s*\n.*?\n\s*#\s*endif\s*)\n//s) {
  220. my @lines = split(/\n/, $1);
  221. $_ = "\n" x $#lines;
  222. &$preprocessor_found_callback("if", "0");
  223. $again = 1;
  224. } else {
  225. $lookahead = 1;
  226. }
  227. next readmore;
  228. }
  229. elsif(/^(\#.*?)\\$/s) {
  230. $_ = "$1\n";
  231. $lookahead = 1;
  232. next;
  233. } elsif(s/^\#\s*(\w+)((?:\s+(.*?))?\s*)$//s) {
  234. my @lines = split(/\n/, $2);
  235. if($#lines > 0) {
  236. $_ = "\n" x $#lines;
  237. }
  238. if(defined($3)) {
  239. &$preprocessor_found_callback($1, $3);
  240. } else {
  241. &$preprocessor_found_callback($1, "");
  242. }
  243. $again = 1;
  244. next;
  245. }
  246. }
  247. # Remove extern "C"
  248. if(s/^\s*extern\s+"C"\s+\{//m) {
  249. $extern_c = 1;
  250. $again = 1;
  251. next;
  252. }
  253. my $documentation_line;
  254. my $documentation;
  255. my @argument_documentations = ();
  256. {
  257. my $n = $#comments;
  258. while($n >= 0 && ($comments[$n] !~ /^\/\*\*/ ||
  259. $comments[$n] =~ /^\/\*\*+\/$/))
  260. {
  261. $n--;
  262. }
  263. if(defined($comments[$n]) && $n >= 0) {
  264. my @lines = split(/\n/, $comments[$n]);
  265. $documentation_line = $comment_lines[$n] - scalar(@lines) + 1;
  266. $documentation = $comments[$n];
  267. for(my $m=$n+1; $m <= $#comments; $m++) {
  268. if($comments[$m] =~ /^\/\*\*+\/$/ ||
  269. $comments[$m] =~ /^\/\*\s*(?:\!)?defined/) # FIXME: Kludge
  270. {
  271. @argument_documentations = ();
  272. next;
  273. }
  274. push @argument_documentations, $comments[$m];
  275. }
  276. } else {
  277. $documentation = "";
  278. }
  279. }
  280. if($level > 0)
  281. {
  282. my $line = "";
  283. while(/^[^\{\}]/) {
  284. s/^([^\{\}\'\"]*)//s;
  285. $line .= $1;
  286. if(s/^\'//) {
  287. $line .= "\'";
  288. while(/^./ && !s/^\'//) {
  289. s/^([^\'\\]*)//s;
  290. $line .= $1;
  291. if(s/^\\//) {
  292. $line .= "\\";
  293. if(s/^(.)//s) {
  294. $line .= $1;
  295. if($1 eq "0") {
  296. s/^(\d{0,3})//s;
  297. $line .= $1;
  298. }
  299. }
  300. }
  301. }
  302. $line .= "\'";
  303. } elsif(s/^\"//) {
  304. $line .= "\"";
  305. while(/^./ && !s/^\"//) {
  306. s/^([^\"\\]*)//s;
  307. $line .= $1;
  308. if(s/^\\//) {
  309. $line .= "\\";
  310. if(s/^(.)//s) {
  311. $line .= $1;
  312. if($1 eq "0") {
  313. s/^(\d{0,3})//s;
  314. $line .= $1;
  315. }
  316. }
  317. }
  318. }
  319. $line .= "\"";
  320. }
  321. }
  322. if(s/^\{//) {
  323. $_ = $'; $again = 1;
  324. $line .= "{";
  325. print "+1: \{$_\n" if $options->debug >= 2;
  326. $level++;
  327. $statements .= $line;
  328. } elsif(s/^\}//) {
  329. $_ = $'; $again = 1;
  330. $line .= "}" if $level > 1;
  331. print "-1: \}$_\n" if $options->debug >= 2;
  332. $level--;
  333. if($level == -1 && $extern_c) {
  334. $extern_c = 0;
  335. $level = 0;
  336. }
  337. $statements .= $line;
  338. } else {
  339. $statements .= "$line\n";
  340. }
  341. if($level == 0) {
  342. if($in_function) {
  343. &$function_end($statements_line, $statements);
  344. $statements = undef;
  345. } elsif($in_type) {
  346. if(/^\s*((?:(?:FAR\s*)?\*\s*(?:RESTRICTED_POINTER\s+)?)?
  347. (?:volatile\s+)?
  348. (?:\w+|WS\(\w+\))\s*
  349. (?:\s*,\s*(?:(?:FAR\s*)?\*+\s*(?:RESTRICTED_POINTER\s+)?)?(?:volatile\s+)?(?:\w+|WS\(\w+\)))*\s*);/sx) {
  350. my @parts = split(/\s*,\s*/, $1);
  351. &$type_end([@parts]);
  352. } elsif(/;/s) {
  353. die "$file: $.: syntax error: '$_'\n";
  354. } else {
  355. $lookahead = 1;
  356. }
  357. }
  358. }
  359. next;
  360. } elsif(/(extern\s+|static\s+)?((interface\s+|struct\s+|union\s+|enum\s+|signed\s+|unsigned\s+)?\w+((\s*\*)+\s*|\s+))
  361. (($CALL_CONVENTION)\s+)?
  362. (?:DECLSPEC_HOTPATCH\s+)?
  363. (\w+(\(\w+\))?)\s*\((.*?)\)\s*(\{|\;)/sx)
  364. {
  365. my @lines = split(/\n/, $&);
  366. my $function_line = $. - scalar(@lines) + 1;
  367. $_ = $'; $again = 1;
  368. if($11 eq "{") {
  369. $level++;
  370. }
  371. my $linkage = $1;
  372. my $return_type = $2;
  373. my $calling_convention = $7;
  374. my $name = $8;
  375. my $arguments = $10;
  376. if(!defined($linkage)) {
  377. $linkage = "";
  378. }
  379. if(!defined($calling_convention)) {
  380. $calling_convention = "";
  381. }
  382. $linkage =~ s/\s*$//;
  383. $return_type =~ s/\s*$//;
  384. $return_type =~ s/\s*\*\s*/*/g;
  385. $return_type =~ s/(\*+)/ $1/g;
  386. if($regs_entrypoints{$name}) {
  387. $name = $regs_entrypoints{$name};
  388. }
  389. $arguments =~ y/\t\n/ /;
  390. $arguments =~ s/^\s*(.*?)\s*$/$1/;
  391. if($arguments eq "") { $arguments = "..." }
  392. my @argument_types;
  393. my @argument_names;
  394. my @arguments;
  395. my $n = 0;
  396. while ($arguments =~ s/^((?:[^,\(\)]*|(?:\([^\)]*\))?)+)(?:,|$)// && $1) {
  397. my $argument = $1;
  398. push @arguments, $argument;
  399. my $argument_type = "";
  400. my $argument_name = "";
  401. $argument =~ s/^\s*(.*?)\s*$/$1/;
  402. # print " " . ($n + 1) . ": '$argument'\n";
  403. $argument =~ s/^(?:IN OUT|IN|OUT)?\s+//;
  404. $argument =~ s/^(?:const|CONST|GDIPCONST|volatile)?\s+//;
  405. if($argument =~ /^\.\.\.$/) {
  406. $argument_type = "...";
  407. $argument_name = "...";
  408. } elsif($argument =~ /^
  409. ((?:interface\s+|struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)?
  410. (?:short\s+(?=int)|long\s+(?=int))?)?(?:\w+|ElfW\(\w+\)|WS\(\w+\)))\s*
  411. ((?:__RPC_FAR|const|CONST|GDIPCONST|volatile)?\s*(?:\*\s*(?:__RPC_FAR|const|CONST|volatile)?\s*?)*)\s*
  412. (\w*)\s*(\[\])?(?:\s+OPTIONAL)?$/x)
  413. {
  414. $argument_type = $1;
  415. if ($2) {
  416. $argument_type .= " $2";
  417. }
  418. if ($4) {
  419. $argument_type .= "$4";
  420. }
  421. $argument_name = $3;
  422. } elsif ($argument =~ /^
  423. ((?:interface\s+|struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)?
  424. (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
  425. ((?:const|volatile)?\s*(?:\*\s*(?:const|volatile)?\s*?)*)\s*
  426. (?:(?:$CALL_CONVENTION)\s+)?
  427. \(\s*(?:$CALL_CONVENTION)?\s*\*\s*((?:\w+)?)\s*\)\s*
  428. \(\s*(.*?)\s*\)$/x)
  429. {
  430. my $return_type = $1;
  431. if($2) {
  432. $return_type .= " $2";
  433. }
  434. $argument_name = $3;
  435. my $arguments = $4;
  436. $return_type =~ s/\s+/ /g;
  437. $arguments =~ s/\s*,\s*/,/g;
  438. $argument_type = "$return_type (*)($arguments)";
  439. } elsif ($argument =~ /^
  440. ((?:interface\s+|struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)
  441. (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
  442. ((?:const|volatile)?\s*(?:\*\s*(?:const|volatile)?\s*?)*)\s*
  443. (\w+)\s*\[\s*(.*?)\s*\](?:\[\s*(.*?)\s*\])?$/x)
  444. {
  445. my $return_type = $1;
  446. if($2) {
  447. $return_type .= " $2";
  448. }
  449. $argument_name = $3;
  450. $argument_type = "$return_type\[$4\]";
  451. if (defined($5)) {
  452. $argument_type .= "\[$5\]";
  453. }
  454. # die "$file: $.: syntax error: '$argument_type':'$argument_name'\n";
  455. } else {
  456. # This is either a complex argument type, typically
  457. # involving parentheses, or a macro argument. This is rare
  458. # so just ignore the 'function' declaration.
  459. print STDERR "$file: $.: cannot parse declaration argument (ignoring): '$argument'\n";
  460. next readmore;
  461. }
  462. $argument_type =~ s/\s*(?:const|volatile)\s*/ /g; # Remove const/volatile
  463. $argument_type =~ s/([^\*\(\s])\*/$1 \*/g; # Assure whitespace between non-* and *
  464. $argument_type =~ s/,([^\s])/, $1/g; # Assure whitespace after ,
  465. $argument_type =~ s/\*\s+\*/\*\*/g; # Remove whitespace between * and *
  466. $argument_type =~ s/([\(\[])\s+/$1/g; # Remove whitespace after ( and [
  467. $argument_type =~ s/\s+([\)\]])/$1/g; # Remove whitespace before ] and )
  468. $argument_type =~ s/\s+/ /; # Remove multiple whitespace
  469. $argument_type =~ s/^\s*(.*?)\s*$/$1/; # Remove leading and trailing whitespace
  470. $argument_name =~ s/^\s*(.*?)\s*$/$1/; # Remove leading and trailing whitespace
  471. $argument_types[$n] = $argument_type;
  472. $argument_names[$n] = $argument_name;
  473. # print " " . ($n + 1) . ": '" . $argument_types[$n] . "', '" . $argument_names[$n] . "'\n";
  474. $n++;
  475. }
  476. if($#argument_types == 0 && $argument_types[0] =~ /^void$/i) {
  477. $#argument_types = -1;
  478. $#argument_names = -1;
  479. }
  480. if($options->debug) {
  481. print "$file: $return_type $calling_convention $name(" . join(",", @arguments) . ")\n";
  482. }
  483. &$function_begin($documentation_line, $documentation,
  484. $function_line, $linkage, $return_type, $calling_convention, $name,
  485. \@argument_types,\@argument_names,\@argument_documentations);
  486. if($level == 0) {
  487. &$function_end(undef, undef);
  488. }
  489. $statements_line = $.;
  490. $statements = "";
  491. } elsif(/__ASM_GLOBAL_FUNC\(\s*(.*?)\s*,/s) {
  492. my @lines = split(/\n/, $&);
  493. my $function_line = $. - scalar(@lines) + 1;
  494. $_ = $'; $again = 1;
  495. &$function_begin($documentation_line, $documentation,
  496. $function_line, "", "void", "__asm", $1);
  497. &$function_end($., "");
  498. } elsif(/DEFINE_THISCALL_WRAPPER\((\S*)\)/s) {
  499. my @lines = split(/\n/, $&);
  500. my $function_line = $. - scalar(@lines) + 1;
  501. $_ = $'; $again = 1;
  502. &$function_begin($documentation_line, $documentation,
  503. $function_line, "", "void", "", "__thiscall_" . $1, \());
  504. &$function_end($function_line, "");
  505. } elsif(/DEFINE_REGS_ENTRYPOINT_\d+\(\s*(\S*)\s*,\s*([^\s,\)]*).*?\)/s) {
  506. $_ = $'; $again = 1;
  507. $regs_entrypoints{$2} = $1;
  508. } elsif(/DEFAULT_DEBUG_CHANNEL\s*\((\S+)\)/s) {
  509. $_ = $'; $again = 1;
  510. unshift @$debug_channels, $1;
  511. } elsif(/(DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\((\S+)\)/s) {
  512. $_ = $'; $again = 1;
  513. push @$debug_channels, $1;
  514. } elsif(/typedef\s+(enum|interface|struct|union)(?:\s+DECLSPEC_ALIGN\(\d+\))?(?:\s+(\w+))?\s*\{/s) {
  515. $_ = $'; $again = 1;
  516. $level++;
  517. my $type = $1;
  518. if(defined($2)) {
  519. $type .= " $2";
  520. }
  521. &$type_begin($type);
  522. } elsif(/typedef\s+
  523. ((?:const\s+|CONST\s+|enum\s+|interface\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+|volatile\s+)*?)
  524. (\w+)
  525. (?:\s+const|\s+volatile)?
  526. ((?:\s*(?:(?:FAR|__RPC_FAR|TW_HUGE)?\s*)?\*+\s*|\s+)(?:volatile\s+|DECLSPEC_ALIGN\(\d+\)\s+)?\w+\s*(?:\[[^\]]*\])*
  527. (?:\s*,\s*(?:\s*(?:(?:FAR|__RPC_FAR|TW_HUGE)?\s*)?\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?)*)
  528. \s*;/sx)
  529. {
  530. $_ = $'; $again = 1;
  531. my $type = "$1 $2";
  532. my @names;
  533. my @parts = split(/\s*,\s*/, $2);
  534. foreach my $part (@parts) {
  535. if($part =~ /(?:\s*((?:(?:FAR|__RPC_FAR|TW_HUGE)?\s*)?\*+)\s*|\s+)(\w+)\s*(\[[^\]]*\])?/) {
  536. my $name = $2;
  537. if(defined($1)) {
  538. $name = "$1$2";
  539. }
  540. if(defined($3)) {
  541. $name .= $3;
  542. }
  543. push @names, $name;
  544. }
  545. }
  546. &$type_begin($type);
  547. &$type_end([@names]);
  548. } elsif(/typedef\s+
  549. (?:(?:const\s+|enum\s+|interface\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+|volatile\s+)*?)
  550. (\w+(?:\s*\*+\s*)?)\s*
  551. (?:(\w+)\s*)?
  552. \((?:(\w+)\s*)?\s*(?:\*\s*(\w+)|_ATL_CATMAPFUNC)\s*\)\s*
  553. (?:\(([^\)]*)\)|\[([^\]]*)\])\s*;/sx)
  554. {
  555. $_ = $'; $again = 1;
  556. my $type;
  557. if(defined($2) || defined($3)) {
  558. my $cc = $2 || $3;
  559. if(defined($5)) {
  560. $type = "$1 ($cc *)($5)";
  561. } else {
  562. $type = "$1 ($cc *)[$6]";
  563. }
  564. } else {
  565. if(defined($5)) {
  566. $type = "$1 (*)($5)";
  567. } else {
  568. $type = "$1 (*)[$6]";
  569. }
  570. }
  571. my $name = $4;
  572. &$type_begin($type);
  573. &$type_end([$name]);
  574. } elsif(/typedef[^\{;]*;/s) {
  575. $_ = $'; $again = 1;
  576. $output->write("$file: $.: could not parse typedef: '$&'\n");
  577. } elsif(/typedef[^\{]*\{[^\}]*\}[^;];/s) {
  578. $_ = $'; $again = 1;
  579. $output->write("$file: $.: could not parse multi-line typedef: '$&'\n");
  580. } elsif(/\'[^\']*\'/s) {
  581. $_ = $'; $again = 1;
  582. } elsif(/\"(?:[^\\\"]*|\\.)*\"/s) {
  583. $_ = $'; $again = 1;
  584. } elsif(/;/s) {
  585. $_ = $'; $again = 1;
  586. } elsif(/extern\s+"C"\s+{/s) {
  587. $_ = $'; $again = 1;
  588. } elsif(/\{/s) {
  589. $_ = $'; $again = 1;
  590. print "+1: $_\n" if $options->debug >= 2;
  591. $level++;
  592. } else {
  593. $lookahead = 1;
  594. }
  595. }
  596. close(IN);
  597. print STDERR "done\n" if $options->verbose;
  598. $output->write("$file: not at toplevel at end of file\n") unless $level == 0;
  599. }
  600. 1;