Search in sources :

Example 6 with VariableDeclarationNode

use of st.gravel.support.compiler.ast.VariableDeclarationNode in project gravel by gravel-st.

the class JVMClassCompiler method compileClassNode.

public JVMClass compileClassNode() {
    final MethodNode[] _allMethods;
    Reference _superclassReference;
    BoundVariableDeclarationNode[] _allInstVars;
    final MethodNode[] _localLinkedMethods;
    ClassDescriptionNode _scn;
    if (_ownerType == null) {
        _ownerType = ((JVMDefinedObjectType) _selfType);
    }
    _allMethods = _classDescriptionNode.methods();
    _superclassReference = _classDescriptionNode.superclassReference();
    _allInstVars = _classDescriptionNode.boundInstVars();
    boolean _temp1 = false;
    while (!_temp1) {
        _temp1 = _superclassReference == null;
        if (!_temp1) {
            _scn = _systemNode.classNodeAt_(_superclassReference);
            _superclassReference = _scn.superclassReference();
            _allInstVars = st.gravel.support.jvm.ArrayExtensions.copyWithAll_(_scn.boundInstVars(), _allInstVars);
        }
    }
    _localLinkedMethods = _systemMappingUpdater.localLinkMethods_instVars_ownerReference_(_allMethods, _allInstVars, _classDescriptionNode.reference());
    for (final MethodNode _each : _localLinkedMethods) {
        JVMClassCompiler.this.compileMethod_(_each);
    }
    for (final VariableDeclarationNode _each : _classDescriptionNode.instVars()) {
        _fields.add(JVMField.factory.ownerType_varName_type_isStatic_(_ownerType, _each.name(), TypeNodeToJVMTypeConverter.factory.namespace_(JVMClassCompiler.this.namespace()).visit_(_each.type()), false));
    }
    this.compileBlocks();
    return this.createContainerClass();
}
Also used : MethodNode(st.gravel.support.compiler.ast.MethodNode) VariableDeclarationNode(st.gravel.support.compiler.ast.VariableDeclarationNode) BoundVariableDeclarationNode(st.gravel.support.compiler.ast.BoundVariableDeclarationNode) Reference(st.gravel.support.compiler.ast.Reference) JVMDefinedObjectType(st.gravel.support.compiler.jvm.JVMDefinedObjectType) BoundVariableDeclarationNode(st.gravel.support.compiler.ast.BoundVariableDeclarationNode) ClassDescriptionNode(st.gravel.support.compiler.ast.ClassDescriptionNode)

Example 7 with VariableDeclarationNode

use of st.gravel.support.compiler.ast.VariableDeclarationNode in project gravel by gravel-st.

the class JVMMethodCompiler method producePrimitive_def_.

public JVMMethodCompiler producePrimitive_def_(final MethodNode _aMethodNode, final String[] _prim) {
    final JVMDefinedObjectType _ownerType;
    final String _name;
    final Invoke _invoke;
    final JVMMethodType[] _staticSignature;
    final int[] _selfOffset;
    _selfOffset = new int[1];
    _staticSignature = new JVMMethodType[1];
    _ownerType = JVMDefinedObjectType.factory.dottedClassName_(_prim[0]);
    _name = _prim[1];
    _invoke = _parent.createInvokeInstruction_name_numArgs_(_ownerType, _name, _aMethodNode.arguments().length);
    if (_invoke == null) {
        JVMMethodCompiler.this.emit_(NewInstance.factory.type_(JVMDefinedObjectType.factory.runtimeException()));
        JVMMethodCompiler.this.emit_(Dup.factory.basicNew());
        JVMMethodCompiler.this.pushString_("Cannot find method " + _ownerType.descriptorString() + ">>" + _name);
        JVMMethodCompiler.this.emit_(InvokeSpecial.factory.init_voidArguments_(JVMDefinedObjectType.factory.runtimeException(), st.gravel.support.jvm.ArrayFactory.with_(JVMDefinedObjectType.factory.string())));
        JVMMethodCompiler.this.emit_(AThrow.factory.basicNew());
        return JVMMethodCompiler.this;
    }
    _staticSignature[0] = _invoke.staticSignature();
    if (_invoke.isInvokeStatic() && st.gravel.support.jvm.IntegerExtensions.equals_(_invoke.signature().numArgs(), _aMethodNode.numArgs())) {
        _selfOffset[0] = 0;
    } else {
        _selfOffset[0] = 1;
        JVMMethodCompiler.this.produceVarRead_("self");
        JVMMethodCompiler.this.ensureCast_(_staticSignature[0].arguments()[0]);
    }
    for (int _temp1 = 0; _temp1 < _aMethodNode.arguments().length; _temp1++) {
        final int _i = _temp1 + 1;
        final VariableDeclarationNode _each = _aMethodNode.arguments()[_temp1];
        JVMMethodCompiler.this.produceVarRead_(_each.name());
        JVMMethodCompiler.this.ensureCast_(_staticSignature[0].arguments()[(_i + (_selfOffset[0])) - 1]);
    }
    this.emit_(_invoke);
    if (_staticSignature[0].returnType().isVoidType()) {
        JVMMethodCompiler.this.produceVarRead_("self");
    } else {
        JVMMethodCompiler.this.ensureCast_(JVMDynamicObjectType.factory.basicNew());
    }
    this.emit_(AReturn.factory.basicNew());
    return this;
}
Also used : VariableDeclarationNode(st.gravel.support.compiler.ast.VariableDeclarationNode) JVMDefinedObjectType(st.gravel.support.compiler.jvm.JVMDefinedObjectType) PushString(st.gravel.support.compiler.jvm.PushString) JVMMethodType(st.gravel.support.compiler.jvm.JVMMethodType) Invoke(st.gravel.support.compiler.jvm.Invoke)

