ocarina-backends-c_tree-generator.adb 43.8 KB
Newer Older
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--    O C A R I N A . B A C K E N D S . C _ T R E E . G E N E R A T O R     --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--    Copyright (C) 2008-2009 Telecom ParisTech, 2010-2017 ESA & ISAE.      --
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
--                                                                          --
-- Ocarina  is free software; you can redistribute it and/or modify under   --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. Ocarina is distributed in the hope that it will be useful, but     --
-- WITHOUT ANY WARRANTY; without even the implied warranty of               --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
26
--                                                                          --
jhugues's avatar
jhugues committed
27 28
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
29 30 31
--                                                                          --
------------------------------------------------------------------------------

yoogx's avatar
yoogx committed
32 33 34 35
with Ocarina.Namet;  use Ocarina.Namet;
with Ocarina.Output; use Ocarina.Output;
with Utils;          use Utils;
with Outfiles;       use Outfiles;
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

with GNAT.OS_Lib; use GNAT.OS_Lib;

with Ocarina.Backends.Utils;
with Ocarina.Backends.C_Tree.Nodes;
with Ocarina.Backends.C_Tree.Nutils;
with Ocarina.Backends.C_Values;
with Ocarina.Backends.Messages;

package body Ocarina.Backends.C_Tree.Generator is

   use Ocarina.Backends.Utils;
   use Ocarina.Backends.C_Tree.Nodes;
   use Ocarina.Backends.C_Tree.Nutils;
   use Ocarina.Backends.C_Values;
   use Ocarina.Backends.Messages;

   procedure Generate_Define_Statement (N : Node_Id);
   procedure Generate_Pointer_Type (N : Node_Id);
   procedure Generate_Constant_Type (N : Node_Id);
   procedure Generate_Array_Declaration (N : Node_Id);
   procedure Generate_Base_Type (N : Node_Id);
   procedure Generate_C_Comment (N : Node_Id);
   procedure Generate_Call_Profile (N : Node_Id);
   procedure Generate_Macro_Call (N : Node_Id);
61
   procedure Generate_Doxygen_C_Comment (N : Node_Id);
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
   procedure Generate_HI_Distributed_Application (N : Node_Id);
   procedure Generate_HI_Node (N : Node_Id);
   procedure Generate_Assignment_Statement (N : Node_Id);
   procedure Generate_Defining_Identifier (N : Node_Id);
   procedure Generate_Expression (N : Node_Id);
   procedure Generate_Enum_Aggregate (N : Node_Id);
   procedure Generate_Array_Values (N : Node_Id);
   procedure Generate_Array_Value (N : Node_Id);
   procedure Generate_For_Statement (N : Node_Id);
   procedure Generate_Full_Type_Declaration (N : Node_Id);
   procedure Generate_Function_Implementation (N : Node_Id);
   procedure Generate_Function_Specification (N : Node_Id);
   procedure Generate_If_Statement (N : Node_Id);
   procedure Generate_Literal (N : Node_Id);
   procedure Generate_Member_Declaration (N : Node_Id);
   procedure Generate_Variable_Declaration (N : Node_Id);
   procedure Generate_Parameter (N : Node_Id);
   procedure Generate_Parameter_List (L : List_Id);
   procedure Generate_Return_Statement (N : Node_Id);
   procedure Generate_Struct_Aggregate (N : Node_Id);
   procedure Generate_Type_Conversion (N : Node_Id);
   procedure Generate_Union_Aggregate (N : Node_Id);
   procedure Generate_While_Statement (N : Node_Id);
   procedure Generate_Source_File (N : Node_Id);
   procedure Generate_Header_File (N : Node_Id);
   procedure Generate_HI_Unit (N : Node_Id);
   procedure Generate_Included_Files (N : List_Id);
   procedure Generate_Include_Clause (N : Node_Id);
   procedure Generate_Ifdef_Clause (N : Node_Id);
   procedure Generate_Variable_Address (N : Node_Id);
   procedure Generate_Member_Designator (N : Node_Id);
   procedure Generate_Switch_Alternative (N : Node_Id);
   procedure Generate_Switch_Statement (N : Node_Id);
   procedure Generate_Extern_Entity_Declaration (N : Node_Id);

   procedure Write (T : Token_Type);
   procedure Write_Line (T : Token_Type);

   procedure Generate_Statement_Delimiter (N : Node_Id);

   function Get_File_Name (N : Node_Id) return Name_Id;
   --  Generate a C file name from the package node given as parameter

   -------------------
   -- Get_File_Name --
   -------------------

   function Get_File_Name (N : Node_Id) return Name_Id is
      Header_Suffix : constant String := ".h";
      Source_Suffix : constant String := ".c";
   begin
      --  The File name corresponding is the lowerd name of N

      Get_Name_String
116
        (Conventional_Base_Name (Name (Defining_Identifier (N))));
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

      --  Adding file suffix

      if Kind (N) = K_Header_File then
         Add_Str_To_Name_Buffer (Header_Suffix);
      else
         Add_Str_To_Name_Buffer (Source_Suffix);
      end if;

      return Name_Find;
   end Get_File_Name;

   --------------
   -- Generate --
   --------------

   procedure Generate (N : Node_Id) is
   begin
      case Kind (N) is
         when K_Header_File =>
            Generate_Header_File (N);

         when K_Source_File =>
            Generate_Source_File (N);

         when K_C_Comment =>
            Generate_C_Comment (N);

         when K_HI_Distributed_Application =>
            Generate_HI_Distributed_Application (N);

         when K_HI_Unit =>
            Generate_HI_Unit (N);

         when K_HI_Node =>
            Generate_HI_Node (N);

         when K_Include_Clause =>
            Generate_Include_Clause (N);

         when K_Ifdef_Clause =>
            Generate_Ifdef_Clause (N);

         when K_Assignment_Statement =>
            Generate_Assignment_Statement (N);

