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 // mark the synthetic variable as being used
45 synchroVariable.useFlag = LocalVariableBinding.USED;
47 // simple propagation to subnodes
51 new InsideSubRoutineFlowContext(flowContext, this),
52 expression.analyseCode(scope, flowContext, flowInfo));
54 // optimizing code gen
55 this.blockExit = !flowInfo.isReachable();
60 public boolean isSubRoutineEscaping() {
66 * Synchronized statement code generation
68 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
69 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
71 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
73 if ((bits & IsReachableMASK) == 0) {
76 // in case the labels needs to be reinitialized
77 // when the code generation is restarted in wide mode
78 if (this.anyExceptionLabelsCount > 0) {
79 this.anyExceptionLabels = NO_EXCEPTION_HANDLER;
80 this.anyExceptionLabelsCount = 0;
82 int pc = codeStream.position;
84 // generate the synchronization expression
85 expression.generateCode(scope, codeStream, true);
86 if (block.isEmptyBlock()) {
87 if ((synchroVariable.type == LongBinding)
88 || (synchroVariable.type == DoubleBinding)) {
94 codeStream.monitorenter();
95 codeStream.monitorexit();
98 codeStream.store(synchroVariable, true);
99 codeStream.monitorenter();
101 // generate the body of the synchronized block
102 this.enterAnyExceptionHandler(codeStream);
103 block.generateCode(scope, codeStream);
104 Label endLabel = new Label(codeStream);
106 codeStream.load(synchroVariable);
107 codeStream.monitorexit();
108 codeStream.goto_(endLabel);
110 // generate the body of the exception handler
111 this.exitAnyExceptionHandler();
112 this.placeAllAnyExceptionHandlers();
113 codeStream.incrStackSize(1);
114 codeStream.load(synchroVariable);
115 codeStream.monitorexit();
121 if (scope != currentScope) {
122 codeStream.exitUserScope(scope);
124 codeStream.recordPositionsFrom(pc, this.sourceStart);
128 * @see org.eclipse.jdt.internal.compiler.ast.SubRoutineStatement#generateSubRoutineInvocation(org.eclipse.jdt.internal.compiler.lookup.BlockScope, org.eclipse.jdt.internal.compiler.codegen.CodeStream)
130 public void generateSubRoutineInvocation(
131 BlockScope currentScope,
132 CodeStream codeStream) {
134 codeStream.load(this.synchroVariable);
135 codeStream.monitorexit();
138 public void resolve(BlockScope upperScope) {
140 // special scope for secret locals optimization.
141 scope = new BlockScope(upperScope);
142 TypeBinding type = expression.resolveType(scope);
154 scope.problemReporter().invalidTypeToSynchronize(expression, type);
157 scope.problemReporter().illegalVoidExpression(expression);
160 scope.problemReporter().invalidNullToSynchronize(expression);
163 //continue even on errors in order to have the TC done into the statements
164 synchroVariable = new LocalVariableBinding(SecretLocalDeclarationName, type, AccDefault, false);
165 scope.addLocalVariable(synchroVariable);
166 synchroVariable.constant = NotAConstant; // not inlinable
167 expression.implicitWidening(type, type);
168 block.resolveUsing(scope);
171 public StringBuffer printStatement(int indent, StringBuffer output) {
173 printIndent(indent, output);
174 output.append("synchronized ("); //$NON-NLS-1$
175 expression.printExpression(0, output).append(')');
177 return block.printStatement(indent + 1, output);
180 public void traverse(
182 BlockScope blockScope) {
184 if (visitor.visit(this, blockScope)) {
185 expression.traverse(visitor, scope);
186 block.traverse(visitor, scope);
188 visitor.endVisit(this, blockScope);