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.flow.FlowContext;
14 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
15 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
16 import net.sourceforge.phpdt.internal.compiler.lookup.BaseTypeBinding;
17 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
18 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
20 public class Expression extends Statement {
22 //some expression may not be used - from a java semantic point
23 //of view only - as statements. Other may. In order to avoid the creation
24 //of wrappers around expression in order to tune them as expression
25 //Expression is a subclass of Statement. See the message isValidJavaStatement()
27 public int implicitConversion;
28 public TypeBinding resolvedType;
30 public Constant constant;
36 public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
41 public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo, boolean valueRequired) {
43 return analyseCode(currentScope, flowContext, flowInfo);
47 * Constant usable for bytecode pattern optimizations, but cannot be inlined
48 * since it is not strictly equivalent to the definition of constant expressions.
49 * In particular, some side-effects may be required to occur (only the end value
51 * Constant is known to be of boolean type
53 public Constant optimizedBooleanConstant() {
58 public static final boolean isConstantValueRepresentable(
63 //true if there is no loss of precision while casting.
64 // constantTypeID == constant.typeID
65 if (targetTypeID == constantTypeID)
67 switch (targetTypeID) {
69 switch (constantTypeID) {
73 return constant.doubleValue() == constant.charValue();
75 return constant.floatValue() == constant.charValue();
77 return constant.intValue() == constant.charValue();
79 return constant.shortValue() == constant.charValue();
81 return constant.byteValue() == constant.charValue();
83 return constant.longValue() == constant.charValue();
85 return false;//boolean
89 switch (constantTypeID) {
91 return constant.charValue() == constant.floatValue();
93 return constant.doubleValue() == constant.floatValue();
97 return constant.intValue() == constant.floatValue();
99 return constant.shortValue() == constant.floatValue();
101 return constant.byteValue() == constant.floatValue();
103 return constant.longValue() == constant.floatValue();
105 return false;//boolean
109 switch (constantTypeID) {
111 return constant.charValue() == constant.doubleValue();
115 return constant.floatValue() == constant.doubleValue();
117 return constant.intValue() == constant.doubleValue();
119 return constant.shortValue() == constant.doubleValue();
121 return constant.byteValue() == constant.doubleValue();
123 return constant.longValue() == constant.doubleValue();
125 return false; //boolean
129 switch (constantTypeID) {
131 return constant.charValue() == constant.byteValue();
133 return constant.doubleValue() == constant.byteValue();
135 return constant.floatValue() == constant.byteValue();
137 return constant.intValue() == constant.byteValue();
139 return constant.shortValue() == constant.byteValue();
143 return constant.longValue() == constant.byteValue();
145 return false; //boolean
149 switch (constantTypeID) {
151 return constant.charValue() == constant.shortValue();
153 return constant.doubleValue() == constant.shortValue();
155 return constant.floatValue() == constant.shortValue();
157 return constant.intValue() == constant.shortValue();
161 return constant.byteValue() == constant.shortValue();
163 return constant.longValue() == constant.shortValue();
165 return false; //boolean
169 switch (constantTypeID) {
171 return constant.charValue() == constant.intValue();
173 return constant.doubleValue() == constant.intValue();
175 return constant.floatValue() == constant.intValue();
179 return constant.shortValue() == constant.intValue();
181 return constant.byteValue() == constant.intValue();
183 return constant.longValue() == constant.intValue();
185 return false; //boolean
189 switch (constantTypeID) {
191 return constant.charValue() == constant.longValue();
193 return constant.doubleValue() == constant.longValue();
195 return constant.floatValue() == constant.longValue();
197 return constant.intValue() == constant.longValue();
199 return constant.shortValue() == constant.longValue();
201 return constant.byteValue() == constant.longValue();
205 return false; //boolean
209 return false; //boolean
214 * Expression statements are plain expressions, however they generate like
215 * normal expressions with no value required.
217 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
218 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
220 // public void generateCode(BlockScope currentScope, CodeStream codeStream) {
222 // if ((bits & IsReachableMASK) == 0) {
225 // generateCode(currentScope, codeStream, false);
229 * Every expression is responsible for generating its implicit conversion when necessary.
231 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
232 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
233 * @param valueRequired boolean
235 // public void generateCode(
236 // BlockScope currentScope,
237 // CodeStream codeStream,
238 // boolean valueRequired) {
240 // if (constant != NotAConstant) {
241 // // generate a constant expression
242 // int pc = codeStream.position;
243 // codeStream.generateConstant(constant, implicitConversion);
244 // codeStream.recordPositionsFrom(pc, this.sourceStart);
246 // // actual non-constant code generation
247 // throw new ShouldNotImplement(ProjectPrefUtil.bind("ast.missingCode")); //$NON-NLS-1$
252 * Default generation of a boolean value
254 // public void generateOptimizedBoolean(
255 // BlockScope currentScope,
256 // CodeStream codeStream,
259 // boolean valueRequired) {
261 // // a label valued to nil means: by default we fall through the case...
262 // // both nil means we leave the value on the stack
264 // if ((constant != Constant.NotAConstant) && (constant.typeID() == T_boolean)) {
265 // int pc = codeStream.position;
266 // if (constant.booleanValue() == true) {
267 // // constant == true
268 // if (valueRequired) {
269 // if (falseLabel == null) {
270 // // implicit falling through the FALSE case
271 // if (trueLabel != null) {
272 // codeStream.goto_(trueLabel);
277 // if (valueRequired) {
278 // if (falseLabel != null) {
279 // // implicit falling through the TRUE case
280 // if (trueLabel == null) {
281 // codeStream.goto_(falseLabel);
286 // codeStream.recordPositionsFrom(pc, this.sourceStart);
289 // generateCode(currentScope, codeStream, valueRequired);
291 // int position = codeStream.position;
292 // if (valueRequired) {
293 // if (falseLabel == null) {
294 // if (trueLabel != null) {
295 // // Implicit falling through the FALSE case
296 // codeStream.ifne(trueLabel);
299 // if (trueLabel == null) {
300 // // Implicit falling through the TRUE case
301 // codeStream.ifeq(falseLabel);
303 // // No implicit fall through TRUE/FALSE --> should never occur
307 // // reposition the endPC
308 // codeStream.updateLastRecordedEndPC(position);
311 // /* Optimized (java) code generation for string concatenations that involve StringBuffer
312 // * creation: going through this path means that there is no need for a new StringBuffer
313 // * creation, further operands should rather be only appended to the current one.
314 // * By default: no optimization.
316 // public void generateOptimizedStringBuffer(
317 // BlockScope blockScope,
318 // org.eclipse.jdt.internal.compiler.codegen.CodeStream codeStream,
321 // generateCode(blockScope, codeStream, true);
322 // codeStream.invokeStringBufferAppendForType(typeID);
325 /* Optimized (java) code generation for string concatenations that involve StringBuffer
326 * creation: going through this path means that there is no need for a new StringBuffer
327 * creation, further operands should rather be only appended to the current one.
329 // public void generateOptimizedStringBufferCreation(
330 // BlockScope blockScope,
331 // CodeStream codeStream,
334 // // Optimization only for integers and strings
335 // if (typeID == T_Object) {
336 // // in the case the runtime value of valueOf(Object) returns null, we have to use append(Object) instead of directly valueOf(Object)
337 // // append(Object) returns append(valueOf(Object)), which means that the null case is handled by append(String).
338 // codeStream.newStringBuffer();
340 // codeStream.invokeStringBufferDefaultConstructor();
341 // generateCode(blockScope, codeStream, true);
342 // codeStream.invokeStringBufferAppendForType(T_Object);
345 // codeStream.newStringBuffer();
347 // if (typeID == T_String || typeID == T_null) {
348 // if (constant != NotAConstant) {
349 // codeStream.ldc(constant.stringValue());
351 // generateCode(blockScope, codeStream, true);
352 // codeStream.invokeStringValueOf(T_Object);
355 // generateCode(blockScope, codeStream, true);
356 // codeStream.invokeStringValueOf(typeID);
358 // codeStream.invokeStringBufferStringConstructor();
361 // Base types need that the widening is explicitly done by the compiler using some bytecode like i2f
362 public void implicitWidening(
363 TypeBinding runtimeTimeType,
364 TypeBinding compileTimeType) {
366 if (runtimeTimeType == null || compileTimeType == null)
369 // if (compileTimeType.id == T_null) {
370 // // this case is possible only for constant null
371 // // The type of runtime is a reference type
372 // // The code gen use the constant id thus any value
373 // // for the runtime id (akak the <<4) could be used.
374 // // T_Object is used as some general T_reference
375 // implicitConversion = (T_Object << 4) + T_null;
379 switch (runtimeTimeType.id) {
383 implicitConversion = (T_int << 4) + compileTimeType.id;
389 case T_int : //implicitConversion may result in i2i which will result in NO code gen
391 implicitConversion = (runtimeTimeType.id << 4) + compileTimeType.id;
393 default : //nothing on regular object ref
397 public boolean isCompactableOperation() {
402 //Return true if the conversion is done AUTOMATICALLY by the vm
403 //while the javaVM is an int based-machine, thus for example pushing
404 //a byte onto the stack , will automatically creates a int on the stack
405 //(this request some work d be done by the VM on signed numbers)
406 public boolean isConstantValueOfTypeAssignableToType(
407 TypeBinding constantType,
408 TypeBinding targetType) {
410 if (constant == Constant.NotAConstant)
412 if (constantType == targetType)
414 if (constantType.isBaseType() && targetType.isBaseType()) {
415 //No free assignment conversion from anything but to integral ones.
416 if ((constantType == IntBinding
417 || BaseTypeBinding.isWidening(T_int, constantType.id))
418 && (BaseTypeBinding.isNarrowing(targetType.id, T_int))) {
419 //use current explicit conversion in order to get some new value to compare with current one
420 return isConstantValueRepresentable(constant, constantType.id, targetType.id);
426 public boolean isTypeReference() {
430 public void resolve(BlockScope scope) {
431 // drops the returning expression's type whatever the type is.
433 this.resolveType(scope);
437 public TypeBinding resolveType(BlockScope scope) {
438 // by default... subclasses should implement a better TC if required.
443 public TypeBinding resolveTypeExpecting(
445 TypeBinding expectedType) {
447 TypeBinding expressionType = this.resolveType(scope);
448 if (expressionType == null) return null;
449 if (expressionType == expectedType) return expressionType;
451 if (!expressionType.isCompatibleWith(expectedType)) {
452 scope.problemReporter().typeMismatchError(expressionType, expectedType, this);
455 return expressionType;
458 public String toString(int tab) {
460 //Subclass re-define toStringExpression
461 String s = tabString(tab);
462 if (constant != null)
463 //before TC has runned
464 if (constant != NotAConstant)
465 //after the TC has runned
466 s += " /*cst:" + constant.toString() + "*/ "; //$NON-NLS-1$ //$NON-NLS-2$
467 return s + toStringExpression(tab);
470 //Subclass re-define toStringExpression
471 //This method is abstract and should never be called
472 //but we provide some code that is running.....just in case
473 //of developpement time (while every thing is not built)
474 public String toStringExpression() {
476 return super.toString(0);
479 public String toStringExpression(int tab) {
480 // default is regular toString expression (qualified allocation expressions redifine this method)
481 return this.toStringExpression();
484 public Expression toTypeReference() {
485 //by default undefined
487 //this method is meanly used by the parser in order to transform
488 //an expression that is used as a type reference in a cast ....
489 //--appreciate the fact that castExpression and ExpressionWithParenthesis
490 //--starts with the same pattern.....