163 164 165
         when K_Doxygen_C_Comment =>
            Generate_Doxygen_C_Comment (N);

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
         when K_Call_Profile =>
            Generate_Call_Profile (N);

         when K_Macro_Call =>
            Generate_Macro_Call (N);

         when K_Defining_Identifier =>
            Generate_Defining_Identifier (N);

         when K_Expression =>
            Generate_Expression (N);

         when K_Enum_Aggregate =>
            Generate_Enum_Aggregate (N);

         when K_For_Statement =>
            Generate_For_Statement (N);

         when K_While_Statement =>
            Generate_While_Statement (N);

         when K_Full_Type_Declaration =>
            Generate_Full_Type_Declaration (N);

         when K_If_Statement =>
            Generate_If_Statement (N);

         when K_Function_Implementation =>
            Generate_Function_Implementation (N);

         when K_Function_Specification =>
            Generate_Function_Specification (N);

         when K_Literal =>
            Generate_Literal (N);

         when K_Extern_Entity_Declaration =>
            Generate_Extern_Entity_Declaration (N);

         when K_Array_Values =>
            Generate_Array_Values (N);

         when K_Array_Value =>
            Generate_Array_Value (N);

         when K_Member_Declaration =>
            Generate_Member_Declaration (N);

         when K_Variable_Declaration =>
            Generate_Variable_Declaration (N);

         when K_Return_Statement =>
            Generate_Return_Statement (N);

         when K_Struct_Aggregate =>
            Generate_Struct_Aggregate (N);

         when K_Type_Conversion =>
            Generate_Type_Conversion (N);

         when K_Union_Aggregate =>
            Generate_Union_Aggregate (N);

         when K_Define_Statement =>
            Generate_Define_Statement (N);

         when K_Pointer_Type =>
            Generate_Pointer_Type (N);

         when K_Constant_Type =>
            Generate_Constant_Type (N);

         when K_Variable_Address =>
            Generate_Variable_Address (N);

         when K_Member_Designator =>
            Generate_Member_Designator (N);

         when K_Switch_Statement =>
            Generate_Switch_Statement (N);

         when K_Switch_Alternative =>
            Generate_Switch_Alternative (N);

         when K_Array_Declaration =>
            Generate_Array_Declaration (N);

         when K_Float .. K_Void =>
            Generate_Base_Type (N);

         when others =>
            Display_Error ("other element in generator", Fatal => False);
            null;
      end case;
   end Generate;

   --------------------------
   -- Generate_C_Comment --
   --------------------------

   procedure Generate_C_Comment (N : Node_Id) is
      --  This procedure does the following :

      --  * It generates a C comment basing on the name of node N

      --  * If the name it too long, and depending on the location of
      --    the comment in the source code, the procedure splits the
      --    comment into more than a line.

      --  The comment is assumed to be a sequence of caracters,
      --  beginning and ending with a NON-SPACE caracter.

      --  A word is :

      --  a space character, or else a sequence of non space
      --  characters located between two spaces.

      --  The maximum length of a line, in colums
      Max_Line_Length : constant Natural := 78;

      function Are_There_More_Words return Boolean;
      --  This function returns True if there are words in the buffer

      function Next_Word_Length return Natural;
      --  This function returns the size of the next word to be
      --  got. It returns zero when the buffer is empty.

      function Get_Next_Word return String;
      --  This function extracts the next word from the buffer

      --------------------------
      -- Are_There_More_Words --
      --------------------------

      function Are_There_More_Words return Boolean is
      begin
         return (Name_Len /= 0);
      end Are_There_More_Words;

      ----------------------
      -- Next_Word_Length --
      ----------------------

      function Next_Word_Length return Natural is
         L : Natural;
      begin
         if not Are_There_More_Words then
            L := 0;
         elsif Name_Buffer (1) = ' ' then
            L := 1;
         else
            L := 0;
318
            while L + 1 <= Name_Len and then Name_Buffer (L + 1) /= ' ' loop
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
               L := L + 1;
            end loop;
         end if;
         return L;
      end Next_Word_Length;

      -------------------
      -- Get_Next_Word --
      -------------------

      function Get_Next_Word return String is
         L : constant Natural := Next_Word_Length;
      begin
         if L = 0 then
            return "";
         else
            declare
               Next_Word : constant String := Name_Buffer (1 .. L);
            begin
               if Name_Len = L then
                  Name_Len := 0;
               else
                  Set_Str_To_Name_Buffer (Name_Buffer (L + 1 .. Name_Len));
               end if;
               return Next_Word;
            end;
         end if;
      end Get_Next_Word;

