use of dyvilx.tools.compiler.ast.expression.access.FieldAccess in project Dyvil by Dyvil.
the class CaseClassMetadata method createUnapplyAnyMethod.
private CodeMethod createUnapplyAnyMethod() {
// static final func unapply<TypeParams...>(value: any) -> (T...)?
final SourcePosition position = this.theClass.position();
final AttributeList attributes = AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC_FINAL | Modifiers.GENERATED);
final IType type = NullableType.apply(this.getUnapplyReturnType());
final CodeMethod unapply = new CodeMethod(this.theClass, Names.unapply, type, attributes);
unapply.setPosition(position);
unapply.getTypeParameters().addAll(this.theClass.getTypeParameters());
final CodeParameter parameter = new CodeParameter(unapply, position, Names.value, Types.NULLABLE_ANY);
unapply.getParameters().add(parameter);
// = (param is This) ? unapply(param as This) : null
final InstanceOfOperator isOperator = new InstanceOfOperator(new FieldAccess(parameter), this.theClass.getClassType());
final CastOperator castOperator = new CastOperator(new FieldAccess(parameter), this.theClass.getThisType());
final IValue call = new MethodCall(position, null, Names.unapply, new ArgumentList(castOperator));
final IfStatement ifStatement = new IfStatement(isOperator, call, new NullValue());
unapply.setValue(ifStatement);
return unapply;
}
use of dyvilx.tools.compiler.ast.expression.access.FieldAccess in project Dyvil by Dyvil.
the class BraceAccessExpr method resolve.
@Override
public IValue resolve(MarkerList markers, IContext context) {
if (this.value != null) {
this.value = this.value.resolve(markers, context);
} else {
this.value = context.resolveImplicit(null);
}
if (this.value == null) {
markers.add(Markers.semanticError(this.position, "braceaccess.invalid"));
} else {
final IType valueType = this.value.getType();
final IValue typedValue = this.value.withType(valueType, valueType, markers, context);
if (typedValue != null) {
this.value = typedValue;
}
this.variable = new Variable(Names.$0, this.value.getType());
this.implicitAccess = new FieldAccess(this.variable);
}
context = context.push(this);
this.statement = this.statement.resolve(markers, context);
context.pop();
return this;
}
use of dyvilx.tools.compiler.ast.expression.access.FieldAccess in project Dyvil by Dyvil.
the class SideEffectHelper method processValue.
public IValue processValue(IValue value) {
if (value == null || !value.hasSideEffects()) {
return value;
}
if (this.statementList == null) {
this.statementList = new StatementList();
}
final Variable variable = new Variable(value.getPosition(), Name.fromRaw("sideEffect$" + this.registered), value.getType());
variable.setValue(value);
this.statementList.add(new VariableStatement(variable));
this.statementList.addVariable(variable);
this.registered++;
return new FieldAccess(value.getPosition(), null, variable);
}
use of dyvilx.tools.compiler.ast.expression.access.FieldAccess in project Dyvil by Dyvil.
the class FuncDirective method convertBlock.
protected static StatementList convertBlock(StatementList block) {
final StatementList value = new StatementList();
// new StringWriter()
final ConstructorCall newStringWriter = new ConstructorCall(null, Template.LazyTypes.StringWriter, ArgumentList.EMPTY);
// let writer = new StringWriter()
final Variable writer = new Variable(Name.fromRaw("writer"), Template.LazyTypes.Writer, newStringWriter);
writer.getAttributes().addFlag(Modifiers.FINAL | Modifiers.GENERATED);
// { let writer = new StringWriter; { ... }; writer.toString }
value.add(new VariableStatement(writer));
value.add(block);
value.add(new MethodCall(null, new FieldAccess(writer), Names.toString));
return value;
}
use of dyvilx.tools.compiler.ast.expression.access.FieldAccess in project Dyvil by Dyvil.
the class OptionalChainAware method transform.
static IValue transform(OptionalChainAware oca) {
// oca = receiver?.access
// <- oco ->
final IValue oco = oca.getReceiver();
if (oco == null || oco.valueTag() != IValue.OPTIONAL_CHAIN) {
// no transformation needed as it is actually not an optional chain
return oca;
}
final SourcePosition position = oca.getPosition();
final IValue receiver = ((OptionalChainOperator) oco).getReceiver();
// receiver is now the actual receiver of the optional chain operator
BindingIfStatement bindingIf;
if (receiver instanceof BindingIfStatement && (bindingIf = (BindingIfStatement) receiver).getElse() == NullValue.NULL) {
// safe bet that the receiver used to be an optional chain
// Perform the following transformation (the entire statement is the receiver):
// if let $0 = oldReceiver { $0.oldAccess } else null
// becomes
// if (let $0 = oldReceiver, let $1 = $0.oldAccess) { $1.access } else null
final Variable var = newVar(position, bindingIf.getThen());
bindingIf.addVariable(var);
oca.setReceiver(new FieldAccess(var));
bindingIf.setThen(oca);
return bindingIf;
}
// oca = receiver?.access, and receiver is not an optional chain
// receiver?.access
// becomes
// if let $0 = receiver { $0.access } else null
final Variable var = newVar(position, receiver);
bindingIf = new BindingIfStatement(position);
bindingIf.addVariable(var);
oca.setReceiver(new FieldAccess(var));
bindingIf.setThen(oca);
bindingIf.setElse(NullValue.NULL);
return bindingIf;
}
Aggregations