KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > idl > Member


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-2004 Gerald Brose.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package org.jacorb.idl;
22
23 /**
24  * @author Gerald Brose
25  * @version $Id: Member.java,v 1.31 2004/05/06 12:39:58 nicolas Exp $
26  *
27  */

28
29 import java.io.PrintWriter JavaDoc;
30 import java.util.*;
31
32 public class Member
33     extends Declaration
34 {
35     public TypeSpec type_spec;
36
37     /**
38         this list initially set by the parser but later flattened so that
39         each member only has a single declarator. The list will be null after
40         calling parse()!
41      */

42     SymbolList declarators;
43
44     public Vector extendVector;
45     public TypeDeclaration containingType;
46
47     public Declarator declarator;
48
49     public Member( int num )
50     {
51         super( num );
52     }
53
54     public void setPackage( String JavaDoc s )
55     {
56         s = parser.pack_replace( s );
57         if( pack_name.length() > 0 )
58             pack_name = s + "." + pack_name;
59         else
60             pack_name = s;
61
62         type_spec.setPackage( s );
63         if ( declarators != null )
64             declarators.setPackage( s );
65     }
66
67     public void setEnclosingSymbol( IdlSymbol s )
68     {
69         enclosing_symbol = s;
70         type_spec.setEnclosingSymbol( s );
71         for( Enumeration e = declarators.v.elements(); e.hasMoreElements(); )
72             ( (Declarator)e.nextElement() ).setEnclosingSymbol( s );
73     }
74
75
76     public void setContainingType( TypeDeclaration t )
77     {
78         containingType = t;
79     }
80
81     /**
82      * must be set by MemberList before parsing
83      */

84     public void setExtendVector( Vector v )
85     {
86         extendVector = v;
87     }
88
89     /**
90      * Creates a new Member that is similar to this one,
91      * but only for declarator d.
92      */

93     public Member extractMember( Declarator d )
94     {
95         Member result = new Member( new_num() );
96         result.declarator = d;
97         return result;
98     }
99
100     /**
101      * Parsing members means creating new members for definitions
102      * with more than one declarator.
103      */

104     public void parse()
105     {
106         boolean clone_and_parse = true;
107
108         if( extendVector == null )
109             throw new ParseException("Internal Compiler Error: extendVector not set!", this.myPosition );
110
111         if( type_spec.typeSpec() instanceof ScopedName )
112         {
113             token = type_spec.typeSpec().get_token();
114             String JavaDoc name = type_spec.typeSpec().toString();
115
116             type_spec = ( (ScopedName)type_spec.typeSpec() ).resolvedTypeSpec();
117             enclosing_symbol.addImportedName( name, type_spec );
118
119             if( type_spec instanceof AliasTypeSpec )
120             {
121                 AliasTypeSpec aliasTS = (AliasTypeSpec)type_spec;
122                 TypeSpec originalType = aliasTS.originalType ();
123                 if( originalType instanceof SequenceType )
124                 {
125                     SequenceType sequenceTS = (SequenceType)originalType;
126                     if( sequenceTS.elementTypeSpec().typeName().equals( containingType.typeName()) )
127                         sequenceTS.setRecursive ();
128                 }
129             }
130
131             clone_and_parse = false;
132             if( type_spec instanceof ConstrTypeSpec )
133             {
134                 if( ( (ConstrTypeSpec)type_spec.typeSpec() ).c_type_spec instanceof StructType )
135                 {
136                     if(
137                        ( (ConstrTypeSpec)type_spec.typeSpec() ).c_type_spec.typeName().equals( containingType.typeName() )
138                        )
139                     {
140                         parser.fatal_error( "Illegal type recursion (use sequence<" +
141                                             containingType.typeName() + "> instead)", token );
142                     }
143                 }
144             }
145         }
146         else if( type_spec.typeSpec() instanceof SequenceType )
147         {
148             TypeSpec ts = ( (SequenceType)type_spec.typeSpec() ).elementTypeSpec().typeSpec();
149             SequenceType seqTs = (SequenceType)type_spec.typeSpec();
150             while( ts instanceof SequenceType )
151             {
152                 seqTs = (SequenceType)ts;
153                 ts = ( (SequenceType)ts.typeSpec() ).elementTypeSpec().typeSpec();
154             }
155
156             if( ScopedName.isRecursionScope( ts.typeName() ) )
157             {
158                 seqTs.setRecursive();
159             }
160         }
161         else if( type_spec instanceof ConstrTypeSpec )
162         {
163             type_spec.parse();
164         }
165
166         String JavaDoc tokName = null;
167         if( token != null && token.line_val != null )
168         {
169             tokName = token.line_val.trim();
170             if( tokName.length() == 0 )
171             {
172                 tokName = null;
173             }
174         }
175
176         for( Enumeration e = declarators.v.elements(); e.hasMoreElements(); )
177         {
178             Declarator d = (Declarator)e.nextElement();
179             String JavaDoc dName = d.name();
180
181             if( tokName != null )
182             {
183
184                 if (org.jacorb.idl.parser.strict_names)
185                 {
186                     // check for name clashes strictly (i.e. case insensitive)
187
if( dName.equalsIgnoreCase( tokName ) )
188                     {
189                         parser.fatal_error( "Declarator " + dName +
190                                             " already defined in scope.", token );
191                     }
192                 }
193                 else
194                 {
195                     // check for name clashes only loosely (i.e. case sensitive)
196
if( dName.equals( tokName ) )
197                     {
198                         parser.fatal_error( "Declarator " + dName +
199                                             " already defined in scope.", token );
200                     }
201                 }
202             }
203
204             // we don't parse the declarator itself
205
// as that would result in its name getting defined
206
// we define the declarator's name as a type name indirectly
207
// through the cloned type specs.
208

209             Member m = extractMember( d );
210
211             TypeSpec ts = type_spec.typeSpec();
212
213             /* create a separate type spec copy for every declarator
214                if the type spec is a new type definition, i.e. a
215                struct, enum, union, sequence or the declarator is
216                an array declarator
217             */

218
219             if( clone_and_parse || d.d instanceof ArrayDeclarator )
220             {
221                 /* arrays need special treatment */
222
223                 if( d.d instanceof ArrayDeclarator )
224                 {
225                     ts = new ArrayTypeSpec( new_num(), ts, (ArrayDeclarator)d.d, pack_name );
226                     ts.parse();
227                 }
228                 else if( !( ts instanceof BaseType ) )
229                 {
230                     ts = (TypeSpec)ts.clone();
231                     if( !( ts instanceof ConstrTypeSpec ) )
232                         ts.set_name( d.name() );
233
234                     /* important: only parse type specs once (we do it for the last
235                        declarator only) */

236                     if( !e.hasMoreElements() )
237                         ts.parse();
238                 }
239             }
240
241             // else
242
if( !( d.d instanceof ArrayDeclarator ) )
243             {
244                 try
245                 {
246                     NameTable.define( containingType + "." + d.name(),
247                             "declarator" );
248                 }
249                 catch( NameAlreadyDefined nad )
250                 {
251                     parser.fatal_error( "Declarator " + d.name() +
252                             " already defined in scope.", token );
253                 }
254             }
255
256             /* if the type spec is a scoped name, it is already parsed and
257              * the type name is defined
258              */

259             m.type_spec = ts;
260             m.pack_name = this.pack_name;
261             m.name = this.name;
262             extendVector.addElement( m );
263         }
264         declarators = null;
265     }
266
267     /**
268      *
269      */

270
271     public void print( PrintWriter JavaDoc ps )
272     {
273         member_print( ps, "\tpublic " );
274     }
275
276     /**
277      *
278      */

279
280     public void member_print( PrintWriter JavaDoc ps, String JavaDoc prefix )
281     {
282         /* only print members that are not interfaces */
283
284         if( ( type_spec.typeSpec() instanceof ConstrTypeSpec &&
285                 !( ( ( (ConstrTypeSpec)type_spec.typeSpec() ).c_type_spec.declaration()
286                 instanceof Interface ) ||
287                 ( ( (ConstrTypeSpec)type_spec.typeSpec() ).c_type_spec.declaration()
288                 instanceof Value ) ) ) ||
289                 type_spec.typeSpec() instanceof SequenceType ||
290                 type_spec.typeSpec() instanceof ArrayTypeSpec )
291         {
292             type_spec.print( ps );
293         }
294
295
296         if( type_spec.typeSpec() instanceof StringType )
297             ps.print( prefix + type_spec.toString() + " " + declarator.toString() + " = \"\";" );
298         else
299             ps.print( prefix + type_spec.toString() + " " + declarator.toString() + ";" );
300     }
301
302     public TypeSpec typeSpec()
303     {
304         return type_spec.typeSpec();
305     }
306 }
307
Popular Tags