Search in sources :

Example 1 with SelfExprToken

use of org.develnext.jphp.core.tokenizer.token.expr.value.SelfExprToken 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 SelfExprToken

use of org.develnext.jphp.core.tokenizer.token.expr.value.SelfExprToken in project jphp by jphp-compiler.

the class NewValueCompiler method write.

@Override
public void write(NewExprToken token, boolean returnValue) {
    method.getEntity().setImmutable(false);
    expr.writeLineNumber(token);
    boolean staticName = false;
    expr.writePushEnv();
    if (token.isDynamic()) {
        Memory className = expr.writeExpression(token.getExprName(), true, true, false);
        if (className != null) {
            expr.writePushConstString(className.toString());
            expr.writePushConstString(className.toString().toLowerCase());
        } else {
            expr.writeExpression(token.getExprName(), true, false);
            expr.writePopString();
            expr.writePushDupLowerCase();
        }
    } else {
        if (token.getName() instanceof StaticExprToken) {
            expr.writePushStatic();
            expr.writePushDupLowerCase();
        } else if (token.getName() instanceof SelfExprToken) {
            expr.writePushEnv();
            expr.writeSysDynamicCall(Environment.class, "__getMacroClass", Memory.class);
            expr.writePopString();
            expr.writePushDupLowerCase();
        } else {
            staticName = true;
            FulledNameToken name = (FulledNameToken) token.getName();
            expr.writePushString(name.getName());
            expr.writePushString(name.getName().toLowerCase());
        }
    }
    expr.writePushTraceInfo(token);
    expr.writePushParameters(token.getParameters());
    if (staticName) {
        int cacheIndex = method.clazz.getAndIncCallClassCount();
        expr.writeGetStatic("$CALL_CLASS_CACHE", ClassCallCache.class);
        expr.writePushConstInt(cacheIndex);
        expr.writeSysDynamicCall(Environment.class, "__newObject", Memory.class, String.class, String.class, TraceInfo.class, Memory[].class, ClassCallCache.class, int.class);
    } else {
        expr.writeSysDynamicCall(Environment.class, "__newObject", Memory.class, String.class, String.class, TraceInfo.class, Memory[].class);
    }
    expr.setStackPeekAsImmutable();
    if (!returnValue)
        expr.writePopAll(1);
}
Also used : Memory(php.runtime.Memory) StaticExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.StaticExprToken) Environment(php.runtime.env.Environment) SelfExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.SelfExprToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)

Aggregations

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