SequenceGen.java revision 608:7e06bf1dcb09
1243791Sdim/*
2243791Sdim * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
3243791Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4243791Sdim *
5243791Sdim * This code is free software; you can redistribute it and/or modify it
6243791Sdim * under the terms of the GNU General Public License version 2 only, as
7243791Sdim * published by the Free Software Foundation.  Oracle designates this
8243791Sdim * particular file as subject to the "Classpath" exception as provided
9243791Sdim * by Oracle in the LICENSE file that accompanied this code.
10243791Sdim *
11243791Sdim * This code is distributed in the hope that it will be useful, but WITHOUT
12243791Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13243791Sdim * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14243791Sdim * version 2 for more details (a copy is included in the LICENSE file that
15243791Sdim * accompanied this code).
16243791Sdim *
17243791Sdim * You should have received a copy of the GNU General Public License version
18251662Sdim * 2 along with this work; if not, write to the Free Software Foundation,
19263508Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20249423Sdim *
21251662Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22243791Sdim * or visit www.oracle.com if you need additional information or have any
23243791Sdim * questions.
24243791Sdim */
25243791Sdim/*
26243791Sdim * COMPONENT_NAME: idl.toJava
27243791Sdim *
28243791Sdim * ORIGINS: 27
29243791Sdim *
30243791Sdim * Licensed Materials - Property of IBM
31243791Sdim * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
32243791Sdim * RMI-IIOP v1.0
33251662Sdim *
34243791Sdim */
35243791Sdim
36243791Sdimpackage com.sun.tools.corba.se.idl.toJavaPortable;
37251662Sdim
38243791Sdim// NOTES:
39243791Sdim// -F46082.51<daz> Remove -stateful feature; javaStatefulName() obsolete.
40243791Sdim// -D61056   <klr> Use Util.helperName
41243791Sdim
42243791Sdimimport java.io.PrintWriter;
43263508Sdimimport java.util.Hashtable;
44243791Sdim
45243791Sdimimport com.sun.tools.corba.se.idl.InterfaceEntry;
46243791Sdimimport com.sun.tools.corba.se.idl.PrimitiveEntry;
47249423Sdimimport com.sun.tools.corba.se.idl.SequenceEntry;
48243791Sdimimport com.sun.tools.corba.se.idl.StringEntry;
49243791Sdimimport com.sun.tools.corba.se.idl.SymtabEntry;
50243791Sdim
51243791Sdimimport com.sun.tools.corba.se.idl.constExpr.Expression;
52243791Sdim
53243791Sdim/**
54263508Sdim *
55263508Sdim **/
56263508Sdimpublic class SequenceGen implements com.sun.tools.corba.se.idl.SequenceGen, JavaGenerator
57243791Sdim{
58243791Sdim  /**
59243791Sdim   * Public zero-argument constructor.
60243791Sdim   **/
61243791Sdim  public SequenceGen ()
62243791Sdim  {
63243791Sdim  } // ctor
64243791Sdim
65243791Sdim  /**
66243791Sdim   *
67243791Sdim   **/
68243791Sdim  public void generate (Hashtable symbolTable, SequenceEntry s, PrintWriter stream)
69243791Sdim  {
70243791Sdim  } // generator
71243791Sdim
72243791Sdim  ///////////////
73243791Sdim  // From JavaGenerator
74243791Sdim
75243791Sdim  public int helperType (int index, String indent, TCOffsets tcoffsets, String name, SymtabEntry entry, PrintWriter stream)
76251662Sdim  {
77243791Sdim    int offsetOfType = tcoffsets.offset (entry.type ().fullName ());
78243791Sdim    if (offsetOfType >= 0)
79243791Sdim    {
80243791Sdim      // This code uses the deprecated create_recursive_sequence_tc()
81243791Sdim      // It should be eliminated when the API is removed from the ORB class
82243791Sdim      // Regardles, this code will not be emitted since updated emitters invoke
83243791Sdim      // method type() below instead of helperType() when handling sequences
84251662Sdim
85251662Sdim      // This is a recursive sequence
86251662Sdim      tcoffsets.set (null);
87243791Sdim      Expression maxSize = ((SequenceEntry)entry).maxSize ();
88243791Sdim      if (maxSize == null)
89243791Sdim        stream.println (indent + name + " = org.omg.CORBA.ORB.init ().create_recursive_sequence_tc (0, " + (offsetOfType - tcoffsets.currentOffset ()) + ");");
90243791Sdim      else
91243791Sdim        stream.println (indent + name + " = org.omg.CORBA.ORB.init ().create_recursive_sequence_tc (" + Util.parseExpression (maxSize) + ", " + (offsetOfType - tcoffsets.currentOffset ()) + ");");
92243791Sdim      tcoffsets.bumpCurrentOffset (4); // add indirection field
93243791Sdim    }
94243791Sdim    else
95243791Sdim    {
96263508Sdim      // This is a normal sequence
97243791Sdim      tcoffsets.set (entry);
98243791Sdim      index = ((JavaGenerator)entry.type ().generator ()).helperType (index + 1, indent, tcoffsets, name, entry.type (), stream);
99243791Sdim      Expression maxSize = ((SequenceEntry)entry).maxSize ();
100243791Sdim      if (maxSize == null)
101243791Sdim        stream.println (indent + name + " = org.omg.CORBA.ORB.init ().create_sequence_tc (0, " + name + ");");
102243791Sdim      else
103243791Sdim        stream.println (indent + name + " = org.omg.CORBA.ORB.init ().create_sequence_tc (" + Util.parseExpression (maxSize) + ", " + name + ");");
104243791Sdim    }
105243791Sdim    tcoffsets.bumpCurrentOffset (4); // add on the seq max size
106243791Sdim    return index;
107243791Sdim  } // helperType
108243791Sdim
109243791Sdim  public int type (int index, String indent, TCOffsets tcoffsets, String name, SymtabEntry entry, PrintWriter stream) {
110243791Sdim    int offsetOfType = tcoffsets.offset (entry.type ().fullName ());
111243791Sdim    if (offsetOfType >= 0)
112243791Sdim    {
113263508Sdim      // This is a recursive sequence
114263508Sdim      tcoffsets.set (null);
115263508Sdim
116243791Sdim      // Need to fix later: how to get repositoryId of IDL type containing this sequence?
117243791Sdim      // entry.repositoryID().ID() returns empty string and
118243791Sdim      // Util.javaQualifiedName(entry) returns internal name which is not valid repId
119263508Sdim
120263508Sdim      stream.println (indent + name + " = org.omg.CORBA.ORB.init ().create_recursive_tc (" + "\"\"" + ");");
121263508Sdim      tcoffsets.bumpCurrentOffset (4); // add indirection field
122243791Sdim    }
123243791Sdim    else
124243791Sdim    {
125243791Sdim      // This is a normal sequence
126243791Sdim      tcoffsets.set (entry);
127243791Sdim      index = ((JavaGenerator)entry.type ().generator ()).type (index + 1, indent, tcoffsets, name, entry.type (), stream);
128243791Sdim      Expression maxSize = ((SequenceEntry)entry).maxSize ();
129243791Sdim      if (maxSize == null)
130243791Sdim        stream.println (indent + name + " = org.omg.CORBA.ORB.init ().create_sequence_tc (0, " + name + ");");
131243791Sdim      else
132243791Sdim        stream.println (indent + name + " = org.omg.CORBA.ORB.init ().create_sequence_tc (" + Util.parseExpression (maxSize) + ", " + name + ");");
133243791Sdim    }
134243791Sdim    //stream.println (indent + name + " = " + Util.helperName (entry, true) + ".type ();"); // <d61056>
135243791Sdim    return index;
136251662Sdim  } // type
137251662Sdim
138251662Sdim  public void helperRead (String entryName, SymtabEntry entry, PrintWriter stream)
139251662Sdim  {
140251662Sdim  } // helperRead
141251662Sdim
142243791Sdim  public void helperWrite (SymtabEntry entry, PrintWriter stream)
143243791Sdim  {
144243791Sdim  } // helperWrite
145243791Sdim
146243791Sdim  public int read (int index, String indent, String name, SymtabEntry entry, PrintWriter stream)
147243791Sdim  {
148243791Sdim    SequenceEntry seq = (SequenceEntry)entry;
149243791Sdim    String length = "_len" + index++;
150243791Sdim    stream.println (indent + "int " + length + " = istream.read_long ();");
151243791Sdim    if (seq.maxSize () != null)
152243791Sdim    {
153243791Sdim      stream.println (indent + "if (" + length + " > (" + Util.parseExpression (seq.maxSize ()) + "))");
154243791Sdim      stream.println (indent + "  throw new org.omg.CORBA.MARSHAL (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);");
155243791Sdim    }
156243791Sdim    String seqOfName;
157243791Sdim    try
158243791Sdim    {
159243791Sdim      seqOfName = Util.sansArrayInfo ((String)seq.dynamicVariable (Compile.typedefInfo));
160243791Sdim    }
161243791Sdim    catch (NoSuchFieldException e)
162243791Sdim    {
163243791Sdim      seqOfName = seq.name ();
164243791Sdim    }
165243791Sdim    int startArray = seqOfName.indexOf ('[');
166243791Sdim    String arrayDcl = seqOfName.substring (startArray);
167243791Sdim    seqOfName = seqOfName.substring (0, startArray);
168243791Sdim
169243791Sdim    // For interfaces having state, e.g., valuetypes.
170243791Sdim    SymtabEntry seqOfEntry = (SymtabEntry)Util.symbolTable.get (seqOfName.replace ('.', '/'));
171243791Sdim    if (seqOfEntry != null && seqOfEntry instanceof InterfaceEntry && ((InterfaceEntry)seqOfEntry).state () != null)
172243791Sdim      // <f46082.51> Remove -stateful feature; javaStatefulName() obsolete.
173243791Sdim      //seqOfName = Util.javaStatefulName ((InterfaceEntry)seqOfEntry);
174243791Sdim      seqOfName = Util.javaName ((InterfaceEntry)seqOfEntry);
175243791Sdim
176243791Sdim    arrayDcl = arrayDcl.substring (2);
177243791Sdim    stream.println (indent + name + " = new " + seqOfName + '[' + length + ']' + arrayDcl + ';');
178243791Sdim    if (seq.type () instanceof PrimitiveEntry)
179243791Sdim      // <d61961> Check for CORBA::Principal, too
180243791Sdim      //if (seq.type ().name ().equals ("any") || seq.type ().name ().equals ("TypeCode"))
181243791Sdim      if (seq.type ().name ().equals ("any") ||
182251662Sdim          seq.type ().name ().equals ("TypeCode") ||
183243791Sdim          seq.type ().name ().equals ("Principal"))
184243791Sdim      {
185243791Sdim        String loopIndex = "_o" + index;
186251662Sdim        stream.println (indent + "for (int " + loopIndex + " = 0;" + loopIndex + " < " + name + ".length; ++" + loopIndex + ')');
187243791Sdim        stream.println (indent + "  " + name + '[' + loopIndex + "] = istream.read_" + seq.type ().name () + " ();");
188251662Sdim      }
189251662Sdim      else
190243791Sdim      { // special case for ValueBox: if name is "xxx tmp", drop xxx
191243791Sdim        String varName = name;
192243791Sdim        int nameIndex = varName.indexOf (' ');
193243791Sdim        if ( nameIndex != -1 )
194243791Sdim          varName = varName.substring( nameIndex + 1 );
195243791Sdim        stream.println (indent + "istream.read_" + Util.collapseName (entry.type ().name ()) + "_array (" + varName + ", 0, " + length + ");");
196243791Sdim      }
197243791Sdim    else if (entry.type () instanceof StringEntry)
198243791Sdim    {
199243791Sdim      String loopIndex = "_o" + index;
200243791Sdim      stream.println (indent + "for (int " + loopIndex + " = 0;" + loopIndex + " < " + name + ".length; ++" + loopIndex + ')');
201243791Sdim      stream.println (indent + "  " + name + '[' + loopIndex + "] = istream.read_" + seq.type ().name () + " ();");
202243791Sdim    }
203243791Sdim    else if (entry.type () instanceof SequenceEntry)
204243791Sdim    {
205243791Sdim      String loopIndex = "_o" + index;
206243791Sdim      stream.println (indent + "for (int " + loopIndex + " = 0;" + loopIndex + " < " + name + ".length; ++" + loopIndex + ')');
207243791Sdim      stream.println (indent + '{');
208243791Sdim      index = ((JavaGenerator)seq.type ().generator ()).read (index, indent + "  ", name + '[' + loopIndex + ']', seq.type (), stream);
209243791Sdim      stream.println (indent + '}');
210243791Sdim    }
211243791Sdim    else
212243791Sdim    { // special case for ValueBox: if name is "xxx tmp", drop xxx
213243791Sdim      String varName = name;
214243791Sdim      int nameIndex = varName.indexOf (' ');
215243791Sdim      if ( nameIndex != -1 )
216243791Sdim        varName = varName.substring( nameIndex + 1 );
217243791Sdim      String loopIndex = "_o" + index;
218243791Sdim      stream.println (indent + "for (int " + loopIndex + " = 0;" + loopIndex + " < " + varName + ".length; ++" + loopIndex + ')');
219243791Sdim      stream.println (indent + "  " + varName + '[' + loopIndex + "] = " + Util.helperName (seq.type (), true) + ".read (istream);"); // <d61056>
220243791Sdim    }
221243791Sdim    return index;
222243791Sdim  } // read
223243791Sdim
224243791Sdim  public int write (int index, String indent, String name, SymtabEntry entry, PrintWriter stream)
225263508Sdim  {
226263508Sdim    SequenceEntry seq = (SequenceEntry)entry;
227263508Sdim    if (seq.maxSize () != null)
228263508Sdim    {
229263508Sdim      stream.println (indent + "if (" + name + ".length > (" + Util.parseExpression (seq.maxSize ()) + "))");
230243791Sdim      stream.println (indent + "  throw new org.omg.CORBA.MARSHAL (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);");
231243791Sdim    }
232243791Sdim    stream.println (indent + "ostream.write_long (" + name + ".length);");
233243791Sdim    if (entry.type () instanceof PrimitiveEntry)
234243791Sdim      // <d61961> Check for CORBA::Principal, too.
235243791Sdim      //if (entry.type ().name ().equals ("any") || entry.type ().name ().equals ("TypeCode"))
236243791Sdim      if (entry.type ().name ().equals ("any") ||
237243791Sdim          entry.type ().name ().equals ("TypeCode") ||
238243791Sdim          entry.type ().name ().equals ("Principal"))
239243791Sdim      {
240243791Sdim        String loopIndex = "_i" + index++;
241243791Sdim        stream.println (indent + "for (int " + loopIndex + " = 0;" + loopIndex + " < " + name + ".length; ++" + loopIndex + ')');
242243791Sdim        stream.println (indent + "  ostream.write_" + seq.type ().name () + " (" + name + '[' + loopIndex + "]);");
243243791Sdim      }
244243791Sdim      else
245243791Sdim        stream.println (indent + "ostream.write_" + Util.collapseName (entry.type ().name ()) + "_array (" + name + ", 0, " + name + ".length);");
246243791Sdim    else if (entry.type () instanceof StringEntry)
247243791Sdim    {
248243791Sdim      String loopIndex = "_i" + index++;
249243791Sdim      stream.println (indent + "for (int " + loopIndex + " = 0;" + loopIndex + " < " + name + ".length; ++" + loopIndex + ')');
250243791Sdim      stream.println (indent + "  ostream.write_" + seq.type ().name () + " (" + name + '[' + loopIndex + "]);");
251243791Sdim    }
252243791Sdim    else if (entry.type () instanceof SequenceEntry)
253243791Sdim    {
254243791Sdim      String loopIndex = "_i" + index++;
255243791Sdim      stream.println (indent + "for (int " + loopIndex + " = 0;" + loopIndex + " < " + name + ".length; ++" + loopIndex + ')');
256243791Sdim      stream.println (indent + '{');
257243791Sdim      index = ((JavaGenerator)seq.type ().generator ()).write (index, indent + "  ", name + '[' + loopIndex + ']', seq.type (), stream);
258243791Sdim      stream.println (indent + '}');
259243791Sdim    }
260243791Sdim    else
261243791Sdim    {
262243791Sdim      String loopIndex = "_i" + index++;
263243791Sdim      stream.println (indent + "for (int " + loopIndex + " = 0;" + loopIndex + " < " + name + ".length; ++" + loopIndex + ')');
264243791Sdim      stream.println (indent + "  " + Util.helperName (seq.type (), true) + ".write (ostream, " + name + '[' + loopIndex + "]);"); // <d61056>
265243791Sdim    }
266243791Sdim    return index;
267243791Sdim  } // write
268243791Sdim
269243791Sdim  // From JavaGenerator
270243791Sdim  ///////////////
271243791Sdim} // class SequenceGen
272243791Sdim