Search in sources :

Example 1 with FatalException

use of php.runtime.exceptions.FatalException in project jphp by jphp-compiler.

the class ExprGenerator method processJump.

protected void processJump(JumpStmtToken result, ListIterator<Token> iterator) {
    Token next = nextToken(iterator);
    long level = 1;
    if (next instanceof IntegerExprToken) {
        level = ((IntegerExprToken) next).getValue();
        if (level < 1)
            throw new FatalException(Messages.ERR_OPERATOR_ACCEPTS_ONLY_POSITIVE.fetch(result.getWord()), result.toTraceInfo(analyzer.getContext()));
        next = nextToken(iterator);
    }
    result.setLevel((int) level);
    if (!(next instanceof SemicolonToken))
        unexpectedToken(next);
}
Also used : FatalException(php.runtime.exceptions.FatalException)

Example 2 with FatalException

use of php.runtime.exceptions.FatalException in project jphp by jphp-compiler.

the class ClassStmtCompiler method compile.

@Override
public ClassEntity compile() {
    entity = new ClassEntity(compiler.getContext());
    entity.setId(compiler.getScope().nextClassIndex());
    entity.setFinal(statement.isFinal());
    entity.setAbstract(statement.isAbstract());
    entity.setName(statement.getFulledName());
    if (statement.getDocComment() != null) {
        entity.setDocComment(new DocumentComment(statement.getDocComment().getComment()));
    }
    entity.setTrace(statement.toTraceInfo(compiler.getContext()));
    entity.setType(statement.getClassType());
    List<ClassEntity> traits = fetchTraits();
    for (ClassEntity e : traits) {
        entity.addTrait(e);
    }
    checkAliasAndReplacementsTraits();
    if (statement.getExtend() != null) {
        ClassEntity parent = fetchClass(statement.getExtend().getName().getName());
        if (parent == null)
            compiler.getEnvironment().error(statement.getExtend().toTraceInfo(compiler.getContext()), Messages.ERR_CLASS_NOT_FOUND.fetch(statement.getExtend().getName().toName()));
        ClassEntity.ExtendsResult result = entity.setParent(parent, false);
        if (isInterfaceCheck) {
            result.check(compiler.getEnvironment());
        }
    }
    if (!isSystem) {
        if (entity.isUseJavaLikeNames()) {
            entity.setInternalName(entity.getName().replace('\\', '/'));
        } else {
            entity.setInternalName(compiler.getModule().getInternalName() + "_class" + compiler.getModule().getClasses().size());
        }
    }
    if (compiler.getModule().findClass(entity.getLowerName()) != null || compiler.getEnvironment().isLoadedClass(entity.getLowerName())) {
        throw new FatalException(Messages.ERR_CANNOT_REDECLARE_CLASS.fetch(entity.getName()), statement.getName().toTraceInfo(compiler.getContext()));
    }
    if (!statement.isInterface()) {
        node.access = ACC_SUPER + ACC_PUBLIC;
        node.name = !isSystem ? /*&& !statement.isTrait()*/
        entity.getCompiledInternalName() : statement.getFulledName(Constants.NAME_DELIMITER);
        node.superName = entity.getParent() == null ? Type.getInternalName(BaseObject.class) : entity.getParent().getInternalName();
        node.sourceFile = compiler.getSourceFile();
        /*if (!isSystem) {
                AnnotationNode annotationNode = new AnnotationNode(Type.getInternalName(Reflection.Name.class));
                annotationNode.values = Arrays.asList("value", entity.getName());

                node.visibleAnnotations.add(annotationNode);
            } */
        writeSystemInfo();
        writeConstructor();
        writeDefaultConstructors();
    }
    // constants
    if (statement.getConstants() != null)
        for (ConstStmtToken constant : statement.getConstants()) {
            writeConstant(constant);
        }
    if (statement.getMethods() != null) {
        for (MethodStmtToken method : statement.getMethods()) {
            ClassEntity.SignatureResult result = entity.addMethod(compiler.compileMethod(this, method, external, generatorEntity), null);
            result.check(compiler.getEnvironment());
        }
    }
    writeTraits(traits);
    ClassEntity.SignatureResult result = entity.updateParentBody();
    if (isInterfaceCheck) {
        result.check(compiler.getEnvironment());
    }
    writeImplements();
    entity.doneDeclare();
    if (!statement.isInterface()) {
        writeDestructor();
        if (entity.getType() != ClassEntity.Type.INTERFACE) {
            writeInitEnvironment();
        }
        writeInitStatic();
        cw = new JPHPClassWriter(entity.isTrait());
        node.accept(cw);
        entity.setData(cw.toByteArray());
    }
    return entity;
}
Also used : JPHPClassWriter(org.develnext.jphp.core.compiler.jvm.JPHPClassWriter) FatalException(php.runtime.exceptions.FatalException) ConstStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken) MethodStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.MethodStmtToken)

