Search in sources :

Example 1 with ClassVarStmtToken

use of org.develnext.jphp.core.tokenizer.token.stmt.ClassVarStmtToken in project jphp by jphp-compiler.

the class ClassStmtCompiler method writeInitEnvironment.

@SuppressWarnings("unchecked")
protected void writeInitEnvironment() {
    if (!dynamicConstants.isEmpty() || !dynamicProperties.isEmpty()) {
        initDynamicExists = true;
        MethodNode node = new MethodNodeImpl();
        node.access = ACC_STATIC + ACC_PUBLIC;
        node.name = "__$initEnvironment";
        node.desc = Type.getMethodDescriptor(Type.getType(void.class), Type.getType(Environment.class));
        if (entity.isTrait()) {
            node.desc = Type.getMethodDescriptor(Type.getType(void.class), Type.getType(Environment.class), Type.getType(String.class));
        }
        MethodStmtCompiler methodCompiler = new MethodStmtCompiler(this, node);
        ExpressionStmtCompiler expressionCompiler = new ExpressionStmtCompiler(methodCompiler, null);
        methodCompiler.writeHeader();
        LabelNode l0 = expressionCompiler.makeLabel();
        methodCompiler.addLocalVariable("~env", l0, Environment.class);
        if (entity.isTrait())
            methodCompiler.addLocalVariable("~class_name", l0, String.class);
        LocalVariable l_class = methodCompiler.addLocalVariable("~class", l0, ClassEntity.class);
        expressionCompiler.writePushEnv();
        if (entity.isTrait()) {
            expressionCompiler.writeVarLoad("~class_name");
            expressionCompiler.writePushDupLowerCase();
        } else {
            expressionCompiler.writePushConstString(entity.getName());
            expressionCompiler.writePushConstString(entity.getLowerName());
        }
        expressionCompiler.writePushConstBoolean(true);
        expressionCompiler.writeSysDynamicCall(Environment.class, "fetchClass", ClassEntity.class, String.class, String.class, Boolean.TYPE);
        expressionCompiler.writeVarStore(l_class, false, false);
        // corrects defination of constants
        final List<ConstStmtToken.Item> first = new ArrayList<ConstStmtToken.Item>();
        final Set<String> usedNames = new HashSet<String>();
        final List<ConstStmtToken.Item> other = new ArrayList<ConstStmtToken.Item>();
        for (ConstStmtToken.Item el : dynamicConstants) {
            Token tk = el.value.getSingle();
            if (tk instanceof StaticAccessExprToken) {
                StaticAccessExprToken access = (StaticAccessExprToken) tk;
                boolean self = false;
                if (access.getClazz() instanceof SelfExprToken)
                    self = true;
                else if (access.getClazz() instanceof FulledNameToken && ((FulledNameToken) access.getClazz()).getName().equalsIgnoreCase(entity.getName())) {
                    self = true;
                }
                if (self) {
                    String name = ((NameToken) access.getField()).getName();
                    if (usedNames.contains(el.getFulledName()))
                        first.add(0, el);
                    else
                        first.add(el);
                    usedNames.add(name);
                    continue;
                }
            }
            if (usedNames.contains(el.getFulledName()))
                first.add(0, el);
            else
                other.add(el);
        }
        other.addAll(0, first);
        for (ConstStmtToken.Item el : other) {
            expressionCompiler.writeVarLoad(l_class);
            expressionCompiler.writePushEnv();
            expressionCompiler.writePushConstString(el.getFulledName());
            expressionCompiler.writeExpression(el.value, true, false, true);
            expressionCompiler.writePopBoxing(true);
            expressionCompiler.writeSysDynamicCall(ClassEntity.class, "addDynamicConstant", void.class, Environment.class, String.class, Memory.class);
        }
        for (ClassVarStmtToken el : dynamicProperties) {
            expressionCompiler.writeVarLoad(l_class);
            expressionCompiler.writePushEnv();
            expressionCompiler.writePushConstString(el.getVariable().getName());
            expressionCompiler.writeExpression(el.getValue(), true, false, true);
            expressionCompiler.writePopBoxing(true);
            expressionCompiler.writeSysDynamicCall(ClassEntity.class, el.isStatic() ? "addDynamicStaticProperty" : "addDynamicProperty", void.class, Environment.class, String.class, Memory.class);
        }
        node.instructions.add(new InsnNode(RETURN));
        methodCompiler.writeFooter();
        this.node.methods.add(node);
    }
}
Also used : StaticAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.StaticAccessExprToken) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken) StaticAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.StaticAccessExprToken) ClassStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ClassStmtToken) ClassVarStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ClassVarStmtToken) MethodStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.MethodStmtToken) Token(org.develnext.jphp.core.tokenizer.token.Token) SelfExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.SelfExprToken) ConstStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) MethodNodeImpl(org.develnext.jphp.core.compiler.jvm.node.MethodNodeImpl) ClassVarStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ClassVarStmtToken) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) ConstStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken) SelfExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.SelfExprToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)

