Package org.openquark.cal.internal.javamodel

Examples of org.openquark.cal.internal.javamodel.JavaMethod


        private void createMethod_isLogicalTrueOverrideFalse() {
            int modifiers = Modifier.PUBLIC;
            JavaTypeName returnType = JavaTypeName.BOOLEAN;

            // Add the method to the class.
            JavaMethod javaMethod = new JavaMethod(modifiers, returnType, "isLogicalTrue");
            javaClassRep.addMethod(javaMethod);

            // Add the body..
            // return false;
            javaMethod.addStatement(new ReturnStatement(LiteralWrapper.FALSE));
        }
View Full Code Here


            int modifiers = Modifier.PUBLIC | Modifier.FINAL;
            JavaTypeName returnType = JavaTypeNames.RTVALUE;
            // Add the method to the class.
            String methodName = "get" + javaFieldNames[i];
            JavaMethod javaMethod = new JavaMethod(modifiers, returnType, methodName);
            javaClassRep.addMethod(javaMethod);

            // return a boxed version of the field.
            JavaExpression jf;
            if (fieldStrictness[i] && SCJavaDefn.canTypeBeUnboxed(fieldTypes[i])) {
                jf = new JavaField.Instance (null, javaFieldNames[i], SCJavaDefn.typeExprToTypeName(fieldTypes[i]));
                jf = SCJavaDefn.boxExpression(fieldTypes[i], jf);
                javaMethod.addStatement(new ReturnStatement(jf));
            } else {
                JavaField field = new JavaField.Instance (null, javaFieldNames[i], JavaTypeNames.RTVALUE);
                jf = field;
               if (!fieldStrictness[i]) {
                   // We have a non-strict field of type RTValue.  In order to reduce space usage
                   // we want to update the field value to be the result of the original suspension.
                   // If the field value is an RTResultFunction we want to re-assign the field with
                   // a call to 'getValue()'.
                   // For example:
                   //public final RTValue get_head() {
                   //    RTValue field;
                   //        if ((field = _head) instanceof RTResultFunction) {
                   //            return (_head = field.getValue());
                   //        }
                   //        return field;
                   //}
                   String localName = javaFieldNames[i]+"$";
                   LocalVariable localVariable = new LocalVariable(localName, JavaTypeNames.RTVALUE);
                   LocalVariableDeclaration localDeclaration = new LocalVariableDeclaration(localVariable);
                   javaMethod.addStatement(localDeclaration);
                   Assignment localAssignment = new Assignment(localVariable, field);
                   InstanceOf checkType = new InstanceOf(localAssignment, JavaTypeNames.RTRESULT_FUNCTION);
                   MethodInvocation getValue = new MethodInvocation.Instance(localVariable, "getValue", JavaTypeNames.RTVALUE, MethodInvocation.InvocationType.VIRTUAL);
                   Assignment fieldAssignment = new Assignment(field, getValue);
                   ReturnStatement returnNewVal = new ReturnStatement(fieldAssignment);
                   IfThenElseStatement ifThen = new IfThenElseStatement(checkType, returnNewVal);
                   javaMethod.addStatement(ifThen);
                   javaMethod.addStatement(new ReturnStatement (localVariable));

               } else {
                   // The field is strict and therefore already in WHNF.
                   javaMethod.addStatement(new ReturnStatement(jf));
               }

            }

            // If the field type is primitive and strict we want to generate
            // a method that retrieves the field as an unboxed value.
            if (SCJavaDefn.canTypeBeUnboxed(fieldTypes[i]) && dc.isArgStrict((i))) {

                returnType = SCJavaDefn.typeExprToTypeName(fieldTypes[i]);
                methodName = methodName + "_As_" + SCJavaDefn.getNameForPrimitive(returnType);

                // If all the fields with this name/type combination across all DCs are strict we
                // don't need an RTExecutionContext passed to the getter.
                javaMethod = new JavaMethod (modifiers, returnType, methodName);
                javaClassRep.addMethod(javaMethod);

                // Add the throws declaration
                javaMethod.addThrows(JavaTypeName.CAL_EXECUTOR_EXCEPTION);

                if (fieldStrictness[i]) {
                    jf = new JavaField.Instance (null, javaFieldNames[i], returnType);
                } else {
                    // This is an error.  We should only get to this point if all
                    // instances of this field/type are strict across all DCs.
                    throw new CodeGenerationException ("Attempt to generate unboxed accessor on lazy field " + javaFieldNames[i] + " for DC " + dc.getName());
                }
                javaMethod.addStatement(new ReturnStatement(jf));

            }
        }
