Package org.openquark.cal.compiler

Examples of org.openquark.cal.compiler.QualifiedName


        if (basicOpExpressions != null) {
            if (basicOpExpressions.getPrimitiveOp() == PrimOps.PRIMOP_EAGER) {
                return true;
            }

            QualifiedName opName = basicOpExpressions.getName();
            MachineFunction mf = module.getFunction(opName);
            if (mf != null && mf.canFunctionBeEagerlyEvaluated()) {
                int nArgs = basicOpExpressions.getNArguments();
                int nWHNFArgs = 0;
                for (int i = 0; i < nArgs; ++i) {
                    Expression eArg = basicOpExpressions.getArgument(i);
                    if (canIgnoreLaziness(eArg, variableContext)) {
                        nWHNFArgs++;
                    }
                }
                if (nArgs == nWHNFArgs) {

                    String unqualifiedOpName = opName.getUnqualifiedName();
                    if (opName.getModuleName().equals(CAL_Prelude.MODULE_NAME) &&
                        (unqualifiedOpName.equals(CAL_Prelude_internal.Functions.divideLong.getUnqualifiedName()) ||
                         unqualifiedOpName.equals(CAL_Prelude_internal.Functions.remainderLong.getUnqualifiedName()) ||
                         unqualifiedOpName.equals(CAL_Prelude_internal.Functions.divideInt.getUnqualifiedName()) ||
                         unqualifiedOpName.equals(CAL_Prelude_internal.Functions.remainderInt.getUnqualifiedName()) ||
                         unqualifiedOpName.equals(CAL_Prelude_internal.Functions.divideShort.getUnqualifiedName()) ||
View Full Code Here


        String baseRecordPatternVarName = recordCaseExpr.getBaseRecordPatternVarName();
        if (baseRecordPatternVarName != null &&
            !baseRecordPatternVarName.equals(Expression.RecordCase.WILDCARD_VAR)) {

            QualifiedName qn = QualifiedName.make(currentModuleName, baseRecordPatternVarName);
            VarInfo.RecordField varInfo = variableContext.addRecordField(qn, null);
            String javaBaseRecordPatternVarName = varInfo.getJavaName();
            LocalName lazyRef = new LocalName(varInfo.getJavaName(), JavaTypeNames.RTVALUE);
            varInfo.updateLazyReference(lazyRef);
            varInfo.updateStrictReference(SCJavaDefn.createInvocation(lazyRef, SCJavaDefn.EVALUATE, SCJavaDefn.EXECUTION_CONTEXT_VAR));

            //generate the Java code:
            //(in the case of both ordinal and textual field names
            //javaBaseRecordPatternVarName = conditionVar.makeMixedRecordRetraction(new int[] {ordinalFieldName1, ..., ordinalFieldNameN},
            //  new String[] {textualFieldName1, ..., textualFieldNameN}

            LocalVariable baseRecordPatternVar = new LocalVariable(javaBaseRecordPatternVarName, JavaTypeNames.RTRECORD_VALUE);

            JavaExpression javaExtractBaseRecordExpr;

            Expression.RecordCase.FieldData fieldData = recordCaseExpr.getBindingFieldsData();
            //todoBI there could be some more optimizations here to handle the cases
            //a. where the ordinal names are in tuple form, then only the tuple size needs to be passed.
            //b. where only a single ordinal or single textual field is being retracted, then we don't
            //   need to form the array.
            //Note however, that if the record pattern var is not used in the expression on the right hand side
            //of the ->, then it will not be created (earlier analysis replaces it by a _), so in fact this
            //code is not called that often anyways.
            int[] retractedOrdinalFields = fieldData.getOrdinalNames();
            String[] retractedTextualFields = fieldData.getTextualNames();

            if (retractedOrdinalFields.length > 0) {

                if (retractedTextualFields.length > 0) {

                    if (fieldData.hasTupleOrdinalPart()) {

                        javaExtractBaseRecordExpr = new MethodInvocation.Instance(
                                conditionVar,
                            "makeTupleMixedRecordRetraction",
                            new JavaExpression[] {
                                LiteralWrapper.make(Integer.valueOf(retractedOrdinalFields.length)),
                                createTextualNamesArray(retractedTextualFields)},
                            new JavaTypeName[] {JavaTypeName.INT, JavaTypeName.STRING_ARRAY},
                            JavaTypeNames.RTRECORD_VALUE,
                            InvocationType.VIRTUAL);
                    } else {

                        javaExtractBaseRecordExpr = new MethodInvocation.Instance(
                                conditionVar,
                            "makeMixedRecordRetraction",
                            new JavaExpression[] {
                                createOrdinalNamesArray(retractedOrdinalFields),
                                createTextualNamesArray(retractedTextualFields)},
                            new JavaTypeName[] {JavaTypeName.INT_ARRAY, JavaTypeName.STRING_ARRAY},
                            JavaTypeNames.RTRECORD_VALUE,
                            InvocationType.VIRTUAL);
                    }
                } else {

                    if (fieldData.hasTupleOrdinalPart()) {

                        javaExtractBaseRecordExpr = new MethodInvocation.Instance(
                                conditionVar,
                            "makeTupleRecordRetraction",
                            new JavaExpression[] {
                                LiteralWrapper.make(Integer.valueOf(retractedOrdinalFields.length))},
                            new JavaTypeName[] {JavaTypeName.INT},
                            JavaTypeNames.RTRECORD_VALUE,
                            InvocationType.VIRTUAL);
                    } else {

                        javaExtractBaseRecordExpr = new MethodInvocation.Instance(
                                conditionVar,
                            "makeOrdinalRecordRetraction",
                            new JavaExpression[] {
                                createOrdinalNamesArray(retractedOrdinalFields)},
                            new JavaTypeName[] {JavaTypeName.INT_ARRAY},
                            JavaTypeNames.RTRECORD_VALUE,
                            InvocationType.VIRTUAL);
                    }
                }

            } else if (retractedTextualFields.length > 0) {

                    javaExtractBaseRecordExpr = new MethodInvocation.Instance(
                            conditionVar,
                        "makeTextualRecordRetraction",
                        new JavaExpression[] {
                            createTextualNamesArray(retractedTextualFields)},
                        new JavaTypeName[] {JavaTypeName.STRING_ARRAY},
                        JavaTypeNames.RTRECORD_VALUE,
                        InvocationType.VIRTUAL);

            } else {
                javaExtractBaseRecordExpr = conditionVar;
            }

            LocalVariableDeclaration extractBaseRecordDeclaration =
                new LocalVariableDeclaration(baseRecordPatternVar, javaExtractBaseRecordExpr);
            recordCaseBlock.addStatement(extractBaseRecordDeclaration);
        }

        for (final Map.Entry<FieldName, String> entry : fieldBindingVarMap.entrySet()) {

            FieldName fieldName = entry.getKey();
            String bindingVarName = entry.getValue();

            //ignore anonymous pattern variables. These are guaranteed not to be used
            //by the result expression, and so don't need to be extracted from the condition record.
            if (!bindingVarName.equals(Expression.RecordCase.WILDCARD_VAR)) {

                QualifiedName qn = QualifiedName.make(currentModuleName, bindingVarName);
                VarInfo.RecordField varInfo = variableContext.addRecordField(qn, null);
                String javaBindingVarName = varInfo.getJavaName();
                LocalName lazyRef = new LocalName(varInfo.getJavaName(), JavaTypeNames.RTVALUE);
                varInfo.updateLazyReference(lazyRef);
                varInfo.updateStrictReference(SCJavaDefn.createInvocation(lazyRef, SCJavaDefn.EVALUATE, SCJavaDefn.EXECUTION_CONTEXT_VAR));
View Full Code Here

     * @throws CodeGenerationException
     */
    private ExpressionContextPair compileLiteralList (ConstructorOpTuple constructorOpExpressions, Scheme scheme, VariableContext variableContext) throws CodeGenerationException {
        // Because of the recursive nature of the List data structur we handle literal lists as a special case.
        DataConstructor dc = constructorOpExpressions.getDataConstructor ();
        QualifiedName qn = dc.getName();
        if (!qn.equals(CAL_Prelude.DataConstructors.Cons)) {
            return null;
        }

        JavaTypeName dcTypeName = CALToJavaNames.createTypeNameFromDC(dc, module);

View Full Code Here

            if (codeGenerationStats != null) {
                codeGenerationStats.incrementLetRecCount(defns.length);
            }

            Expression.Let.LetDefn def = defns [i];
            QualifiedName qn = QualifiedName.make(currentModuleName, def.getVar());
            letVars[i] = variableContext.addLetRecVar(qn, def.getVarType());
            LocalName lazyRef = new LocalName(letVars[i].getJavaName(), JavaTypeNames.RTVALUE);
            letVars[i].updateLazyReference(lazyRef);
            letVars[i].updateStrictReference(SCJavaDefn.createInvocation(lazyRef, SCJavaDefn.EVALUATE, SCJavaDefn.EXECUTION_CONTEXT_VAR));
        }
View Full Code Here

        // a CAL source file.
        if (errorInfo == null) {
            return LiteralWrapper.NULL;
        }

        QualifiedName topLevelFunctionName = errorInfo.getTopLevelFunctionName();
        int line = errorInfo.getLine();
        int column = errorInfo.getColumn();

        // '.' in the module name gets mapped to '_'
        // '_' in the module name gets mapped to "__"
        String errorVarName = topLevelFunctionName.getModuleName().toSourceText().replaceAll("_", "__").replace('.', '_') + "_" + topLevelFunctionName.getUnqualifiedName() + "_" + line + "_" + column;

        JavaExpression newErrorInfo = sharedValues.getStaticError(errorVarName);

        if (newErrorInfo == null) {
            // Initialize newErrorInfo
            JavaExpression args[] = new JavaExpression[4];
            args[0] = LiteralWrapper.make (topLevelFunctionName.getModuleName().toSourceText());
            args[1] = LiteralWrapper.make (topLevelFunctionName.getUnqualifiedName());
            args[2] = LiteralWrapper.make( Integer.valueOf(line) );
            args[3] = LiteralWrapper.make( Integer.valueOf(column) );

            JavaTypeName paramTypes[] = new JavaTypeName[4];
            paramTypes[0] = JavaTypeName.STRING;
View Full Code Here

    @Override
    protected List<ValueNode> getValueList() {

        // Add values to the list.
        TypeExpr typeExpr = getValueNode().getTypeExpr();
        QualifiedName typeConstructorName = ((TypeConsApp)typeExpr).getName();
        DataConstructor[] dcList = valueEditorManager.getPerspective().getDataConstructorsForType(typeConstructorName);

        List<ValueNode> valueList = new ArrayList<ValueNode>();
        for (final DataConstructor dataConstructor : dcList) {
            ValueNode value = valueEditorManager.getValueNodeBuilderHelper().buildValueNode(null, dataConstructor, typeExpr);
View Full Code Here

     * @param loadStatus a Status object for storing the status of the load operation
     * @return The GemDesign object loaded from the given file
     */
    public static GemDesign loadGemDesign(File designFile, Status loadStatus) {
   
        QualifiedName gemName = getDesignName(designFile);
        if (gemName == null) {
            loadStatus.add(new Status(Status.Severity.ERROR, "Cannot determine the name of the gem described by the design.", null));
        }
       
        try {
View Full Code Here

     */
    public static GemDesign loadGemDesign(WorkspaceResource gemDefinition, Status loadStatus) {
        InputStream inputStream = null;
        try {
            // Get the name of the gem.
            QualifiedName gemName = QualifiedName.makeFromCompoundName(gemDefinition.getIdentifier().getFeatureName().getName());
           
            // Get the input stream on the gem definition.
            inputStream = gemDefinition.getInputStream(loadStatus);
            if (inputStream == null) {
                return null;
View Full Code Here

                        for (final AltVarIndexPair altVarIndexPair : getAltVarIndexList(alt, firstDC)) {

                            String altVar = altVarIndexPair.getAltVar();
                            int fieldIndex = altVarIndexPair.getIndex();

                            QualifiedName qn = QualifiedName.make(currentModuleName, altVar);
                            VarInfo.DCMember vi = variableContext.addDCField(qn, fieldTypes[fieldIndex]);

                            boolean fieldIsStrict =
                                !LECCMachineConfiguration.IGNORE_STRICTNESS_ANNOTATIONS;

                            for (final DataConstructor tagDC : group) {
                                fieldIsStrict = fieldIsStrict && tagDC.isArgStrict(fieldIndex);
                            }

                            if (fieldIsStrict) {
                                vi.setEvaluated(true);
                            }

                            String fieldName = SCJavaDefn.getJavaFieldNameFromDC(firstDC, fieldIndex);
                            String fieldGetterName = "get" + fieldName;

                            // Generate the code defining the variable.
                            if (fieldIsStrict) {
                                if (SCJavaDefn.canTypeBeUnboxed(fieldTypes[fieldIndex])) {
                                    // This is a strict field of a primitive type so has both a boxed and unboxed form.
                                    JavaExpression unboxedInitializer =
                                        new JavaExpression.MethodInvocation.Instance(caseVar,
                                                                                     fieldGetterName + "_As_" + SCJavaDefn.getNameForPrimitive(fieldTypes[fieldIndex]),
                                                                                     SCJavaDefn.typeExprToTypeName(fieldTypes[fieldIndex]),
                                                                                     JavaExpression.MethodInvocation.InvocationType.VIRTUAL);

                                    vi.updateUnboxedVarDef(unboxedInitializer);
                                    JavaExpression localVar = new LocalVariable(vi.getJavaName()+"$U", vi.getUnboxedType());
                                    vi.updateUnboxedReference(localVar);
                                    JavaExpression boxedDef = SCJavaDefn.boxExpression(vi.getUnboxedType(), localVar);
                                    vi.updateStrictReference(boxedDef);
                                    vi.updateLazyReference(boxedDef);
                                } else {
                                    // RTValue altVarName = ((DCClass)caseVar).getFieldn();
                                    JavaExpression initializer = new JavaExpression.MethodInvocation.Instance(caseVar, fieldGetterName, JavaTypeNames.RTVALUE, JavaExpression.MethodInvocation.InvocationType.VIRTUAL);
                                    vi.updateStrictVarDef (initializer);
                                    JavaExpression localVar = new LocalVariable(vi.getJavaName(), JavaTypeNames.RTVALUE);
                                    vi.updateStrictReference(localVar);
                                    vi.updateLazyReference(localVar);
                                }
                            } else {
                                // RTValue altVarName = ((DCClass)caseVar).getFieldn();
                                JavaExpression initializer = new JavaExpression.MethodInvocation.Instance(caseVar, fieldGetterName, JavaTypeNames.RTVALUE, JavaExpression.MethodInvocation.InvocationType.VIRTUAL);
                                vi.updateLazyVarDef (initializer);
                                JavaExpression localVar = new LocalVariable(vi.getJavaName(), JavaTypeNames.RTVALUE);
                                vi.updateLazyReference(localVar);

                                JavaExpression evaluatedVar = SCJavaDefn.createInvocation(localVar, SCJavaDefn.EVALUATE, SCJavaDefn.EXECUTION_CONTEXT_VAR);
                                vi.updateStrictReference(evaluatedVar);
                                if (SCJavaDefn.canTypeBeUnboxed(fieldTypes[fieldIndex])) {
                                    vi.updateUnboxedReference(SCJavaDefn.unboxValue(vi.getUnboxedType(), evaluatedVar));
                                }
                            }
                        }

                        // Compile the actual body of the alternate.
                        JavaStatement altStatement = genS_R(alt.getAltExpr(), variableContext);
                        caseBlock.addStatement(variableContext.popJavaScope());
                        caseBlock.addStatement(altStatement);

                        switchStatement.addCase(new SwitchStatement.IntCaseGroup(caseLabels, caseBlock));
                    }
                } else {
                    // Matching notation for switch alternate.
                    Map<FieldName, String> fieldNameToVarNameMap = ((SwitchAlt.Matching)alt).getFieldNameToVarNameMap();

                    Block caseBlock = new Block();

                    int[] caseLabels = new int[altTags.size()];
                    int index = 0;
                    DataConstructor firstDC = null;
                    // Must be a data constructor tag, since there are field names (see Expression.Switch.SwitchAlt).
                    for (final Object altTag : altTags) {
                        DataConstructor tagDC = (DataConstructor)altTag;
                        if (firstDC == null) {
                            firstDC = tagDC;
                        } else if (tagDC.getOrdinal() < firstDC.getOrdinal()) {
                            firstDC = tagDC;
                        }
                        caseBlock.addStatement(new LineComment(tagDC.getName().getQualifiedName()));
                        caseLabels[index] = tagDC.getOrdinal();
                        index++;
                    }

                    caseBlock.addStatement(new LineComment("Decompose data type to access members."));

                    // Create a new child variable scope to handle the alternate and any let variables it contains.
                    variableContext.pushJavaScope();

                    for (int iField = 0; iField < firstDC.getArity(); ++iField) {
                        FieldName fn = firstDC.getNthFieldName(iField);
                        String altVar = fieldNameToVarNameMap.get(fn);
                        if (altVar == null) {
                            continue;
                        }

                        QualifiedName qn = QualifiedName.make(currentModuleName, altVar);
                        TypeExpr fieldType = SCJavaDefn.getFieldTypeForDC(firstDC, fn);

                        VarInfo.DCMember vi = variableContext.addDCField(qn, fieldType);

                        boolean fieldIsStrict = !LECCMachineConfiguration.IGNORE_STRICTNESS_ANNOTATIONS;
View Full Code Here

                int fieldIndex = altVarIndexPair.getIndex();

                String fieldGetterName = "get" + SCJavaDefn.getJavaFieldNameFromDC(tagDC, fieldIndex);

                // Build in place representation of this variable mapped to its name
                QualifiedName qn = QualifiedName.make(currentModuleName, altVar);
                VarInfo.DCMember vi = variableContext.addDCField(qn, fieldTypes[fieldIndex]);

                boolean fieldIsStrict =
                    !LECCMachineConfiguration.IGNORE_STRICTNESS_ANNOTATIONS && tagDC.isArgStrict(fieldIndex);
View Full Code Here

TOP

Related Classes of org.openquark.cal.compiler.QualifiedName

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.