home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 20 / AACD20.BIN / AACD / Programming / Jikes / Source / src / unparse.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-24  |  34.2 KB  |  1,100 lines

  1. // $Id: unparse.cpp,v 1.15 2001/01/14 09:56:31 mdejong Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1998, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10.  
  11. #include "ast.h"
  12.  
  13. //FIXME: include stuff
  14. //#include <iostream.h>
  15. //#include <fstream.h>
  16.  
  17. #ifdef JIKES_DEBUG
  18.  
  19. #ifdef    HAVE_JIKES_NAMESPACE
  20. namespace Jikes {    // Open namespace Jikes block
  21. #endif
  22.  
  23.  
  24. bool Ast::debug_unparse = false;
  25.  
  26. // Special top-level form
  27. void AstCompilationUnit::Unparse(LexStream& lex_stream, char *directory)
  28. {
  29.     char *in_file_name = lex_stream.FileName();
  30.     // char *suffix = ".unparse";
  31.     char *suffix = "";
  32.     char *out_file_name = strcat3(directory, in_file_name, suffix);
  33.     // Create the directory if necessary
  34.     for (int i=strlen(out_file_name); i>=0; i--) {
  35.        if (out_file_name[i] == U_SLASH) {
  36.            out_file_name[i] = U_NULL;
  37.            if (! SystemIsDirectory(out_file_name))
  38.            {
  39.                Ostream() << "making directory " << out_file_name << "\n";
  40.                SystemMkdirhier(out_file_name);
  41.            }
  42.            out_file_name[i] = U_SLASH;
  43.            break;
  44.        }
  45.     }
  46.     ofstream os_base(out_file_name);
  47.     if (!os_base)
  48.     {
  49.         Ostream() << "Cannot open output file " << out_file_name << "\n";
  50.         abort();
  51.     }
  52.     Ostream os(&os_base);
  53.     this -> Unparse(os, lex_stream);
  54.     delete[] out_file_name;
  55. }
  56.  
  57. void Ast::Unparse(Ostream& os, LexStream& lex_stream)
  58. {
  59.     if (debug_unparse)
  60.         os << "/*Ast:#" << this-> id << "*/";
  61.     os << "***** TO DO *****";
  62.     os << "#" << this -> id << " (Ast):  ";
  63.     os << "Node kind " << (int) kind << " does not contain an unparse routine\n";
  64.     if (debug_unparse)
  65.         os << "/*:Ast#" << this-> id << "*/";
  66. }
  67.  
  68. void AstBlock::Unparse(Ostream& os, LexStream& lex_stream)
  69. {
  70.     if (Ast::debug_unparse)
  71.         os << "/*AstBlock:#" << this-> id << "*/";
  72.     if (Ast::debug_unparse)
  73.         os << "/*no_braces:" << no_braces << "*/";
  74.     for (int il = 0; il < this -> NumLabels(); il++)
  75.     {
  76.         os << lex_stream.NameString(this -> Label(il)) << ": ";
  77.     }
  78.  
  79.     if (!no_braces)
  80.         os << "{\n";
  81.     for (int is = 0; is < this -> NumStatements(); is++)
  82.     {
  83.         this -> Statement(is) -> Unparse(os, lex_stream);
  84.     }
  85.     if (!no_braces)
  86.         os << "}\n";
  87.     if (Ast::debug_unparse)
  88.         os << "/*:AstBlock#" << this-> id << "*/";
  89. }
  90.  
  91. void AstPrimitiveType::Unparse(Ostream& os, LexStream& lex_stream)
  92. {
  93.     if (Ast::debug_unparse)
  94.         os << "/*AstPrimitiveType:#" << this-> id << "*/";
  95.     os << lex_stream.NameString(primitive_kind_token);
  96.     if (Ast::debug_unparse)
  97.         os << "/*:AstPrimitiveType#" << this-> id << "*/";
  98. }
  99.  
  100. void AstArrayType::Unparse(Ostream& os, LexStream& lex_stream)
  101. {
  102.     if (Ast::debug_unparse)
  103.         os << "/*AstArrayType:#" << this-> id << "*/";
  104.     type -> Unparse(os, lex_stream);
  105.     for (int i = 0; i < this -> NumBrackets(); i++)
  106.         os << "[]";
  107.     if (Ast::debug_unparse)
  108.         os << "/*:AstArrayType#" << this-> id << "*/";
  109. }
  110.  
  111. void AstSimpleName::Unparse(Ostream& os, LexStream& lex_stream)
  112. {
  113.     if (Ast::debug_unparse)
  114.         os << "/*AstSimpleName:#" << this-> id << "*/";
  115.     os << lex_stream.NameString(identifier_token);
  116.     if (Ast::debug_unparse)
  117.         os << "/*:AstSimpleName#" << this-> id << "*/";
  118. }
  119.  
  120. void AstPackageDeclaration::Unparse(Ostream& os, LexStream& lex_stream)
  121. {
  122.     if (Ast::debug_unparse)
  123.         os << "/*AstPackageDeclaration:#" << this-> id << "*/";
  124.     os << lex_stream.NameString(package_token);
  125.     os << " ";
  126.     name -> Unparse(os, lex_stream);
  127.     os << ";\n";
  128.     if (Ast::debug_unparse)
  129.         os << "/*:AstPackageDeclaration#" << this-> id << "*/";
  130. }
  131.  
  132. void AstImportDeclaration::Unparse(Ostream& os, LexStream& lex_stream)
  133. {
  134.     if (Ast::debug_unparse)
  135.         os << "/*AstImportDeclaration:#" << this-> id << "*/";
  136.     os << lex_stream.NameString(import_token);
  137.     os << " ";
  138.     name -> Unparse(os, lex_stream);
  139.     os << (star_token_opt ? "." : "");
  140.     if (star_token_opt)
  141.         os << lex_stream.NameString(star_token_opt);
  142.     os << ";\n";
  143.     if (Ast::debug_unparse)
  144.         os << "/*:AstImportDeclaration#" << this-> id << "*/";
  145. }
  146.  
  147. void AstCompilationUnit::Unparse(Ostream& os, LexStream& lex_stream)
  148. {
  149.     if (Ast::debug_unparse) os << "/*AstCompilationUnit:#" << this-> id << "*/";
  150.     // The file is
  151.     // os << lex_stream.FileName();
  152.     if (package_declaration_opt)
  153.         package_declaration_opt -> Unparse(os, lex_stream);
  154.     for (int m = 0; m < this -> NumImportDeclarations(); m++)
  155.         this -> ImportDeclaration(m) -> Unparse(os, lex_stream);
  156.     for (int n = 0; n < this -> NumTypeDeclarations(); n++)
  157.         this -> TypeDeclaration(n) -> Unparse(os, lex_stream);
  158.     if (Ast::debug_unparse)
  159.         os << "/*:AstCompilationUnit#" << this-> id << "*/";
  160. }
  161.  
  162. void AstModifier::Unparse(Ostream& os, LexStream& lex_stream)
  163. {
  164.     if (Ast::debug_unparse)
  165.         os << "/*AstModifier:#" << this-> id << "*/";
  166.     os << lex_stream.NameString(modifier_kind_token);
  167.     os << " ";
  168.     if (Ast::debug_unparse)
  169.        os << "/*:AstModifier#" << this-> id << "*/";
  170. }
  171.  
  172. void AstEmptyDeclaration::Unparse(Ostream& os, LexStream& lex_stream)
  173. {
  174.     if (Ast::debug_unparse)
  175.        os << "/*AstEmptyDeclaration:#" << this-> id << "*/";
  176.     os << lex_stream.NameString(semicolon_token);
  177.     os << "\n";
  178.     if (Ast::debug_unparse)
  179.     os << "/*:AstEmptyDeclaration#" << this-> id << "*/";
  180. }
  181.  
  182. void AstClassBody::Unparse(Ostream& os, LexStream& lex_stream)
  183. {
  184.     if (Ast::debug_unparse)
  185.         os << "/*AstClassBody:#" << this-> id << "*/";
  186.     os << "{\n";
  187.     for (int k = 0; k < this -> NumClassBodyDeclarations(); k++)
  188.         this -> ClassBodyDeclaration(k) -> Unparse(os, lex_stream);
  189.     os << "}\n\n";
  190.     if (Ast::debug_unparse)
  191.         os << "/*:AstClassBody#" << this-> id << "*/";
  192. }
  193.  
  194. void AstClassDeclaration::Unparse(Ostream& os, LexStream& lex_stream)
  195. {
  196.     if (Ast::debug_unparse)
  197.         os << "/*AstClassDeclaration:#" << this-> id << "*/";
  198.     for (int i = 0; i < this -> NumClassModifiers(); i++)
  199.     {
  200.         os << lex_stream.NameString(this -> ClassModifier(i) -> modifier_kind_token);
  201.         os << " ";
  202.     }
  203.     os << lex_stream.NameString(class_token);
  204.     os << " ";
  205.     os << lex_stream.NameString(identifier_token);
  206.     os << " ";
  207.     if (super_opt)
  208.     {
  209.         os << "extends ";
  210.         super_opt -> Unparse(os, lex_stream);
  211.         os << " ";
  212.     }
  213.     if (NumInterfaces() > 0)
  214.     {
  215.         os << "implements ";
  216.         for (int j = 0; j < NumInterfaces(); j++)
  217.         {
  218.             if (j>0)
  219.                 os << ", ";
  220.             this -> Interface(j) -> Unparse(os, lex_stream);
  221.         }
  222.         os << " ";
  223.     }
  224.     // os << ") #" << class_body -> id << "\n";
  225.     class_body -> Unparse(os, lex_stream);
  226.     if (Ast::debug_unparse)
  227.         os << "/*:AstClassDeclaration#" << this-> id << "*/";
  228. }
  229.  
  230. void AstArrayInitializer::Unparse(Ostream& os, LexStream& lex_stream)
  231. {
  232.     if (Ast::debug_unparse)
  233.         os << "/*AstArrayInitializer:#" << this-> id << "*/";
  234.     os << "\n{ ";
  235.     for (int k = 0; k < NumVariableInitializers(); k++)
  236.     {
  237.         if (k>0)
  238.             os << ", ";
  239.         this -> VariableInitializer(k) -> Unparse(os, lex_stream);
  240.     }
  241.     os << " }";
  242.     if (Ast::debug_unparse)
  243.         os << "/*:AstArrayInitializer#" << this-> id << "*/";
  244. }
  245.  
  246. void AstBrackets::Unparse(Ostream& os, LexStream& lex_stream)
  247. {
  248.     if (Ast::debug_unparse)
  249.         os << "/*AstBrackets:#" << this-> id << "*/";
  250.     os << "[]";
  251.     if (Ast::debug_unparse)
  252.         os << "/*:AstBrackets#" << this-> id << "*/";
  253. }
  254.  
  255. void AstVariableDeclaratorId::Unparse(Ostream& os, LexStream& lex_stream)
  256. {
  257.     if (Ast::debug_unparse)
  258.         os << "/*AstVariableDeclaratorId:#" << this-> id << "*/";
  259.     os << lex_stream.NameString(identifier_token);
  260.     for (int i = 0; i < NumBrackets(); i++)
  261.         os << "[]";
  262.     if (Ast::debug_unparse)
  263.         os << "/*:AstVariableDeclaratorId#" << this-> id << "*/";
  264. }
  265.  
  266. void AstVariableDeclarator::Unparse(Ostream& os, LexStream& lex_stream)
  267. {
  268.     if (Ast::debug_unparse)
  269.         os << "/*AstVariableDeclarator:#" << this-> id << "*/";
  270.     variable_declarator_name -> Unparse(os, lex_stream);
  271.     if (variable_initializer_opt)
  272.     {
  273.         os << " = ";
  274.         variable_initializer_opt -> Unparse(os, lex_stream);
  275.     }
  276.     if (Ast::debug_unparse)
  277.         os << "/*:AstVariableDeclarator#" << this-> id << "*/";
  278. }
  279.  
  280. void AstFieldDeclaration::Unparse(Ostream& os, LexStream& lex_stream)
  281. {
  282.     if (Ast::debug_unparse)
  283.         os << "/*AstFieldDeclaration:#" << this-> id << "*/";
  284.     for (int i = 0; i < this -> NumVariableModifiers(); i++)
  285.     {
  286.         os << lex_stream.NameString(this -> VariableModifier(i) -> modifier_kind_token);
  287.         os << " ";
  288.     }
  289.     type -> Unparse(os, lex_stream);
  290.     os << " ";
  291.     for (int k = 0; k < this -> NumVariableDeclarators(); k++)
  292.     {
  293.         if (k>0)
  294.             os << " ,";
  295.         this -> VariableDeclarator(k) -> Unparse(os, lex_stream);
  296.     }
  297.     os << ";\n";
  298.     if (Ast::debug_unparse)
  299.         os << "/*:AstFieldDeclaration#" << this-> id << "*/";
  300. }
  301.  
  302. void AstFormalParameter::Unparse(Ostream& os, LexStream& lex_stream)
  303. {
  304.     if (Ast::debug_unparse)
  305.         os << "/*AstFormalParameter:#" << this-> id << "*/";
  306.     for (int i = 0; i < this -> NumParameterModifiers(); i++)
  307.     {
  308.         os << lex_stream.NameString(this -> ParameterModifier(i) -> modifier_kind_token);
  309.         os << " ";
  310.     }
  311.     type -> Unparse(os, lex_stream);
  312.     os << " ";
  313.     formal_declarator -> Unparse(os, lex_stream);
  314.     if (Ast::debug_unparse)
  315.         os << "/*:AstFormalParameter#" << this-> id << "*/";
  316. }
  317.  
  318. void AstMethodDeclarator::Unparse(Ostream& os, LexStream& lex_stream)
  319. {
  320.     if (Ast::debug_unparse)
  321.         os << "/*AstMethodDeclarator:#" << this-> id << "*/";
  322.     os << lex_stream.NameString(identifier_token);
  323.     os << " (";
  324.     for (int k = 0; k < this -> NumFormalParameters(); k++)
  325.     {
  326.         if (k>0)
  327.             os << ", ";
  328.         this -> FormalParameter(k) -> Unparse(os, lex_stream);
  329.     }
  330.     os << ") ";
  331.     for (int i = 0; i < NumBrackets(); i++)
  332.         os << "[]";
  333.     if (Ast::debug_unparse)
  334.         os << "/*:AstMethodDeclarator#" << this-> id << "*/";
  335. }
  336.  
  337. void AstMethodDeclaration::Unparse(Ostream& os, LexStream& lex_stream)
  338. {
  339.     if (Ast::debug_unparse)
  340.         os << "/*AstMethodDeclaration:#" << this-> id << "*/";
  341.     for (int i = 0; i < this -> NumMethodModifiers(); i++)
  342.     {
  343.         os << lex_stream.NameString(this -> MethodModifier(i) -> modifier_kind_token);
  344.         os << " ";
  345.     }
  346.     type -> Unparse(os, lex_stream);
  347.     os << " ";
  348.     method_declarator -> Unparse(os, lex_stream);
  349.     if (NumThrows() > 0)
  350.     {
  351.         os << " throws ";
  352.         for (int k = 0; k < this -> NumThrows(); k++)
  353.         {
  354.             if (k>0)
  355.                 os << ", ";
  356.             this -> Throw(k) -> Unparse(os, lex_stream);
  357.         }
  358.     }
  359.     method_body -> Unparse(os, lex_stream);
  360.     if (Ast::debug_unparse)
  361.         os << "/*:AstMethodDeclaration#" << this-> id << "*/";
  362. }
  363.  
  364. void AstStaticInitializer::Unparse(Ostream& os, LexStream& lex_stream)
  365. {
  366.     if (Ast::debug_unparse)
  367.         os << "/*AstStaticInitializer:#" << this-> id << "*/";
  368.     os << lex_stream.NameString(static_token);
  369.     block -> Unparse(os, lex_stream);
  370.     if (Ast::debug_unparse)
  371.         os << "/*:AstStaticInitializer#" << this-> id << "*/";
  372. }
  373.  
  374. void AstThisCall::Unparse(Ostream& os, LexStream& lex_stream)
  375. {
  376.     if (Ast::debug_unparse)
  377.         os << "/*AstThisCall:#" << this-> id << "*/";
  378.     os << lex_stream.NameString(this_token);
  379.     os << " (";
  380.     for (int i = 0; i < this -> NumArguments(); i++)
  381.     {
  382.         if (i>0)
  383.             os << ", ";
  384.         this -> Argument(i) -> Unparse(os, lex_stream);
  385.     }
  386.     os << ");\n";
  387.     if (Ast::debug_unparse)
  388.         os << "/*:AstThisCall#" << this-> id << "*/";
  389. }
  390.  
  391. void AstSuperCall::Unparse(Ostream& os, LexStream& lex_stream)
  392. {
  393.     if (Ast::debug_unparse)
  394.         os << "/*AstSuperCall:#" << this-> id << "*/";
  395.     if (wcscmp(lex_stream.NameString(super_token), L"super") == 0)
  396.     {
  397.         if (base_opt)
  398.         {
  399.             base_opt -> Unparse(os, lex_stream);
  400.             os << lex_stream.NameString(dot_token_opt);
  401.         }
  402.         os << lex_stream.NameString(super_token);
  403.         os << lex_stream.NameString(left_parenthesis_token);
  404.         for (int j = 0; j < NumArguments(); j++)
  405.         {
  406.             if (j>0)
  407.                 os << ", ";
  408.             this -> Argument(j) -> Unparse(os, lex_stream);
  409.         }
  410.         os << lex_stream.NameString(right_parenthesis_token);
  411.         os << lex_stream.NameString(semicolon_token);
  412.         os << "\n";
  413.     }
  414.     if (Ast::debug_unparse)
  415.          os << "/*:AstSuperCall#" << this-> id << "*/";
  416. }
  417.  
  418. void AstConstructorBlock::Unparse(Ostream& os, LexStream& lex_stream)
  419. {
  420.     if (Ast::debug_unparse)
  421.         os << "/*AstConstructorBlock:#" << this-> id << "*/";
  422.     if (explicit_constructor_invocation_opt)
  423.     {
  424.         os << "{\n";
  425.         explicit_constructor_invocation_opt -> Unparse(os, lex_stream);
  426.         // os << ";\n";
  427.     }
  428.     block -> Unparse(os, lex_stream);
  429.     if (explicit_constructor_invocation_opt)
  430.         os << "}\n";
  431.     if (Ast::debug_unparse)
  432.         os << "/*:AstConstructorBlock#" << this-> id << "*/";
  433. }
  434.  
  435. void AstConstructorDeclaration::Unparse(Ostream& os, LexStream& lex_stream)
  436. {
  437.     if (Ast::debug_unparse)
  438.         os << "/*AstConstructorDeclaration:#" << this-> id << "*/";
  439.     for (int i = 0; i < this -> NumConstructorModifiers(); i++)
  440.     {
  441.         os << lex_stream.NameString(this -> ConstructorModifier(i) -> modifier_kind_token);
  442.         os << " ";
  443.     }
  444.     constructor_declarator -> Unparse(os, lex_stream);
  445.     if (NumThrows() > 0)
  446.     {
  447.         os << " throws ";
  448.         for (int k = 0; k < this -> NumThrows(); k++)
  449.         {
  450.             if (k>0) os << ", ";
  451.             this -> Throw(k) -> Unparse(os, lex_stream);
  452.         }
  453.     }
  454.     constructor_body -> Unparse(os, lex_stream);
  455.     if (Ast::debug_unparse)
  456.         os << "/*:AstConstructorDeclaration#" << this-> id << "*/";
  457. }
  458.  
  459. void AstInterfaceDeclaration::Unparse(Ostream& os, LexStream& lex_stream)
  460. {
  461.     if (Ast::debug_unparse)
  462.         os << "/*AstInterfaceDeclaration:#" << this-> id << "*/";
  463.     for (int i = 0; i < this -> NumInterfaceModifiers(); i++)
  464.     {
  465.         os << lex_stream.NameString(this -> InterfaceModifier(i) -> modifier_kind_token);
  466.         os << " ";
  467.     }
  468.     os << lex_stream.NameString(interface_token);
  469.     os << " ";
  470.     os << lex_stream.NameString(identifier_token);
  471.     if (NumExtendsInterfaces() > 0)
  472.     {
  473.         os << " extends ";
  474.         for (int j = 0; j < NumExtendsInterfaces(); j++)
  475.         {
  476.             if (j>0)
  477.                 os << ", ";
  478.             this -> ExtendsInterface(j) -> Unparse(os, lex_stream);
  479.         }
  480.     }
  481.     os << " {\n";
  482.     for (int k = 0; k < NumInterfaceMemberDeclarations(); k++)
  483.     {
  484.         this -> InterfaceMemberDeclaration(k) -> Unparse(os, lex_stream);
  485.         os << "\n";
  486.     }
  487.     os << "}\n";
  488.     if (Ast::debug_unparse)
  489.         os << "/*:AstInterfaceDeclaration#" << this-> id << "*/";
  490. }
  491.  
  492. void AstLocalVariableDeclarationStatement::Unparse(Ostream& os, LexStream& lex_stream)
  493. {
  494.     if (Ast::debug_unparse)
  495.         os << "/*AstLocalVariableDeclarationStatement:#" << this-> id << "*/";
  496.     for (int i = 0; i < this -> NumLocalModifiers(); i++)
  497.     {
  498.         os << lex_stream.NameString(this -> LocalModifier(i) -> modifier_kind_token);
  499.         os << " ";
  500.     }
  501.     type -> Unparse(os, lex_stream);
  502.     os << " ";
  503.     for (int k = 0; k < this -> NumVariableDeclarators(); k++)
  504.     {
  505.         if (k>0)
  506.             os << ",";
  507.         this -> VariableDeclarator(k) -> Unparse(os, lex_stream);
  508.     }
  509.     if (semicolon_token_opt)
  510.         os << ";\n";
  511.     if (Ast::debug_unparse)
  512.         os << "/*:AstLocalVariableDeclarationStatement#" << this-> id << "*/";
  513. }
  514.  
  515. void AstIfStatement::Unparse(Ostream& os, LexStream& lex_stream)
  516. {
  517.     if (Ast::debug_unparse)
  518.         os << "/*AstIfStatement:#" << this-> id << "*/";
  519.     os << lex_stream.NameString(if_token);
  520.     AstParenthesizedExpression *parenth = expression -> ParenthesizedExpressionCast();
  521.     if (!parenth)
  522.         os << "(";
  523.     expression -> Unparse(os, lex_stream);
  524.     if (!parenth)
  525.         os << ")";
  526.     os << "\n";
  527.     true_statement -> Unparse(os, lex_stream);
  528.     if (false_statement_opt)
  529.     {
  530.         os << "else\n";
  531.         false_statement_opt -> Unparse(os, lex_stream);
  532.     }
  533.     os << "\n";
  534.     if (Ast::debug_unparse)
  535.         os << "/*:AstIfStatement#" << this-> id << "*/";
  536. }
  537.  
  538. void AstEmptyStatement::Unparse(Ostream& os, LexStream& lex_stream)
  539. {
  540.     if (Ast::debug_unparse)
  541.         os << "/*AstEmptyStatement:#" << this-> id << "*/";
  542.     os << lex_stream.NameString(semicolon_token);
  543.     os << "\n";
  544.     if (Ast::debug_unparse)
  545.         os << "/*:AstEmptyStatement#" << this-> id << "*/";
  546. }
  547.  
  548. void AstExpressionStatement::Unparse(Ostream& os, LexStream& lex_stream)
  549. {
  550.     if (Ast::debug_unparse)
  551.         os << "/*AstExpressionStatement:#" << this-> id << "*/";
  552.     expression -> Unparse(os, lex_stream);
  553.     if (semicolon_token_opt)
  554.         os << ";\n";
  555.     if (Ast::debug_unparse)
  556.         os << "/*:AstExpressionStatement#" << this-> id << "*/";
  557. }
  558.  
  559. void AstCaseLabel::Unparse(Ostream& os, LexStream& lex_stream)
  560. {
  561.     if (Ast::debug_unparse)
  562.         os << "/*AstCaseLabel:#" << this-> id << "*/";
  563.     os << lex_stream.NameString(case_token);
  564.     os << " ";
  565.     expression -> Unparse(os, lex_stream);
  566.     os << ":\n";
  567.     if (Ast::debug_unparse)
  568.         os << "/*:AstCaseLabel#" << this-> id << "*/";
  569. }
  570.  
  571. void AstDefaultLabel::Unparse(Ostream& os, LexStream& lex_stream)
  572. {
  573.     if (Ast::debug_unparse)
  574.         os << "/*AstDefaultLabel:#" << this-> id << "*/";
  575.     os << lex_stream.NameString(default_token);
  576.     os << ":\n";
  577.     if (Ast::debug_unparse)
  578.         os << "/*:AstDefaultLabel#" << this-> id << "*/";
  579. }
  580.  
  581. void AstSwitchBlockStatement::Unparse(Ostream& os, LexStream& lex_stream)
  582. {
  583.     if (Ast::debug_unparse)
  584.         os << "/*AstSwitchBlockStatement:#" << this-> id << "*/";
  585.     for (int j = 0; j < NumSwitchLabels(); j++)
  586.         this -> SwitchLabel(j) -> Unparse(os, lex_stream);
  587.     for (int l = 0; l < NumStatements(); l++)
  588.         this -> Statement(l) -> Unparse(os, lex_stream);
  589.     if (Ast::debug_unparse)
  590.         os << "/*:AstSwitchBlockStatement#" << this-> id << "*/";
  591. }
  592.  
  593. void AstSwitchStatement::Unparse(Ostream& os, LexStream& lex_stream)
  594. {
  595.     if (Ast::debug_unparse)
  596.         os << "/*AstSwitchStatement:#" << this-> id << "*/";
  597.   // What about the label_opt??
  598.     os << lex_stream.NameString(switch_token);
  599.     AstParenthesizedExpression *parenth = expression -> ParenthesizedExpressionCast();
  600.     if (!parenth)
  601.         os << "(";
  602.     expression -> Unparse(os, lex_stream);
  603.     if (!parenth)
  604.         os << ")";
  605.     // I think that switch_block will output its own braces.
  606.     // os << "{\n";
  607.     switch_block -> Unparse(os, lex_stream);
  608.     // what about switch_labels_opt?
  609.     // os << "}\n";
  610.     if (Ast::debug_unparse)
  611.         os << "/*:AstSwitchStatement#" << this-> id << "*/";
  612. }
  613.  
  614. void AstWhileStatement::Unparse(Ostream& os, LexStream& lex_stream)
  615. {
  616.     if (Ast::debug_unparse)
  617.         os << "/*AstWhileStatement:#" << this-> id << "*/";
  618.     os << lex_stream.NameString(while_token);
  619.     // What about Label_opt?
  620.     os << " ";
  621.     AstParenthesizedExpression *parenth = expression -> ParenthesizedExpressionCast();
  622.     if (!parenth)
  623.         os << "(";
  624.     expression -> Unparse(os, lex_stream);
  625.     if (!parenth)
  626.         os << ")";
  627.     os << "\n";
  628.     statement -> Unparse(os, lex_stream);
  629.     if (Ast::debug_unparse)
  630.         os << "/*:AstWhileStatement#" << this-> id << "*/";
  631. }
  632.  
  633. void AstDoStatement::Unparse(Ostream& os, LexStream& lex_stream)
  634. {
  635.     if (Ast::debug_unparse)
  636.         os << "/*AstDoStatement:#" << this-> id << "*/";
  637.     os << lex_stream.NameString(do_token);
  638.     os << "\n";
  639.     statement -> Unparse(os, lex_stream);
  640.     os << lex_stream.NameString(while_token);
  641.     AstParenthesizedExpression *parenth = expression -> ParenthesizedExpressionCast();
  642.     if (!parenth)
  643.         os << "(";
  644.     expression -> Unparse(os, lex_stream);
  645.     if (!parenth)
  646.         os << ")";
  647.     os << lex_stream.NameString(semicolon_token);
  648.     os << "\n";
  649.     if (Ast::debug_unparse)
  650.         os << "/*:AstDoStatement#" << this-> id << "*/";
  651. }
  652.  
  653. void AstForStatement::Unparse(Ostream& os, LexStream& lex_stream)
  654. {
  655.     if (Ast::debug_unparse)
  656.         os << "/*AstForStatement:#" << this-> id << "*/";
  657.     os << lex_stream.NameString(for_token);
  658.     os << " (";
  659.     for (int i = 0; i < this -> NumForInitStatements(); i++)
  660.     {
  661.         if (i>0)
  662.             os << ", ";
  663.         this -> ForInitStatement(i) -> Unparse(os, lex_stream);
  664.     }
  665.     os << "; ";
  666.     if (end_expression_opt)
  667.         end_expression_opt -> Unparse(os, lex_stream);
  668.     os << "; ";
  669.     for (int k = 0; k < this -> NumForUpdateStatements(); k++)
  670.     {
  671.         if (k>0)
  672.             os << ", ";
  673.         this -> ForUpdateStatement(k) -> Unparse(os, lex_stream);
  674.     }
  675.     os << ")\n";
  676.     statement -> Unparse(os, lex_stream);
  677.     if (Ast::debug_unparse)
  678.         os << "/*:AstForStatement#" << this-> id << "*/";
  679. }
  680.  
  681. void AstBreakStatement::Unparse(Ostream& os, LexStream& lex_stream)
  682. {
  683.     if (Ast::debug_unparse)
  684.         os << "/*AstBreakStatement:#" << this-> id << "*/";
  685.     os << lex_stream.NameString(break_token);
  686.     if (identifier_token_opt)
  687.     {
  688.         os << " ";
  689.         os << lex_stream.NameString(identifier_token_opt);
  690.     }
  691.     os << ";\n";
  692.     if (Ast::debug_unparse)
  693.         os << "/*:AstBreakStatement#" << this-> id << "*/";
  694. }
  695.  
  696. void AstContinueStatement::Unparse(Ostream& os, LexStream& lex_stream)
  697. {
  698.     if (Ast::debug_unparse)
  699.         os << "/*AstContinueStatement:#" << this-> id << "*/";
  700.     os << lex_stream.NameString(continue_token);
  701.     if (identifier_token_opt)
  702.     {
  703.         os << " ";
  704.         os << lex_stream.NameString(identifier_token_opt);
  705.     }
  706.     os << ";\n";
  707.     if (Ast::debug_unparse)
  708.         os << "/*:AstContinueStatement#" << this-> id << "*/";
  709. }
  710.  
  711. void AstReturnStatement::Unparse(Ostream& os, LexStream& lex_stream)
  712. {
  713.     if (Ast::debug_unparse)
  714.         os << "/*AstReturnStatement:#" << this-> id << "*/";
  715.     // Do NOT use this; when the return statement is not literally
  716.     // present in the source, the return_token points at the next "}".
  717.     // os << lex_stream.NameString(return_token);
  718.     os << "return";
  719.     if (expression_opt)
  720.     {
  721.         os << " ";
  722.         expression_opt -> Unparse(os, lex_stream);
  723.     }
  724.     os << ";\n";
  725.     if (Ast::debug_unparse)
  726.         os << "/*:AstReturnStatement#" << this-> id << "*/";
  727. }
  728.  
  729. void AstThrowStatement::Unparse(Ostream& os, LexStream& lex_stream)
  730. {
  731.     if (Ast::debug_unparse)
  732.         os << "/*AstThrowStatement:#" << this-> id << "*/";
  733.     os << lex_stream.NameString(throw_token);
  734.     os << " ";
  735.     expression -> Unparse(os, lex_stream);
  736.     os << ";\n";
  737.     if (Ast::debug_unparse)
  738.         os << "/*:AstThrowStatement#" << this-> id << "*/";
  739. }
  740.  
  741. void AstSynchronizedStatement::Unparse(Ostream& os, LexStream& lex_stream)
  742. {
  743.     if (Ast::debug_unparse)
  744.         os << "/*AstSynchronizedStatement:#" << this-> id << "*/";
  745.     os << lex_stream.NameString(synchronized_token);
  746.     os << " ";
  747.     AstParenthesizedExpression *parenth = expression -> ParenthesizedExpressionCast();
  748.     if (!parenth)
  749.         os << "(";
  750.     expression -> Unparse(os, lex_stream);
  751.     if (!parenth)
  752.         os << ")";
  753.     os << "\n";
  754.     block -> Unparse(os, lex_stream);
  755.     if (Ast::debug_unparse)
  756.         os << "/*:AstSynchronizedStatement#" << this-> id << "*/";
  757. }
  758.  
  759. void AstCatchClause::Unparse(Ostream& os, LexStream& lex_stream)
  760. {
  761.     if (Ast::debug_unparse)
  762.         os << "/*AstCatchClause:#" << this-> id << "*/";
  763.     os << lex_stream.NameString(catch_token);
  764.     os << " (";
  765.     formal_parameter -> Unparse(os, lex_stream);
  766.     os << ")\n";
  767.     block -> Unparse(os, lex_stream);
  768.     if (Ast::debug_unparse)
  769.         os << "/*:AstCatchClause#" << this-> id << "*/";
  770. }
  771.  
  772. void AstFinallyClause::Unparse(Ostream& os, LexStream& lex_stream)
  773. {
  774.     if (Ast::debug_unparse)
  775.         os << "/*AstFinallyClause:#" << this-> id << "*/";
  776.     os << lex_stream.NameString(finally_token);
  777.     os << "\n";
  778.     block -> Unparse(os, lex_stream);
  779.     if (Ast::debug_unparse) os << "/*:AstFinallyClause#" << this-> id << "*/";
  780. }
  781.  
  782. void AstTryStatement::Unparse(Ostream& os, LexStream& lex_stream)
  783. {
  784.     if (Ast::debug_unparse)
  785.         os << "/*AstTryStatement:#" << this-> id << "*/";
  786.     os << lex_stream.NameString(try_token);
  787.     os << "\n";
  788.     block -> Unparse(os, lex_stream);
  789.     for (int k = 0; k < this -> NumCatchClauses(); k++)
  790.         this -> CatchClause(k) -> Unparse(os, lex_stream);
  791.     if (finally_clause_opt)
  792.         finally_clause_opt -> Unparse(os, lex_stream);
  793.     if (Ast::debug_unparse)
  794.         os << "/*:AstTryStatement#" << this-> id << "*/";
  795. }
  796.  
  797. void AstIntegerLiteral::Unparse(Ostream& os, LexStream& lex_stream)
  798. {
  799.     if (Ast::debug_unparse)
  800.         os << "/*AstIntegerLiteral:#" << this-> id << "*/";
  801.     os << lex_stream.NameString(integer_literal_token);
  802.     if (Ast::debug_unparse)
  803.         os << "/*:AstIntegerLiteral#" << this-> id << "*/";
  804. }
  805.  
  806. void AstLongLiteral::Unparse(Ostream& os, LexStream& lex_stream)
  807. {
  808.     if (Ast::debug_unparse)
  809.         os << "/*AstLongLiteral:#" << this-> id << "*/";
  810.     os << lex_stream.NameString(long_literal_token);
  811.     if (Ast::debug_unparse)
  812.         os << "/*:AstLongLiteral#" << this-> id << "*/";
  813. }
  814.  
  815. void AstFloatingPointLiteral::Unparse(Ostream& os, LexStream& lex_stream)
  816. {
  817.     if (Ast::debug_unparse)
  818.         os << "/*AstFloatingPointLiteral:#" << this-> id << "*/";
  819.     os << lex_stream.NameString(floating_point_literal_token);
  820.     if (Ast::debug_unparse)
  821.         os << "/*:AstFloatingPointLiteral#" << this-> id << "*/";
  822. }
  823.  
  824. void AstDoubleLiteral::Unparse(Ostream& os, LexStream& lex_stream)
  825. {
  826.     if (Ast::debug_unparse)
  827.         os << "/*AstDoubleLiteral:#" << this-> id << "*/";
  828.     os << lex_stream.NameString(double_literal_token);
  829.     if (Ast::debug_unparse)
  830.         os << "/*:AstDoubleLiteral#" << this-> id << "*/";
  831. }
  832.  
  833. void AstTrueLiteral::Unparse(Ostream& os, LexStream& lex_stream)
  834. {
  835.     if (Ast::debug_unparse)
  836.         os << "/*AstTrueLiteral:#" << this-> id << "*/";
  837.     os << lex_stream.NameString(true_literal_token);
  838.     if (Ast::debug_unparse)
  839.         os << "/*:AstTrueLiteral#" << this-> id << "*/";
  840. }
  841.  
  842. void AstFalseLiteral::Unparse(Ostream& os, LexStream& lex_stream)
  843. {
  844.     if (Ast::debug_unparse)
  845.         os << "/*AstFalseLiteral:#" << this-> id << "*/";
  846.     os << lex_stream.NameString(false_literal_token);
  847.     if (Ast::debug_unparse)
  848.         os << "/*:AstFalseLiteral#" << this-> id << "*/";
  849. }
  850.  
  851. void AstStringLiteral::Unparse(Ostream& os, LexStream& lex_stream)
  852. {
  853.     if (Ast::debug_unparse)
  854.         os << "/*AstStringLiteral:#" << this-> id << "*/";
  855.     {
  856.         bool old_expand = os.ExpandWchar();
  857.         os.SetExpandWchar(true);
  858.         os << lex_stream.NameString(string_literal_token), lex_stream.NameStringLength(string_literal_token);
  859.         os.SetExpandWchar(old_expand);
  860.     }
  861.     if (Ast::debug_unparse)
  862.         os << "/*:AstStringLiteral#" << this-> id << "*/";
  863. }
  864.  
  865. void AstCharacterLiteral::Unparse(Ostream& os, LexStream& lex_stream)
  866. {
  867.     if (Ast::debug_unparse)
  868.         os << "/*AstCharacterLiteral:#" << this-> id << "*/";
  869.     {
  870.         bool old_expand = os.ExpandWchar();
  871.         os.SetExpandWchar(true);
  872.         os << lex_stream.NameString(character_literal_token), lex_stream.NameStringLength(character_literal_token);
  873.         os.SetExpandWchar(old_expand);
  874.     }
  875.     if (Ast::debug_unparse)
  876.         os << "/*:AstCharacterLiteral#" << this-> id << "*/";
  877. }
  878.  
  879. void AstNullLiteral::Unparse(Ostream& os, LexStream& lex_stream)
  880. {
  881.     if (Ast::debug_unparse)
  882.         os << "/*AstNullLiteral:#" << this-> id << "*/";
  883.     os << lex_stream.NameString(null_token);
  884.     if (Ast::debug_unparse)
  885.         os << "/*:AstNullLiteral#" << this-> id << "*/";
  886. }
  887.  
  888. void AstThisExpression::Unparse(Ostream& os, LexStream& lex_stream)
  889. {
  890.     if (Ast::debug_unparse)
  891.         os << "/*AstThisExpression:#" << this-> id << "*/";
  892.     os << lex_stream.NameString(this_token);
  893.     if (Ast::debug_unparse)
  894.         os << "/*:AstThisExpression#" << this-> id << "*/";
  895. }
  896.  
  897. void AstSuperExpression::Unparse(Ostream& os, LexStream& lex_stream)
  898. {
  899.     if (Ast::debug_unparse)
  900.         os << "/*AstSuperExpression:#" << this-> id << "*/";
  901.     os << lex_stream.NameString(super_token);
  902.     if (Ast::debug_unparse)
  903.         os << "/*:AstSuperExpression#" << this-> id << "*/";
  904. }
  905.  
  906. void AstParenthesizedExpression::Unparse(Ostream& os, LexStream& lex_stream)
  907. {
  908.     if (Ast::debug_unparse)
  909.         os << "/*AstParenthesizedExpression:#" << this-> id << "*/";
  910.     os << lex_stream.NameString(left_parenthesis_token);
  911.     expression -> Unparse(os, lex_stream);
  912.     os << lex_stream.NameString(right_parenthesis_token);
  913.     if (Ast::debug_unparse)
  914.         os << "/*:AstParenthesizedExpression#" << this-> id << "*/";
  915. }
  916.  
  917. void AstTypeExpression::Unparse(Ostream& os, LexStream& lex_stream)
  918. {
  919.     if (Ast::debug_unparse)
  920.         os << "/*AstTypeExpression:#" << this-> id << "*/";
  921.     type -> Unparse(os, lex_stream);
  922.     if (Ast::debug_unparse)
  923.         os << "/*:AstTypeExpression#" << this-> id << "*/";
  924. }
  925.  
  926. void AstClassInstanceCreationExpression::Unparse(Ostream& os, LexStream& lex_stream)
  927. {
  928.     if (Ast::debug_unparse)
  929.         os << "/*AstClassInstanceCreationExpression:#" << this-> id << "*/";
  930.     if (dot_token_opt /* base_opt - see ast.h for explanation */)
  931.         base_opt -> Unparse(os, lex_stream);
  932.     os << lex_stream.NameString(new_token);
  933.     os << " ";
  934.     class_type -> Unparse(os, lex_stream);
  935.     os << "( ";
  936.     for (int j = 0; j < NumArguments(); j++)
  937.     {
  938.         if (j>0)
  939.             os << ", ";
  940.         this -> Argument(j) -> Unparse(os, lex_stream);
  941.     }
  942.     os << " )";
  943.  
  944.     if (class_body_opt)
  945.         class_body_opt -> Unparse(os, lex_stream);
  946.     if (Ast::debug_unparse)
  947.         os << "/*:AstClassInstanceCreationExpression#" << this-> id << "*/";
  948. }
  949.  
  950. void AstDimExpr::Unparse(Ostream& os, LexStream& lex_stream)
  951. {
  952.     if (Ast::debug_unparse)
  953.         os << "/*AstDimExpr:#" << this-> id << "*/";
  954.     os << "[";
  955.     expression -> Unparse(os, lex_stream);
  956.     os << "]";
  957.     if (Ast::debug_unparse)
  958.         os << "/*:AstDimExpr#" << this-> id << "*/";
  959. }
  960.  
  961. void AstArrayCreationExpression::Unparse(Ostream& os, LexStream& lex_stream)
  962. {
  963.     if (Ast::debug_unparse)
  964.         os << "/*AstArrayCreationExpression:#" << this-> id << "*/";
  965.     os << lex_stream.NameString(new_token);
  966.     os << " ";
  967.     array_type -> Unparse(os, lex_stream);
  968.     for (int i = 0; i < NumDimExprs(); i++)
  969.         DimExpr(i) -> Unparse(os, lex_stream);
  970.     for (int k = 0; k < NumBrackets(); k++)
  971.        os << "[]";
  972.     if (array_initializer_opt)
  973.         array_initializer_opt -> Unparse(os, lex_stream);
  974.     if (Ast::debug_unparse)
  975.         os << "/*:AstArrayCreationExpression#" << this-> id << "*/";
  976. }
  977.  
  978. void AstFieldAccess::Unparse(Ostream& os, LexStream& lex_stream)
  979. {
  980.     if (Ast::debug_unparse)
  981.         os << "/*AstFieldAccess:#" << this-> id << "*/";
  982.     base -> Unparse(os, lex_stream);
  983.     os << lex_stream.NameString(dot_token);
  984.     os << lex_stream.NameString(identifier_token);
  985.     if (Ast::debug_unparse)
  986.         os << "/*:AstFieldAccess#" << this-> id << "*/";
  987. }
  988.  
  989. void AstMethodInvocation::Unparse(Ostream& os, LexStream& lex_stream)
  990. {
  991.     if (Ast::debug_unparse)
  992.         os << "/*AstMethodInvocation:#" << this-> id << "*/";
  993.     method -> Unparse(os, lex_stream);
  994.     os << "(";
  995.     for (int i = 0; i < this -> NumArguments(); i++)
  996.     {
  997.         if (i>0)
  998.             os << ", ";
  999.         this -> Argument(i) -> Unparse(os, lex_stream);
  1000.     }
  1001.     os << ")";
  1002.     if (Ast::debug_unparse)
  1003.         os << "/*:AstMethodInvocation#" << this-> id << "*/";
  1004. }
  1005.  
  1006. void AstArrayAccess::Unparse(Ostream& os, LexStream& lex_stream)
  1007. {
  1008.     if (Ast::debug_unparse)
  1009.         os << "/*AstArrayAccess:#" << this-> id << "*/";
  1010.     base -> Unparse(os, lex_stream);
  1011.     os << "[";
  1012.     expression -> Unparse(os, lex_stream);
  1013.     os << "]";
  1014.     if (Ast::debug_unparse)
  1015.         os << "/*:AstArrayAccess#" << this-> id << "*/";
  1016. }
  1017.  
  1018. void AstPostUnaryExpression::Unparse(Ostream& os, LexStream& lex_stream)
  1019. {
  1020.     if (Ast::debug_unparse)
  1021.         os << "/*AstPostUnaryExpression:#" << this-> id << "*/";
  1022.     expression -> Unparse(os, lex_stream);
  1023.     os << lex_stream.NameString(post_operator_token);
  1024.     if (Ast::debug_unparse)
  1025.         os << "/*:AstPostUnaryExpression#" << this-> id << "*/";
  1026. }
  1027.  
  1028. void AstPreUnaryExpression::Unparse(Ostream& os, LexStream& lex_stream)
  1029. {
  1030.     if (Ast::debug_unparse)
  1031.         os << "/*AstPreUnaryExpression:#" << this-> id << "*/";
  1032.     os << lex_stream.NameString(pre_operator_token);
  1033.     expression -> Unparse(os, lex_stream);
  1034.     if (Ast::debug_unparse)
  1035.         os << "/*:AstPreUnaryExpression#" << this-> id << "*/";
  1036. }
  1037.  
  1038. void AstCastExpression::Unparse(Ostream& os, LexStream& lex_stream)
  1039. {
  1040.     if (Ast::debug_unparse)
  1041.         os << "/*AstCastExpression:#" << this-> id << "*/";
  1042.     if (left_parenthesis_token_opt && type_opt)
  1043.     {
  1044.         os << "(";
  1045.         type_opt -> Unparse(os, lex_stream);
  1046.         for (int i = 0; i < NumBrackets(); i++)
  1047.             os << "[]";
  1048.         os << ")";
  1049.     }
  1050.  
  1051.     expression -> Unparse(os, lex_stream);
  1052.     if (Ast::debug_unparse)
  1053.         os << "/*:AstCastExpression#" << this-> id << "*/";
  1054. }
  1055.  
  1056. void AstBinaryExpression::Unparse(Ostream& os, LexStream& lex_stream)
  1057. {
  1058.     if (Ast::debug_unparse)
  1059.         os << "/*AstBinaryExpression:#" << this-> id << "*/";
  1060.     left_expression -> Unparse(os, lex_stream);
  1061.     os << " ";
  1062.     os << lex_stream.NameString(binary_operator_token);
  1063.     os << " ";
  1064.     right_expression -> Unparse(os, lex_stream);
  1065.     if (Ast::debug_unparse)
  1066.         os << "/*:AstBinaryExpression#" << this-> id << "*/";
  1067. }
  1068.  
  1069. void AstConditionalExpression::Unparse(Ostream& os, LexStream& lex_stream)
  1070. {
  1071.     if (Ast::debug_unparse)
  1072.         os << "/*AstConditionalExpression:#" << this-> id << "*/";
  1073.     test_expression -> Unparse(os, lex_stream);
  1074.     os << " ? ";
  1075.     true_expression -> Unparse(os, lex_stream);
  1076.     os << " : ";
  1077.     false_expression -> Unparse(os, lex_stream);
  1078.     if (Ast::debug_unparse)
  1079.         os << "/*:AstConditionalExpression#" << this-> id << "*/";
  1080. }
  1081.  
  1082. void AstAssignmentExpression::Unparse(Ostream& os, LexStream& lex_stream)
  1083. {
  1084.     if (Ast::debug_unparse)
  1085.         os << "/*AstAssignmentExpression:#" << this-> id << "*/";
  1086.     left_hand_side -> Unparse(os, lex_stream);
  1087.     os << " ";
  1088.     os << lex_stream.NameString(assignment_operator_token);
  1089.     os << " ";
  1090.     expression -> Unparse(os, lex_stream);
  1091.     if (Ast::debug_unparse)
  1092.         os << "/*:AstAssignmentExpression#" << this-> id << "*/";
  1093. }
  1094.  
  1095. #ifdef    HAVE_JIKES_NAMESPACE
  1096. }            // Close namespace Jikes block
  1097. #endif
  1098.  
  1099. #endif // JIKES_DEBUG
  1100.