001: /*
002: * Copyright 1999-2001 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: * @(#)Compile.java 1.21 07/05/05
035: */
036:
037: package com.sun.tools.corba.se.idl.toJavaPortable;
038:
039: // NOTES:
040: // -09/23/98 KLR Ported -m updates (F46838.1-3)
041: // -f46082.51<daz> Transferred makefile list generation (for ODE delta-builds,
042: // see f46838) to toJava; cleaned-out dead code.
043: // -D58319<daz> Display version info. for -version option.
044: // -D58951<daz> Modify to allow QuickTest to build.
045: // -D49526<daz> Remove "TypeCode" symbol from preParse().
046: // -D58591<daz> Publicise _factories and compile for QuickTest. Need to revert
047: // t0 private and add accessor methods.
048: // -D59437<daz> Fill typename information for value boxes.
049:
050: import java.io.File;
051: import java.io.IOException;
052: import java.io.BufferedWriter;
053: import java.io.FileWriter;
054: import java.io.PrintWriter;
055:
056: import java.util.Enumeration;
057: import java.util.Hashtable;
058: import java.util.Vector;
059:
060: import com.sun.tools.corba.se.idl.GenFileStream;
061: import com.sun.tools.corba.se.idl.SymtabFactory;
062: import com.sun.tools.corba.se.idl.IncludeEntry;
063: import com.sun.tools.corba.se.idl.InterfaceEntry;
064: import com.sun.tools.corba.se.idl.InterfaceState;
065: import com.sun.tools.corba.se.idl.ModuleEntry;
066: import com.sun.tools.corba.se.idl.PrimitiveEntry;
067: import com.sun.tools.corba.se.idl.SequenceEntry;
068: import com.sun.tools.corba.se.idl.StructEntry;
069: import com.sun.tools.corba.se.idl.SymtabEntry;
070: import com.sun.tools.corba.se.idl.TypedefEntry;
071: import com.sun.tools.corba.se.idl.UnionBranch;
072: import com.sun.tools.corba.se.idl.UnionEntry;
073: import com.sun.tools.corba.se.idl.ValueEntry;
074: import com.sun.tools.corba.se.idl.ValueBoxEntry;
075: import com.sun.tools.corba.se.idl.InvalidArgument;
076:
077: /**
078: * Compiler usage:
079: * <br><br>
080: *
081: * java com.sun.tools.corba.se.idl.toJavaPortable.Compile [options] <idl file>
082: * <br><br>
083: *
084: * where <idl file> is the name of a file containing IDL definitions,
085: * and [options] is any combination of the options listed below. The options
086: * may appear in any order.
087: * <br><br>
088: *
089: * Options:
090: * <dl>
091: * <dt>-i <include path>
092: * <dd>By default, the current directory is scanned for included files.
093: * This option adds another directory. See also the note below.
094: *
095: * <dt>-d <symbol>
096: * <dd>This is equivalent to the following line in an IDL file:
097: * #define <symbol>
098: *
099: * <dt>-f<side>
100: * <dd>Defines what bindings to emit. <side> is one of client, server, all,
101: * serverTIE, allTIE. serverTIE and allTIE cause delegate model skeletons
102: * to be emitted. If this flag is not used, -fclient is assumed.
103: * allPOA has the same effect as all, except for generation POA type skeletons.
104: *
105: * <dt>-keep
106: * <dd>If a file to be generated already exists, do not overwrite it. By
107: * default it is overwritten.
108: *
109: * <dt>-sep <string>
110: * <dd>Only valid with -m. Replace the file separator character with
111: * <string> in the file names listed in the .u file.
112: *
113: * <dt>-emitAll
114: * <dd>Emit all types, including those found in #included files.
115: *
116: * <dt>-v
117: * <dd>Verbose mode.
118: *
119: * <dt>-pkgPrefix <type> <package>
120: * <dd>Whereever <type> is encountered, make sure it resides within
121: * <package> in all generated files. <type> is a fully
122: * qualified, java-style name.
123: * </dl>
124: *
125: * <B>Note:</B> If you have an include path or paths that you will always
126: * be using, it can get tedious putting these on the command with the -i
127: * option all the time. Instead, these can be placed into a config file
128: * called idl.config. This file must be in the CLASSPATH. The format of
129: * the includes line is:
130: *
131: * <pre>
132: * includes=<path1>;<path2>;...;<pathN>
133: * </pre>
134: *
135: * Note that the path separator character, here shown as a semicolon,
136: * is machine dependent. For instance, on Windows 95 this character
137: * is a semicolon, on UNIX it is a colon.
138: **/
139: public class Compile extends com.sun.tools.corba.se.idl.Compile {
140: /**
141: *
142: **/
143: public static void main(String[] args) {
144: compiler = new Compile();
145: compiler.start(args);
146: } // main
147:
148: /**
149: *
150: **/
151: public void start(String[] args) {
152: try {
153: // <f46082.51> Use generator-specific messages file.
154: //Util.registerMessageFile ("com/sun/corba/se/idl/toJavaPortable/toJava.prp");
155: Util
156: .registerMessageFile("com/sun/tools/corba/se/idl/toJavaPortable/toJavaPortable.prp");
157: init(args);
158: if (arguments.versionRequest)
159: displayVersion();
160: else {
161: preParse();
162: Enumeration e = parse();
163: if (e != null) {
164: preEmit(e);
165: generate();
166: // <f46082.03> Move ODE delta-build support to toJava
167: //if (((Arguments)arguments).genMakefileLists)
168: // generateMakefileLists ();
169: }
170: }
171: } catch (InvalidArgument e) {
172: System.err.println(e);
173: } catch (IOException e) {
174: System.err.println(e);
175: }
176: } // start
177:
178: /**
179: *
180: **/
181: protected Compile() {
182: factory = factories().symtabFactory();
183: } // ctor
184:
185: // <d58591> _factories was made public for QuickTest to operate correctly,
186: // but the code needs to be changed to this:
187: //private Factories _factories = null;
188: //protected com.sun.tools.corba.se.idl.Factories factories ()
189: //{
190: // if (_factories == null)
191: // _factories = new Factories ();
192: // return _factories;
193: //} // factories
194:
195: public Factories _factories = new Factories(); // 58974 - changed for quicktest
196:
197: protected com.sun.tools.corba.se.idl.Factories factories() {
198: return _factories;
199: } // factories
200:
201: ModuleEntry org;
202: ModuleEntry omg;
203: ModuleEntry corba;
204: InterfaceEntry object;
205:
206: /**
207: *
208: **/
209: protected void preParse() {
210: Util.setSymbolTable(symbolTable);
211: Util
212: .setPackageTranslation(((Arguments) arguments).packageTranslation);
213:
214: // Need modules for the predefined objects
215: org = factory.moduleEntry();
216: // <d61919> Suppress generation of this module. If the parser reopens it
217: // while parsing the main IDL source, any definitions appearing in the module
218: // -- and not appearing in a global-scope include file -- will be added to
219: // the emit list with emit=true for eventual generation.
220: org.emit(false);
221: org.name("org");
222: org.container(null);
223: omg = factory.moduleEntry();
224: omg.emit(false); // <d61919>
225: omg.name("omg");
226: omg.module("org");
227: omg.container(org);
228: org.addContained(omg);
229: corba = factory.moduleEntry();
230: corba.emit(false); // <d61919>
231: corba.name("CORBA");
232: corba.module("org/omg");
233: corba.container(omg);
234: omg.addContained(corba);
235: symbolTable.put("org", org);
236: symbolTable.put("org/omg", omg);
237: symbolTable.put("org/omg/CORBA", corba);
238:
239: // Add CORBA::Object to symbol table.
240: object = (InterfaceEntry) symbolTable.get("Object");
241: object.module("org/omg/CORBA");
242: object.container(corba);
243: symbolTable.put("org/omg/CORBA/Object", object);
244:
245: // <d61961> Add PIDL type (primitive) CORBA::TypeCode to symbol table.
246: PrimitiveEntry pEntry = factory.primitiveEntry();
247: pEntry.name("TypeCode");
248: pEntry.module("org/omg/CORBA");
249: pEntry.container(corba);
250: symbolTable.put("org/omg/CORBA/TypeCode", pEntry);
251: symbolTable.put("CORBA/TypeCode", pEntry); // <d55699>
252: overrideNames.put("CORBA/TypeCode", "org/omg/CORBA/TypeCode"); // <d55699>
253: overrideNames.put("org/omg/CORBA/TypeCode", "CORBA/TypeCode"); // <d55699>
254: // <d49526> Allow user to specify types named "TypeCode"
255: //symbolTable.put ("TypeCode", pEntry);
256: //overrideNames.put ("TypeCode", "org/omg/CORBA/TypeCode");
257:
258: // CORBA::Principal is deprecated!
259: // <d61961> Add PIDL type (primitive) CORBA::Principal to symbol table.
260: pEntry = factory.primitiveEntry();
261: pEntry.name("Principal");
262: pEntry.module("org/omg/CORBA");
263: pEntry.container(corba);
264: symbolTable.put("org/omg/CORBA/Principle", pEntry);
265: symbolTable.put("CORBA/Principal", pEntry);
266: overrideNames.put("CORBA/Principal", "org/omg/CORBA/Principal");
267: overrideNames.put("org/omg/CORBA/Principal", "CORBA/Principal");
268:
269: // <d61961> Add PIDL type (interface) CORBA::Current to symbol table.
270: //InterfaceEntry iEntry = factory.interfaceEntry ();
271: //iEntry.name ("Current");
272: //iEntry.module ("org/omg/CORBA");
273: //iEntry.container (corba);
274: //symbolTable.put ("org/omg/CORBA/Current", iEntry);
275: //symbolTable.put ("CORBA/Current", iEntry);
276: //overrideNames.put ("CORBA/Current", "org/omg/CORBA/Current");
277: //overrideNames.put ("org/omg/CORBA/Current", "CORBA/Current");
278:
279: overrideNames.put("TRUE", "true");
280: overrideNames.put("FALSE", "false");
281: //overrideNames.put ("any", "org/omg/CORBA/Any");
282:
283: // Add CORBA module to symbol table
284: symbolTable.put("CORBA", corba); // 55699
285: overrideNames.put("CORBA", "org/omg/CORBA"); // <d55699>
286: overrideNames.put("org/omg/CORBA", "CORBA"); // <d55699>
287: } // preParse
288:
289: protected void preEmit(Enumeration emitList) {
290: typedefInfo = SymtabEntry.getVariableKey();
291: Hashtable tempST = (Hashtable) symbolTable.clone();
292:
293: for (Enumeration e = tempST.elements(); e.hasMoreElements();) {
294: SymtabEntry element = (SymtabEntry) e.nextElement();
295:
296: // Any other symbolTable processing?
297: preEmitSTElement(element);
298: }
299:
300: // Do this processing AFTER any other processing to get the
301: // correct names.
302: Enumeration elements = symbolTable.elements();
303: while (elements.hasMoreElements()) {
304: // Find all TypedefEntry's and fill in the SymtabEntry.info
305: // field with it's real type , including [][]... with const
306: // exprs.
307: SymtabEntry element = (SymtabEntry) elements.nextElement();
308: if (element instanceof TypedefEntry
309: || element instanceof SequenceEntry)
310: Util.fillInfo(element);
311:
312: // <d59437> Members of constructed types may now be value boxes, and value
313: // boxes may contain types that are directly defined rather than typedef-ed
314: // (e.g., "valuetype vb sequence <long, 5>;"). If member resolves to a value
315: // box, then check and fillInfo() for value box and its content type BEFORE
316: // doing fillInfo() on member; otherwise, could get an exception. There's
317: // code in fillInfo() that performs this check, so it does not appear here.
318:
319: else if (element instanceof StructEntry) {
320: Enumeration members = ((StructEntry) element).members()
321: .elements();
322: while (members.hasMoreElements())
323: Util.fillInfo((SymtabEntry) members.nextElement());
324: } else if (element instanceof InterfaceEntry
325: && ((InterfaceEntry) element).state() != null) {
326: Enumeration members = ((InterfaceEntry) element)
327: .state().elements();
328: while (members.hasMoreElements())
329: Util.fillInfo(((InterfaceState) members
330: .nextElement()).entry);
331: } else if (element instanceof UnionEntry) {
332: Enumeration branches = ((UnionEntry) element)
333: .branches().elements();
334: while (branches.hasMoreElements())
335: Util
336: .fillInfo(((UnionBranch) branches
337: .nextElement()).typedef);
338: }
339:
340: // For each type that is at the top level that is NOT a module
341: // or IncludeEntry, add it to the imports list. If there are
342: // types within modules which refer to these, their types must
343: // be explicitly stated in an import statement.
344: if (element.module().equals("")
345: && !(element instanceof ModuleEntry
346: || element instanceof IncludeEntry || element instanceof PrimitiveEntry))
347: importTypes.addElement(element);
348: }
349:
350: while (emitList.hasMoreElements()) {
351: SymtabEntry entry = (SymtabEntry) emitList.nextElement();
352:
353: // Any other emitList processing:
354: preEmitELElement(entry);
355: }
356: } // preEmit
357:
358: /**
359: * This method is called by preEmit once for each symbol table entry.
360: * It can be called by extenders.
361: **/
362: protected void preEmitSTElement(SymtabEntry entry) {
363: // If the -package argument was used, search the packages list
364: // for the given type name and prepend the package to it.
365: Hashtable packages = ((Arguments) arguments).packages;
366: if (packages.size() > 0) {
367: String substr = (String) packages.get(entry.fullName());
368: if (substr != null) {
369: String pkg = null;
370: ModuleEntry mod = null;
371: ModuleEntry prev = null;
372: while (substr != null) {
373: int dot = substr.indexOf('.');
374: if (dot < 0) {
375: pkg = substr;
376: substr = null;
377: } else {
378: pkg = substr.substring(0, dot);
379: substr = substr.substring(dot + 1);
380: }
381:
382: String fullName = prev == null ? pkg : prev
383: .fullName()
384: + '/' + pkg;
385: mod = (ModuleEntry) symbolTable.get(fullName);
386: if (mod == null) {
387: mod = factory.moduleEntry();
388: mod.name(pkg);
389: mod.container(prev);
390: if (prev != null)
391: mod.module(prev.fullName());
392: symbolTable.put(pkg, mod);
393: }
394: prev = mod;
395: }
396: entry.module(mod.fullName());
397: entry.container(mod);
398: }
399: }
400: } // preEmitSTElement
401:
402: /**
403: * This method is called by preEmit once for each emitList entry.
404: * It can be called by extenders.
405: **/
406: protected void preEmitELElement(SymtabEntry entry) {
407: } // preEmitELElement
408:
409: public Vector importTypes = new Vector();
410: public SymtabFactory factory;
411: public static int typedefInfo;
412: public Hashtable list = new Hashtable();
413: public static Compile compiler = null; // <d58591>
414: } // class Compile
|