1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
14 import net.sourceforge.phpdt.internal.compiler.codegen.CodeStream;
15 import net.sourceforge.phpdt.internal.compiler.codegen.ExceptionLabel;
16 import net.sourceforge.phpdt.internal.compiler.codegen.Label;
17 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
18 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
19 import net.sourceforge.phpdt.internal.compiler.flow.InsideSubRoutineFlowContext;
20 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
21 import net.sourceforge.phpdt.internal.compiler.lookup.LocalVariableBinding;
22 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
24 public class SynchronizedStatement extends Statement {
26 public Expression expression;
28 public BlockScope scope;
31 public LocalVariableBinding synchroVariable;
32 static final char[] SecretLocalDeclarationName = " syncValue".toCharArray(); //$NON-NLS-1$
34 public SynchronizedStatement(
35 Expression expression,
40 this.expression = expression;
41 this.block = statement;
46 public FlowInfo analyseCode(
47 BlockScope currentScope,
48 FlowContext flowContext,
51 // mark the synthetic variable as being used
52 synchroVariable.used = true;
54 // simple propagation to subnodes
58 new InsideSubRoutineFlowContext(flowContext, this),
59 expression.analyseCode(scope, flowContext, flowInfo));
61 // optimizing code gen
62 if ((flowInfo == FlowInfo.DeadEnd) || flowInfo.isFakeReachable()) {
69 * Synchronized statement code generation
71 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
72 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
74 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
76 if ((bits & IsReachableMASK) == 0) {
79 int pc = codeStream.position;
81 // generate the synchronization expression
82 expression.generateCode(scope, codeStream, true);
83 if (block.isEmptyBlock()) {
84 if ((synchroVariable.type == LongBinding)
85 || (synchroVariable.type == DoubleBinding)) {
91 codeStream.monitorenter();
92 codeStream.monitorexit();
95 codeStream.store(synchroVariable, true);
96 codeStream.monitorenter();
98 // generate the body of the synchronized block
99 ExceptionLabel anyExceptionHandler = new ExceptionLabel(codeStream, null);
100 //'null' denotes any kind of exception
101 block.generateCode(scope, codeStream);
102 Label endLabel = new Label(codeStream);
104 codeStream.load(synchroVariable);
105 codeStream.monitorexit();
106 codeStream.goto_(endLabel);
108 // generate the body of the exception handler
109 anyExceptionHandler.placeEnd();
110 anyExceptionHandler.place();
111 codeStream.incrStackSize(1);
112 codeStream.load(synchroVariable);
113 codeStream.monitorexit();
119 if (scope != currentScope) {
120 codeStream.exitUserScope(scope);
122 codeStream.recordPositionsFrom(pc, this.sourceStart);
125 public void resolve(BlockScope upperScope) {
127 // special scope for secret locals optimization.
128 scope = new BlockScope(upperScope);
129 TypeBinding type = expression.resolveType(scope);
141 scope.problemReporter().invalidTypeToSynchronize(expression, type);
144 scope.problemReporter().illegalVoidExpression(expression);
147 scope.problemReporter().invalidNullToSynchronize(expression);
150 //continue even on errors in order to have the TC done into the statements
151 synchroVariable = new LocalVariableBinding(SecretLocalDeclarationName, type, AccDefault, false);
152 scope.addLocalVariable(synchroVariable);
153 synchroVariable.constant = NotAConstant; // not inlinable
154 expression.implicitWidening(type, type);
155 block.resolveUsing(scope);
158 public String toString(int tab) {
160 String s = tabString(tab);
161 s = s + "synchronized (" + expression.toStringExpression() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
162 s = s + "\n" + block.toString(tab + 1); //$NON-NLS-1$
166 public void traverse(
167 IAbstractSyntaxTreeVisitor visitor,
168 BlockScope blockScope) {
170 if (visitor.visit(this, blockScope)) {
171 expression.traverse(visitor, scope);
172 block.traverse(visitor, scope);
174 visitor.endVisit(this, blockScope);