View Full Code Here

                String methodName = "getFieldByIndex";
                if (forType != null) {
                    methodName = methodName + "_As_" + nameForType;
                }

                JavaMethod javaMethod =
                    new JavaMethod (modifiers,
                            (forType == null) ? JavaTypeNames.RTVALUE : forType,
                            new String[]{"dcOrdinal", "fieldIndex", "errorInfo"},
                            new JavaTypeName[]{JavaTypeName.INT, JavaTypeName.INT, JavaTypeName.ERRORINFO},
                            null, methodName);

                javaMethod.addThrows(JavaTypeName.CAL_EXECUTOR_EXCEPTION);

                javaClassRep.addMethod (javaMethod);
                javaMethod.addStatement(methodBodyBlock);
            }

        }
View Full Code Here

            // Add the method to the class.
            String methodName = "get" + fieldName + "_As_" + SCJavaDefn.getNameForPrimitive(fieldType);


            JavaMethod javaMethod;
            // Don't need to pass in an execution context as this will always be evaluted.
            javaMethod = new JavaMethod(modifiers, fieldType, methodName);

            // Add the throws declaration
            javaMethod.addThrows(JavaTypeName.CAL_EXECUTOR_EXCEPTION);

            javaClassRep.addMethod(javaMethod);

            if (implementAtThisLevel && isStrict) {
                JavaField field = new JavaField.Instance (null, fieldName, fieldType);
                javaMethod.addStatement(new ReturnStatement (field));
            } else {
                // This class should throw an error for any access.  The methods will
                // be overridden by derived classes for each data constructor.
                // We want to call the RTCons method badFieldAccessor_...
                // that matches the type of the field.

                String fieldTypeString = SCJavaDefn.getNameForPrimitive(fieldType);
                String castTypeString = null;
                if (!(fieldType instanceof JavaTypeName.Primitive)) {
                    castTypeString = fieldTypeString;
                    fieldTypeString = "Object";
                }
                MethodInvocation mi =
                    new MethodInvocation.Instance(
                        null,
                        "badFieldAccessor_" + fieldTypeString,
                        LiteralWrapper.make(fieldName),
                        JavaTypeName.STRING,
                        fieldType,
                        MethodInvocation.InvocationType.VIRTUAL);

                if (castTypeString != null) {
                    javaMethod.addStatement(new ReturnStatement(new JavaExpression.CastExpression(fieldType, mi)));
                } else {
                    javaMethod.addStatement (new ReturnStatement (mi));
                }
            }
        }
View Full Code Here


            // public final int getArity() {return 0;}
            int modifiers = Modifier.PUBLIC | Modifier.FINAL;
            JavaTypeName returnType = JavaTypeName.INT;
            JavaMethod javaMethod = new JavaMethod(modifiers, returnType, "getArity");
            tagDCClassRep.addMethod(javaMethod);
            javaMethod.addStatement(new ReturnStatement(LiteralWrapper.make(Integer.valueOf(0))));

            // public final int getOrdinalValue(){return tag;}
            modifiers = Modifier.PUBLIC | Modifier.FINAL;
            returnType = JavaTypeName.INT;
            javaMethod = new JavaMethod(modifiers, returnType, "getOrdinalValue");
            tagDCClassRep.addMethod(javaMethod);
            javaMethod.addStatement(new ReturnStatement(new JavaField.Instance(null, "tag", JavaTypeName.INT)));

            // public final String getModuleName() ...
            modifiers = Modifier.PUBLIC | Modifier.FINAL;
            returnType = JavaTypeName.STRING;
            javaMethod = new JavaMethod(modifiers, returnType, "getModuleName");
            tagDCClassRep.addMethod(javaMethod);
            javaMethod.addStatement(new ReturnStatement(LiteralWrapper.make (typeConstructor.getName().getModuleName().toSourceText())));

            // public final String getUnqualifiedName() ...
            modifiers = Modifier.PUBLIC | Modifier.FINAL;
            returnType = JavaTypeName.STRING;
            javaMethod = new JavaMethod(modifiers, returnType, "getUnqualifiedName");
            tagDCClassRep.addMethod(javaMethod);
            SwitchStatement sw = new SwitchStatement (new JavaField.Instance(null, "tag", JavaTypeName.INT));
            for (int i = 0, nDCs = dataConsList.size(); i < nDCs; ++i) {
                DataConstructor dc = dataConsList.get (i);
                sw.addCase(new SwitchStatement.IntCaseGroup(dc.getOrdinal(), new ReturnStatement (LiteralWrapper.make(dc.getName().getUnqualifiedName()))));
            }
            javaMethod.addStatement (sw);
            javaMethod.addStatement(new ReturnStatement(LiteralWrapper.make ("Unknown data constructor")));

            // public final String getQualfiedName() ...
            modifiers = Modifier.PUBLIC | Modifier.FINAL;
            returnType = JavaTypeName.STRING;
            javaMethod = new JavaMethod(modifiers, returnType, "getQualifiedName");
            tagDCClassRep.addMethod(javaMethod);
            sw = new SwitchStatement (new JavaField.Instance(null, "tag", JavaTypeName.INT));
            for (int i = 0, nDCs = dataConsList.size(); i < nDCs; ++i) {
                DataConstructor dc = dataConsList.get (i);
                sw.addCase(new SwitchStatement.IntCaseGroup(dc.getOrdinal(), new ReturnStatement (LiteralWrapper.make(dc.getName().getQualifiedName()))));
            }
            javaMethod.addStatement (sw);
            javaMethod.addStatement(new ReturnStatement(LiteralWrapper.make ("Unknown data constructor")));

            return tagDCClassRep;
        }