Example 3 with FatalException

use of php.runtime.exceptions.FatalException in project jphp by jphp-compiler.

the class ForeachCompiler method write.

@Override
public void write(ForeachStmtToken token) {
    expr.writeDefineVariables(token.getLocal());
    LabelNode start = new LabelNode();
    LabelNode end = new LabelNode();
    LabelNode l = new LabelNode();
    add(l);
    expr.writePushEnv();
    expr.writePushTraceInfo(token);
    expr.writeExpression(token.getIterator(), true, false, true);
    expr.writePopBoxing();
    expr.writePushConstBoolean(token.isValueReference());
    expr.writePushConstBoolean(token.isKeyReference());
    expr.writeSysDynamicCall(Environment.class, "__getIterator", ForeachIterator.class, TraceInfo.class, Memory.class, Boolean.TYPE, Boolean.TYPE);
    String name = "~foreach~" + method.nextStatementIndex(ForeachIterator.class);
    LocalVariable foreachVariable = method.getLocalVariable(name);
    if (foreachVariable == null)
        foreachVariable = method.addLocalVariable(name, l, ForeachIterator.class);
    /*LocalVariable foreachVariable = method.addLocalVariable(
                "~foreach~" + method.nextStatementIndex(ForeachIterator.class), l, ForeachIterator.class
        );*/
    foreachVariable.setEndLabel(end);
    expr.writeVarStore(foreachVariable, false, false);
    method.pushJump(end, start);
    add(start);
    expr.writeVarLoad(foreachVariable);
    expr.writeSysDynamicCall(ForeachIterator.class, "next", Boolean.TYPE);
    add(new JumpInsnNode(IFEQ, end));
    expr.stackPop();
    // $key
    if (token.getKey() != null) {
        LocalVariable key = method.getLocalVariable(token.getKey().getName());
        expr.checkAssignableVar(token.getKey());
        expr.writeVarLoad(foreachVariable);
        expr.writeSysDynamicCall(ForeachIterator.class, "getMemoryKey", Memory.class);
        if (token.isKeyReference()) {
            throw new FatalException("Key element cannot be a reference", token.getKey().toTraceInfo(compiler.getContext()));
        // writeVarStore(key, false, false);
        } else
            expr.writeVarAssign(key, null, false, false);
    }
    // $var
    // LocalVariable variable = method.getLocalVariable(token.getValue().getName());
    Token last = token.getValue().getLast();
    VariableExprToken var = null;
    if (last instanceof DynamicAccessExprToken) {
        DynamicAccessExprToken setter = (DynamicAccessExprToken) last;
        ExprStmtToken value = new ExprStmtToken(this.env, this.compiler.getContext(), token.getValue().getTokens());
        value.getTokens().remove(value.getTokens().size() - 1);
        value.updateAsmExpr(this.env, this.compiler.getContext());
        expr.writeExpression(value, true, false);
        expr.writeVarLoad(foreachVariable);
        expr.writeSysDynamicCall(ForeachIterator.class, "getValue", Memory.class);
        if (!token.isValueReference())
            expr.writePopImmutable();
        expr.writeDynamicAccessInfo(setter, false);
        expr.writeGetStatic("$CALL_PROP_CACHE", PropertyCallCache.class);
        expr.writePushConstInt(method.clazz.getAndIncCallPropCount());
        expr.writeSysStaticCall(ObjectInvokeHelper.class, "assignProperty", Memory.class, Memory.class, Memory.class, String.class, Environment.class, TraceInfo.class, PropertyCallCache.class, int.class);
    } else {
        if (token.getValue().getSingle() instanceof VariableExprToken)
            expr.checkAssignableVar(var = (VariableExprToken) token.getValue().getSingle());
        ExprStmtToken value = token.getValue();
        expr.writeVarLoad(foreachVariable);
        expr.writeSysDynamicCall(ForeachIterator.class, "getValue", Memory.class);
        if (value.isSingle() && value.getSingle() instanceof ListExprToken) {
            ListExprToken listExprToken = (ListExprToken) value.getSingle();
            if (!token.isValueReference() && !listExprToken.isHasRef()) {
                expr.writePopImmutable();
            }
            ListCompiler listCompiler = (ListCompiler) expr.getCompiler(ListExprToken.class);
            listCompiler.write(listExprToken, false, false);
        } else {
            if (!token.isValueReference())
                expr.writePopImmutable();
            expr.writeExpression(value, true, false);
            if (expr.stackPeek().immutable)
                expr.unexpectedToken(value.getLast());
            expr.writeSysStaticCall(Memory.class, token.isValueReference() ? "assignRefRight" : "assignRight", Memory.class, Memory.class, Memory.class);
        }
    }
    expr.writePopAll(1);
    /*
        if (token.isValueReference())
            writeVarStore(variable, false, false);
        else
            writeVarAssign(variable, false, true); */
    // body
    expr.write(BodyStmtToken.class, token.getBody());
    add(new JumpInsnNode(GOTO, start));
    add(end);
    /*if (compiler.getLangMode() == LangMode.JPHP){
            if (token.isValueReference() && var != null){
                expr.writeVarLoad(var.getName());
                expr.writeSysDynamicCall(Memory.class, "unset", void.class);
            }
        }*/
    method.popJump();
    expr.writeUndefineVariables(token.getLocal());
    method.prevStatementIndex(ForeachIterator.class);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) ForeachIterator(php.runtime.lang.ForeachIterator) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) FatalException(php.runtime.exceptions.FatalException) ListCompiler(org.develnext.jphp.core.compiler.jvm.statement.expr.value.ListCompiler) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) DynamicAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.DynamicAccessExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) BodyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken) ForeachStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ForeachStmtToken) VariableExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) ListExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.ListExprToken) DynamicAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.DynamicAccessExprToken) VariableExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken) ListExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.ListExprToken)