Example 2 with ClassVarStmtToken

use of org.develnext.jphp.core.tokenizer.token.stmt.ClassVarStmtToken in project jphp by jphp-compiler.

the class ClassStmtCompiler method writeConstructor.

@SuppressWarnings("unchecked")
protected void writeConstructor() {
    MethodNode constructor = new MethodNodeImpl();
    constructor.name = Constants.INIT_METHOD;
    constructor.access = ACC_PUBLIC;
    constructor.exceptions = new ArrayList();
    MethodStmtCompiler methodCompiler = new MethodStmtCompiler(this, constructor);
    ExpressionStmtCompiler expressionCompiler = new ExpressionStmtCompiler(methodCompiler, null);
    methodCompiler.writeHeader();
    LabelNode l0 = writeLabel(constructor, statement.getMeta().getStartLine());
    methodCompiler.addLocalVariable("~this", l0);
    if (isClosure() || generatorEntity != null) {
        constructor.desc = Type.getMethodDescriptor(Type.getType(void.class), Type.getType(Environment.class), Type.getType(ClassEntity.class), Type.getType(Memory.class), Type.getType(Memory[].class));
        if (isClosure()) {
            constructor.desc = Type.getMethodDescriptor(Type.getType(void.class), Type.getType(Environment.class), Type.getType(ClassEntity.class), Type.getType(Memory.class), Type.getType(String.class), Type.getType(Memory[].class));
        }
        methodCompiler.addLocalVariable("~env", l0, Environment.class);
        methodCompiler.addLocalVariable("~class", l0, ClassEntity.class);
        methodCompiler.addLocalVariable("~self", l0, Memory.class);
        if (isClosure()) {
            methodCompiler.addLocalVariable("~context", l0, String.class);
        }
        methodCompiler.addLocalVariable("~uses", l0, Memory[].class);
        methodCompiler.writeHeader();
        expressionCompiler.writeVarLoad("~this");
        expressionCompiler.writeVarLoad("~env");
        expressionCompiler.writeVarLoad("~class");
        expressionCompiler.writeVarLoad("~self");
        if (isClosure()) {
            expressionCompiler.writeVarLoad("~context");
        }
        expressionCompiler.writeVarLoad("~uses");
        constructor.instructions.add(new MethodInsnNode(INVOKESPECIAL, node.superName, Constants.INIT_METHOD, constructor.desc, false));
    } else {
        constructor.desc = Type.getMethodDescriptor(Type.getType(void.class), Type.getType(Environment.class), Type.getType(ClassEntity.class));
        methodCompiler.addLocalVariable("~env", l0, Environment.class);
        methodCompiler.addLocalVariable("~class", l0, String.class);
        expressionCompiler.writeVarLoad("~this");
        expressionCompiler.writeVarLoad("~env");
        expressionCompiler.writeVarLoad("~class");
        constructor.instructions.add(new MethodInsnNode(INVOKESPECIAL, node.superName, Constants.INIT_METHOD, constructor.desc, false));
        // PROPERTIES
        for (ClassVarStmtToken property : statement.getProperties()) {
            ExpressionStmtCompiler expressionStmtCompiler = new ExpressionStmtCompiler(methodCompiler, null);
            Memory value = Memory.NULL;
            if (property.getValue() != null)
                value = expressionStmtCompiler.writeExpression(property.getValue(), true, true, false);
            PropertyEntity prop = new PropertyEntity(compiler.getContext());
            prop.setName(property.getVariable().getName());
            prop.setModifier(property.getModifier());
            prop.setStatic(property.isStatic());
            prop.setDefaultValue(value);
            prop.setDefault(property.getValue() != null);
            prop.setTrace(property.toTraceInfo(compiler.getContext()));
            if (property.getDocComment() != null) {
                prop.setDocComment(new DocumentComment(property.getDocComment().getComment()));
            }
            ClassEntity.PropertyResult result = entity.addProperty(prop);
            result.check(compiler.getEnvironment());
            if (value == null && property.getValue() != null) {
                if (property.getValue().isSingle() && ValueExprToken.isConstable(property.getValue().getSingle(), true))
                    dynamicProperties.add(property);
                else
                    compiler.getEnvironment().error(property.getVariable().toTraceInfo(compiler.getContext()), ErrorType.E_COMPILE_ERROR, Messages.ERR_EXPECTED_CONST_VALUE.fetch(entity.getName() + "::$" + property.getVariable().getName()));
            }
        }
    }
    methodCompiler.writeFooter();
    constructor.instructions.add(new InsnNode(RETURN));
    node.methods.add(constructor);
}
Also used : Memory(php.runtime.Memory) MethodNodeImpl(org.develnext.jphp.core.compiler.jvm.node.MethodNodeImpl) ClassVarStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ClassVarStmtToken)

