Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
cprint.ml1 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(* cprint -- pretty printer of C program from abstract syntax *) open Cabs let version = "Cprint 4.0 Hugues Cassé et al." (* ** FrontC Pretty printer *) let out = ref stdout let width = ref 80 let tab = ref 8 let max_indent = ref 60 let line = ref "" let line_len = ref 0 let current = ref "" let current_len = ref 0 let spaces = ref 0 let follow = ref 0 let roll = ref 0 let print_tab size = output_string !out (String.make (size / 8) '\t'); output_string !out (String.make (size mod 8) ' ') let flush _ = if !line <> "" then begin print_tab (!spaces + !follow); output_string !out !line; line := ""; line_len := 0 end let commit _ = if !current <> "" then begin if !line = "" then begin line := !current; line_len := !current_len end else begin line := (!line ^ " " ^ !current); line_len := !line_len + 1 + !current_len end; current := ""; current_len := 0 end let new_line _ = commit (); if !line <> "" then begin flush (); output_char !out '\n' end; follow := 0 let force_new_line _ = commit (); flush (); output_char !out '\n'; follow := 0 let indent _ = new_line (); spaces := !spaces + !tab; if !spaces >= !max_indent then begin spaces := !tab; roll := !roll + 1 end let unindent _ = new_line (); spaces := !spaces - !tab; if (!spaces <= 0) && (!roll > 0) then begin spaces := ((!max_indent - 1) / !tab) * !tab; roll := !roll - 1 end let space _ = commit () let print str = current := !current ^ str; current_len := !current_len + (String.length str); if (!spaces + !follow + !line_len + 1 + !current_len) > !width then begin if !line_len = 0 then commit (); flush (); output_char !out '\n'; if !follow = 0 then follow := !tab end (* ** Useful primitives *) let print_commas nl fct lst = let _ = List.fold_left (fun com elt -> if com then begin print ","; if nl then new_line () else space () end else (); fct elt; true) false lst in () let escape_string str = let lng = String.length str in let conv value = String.make 1 (Char.chr (value + (if value < 10 then (Char.code '0') else (Char.code 'a' - 10)))) in let rec build idx = if idx >= lng then "" else let sub = String.sub str idx 1 in let res = match sub with "\n" -> "\\n" | "\"" -> "\\\"" | "'" -> "\\'" | "\r" -> "\\r" | "\t" -> "\\t" | "\b" -> "\\b" | "\000" -> "\\0" | _ -> if sub = (Char.escaped (String.get sub 0)) then sub else let code = Char.code (String.get sub 0) in "\\" ^ (conv (code / 64)) ^ (conv ((code mod 64) / 8)) ^ (conv (code mod 8)) in res ^ (build (idx + 1)) in build 0 let rec has_extension attrs = match attrs with [] -> false | GNU_EXTENSION::_ -> true | _::attrs -> has_extension attrs (* ** Base Type Printing *) let get_sign si = match si with NO_SIGN -> "" | SIGNED -> "signed " | UNSIGNED -> "unsigned " let get_size siz = match siz with NO_SIZE -> "" | SHORT -> "short " | LONG -> "long " | LONG_LONG -> "long long " let rec print_base_type typ = match typ with NO_TYPE -> () | VOID -> print "void" | BOOL -> print "_Bool" | CHAR sign -> print ((get_sign sign) ^ "char") | INT (size, sign) -> print ((get_sign sign) ^ (get_size size) ^ "int") | BITFIELD (sign, _) -> print ((get_sign sign) ^ "int") | FLOAT size -> print ((if size then "long " else "") ^ "float") | DOUBLE size -> print ((if size then "long " else "") ^ "double") | COMPLEX_FLOAT -> print "float _Complex" | COMPLEX_DOUBLE -> print "double _Complex" | COMPLEX_LONG_DOUBLE -> print "long double _Complex" | NAMED_TYPE id -> print id | ENUM (id, items) -> print_enum id items | STRUCT (id, flds) -> print_fields ("struct " ^ id) flds | UNION (id, flds) -> print_fields ("union " ^ id) flds | PROTO (typ, _, _) -> print_base_type typ | OLD_PROTO (typ, _, _) -> print_base_type typ | PTR typ -> print_base_type typ | RESTRICT_PTR typ -> print_base_type typ | ARRAY (typ, _) -> print_base_type typ | CONST typ -> print_base_type typ | VOLATILE typ -> print_base_type typ | GNU_TYPE (attrs, typ) -> print_attributes attrs; print_base_type typ | BUILTIN_TYPE t -> print t | TYPE_LINE (_, _, _type) -> print_base_type _type and print_fields id (flds : name_group list) = print id; if flds = [] then () else begin print " {"; indent (); List.iter (fun fld -> print_name_group fld; print ";"; new_line ()) flds; unindent (); print "}" end and print_enum id items = print ("enum " ^ id); if items = [] then () else begin print " {"; indent (); print_commas true (fun (id, exp) -> print id; if exp = NOTHING then () else begin space (); print "= "; print_expression exp 1 end) items; unindent (); print "}"; end (* ** Declaration Printing *) and get_base_type typ = match typ with PTR typ -> get_base_type typ | RESTRICT_PTR typ -> get_base_type typ | CONST typ -> get_base_type typ | VOLATILE typ -> get_base_type typ | ARRAY (typ, _) -> get_base_type typ | _ -> typ and print_pointer typ = match typ with PTR typ -> print_pointer typ; print "*" | RESTRICT_PTR typ -> print_pointer typ; print "* __restrict"; space () | CONST typ -> print_pointer typ; print " const " | VOLATILE typ -> print_pointer typ; print " volatile " | ARRAY (typ, _) -> print_pointer typ | _ -> (*print_base_type typ*) () and print_array typ = match typ with ARRAY (typ, dim) -> print_array typ; print "["; print_expression dim 0; print "]" | _ -> () (** Print a type. @param fct Function called to display the name of the. @param typ Type to display. *) and print_type (fct : unit -> unit) (typ : base_type ) = let base = get_base_type typ in match base with BITFIELD (_, exp) -> fct (); print " : "; print_expression exp 1 | PROTO (typ', pars, ell) -> print_type (fun _ -> if base <> typ then print "("; print_pointer typ; fct (); print_array typ; if base <> typ then print ")"; print "("; print_params pars ell; print ")") typ' | OLD_PROTO (typ', pars, ell) -> print_type (fun _ -> if base <> typ then print "("; print_pointer typ; fct (); print_array typ; if base <> typ then print ")"; print "("; print_old_params pars ell; print ")") typ' | _ -> print_pointer typ; fct (); print_array typ and print_onlytype typ = print_base_type typ; print_type (fun _ -> ()) typ and print_name ((id, typ, attr, exp) : name) = print_type (fun _ -> print id) typ; print_attributes attr; if exp <> NOTHING then begin space (); print "= "; print_expression exp 1 end else () and get_storage sto = match sto with NO_STORAGE -> "" | AUTO -> "auto" | STATIC -> "static" | EXTERN -> "extern" | REGISTER -> "register" and print_name_group (typ, sto, names) = let extension = List.exists (fun (_, _, attrs, _) -> has_extension attrs) names in if extension then begin print "__extension__"; space () end; if sto <> NO_STORAGE then begin print (get_storage sto); space () end; print_base_type typ; space (); print_commas false print_name names and print_single_name (typ, sto, name) = if sto <> NO_STORAGE then begin print (get_storage sto); space () end; print_base_type typ; space (); print_name name and print_params (pars : single_name list) (ell : bool) = print_commas false print_single_name pars; if ell then print (if pars = [] then "..." else ", ...") else () and print_old_params pars ell = print_commas false (fun id -> print id) pars; if ell then print (if pars = [] then "..." else ", ...") else () (* ** Expression printing ** Priorities ** 16 variables ** 15 . -> [] call() ** 14 ++, -- (post) ** 13 ++ -- (pre) ~ ! - + & *(cast) ** 12 * / % ** 11 + - ** 10 << >> ** 9 < <= > >= ** 8 == != ** 7 & ** 6 ^ ** 5 | ** 4 && ** 3 || ** 2 ? : ** 1 = ?= ** 0 , *) and get_operator exp = match exp with NOTHING -> ("", 16) | UNARY (op, _) -> (match op with MINUS -> ("-", 13) | PLUS -> ("+", 13) | NOT -> ("!", 13) | BNOT -> ("~", 13) | MEMOF -> ("*", 13) | ADDROF -> ("&", 13) | PREINCR -> ("++", 13) | PREDECR -> ("--", 13) | POSINCR -> ("++", 14) | POSDECR -> ("--", 14)) | BINARY (op, _, _) -> (match op with MUL -> ("*", 12) | DIV -> ("/", 12) | MOD -> ("%", 12) | ADD -> ("+", 11) | SUB -> ("-", 11) | SHL -> ("<<", 10) | SHR -> (">>", 10) | LT -> ("<", 9) | LE -> ("<=", 9) | GT -> (">", 9) | GE -> (">=", 9) | EQ -> ("==", 8) | NE -> ("!=", 8) | BAND -> ("&", 7) | XOR -> ("^", 6) | BOR -> ("|", 5) | AND -> ("&&", 4) | OR -> ("||", 3) | ASSIGN -> ("=", 1) | ADD_ASSIGN -> ("+=", 1) | SUB_ASSIGN -> ("-=", 1) | MUL_ASSIGN -> ("*=", 1) | DIV_ASSIGN -> ("/=", 1) | MOD_ASSIGN -> ("%=", 1) | BAND_ASSIGN -> ("&=", 1) | BOR_ASSIGN -> ("|=", 1) | XOR_ASSIGN -> ("^=", 1) | SHL_ASSIGN -> ("<<=", 1) | SHR_ASSIGN -> (">>=", 1)) | QUESTION _ -> ("", 2) | CAST _ -> ("", 13) | CALL _ -> ("", 15) | COMMA _ -> ("", 0) | CONSTANT _ -> ("", 16) | VARIABLE _ -> ("", 16) | EXPR_SIZEOF _ -> ("", 16) | TYPE_SIZEOF _ -> ("", 16) | INDEX _ -> ("", 15) | MEMBEROF _ -> ("", 15) | MEMBEROFPTR _ -> ("", 15) | GNU_BODY _ -> ("", 17) | DESIGNATED _ -> ("", 15) | EXPR_LINE (expr, _, _) -> get_operator expr and print_comma_exps exps = print_commas false (fun exp -> print_expression exp 1) exps and print_expression (exp : expression) (lvl : int) = let (txt, lvl') = get_operator exp in let _ = if lvl > lvl' then print "(" else () in let _ = match exp with NOTHING -> () | UNARY (op, exp') -> (match op with POSINCR | POSDECR -> print_expression exp' lvl'; print txt | _ -> print txt; print_expression exp' lvl') | BINARY (_, exp1, exp2) -> (*if (op = SUB) && (lvl <= lvl') then print "(";*) print_expression exp1 lvl'; space (); print txt; space (); (*print_expression exp2 (if op = SUB then (lvl' + 1) else lvl');*) print_expression exp2 (lvl' + 1) (*if (op = SUB) && (lvl <= lvl') then print ")"*) | QUESTION (exp1, exp2, exp3) -> print_expression exp1 2; space (); print "? "; print_expression exp2 2; space (); print ": "; print_expression exp3 2; | CAST (typ, exp) -> print "("; print_onlytype typ; print ")"; print_expression exp 15 | CALL (exp, args) -> print_expression exp 16; print "("; print_comma_exps args; print ")" | COMMA exps -> print_comma_exps exps | CONSTANT cst -> print_constant cst | VARIABLE name -> print name | EXPR_SIZEOF exp -> print "sizeof("; print_expression exp 0; print ")" | TYPE_SIZEOF typ -> print "sizeof("; print_onlytype typ; print ")" | INDEX (exp, idx) -> print_expression exp 16; print "["; print_expression idx 0; print "]" | MEMBEROF (exp, fld) -> print_expression exp 16; print ("." ^ fld) | MEMBEROFPTR (exp, fld) -> print_expression exp 16; print ("->" ^ fld) | GNU_BODY (decs, stat) -> print "("; print_statement (BLOCK (decs, stat)); print ")" | DESIGNATED (member, exp) -> print "."; print member; print "="; print_expression exp 16; | EXPR_LINE (expr, _, _) -> print_expression expr lvl in if lvl > lvl' then print ")" else () and print_constant cst = match cst with CONST_INT i -> print i | CONST_FLOAT r -> print r | CONST_CHAR c -> print ("'" ^ (escape_string c) ^ "'") | CONST_STRING s -> print ("\"" ^ (escape_string s) ^ "\"") | CONST_COMPOUND exps -> begin print "{"; print_comma_exps exps; print "}" end (* ** Statement printing *) and print_statement stat = match stat with NOP -> print ";"; new_line () | COMPUTATION exp -> print_expression exp 0; print ";"; new_line () | BLOCK (defs, stat) -> new_line (); print "{"; indent (); print_defs defs; if stat <> NOP then print_statement stat else (); unindent (); print "}"; new_line (); | SEQUENCE (s1, s2) -> print_statement s1; print_statement s2; | IF (exp, s1, s2) -> print "if("; print_expression exp 0; print ")"; print_substatement s1; if s2 = NOP then () else begin print "else"; print_substatement s2; end | WHILE (exp, stat) -> print "while("; print_expression exp 0; print ")"; print_substatement stat | DOWHILE (exp, stat) -> print "do"; print_substatement stat; print "while("; print_expression exp 0; print ");"; new_line (); | FOR (exp1, exp2, exp3, stat) -> print "for("; print_expression exp1 0; print ";"; space (); print_expression exp2 0; print ";"; space (); print_expression exp3 0; print ")"; print_substatement stat | BREAK -> print "break;"; new_line () | CONTINUE -> print "continue;"; new_line () | RETURN exp -> print "return"; if exp = NOTHING then () else begin print " "; print_expression exp 1 end; print ";"; new_line () | SWITCH (exp, stat) -> print "switch("; print_expression exp 0; print ")"; print_substatement stat | CASE (exp, stat) -> unindent (); print "case "; print_expression exp 1; print ":"; indent (); print_substatement stat | DEFAULT stat -> unindent (); print "default :"; indent (); print_substatement stat | LABEL (name, stat) -> print (name ^ ":"); space (); print_substatement stat | GOTO name -> print ("goto " ^ name ^ ";"); new_line () | ASM desc -> print ("asm(\"" ^ (escape_string desc) ^ "\");") | GNU_ASM (desc, output, input, mods) -> print ("asm(" ^ (escape_string desc) ^ "\""); print " : "; print_commas false print_gnu_asm_arg output; print " : "; print_commas false print_gnu_asm_arg input; if mods <> [] then begin print " : "; print_commas false print mods end; print ");" | STAT_LINE (stat, _, _) -> print_statement stat and print_gnu_asm_arg (id, desc, exp) = if id <> "" then print ("[" ^ id ^ "]"); print ("\"" ^ (escape_string desc) ^ "\"("); print_expression exp 0; print ("\"") and print_substatement stat = match stat with IF _ | SEQUENCE _ | DOWHILE _ -> new_line (); print "{"; indent (); print_statement stat; unindent (); print "}"; new_line (); | BLOCK _ -> print_statement stat | _ -> indent (); print_statement stat; unindent () (* ** GCC Attributes *) and print_attributes attrs = match attrs with [] -> () | [GNU_EXTENSION] -> () | _ -> if attrs <> [] then begin print " __attribute__ (("; print_commas false print_attribute attrs; print ")) " end and print_attribute attr = match attr with GNU_NONE -> () | GNU_ID id -> print id | GNU_CALL (id, args) -> print id; print "("; print_commas false print_attribute args; print ")" | GNU_CST cst -> print_constant cst | GNU_EXTENSION -> print "__extension__" | GNU_INLINE -> print "__inline__" | GNU_TYPE_ARG (typ,sto) -> if sto <> NO_STORAGE then begin print (get_storage sto); space () end; print_base_type typ (* ** Declaration printing *) and print_defs defs = let prev = ref false in List.iter (fun def -> (match def with DECDEF _ -> prev := false | _ -> if not !prev then force_new_line (); prev := true); print_def def) defs and print_def def = match def with FUNDEF (proto, body) -> print_single_name proto; let (decs, stat) = body in print_statement (BLOCK (decs, stat)); force_new_line (); | OLDFUNDEF (proto, decs, body) -> print_single_name proto; force_new_line (); List.iter (fun dec -> print_name_group dec; print ";"; new_line ()) decs; let (decs, stat) = body in print_statement (BLOCK (decs, stat)); force_new_line (); | DECDEF names -> print_name_group names; print ";"; new_line () | TYPEDEF (names, attrs) -> if has_extension attrs then begin print "__extension__"; space (); end; print "typedef "; print_name_group names; print ";"; new_line (); force_new_line () | ONLYTYPEDEF names -> print_name_group names; print ";"; new_line (); force_new_line () (* print abstrac_syntax -> () ** Pretty printing the given abstract syntax program. *) let print (result : out_channel) (defs : file) = out := result; print_defs defs let set_tab t = tab := t let set_width w = width := w