Example 4 with FatalException

use of php.runtime.exceptions.FatalException in project jphp by jphp-compiler.

the class Environment method __defineFunction.

public void __defineFunction(TraceInfo trace, String moduleInternalName, int index) {
    ModuleEntity module = scope.moduleIndexMap.get(moduleInternalName);
    if (module == null)
        throw new CriticalException("Cannot find module: " + moduleInternalName);
    FunctionEntity function = module.findFunction(index);
    if (functionMap.put(function.getLowerName(), function) != null) {
        triggerError(new FatalException(Messages.ERR_CANNOT_REDECLARE_FUNCTION.fetch(function.getName()), trace));
    }
}
Also used : FunctionEntity(php.runtime.reflection.FunctionEntity) FatalException(php.runtime.exceptions.FatalException) ModuleEntity(php.runtime.reflection.ModuleEntity) CriticalException(php.runtime.exceptions.CriticalException)

Example 5 with FatalException

use of php.runtime.exceptions.FatalException in project jphp by jphp-compiler.

the class Environment method __throwException.

public void __throwException(TraceInfo trace, Memory exception) {
    if (exception.isObject()) {
        IObject object;
        if ((object = exception.toValue(ObjectMemory.class).value) instanceof BaseBaseException) {
            __clearSilent();
            BaseBaseException e = (BaseBaseException) object;
            e.setTraceInfo(this, trace);
            throw e;
        } else {
            triggerError(new FatalException("Exceptions must be valid objects derived from the Exception base class", trace));
        }
    } else {
        triggerError(new FatalException("Can only throw objects", trace));
    }
}
Also used : IObject(php.runtime.lang.IObject) FatalException(php.runtime.exceptions.FatalException) BaseBaseException(php.runtime.lang.exception.BaseBaseException)

Aggregations

FatalException (php.runtime.exceptions.FatalException)5 JPHPClassWriter (org.develnext.jphp.core.compiler.jvm.JPHPClassWriter)1 LocalVariable (org.develnext.jphp.core.compiler.jvm.misc.LocalVariable)1 ListCompiler (org.develnext.jphp.core.compiler.jvm.statement.expr.value.ListCompiler)1 Token (org.develnext.jphp.core.tokenizer.token.Token)1 DynamicAccessExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.DynamicAccessExprToken)1 ListExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.ListExprToken)1 VariableExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken)1 BodyStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken)1 ConstStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ConstStmtToken)1 ExprStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken)1 ForeachStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ForeachStmtToken)1 MethodStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.MethodStmtToken)1 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)1 LabelNode (org.objectweb.asm.tree.LabelNode)1 CriticalException (php.runtime.exceptions.CriticalException)1 ForeachIterator (php.runtime.lang.ForeachIterator)1 IObject (php.runtime.lang.IObject)1 BaseBaseException (php.runtime.lang.exception.BaseBaseException)1 FunctionEntity (php.runtime.reflection.FunctionEntity)1