Example 3 with ClassVarStmtToken

use of org.develnext.jphp.core.tokenizer.token.stmt.ClassVarStmtToken in project jphp by jphp-compiler.

the class ClassDescription method parse.

@Override
protected void parse() {
    methods = new LinkedHashMap<String, MethodDescription>();
    for (MethodStmtToken el : token.getMethods()) {
        methods.put(el.getName().getName().toLowerCase(), new MethodDescription(el));
    }
    properties = new LinkedHashMap<String, PropertyDescription>();
    for (ClassVarStmtToken el : token.getProperties()) {
        properties.put(el.getVariable().getName(), new PropertyDescription(el));
    }
    constants = new LinkedHashMap<String, ConstantDescription>();
    for (ConstStmtToken el : token.getConstants()) {
        constants.put(el.items.get(0).getFulledName(), new ConstantDescription(el));
    }
    if (token.getDocComment() != null) {
        DocAnnotations annotations = new DocAnnotations(token.getDocComment().getComment());
        description = annotations.getDescription();
    }
}
Also used : ConstStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken) MethodStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.MethodStmtToken) ClassVarStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ClassVarStmtToken) DocAnnotations(org.develnext.jphp.genapi.DocAnnotations)

Aggregations

ClassVarStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ClassVarStmtToken)3 MethodNodeImpl (org.develnext.jphp.core.compiler.jvm.node.MethodNodeImpl)2 ConstStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken)2 MethodStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.MethodStmtToken)2 LocalVariable (org.develnext.jphp.core.compiler.jvm.misc.LocalVariable)1 Token (org.develnext.jphp.core.tokenizer.token.Token)1 ValueExprToken (org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)1 FulledNameToken (org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)1 NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)1 SelfExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.SelfExprToken)1 StaticAccessExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.StaticAccessExprToken)1 ClassStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ClassStmtToken)1 DocAnnotations (org.develnext.jphp.genapi.DocAnnotations)1 Memory (php.runtime.Memory)1