use of org.develnext.jphp.core.tokenizer.token.stmt.ClassVarStmtToken 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.stmt.ClassVarStmtToken in project jphp by jphp-compiler.
the class ClassStmtCompiler method writeConstructor.
@SuppressWarnings("unchecked")
protected void writeConstructor() {
MethodNode constructor = new MethodNodeImpl();
constructor.name = Constants.INIT_METHOD;
constructor.access = ACC_PUBLIC;
constructor.exceptions = new ArrayList();
MethodStmtCompiler methodCompiler = new MethodStmtCompiler(this, constructor);
ExpressionStmtCompiler expressionCompiler = new ExpressionStmtCompiler(methodCompiler, null);
methodCompiler.writeHeader();
LabelNode l0 = writeLabel(constructor, statement.getMeta().getStartLine());
methodCompiler.addLocalVariable("~this", l0);
if (isClosure() || generatorEntity != null) {
constructor.desc = Type.getMethodDescriptor(Type.getType(void.class), Type.getType(Environment.class), Type.getType(ClassEntity.class), Type.getType(Memory.class), Type.getType(Memory[].class));
if (isClosure()) {
constructor.desc = Type.getMethodDescriptor(Type.getType(void.class), Type.getType(Environment.class), Type.getType(ClassEntity.class), Type.getType(Memory.class), Type.getType(String.class), Type.getType(Memory[].class));
}
methodCompiler.addLocalVariable("~env", l0, Environment.class);
methodCompiler.addLocalVariable("~class", l0, ClassEntity.class);
methodCompiler.addLocalVariable("~self", l0, Memory.class);
if (isClosure()) {
methodCompiler.addLocalVariable("~context", l0, String.class);
}
methodCompiler.addLocalVariable("~uses", l0, Memory[].class);
methodCompiler.writeHeader();
expressionCompiler.writeVarLoad("~this");
expressionCompiler.writeVarLoad("~env");
expressionCompiler.writeVarLoad("~class");
expressionCompiler.writeVarLoad("~self");
if (isClosure()) {
expressionCompiler.writeVarLoad("~context");
}
expressionCompiler.writeVarLoad("~uses");
constructor.instructions.add(new MethodInsnNode(INVOKESPECIAL, node.superName, Constants.INIT_METHOD, constructor.desc, false));
} else {
constructor.desc = Type.getMethodDescriptor(Type.getType(void.class), Type.getType(Environment.class), Type.getType(ClassEntity.class));
methodCompiler.addLocalVariable("~env", l0, Environment.class);
methodCompiler.addLocalVariable("~class", l0, String.class);
expressionCompiler.writeVarLoad("~this");
expressionCompiler.writeVarLoad("~env");
expressionCompiler.writeVarLoad("~class");
constructor.instructions.add(new MethodInsnNode(INVOKESPECIAL, node.superName, Constants.INIT_METHOD, constructor.desc, false));
// PROPERTIES
for (ClassVarStmtToken property : statement.getProperties()) {
ExpressionStmtCompiler expressionStmtCompiler = new ExpressionStmtCompiler(methodCompiler, null);
Memory value = Memory.NULL;
if (property.getValue() != null)
value = expressionStmtCompiler.writeExpression(property.getValue(), true, true, false);
PropertyEntity prop = new PropertyEntity(compiler.getContext());
prop.setName(property.getVariable().getName());
prop.setModifier(property.getModifier());
prop.setStatic(property.isStatic());
prop.setDefaultValue(value);
prop.setDefault(property.getValue() != null);
prop.setTrace(property.toTraceInfo(compiler.getContext()));
if (property.getDocComment() != null) {
prop.setDocComment(new DocumentComment(property.getDocComment().getComment()));
}
ClassEntity.PropertyResult result = entity.addProperty(prop);
result.check(compiler.getEnvironment());
if (value == null && property.getValue() != null) {
if (property.getValue().isSingle() && ValueExprToken.isConstable(property.getValue().getSingle(), true))
dynamicProperties.add(property);
else
compiler.getEnvironment().error(property.getVariable().toTraceInfo(compiler.getContext()), ErrorType.E_COMPILE_ERROR, Messages.ERR_EXPECTED_CONST_VALUE.fetch(entity.getName() + "::$" + property.getVariable().getName()));
}
}
}
methodCompiler.writeFooter();
constructor.instructions.add(new InsnNode(RETURN));
node.methods.add(constructor);
}
use of org.develnext.jphp.core.tokenizer.token.stmt.ClassVarStmtToken in project jphp by jphp-compiler.
the class ClassDescription method parse.
@Override
protected void parse() {
methods = new LinkedHashMap<String, MethodDescription>();
for (MethodStmtToken el : token.getMethods()) {
methods.put(el.getName().getName().toLowerCase(), new MethodDescription(el));
}
properties = new LinkedHashMap<String, PropertyDescription>();
for (ClassVarStmtToken el : token.getProperties()) {
properties.put(el.getVariable().getName(), new PropertyDescription(el));
}
constants = new LinkedHashMap<String, ConstantDescription>();
for (ConstStmtToken el : token.getConstants()) {
constants.put(el.items.get(0).getFulledName(), new ConstantDescription(el));
}
if (token.getDocComment() != null) {
DocAnnotations annotations = new DocAnnotations(token.getDocComment().getComment());
description = annotations.getDescription();
}
}
Aggregations