Search in sources :

Example 1 with DocumentComment

use of php.runtime.reflection.DocumentComment in project jphp by jphp-compiler.

the class PropertyDumper method load.

@Override
public PropertyEntity load(InputStream input) throws IOException {
    PropertyEntity property = new PropertyEntity(context);
    DumpInputStream data = new DumpInputStream(input);
    String docComment = data.readUTF();
    if (!docComment.isEmpty()) {
        property.setDocComment(new DocumentComment(docComment));
    }
    property.setStatic(data.readBoolean());
    property.setModifier(data.readModifier());
    property.setName(data.readName());
    property.setTrace(data.readTrace(context));
    // typed
    property.setNullable(data.readBoolean());
    property.setType(data.readHintType());
    String typeClass = data.readName();
    if (typeClass != null && !typeClass.isEmpty()) {
        property.setTypeClass(typeClass);
    }
    property.setDefault(data.readBoolean());
    property.setDefaultValue(data.readMemory());
    byte[] raw = data.readRawData();
    return property;
}
Also used : DumpInputStream(php.runtime.loader.dump.io.DumpInputStream) DocumentComment(php.runtime.reflection.DocumentComment) PropertyEntity(php.runtime.reflection.PropertyEntity)

Example 2 with DocumentComment

use of php.runtime.reflection.DocumentComment in project jphp by jphp-compiler.

the class MethodDumper method load.

@Override
public MethodEntity load(InputStream input) throws IOException {
    DumpInputStream data = new DumpInputStream(input);
    MethodEntity entity = new MethodEntity(context);
    String docComment = data.readUTF();
    if (!docComment.isEmpty()) {
        entity.setDocComment(new DocumentComment(docComment));
    }
    // static
    entity.setStatic(data.readBoolean());
    // final
    entity.setFinal(data.readBoolean());
    // abstract
    entity.setAbstract(data.readBoolean());
    entity.setAbstractable(data.readBoolean());
    entity.setImmutable(data.readBoolean());
    entity.setResult(data.readMemory());
    entity.setEmpty(data.readBoolean());
    // ref
    entity.setReturnReference(data.readBoolean());
    // type hinting
    entity.setReturnType(data.readHintType());
    String returnTypeClass = data.readName();
    if (returnTypeClass != null && !returnTypeClass.isEmpty()) {
        entity.setReturnTypeClass(returnTypeClass);
    }
    entity.setReturnTypeNullable(data.readBoolean());
    // uses stack trace
    entity.setUsesStackTrace(data.readBoolean());
    // modifier
    entity.setModifier(data.readModifier());
    // name
    entity.setName(data.readName());
    entity.setInternalName(data.readName());
    // trace
    entity.setTrace(data.readTrace(context));
    int paramCount = data.readInt();
    if (paramCount < 0)
        throw new DumpException("Invalid param count");
    entity.setParameters(new ParameterEntity[paramCount]);
    for (int i = 0; i < paramCount; i++) {
        ParameterEntity param = parameterDumper.load(input);
        param.setTrace(entity.getTrace());
        entity.getParameters()[i] = param;
    }
    data.readRawData();
    return entity;
}
Also used : DumpInputStream(php.runtime.loader.dump.io.DumpInputStream) DocumentComment(php.runtime.reflection.DocumentComment) ParameterEntity(php.runtime.reflection.ParameterEntity) MethodEntity(php.runtime.reflection.MethodEntity) DumpException(php.runtime.loader.dump.io.DumpException)

Example 3 with DocumentComment

use of php.runtime.reflection.DocumentComment in project jphp by jphp-compiler.

the class MethodStmtCompiler method compile.

