View Javadoc

1   /*
2    * Copyright (c) 2001-2005,
3    * RedVerst Group, ISP RAS http://www.ispras.ru
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are met:
8    *
9    * 1. Redistributions of source code must retain the above copyright notice, this
10   *    list of conditions and the following disclaimer.
11   *
12   * 2. Redistributions in binary form must reproduce the above copyright notice,
13   *    this list of conditions and the following disclaimer in the documentation
14   *    and/or other materials provided with the distribution.
15   *
16   * 3. The names "ATP", "TreeDL", "RedVerst", "ISP RAS"
17   *    may not be used to endorse or promote products derived from this software
18   *    without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  
32  package com.unitesk.atp.treedl;
33  
34  import java.util.Iterator;
35  
36  import com.unitesk.atp.treedl.TDL.*;
37  import com.unitesk.atp.tool.PluginClass;
38  
39  /***
40   * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
41   * @version $Id: CSharpCopyVisitorGenerator.java,v 1.8 2005/11/20 21:26:55 all-x Exp $
42   */
43  public class CSharpCopyVisitorGenerator
44      extends CSharpTextGenerator
45      implements TDL_Visitor
46  {
47      /***
48       * {@inheritDoc}
49       */
50      public String getVersion()
51      {
52          return PluginClass.getRevision( "$Revision: 1.8 $" ) + "-alpha";
53      }
54  
55      public void visitModule( Module node )
56      {
57          setFunction( "Up", new NameFunction() );
58  
59          generateHeader();
60  
61          int indexOfLastDot = getOutClassName().lastIndexOf( '.' );
62          if( indexOfLastDot != -1 )
63          {
64              txt( "namespace " + getOutClassName().substring( 0, indexOfLastDot ) ); nl();
65              block_nl();
66          }
67  
68          String outShortClassName = getOutClassName().substring( indexOfLastDot + 1 );
69  
70          txt( "public class " + outShortClassName + " : ${name}.${visitorName}" ); nl();
71          block_nl();
72  
73          txt( "private ${.Node} res;" ); nl();
74          nl();
75          txt( "public ${.Node} Copy( ${.Node} node )" ); nl();
76          block_nl();
77          txt( "if( node != null ) node.Accept( this ); else res = null;" ); nl();
78          txt( "return res;" ); nl();
79          unblock_nl();
80          nl();
81          txt( "public virtual System.Collections.IList CopyList( System.Collections.IList list )" ); nl();
82          block_nl();
83           txt("System.Collections.IList res;"); nl();
84           txt( "if( list is System.ICloneable )" ); nl();
85           block_nl();
86            txt("res = (System.Collections.IList)((System.ICloneable)list).Clone();" ); nl();
87            nl();
88            txt( "for( int i = 0; i < list.Count; i++ )" ); nl();
89            block_nl();
90             txt( "res[i] = Copy( (${.Node})list[i] );" ); nl();
91            unblock_nl();
92           unblock(); txt( " else " ); block_nl();
93            txt( "try" ); nl();
94            block_nl();
95             txt( "System.Type type = list.GetType();" ); nl();
96             txt( "res = (System.Collections.IList)type.Assembly.CreateInstance( type.FullName );" ); nl();
97            unblock_nl();
98            txt( "catch" ); nl();
99            block_nl();
100            txt( "res = new System.Collections.ArrayList();" ); nl();
101           unblock_nl();
102           txt( "for( int i = 0; i < list.Count; i++ )" ); nl();
103           block_nl();
104            txt( "res.Add( Copy( (${.Node})list[i] ) );" ); nl();
105           unblock_nl();
106          unblock_nl();
107          txt( "return res;" ); nl();
108         unblock_nl();
109         nl();
110 
111         TDL_ModuleMap moduleMap = node.getModuleMap();
112         Iterator it = moduleMap.getModuleNames().iterator();
113         while( it.hasNext() )
114         {
115             TDL_Module tdlModule = moduleMap.getModule( (QID)it.next() );
116             Module t = tdlModule.getRoot();
117             pushNode( t );
118             list( "i", 0, t.sizeOptMemberList(), "${optMemberList[i]}", null );
119             popNode();
120         }
121         unblock_nl();
122         
123         if( node.getName().sizeIdList() > 1 )
124         {
125             txt( "}" ); nl();
126         }
127     }
128 
129     public void visitNodeTypeDecl( NodeTypeDecl node )
130     {
131         if( node.isAbstract() ) return;
132         nl();
133         txt( "public virtual void Visit${name}( ${parent.name}.${name} node )" ); nl();
134         block_nl();
135         txt( "res =" ); nl();
136         txt( "new ${parent.name}.${name}" );
137         incIndent();
138         int constructorParametersCount = 0;
139         for( int i = 0; i < node.sizeFullFieldList(); i++ )
140         {
141             Field field = node.getFullFieldList( i );
142 
143             if( TDL_Module.isConstructorParameter( field ) )
144             {
145                 pushNode( field );
146 
147                 nl();
148                 txtif( constructorParametersCount == 0, "(", "," );
149                 if( field.isChild() )
150                 {
151                     if( field.getType().checkCardinality( Cardinality.LIST ) )
152                     {
153                         txt( " CopyList( node.${Up:name} )" );
154                     } else {
155                         txt( " (${type})Copy( node.${Up:name} )" );
156                     }
157                 } else {
158                     txt( " node.${Up:name}" );
159                 }
160 
161                 constructorParametersCount++;
162 
163                 popNode();
164             }
165         }
166         switch( constructorParametersCount )
167         {
168         case 0:
169             txt( "(" );
170             break;
171         case 1:
172             txt( " " );
173             break;
174         default:
175             nl();
176             break;
177         }
178         txt( ");" ); nl();
179         popIndent();
180         unblock_nl();
181     }
182 
183     public void visitConstTypeDecl( ConstTypeDecl node )
184     {
185     }
186 
187     public void visitOperationDecl( OperationDecl node )
188     {
189     }
190 
191     public void visitBaseModule( BaseModule node )
192     {
193         throw new UnsupportedOperationException();
194     }
195 
196     public void visitConstructorCodeMember( ConstructorCodeMember node )
197     {
198         throw new UnsupportedOperationException();
199     }
200     
201     public void visitBodyCodeMember( BodyCodeMember node )
202     {
203         throw new UnsupportedOperationException();
204     }
205 
206     public void visitField( Field node ) 
207     {
208         throw new UnsupportedOperationException();
209     }
210 
211     public void visitNonVirtualParameterDecl( NonVirtualParameterDecl node ) 
212     {
213         throw new UnsupportedOperationException();
214     }
215 
216     public void visitVirtualParameterDecl( VirtualParameterDecl node )
217     {
218         throw new UnsupportedOperationException();
219     }
220 
221     public void visitCase( Case node ) 
222     {
223         throw new UnsupportedOperationException();
224     }
225 
226     public void visitCaseSignature( CaseSignature node ) 
227     {
228         throw new UnsupportedOperationException();
229     }
230 
231     public void visitParameter( Parameter node ) 
232     {
233         throw new UnsupportedOperationException();
234     }
235 }