View Full Code Here

         * Create the method:
         *     protected final String getDCNameByOrdinal(int dcOrdinal)
         * This overrides the implementation in the RTCons base class.
         */
        private void createMethod_getDCNameByOrdinal() {
            JavaMethod javaMethod =
                new JavaMethod(Modifier.PROTECTED | Modifier.FINAL,
                               JavaTypeName.STRING,
                               "dcOrdinal",
                               JavaTypeName.INT,
                               false, "getDCNameByOrdinal");
            javaClassRep.addMethod(javaMethod);

            SwitchStatement sw =
                new SwitchStatement(new MethodVariable("dcOrdinal"));

            for (int i = 0, n = dataConsList.size(); i < n; ++i) {
                DataConstructor dc = dataConsList.get(i);
                SwitchStatement.IntCaseGroup icg =
                    new SwitchStatement.IntCaseGroup(
                            dc.getOrdinal(),
                            new ReturnStatement(LiteralWrapper.make(dc.getName().getUnqualifiedName())));
                sw.addCase(icg);
            }

            javaMethod.addStatement(sw);

            // If the argument doesn't match the ordinal for any DC we
            // throw an error.
            MethodInvocation badValue =
                new MethodInvocation.Static(
                        JavaTypeNames.RTVALUE,
                        "badValue_Object",
                        new JavaExpression[]{LiteralWrapper.NULL, LiteralWrapper.make("Invalid DC ordinal in getDCNameByOrdinal() for " + className.toString())},
                        new JavaTypeName[]{JavaTypeName.ERRORINFO, JavaTypeName.STRING},
                        JavaTypeName.OBJECT);

            javaMethod.addStatement (new ReturnStatement(new CastExpression(JavaTypeName.STRING, badValue)));
        }