348
      First_Line   : Boolean := True;
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
      Used_Columns : Natural;
   begin
      Get_Name_String (Name (Defining_Identifier (N)));

      while Are_There_More_Words loop
         Used_Columns := N_Space;
         if First_Line then
            First_Line := False;
         else
            Write_Indentation;
         end if;

         --  We consume 4 colums

         Used_Columns := Used_Columns + 2;
         Write_Str ("/*");

         if Has_Header_Spaces (N) then
            Used_Columns := Used_Columns + 2;
            Write_Str ("  ");
         end if;

         Used_Columns := Used_Columns + Next_Word_Length;
         Write_Str (Get_Next_Word);

         while Are_There_More_Words
           and then (Used_Columns + Next_Word_Length < Max_Line_Length)
         loop
            Used_Columns := Used_Columns + Next_Word_Length;
            Write_Str (Get_Next_Word);
         end loop;
         Write_Str ("*/");

         if Are_There_More_Words then
            Write_Eol;
         end if;
      end loop;
      Write_Eol;
   end Generate_C_Comment;

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
   --------------------------
   -- Generate_Doxygen_C_Comment --
   --------------------------

   procedure Generate_Doxygen_C_Comment (N : Node_Id) is
      --  This procedure does the following :

      --  * It generates a C comment basing on the name of node N

      --  * If the name it too long, and depending on the location of
      --    the comment in the source code, the procedure splits the
      --    comment into more than a line.

      --  The comment is assumed to be a sequence of caracters,
      --  beginning and ending with a NON-SPACE caracter.

      --  A word is :

      --  a space character, or else a sequence of non space
      --  characters located between two spaces.

      --  The maximum length of a line, in colums
      Max_Line_Length : constant Natural := 78;

      function Are_There_More_Words return Boolean;
      --  This function returns True if there are words in the buffer

      function Next_Word_Length return Natural;
      --  This function returns the size of the next word to be
      --  got. It returns zero when the buffer is empty.

      function Get_Next_Word return String;
      --  This function extracts the next word from the buffer

      --------------------------
      -- Are_There_More_Words --
      --------------------------

      function Are_There_More_Words return Boolean is
      begin
         return (Name_Len /= 0);
      end Are_There_More_Words;

      ----------------------
      -- Next_Word_Length --
      ----------------------

      function Next_Word_Length return Natural is
         L : Natural;
      begin
         if not Are_There_More_Words then
            L := 0;
         elsif Name_Buffer (1) = ' ' then
            L := 1;
         else
            L := 0;
445
            while L + 1 <= Name_Len and then Name_Buffer (L + 1) /= ' ' loop
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
               L := L + 1;
            end loop;
         end if;
         return L;
      end Next_Word_Length;

      -------------------
      -- Get_Next_Word --
      -------------------

      function Get_Next_Word return String is
         L : constant Natural := Next_Word_Length;
      begin
         if L = 0 then
            return "";
         else
            declare
               Next_Word : constant String := Name_Buffer (1 .. L);
            begin
               if Name_Len = L then
                  Name_Len := 0;
               else
                  Set_Str_To_Name_Buffer (Name_Buffer (L + 1 .. Name_Len));
               end if;
               return Next_Word;
            end;
         end if;
      end Get_Next_Word;

      Used_Columns : Natural;
   begin
      Used_Columns := N_Space;
      Used_Columns := Used_Columns + 3;
      Write_Eol;
      Write_Str ("/*!");

      if For_Struct (N) then
         Write_Eol;
         Write_Str (" * \struct ");
         Write_Name (Name (Element (N)));
      end if;

      if For_Union (N) then
         Write_Eol;
         Write_Str (" * \union ");
         Write_Name (Name (Element (N)));
      end if;

      if For_Enum (N) then
         Write_Eol;
         Write_Str (" * \enum ");
         Write_Name (Name (Element (N)));
      end if;

      if For_Function (N) then
         Write_Eol;
         Write_Str (" * \fn ");
         Write_Name (Name (Element (N)));
      end if;

      if For_Variable (N) then
         Write_Eol;
         Write_Str (" * \var ");
         Write_Name (Name (Element (N)));
      end if;

      if For_Define (N) then
         Write_Eol;
         Write_Str (" * \def ");
         Write_Name (Name (Element (N)));
      end if;

      if For_Typedef (N) then
         Write_Eol;
         Write_Str (" * \typedef ");
         Write_Name (Name (Element (N)));
      end if;

      if For_File (N) then
         Write_Eol;
         Write_Str (" * \file ");
         Write_Name (Name (Element (N)));
      end if;

      if For_Namespace (N) then
         Write_Eol;
         Write_Str (" * \namespace ");
         Write_Name (Name (Element (N)));
      end if;

      if For_Package (N) then
         Write_Eol;
         Write_Str (" * \package ");
         Write_Name (Name (Element (N)));
      end if;

      if For_Interface (N) then
         Write_Eol;
         Write_Str (" * \interface ");
         Write_Name (Name (Element (N)));
      end if;

      if Summary (N) /= No_Node then
         Write_Eol;
         Write_Str (" * \brief ");
551
         Write_Name (Name (Summary (N)));
552 553 554 555 556
         Write_Eol;
         Write_Str (" *");
      end if;

      Write_Eol;
