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);
}
}
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);
}
Aggregations