Package org.openquark.cal.internal.javamodel.JavaStatement

Examples of org.openquark.cal.internal.javamodel.JavaStatement.ReturnStatement


            // Assign the hash code value to the hashCode field.
            JavaExpression assign = new JavaExpression.Assignment(hashCodeField, result);
            thenBlock.addStatement(new ExpressionStatement (assign));
           
            // Return the initialized hashCode field.
            hashCode.addStatement (new ReturnStatement (hashCodeField));
           
            return hashCode;
        }
View Full Code Here


                        thisField,
                        objectArg);
            JavaStatement.IfThenElseStatement ifThen =
                new IfThenElseStatement(
                        condition,
                        new ReturnStatement(LiteralWrapper.TRUE));
           
            equals.addStatement(ifThen);

            // if (object == null) {
            //     return false;
            // }
            condition =
                new JavaExpression.OperatorExpression.Binary(JavaOperator.EQUALS_OBJECT, objectArg, LiteralWrapper.NULL);
            ifThen =
                new IfThenElseStatement (condition, new ReturnStatement(LiteralWrapper.FALSE));
            equals.addStatement(ifThen);
           
            // if (!(object instanceOf ThisType)) {
            //     return false;
            // }
            condition =
                new JavaExpression.InstanceOf(objectArg, typeName);
            condition =
                new OperatorExpression.Unary(JavaOperator.LOGICAL_NEGATE, condition);
            ifThen =
                new IfThenElseStatement (
                        condition,
                        new ReturnStatement(LiteralWrapper.FALSE));
            equals.addStatement(ifThen);
           
            // ThisType castObject = (ThisType)object;
            LocalVariable localVar =
                new LocalVariable ("cast" + argName, typeName);
           
            JavaStatement localVarDecl =
                new JavaStatement.LocalVariableDeclaration (
                        localVar,
                        new JavaExpression.CastExpression(typeName, objectArg));
            equals.addStatement (localVarDecl);
           

            // Check DC name
            // if (!getDCName().equals(castObject.getDCName())) {
            //     return false;
            // }
            JavaExpression thisGetDCName =
                new MethodInvocation.Instance(null, GET_DC_NAME_METHOD_NAME, JavaTypeName.STRING, MethodInvocation.InvocationType.VIRTUAL);
            JavaExpression otherGetDCName =
                new MethodInvocation.Instance(localVar, GET_DC_NAME_METHOD_NAME, JavaTypeName.STRING, MethodInvocation.InvocationType.VIRTUAL);
            JavaExpression compareDCNames =
                new MethodInvocation.Instance(thisGetDCName, "equals", otherGetDCName, JavaTypeName.OBJECT, JavaTypeName.BOOLEAN, MethodInvocation.InvocationType.VIRTUAL);
            compareDCNames = new OperatorExpression.Unary(JavaOperator.LOGICAL_NEGATE, compareDCNames);

            ifThen = new IfThenElseStatement(compareDCNames, new ReturnStatement(LiteralWrapper.FALSE));
            equals.addStatement(ifThen);
           
            if (fieldNames != null && fieldNames.size() > 0) {
                // Now we need to compare equality for the various fields.
                Iterator<FieldName> fields = fieldNames.iterator();
                FieldName fn = fields.next ();
                JavaTypeName fieldType = (JavaTypeName)fieldNameToType.get(fn);
                JavaExpression compare = makeFieldComparison (fn, fieldType, localVar);
               
                while (fields.hasNext()) {
                    fn = fields.next ();
                    fieldType = (JavaTypeName)fieldNameToType.get(fn);
                    JavaExpression nextCompare = makeFieldComparison (fn, fieldType, localVar);
                   
                    compare =
                        new OperatorExpression.Binary (JavaOperator.CONDITIONAL_AND, compare, nextCompare);
                }
                equals.addStatement(new ReturnStatement(compare));
            } else {
                equals.addStatement(new ReturnStatement(LiteralWrapper.TRUE));
            }
           
            return equals;
        }
View Full Code Here

                            JavaOperator.STRING_CONCATENATION,
                            message,
                            LiteralWrapper.make("\n"));
            }
           
            toString.addStatement (new ReturnStatement (message));
           
            return toString;
        }
View Full Code Here

           
            private final JavaMethod createMethod_getDCOrdinal(int ordinal) {
               
                JavaMethod javaMethod = new JavaMethod(Modifier.PUBLIC, JavaTypeName.INT, GET_DC_ORDINAL_METHOD_NAME);
                // return dc ordinal.
                javaMethod.addStatement (new ReturnStatement(LiteralWrapper.make (new Integer (ordinal))));

                JavaDocComment jdc =
                    new JavaDocComment ("@return the ordinal of this instance of " + typeConstructorInfo.javaClassName);
                javaMethod.setJavaDocComment(jdc);
View Full Code Here

           
            private final JavaMethod createMethod_getDCName(String dcName) {
                int modifiers = Modifier.PUBLIC | Modifier.FINAL;
                JavaMethod javaMethod = new JavaMethod(modifiers, JavaTypeName.STRING, GET_DC_NAME_METHOD_NAME);
               
                javaMethod.addStatement(new ReturnStatement(LiteralWrapper.make(dcName)));

                JavaDocComment jdc =
                    new JavaDocComment ("@return the name of the data constructor corresponding to this instance of " + typeConstructorInfo.javaClassName);
                javaMethod.setJavaDocComment(jdc);
View Full Code Here

                }

                MethodInvocation eval = createInvocation(result, SCJavaDefn.EVALUATE, SCJavaDefn.EXECUTION_CONTEXT_VAR);
                JavaExpression unbox = SCJavaDefn.unboxValue(SCJavaDefn.typeExprToTypeName(resultType), eval);

                body.addStatement(new ReturnStatement(unbox));

                unboxedBodyCode = body;
            }

        }
View Full Code Here

    private JavaStatement generateReturn(ExpressionContextPair pair, VariableContext context) {
        Block contextBlock = pair.getContextBlock();
        JavaExpression javaExpression = pair.getJavaExpression();

        if (javaExpression != null) {
            ReturnStatement ret = new ReturnStatement(javaExpression);

            if (context != null) {
                Set<VarInfo> vars = new HashSet<VarInfo>();
                Map<QualifiedName, VarInfo> scope = context.getCurrentScope();
                for (final Map.Entry<QualifiedName, VarInfo> entry : scope.entrySet()) {
View Full Code Here

     * @param javaExpression
     * @param context - current variable context.  May be null.
     * @return the return statement
     */
    private JavaStatement generateReturn(JavaExpression javaExpression, VariableContext context) {
        ReturnStatement ret = new ReturnStatement(javaExpression);

        if (context != null) {
            Set<VarInfo> vars = new HashSet<VarInfo>();
            Map<QualifiedName, VarInfo> scope = context.getCurrentScope();
            for (final Map.Entry<QualifiedName, VarInfo> entry : scope.entrySet()) {
View Full Code Here

            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

            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

TOP

Related Classes of org.openquark.cal.internal.javamodel.JavaStatement.ReturnStatement

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.