View Full Code Here

        private void createMethod_getTagDC() {
            int modifiers = Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC;

            // Add the method to the class.
            JavaTypeName tagDCTypeName = CALToJavaNames.createTypeNameForTagDCFromType(typeConstructor, module);
            JavaMethod javaMethod = new JavaMethod(modifiers, tagDCTypeName, "ordinal", JavaTypeName.INT, false, "getTagDC");
            javaClassRep.addMethod(javaMethod);

            // Add the body..
            // switch (ordinal) {
            //     case 1: ...;
            // }
            SwitchStatement ordSwitch = new SwitchStatement(METHODVAR_ORDINAL);
            for (final DataConstructor dc : dataConsList) {
                if (dc.getArity() == 0) {
                    // Return the static TagDC instance for this ordinal.
                    String fieldName = CALToJavaNames.fixupVarName(dc.getName().getUnqualifiedName());
                    JavaField field = new JavaField.Static(className, fieldName, tagDCTypeName);
                    SwitchStatement.SwitchCase sc = new SwitchStatement.IntCaseGroup(dc.getOrdinal(), new ReturnStatement(field));
                    ordSwitch.addCase(sc);
                } else {
                    // This is a valid ordinal for the data type but does not correspond to a zero arity DC.
                    LiteralWrapper badValueMessageWrapper = LiteralWrapper.make ("Attempt to treat " + dc.getName() + " as a zero arity data constructor.");
                    Block block = new Block();
                    block.addStatement(new JavaStatement.LineComment(dc.getName().getQualifiedName()));
                    JavaExpression castExpression = new CastExpression(tagDCTypeName, new MethodInvocation.Static(JavaTypeNames.RTVALUE, "badValue", badValueMessageWrapper, JavaTypeName.STRING, JavaTypeNames.RTVALUE));
                    block.addStatement(new ReturnStatement(castExpression));
                    ordSwitch.addCase(new SwitchStatement.IntCaseGroup (dc.getOrdinal(), block));
                }
            }

            // Add a default case in the switch to throw an error if an invalid ordinal value is used.
            Block defaultBlock = new Block();
            LocalVariable bf = new LocalVariable("bf", JavaTypeName.STRING_BUILDER);
            defaultBlock.addStatement(new LocalVariableDeclaration (bf, new ClassInstanceCreationExpression(JavaTypeName.STRING_BUILDER)));
            LiteralWrapper badValueMessageWrapper1 = LiteralWrapper.make("Invalid ordinal value of ");
            JavaExpression message = new MethodInvocation.Instance(bf, "append", badValueMessageWrapper1, JavaTypeName.STRING, JavaTypeName.STRING_BUILDER, MethodInvocation.InvocationType.VIRTUAL);
            message = new  MethodInvocation.Instance(message, "append", METHODVAR_ORDINAL, JavaTypeName.INT, JavaTypeName.STRING_BUILDER, MethodInvocation.InvocationType.VIRTUAL);
            LiteralWrapper badValueMessageWrapper2 = LiteralWrapper.make(" in " + className.toString() + ".getTagDC().");
            message = new MethodInvocation.Instance(message, "append", badValueMessageWrapper2, JavaTypeName.STRING, JavaTypeName.STRING_BUILDER, MethodInvocation.InvocationType.VIRTUAL);
            defaultBlock.addStatement (new ExpressionStatement(message));
            message = new MethodInvocation.Instance(bf, "toString", JavaTypeName.STRING, MethodInvocation.InvocationType.VIRTUAL);
            defaultBlock.addStatement (new ReturnStatement(new CastExpression(tagDCTypeName, new MethodInvocation.Static(JavaTypeNames.RTVALUE, "badValue", message, JavaTypeName.STRING, JavaTypeNames.RTVALUE))));
            ordSwitch.addCase(new SwitchStatement.DefaultCase (defaultBlock));

            // Add the switch statement to the method.
            javaMethod.addStatement(ordSwitch);
        }
View Full Code Here

            JavaTypeName fieldType = SCJavaDefn.typeExprToTypeName(fieldTypeExpr);
            boolean primitiveType = !fieldType.equals(JavaTypeNames.RTVALUE);

            // Add the method to the class.
            String methodName = "get" + fieldName;
            JavaMethod javaMethod = new JavaMethod(modifiers, JavaTypeNames.RTVALUE, methodName);
            javaClassRep.addMethod(javaMethod);

            if (implementAtThisLevel) {
                JavaField field = new JavaField.Instance (null, fieldName, fieldType);
                if (primitiveType) {
                    javaMethod.addStatement(new ReturnStatement(SCJavaDefn.boxExpression(fieldTypeExpr, field)));
                } else {
                    if (!fieldIsStrict) {
                        // We have a non-strict field of type RTValue.  In order to reduce space usage
                        // we want to update the field value to be the result of the original suspension.
                        // If the field value is an RTResultFunction we want to re-assign the field with
                        // a call to 'getValue()'.
                        // For example:
                        //public final RTValue get_head() {
                        //    RTValue field;
                        //        if ((field = _head) instanceof RTResultFunction) {
                        //            return (_head = field.getValue());
                        //        }
                        //        return field;
                        //}
                        String localName = fieldName+"$";
                        LocalVariable localVariable = new LocalVariable(localName, fieldType);
                        LocalVariableDeclaration localDeclaration = new LocalVariableDeclaration(localVariable);
                        javaMethod.addStatement(localDeclaration);
                        Assignment localAssignment = new Assignment(localVariable, field);
                        InstanceOf checkType = new InstanceOf(localAssignment, JavaTypeNames.RTRESULT_FUNCTION);
                        MethodInvocation getValue = new MethodInvocation.Instance(localVariable, "getValue", JavaTypeNames.RTVALUE, MethodInvocation.InvocationType.VIRTUAL);
                        Assignment fieldAssignment = new Assignment(field, getValue);
                        ReturnStatement returnNewVal = new ReturnStatement(fieldAssignment);
                        IfThenElseStatement ifThen = new IfThenElseStatement(checkType, returnNewVal);
                        javaMethod.addStatement(ifThen);
                        javaMethod.addStatement(new ReturnStatement (localVariable));
                    } else {
                        javaMethod.addStatement(new ReturnStatement (field));
                    }
                }
            } else {
                // This class should throw an error for any access.  The methods will
                // be overridden by derived classes for each data constructor.
                // We can simply call the method badFieldAccessor() implemented in
                // RTCons.
                MethodInvocation mi =
                    new MethodInvocation.Instance(
                            null,
                            "badFieldAccessor",
                            LiteralWrapper.make(fieldName),
                            JavaTypeName.STRING,
                            JavaTypeNames.RTVALUE,
                            MethodInvocation.InvocationType.VIRTUAL);

                javaMethod.addStatement (new ReturnStatement (mi));
            }
        }
