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.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.codegen.*;
15 import org.eclipse.jdt.internal.compiler.flow.*;
16 import org.eclipse.jdt.internal.compiler.lookup.*;
18 public class SynchronizedStatement extends SubRoutineStatement {
20 public Expression expression;
22 public BlockScope scope;
24 public LocalVariableBinding synchroVariable;
25 static final char[] SecretLocalDeclarationName = " syncValue".toCharArray(); //$NON-NLS-1$
27 public SynchronizedStatement(
28 Expression expression,
33 this.expression = expression;
34 this.block = statement;
39 public FlowInfo analyseCode(
40 BlockScope currentScope,
41 FlowContext flowContext,
44 // TODO (philippe) shouldn't it be protected by a check whether reachable statement ?
46 // mark the synthetic variable as being used
47 synchroVariable.useFlag = LocalVariableBinding.USED;
49 // simple propagation to subnodes
53 new InsideSubRoutineFlowContext(flowContext, this),
54 expression.analyseCode(scope, flowContext, flowInfo));
56 // optimizing code gen
57 this.blockExit = !flowInfo.isReachable();
62 public boolean isSubRoutineEscaping() {
68 * Synchronized statement code generation
70 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
71 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
73 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
75 if ((bits & IsReachableMASK) == 0) {
78 // in case the labels needs to be reinitialized
79 // when the code generation is restarted in wide mode
80 if (this.anyExceptionLabelsCount > 0) {
81 this.anyExceptionLabels = NO_EXCEPTION_HANDLER;
82 this.anyExceptionLabelsCount = 0;
84 int pc = codeStream.position;
86 // generate the synchronization expression
87 expression.generateCode(scope, codeStream, true);
88 if (block.isEmptyBlock()) {
89 if ((synchroVariable.type == LongBinding)
90 || (synchroVariable.type == DoubleBinding)) {
96 codeStream.monitorenter();
97 codeStream.monitorexit();
100 codeStream.store(synchroVariable, true);
101 codeStream.monitorenter();
103 // generate the body of the synchronized block
104 this.enterAnyExceptionHandler(codeStream);
105 block.generateCode(scope, codeStream);
106 Label endLabel = new Label(codeStream);
108 codeStream.load(synchroVariable);
109 codeStream.monitorexit();
110 this.exitAnyExceptionHandler();
111 codeStream.goto_(endLabel);
112 this.enterAnyExceptionHandler(codeStream);
114 // generate the body of the exception handler
115 this.placeAllAnyExceptionHandlers();
116 codeStream.incrStackSize(1);
117 codeStream.load(synchroVariable);
118 codeStream.monitorexit();
119 this.exitAnyExceptionHandler();
125 if (scope != currentScope) {
126 codeStream.exitUserScope(scope);
128 codeStream.recordPositionsFrom(pc, this.sourceStart);
132 * @see org.eclipse.jdt.internal.compiler.ast.SubRoutineStatement#generateSubRoutineInvocation(org.eclipse.jdt.internal.compiler.lookup.BlockScope, org.eclipse.jdt.internal.compiler.codegen.CodeStream)
134 public void generateSubRoutineInvocation(
135 BlockScope currentScope,
136 CodeStream codeStream) {
138 codeStream.load(this.synchroVariable);
139 codeStream.monitorexit();
142 public void resolve(BlockScope upperScope) {
144 // special scope for secret locals optimization.
145 scope = new BlockScope(upperScope);
146 TypeBinding type = expression.resolveType(scope);
158 scope.problemReporter().invalidTypeToSynchronize(expression, type);
161 scope.problemReporter().illegalVoidExpression(expression);
164 scope.problemReporter().invalidNullToSynchronize(expression);
167 //continue even on errors in order to have the TC done into the statements
168 synchroVariable = new LocalVariableBinding(SecretLocalDeclarationName, type, AccDefault, false);
169 scope.addLocalVariable(synchroVariable);
170 synchroVariable.setConstant(NotAConstant); // not inlinable
171 expression.computeConversion(scope, type, type);
172 block.resolveUsing(scope);
175 public StringBuffer printStatement(int indent, StringBuffer output) {
177 printIndent(indent, output);
178 output.append("synchronized ("); //$NON-NLS-1$
179 expression.printExpression(0, output).append(')');
181 return block.printStatement(indent + 1, output);
184 public void traverse(
186 BlockScope blockScope) {
188 if (visitor.visit(this, blockScope)) {
189 expression.traverse(visitor, scope);
190 block.traverse(visitor, scope);
192 visitor.endVisit(this, blockScope);