001: /*
002: * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: /*
026: * COMPONENT_NAME: idl.toJava
027: *
028: * ORIGINS: 27
029: *
030: * Licensed Materials - Property of IBM
031: * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
032: * RMI-IIOP v1.0
033: *
034: * @(#)TypedefGen.java 1.20 07/05/05
035: */
036:
037: package com.sun.tools.corba.se.idl.toJavaPortable;
038:
039: // NOTES:
040: // -11aug1997<daz> No modification: comments for type_defs will appear in
041: // helper, holder classes as a result of modifications to routines
042: // makeHelper(), makeHolder() in class com.sun.tools.corba.se.idl.toJava.Util.
043: // -F46082.51<daz> Remove -stateful feature; javaStatefulName() obsolete.
044: // -D61056 <klr> Use Util.helperName
045:
046: import java.io.PrintWriter;
047:
048: import java.util.Enumeration;
049: import java.util.Hashtable;
050:
051: import com.sun.tools.corba.se.idl.InterfaceEntry;
052: import com.sun.tools.corba.se.idl.InterfaceState;
053: import com.sun.tools.corba.se.idl.PrimitiveEntry;
054: import com.sun.tools.corba.se.idl.SequenceEntry;
055: import com.sun.tools.corba.se.idl.StringEntry;
056: import com.sun.tools.corba.se.idl.StructEntry;
057: import com.sun.tools.corba.se.idl.SymtabEntry;
058: import com.sun.tools.corba.se.idl.TypedefEntry;
059: import com.sun.tools.corba.se.idl.UnionEntry;
060:
061: import com.sun.tools.corba.se.idl.constExpr.Expression;
062:
063: // Notes:
064:
065: /**
066: *
067: **/
068: public class TypedefGen implements
069: com.sun.tools.corba.se.idl.TypedefGen, JavaGenerator {
070: /**
071: * Public zero-argument constructor.
072: **/
073: public TypedefGen() {
074: } // ctor
075:
076: /**
077: *
078: **/
079: public void generate(Hashtable symbolTable, TypedefEntry t,
080: PrintWriter stream) {
081: this .symbolTable = symbolTable;
082: this .t = t;
083:
084: if (t.arrayInfo().size() > 0
085: || t.type() instanceof SequenceEntry)
086: generateHolder();
087: generateHelper();
088: } // generator
089:
090: /**
091: *
092: **/
093: protected void generateHolder() {
094: ((Factories) Compile.compiler.factories()).holder().generate(
095: symbolTable, t);
096: }
097:
098: /**
099: *
100: **/
101: protected void generateHelper() {
102: ((Factories) Compile.compiler.factories()).helper().generate(
103: symbolTable, t);
104: }
105:
106: ///////////////
107: // From JavaGenerator
108:
109: private boolean inStruct(TypedefEntry entry) {
110: boolean inStruct = false;
111: if (entry.container() instanceof StructEntry
112: || entry.container() instanceof UnionEntry)
113: inStruct = true;
114: else if (entry.container() instanceof InterfaceEntry) {
115: InterfaceEntry i = (InterfaceEntry) entry.container();
116: if (i.state() != null) {
117: Enumeration e = i.state().elements();
118: while (e.hasMoreElements())
119: if (((InterfaceState) e.nextElement()).entry == entry) {
120: inStruct = true;
121: break;
122: }
123: }
124: }
125: return inStruct;
126: } // inStruct
127:
128: public int helperType(int index, String indent,
129: TCOffsets tcoffsets, String name, SymtabEntry entry,
130: PrintWriter stream) {
131: TypedefEntry td = (TypedefEntry) entry;
132: boolean inStruct = inStruct(td);
133: if (inStruct)
134: tcoffsets.setMember(entry);
135: else
136: tcoffsets.set(entry);
137:
138: // Print the base types typecode
139: index = ((JavaGenerator) td.type().generator()).type(index,
140: indent, tcoffsets, name, td.type(), stream);
141:
142: if (inStruct && td.arrayInfo().size() != 0)
143: tcoffsets.bumpCurrentOffset(4); // for array length field
144:
145: // Print the array typecodes (if there are any)
146: int dimensions = td.arrayInfo().size();
147: for (int i = 0; i < dimensions; ++i) {
148: String size = Util.parseExpression((Expression) td
149: .arrayInfo().elementAt(i));
150: stream.println(indent + name
151: + " = org.omg.CORBA.ORB.init ().create_array_tc ("
152: + size + ", " + name + " );");
153: }
154:
155: // If this typedef describes a struct/union member, don't put it
156: // in an alias typedef; otherwise that's where it belongs.
157: if (!inStruct)
158: // <54697>
159: //stream.println (indent + name + " = org.omg.CORBA.ORB.init ().create_alias_tc (id (), \"" + Util.stripLeadingUnderscores (td.name ()) + "\", " + name + ");");
160: stream.println(indent + name
161: + " = org.omg.CORBA.ORB.init ().create_alias_tc ("
162: + Util.helperName(td, true) + ".id (), \""
163: + Util.stripLeadingUnderscores(td.name()) + "\", "
164: + name + ");"); // <d61056>
165:
166: return index;
167: } // helperType
168:
169: public int type(int index, String indent, TCOffsets tcoffsets,
170: String name, SymtabEntry entry, PrintWriter stream) {
171: // The type() method is invoked from other emitters instead of when an IDL
172: // typedef statement is being processed. Code generated is identical minus the
173: // generation of a create_alias_tc() which is required for IDL typedef's but not
174: // needed when typedef is being processed as a member of struct/union/valuetype.
175:
176: return helperType(index, indent, tcoffsets, name, entry, stream);
177: } // type
178:
179: public void helperRead(String entryName, SymtabEntry entry,
180: PrintWriter stream) {
181: Util.writeInitializer(" ", "value", "", entry, stream);
182: read(0, " ", "value", entry, stream);
183: stream.println(" return value;");
184: } // helperRead
185:
186: public void helperWrite(SymtabEntry entry, PrintWriter stream) {
187: write(0, " ", "value", entry, stream);
188: } // helperWrite
189:
190: public int read(int index, String indent, String name,
191: SymtabEntry entry, PrintWriter stream) {
192: TypedefEntry td = (TypedefEntry) entry;
193: String modifier = Util.arrayInfo(td.arrayInfo());
194: if (!modifier.equals("")) {
195: // arrayInfo is a vector of Expressions which indicate the
196: // number of array dimensions for this typedef. But what if
197: // this is a typedef of a sequence?
198: // The `new' statement being generated must know the full
199: // number of brackets. That can be found in td.info.
200: // For instance:
201: // typedef sequence<short> A[10][10];
202: // void proc (out A a);
203: // typeModifier = "[10][10]"
204: // td.info = "short[][][]";
205: // The first new statement generated is:
206: // a.value = new short[10][][];
207: // Note that the 3 sets of brackets come from td.info, not
208: // arrayInfo;
209: // The second new statement generated is:
210: // a.value[_i1] = new short[10][];
211: // ------------ ---- ------
212: // \ \ \
213: // name baseName arrayDcl
214: int closingBrackets = 0;
215: String loopIndex = "";
216: String baseName;
217: try {
218: baseName = (String) td
219: .dynamicVariable(Compile.typedefInfo);
220: } catch (NoSuchFieldException e) {
221: baseName = td.name();
222: }
223: int startArray = baseName.indexOf('[');
224: String arrayDcl = Util.sansArrayInfo(baseName
225: .substring(startArray))
226: + "[]"; // Add an extra set because the first gets stripped off in the loop.
227: baseName = baseName.substring(0, startArray);
228:
229: // For interfaces having state, e.g., valuetypes.
230: SymtabEntry baseEntry = (SymtabEntry) Util.symbolTable
231: .get(baseName.replace('.', '/'));
232: if (baseEntry instanceof InterfaceEntry
233: && ((InterfaceEntry) baseEntry).state() != null)
234: // <f46082.51> Remove -stateful feature; javaStatefulName() obsolete.
235: //baseName = Util.javaStatefulName ((InterfaceEntry)baseEntry);
236: baseName = Util.javaName((InterfaceEntry) baseEntry);
237:
238: int end1stArray;
239: while (!modifier.equals("")) {
240: int rbracket = modifier.indexOf(']');
241: String size = modifier.substring(1, rbracket);
242: end1stArray = arrayDcl.indexOf(']');
243: arrayDcl = '[' + size
244: + arrayDcl.substring(end1stArray + 2);
245: stream.println(indent + name + " = new " + baseName
246: + arrayDcl + ';');
247: loopIndex = "_o" + index++;
248: stream.println(indent + "for (int " + loopIndex
249: + " = 0;" + loopIndex + " < (" + size + "); ++"
250: + loopIndex + ')');
251: stream.println(indent + '{');
252: ++closingBrackets;
253: modifier = modifier.substring(rbracket + 1);
254: indent = indent + " ";
255: name = name + '[' + loopIndex + ']';
256: }
257: end1stArray = arrayDcl.indexOf(']');
258: if (td.type() instanceof SequenceEntry
259: || td.type() instanceof PrimitiveEntry
260: || td.type() instanceof StringEntry)
261: index = ((JavaGenerator) td.type().generator()).read(
262: index, indent, name, td.type(), stream);
263: else if (td.type() instanceof InterfaceEntry
264: && td.type().fullName().equals(
265: "org/omg/CORBA/Object"))
266: stream.println(indent + name
267: + " = istream.read_Object ();");
268: else
269: stream.println(indent + name + " = "
270: + Util.helperName(td.type(), true)
271: + ".read (istream);"); // <d61056>
272: for (int i = 0; i < closingBrackets; ++i) {
273: indent = indent.substring(2);
274: stream.println(indent + '}');
275: }
276: } else {
277: SymtabEntry tdtype = Util.typeOf(td.type());
278: if (tdtype instanceof SequenceEntry
279: || tdtype instanceof PrimitiveEntry
280: || tdtype instanceof StringEntry)
281: index = ((JavaGenerator) tdtype.generator()).read(
282: index, indent, name, tdtype, stream);
283: else if (tdtype instanceof InterfaceEntry
284: && tdtype.fullName().equals("org/omg/CORBA/Object"))
285: stream.println(indent + name
286: + " = istream.read_Object ();");
287: else
288: stream.println(indent + name + " = "
289: + Util.helperName(tdtype, true)
290: + ".read (istream);"); // <d61056>
291: }
292: return index;
293: } // read
294:
295: public int write(int index, String indent, String name,
296: SymtabEntry entry, PrintWriter stream) {
297: TypedefEntry td = (TypedefEntry) entry;
298: String modifier = Util.arrayInfo(td.arrayInfo());
299: if (!modifier.equals("")) {
300: int closingBrackets = 0;
301: String loopIndex = "";
302: while (!modifier.equals("")) {
303: int rbracket = modifier.indexOf(']');
304: String size = modifier.substring(1, rbracket);
305: stream.println(indent + "if (" + name + ".length != ("
306: + size + "))");
307: stream
308: .println(indent
309: + " throw new org.omg.CORBA.MARSHAL (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);");
310: loopIndex = "_i" + index++;
311: stream.println(indent + "for (int " + loopIndex
312: + " = 0;" + loopIndex + " < (" + size + "); ++"
313: + loopIndex + ')');
314: stream.println(indent + '{');
315: ++closingBrackets;
316: modifier = modifier.substring(rbracket + 1);
317: indent = indent + " ";
318: name = name + '[' + loopIndex + ']';
319: }
320: if (td.type() instanceof SequenceEntry
321: || td.type() instanceof PrimitiveEntry
322: || td.type() instanceof StringEntry)
323: index = ((JavaGenerator) td.type().generator()).write(
324: index, indent, name, td.type(), stream);
325: else if (td.type() instanceof InterfaceEntry
326: && td.type().fullName().equals(
327: "org/omg/CORBA/Object"))
328: stream.println(indent + "ostream.write_Object (" + name
329: + ");");
330: else
331: stream.println(indent
332: + Util.helperName(td.type(), true)
333: + ".write (ostream, " + name + ");"); // <d61056>
334: for (int i = 0; i < closingBrackets; ++i) {
335: indent = indent.substring(2);
336: stream.println(indent + '}');
337: }
338: } else {
339: SymtabEntry tdtype = Util.typeOf(td.type());
340: if (tdtype instanceof SequenceEntry
341: || tdtype instanceof PrimitiveEntry
342: || tdtype instanceof StringEntry)
343: index = ((JavaGenerator) tdtype.generator()).write(
344: index, indent, name, tdtype, stream);
345: else if (tdtype instanceof InterfaceEntry
346: && tdtype.fullName().equals("org/omg/CORBA/Object"))
347: stream.println(indent + "ostream.write_Object (" + name
348: + ");");
349: else
350: stream.println(indent + Util.helperName(tdtype, true)
351: + ".write (ostream, " + name + ");"); // <d61056>
352: }
353: return index;
354: } // write
355:
356: // From JavaGenerator
357: ////////////////
358:
359: protected Hashtable symbolTable = null;
360: protected TypedefEntry t = null;
361: } // class TypedefGen
|