557
      Get_Name_String (Name (Description (N)));
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
      while Are_There_More_Words loop
         Used_Columns := N_Space;

         --  We consume 4 colums

         Used_Columns := Used_Columns + 2;
         Write_Str (" * ");
         if Has_Header_Spaces (N) then
            Used_Columns := Used_Columns + 2;
            Write_Str ("  ");
         end if;

         Used_Columns := Used_Columns + Next_Word_Length;
         Write_Str (Get_Next_Word);

         while Are_There_More_Words
           and then (Used_Columns + Next_Word_Length < Max_Line_Length)
         loop
            Used_Columns := Used_Columns + Next_Word_Length;
            Write_Str (Get_Next_Word);
         end loop;

         if Are_There_More_Words then
            Write_Eol;
         end if;
      end loop;
      Write_Eol;
      Write_Str (" */");
      Write_Eol;
   end Generate_Doxygen_C_Comment;

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
   -----------------------------------
   -- Generate_Assignment_Statement --
   -----------------------------------

   procedure Generate_Assignment_Statement (N : Node_Id) is
   begin
      Generate (Defining_Identifier (N));
      Write_Space;
      Write (Tok_Equal);
      Write_Eol;
      Increment_Indentation;
      Write_Indentation (-1);
      Generate (Expression (N));
      Decrement_Indentation;
   end Generate_Assignment_Statement;

   --------------------------------
   -- Generate_Array_Declaration --
   --------------------------------

   procedure Generate_Array_Declaration (N : Node_Id) is
   begin
      Generate (Defining_Identifier (N));
      Write (Tok_Left_Hook);
      Generate (Array_Size (N));
      Write (Tok_Right_Hook);
   end Generate_Array_Declaration;

   ----------------------------------------
   -- Generate_Extern_Entity_Declaration --
   ----------------------------------------

   procedure Generate_Extern_Entity_Declaration (N : Node_Id) is
   begin
      Write (Tok_Extern);
      Write_Space;
      Generate (Entity (N));
   end Generate_Extern_Entity_Declaration;

   ---------------------------
   -- Generate_Array_Values --
   ---------------------------

   procedure Generate_Array_Values (N : Node_Id) is
      D : Node_Id := First_Node (Values (N));
   begin
      Write (Tok_Left_Brace);
      while Present (D) loop
         Generate (D);
         D := Next_Node (D);
         if Present (D) then
            Write (Tok_Comma);
         end if;
      end loop;
      Write (Tok_Right_Brace);
   end Generate_Array_Values;

   --------------------------
   -- Generate_Array_Value --
   --------------------------

   procedure Generate_Array_Value (N : Node_Id) is
   begin
      Generate (Defining_Identifier (N));
      Write (Tok_Left_Hook);
      Generate (Array_Item (N));
      Write (Tok_Right_Hook);
   end Generate_Array_Value;

   ----------------------------------
   -- Generate_Defining_Identifier --
   ----------------------------------

   procedure Generate_Defining_Identifier (N : Node_Id) is
   begin
      if Is_Pointer (N) then
         Write (Tok_Asterisk);
      end if;

      Write_Name (Name (N));
   end Generate_Defining_Identifier;

   -------------------------
   -- Generate_Expression --
   -------------------------

   procedure Generate_Expression (N : Node_Id) is
676 677 678
      L_Expr : constant Node_Id     := Left_Expression (N);
      Op     : constant Operator_Id := Operator (N);
      R_Expr : constant Node_Id     := Right_Expression (N);
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
   begin
      --  Each expression having a right part and a left part is
      --  systematically put between two parentheses.

      Generate (L_Expr);
      Write_Space;
      Write_Name (Operator_Image (Standard.Integer (Op)));
      Write_Space;
      Generate (R_Expr);

   end Generate_Expression;

   ----------------------------
   -- Generate_For_Statement --
   ----------------------------

   procedure Generate_For_Statement (N : Node_Id) is
      D : Node_Id := First_Node (Statements (N));
   begin
      Write (Tok_For);
      Write_Space;
      Write (Tok_Left_Paren);
      Generate (Pre_Cond (N));
      Write (Tok_Semicolon);
      Generate (Condition (N));
      Write (Tok_Semicolon);
      Generate (Post_Cond (N));
      Write (Tok_Right_Paren);
      Write_Eol;
      Write (Tok_Left_Brace);
      Increment_Indentation;
      while Present (D) loop
         Write_Indentation;
         Generate (D);
         Generate_Statement_Delimiter (D);
         D := Next_Node (D);
      end loop;
      Decrement_Indentation;
      Write_Indentation;
      Write (Tok_Right_Brace);
   end Generate_For_Statement;

   ------------------------------------
   -- Generate_Full_Type_Declaration --
   ------------------------------------

   procedure Generate_Full_Type_Declaration (N : Node_Id) is
   begin
      Write (Tok_Typedef);
      Write_Space;
      Generate (Type_Definition (N));
      Write_Space;
      Generate (Type_Name (N));
--      Write_Name (Name (Defining_Identifier (N)));
   end Generate_Full_Type_Declaration;

   ---------------------------
   -- Generate_If_Statement --
   ---------------------------

   procedure Generate_If_Statement (N : Node_Id) is
      T : constant List_Id := Statements (N);
      E : constant List_Id := Else_Statements (N);
      I : Node_Id;

   begin
      --  Enter If_Statement

