Refactory: smarty.ui plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / ASTNode.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.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.ASTVisitor;
14 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
15 import net.sourceforge.phpdt.internal.compiler.lookup.ArrayBinding;
16 import net.sourceforge.phpdt.internal.compiler.lookup.BaseTypes;
17 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
18 import net.sourceforge.phpdt.internal.compiler.lookup.CompilerModifiers;
19 import net.sourceforge.phpdt.internal.compiler.lookup.FieldBinding;
20 import net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding;
21 import net.sourceforge.phpdt.internal.compiler.lookup.ReferenceBinding;
22 import net.sourceforge.phpdt.internal.compiler.lookup.Scope;
23 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
24 import net.sourceforge.phpdt.internal.compiler.lookup.TypeConstants;
25 import net.sourceforge.phpdt.internal.compiler.lookup.TypeIds;
26
27 public abstract class ASTNode implements BaseTypes, CompilerModifiers,
28                 TypeConstants, TypeIds {
29
30         public int sourceStart, sourceEnd;
31
32         // some global provision for the hierarchy
33         public final static Constant NotAConstant = Constant.NotAConstant;
34
35         // storage for internal flags (32 bits) BIT USAGE
36         public final static int Bit1 = 0x1; // return type (operators) | name
37                                                                                 // reference kind (name ref) | add
38                                                                                 // assertion (type decl)
39
40         public final static int Bit2 = 0x2; // return type (operators) | name
41                                                                                 // reference kind (name ref) | has local
42                                                                                 // type (type, method, field decl)
43
44         public final static int Bit3 = 0x4; // return type (operators) | name
45                                                                                 // reference kind (name ref) | implicit
46                                                                                 // this (this ref)
47
48         public final static int Bit4 = 0x8; // return type (operators) | first
49                                                                                 // assignment to local (local decl)
50
51         public final static int Bit5 = 0x10; // value for return (binary
52                                                                                         // expression) |
53
54         public final static int Bit6 = 0x20; // depth (name ref, msg) | only
55                                                                                         // value required (binary
56                                                                                         // expression)
57
58         public final static int Bit7 = 0x40; // depth (name ref, msg) | operator
59                                                                                         // (operators)
60
61         public final static int Bit8 = 0x80; // depth (name ref, msg) | operator
62                                                                                         // (operators)
63
64         public final static int Bit9 = 0x100; // depth (name ref, msg) | operator
65                                                                                         // (operators)
66
67         public final static int Bit10 = 0x200; // depth (name ref, msg) | operator
68                                                                                         // (operators)
69
70         public final static int Bit11 = 0x400; // depth (name ref, msg) | operator
71                                                                                         // (operators)
72
73         public final static int Bit12 = 0x800; // depth (name ref, msg) | operator
74                                                                                         // (operators)
75
76         public final static int Bit13 = 0x1000; // depth (name ref, msg)
77
78         public final static int Bit14 = 0x2000; // assigned (reference lhs)
79
80         public final static int Bit15 = 0x4000;
81
82         public final static int Bit16 = 0x8000;
83
84         public final static int Bit17 = 0x10000;
85
86         public final static int Bit18 = 0x20000;
87
88         public final static int Bit19 = 0x40000;
89
90         public final static int Bit20 = 0x80000;
91
92         public final static int Bit21 = 0x100000;
93
94         public final static int Bit22 = 0x200000; // parenthesis count
95                                                                                                 // (expression)
96
97         public final static int Bit23 = 0x400000; // parenthesis count
98                                                                                                 // (expression)
99
100         public final static int Bit24 = 0x800000; // parenthesis count
101                                                                                                 // (expression)
102
103         public final static int Bit25 = 0x1000000; // parenthesis count
104                                                                                                 // (expression)
105
106         public final static int Bit26 = 0x2000000; // parenthesis count
107                                                                                                 // (expression)
108
109         public final static int Bit27 = 0x4000000; // parenthesis count
110                                                                                                 // (expression)
111
112         public final static int Bit28 = 0x8000000; // parenthesis count
113                                                                                                 // (expression)
114
115         public final static int Bit29 = 0x10000000; // parenthesis count
116                                                                                                 // (expression)
117
118         public final static int Bit30 = 0x20000000; // assignment with no effect
119                                                                                                 // (assignment)
120
121         public final static int Bit31 = 0x40000000; // local declaration reachable
122                                                                                                 // (local decl)
123
124         public final static int Bit32 = 0x80000000; // reachable (statement)
125
126         public int bits = IsReachableMASK; // reachable by default
127
128         // for operators
129         public static final int ReturnTypeIDMASK = Bit1 | Bit2 | Bit3 | Bit4;
130
131         public static final int OperatorSHIFT = 6; // Bit7 -> Bit12
132
133         public static final int OperatorMASK = Bit7 | Bit8 | Bit9 | Bit10 | Bit11
134                         | Bit12; // 6 bits for operator ID
135
136         // for binary expressions
137         public static final int ValueForReturnMASK = Bit5;
138
139         public static final int OnlyValueRequiredMASK = Bit6;
140
141         // for name references
142         public static final int RestrictiveFlagMASK = Bit1 | Bit2 | Bit3;
143
144         public static final int FirstAssignmentToLocalMASK = Bit4;
145
146         // for this reference
147         public static final int IsImplicitThisMask = Bit3;
148
149         // for single name references
150         public static final int DepthSHIFT = 5; // Bit6 -> Bit13
151
152         public static final int DepthMASK = Bit6 | Bit7 | Bit8 | Bit9 | Bit10
153                         | Bit11 | Bit12 | Bit13; // 8 bits for actual depth value (max.
154                                                                                 // 255)
155
156         // for statements
157         public static final int IsReachableMASK = Bit32;
158
159         public static final int IsLocalDeclarationReachableMASK = Bit31;
160
161         // for type declaration
162         public static final int AddAssertionMASK = Bit1;
163
164         public static final int IsMemberTypeMASK = Bit11; // local member do not
165                                                                                                                 // know it is local at
166                                                                                                                 // parse time (need to
167                                                                                                                 // look at binding)
168
169         // for type, method and field declarations
170         public static final int HasLocalTypeMASK = Bit2; // cannot conflict with
171                                                                                                                 // AddAssertionMASK
172
173         // for expression
174         public static final int ParenthesizedSHIFT = 21; // Bit22 -> Bit29
175
176         public static final int ParenthesizedMASK = Bit22 | Bit23 | Bit24 | Bit25
177                         | Bit26 | Bit27 | Bit28 | Bit29; // 8 bits for parenthesis count
178                                                                                                 // value (max. 255)
179
180         // for assignment
181         public static final int IsAssignmentWithNoEffectMASK = Bit30;
182
183         // for references on lhs of assignment (set only for true assignments, as
184         // opposed to compound ones)
185         public static final int IsStrictlyAssignedMASK = Bit14;
186
187         public static final int IsCompoundAssignedMASK = Bit17; // set only for
188                                                                                                                         // compound
189                                                                                                                         // assignments, as
190                                                                                                                         // opposed to other
191                                                                                                                         // ones
192
193         // for empty statement
194         public static final int IsUsefulEmptyStatementMASK = Bit1;
195
196         // for block and method declaration
197         public static final int UndocumentedEmptyBlockMASK = Bit4;
198
199         // for compilation unit
200         public static final int HasAllMethodBodies = Bit5;
201
202         // for references in Javadoc comments
203         public static final int InsideJavadoc = Bit16;
204
205         // for if statement
206         public static final int IsElseIfStatement = Bit30;
207
208         public ASTNode() {
209
210                 super();
211         }
212
213         public boolean cannotReturn() {
214                 return false;
215         }
216
217         public ASTNode concreteStatement() {
218                 return this;
219         }
220
221         /*
222          * Answer true if the field use is considered deprecated. An access in the
223          * same compilation unit is allowed.
224          */
225         public final boolean isFieldUseDeprecated(FieldBinding field, Scope scope) {
226
227                 if (field.isPrivate() && !scope.isDefinedInField(field)) {
228                         // ignore cases where field is used from within inside itself
229                         field.modifiers |= AccPrivateUsed;
230                 }
231
232                 if (!field.isViewedAsDeprecated())
233                         return false;
234
235                 // inside same unit - no report
236                 if (scope.isDefinedInSameUnit(field.declaringClass))
237                         return false;
238
239                 // if context is deprecated, may avoid reporting
240                 // if
241                 // (!scope.environment().options.reportDeprecationInsideDeprecatedCode
242                 // && scope.isInsideDeprecatedCode()) return false;
243                 return true;
244         }
245
246         public boolean isImplicitThis() {
247
248                 return false;
249         }
250
251         /*
252          * Answer true if the method use is considered deprecated. An access in the
253          * same compilation unit is allowed.
254          */
255         public final boolean isMethodUseDeprecated(MethodBinding method, Scope scope) {
256
257                 if (method.isPrivate() && !scope.isDefinedInMethod(method)) {
258                         // ignore cases where method is used from within inside itself (e.g.
259                         // direct recursions)
260                         method.modifiers |= AccPrivateUsed;
261                 }
262
263                 if (!method.isViewedAsDeprecated())
264                         return false;
265
266                 // inside same unit - no report
267                 if (scope.isDefinedInSameUnit(method.declaringClass))
268                         return false;
269
270                 // if context is deprecated, may avoid reporting
271                 // if
272                 // (!scope.environment().options.reportDeprecationInsideDeprecatedCode
273                 // && scope.isInsideDeprecatedCode()) return false;
274                 return true;
275         }
276
277         public boolean isSuper() {
278
279                 return false;
280         }
281
282         public boolean isThis() {
283
284                 return false;
285         }
286
287         /*
288          * Answer true if the type use is considered deprecated. An access in the
289          * same compilation unit is allowed.
290          */
291         public final boolean isTypeUseDeprecated(TypeBinding type, Scope scope) {
292
293                 if (type.isArrayType())
294                         type = ((ArrayBinding) type).leafComponentType;
295                 if (type.isBaseType())
296                         return false;
297
298                 ReferenceBinding refType = (ReferenceBinding) type;
299
300                 if (refType.isPrivate() && !scope.isDefinedInType(refType)) {
301                         // ignore cases where type is used from within inside itself
302                         refType.modifiers |= AccPrivateUsed;
303                 }
304
305                 if (!refType.isViewedAsDeprecated())
306                         return false;
307
308                 // inside same unit - no report
309                 if (scope.isDefinedInSameUnit(refType))
310                         return false;
311
312                 // if context is deprecated, may avoid reporting
313                 // if
314                 // (!scope.environment().options.reportDeprecationInsideDeprecatedCode
315                 // && scope.isInsideDeprecatedCode()) return false;
316                 return true;
317         }
318
319         public static String modifiersString(int modifiers) {
320
321                 String s = ""; //$NON-NLS-1$
322                 if ((modifiers & AccPublic) != 0)
323                         s = s + "public "; //$NON-NLS-1$
324                 if ((modifiers & AccPrivate) != 0)
325                         s = s + "private "; //$NON-NLS-1$
326                 if ((modifiers & AccProtected) != 0)
327                         s = s + "protected "; //$NON-NLS-1$
328                 if ((modifiers & AccStatic) != 0)
329                         s = s + "static "; //$NON-NLS-1$
330                 if ((modifiers & AccFinal) != 0)
331                         s = s + "final "; //$NON-NLS-1$
332                         // if ((modifiers & AccSynchronized) != 0)
333                         // s = s + "synchronized "; //$NON-NLS-1$
334                         // if ((modifiers & AccVolatile) != 0)
335                         // s = s + "volatile "; //$NON-NLS-1$
336                         // if ((modifiers & AccTransient) != 0)
337                         // s = s + "transient "; //$NON-NLS-1$
338                         // if ((modifiers & AccNative) != 0)
339                         // s = s + "native "; //$NON-NLS-1$
340                 if ((modifiers & AccAbstract) != 0)
341                         s = s + "abstract "; //$NON-NLS-1$
342                 return s;
343         }
344
345         public abstract StringBuffer print(int indent, StringBuffer output);
346
347         public static StringBuffer printIndent(int indent, StringBuffer output) {
348
349                 for (int i = indent; i > 0; i--)
350                         output.append("  "); //$NON-NLS-1$
351                 return output;
352         }
353
354         public static StringBuffer printModifiers(int modifiers, StringBuffer output) {
355
356                 if ((modifiers & AccPublic) != 0)
357                         output.append("public "); //$NON-NLS-1$
358                 if ((modifiers & AccPrivate) != 0)
359                         output.append("private "); //$NON-NLS-1$
360                 if ((modifiers & AccProtected) != 0)
361                         output.append("protected "); //$NON-NLS-1$
362                 if ((modifiers & AccStatic) != 0)
363                         output.append("static "); //$NON-NLS-1$
364                 if ((modifiers & AccFinal) != 0)
365                         output.append("final "); //$NON-NLS-1$
366                         // if ((modifiers & AccSynchronized) != 0)
367                         // output.append("synchronized "); //$NON-NLS-1$
368                         // if ((modifiers & AccVolatile) != 0)
369                         // output.append("volatile "); //$NON-NLS-1$
370                         // if ((modifiers & AccTransient) != 0)
371                         // output.append("transient "); //$NON-NLS-1$
372                         // if ((modifiers & AccNative) != 0)
373                         // output.append("native "); //$NON-NLS-1$
374                 if ((modifiers & AccAbstract) != 0)
375                         output.append("abstract "); //$NON-NLS-1$
376                 return output;
377         }
378
379         /**
380          * @deprecated - use field instead
381          */
382         public int sourceEnd() {
383                 return sourceEnd;
384         }
385
386         /**
387          * @deprecated - use field instead
388          */
389         public int sourceStart() {
390                 return sourceStart;
391         }
392
393         public static String tabString(int tab) {
394
395                 String s = ""; //$NON-NLS-1$
396                 for (int i = tab; i > 0; i--)
397                         s = s + "  "; //$NON-NLS-1$
398                 return s;
399         }
400
401         public String toString() {
402
403                 return toString(0);
404         }
405
406         public String toString(int tab) {
407
408                 return "****" + super.toString() + "****"; //$NON-NLS-2$ //$NON-NLS-1$
409         }
410
411         public void traverse(ASTVisitor visitor, BlockScope scope) {
412         }
413 }