More 3.1 compatibility
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / lookup / LocalVariableBinding.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.lookup;
12
13 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
14 import net.sourceforge.phpeclipse.internal.compiler.ast.LocalDeclaration;
15
16 public class LocalVariableBinding extends VariableBinding {
17
18         public boolean isArgument;
19         public int resolvedPosition; // for code generation (position in method context)
20         
21         public static final int UNUSED = 0;
22         public static final int USED = 1;
23         public static final int FAKE_USED = 2;
24         public int useFlag; // for flow analysis (default is UNUSED)
25         
26         public BlockScope declaringScope; // back-pointer to its declaring scope
27         public LocalDeclaration declaration; // for source-positions
28
29         public int[] initializationPCs;
30         public int initializationCount = 0;
31
32         // for synthetic local variables        
33         public LocalVariableBinding(char[] name, TypeBinding type, int modifiers, boolean isArgument) {
34
35                 this.name = name;
36                 this.type = type;
37                 this.modifiers = modifiers;
38                 if (this.isArgument = isArgument)
39                         this.constant = Constant.NotAConstant;
40         }
41         
42         // regular local variable or argument
43         public LocalVariableBinding(LocalDeclaration declaration, TypeBinding type, int modifiers, boolean isArgument) {
44
45                 this(declaration.name, type, modifiers, isArgument);
46                 this.declaration = declaration;
47         }
48
49         /* API
50         * Answer the receiver's binding type from Binding.BindingID.
51         */
52         public final int bindingType() {
53
54                 return LOCAL;
55         }
56         
57         // Answer whether the variable binding is a secret variable added for code gen purposes
58         public boolean isSecret() {
59
60                 return declaration == null && !isArgument;
61         }
62
63         public void recordInitializationEndPC(int pc) {
64
65                 if (initializationPCs[((initializationCount - 1) << 1) + 1] == -1)
66                         initializationPCs[((initializationCount - 1) << 1) + 1] = pc;
67         }
68
69         public void recordInitializationStartPC(int pc) {
70
71                 if (initializationPCs == null)  return;
72                 // optimize cases where reopening a contiguous interval
73                 if ((initializationCount > 0) && (initializationPCs[ ((initializationCount - 1) << 1) + 1] == pc)) {
74                         initializationPCs[ ((initializationCount - 1) << 1) + 1] = -1; // reuse previous interval (its range will be augmented)
75                 } else {
76                         int index = initializationCount << 1;
77                         if (index == initializationPCs.length) {
78                                 System.arraycopy(initializationPCs, 0, (initializationPCs = new int[initializationCount << 2]), 0, index);
79                         }
80                         initializationPCs[index] = pc;
81                         initializationPCs[index + 1] = -1;
82                         initializationCount++;
83                 }
84         }
85
86         public String toString() {
87
88                 String s = super.toString();
89                 switch (useFlag){
90                         case USED:
91                                 s += "[pos: " + String.valueOf(resolvedPosition) + "]"; //$NON-NLS-2$ //$NON-NLS-1$
92                                 break;
93                         case UNUSED:
94                                 s += "[pos: unused]"; //$NON-NLS-1$
95                                 break;
96                         case FAKE_USED:
97                                 s += "[pos: fake_used]"; //$NON-NLS-1$
98                                 break;
99                 }
100                 s += "[id:" + String.valueOf(id) + "]"; //$NON-NLS-2$ //$NON-NLS-1$
101                 if (initializationCount > 0) {
102                         s += "[pc: "; //$NON-NLS-1$
103                         for (int i = 0; i < initializationCount; i++) {
104                                 if (i > 0)
105                                         s += ", "; //$NON-NLS-1$
106                                 s += String.valueOf(initializationPCs[i << 1]) + "-" + ((initializationPCs[(i << 1) + 1] == -1) ? "?" : String.valueOf(initializationPCs[(i<< 1) + 1])); //$NON-NLS-2$ //$NON-NLS-1$
107                         }
108                         s += "]"; //$NON-NLS-1$
109                 }
110                 return s;
111         }
112 }