1 /*******************************************************************************
2  * Copyright (c) 2000, 2014 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *     Stephan Herrmann - Contribution for
14  *								Bug 400874 - [1.8][compiler] Inference infrastructure should evolve to meet JLS8 18.x (Part G of JSR335 spec)
15  *								Bug 427438 - [1.8][compiler] NPE at org.eclipse.jdt.internal.compiler.ast.ConditionalExpression.generateCode(ConditionalExpression.java:280)
16  *******************************************************************************/
17 package org.eclipse.jdt.internal.compiler.ast;
18 
19 import org.eclipse.jdt.internal.compiler.flow.FlowContext;
20 import org.eclipse.jdt.internal.compiler.flow.FlowInfo;
21 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
22 import org.eclipse.jdt.internal.compiler.lookup.InferenceContext18;
23 import org.eclipse.jdt.internal.compiler.lookup.InvocationSite;
24 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
25 import org.eclipse.jdt.internal.compiler.lookup.Scope;
26 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
27 
28 public abstract class AbstractVariableDeclaration extends Statement implements InvocationSite {
29 	public int declarationEnd;
30 	/**
31 	 * For local declarations (outside of for statement initialization) and field declarations,
32 	 * the declarationSourceEnd covers multiple locals if any.
33 	 * For local declarations inside for statement initialization, this is not the case.
34 	 */
35 	public int declarationSourceEnd;
36 	public int declarationSourceStart;
37 	public int hiddenVariableDepth; // used to diagnose hiding scenarii
38 	public Expression initialization;
39 	public int modifiers;
40 	public int modifiersSourceStart;
41 	public Annotation[] annotations;
42 
43 	public char[] name;
44 
45 	public TypeReference type;
46 
47 	@Override
analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo)48 	public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
49 		return flowInfo;
50 	}
51 
52 	public static final int FIELD = 1;
53 	public static final int INITIALIZER = 2;
54 	public static final int ENUM_CONSTANT = 3;
55 	public static final int LOCAL_VARIABLE = 4;
56 	public static final int PARAMETER = 5;
57 	public static final int TYPE_PARAMETER = 6;
58 	public static final int RECORD_COMPONENT = 7; // record
59 
60 
61 	/**
62 	 * @see org.eclipse.jdt.internal.compiler.lookup.InvocationSite#genericTypeArguments()
63 	 */
64 	@Override
genericTypeArguments()65 	public TypeBinding[] genericTypeArguments() {
66 		return null;
67 	}
68 
69 	/**
70 	 * Returns the constant kind of this variable declaration
71 	 */
getKind()72 	public abstract int getKind();
73 
74 	@Override
freshInferenceContext(Scope scope)75 	public InferenceContext18 freshInferenceContext(Scope scope) {
76 		return null;
77 	}
78 
79 	@Override
isSuperAccess()80 	public boolean isSuperAccess() {
81 		return false;
82 	}
83 
84 	@Override
isTypeAccess()85 	public boolean isTypeAccess() {
86 		return false;
87 	}
88 
89 	@Override
printStatement(int indent, StringBuffer output)90 	public StringBuffer printStatement(int indent, StringBuffer output) {
91 		printAsExpression(indent, output);
92 		switch(getKind()) {
93 			case ENUM_CONSTANT:
94 				return output.append(',');
95 			default:
96 				return output.append(';');
97 		}
98 	}
99 
printAsExpression(int indent, StringBuffer output)100 	public StringBuffer printAsExpression(int indent, StringBuffer output) {
101 		printIndent(indent, output);
102 		printModifiers(this.modifiers, output);
103 		if (this.annotations != null) {
104 			printAnnotations(this.annotations, output);
105 			output.append(' ');
106 		}
107 
108 		if (this.type != null) {
109 			this.type.print(0, output).append(' ');
110 		}
111 		output.append(this.name);
112 		switch(getKind()) {
113 			case ENUM_CONSTANT:
114 				if (this.initialization != null) {
115 					this.initialization.printExpression(indent, output);
116 				}
117 				break;
118 			default:
119 				if (this.initialization != null) {
120 					output.append(" = "); //$NON-NLS-1$
121 					this.initialization.printExpression(indent, output);
122 				}
123 		}
124 		return output;
125 	}
126 
127 	@Override
resolve(BlockScope scope)128 	public void resolve(BlockScope scope) {
129 		// do nothing by default (redefined for local variables)
130 	}
131 
132 	@Override
setActualReceiverType(ReferenceBinding receiverType)133 	public void setActualReceiverType(ReferenceBinding receiverType) {
134 		// do nothing by default
135 	}
136 
137 	@Override
setDepth(int depth)138 	public void setDepth(int depth) {
139 
140 		this.hiddenVariableDepth = depth;
141 	}
142 
143 	@Override
setFieldIndex(int depth)144 	public void setFieldIndex(int depth) {
145 		// do nothing by default
146 	}
147 }
148