winapi_parser.pm 18 KB

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