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.impl.*;
14 import org.eclipse.jdt.internal.compiler.codegen.*;
15 import org.eclipse.jdt.internal.compiler.flow.*;
16 import org.eclipse.jdt.internal.compiler.lookup.*;
17 import org.eclipse.jdt.internal.compiler.problem.*;
18 import org.eclipse.jdt.internal.compiler.util.Util;
20 public abstract 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 * @return Constant known to be of boolean type
53 public Constant optimizedBooleanConstant() {
57 public static final boolean isConstantValueRepresentable(
62 //true if there is no loss of precision while casting.
63 // constantTypeID == constant.typeID
64 if (targetTypeID == constantTypeID)
66 switch (targetTypeID) {
68 switch (constantTypeID) {
72 return constant.doubleValue() == constant.charValue();
74 return constant.floatValue() == constant.charValue();
76 return constant.intValue() == constant.charValue();
78 return constant.shortValue() == constant.charValue();
80 return constant.byteValue() == constant.charValue();
82 return constant.longValue() == constant.charValue();
84 return false;//boolean
88 switch (constantTypeID) {
90 return constant.charValue() == constant.floatValue();
92 return constant.doubleValue() == constant.floatValue();
96 return constant.intValue() == constant.floatValue();
98 return constant.shortValue() == constant.floatValue();
100 return constant.byteValue() == constant.floatValue();
102 return constant.longValue() == constant.floatValue();
104 return false;//boolean
108 switch (constantTypeID) {
110 return constant.charValue() == constant.doubleValue();
114 return constant.floatValue() == constant.doubleValue();
116 return constant.intValue() == constant.doubleValue();
118 return constant.shortValue() == constant.doubleValue();
120 return constant.byteValue() == constant.doubleValue();
122 return constant.longValue() == constant.doubleValue();
124 return false; //boolean
128 switch (constantTypeID) {
130 return constant.charValue() == constant.byteValue();
132 return constant.doubleValue() == constant.byteValue();
134 return constant.floatValue() == constant.byteValue();
136 return constant.intValue() == constant.byteValue();
138 return constant.shortValue() == constant.byteValue();
142 return constant.longValue() == constant.byteValue();
144 return false; //boolean
148 switch (constantTypeID) {
150 return constant.charValue() == constant.shortValue();
152 return constant.doubleValue() == constant.shortValue();
154 return constant.floatValue() == constant.shortValue();
156 return constant.intValue() == constant.shortValue();
160 return constant.byteValue() == constant.shortValue();
162 return constant.longValue() == constant.shortValue();
164 return false; //boolean
168 switch (constantTypeID) {
170 return constant.charValue() == constant.intValue();
172 return constant.doubleValue() == constant.intValue();
174 return constant.floatValue() == constant.intValue();
178 return constant.shortValue() == constant.intValue();
180 return constant.byteValue() == constant.intValue();
182 return constant.longValue() == constant.intValue();
184 return false; //boolean
188 switch (constantTypeID) {
190 return constant.charValue() == constant.longValue();
192 return constant.doubleValue() == constant.longValue();
194 return constant.floatValue() == constant.longValue();
196 return constant.intValue() == constant.longValue();
198 return constant.shortValue() == constant.longValue();
200 return constant.byteValue() == constant.longValue();
204 return false; //boolean
208 return false; //boolean
213 * Expression statements are plain expressions, however they generate like
214 * normal expressions with no value required.
216 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
217 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
219 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
221 if ((bits & IsReachableMASK) == 0) {
224 generateCode(currentScope, codeStream, false);
228 * Every expression is responsible for generating its implicit conversion when necessary.
230 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
231 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
232 * @param valueRequired boolean
234 public void generateCode(
235 BlockScope currentScope,
236 CodeStream codeStream,
237 boolean valueRequired) {
239 if (constant != NotAConstant) {
240 // generate a constant expression
241 int pc = codeStream.position;
242 codeStream.generateConstant(constant, implicitConversion);
243 codeStream.recordPositionsFrom(pc, this.sourceStart);
245 // actual non-constant code generation
246 throw new ShouldNotImplement(Util.bind("ast.missingCode")); //$NON-NLS-1$
251 * Default generation of a boolean value
252 * @param currentScope
256 * @param valueRequired
258 public void generateOptimizedBoolean(
259 BlockScope currentScope,
260 CodeStream codeStream,
263 boolean valueRequired) {
265 // a label valued to nil means: by default we fall through the case...
266 // both nil means we leave the value on the stack
268 if ((constant != Constant.NotAConstant) && (constant.typeID() == T_boolean)) {
269 int pc = codeStream.position;
270 if (constant.booleanValue() == true) {
273 if (falseLabel == null) {
274 // implicit falling through the FALSE case
275 if (trueLabel != null) {
276 codeStream.goto_(trueLabel);
282 if (falseLabel != null) {
283 // implicit falling through the TRUE case
284 if (trueLabel == null) {
285 codeStream.goto_(falseLabel);
290 codeStream.recordPositionsFrom(pc, this.sourceStart);
293 generateCode(currentScope, codeStream, valueRequired);
295 int position = codeStream.position;
297 if (falseLabel == null) {
298 if (trueLabel != null) {
299 // Implicit falling through the FALSE case
300 codeStream.ifne(trueLabel);
303 if (trueLabel == null) {
304 // Implicit falling through the TRUE case
305 codeStream.ifeq(falseLabel);
307 // No implicit fall through TRUE/FALSE --> should never occur
311 // reposition the endPC
312 codeStream.updateLastRecordedEndPC(position);
315 /* Optimized (java) code generation for string concatenations that involve StringBuffer
316 * creation: going through this path means that there is no need for a new StringBuffer
317 * creation, further operands should rather be only appended to the current one.
318 * By default: no optimization.
320 public void generateOptimizedStringBuffer(
321 BlockScope blockScope,
322 org.eclipse.jdt.internal.compiler.codegen.CodeStream codeStream,
325 if (typeID == T_String && this.constant != NotAConstant && this.constant.stringValue().length() == 0) {
326 return; // optimize str + ""
328 generateCode(blockScope, codeStream, true);
329 codeStream.invokeStringBufferAppendForType(typeID);
332 /* Optimized (java) code generation for string concatenations that involve StringBuffer
333 * creation: going through this path means that there is no need for a new StringBuffer
334 * creation, further operands should rather be only appended to the current one.
336 public void generateOptimizedStringBufferCreation(
337 BlockScope blockScope,
338 CodeStream codeStream,
341 // Optimization only for integers and strings
342 if (typeID == T_Object) {
343 // in the case the runtime value of valueOf(Object) returns null, we have to use append(Object) instead of directly valueOf(Object)
344 // append(Object) returns append(valueOf(Object)), which means that the null case is handled by append(String).
345 codeStream.newStringBuffer();
347 codeStream.invokeStringBufferDefaultConstructor();
348 generateCode(blockScope, codeStream, true);
349 codeStream.invokeStringBufferAppendForType(T_Object);
352 codeStream.newStringBuffer();
354 if (typeID == T_String || typeID == T_null) {
355 if (constant != NotAConstant) {
356 String stringValue = constant.stringValue();
357 if (stringValue.length() == 0) { // optimize ""+<str>
358 codeStream.invokeStringBufferDefaultConstructor();
361 codeStream.ldc(stringValue);
363 generateCode(blockScope, codeStream, true);
364 codeStream.invokeStringValueOf(T_Object);
367 generateCode(blockScope, codeStream, true);
368 codeStream.invokeStringValueOf(typeID);
370 codeStream.invokeStringBufferStringConstructor();
373 // Base types need that the widening is explicitly done by the compiler using some bytecode like i2f
374 public void implicitWidening(
375 TypeBinding runtimeTimeType,
376 TypeBinding compileTimeType) {
378 if (runtimeTimeType == null || compileTimeType == null)
381 // if (compileTimeType.id == T_null) {
382 // // this case is possible only for constant null
383 // // The type of runtime is a reference type
384 // // The code gen use the constant id thus any value
385 // // for the runtime id (akak the <<4) could be used.
386 // // T_Object is used as some general T_reference
387 // implicitConversion = (T_Object << 4) + T_null;
391 switch (runtimeTimeType.id) {
395 implicitConversion = (T_int << 4) + compileTimeType.id;
401 case T_int : //implicitConversion may result in i2i which will result in NO code gen
403 implicitConversion = (runtimeTimeType.id << 4) + compileTimeType.id;
405 default : //nothing on regular object ref
409 public boolean isCompactableOperation() {
414 //Return true if the conversion is done AUTOMATICALLY by the vm
415 //while the javaVM is an int based-machine, thus for example pushing
416 //a byte onto the stack , will automatically creates a int on the stack
417 //(this request some work d be done by the VM on signed numbers)
418 public boolean isConstantValueOfTypeAssignableToType(
419 TypeBinding constantType,
420 TypeBinding targetType) {
422 if (constant == Constant.NotAConstant)
424 if (constantType == targetType)
426 if (constantType.isBaseType() && targetType.isBaseType()) {
427 //No free assignment conversion from anything but to integral ones.
428 if ((constantType == IntBinding
429 || BaseTypeBinding.isWidening(T_int, constantType.id))
430 && (BaseTypeBinding.isNarrowing(targetType.id, T_int))) {
431 //use current explicit conversion in order to get some new value to compare with current one
432 return isConstantValueRepresentable(constant, constantType.id, targetType.id);
438 public boolean isTypeReference() {
442 public void resolve(BlockScope scope) {
443 // drops the returning expression's type whatever the type is.
445 this.resolveType(scope);
449 public TypeBinding resolveType(BlockScope scope) {
450 // by default... subclasses should implement a better TC if required.
455 public TypeBinding resolveType(ClassScope classScope) {
456 // by default... subclasses should implement a better TB if required.
460 public TypeBinding resolveTypeExpecting(
462 TypeBinding expectedType) {
464 TypeBinding expressionType = this.resolveType(scope);
465 if (expressionType == null) return null;
466 if (expressionType == expectedType) return expressionType;
468 if (!expressionType.isCompatibleWith(expectedType)) {
469 scope.problemReporter().typeMismatchError(expressionType, expectedType, this);
472 return expressionType;
475 public StringBuffer print(int indent, StringBuffer output) {
476 printIndent(indent, output);
477 return printExpression(indent, output);
480 public abstract StringBuffer printExpression(int indent, StringBuffer output);
482 public StringBuffer printStatement(int indent, StringBuffer output) {
483 return print(indent, output).append(";"); //$NON-NLS-1$
486 public Expression toTypeReference() {
487 //by default undefined
489 //this method is meanly used by the parser in order to transform
490 //an expression that is used as a type reference in a cast ....
491 //--appreciate the fact that castExpression and ExpressionWithParenthesis
492 //--starts with the same pattern.....