use of org.develnext.jphp.core.tokenizer.token.expr.value.NameToken in project jphp by jphp-compiler.
the class InstanceOfCompiler method pushName.
protected void pushName(StackItem R) {
NameToken name = null;
if (R.getToken() instanceof NameToken)
name = this.compiler.getAnalyzer().getRealName((NameToken) R.getToken());
if (name == null) {
expr.writePush(R);
expr.writePopString();
expr.writePushDupLowerCase();
} else {
expr.writePushConstString(name.getName());
expr.writePushConstString(name.getName().toLowerCase());
}
}
use of org.develnext.jphp.core.tokenizer.token.expr.value.NameToken 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 (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.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);
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 (statement != null && statement.getBody() != null) {
expr.writeDefineVariables(statement.getLocal());
expr.write(statement.getBody());
if (!statement.getBody().getInstructions().isEmpty()) {
entity.setEmpty(false);
}
}
if (generatorEntity != null) {
expr.writeVarLoad("~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);
expr.getCompiler(ReturnStmtToken.class).write(token);
}
writeFooter();
}
return entity;
}
use of org.develnext.jphp.core.tokenizer.token.expr.value.NameToken in project jphp by jphp-compiler.
the class SyntaxAnalyzer method getRealName.
public static FulledNameToken getRealName(NameToken what, NamespaceStmtToken namespace, NamespaceUseStmtToken.UseType useType) {
if (what instanceof FulledNameToken) {
FulledNameToken fulledNameToken = (FulledNameToken) what;
if (fulledNameToken.isProcessed(useType)) {
return fulledNameToken;
} else {
if (fulledNameToken.isAnyProcessed()) {
what = (fulledNameToken).getLastName();
}
}
}
String name = what.getName();
// check name in uses
if (namespace != null) {
for (NamespaceUseStmtToken use : namespace.getUses()) {
if (use.getUseType() != useType) {
continue;
}
if (use.getAs() == null) {
String string = use.getName().getLastName().getName();
if ((useType == CONSTANT && name.equals(string)) || (useType != CONSTANT && name.equalsIgnoreCase(string))) {
FulledNameToken t = new FulledNameToken(use.getName());
t.setProcessed(useType);
return t;
}
} else {
String string = use.getAs().getName();
if ((useType == CONSTANT && name.equals(string)) || (useType != CONSTANT && name.equalsIgnoreCase(string))) {
FulledNameToken t = new FulledNameToken(use.getName());
t.setProcessed(useType);
return t;
}
}
}
}
List<NameToken> names = namespace == null || namespace.getName() == null ? new ArrayList<NameToken>() : new ArrayList<NameToken>(namespace.getName().getNames());
if (what instanceof FulledNameToken)
names.addAll(((FulledNameToken) what).getNames());
else
names.add(what);
FulledNameToken t = new FulledNameToken(what.getMeta(), names);
t.setProcessed(useType);
return t;
}
use of org.develnext.jphp.core.tokenizer.token.expr.value.NameToken in project jphp by jphp-compiler.
the class JvmCompiler method compile.
@Override
public ModuleEntity compile(boolean autoRegister) {
this.classes = new ArrayList<>();
module.setInternalName("$php_module_m" + UUID.randomUUID().toString().replace("-", ""));
List<ExprStmtToken> externalCode = process(tokens, NamespaceStmtToken.getDefault());
NamespaceStmtToken namespace = NamespaceStmtToken.getDefault();
ClassStmtToken token = new ClassStmtToken(TokenMeta.of(module.getName()));
token.setFinal(true);
token.setName(new NameToken(TokenMeta.of(module.getInternalName())));
token.setNamespace(namespace);
MethodStmtToken methodToken = new MethodStmtToken(token.getMeta());
methodToken.setFinal(true);
methodToken.setClazz(token);
methodToken.setModifier(Modifier.PUBLIC);
methodToken.setStatic(true);
methodToken.setDynamicLocal(true);
methodToken.setName((NameToken) Token.of("__include"));
methodToken.setArguments(new ArrayList<ArgumentStmtToken>());
methodToken.setLocal(analyzer.getScope().getVariables());
methodToken.setLabels(analyzer.getScope().getLabels());
methodToken.setBody(BodyStmtToken.of(externalCode));
token.setMethods(Arrays.asList(methodToken));
ClassStmtCompiler classStmtCompiler = new ClassStmtCompiler(this, token);
classStmtCompiler.setExternal(true);
classStmtCompiler.setSystem(true);
module.setData(classStmtCompiler.compile().getData());
if (autoRegister)
scope.addUserModule(module);
return module;
}
use of org.develnext.jphp.core.tokenizer.token.expr.value.NameToken in project jphp by jphp-compiler.
the class MethodStmtToken method of.
public static MethodStmtToken of(String name, ClassStmtToken clazz) {
MethodStmtToken mToken = new MethodStmtToken(clazz.getMeta());
mToken.setClazz(clazz);
mToken.setName(new NameToken(TokenMeta.of(name, clazz)));
mToken.setNamespace(null);
return mToken;
}
Aggregations