747
      Write_Str ("/* :: Yes if commentary :: */");
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
      Write (Tok_If);
      Write_Space;
      Write (Tok_Left_Paren);
      Generate (Condition (N));
      Write (Tok_Right_Paren);
      Write_Eol;
      Write_Indentation;
      Write (Tok_Left_Brace);
      Write_Eol;
      Write_Indentation;

      --  If_Statement cannot be empty. A null statement is always
      --  there if needed.

      Increment_Indentation;
      I := First_Node (T);
      while Present (I) loop
         Write_Indentation;
         Generate (I);
         Generate_Statement_Delimiter (I);
         I := Next_Node (I);
      end loop;
      Write_Eol;
      Decrement_Indentation;
      Write_Indentation;
      Write (Tok_Right_Brace);

      --  Else_Statement can be empty

      if not Is_Empty (E) then
         Write_Indentation;
         Write (Tok_Else);
         Write_Eol;
         Write (Tok_Left_Brace);
         Write_Eol;
         Increment_Indentation;
         I := First_Node (E);
         while Present (I) loop
            Write_Indentation;
            Generate (I);
            Generate_Statement_Delimiter (I);
            I := Next_Node (I);
         end loop;
         Decrement_Indentation;
         Write_Eol;
         Write_Indentation;
         Write (Tok_Right_Brace);
         Write_Eol;
      end if;
   end Generate_If_Statement;

   ----------------------
   -- Generate_Literal --
   ----------------------

   procedure Generate_Literal (N : Node_Id) is
   begin
      Write_Str (Image (Value (N)));
   end Generate_Literal;

   -----------------------------
   -- Generate_While_Statement --
   -----------------------------

   procedure Generate_While_Statement (N : Node_Id) is
      D : Node_Id := First_Node (Statements (N));
   begin
      Write (Tok_While);
      Write_Space;
      Write (Tok_Left_Paren);
      Generate (Condition (N));
      Write (Tok_Right_Paren);
      Write_Eol;
      Write_Indentation;
      Write (Tok_Left_Brace);
      Write_Eol;
      Increment_Indentation;
      while Present (D) loop
         Write_Indentation;
         Generate (D);
         Generate_Statement_Delimiter (D);
         D := Next_Node (D);
      end loop;
      Decrement_Indentation;
      Write_Indentation;
      Write (Tok_Right_Brace);
   end Generate_While_Statement;

   ------------------------
   -- Generate_Parameter --
   ------------------------

   procedure Generate_Parameter (N : Node_Id) is
   begin
      Generate (Parameter_Type (N));
      Write_Space;
      Name_Buffer (1 .. Var_Name_Len) := (others => ' ');
      Get_Name_String (Name (Defining_Identifier (N)));

      if Var_Name_Len > Name_Len then
         Name_Len := Var_Name_Len;
      end if;

      Write_Str (Name_Buffer (1 .. Name_Len));
   end Generate_Parameter;

   -----------------------------
   -- Generate_Parameter_List --
   -----------------------------

   procedure Generate_Parameter_List (L : List_Id) is
      N : Node_Id;

   begin
      --  If we got there, then L is not empty.
      if Is_Empty (L) then
         Write (Tok_Left_Paren);
865
         Write_Str ("void");
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
         Write (Tok_Right_Paren);
         return;
      end if;

      Write_Eol;
      Increment_Indentation;
      Increment_Indentation;
      Write_Indentation;
      Write (Tok_Left_Paren);

      N := First_Node (L);
      loop
         Generate_Parameter (N);
         exit when No (Next_Node (N));
         Write (Tok_Comma);
         N := Next_Node (N);
         Write_Eol;
         Write_Indentation;
      end loop;

      Write (Tok_Right_Paren);
      Decrement_Indentation;
      Decrement_Indentation;
      Write_Indentation;
   end Generate_Parameter_List;

   -------------------------------
   -- Generate_Return_Statement --
   -------------------------------

   procedure Generate_Return_Statement (N : Node_Id) is
      E : constant Node_Id := Expression (N);
   begin
      Write (Tok_Return);

      if Present (E) then
         Write_Space;
         Write (Tok_Left_Paren);
         Generate (E);
         Write (Tok_Right_Paren);
      end if;
   end Generate_Return_Statement;

   ---------------------------
   -- Generate_Call_Profile --
   ---------------------------

   procedure Generate_Call_Profile (N : Node_Id) is
      L : constant List_Id := Parameters (N);
      P : Node_Id;

   begin
      Generate (Defining_Identifier (N));

      Write_Space;

      Write (Tok_Left_Paren);
      if not Is_Empty (L) then
         P := First_Node (L);
         loop
            Generate (P);
            P := Next_Node (P);
            exit when No (P);
            Write (Tok_Comma);
            Write_Space;
         end loop;
      end if;
      Write (Tok_Right_Paren);
   end Generate_Call_Profile;

   -------------------------
   -- Generate_Macro_Call --
   -------------------------

   procedure Generate_Macro_Call (N : Node_Id) is
      L : constant List_Id := Parameters (N);
      P : Node_Id;
   begin
      Generate (Defining_Identifier (N));

      Write (Tok_Left_Paren);
      if not Is_Empty (L) then
         P := First_Node (L);
         loop
            Generate (P);
            P := Next_Node (P);
            exit when No (P);
            Write (Tok_Comma);
            Write_Space;
         end loop;
      end if;
      Write (Tok_Right_Paren);
   end Generate_Macro_Call;

   --------------------------------------
   -- Generate_Function_Implementation --
   --------------------------------------

   procedure Generate_Function_Implementation (N : Node_Id) is
      D : constant List_Id := Declarations (N);
      S : constant List_Id := Statements (N);
      P : constant Node_Id := Specification (N);
      M : Node_Id;
   begin
      Write_Indentation;
      Generate (P);

      Write_Eol;
      Write (Tok_Left_Brace);
      Write_Eol;
      Increment_Indentation;

