1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.ast;
13 import org.eclipse.jdt.internal.compiler.impl.*;
14 import org.eclipse.jdt.internal.compiler.lookup.*;
15 import org.eclipse.jdt.internal.compiler.ASTVisitor;
17 public abstract class ASTNode implements BaseTypes, CompilerModifiers, TypeConstants, TypeIds {
19 public int sourceStart, sourceEnd;
21 //some global provision for the hierarchy
22 public final static Constant NotAConstant = Constant.NotAConstant;
24 // storage for internal flags (32 bits) BIT USAGE
25 public final static int Bit1 = 0x1; // return type (operator) | name reference kind (name ref) | add assertion (type decl) | useful empty statement (empty statement)
26 public final static int Bit2 = 0x2; // return type (operator) | name reference kind (name ref) | has local type (type, method, field decl)
27 public final static int Bit3 = 0x4; // return type (operator) | name reference kind (name ref) | implicit this (this ref)
28 public final static int Bit4 = 0x8; // return type (operator) | first assignment to local (local decl) | undocumented empty block (block, type and method decl)
29 public final static int Bit5 = 0x10; // value for return (expression) | has all method bodies (unit)
30 public final static int Bit6 = 0x20; // depth (name ref, msg) | only value required (binary expression) | ignore need cast check (cast expression)
31 public final static int Bit7 = 0x40; // depth (name ref, msg) | operator (operator) | need runtime checkcast (cast expression)
32 public final static int Bit8 = 0x80; // depth (name ref, msg) | operator (operator)
33 public final static int Bit9 = 0x100; // depth (name ref, msg) | operator (operator) | is local type (type decl)
34 public final static int Bit10= 0x200; // depth (name ref, msg) | operator (operator) | is anonymous type (type decl)
35 public final static int Bit11 = 0x400; // depth (name ref, msg) | operator (operator) | is member type (type decl)
36 public final static int Bit12 = 0x800; // depth (name ref, msg) | operator (operator)
37 public final static int Bit13 = 0x1000; // depth (name ref, msg)
38 public final static int Bit14 = 0x2000; // strictly assigned (reference lhs)
39 public final static int Bit15 = 0x4000; // is unnecessary cast (expression)
40 public final static int Bit16 = 0x8000; // in javadoc comment (name ref, type ref, msg)
41 public final static int Bit17 = 0x10000; // compound assigned (reference lhs)
42 public final static int Bit18 = 0x20000;
43 public final static int Bit19 = 0x40000;
44 public final static int Bit20 = 0x80000;
45 public final static int Bit21 = 0x100000;
46 public final static int Bit22 = 0x200000; // parenthesis count (expression)
47 public final static int Bit23 = 0x400000; // parenthesis count (expression)
48 public final static int Bit24 = 0x800000; // parenthesis count (expression)
49 public final static int Bit25 = 0x1000000; // parenthesis count (expression)
50 public final static int Bit26 = 0x2000000; // parenthesis count (expression)
51 public final static int Bit27 = 0x4000000; // parenthesis count (expression)
52 public final static int Bit28 = 0x8000000; // parenthesis count (expression)
53 public final static int Bit29 = 0x10000000; // parenthesis count (expression)
54 public final static int Bit30 = 0x20000000; // assignment with no effect (assignment) | elseif (if statement)
55 public final static int Bit31 = 0x40000000; // local declaration reachable (local decl)
56 public final static int Bit32 = 0x80000000; // reachable (statement)
58 public final static long Bit32L = 0x80000000L;
59 public final static long Bit33L = 0x100000000L;
60 public final static long Bit34L = 0x200000000L;
61 public final static long Bit35L = 0x400000000L;
62 public final static long Bit36L = 0x800000000L;
63 public final static long Bit37L = 0x1000000000L;
64 public final static long Bit38L = 0x2000000000L;
65 public final static long Bit39L = 0x4000000000L;
66 public final static long Bit40L = 0x8000000000L;
68 public int bits = IsReachableMASK; // reachable by default
71 public static final int ReturnTypeIDMASK = Bit1|Bit2|Bit3|Bit4;
72 public static final int OperatorSHIFT = 6; // Bit7 -> Bit12
73 public static final int OperatorMASK = Bit7|Bit8|Bit9|Bit10|Bit11|Bit12; // 6 bits for operator ID
75 // for binary expressions
76 public static final int ValueForReturnMASK = Bit5;
77 public static final int OnlyValueRequiredMASK = Bit6;
79 // for cast expressions
80 public static final int UnnecessaryCastMask = Bit15;
81 public static final int NeedRuntimeCheckCastMASK = Bit7;
82 public static final int IgnoreNeedForCastCheckMASK = Bit6;
84 // for name references
85 public static final int RestrictiveFlagMASK = Bit1|Bit2|Bit3;
86 public static final int FirstAssignmentToLocalMASK = Bit4;
89 public static final int IsImplicitThisMask = Bit3;
91 // for single name references
92 public static final int DepthSHIFT = 5; // Bit6 -> Bit13
93 public static final int DepthMASK = Bit6|Bit7|Bit8|Bit9|Bit10|Bit11|Bit12|Bit13; // 8 bits for actual depth value (max. 255)
96 public static final int IsReachableMASK = Bit32;
97 public static final int IsLocalDeclarationReachableMASK = Bit31;
99 // for type declaration
100 public static final int AddAssertionMASK = Bit1;
101 public static final int IsLocalTypeMASK = Bit9;
102 public static final int IsAnonymousTypeMASK = Bit10; // used to test for anonymous
103 public static final int AnonymousAndLocalMask = IsAnonymousTypeMASK | IsLocalTypeMASK; // used to set anonymous marker
104 public static final int IsMemberTypeMASK = Bit11; // local member do not know it is local at parse time (need to look at binding)
106 // for type, method and field declarations
107 public static final int HasLocalTypeMASK = Bit2; // cannot conflict with AddAssertionMASK
110 public static final int ParenthesizedSHIFT = 21; // Bit22 -> Bit29
111 public static final int ParenthesizedMASK = Bit22|Bit23|Bit24|Bit25|Bit26|Bit27|Bit28|Bit29; // 8 bits for parenthesis count value (max. 255)
114 public static final int IsAssignmentWithNoEffectMASK = Bit30;
116 // for references on lhs of assignment
117 public static final int IsStrictlyAssignedMASK = Bit14; // set only for true assignments, as opposed to compound ones
118 public static final int IsCompoundAssignedMASK = Bit17; // set only for compound assignments, as opposed to other ones
120 // for empty statement
121 public static final int IsUsefulEmptyStatementMASK = Bit1;
123 // for block and method declaration
124 public static final int UndocumentedEmptyBlockMASK = Bit4;
126 // for compilation unit
127 public static final int HasAllMethodBodies = Bit5;
129 // for references in Javadoc comments
130 public static final int InsideJavadoc = Bit16;
133 public static final int IsElseIfStatement = Bit30;
140 public ASTNode concreteStatement() {
144 /* Answer true if the field use is considered deprecated.
145 * An access in the same compilation unit is allowed.
147 public final boolean isFieldUseDeprecated(FieldBinding field, Scope scope, boolean isStrictlyAssigned) {
149 if (!isStrictlyAssigned && field.isPrivate() && !scope.isDefinedInField(field)) {
150 // ignore cases where field is used from within inside itself
151 field.modifiers |= AccPrivateUsed;
154 if (!field.isViewedAsDeprecated()) return false;
156 // inside same unit - no report
157 if (scope.isDefinedInSameUnit(field.declaringClass)) return false;
159 // if context is deprecated, may avoid reporting
160 if (!scope.environment().options.reportDeprecationInsideDeprecatedCode && scope.isInsideDeprecatedCode()) return false;
164 public boolean isImplicitThis() {
169 /* Answer true if the method use is considered deprecated.
170 * An access in the same compilation unit is allowed.
172 public final boolean isMethodUseDeprecated(MethodBinding method, Scope scope) {
174 if (method.isPrivate() && !scope.isDefinedInMethod(method)) {
175 // ignore cases where method is used from within inside itself (e.g. direct recursions)
176 method.modifiers |= AccPrivateUsed;
179 if (!method.isViewedAsDeprecated()) return false;
181 // inside same unit - no report
182 if (scope.isDefinedInSameUnit(method.declaringClass)) return false;
184 // if context is deprecated, may avoid reporting
185 if (!scope.environment().options.reportDeprecationInsideDeprecatedCode && scope.isInsideDeprecatedCode()) return false;
189 public boolean isSuper() {
194 public boolean isThis() {
199 /* Answer true if the type use is considered deprecated.
200 * An access in the same compilation unit is allowed.
202 public final boolean isTypeUseDeprecated(TypeBinding type, Scope scope) {
204 if (type.isArrayType())
205 type = ((ArrayBinding) type).leafComponentType;
206 if (type.isBaseType())
209 ReferenceBinding refType = (ReferenceBinding) type;
211 if (refType.isPrivate() && !scope.isDefinedInType(refType)) {
212 // ignore cases where type is used from within inside itself
213 refType.modifiers |= AccPrivateUsed;
216 if (!refType.isViewedAsDeprecated()) return false;
218 // inside same unit - no report
219 if (scope.isDefinedInSameUnit(refType)) return false;
221 // if context is deprecated, may avoid reporting
222 if (!scope.environment().options.reportDeprecationInsideDeprecatedCode && scope.isInsideDeprecatedCode()) return false;
226 public abstract StringBuffer print(int indent, StringBuffer output);
228 public static StringBuffer printIndent(int indent, StringBuffer output) {
230 for (int i = indent; i > 0; i--) output.append(" "); //$NON-NLS-1$
234 public static StringBuffer printModifiers(int modifiers, StringBuffer output) {
236 if ((modifiers & AccPublic) != 0)
237 output.append("public "); //$NON-NLS-1$
238 if ((modifiers & AccPrivate) != 0)
239 output.append("private "); //$NON-NLS-1$
240 if ((modifiers & AccProtected) != 0)
241 output.append("protected "); //$NON-NLS-1$
242 if ((modifiers & AccStatic) != 0)
243 output.append("static "); //$NON-NLS-1$
244 if ((modifiers & AccFinal) != 0)
245 output.append("final "); //$NON-NLS-1$
246 if ((modifiers & AccSynchronized) != 0)
247 output.append("synchronized "); //$NON-NLS-1$
248 if ((modifiers & AccVolatile) != 0)
249 output.append("volatile "); //$NON-NLS-1$
250 if ((modifiers & AccTransient) != 0)
251 output.append("transient "); //$NON-NLS-1$
252 if ((modifiers & AccNative) != 0)
253 output.append("native "); //$NON-NLS-1$
254 if ((modifiers & AccAbstract) != 0)
255 output.append("abstract "); //$NON-NLS-1$
258 public int sourceStart() {
259 return this.sourceStart;
261 public int sourceEnd() {
262 return this.sourceEnd;
264 public String toString() {
266 return print(0, new StringBuffer(30)).toString();
269 public void traverse(ASTVisitor visitor, BlockScope scope) {
270 // do nothing by default