View Full Code Here

            //    } else {
            //        return 3;
            //    }
            //}

            JavaMethod method = new JavaMethod (Modifier.PUBLIC | Modifier.FINAL, JavaTypeName.INT, "debug_getNChildren");

            JavaField resultField = new JavaField.Instance(null, "result", JavaTypeNames.RTVALUE);
            JavaExpression conditionExpr =
                new JavaExpression.OperatorExpression.Binary(
                    JavaOperator.NOT_EQUALS_OBJECT,
                    resultField,
                    LiteralWrapper.NULL)
                    ;

            JavaStatement thenStatement =
                new ReturnStatement(
                    new MethodInvocation.Instance(
                        null,
                        "debug_getNChildren",
                        JavaTypeNames.RTFULLAPP,
                        JavaExpression.EMPTY_JAVA_EXPRESSION_ARRAY,
                        JavaExpression.EMPTY_TYPE_NAME_ARRAY,
                        JavaTypeName.INT,
                        MethodInvocation.InvocationType.SPECIAL));

            JavaStatement elseStatement =
                new ReturnStatement(LiteralWrapper.make(Integer.valueOf(memberFields.length)));

            JavaStatement.IfThenElseStatement ifThenElseStatement =
                new JavaStatement.IfThenElseStatement (conditionExpr, thenStatement, elseStatement);

            method.addStatement(ifThenElseStatement);

            return method;
        }
View Full Code Here

            //    default:
            //        throw new IndexOutOfBoundsException();
            //    }
            //}

            JavaMethod method = new JavaMethod (Modifier.PUBLIC | Modifier.FINAL, JavaTypeName.CAL_VALUE, "childN", JavaTypeName.INT, false, "debug_getChild");

            MethodVariable childNVar = new JavaExpression.MethodVariable("childN");

            {
                //    if (result != null) {
                //        return super.debug_getChild(childN);
                //    }

                JavaField resultField = new JavaField.Instance(null, "result", JavaTypeNames.RTVALUE);
                JavaExpression conditionExpr =
                    new OperatorExpression.Binary(
                        JavaOperator.NOT_EQUALS_OBJECT,
                        resultField,
                        LiteralWrapper.NULL)
                        ;

                JavaStatement thenStatement =
                    new ReturnStatement(
                        new MethodInvocation.Instance(
                            null,
                            "debug_getChild",
                            JavaTypeNames.RTFULLAPP,
                            new JavaExpression[]{childNVar},
                            new JavaTypeName[] {JavaTypeName.INT},
                            JavaTypeName.CAL_VALUE,
                            MethodInvocation.InvocationType.SPECIAL));

                JavaStatement.IfThenElseStatement ifThenStatement =
                    new JavaStatement.IfThenElseStatement (conditionExpr, thenStatement);

                method.addStatement(ifThenStatement);
            }

            SwitchStatement switchStatement =
                new SwitchStatement(childNVar);

            for (int i = 0, nFields = memberFields.length; i < nFields; ++i) {

                JavaField javaField = memberFields[i];
                String javaFieldName = javaField.getFieldName();
                JavaTypeName javaFieldType = javaField.getFieldType();
                JavaExpression javaFieldExpr = new JavaExpression.JavaField.Instance(null, javaFieldName, javaFieldType);
                if (!javaFieldType.equals(JavaTypeNames.RTVALUE)) {
                    javaFieldExpr = SCJavaDefn.boxExpression(javaFieldType, javaFieldExpr);
                }
                switchStatement.addCase(
                    new SwitchStatement.IntCaseGroup(i, new ReturnStatement(javaFieldExpr)));
            }

            switchStatement.addCase(
                new SwitchStatement.DefaultCase(
                    new JavaStatement.ThrowStatement(
                        new JavaExpression.ClassInstanceCreationExpression(JavaTypeName.INDEX_OUT_OF_BOUNDS_EXCEPTION))));

            method.addStatement(switchStatement);

            return method;
        }
View Full Code Here

TOP

Related Classes of org.openquark.cal.internal.javamodel.JavaMethod

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.