978
      if not Is_Empty (D) then
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
         M := First_Node (D);
         while Present (M) loop
            Write_Indentation;
            Generate (M);
            Generate_Statement_Delimiter (M);
            M := Next_Node (M);
         end loop;
      end if;

      Write_Eol;

      if not Is_Empty (S) then
         M := First_Node (S);
         while Present (M) loop
            Write_Indentation;
            Generate (M);
            Generate_Statement_Delimiter (M);
            M := Next_Node (M);
         end loop;
      end if;

      Decrement_Indentation;
      Write_Indentation;
      Write (Tok_Right_Brace);
      Write_Eol;
   end Generate_Function_Implementation;

   ---------------------------------------
   -- Generate_Function_Specification --
   ---------------------------------------

   procedure Generate_Function_Specification (N : Node_Id) is
      P : constant List_Id := Parameters (N);
      T : constant Node_Id := Return_Type (N);
   begin
      --  If we deal with a main subprogram, then we generate its
      --  withed packages

      if T /= No_Node then
         Generate (T);
      end if;

      if Present (Defining_Identifier (N)) then
         Write_Space;
         Write_Name (Name (Defining_Identifier (N)));
      end if;

      Write_Space;
      Generate_Parameter_List (P);

   end Generate_Function_Specification;

   ------------------------------
   -- Generate_Struct_Aggregate --
   ------------------------------

   procedure Generate_Struct_Aggregate (N : Node_Id) is
      P : Node_Id := First_Node (Struct_Members (N));
   begin
      Write (Tok_Struct);
      Write_Eol;
      Write_Indentation;
      Write (Tok_Left_Brace);
      Write_Eol;
      Increment_Indentation;

      while Present (P) loop
         Write_Indentation;
         Generate (P);
         Generate_Statement_Delimiter (P);
         P := Next_Node (P);
         Write_Eol;
      end loop;

      Decrement_Indentation;
      Write_Indentation;
      Write (Tok_Right_Brace);
   end Generate_Struct_Aggregate;

   -----------------------------
   -- Generate_Enum_Aggregate --
   -----------------------------

   procedure Generate_Enum_Aggregate (N : Node_Id) is
      P : Node_Id := First_Node (Enum_Members (N));
   begin
      Write (Tok_Enum);
      Write_Eol;
      Write (Tok_Left_Brace);
      Write_Eol;
      Increment_Indentation;
      while Present (P) loop
         Write_Indentation;
         Generate (P);
         P := Next_Node (P);
         if Present (P) then
            Write (Tok_Comma);
         end if;
         Write_Eol;
      end loop;
      Decrement_Indentation;
      Write_Indentation;
      Write (Tok_Right_Brace);
   end Generate_Enum_Aggregate;

   ------------------------------
   -- Generate_Union_Aggregate --
   ------------------------------

   procedure Generate_Union_Aggregate (N : Node_Id) is
      P : Node_Id;
   begin
      Write (Tok_Union);
      Write_Eol;
      Write_Indentation;
      Write (Tok_Left_Brace);
      Write_Eol;
      Increment_Indentation;
      if not Is_Empty (Union_Members (N)) then
         P := First_Node (Union_Members (N));
         while Present (P) loop
            Write_Indentation;
            Generate (P);
            Generate_Statement_Delimiter (P);
            P := Next_Node (P);
            Write_Eol;
         end loop;
      end if;
      Decrement_Indentation;
      Write_Indentation;
      Write (Tok_Right_Brace);
   end Generate_Union_Aggregate;

   -------------------------------
   -- Generate_Switch_Statement --
   -------------------------------

   procedure Generate_Switch_Statement (N : Node_Id) is
      P : Node_Id;
   begin

      if Is_Empty (Alternatives (N)) then
         return;
      end if;

      Write (Tok_Switch);
      Write_Space;
      Write (Tok_Left_Paren);
      Generate (Expression (N));
      Write (Tok_Right_Paren);
      Write_Eol;
      Write_Indentation;
      Write (Tok_Left_Brace);
      Write_Eol;
      Increment_Indentation;

      P := First_Node (Alternatives (N));
      while Present (P) loop
         Write_Indentation;
         Generate (P);
         P := Next_Node (P);
         Write_Eol;
      end loop;

      Decrement_Indentation;
      Write_Indentation;
      Write (Tok_Right_Brace);
   end Generate_Switch_Statement;

   ---------------------------------
   -- Generate_Switch_Alternative --
   ---------------------------------

   procedure Generate_Switch_Alternative (N : Node_Id) is
      P : Node_Id;
   begin
      if Is_Empty (Labels (N)) then
         Write (Tok_Default);
         Write (Tok_Colon);
      else
         P := First_Node (Labels (N));
         while Present (P) loop
            Write (Tok_Case);
            Write_Space;
            Generate (P);
            Write (Tok_Colon);
            P := Next_Node (P);
         end loop;
      end if;
      Write_Eol;
      Write_Indentation;
      Write (Tok_Left_Brace);
      Write_Eol;
      Increment_Indentation;
      if not Is_Empty (Statements (N)) then
         P := First_Node (Statements (N));
         while Present (P) loop
            Write_Indentation;
            Generate (P);
            Generate_Statement_Delimiter (P);
            P := Next_Node (P);
            Write_Eol;
         end loop;
      end if;

      Write_Indentation;
      Write (Tok_Break);
      Write (Tok_Semicolon);
      Write_Eol;

      Decrement_Indentation;
      Write_Indentation;
      Write (Tok_Right_Brace);
   end Generate_Switch_Alternative;

   -----------------------------------
   -- Generate_Variable_Declaration --
   -----------------------------------

   procedure Generate_Variable_Declaration (N : Node_Id) is
   begin
