use of org.develnext.jphp.core.compiler.jvm.JPHPClassWriter 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;
}
Aggregations