1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 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 net.sourceforge.phpeclipse.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
14 import net.sourceforge.phpdt.internal.compiler.codegen.Label;
15 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
16 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
17 import net.sourceforge.phpdt.internal.compiler.flow.InsideSubRoutineFlowContext;
18 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
19 import net.sourceforge.phpdt.internal.compiler.lookup.LocalVariableBinding;
20 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
22 public class SynchronizedStatement extends Statement {
24 public Expression expression;
26 public BlockScope scope;
29 public LocalVariableBinding synchroVariable;
30 static final char[] SecretLocalDeclarationName = " syncValue".toCharArray(); //$NON-NLS-1$
32 public SynchronizedStatement(
33 Expression expression,
38 this.expression = expression;
39 this.block = statement;
44 public FlowInfo analyseCode(
45 BlockScope currentScope,
46 FlowContext flowContext,
49 // mark the synthetic variable as being used
50 synchroVariable.useFlag = LocalVariableBinding.USED;
52 // simple propagation to subnodes
56 new InsideSubRoutineFlowContext(flowContext, this),
57 expression.analyseCode(scope, flowContext, flowInfo));
59 // optimizing code gen
60 this.blockExit = !flowInfo.isReachable();
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 // int pc = codeStream.position;
78 // // generate the synchronization expression
79 // expression.generateCode(scope, codeStream, true);
80 // if (block.isEmptyBlock()) {
81 // if ((synchroVariable.type == LongBinding)
82 // || (synchroVariable.type == DoubleBinding)) {
87 // // only take the lock
88 // codeStream.monitorenter();
89 // codeStream.monitorexit();
91 // // enter the monitor
92 // codeStream.store(synchroVariable, true);
93 // codeStream.monitorenter();
95 // // generate the body of the synchronized block
96 // ExceptionLabel anyExceptionHandler = new ExceptionLabel(codeStream, null);
97 // //'null' denotes any kind of exception
98 // block.generateCode(scope, codeStream);
99 // Label endLabel = new Label(codeStream);
101 // codeStream.load(synchroVariable);
102 // codeStream.monitorexit();
103 // codeStream.goto_(endLabel);
105 // // generate the body of the exception handler
106 // anyExceptionHandler.placeEnd();
107 // anyExceptionHandler.place();
108 // codeStream.incrStackSize(1);
109 // codeStream.load(synchroVariable);
110 // codeStream.monitorexit();
111 // codeStream.athrow();
116 // if (scope != currentScope) {
117 // codeStream.exitUserScope(scope);
119 // codeStream.recordPositionsFrom(pc, this.sourceStart);
122 public void resolve(BlockScope upperScope) {
124 // special scope for secret locals optimization.
125 scope = new BlockScope(upperScope);
126 TypeBinding type = expression.resolveType(scope);
138 scope.problemReporter().invalidTypeToSynchronize(expression, type);
141 scope.problemReporter().illegalVoidExpression(expression);
144 scope.problemReporter().invalidNullToSynchronize(expression);
147 //continue even on errors in order to have the TC done into the statements
148 synchroVariable = new LocalVariableBinding(SecretLocalDeclarationName, type, AccDefault, false);
149 scope.addLocalVariable(synchroVariable);
150 synchroVariable.constant = NotAConstant; // not inlinable
151 expression.implicitWidening(type, type);
152 block.resolveUsing(scope);
155 public String toString(int tab) {
157 String s = tabString(tab);
158 s = s + "synchronized (" + expression.toStringExpression() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
159 s = s + "\n" + block.toString(tab + 1); //$NON-NLS-1$
163 public void traverse(
164 IAbstractSyntaxTreeVisitor visitor,
165 BlockScope blockScope) {
167 if (visitor.visit(this, blockScope)) {
168 expression.traverse(visitor, scope);
169 block.traverse(visitor, scope);
171 visitor.endVisit(this, blockScope);