1200 1201
      if Is_Static (N) then
         Write (Tok_Static);
1202
         Write_Space;
1203
      end if;
1204
      Generate (Used_Type (N));
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
      Write_Space;
      Generate (Defining_Identifier (N));
   end Generate_Variable_Declaration;

   ---------------------------------
   -- Generate_Member_Declaration --
   ---------------------------------

   procedure Generate_Member_Declaration (N : Node_Id) is
   begin
      Generate (Used_Type (N));
      Write_Space;
      Generate (Defining_Identifier (N));
   end Generate_Member_Declaration;

   -----------------------------------------
   -- Generate_HI_Distributed_Application --
   -----------------------------------------

   procedure Generate_HI_Distributed_Application (N : Node_Id) is
      P                     : Node_Id := First_Node (HI_Nodes (N));
      Application_Directory : Name_Id;
   begin
      --  Create the application directory (a lower case string)
hugues.jerome's avatar
hugues.jerome committed
1229

1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
      Get_Name_String (Name (N));
      Application_Directory := To_Lower (Name_Find);

      Create_Directory (Application_Directory);

      --  Process the application nodes

      Enter_Directory (Application_Directory);

      while Present (P) loop
         Generate (P);
         P := Next_Node (P);
      end loop;

      Leave_Directory;
   end Generate_HI_Distributed_Application;

   ----------------------
   -- Generate_HI_Node --
   ----------------------

   procedure Generate_HI_Node (N : Node_Id) is
1252
      U                   : Node_Id          := First_Node (Units (N));
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
      Partition_Directory : constant Name_Id := To_Lower (Name (N));
   begin
      --  Create the node directory

      Create_Directory (Partition_Directory);
      Enter_Directory (Partition_Directory);

      while Present (U) loop
         Generate (U);
         U := Next_Node (U);
      end loop;

      Leave_Directory;
   end Generate_HI_Node;

   -----------
   -- Write --
   -----------

   procedure Write (T : Token_Type) is
   begin
      Write_Name (Token_Image (T));
   end Write;

   ----------------
   -- Write_Line --
   ----------------

   procedure Write_Line (T : Token_Type) is
   begin
      Write (T);
      Write_Eol;
   end Write_Line;

   ----------------------------------
   -- Generate_Statement_Delimiter --
   ----------------------------------

   procedure Generate_Statement_Delimiter (N : Node_Id) is
   begin
      if No (N)
        or else Kind (N) = K_Define_Statement
        or else Kind (N) = K_Switch_Statement
        or else Kind (N) = K_Switch_Alternative
        or else Kind (N) = K_While_Statement
        or else Kind (N) = K_If_Statement
        or else Kind (N) = K_Function_Implementation
      then
         Write_Eol;
1302 1303 1304
      elsif Kind (N) /= K_C_Comment
        and then Kind (N) /= K_Doxygen_C_Comment
      then
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
         Write_Line (Tok_Semicolon);
      end if;
   end Generate_Statement_Delimiter;

   ------------------------------
   -- Generate_Type_Conversion --
   ------------------------------

   procedure Generate_Type_Conversion (N : Node_Id) is
   begin
      Increment_Indentation;
      Write (Tok_Left_Paren);
      Generate (Subtype_Mark (N));
      Write (Tok_Right_Paren);
      Generate (Expression (N));
      Decrement_Indentation;
   end Generate_Type_Conversion;

   --------------------------
   -- Generate_Source_File --
   --------------------------

   procedure Generate_Source_File (N : Node_Id) is
      Fd : File_Descriptor;
1329
      D  : Node_Id := First_Node (Declarations (N));
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
   begin
      if No (N) then
         return;
      end if;
      Fd := Set_Output (Get_File_Name (N));

      if not Is_Empty (Included_Headers (N)) then
         Generate_Included_Files (Included_Headers (N));
      end if;

      while Present (D) loop
         Generate (D);
         Generate_Statement_Delimiter (D);
         D := Next_Node (D);
      end loop;
      Write_Eol;

      --  Always leave a blank line at the end of a C-source file

      Release_Output (Fd);
   end Generate_Source_File;

   -----------------------------
   -- Generate_Included_Files --
   -----------------------------

   procedure Generate_Included_Files (N : List_Id) is
      H : Node_Id := First_Node (N);
   begin
      while Present (H) loop
         Write (Tok_Sharp);
         Write (Tok_Include);
         Write_Space;

         if Is_Local (H) then
            Write (Tok_Quote);
         else
            Write (Tok_Less);
         end if;

         Generate (Header_Name (H));
         Write (Tok_Dot);
         Write_Str ("h");

         if Is_Local (H) then
            Write (Tok_Quote);
         else
            Write (Tok_Greater);
         end if;

         Write_Eol;
         H := Next_Node (H);
      end loop;
   end Generate_Included_Files;

   --------------------------
   -- Generate_Header_File --
   --------------------------

   procedure Generate_Header_File (N : Node_Id) is
      Fd : File_Descriptor;
