use of dyvilx.tools.compiler.ast.expression.access.InitializerCall in project Dyvil by Dyvil.
the class ConstructorParser method parse.
@Override
public void parse(IParserManager pm, IToken token) {
final int type = token.type();
switch(this.mode) {
case DECLARATOR:
switch(type) {
case DyvilSymbols.AT:
this.parseAnnotation(pm, token);
return;
case DyvilKeywords.INIT:
this.member = this.consumer.createConstructor(token.raw(), this.attributes);
this.mode = PARAMETERS;
return;
}
if (this.parseModifier(pm, token)) {
return;
}
// Fallthrough
case PARAMETERS:
this.mode = PARAMETERS_END;
if (type == BaseSymbols.OPEN_PARENTHESIS) {
pm.pushParser(new ParameterListParser(this.member));
return;
}
pm.reparse();
pm.report(token, "constructor.parameters.open_paren");
return;
case PARAMETERS_END:
this.mode = INITIALIZER;
if (type != BaseSymbols.CLOSE_PARENTHESIS) {
pm.reparse();
pm.report(token, "constructor.parameters.close_paren");
}
return;
case INITIALIZER:
if (type == BaseSymbols.COLON) {
this.mode = INIT_TYPE;
return;
}
// Fallthrough
case EXCEPTIONS:
if (type == DyvilKeywords.THROWS) {
pm.pushParser(new TypeListParser(this.member.getExceptions()));
this.mode = BODY;
return;
}
// Fallthrough
case BODY:
switch(type) {
case BaseSymbols.OPEN_CURLY_BRACKET:
pm.pushParser(new StatementListParser(this.member), true);
this.mode = END;
return;
case BaseSymbols.EQUALS:
pm.pushParser(new ExpressionParser(this.member));
this.mode = END;
return;
}
// Fallthrough
case END:
this.consumer.addConstructor(this.member);
pm.popParser(type != Tokens.EOF);
return;
case INIT_TYPE:
boolean isSuper = false;
switch(type) {
case DyvilKeywords.SUPER:
isSuper = true;
// Fallthrough
case DyvilKeywords.THIS:
final InitializerCall init = new InitializerCall(token.raw(), isSuper);
this.member.setInitializer(init);
this.mode = INIT_ARGUMENTS;
return;
}
pm.report(token, "initializer.call.type");
return;
case INIT_ARGUMENTS:
if (type == BaseSymbols.OPEN_PARENTHESIS) {
pm.pushParser(new ArgumentListParser(this.member.getInitializer()));
this.mode = INIT_END;
return;
}
pm.report(token, "initializer.call.open_paren");
this.mode = EXCEPTIONS;
return;
case INIT_END:
if (type != BaseSymbols.CLOSE_PARENTHESIS) {
pm.report(token, "initializer.call.close_paren");
return;
}
this.mode = EXCEPTIONS;
return;
}
}
use of dyvilx.tools.compiler.ast.expression.access.InitializerCall in project Dyvil by Dyvil.
the class ClassMetadata method createDefaultConstructor.
private CodeConstructor createDefaultConstructor() {
// init(classParams...)
final SourcePosition position = this.theClass.position();
final AttributeList attributes = this.theClass.getConstructorAttributes();
attributes.addFlag(Modifiers.GENERATED);
final CodeConstructor constructor = new CodeConstructor(this.theClass, attributes);
constructor.setPosition(position);
this.copyClassParameters(constructor);
// : super(superParams...)
final IType superType = this.theClass.getSuperType();
if (superType != null) {
// Generate the constructor body
ArgumentList arguments = this.theClass.getSuperConstructorArguments();
if (arguments == null) {
arguments = ArgumentList.empty();
}
final InitializerCall init = new InitializerCall(this.theClass.getPosition(), true, arguments, superType);
constructor.setInitializer(init);
}
// { this.classParams... = classParams... }
final ParameterList classParams = this.theClass.getParameters();
final StatementList ctorBody = new StatementList();
final ParameterList ctorParams = constructor.getParameters();
// j is the counter for class parameters, as there may be leading synthetic constructor parameters
for (int i = 0, j = 0, count = ctorParams.size(); i < count; i++) {
final IParameter ctorParam = ctorParams.get(i);
if (ctorParam.hasModifier(Modifiers.SYNTHETIC)) {
continue;
}
final IParameter classParam = classParams.get(j++);
if (classParam.hasModifier(Modifiers.OVERRIDE)) {
continue;
}
final IValue receiver = new ThisExpr(this.theClass);
final FieldAccess access = new FieldAccess(ctorParam);
final FieldAssignment assignment = new FieldAssignment(position, receiver, classParam, access);
ctorBody.add(assignment);
}
constructor.setValue(ctorBody);
return constructor;
}
use of dyvilx.tools.compiler.ast.expression.access.InitializerCall in project Dyvil by Dyvil.
the class CodeConstructor method resolveInitCall.
private void resolveInitCall(MarkerList markers, IContext context) {
if (this.initializerCall != null) {
this.initializerCall.resolve(markers, context);
return;
}
// No Super Type -> don't try to resolve a Super Constructor
final IType superType = this.enclosingClass.getSuperType();
if (superType == null) {
return;
}
// Implicit Super Constructor
final IConstructor match = IContext.resolveConstructor(context, superType, ArgumentList.EMPTY);
if (match == null) {
markers.add(Markers.semanticError(this.position, "constructor.super"));
return;
}
this.initializerCall = new InitializerCall(this.position, true, ArgumentList.EMPTY, superType, match);
}
use of dyvilx.tools.compiler.ast.expression.access.InitializerCall in project Dyvil by Dyvil.
the class AbstractConstructor method toString.
@Override
public void toString(@NonNull String indent, @NonNull StringBuilder buffer) {
super.toString(indent, buffer);
buffer.append("init");
this.parameters.toString(indent, buffer);
final InitializerCall init = this.getInitializer();
if (init != null) {
Formatting.appendSeparator(buffer, "initializer.call.colon", ':');
init.toString(indent, buffer);
}
if (this.exceptions != null && this.exceptions.size() > 0) {
String throwsPrefix = indent;
if (Formatting.getBoolean("constructor.throws.newline")) {
throwsPrefix = Formatting.getIndent("constructor.throws.indent", indent);
buffer.append('\n').append(throwsPrefix).append("throws ");
} else {
buffer.append(" throws ");
}
Util.astToString(throwsPrefix, this.exceptions.getTypes(), this.exceptions.size(), Formatting.getSeparator("constructor.throws", ','), buffer);
}
final IValue value = this.getValue();
if (value != null && !Util.formatStatementList(indent, buffer, value)) {
buffer.append(" = ");
value.toString(indent, buffer);
}
if (Formatting.getBoolean("constructor.semicolon")) {
buffer.append(';');
}
}
Aggregations