Example 8 with VariableDeclarationNode

use of st.gravel.support.compiler.ast.VariableDeclarationNode in project gravel by gravel-st.

the class JVMMethodCompiler method buildBlock_copiedVariables_blockType_.

public JVMMethod buildBlock_copiedVariables_blockType_(final BlockNode _aBlockNode, final JVMField[] _anArray, final JVMDefinedObjectType _blockType) {
    final int _numArgs;
    this.reset();
    _isBlock = true;
    _copiedVariables = new java.util.HashMap<String, JVMField>();
    for (final JVMField _each : _anArray) {
        _copiedVariables.put(_each.varName(), _each);
    }
    _copiedVariablesOwner = this.newTempName();
    this.pushLocal_type_(_copiedVariablesOwner, _blockType);
    for (final VariableDeclarationNode _each : _aBlockNode.arguments()) {
        JVMMethodCompiler.this.pushLocal_(_each.name());
    }
    this.produceBlockBody_(_aBlockNode.body());
    _numArgs = _aBlockNode.arguments().length;
    _methodName = _parent.selectorConverter().functionNameForNumArgs_(_numArgs);
    _signature = JVMMethodType.factory.dynamic_(_numArgs);
    return this.buildMethod();
}
Also used : VariableDeclarationNode(st.gravel.support.compiler.ast.VariableDeclarationNode) JVMField(st.gravel.support.compiler.jvm.JVMField) PushString(st.gravel.support.compiler.jvm.PushString)

Example 9 with VariableDeclarationNode

use of st.gravel.support.compiler.ast.VariableDeclarationNode in project gravel by gravel-st.

the class NonLocalTempAccessToHolderConverter method visitSequenceNode_.

@Override
public SequenceNode visitSequenceNode_(final SequenceNode _anObject) {
    final SequenceNode[] _node;
    _node = new SequenceNode[1];
    _node[0] = SequenceNode.factory.temporaries_statements_(st.gravel.support.jvm.ArrayExtensions.collect_(_anObject.temporaries(), ((st.gravel.support.jvm.Block1<VariableDeclarationNode, VariableDeclarationNode>) (new st.gravel.support.jvm.Block1<VariableDeclarationNode, VariableDeclarationNode>() {

        @Override
        public VariableDeclarationNode value_(final VariableDeclarationNode _each) {
            _temps.put(_each.name(), VariableScopeAnalyzerStateDefined.factory.basicNew());
            return (VariableDeclarationNode) NonLocalTempAccessToHolderConverter.this.visit_(_each);
        }
    }))), st.gravel.support.jvm.ArrayExtensions.collect_(_anObject.statements(), ((st.gravel.support.jvm.Block1<Statement, Statement>) (new st.gravel.support.jvm.Block1<Statement, Statement>() {

        @Override
        public Statement value_(final Statement _each) {
            return (Statement) NonLocalTempAccessToHolderConverter.this.visit_(_each);
        }
    }))));
    for (final Map.Entry<String, VariableScopeAnalyzerState> _temp1 : _temps.entrySet()) {
        String _varName = _temp1.getKey();
        VariableScopeAnalyzerState _state = _temp1.getValue();
        if (NonLocalTempAccessToHolderConverter.this.needsRewrite_(_state)) {
            _node[0] = ((SequenceNode) VariableToHolderRewriter.factory.varName_(_varName).visit_(_node[0]));
        }
    }
    return _node[0];
}
Also used : VariableScopeAnalyzerState(st.gravel.support.compiler.ast.VariableScopeAnalyzerState) VariableDeclarationNode(st.gravel.support.compiler.ast.VariableDeclarationNode) Statement(st.gravel.support.compiler.ast.Statement) HashMap(java.util.HashMap) Map(java.util.Map) SequenceNode(st.gravel.support.compiler.ast.SequenceNode)

Example 10 with VariableDeclarationNode

use of st.gravel.support.compiler.ast.VariableDeclarationNode in project gravel by gravel-st.