@Override
public MethodEntity compile() {
    if (statement != null) {
        if (external)
            statement.setDynamicLocal(true);
        if (statement.getDocComment() != null)
            entity.setDocComment(new DocumentComment(statement.getDocComment().getComment()));
        entity.setAbstract(statement.isAbstract());
        entity.setAbstractable(statement.getBody() == null);
        entity.setFinal(statement.isFinal());
        entity.setStatic(statement.isStatic());
        entity.setModifier(statement.getModifier());
        entity.setReturnReference(statement.isReturnReference());
        entity.setTrace(statement.toTraceInfo(compiler.getContext()));
        entity.setImmutable(statement.getArguments().isEmpty());
        entity.setGeneratorEntity(generatorEntity);
        if (statement.getReturnHintTypeClass() != null) {
            entity.setReturnTypeChecker(TypeChecker.of(statement.getReturnHintTypeClass().getName()));
        } else if (statement.getReturnHintType() != null) {
            entity.setReturnTypeChecker(TypeChecker.of(statement.getReturnHintType()));
            if (statement.getReturnHintType() == HintType.SELF) {
                entity.setUsesStackTrace(true);
            }
        }
        entity.setReturnTypeNullable(statement.isReturnOptional());
        if (clazz.isSystem())
            entity.setInternalName(entity.getName());
        else
            entity.setInternalName(entity.getName() + "$" + clazz.entity.nextMethodIndex());
        ParameterEntity[] parameters = new ParameterEntity[statement.getArguments().size()];
        int i = 0;
        for (ArgumentStmtToken argument : statement.getArguments()) {
            parameters[i] = new ParameterEntity(compiler.getContext());
            ParameterEntity parameter = parameters[i];
            parameter.setReference(argument.isReference());
            parameter.setName(argument.getName().getName());
            parameter.setTrace(argument.toTraceInfo(compiler.getContext()));
            parameter.setNullable(argument.isOptional());
            parameter.setMutable(statement.isDynamicLocal() || statement.variable(argument.getName()).isMutable());
            parameter.setUsed(!statement.isUnusedVariable(argument.getName()));
            parameter.setVariadic(argument.isVariadic());
            parameter.setType(argument.getHintType());
            if (argument.getHintTypeClass() != null) {
                parameter.setTypeClass(argument.getHintTypeClass().getName());
            }
            ExpressionStmtCompiler expressionStmtCompiler = new ExpressionStmtCompiler(compiler);
            ExprStmtToken value = argument.getValue();
            if (value != null) {
                Memory defaultValue = expressionStmtCompiler.writeExpression(value, true, true, false);
                // try detect constant
                if (value.isSingle()) {
                    if (value.getSingle() instanceof NameToken) {
                        parameter.setDefaultValueConstName(((NameToken) value.getSingle()).getName());
                        if (defaultValue == null) {
                            defaultValue = (new ConstantMemory(((NameToken) value.getSingle()).getName()));
                            parameter.setMutable(true);
                        }
                    } else if (value.getSingle() instanceof StaticAccessExprToken) {
                        StaticAccessExprToken access = (StaticAccessExprToken) value.getSingle();
                        if (access.getClazz() instanceof NameToken && access.getField() instanceof NameToken) {
                            if (defaultValue == null)
                                defaultValue = (new ClassConstantMemory(((NameToken) access.getClazz()).getName(), ((NameToken) access.getField()).getName()));
                            parameter.setDefaultValueConstName(((NameToken) access.getClazz()).getName() + "::" + ((NameToken) access.getField()).getName());
                            parameter.setMutable(true);
                        }
                    }
                }
                if (defaultValue == null)
                    compiler.getEnvironment().error(argument.toTraceInfo(compiler.getContext()), ErrorType.E_COMPILE_ERROR, Messages.ERR_EXPECTED_CONST_VALUE, "$" + argument.getName().getName());
                parameter.setDefaultValue(defaultValue);
            }
            i++;
        }
        entity.setParameters(parameters);
    }
    if (statement != null && clazz.statement.isInterface()) {
        if (!statement.isInterfacable()) {
            compiler.getEnvironment().error(entity.getTrace(), Messages.ERR_INTERFACE_FUNCTION_CANNOT_CONTAIN_BODY.fetch(entity.getSignatureString(false)));
        }
        if (statement.isAbstract() || statement.isFinal()) {
            compiler.getEnvironment().error(entity.getTrace(), Messages.ERR_ACCESS_TYPE_FOR_INTERFACE_METHOD.fetch(entity.getSignatureString(false)));
        }
    } else {
        writeHeader();
        if (statement.isGenerator()) {
            entity.setEmpty(false);
            entity.setImmutable(false);
            entity.setResult(null);
            GeneratorStmtCompiler generatorStmtCompiler = new GeneratorStmtCompiler(compiler, statement);
            entity.setGeneratorEntity(generatorStmtCompiler.compile());
            ExpressionStmtCompiler expr = new ExpressionStmtCompiler(this, null);
            expr.makeUnknown(new TypeInsnNode(NEW, entity.getGeneratorEntity().getInternalName()));
            expr.stackPush(Memory.Type.REFERENCE);
            expr.writePushDup();
            // env
            expr.writePushEnv();
            // classEntity
            expr.writePushDup();
            expr.writePushConstString(compiler.getModule().getInternalName());
            expr.writePushConstInt((int) entity.getGeneratorEntity().getId());
            expr.writeSysDynamicCall(Environment.class, "__getGenerator", ClassEntity.class, String.class, Integer.TYPE);
            // self
            expr.writePushThis();
            // uses
            expr.writeVarLoad("~args");
            expr.writeSysCall(entity.getGeneratorEntity().getInternalName(), INVOKESPECIAL, Constants.INIT_METHOD, void.class, Environment.class, ClassEntity.class, Memory.class, Memory[].class);
            expr.writeSysStaticCall(ObjectMemory.class, "valueOf", Memory.class, IObject.class);
            expr.makeUnknown(new InsnNode(Opcodes.ARETURN));
            expr.stackPop();
        } else {
            ExpressionStmtCompiler expr = new ExpressionStmtCompiler(this, null);
            entity.setEmpty(true);
            if (entity.getResult() == null) {
                entity.setResult(Memory.UNDEFINED);
            }
            if (statement != null && statement.getBody() != null) {
                expr.writeDefineVariables(statement.getLocal());
                expr.write(statement.getBody());
                if (!statement.getBody().getInstructions().isEmpty()) {
                    entity.setEmpty(false);
                    if (entity.getResult() != null && entity.getResult().isUndefined()) {
                        entity.setResult(null);
                    }
                }
            }
            if (generatorEntity != null) {
                expr.writeVarLoad(LocalVariable.THIS);
                expr.writePushConstBoolean(false);
                expr.writeSysDynamicCall(null, "_setValid", void.class, Boolean.TYPE);
            }
            ReturnStmtToken token = new ReturnStmtToken(new TokenMeta("", 0, 0, 0, 0));
            token.setValue(null);
            token.setEmpty(true);
            expr.getCompiler(ReturnStmtToken.class).write(token);
        }
        writeFooter();
    }
    return entity;
}
Also used : TokenMeta(org.develnext.jphp.core.tokenizer.TokenMeta) StaticAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.StaticAccessExprToken) ArrayMemory(php.runtime.memory.ArrayMemory) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) Memory(php.runtime.Memory) ConstantMemory(php.runtime.memory.helper.ConstantMemory) ObjectMemory(php.runtime.memory.ObjectMemory) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) DocumentComment(php.runtime.reflection.DocumentComment) ParameterEntity(php.runtime.reflection.ParameterEntity) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) ConstantMemory(php.runtime.memory.helper.ConstantMemory)

