2c4fb608066d2fb8ab00cfba2e3dc7bd21a98519
[phpeclipse.git] /
1 /*******************************************************************************
2  * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v0.5 
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v05.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
12
13 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
14 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
15 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
16 import net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding;
17 import net.sourceforge.phpdt.internal.compiler.lookup.MethodScope;
18 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
19 import net.sourceforge.phpdt.internal.compiler.problem.AbortType;
20
21 public class AnonymousLocalTypeDeclaration extends LocalTypeDeclaration {
22
23         public static final char[] ANONYMOUS_EMPTY_NAME = new char[] {};
24         public QualifiedAllocationExpression allocation;
25
26         public AnonymousLocalTypeDeclaration(CompilationResult compilationResult) {
27                 super(compilationResult);
28                 modifiers = AccDefault;
29                 name = ANONYMOUS_EMPTY_NAME;
30         } 
31         
32         // use a default name in order to th name lookup 
33         // to operate juat like a regular type (which has a name)
34         //without checking systematically if the naem is null .... 
35         public MethodBinding createsInternalConstructorWithBinding(MethodBinding inheritedConstructorBinding) {
36
37                 //Add to method'set, the default constuctor that just recall the
38                 //super constructor with the same arguments
39                 String baseName = "$anonymous"; //$NON-NLS-1$
40                 TypeBinding[] argumentTypes = inheritedConstructorBinding.parameters;
41                 int argumentsLength = argumentTypes.length;
42                 //the constructor
43                 ConstructorDeclaration cd = new ConstructorDeclaration(this.compilationResult);
44                 cd.selector = new char[] { 'x' }; //no maining
45                 cd.sourceStart = sourceStart;
46                 cd.sourceEnd = sourceEnd;
47                 cd.modifiers = modifiers & AccVisibilityMASK;
48                 cd.isDefaultConstructor = true;
49
50                 if (argumentsLength > 0) {
51                         Argument[] arguments = (cd.arguments = new Argument[argumentsLength]);
52                         for (int i = argumentsLength; --i >= 0;) {
53                                 arguments[i] = new Argument((baseName + i).toCharArray(), 0L, null /*type ref*/, AccDefault);
54                         }
55                 }
56
57                 //the super call inside the constructor
58                 cd.constructorCall =
59                         new ExplicitConstructorCall(ExplicitConstructorCall.ImplicitSuper);
60                 cd.constructorCall.sourceStart = sourceStart;
61                 cd.constructorCall.sourceEnd = sourceEnd;
62
63                 if (argumentsLength > 0) {
64                         Expression[] args;
65                         args = cd.constructorCall.arguments = new Expression[argumentsLength];
66                         for (int i = argumentsLength; --i >= 0;) {
67                                 args[i] = new SingleNameReference((baseName + i).toCharArray(), 0L);
68                         }
69                 }
70
71                 //adding the constructor in the methods list
72                 if (methods == null) {
73                         methods = new AbstractMethodDeclaration[] { cd };
74                 } else {
75                         AbstractMethodDeclaration[] newMethods;
76                         System.arraycopy(
77                                 methods,
78                                 0,
79                                 newMethods = new AbstractMethodDeclaration[methods.length + 1],
80                                 1,
81                                 methods.length);
82                         newMethods[0] = cd;
83                         methods = newMethods;
84                 }
85
86                 //============BINDING UPDATE==========================
87                 cd.binding = new MethodBinding(
88                                 cd.modifiers, //methodDeclaration
89                                 argumentsLength == 0 ? NoParameters : argumentTypes, //arguments bindings
90                                 inheritedConstructorBinding.thrownExceptions, //exceptions
91                                 binding); //declaringClass
92                                 
93                 cd.scope = new MethodScope(scope, this, true);
94                 cd.bindArguments();
95                 cd.constructorCall.resolve(cd.scope);
96
97                 if (binding.methods == null) {
98                         binding.methods = new MethodBinding[] { cd.binding };
99                 } else {
100                         MethodBinding[] newMethods;
101                         System.arraycopy(
102                                 binding.methods,
103                                 0,
104                                 newMethods = new MethodBinding[binding.methods.length + 1],
105                                 1,
106                                 binding.methods.length);
107                         newMethods[0] = cd.binding;
108                         binding.methods = newMethods;
109                 }
110                 //===================================================
111
112                 return cd.binding;
113
114         }
115         public void resolve(BlockScope scope) {
116
117                 // scope and binding are provided in updateBindingSuperclass 
118                 resolve();
119                 updateMaxFieldCount();
120         }
121
122         public String toString(int tab) {
123
124                 return toStringBody(tab);
125         }
126
127         /**
128          *      Iteration for a local anonymous innertype
129          *
130          */
131         public void traverse(
132                 IAbstractSyntaxTreeVisitor visitor,
133                 BlockScope blockScope) {
134
135                 if (ignoreFurtherInvestigation)
136                         return;
137                 try {
138                         if (visitor.visit(this, blockScope)) {
139
140                                 int fieldsLength;
141                                 int methodsLength;
142                                 int memberTypesLength;
143
144                                 // <superclass> is bound to the actual type from the allocation expression
145                                 // therefore it has already been iterated at this point.
146
147                                 if (memberTypes != null) {
148                                         memberTypesLength = memberTypes.length;
149                                         for (int i = 0; i < memberTypesLength; i++)
150                                                 memberTypes[i].traverse(visitor, scope);
151                                 }
152                                 if (fields != null) {
153                                         fieldsLength = fields.length;
154                                         for (int i = 0; i < fieldsLength; i++) {
155                                                 FieldDeclaration field;
156                                                 if ((field = fields[i]).isStatic()) {
157                                                         // local type cannot have static fields
158                                                 } else {
159                                                         field.traverse(visitor, initializerScope);
160                                                 }
161                                         }
162                                 }
163                                 if (methods != null) {
164                                         methodsLength = methods.length;
165                                         for (int i = 0; i < methodsLength; i++)
166                                                 methods[i].traverse(visitor, scope);
167                                 }
168                         }
169                         visitor.endVisit(this, blockScope);
170                 } catch (AbortType e) {
171                 }
172         }
173 }