the class SystemMapping method newClassMappingForJavaClass_.

public ClassMapping newClassMappingForJavaClass_(final Class _receiverClass) {
    final Map<st.gravel.core.Symbol, AbstractMethodMapping>[] _methodMappings;
    final ClassNode _classNode;
    final ClassMapping _superMapping;
    final java.util.Set<st.gravel.core.Symbol>[] _allSelectors;
    _allSelectors = new java.util.Set[1];
    _methodMappings = new Map[1];
    _superMapping = this.bestClassMappingFor_(_receiverClass);
    _allSelectors[0] = _superMapping.allSelectorsIn_(this);
    _methodMappings[0] = new java.util.HashMap<st.gravel.core.Symbol, AbstractMethodMapping>();
    _compilerTools.methodNamesIn_do_(_receiverClass, new st.gravel.support.jvm.Block2<Object, String, Integer>() {

        @Override
        public Object value_value_(final String _methodName, final Integer _numArgs) {
            final st.gravel.core.Symbol _sel;
            _sel = _selectorConverter.functionNameAsSelector_(_methodName);
            if (st.gravel.support.jvm.IntegerExtensions.equals_(_sel.numArgs(), _numArgs) && _allSelectors[0].contains(_sel)) {
                final java.lang.invoke.MethodHandle _methodHandle;
                _methodHandle = _compilerTools.methodHandleAt_numArgs_in_identityClass_isStatic_(_methodName, _sel.numArgs(), _receiverClass, _receiverClass, false);
                if (_methodHandle != null) {
                    return _methodMappings[0].put(_sel, AnonymousMethodMapping.factory.methodHandle_definingClass_(_methodHandle, _receiverClass));
                }
            }
            return SystemMapping.this;
        }
    });
    _classNode = ClassNode.factory.name_superclassPath_properties_instVars_classInstVars_sharedVariables_methods_classMethods_namespace_isExtension_isTrait_traitUsage_classTraitUsage_(_superMapping.classNode().name(), _superMapping.reference().toString(), new java.util.HashMap<String, String>(), new VariableDeclarationNode[] {}, new VariableDeclarationNode[] {}, new SharedDeclarationNode[] {}, new MethodNode[] {}, new MethodNode[] {}, _superMapping.classNode().namespace(), false, false, EmptyTraitUsageNode.factory.basicNew(), EmptyTraitUsageNode.factory.basicNew());
    return ClassMapping.factory.identityMapping_extensions_instVarMappings_classNode_(IdentityClassPartMapping.factory.javaClass_isGenerated_(_receiverClass, false).withMethodMappings_(_methodMappings[0]), new ExtensionClassPartMapping[] {}, new java.util.HashMap<String, InstVarMapping>(), _classNode);
}
Also used : ClassMapping(st.gravel.support.compiler.ast.ClassMapping) AbstractClassMapping(st.gravel.support.compiler.ast.AbstractClassMapping) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) SharedDeclarationNode(st.gravel.support.compiler.ast.SharedDeclarationNode) InstVarMapping(st.gravel.support.compiler.ast.InstVarMapping) MethodNode(st.gravel.support.compiler.ast.MethodNode) VariableDeclarationNode(st.gravel.support.compiler.ast.VariableDeclarationNode) ClassNode(st.gravel.support.compiler.ast.ClassNode) AbstractMethodMapping(st.gravel.support.compiler.ast.AbstractMethodMapping) BigInteger(java.math.BigInteger) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

VariableDeclarationNode (st.gravel.support.compiler.ast.VariableDeclarationNode)24 ArrayList (java.util.ArrayList)6 List (java.util.List)6 MethodNode (st.gravel.support.compiler.ast.MethodNode)6 Statement (st.gravel.support.compiler.ast.Statement)5 HashMap (java.util.HashMap)4 BoundVariableDeclarationNode (st.gravel.support.compiler.ast.BoundVariableDeclarationNode)4 PragmaNode (st.gravel.support.compiler.ast.PragmaNode)4 SequenceNode (st.gravel.support.compiler.ast.SequenceNode)4 TypeNode (st.gravel.support.compiler.ast.TypeNode)4 Map (java.util.Map)3 BlockTypeNode (st.gravel.support.compiler.ast.BlockTypeNode)3 ClassNode (st.gravel.support.compiler.ast.ClassNode)3 InstanceTypeNode (st.gravel.support.compiler.ast.InstanceTypeNode)3 SelfTypeNode (st.gravel.support.compiler.ast.SelfTypeNode)3 BlockSendArgument (st.gravel.support.compiler.jvm.BlockSendArgument)3 Expression (st.gravel.support.compiler.ast.Expression)2 KeywordMethodNode (st.gravel.support.compiler.ast.KeywordMethodNode)2 SharedDeclarationNode (st.gravel.support.compiler.ast.SharedDeclarationNode)2 TypeCast (st.gravel.support.compiler.ast.TypeCast)2