/** Code generation for a array allocation expression */ public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) { int pc = codeStream.position; if (initializer != null) { initializer.generateCode(currentScope, codeStream, valueRequired); return; } int nonNullDimensionsLength = 0; for (int i = 0, max = dimensions.length; i < max; i++) if (dimensions[i] != null) { dimensions[i].generateCode(currentScope, codeStream, true); nonNullDimensionsLength++; } // Generate a sequence of bytecodes corresponding to an array allocation if (this.resolvedType.dimensions() == 1) { // Mono-dimensional array codeStream.newArray((ArrayBinding) this.resolvedType); } else { // Multi-dimensional array codeStream.multianewarray(this.resolvedType, nonNullDimensionsLength); } if (valueRequired) { codeStream.generateImplicitConversion(implicitConversion); } else { codeStream.pop(); } codeStream.recordPositionsFrom(pc, this.sourceStart); }
/** Generate invocation arguments, considering varargs methods */ public void generateArguments( MethodBinding binding, Expression[] arguments, BlockScope currentScope, CodeStream codeStream) { if (binding.isVarargs()) { // 5 possibilities exist for a call to the vararg method foo(int i, int ... value) : // foo(1), foo(1, null), foo(1, 2), foo(1, 2, 3, 4) & foo(1, new int[] {1, 2}) TypeBinding[] params = binding.parameters; int paramLength = params.length; int varArgIndex = paramLength - 1; for (int i = 0; i < varArgIndex; i++) { arguments[i].generateCode(currentScope, codeStream, true); } ArrayBinding varArgsType = (ArrayBinding) params[varArgIndex]; // parameterType has to be an array type ArrayBinding codeGenVarArgsType = (ArrayBinding) binding.parameters[varArgIndex].erasure(); int elementsTypeID = varArgsType.elementsType().id; int argLength = arguments == null ? 0 : arguments.length; if (argLength > paramLength) { // right number but not directly compatible or too many arguments - wrap extra into array // called with (argLength - lastIndex) elements : foo(1, 2) or foo(1, 2, 3, 4) // need to gen elements into an array, then gen each remaining element into created array codeStream.generateInlinedValue(argLength - varArgIndex); codeStream.newArray(null, codeGenVarArgsType); // create a mono-dimensional array for (int i = varArgIndex; i < argLength; i++) { codeStream.dup(); codeStream.generateInlinedValue(i - varArgIndex); arguments[i].generateCode(currentScope, codeStream, true); codeStream.arrayAtPut(elementsTypeID, false); } } else if (argLength == paramLength) { // right number of arguments - could be inexact - pass argument as is TypeBinding lastType = arguments[varArgIndex].resolvedType; if (lastType == TypeBinding.NULL || (varArgsType.dimensions() == lastType.dimensions() && lastType.isCompatibleWith(varArgsType))) { // foo(1, new int[]{2, 3}) or foo(1, null) --> last arg is passed as-is arguments[varArgIndex].generateCode(currentScope, codeStream, true); } else { // right number but not directly compatible or too many arguments - wrap extra into array // need to gen elements into an array, then gen each remaining element into created array codeStream.generateInlinedValue(1); codeStream.newArray(null, codeGenVarArgsType); // create a mono-dimensional array codeStream.dup(); codeStream.generateInlinedValue(0); arguments[varArgIndex].generateCode(currentScope, codeStream, true); codeStream.arrayAtPut(elementsTypeID, false); } } else { // not enough arguments - pass extra empty array // scenario: foo(1) --> foo(1, new int[0]) // generate code for an empty array of parameterType codeStream.generateInlinedValue(0); codeStream.newArray(null, codeGenVarArgsType); // create a mono-dimensional array } } else if (arguments != null) { // standard generation for method arguments for (int i = 0, max = arguments.length; i < max; i++) arguments[i].generateCode(currentScope, codeStream, true); } }
/** Code generation for a array initializer */ public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) { // Flatten the values and compute the dimensions, by iterating in depth into nested array // initializers int pc = codeStream.position; int expressionLength = (this.expressions == null) ? 0 : this.expressions.length; codeStream.generateInlinedValue(expressionLength); codeStream.newArray(this.binding); if (this.expressions != null) { // binding is an ArrayType, so I can just deal with the dimension int elementsTypeID = this.binding.dimensions > 1 ? -1 : this.binding.leafComponentType.id; for (int i = 0; i < expressionLength; i++) { Expression expr; if ((expr = this.expressions[i]).constant != Constant.NotAConstant) { switch (elementsTypeID) { // filter out initializations to default values case T_int: case T_short: case T_byte: case T_char: case T_long: if (expr.constant.longValue() != 0) { codeStream.dup(); codeStream.generateInlinedValue(i); expr.generateCode(currentScope, codeStream, true); codeStream.arrayAtPut(elementsTypeID, false); } break; case T_float: case T_double: double constantValue = expr.constant.doubleValue(); if (constantValue == -0.0 || constantValue != 0) { codeStream.dup(); codeStream.generateInlinedValue(i); expr.generateCode(currentScope, codeStream, true); codeStream.arrayAtPut(elementsTypeID, false); } break; case T_boolean: if (expr.constant.booleanValue() != false) { codeStream.dup(); codeStream.generateInlinedValue(i); expr.generateCode(currentScope, codeStream, true); codeStream.arrayAtPut(elementsTypeID, false); } break; default: if (!(expr instanceof NullLiteral)) { codeStream.dup(); codeStream.generateInlinedValue(i); expr.generateCode(currentScope, codeStream, true); codeStream.arrayAtPut(elementsTypeID, false); } } } else if (!(expr instanceof NullLiteral)) { codeStream.dup(); codeStream.generateInlinedValue(i); expr.generateCode(currentScope, codeStream, true); codeStream.arrayAtPut(elementsTypeID, false); } } } if (valueRequired) { codeStream.generateImplicitConversion(this.implicitConversion); } else { codeStream.pop(); } codeStream.recordPositionsFrom(pc, this.sourceStart); }
/** * MessageSend code generation * * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream * @param valueRequired boolean */ public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) { int pc = codeStream.position; MethodBinding codegenBinding = this.binding.original(); if (codegenBinding.canBeSeenBy(this.actualReceiverType, this, currentScope)) { // generate receiver/enclosing instance access boolean isStatic = codegenBinding.isStatic(); // outer access ? if (!isStatic && ((this.bits & DepthMASK) != 0)) { // outer method can be reached through emulation ReferenceBinding targetType = currentScope .enclosingSourceType() .enclosingTypeAt((this.bits & DepthMASK) >> DepthSHIFT); Object[] path = currentScope.getEmulationPath( targetType, true /*only exact match*/, false /*consider enclosing arg*/); if (path == null) { // emulation was not possible (should not happen per construction) currentScope.problemReporter().needImplementation(this); } else { codeStream.generateOuterAccess(path, this, targetType, currentScope); } } else { this.receiver.generateCode(currentScope, codeStream, !isStatic); if ((this.bits & NeedReceiverGenericCast) != 0) { codeStream.checkcast(this.actualReceiverType); } codeStream.recordPositionsFrom(pc, this.sourceStart); } // generate arguments generateArguments(this.binding, this.arguments, currentScope, codeStream); // actual message invocation TypeBinding constantPoolDeclaringClass = CodeStream.getConstantPoolDeclaringClass( currentScope, codegenBinding, this.actualReceiverType, this.receiver.isImplicitThis()); if (isStatic) { codeStream.invoke(Opcodes.OPC_invokestatic, codegenBinding, constantPoolDeclaringClass); } else if ((this.receiver.isSuper()) || codegenBinding.isPrivate()) { codeStream.invoke(Opcodes.OPC_invokespecial, codegenBinding, constantPoolDeclaringClass); } else { if (constantPoolDeclaringClass.isInterface()) { // interface or annotation type codeStream.invoke( Opcodes.OPC_invokeinterface, codegenBinding, constantPoolDeclaringClass); } else { codeStream.invoke(Opcodes.OPC_invokevirtual, codegenBinding, constantPoolDeclaringClass); } } } else { codeStream.generateEmulationForMethod(currentScope, codegenBinding); // generate receiver/enclosing instance access boolean isStatic = codegenBinding.isStatic(); // outer access ? if (!isStatic && ((this.bits & DepthMASK) != 0)) { // not supported yet currentScope.problemReporter().needImplementation(this); } else { this.receiver.generateCode(currentScope, codeStream, !isStatic); if ((this.bits & NeedReceiverGenericCast) != 0) { codeStream.checkcast(this.actualReceiverType); } codeStream.recordPositionsFrom(pc, this.sourceStart); } if (isStatic) { // we need an object on the stack which is ignored for the method invocation codeStream.aconst_null(); } // generate arguments if (this.arguments != null) { int argsLength = this.arguments.length; codeStream.generateInlinedValue(argsLength); codeStream.newArray( currentScope.createArrayType( currentScope.getType(TypeConstants.JAVA_LANG_OBJECT, 3), 1)); codeStream.dup(); for (int i = 0; i < argsLength; i++) { codeStream.generateInlinedValue(i); this.arguments[i].generateCode(currentScope, codeStream, true); TypeBinding parameterBinding = codegenBinding.parameters[i]; if (parameterBinding.isBaseType() && parameterBinding != TypeBinding.NULL) { codeStream.generateBoxingConversion(codegenBinding.parameters[i].id); } codeStream.aastore(); if (i < argsLength - 1) { codeStream.dup(); } } } else { codeStream.generateInlinedValue(0); codeStream.newArray( currentScope.createArrayType( currentScope.getType(TypeConstants.JAVA_LANG_OBJECT, 3), 1)); } codeStream.invokeJavaLangReflectMethodInvoke(); // convert the return value to the appropriate type for primitive types if (codegenBinding.returnType.isBaseType()) { int typeID = codegenBinding.returnType.id; if (typeID == T_void) { // remove the null from the stack codeStream.pop(); } codeStream.checkcast(typeID); codeStream.getBaseTypeValue(typeID); } else { codeStream.checkcast(codegenBinding.returnType); } } // required cast must occur even if no value is required if (this.valueCast != null) codeStream.checkcast(this.valueCast); if (valueRequired) { // implicit conversion if necessary codeStream.generateImplicitConversion(this.implicitConversion); } else { boolean isUnboxing = (this.implicitConversion & TypeIds.UNBOXING) != 0; // conversion only generated if unboxing if (isUnboxing) codeStream.generateImplicitConversion(this.implicitConversion); switch (isUnboxing ? postConversionType(currentScope).id : codegenBinding.returnType.id) { case T_long: case T_double: codeStream.pop2(); break; case T_void: break; default: codeStream.pop(); } } codeStream.recordPositionsFrom( pc, (int) (this.nameSourcePosition >>> 32)); // highlight selector }