1391
      D  : Node_Id := First_Node (Declarations (N));
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
      NA : Name_Id;
   begin
      if No (D) then
         return;
      end if;
      NA := Name (Defining_Identifier (N));
      NA := To_Upper (NA);

      Fd := Set_Output (Get_File_Name (N));

      Write (Tok_Sharp);
      Write (Tok_Ifndef);
      Write_Space;
      Write (Tok_Underscore);
      Write (Tok_Underscore);
      Write_Str ("OCARINA_GENERATED_");
      Write_Name (NA);
      Write (Tok_Underscore);
      Write_Str ("H");
      Write (Tok_Underscore);
      Write_Eol;

      Write (Tok_Sharp);
      Write (Tok_Define);
      Write_Space;
      Write (Tok_Underscore);
      Write (Tok_Underscore);
      Write_Str ("OCARINA_GENERATED_");
      Write_Name (NA);
      Write (Tok_Underscore);
      Write_Str ("H");
      Write (Tok_Underscore);
      Write_Space;
      Write_Eol;

      if not Is_Empty (Included_Headers (N)) then
         Generate_Included_Files (Included_Headers (N));
      end if;

      while Present (D) loop
         Generate (D);
         Generate_Statement_Delimiter (D);
         Write_Eol;
         D := Next_Node (D);
      end loop;
      Write (Tok_Sharp);
      Write (Tok_Endif);

      Write_Eol;

      --  Always leave a blank line at the end of a C-source file

      Release_Output (Fd);
   end Generate_Header_File;

   ------------------------
   -- Generate_Base_Type --
   ------------------------

   procedure Generate_Base_Type (N : Node_Id) is
   begin
      case Kind (N) is
         when K_Int =>
            Write_Str ("int");

         when K_Float =>
            Write_Str ("float");

         when K_Char =>
            Write_Str ("char");

         when K_Void =>
            Write_Str ("void");

         when others =>
            Display_Error ("other element in generator", Fatal => False);
            null;
      end case;

   end Generate_Base_Type;

   ----------------------
   -- Generate_HI_Unit --
   ----------------------

   procedure Generate_HI_Unit (N : Node_Id) is
      S : Node_Id := First_Node (Sources (N));
      H : Node_Id := First_Node (Headers (N));
   begin
      while Present (S) loop
         Generate (S);
         S := Next_Node (S);
      end loop;
      while Present (H) loop
         Generate (H);
         H := Next_Node (H);
      end loop;
   end Generate_HI_Unit;

   -------------------------------
   -- Generate_Define_Statement --
   -------------------------------

   procedure Generate_Define_Statement (N : Node_Id) is
      V : constant Node_Id := Defined_Value (N);
      I : constant Node_Id := Defining_Identifier (N);
   begin
      Write (Tok_Sharp);
      Write (Tok_Define);
      Write_Space;
      Generate (I);
      Write_Space;
      Generate (V);
   end Generate_Define_Statement;

   ---------------------------
   -- Generate_Pointer_Type --
   ---------------------------

   procedure Generate_Pointer_Type (N : Node_Id) is
   begin
      Generate (Used_Type (N));
      Write (Tok_Asterisk);
   end Generate_Pointer_Type;

   ----------------------------
   -- Generate_Constant_Type --
   ----------------------------

   procedure Generate_Constant_Type (N : Node_Id) is
   begin
      Write (Tok_Const);
      Write_Space;
      Generate (Used_Type (N));
   end Generate_Constant_Type;

   -------------------------------
   -- Generate_Variable_Address --
   -------------------------------

   procedure Generate_Variable_Address (N : Node_Id) is
   begin
      Write (Tok_Ampersand);
      Write (Tok_Left_Paren);
      Generate (Expression (N));
      Write (Tok_Right_Paren);
   end Generate_Variable_Address;

   --------------------------------
   -- Generate_Member_Designator --
   --------------------------------

   procedure Generate_Member_Designator (N : Node_Id) is
   begin
      Generate (Aggregate_Name (N));
      if Is_Pointer (N) then
         Write (Tok_Arrow);
      else
         Write (Tok_Dot);
      end if;
      Generate (Defining_Identifier (N));
   end Generate_Member_Designator;

   -----------------------------
   -- Generate_Include_Clause --
   -----------------------------

   procedure Generate_Include_Clause (N : Node_Id) is
   begin
      Write (Tok_Sharp);
      Write (Tok_Include);
      Write_Space;

      if Is_Local (N) then
         Write (Tok_Quote);
      else
         Write (Tok_Less);
      end if;

      Generate (Header_Name (N));
      Write (Tok_Dot);
      Write_Str ("h");

      if Is_Local (N) then
         Write (Tok_Quote);
      else
         Write (Tok_Greater);
      end if;

      Write_Eol;
   end Generate_Include_Clause;

   ---------------------------
   -- Generate_Ifdef_Clause --
   ---------------------------

   procedure Generate_Ifdef_Clause (N : Node_Id) is
      S : Node_Id;
   begin
      Write (Tok_Sharp);
      if Negation (N) then
         Write (Tok_Ifndef);
      else
         Write (Tok_Ifdef);
      end if;

      Write_Space;

      Generate (Clause (N));

      Write_Eol;

      S := First_Node (Then_Statements (N));
      while Present (S) loop
         Generate (S);
         S := Next_Node (S);
      end loop;

      if not Is_Empty (Else_Statements (N)) then
         S := First_Node (Else_Statements (S));
         while Present (S) loop
            Generate (S);
            S := Next_Node (S);
         end loop;
      end if;

      Write (Tok_Sharp);
      Write (Tok_Endif);

      Write_Eol;

   end Generate_Ifdef_Clause;

end Ocarina.Backends.C_Tree.Generator;