Example 4 with DocumentComment

use of php.runtime.reflection.DocumentComment in project jphp by jphp-compiler.

the class MethodDumper method save.

@Override
public void save(MethodEntity entity, OutputStream output) throws IOException {
    DumpOutputStream print = new DumpOutputStream(output);
    DocumentComment docComment = entity.getDocComment();
    if (docComment != null) {
        print.writeUTF(docComment.toString());
    } else {
        print.writeUTF("");
    }
    // static
    print.writeBoolean(entity.isStatic());
    // final
    print.writeBoolean(entity.isFinal());
    // abstract
    print.writeBoolean(entity.isAbstract());
    // abstractable
    print.writeBoolean(entity.isAbstractable());
    // immutable
    print.writeBoolean(entity.isImmutable());
    print.writeMemory(entity.getImmutableResult());
    print.writeBoolean(entity.isEmpty());
    // ref
    print.writeBoolean(entity.isReturnReference());
    // type hinting.
    print.writeEnum(entity.getReturnType());
    print.writeName(entity.getReturnTypeClass());
    print.writeBoolean(entity.isReturnTypeNullable());
    // uses stack trace
    print.writeBoolean(entity.isUsesStackTrace());
    // modifier
    print.writeEnum(entity.getModifier());
    // name
    print.writeName(entity.getName());
    print.writeName(entity.getInternalName());
    // trace
    print.writeTrace(debugInformation ? entity.getTrace() : null);
    print.writeInt(entity.getParameters() == null ? 0 : entity.getParameters().length);
    if (entity.getParameters() != null)
        for (ParameterEntity param : entity.getParameters()) {
            parameterDumper.save(param, output);
        }
    // raw data
    print.writeRawData(null);
}
Also used : DocumentComment(php.runtime.reflection.DocumentComment) ParameterEntity(php.runtime.reflection.ParameterEntity) DumpOutputStream(php.runtime.loader.dump.io.DumpOutputStream)

Example 5 with DocumentComment

use of php.runtime.reflection.DocumentComment in project jphp by jphp-compiler.

the class ConstantDumper method load.

@Override
public ConstantEntity load(InputStream input) throws IOException {
    DumpInputStream dump = new DumpInputStream(input);
    String docComment = dump.readUTF();
    boolean cs = dump.readBoolean();
    Modifier modifier = dump.readModifier();
    String name = dump.readName();
    TraceInfo trace = dump.readTrace(context);
    Memory value = dump.readMemory();
    ConstantEntity entity = new ConstantEntity(name, value, cs);
    entity.setContext(context);
    entity.setTrace(trace);
    entity.setModifier(modifier);
    if (!docComment.isEmpty()) {
        entity.setDocComment(new DocumentComment(docComment));
    }
    dump.readRawData();
    return entity;
}
Also used : DumpInputStream(php.runtime.loader.dump.io.DumpInputStream) DocumentComment(php.runtime.reflection.DocumentComment) Memory(php.runtime.Memory) TraceInfo(php.runtime.env.TraceInfo) Modifier(php.runtime.common.Modifier) ConstantEntity(php.runtime.reflection.ConstantEntity)

Aggregations

DocumentComment (php.runtime.reflection.DocumentComment)7 DumpInputStream (php.runtime.loader.dump.io.DumpInputStream)3 DumpOutputStream (php.runtime.loader.dump.io.DumpOutputStream)3 ParameterEntity (php.runtime.reflection.ParameterEntity)3 Memory (php.runtime.Memory)2 TokenMeta (org.develnext.jphp.core.tokenizer.TokenMeta)1 NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)1 StaticAccessExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.StaticAccessExprToken)1 Modifier (php.runtime.common.Modifier)1 TraceInfo (php.runtime.env.TraceInfo)1 DumpException (php.runtime.loader.dump.io.DumpException)1 ArrayMemory (php.runtime.memory.ArrayMemory)1 ObjectMemory (php.runtime.memory.ObjectMemory)1 ClassConstantMemory (php.runtime.memory.helper.ClassConstantMemory)1 ConstantMemory (php.runtime.memory.helper.ConstantMemory)1 ConstantEntity (php.runtime.reflection.ConstantEntity)1 MethodEntity (php.runtime.reflection.MethodEntity)1 PropertyEntity (php.runtime.reflection.PropertyEntity)1