use of com.redhat.ceylon.model.typechecker.model.Function in project ceylon-compiler by ceylon.
the class ClassTransformer method makeMethodForFunctionalParameter.
/**
* Generate a method for a shared FunctionalParameter which delegates to the Callable
* @param klass
* @param annotations */
private void makeMethodForFunctionalParameter(ClassDefinitionBuilder classBuilder, Tree.Parameter paramTree, Tree.TypedDeclaration memberDecl) {
Parameter paramModel = paramTree.getParameterModel();
if (Strategy.createMethod(paramModel)) {
Tree.MethodDeclaration methodDecl = (Tree.MethodDeclaration) memberDecl;
makeFieldForParameter(classBuilder, paramModel, memberDecl);
Function method = (Function) paramModel.getModel();
java.util.List<Parameter> parameters = method.getFirstParameterList().getParameters();
CallBuilder callBuilder = CallBuilder.instance(this).invoke(naming.makeQualIdent(naming.makeName(method, Naming.NA_IDENT), Naming.getCallableMethodName(method)));
for (Parameter parameter : parameters) {
JCExpression parameterExpr = naming.makeName(parameter.getModel(), Naming.NA_IDENT);
parameterExpr = expressionGen().applyErasureAndBoxing(parameterExpr, parameter.getType(), !CodegenUtil.isUnBoxed(parameter.getModel()), BoxingStrategy.BOXED, parameter.getType());
callBuilder.argument(parameterExpr);
}
JCExpression expr = callBuilder.build();
JCStatement body;
if (isVoid(memberDecl) && Decl.isUnboxedVoid(method) && !Strategy.useBoxedVoid(method)) {
body = make().Exec(expr);
} else {
expr = expressionGen().applyErasureAndBoxing(expr, paramModel.getType(), true, CodegenUtil.getBoxingStrategy(method), paramModel.getType());
body = make().Return(expr);
}
classBuilder.methods(transformMethod(method, null, methodDecl, methodDecl.getParameterLists(), methodDecl, true, method.isActual(), true, List.of(body), new DaoThis(methodDecl, methodDecl.getParameterLists().get(0)), false));
}
}
use of com.redhat.ceylon.model.typechecker.model.Function in project ceylon-compiler by ceylon.
the class ClassTransformer method transform.
List<MethodDefinitionBuilder> transform(Tree.AnyMethod def, ClassDefinitionBuilder classBuilder, List<JCStatement> body) {
final Function model = def.getDeclarationModel();
List<MethodDefinitionBuilder> result = List.<MethodDefinitionBuilder>nil();
if (!Decl.withinInterface(model)) {
// Transform to the class
boolean refinedResultType = !model.getType().isExactly(((TypedDeclaration) model.getRefinedDeclaration()).getType());
result = transformMethod(def, true, true, true, transformMplBodyUnlessSpecifier(def, model, body), refinedResultType && !Decl.withinInterface(model.getRefinedDeclaration()) ? new DaoSuper() : new DaoThis(def, def.getParameterLists().get(0)), !Strategy.defaultParameterMethodOnSelf(model));
} else {
// Is within interface
// Transform the definition to the companion class, how depends
// on what kind of method it is
List<MethodDefinitionBuilder> companionDefs;
if (def instanceof Tree.MethodDeclaration) {
final SpecifierExpression specifier = ((Tree.MethodDeclaration) def).getSpecifierExpression();
if (specifier == null) {
// formal or abstract
// (still need overloads and DPMs on the companion)
companionDefs = transformMethod(def, false, true, true, null, new DaoCompanion(def, def.getParameterLists().get(0)), false);
} else {
companionDefs = transformMethod(def, true, false, !model.isShared(), transformMplBodyUnlessSpecifier(def, model, body), new DaoCompanion(def, def.getParameterLists().get(0)), false);
}
} else if (def instanceof Tree.MethodDefinition) {
companionDefs = transformMethod(def, true, false, !model.isShared(), transformMplBodyUnlessSpecifier(def, model, body), new DaoCompanion(def, def.getParameterLists().get(0)), false);
} else {
throw BugException.unhandledNodeCase(def);
}
if (!companionDefs.isEmpty())
classBuilder.getCompanionBuilder((TypeDeclaration) model.getContainer()).methods(companionDefs);
// but only if it's shared
if (Decl.isShared(model)) {
result = transformMethod(def, true, true, true, null, daoAbstract, !Strategy.defaultParameterMethodOnSelf(model));
}
}
return result;
}
use of com.redhat.ceylon.model.typechecker.model.Function in project ceylon-compiler by ceylon.
the class ClassTransformer method transformRefinementSpecifierStatement.
public List<JCStatement> transformRefinementSpecifierStatement(SpecifierStatement op, ClassDefinitionBuilder classBuilder) {
List<JCStatement> result = List.<JCStatement>nil();
// Check if this is a shortcut form of formal attribute refinement
if (op.getRefinement()) {
Tree.Term baseMemberTerm = op.getBaseMemberExpression();
if (baseMemberTerm instanceof Tree.ParameterizedExpression)
baseMemberTerm = ((Tree.ParameterizedExpression) baseMemberTerm).getPrimary();
Tree.BaseMemberExpression expr = (BaseMemberExpression) baseMemberTerm;
Declaration decl = expr.getDeclaration();
if (Decl.isValue(decl) || Decl.isGetter(decl)) {
// Now build a "fake" declaration for the attribute
Tree.AttributeDeclaration attrDecl = new Tree.AttributeDeclaration(null);
attrDecl.setDeclarationModel((Value) decl);
attrDecl.setIdentifier(expr.getIdentifier());
attrDecl.setScope(op.getScope());
attrDecl.setSpecifierOrInitializerExpression(op.getSpecifierExpression());
attrDecl.setAnnotationList(makeShortcutRefinementAnnotationTrees());
// Make sure the boxing information is set correctly
BoxingDeclarationVisitor v = new CompilerBoxingDeclarationVisitor(this);
v.visit(attrDecl);
// Generate the attribute
transform(attrDecl, classBuilder);
} else if (decl instanceof Function) {
// Now build a "fake" declaration for the method
Tree.MethodDeclaration methDecl = new Tree.MethodDeclaration(null);
Function m = (Function) decl;
methDecl.setDeclarationModel(m);
methDecl.setIdentifier(expr.getIdentifier());
methDecl.setScope(op.getScope());
methDecl.setAnnotationList(makeShortcutRefinementAnnotationTrees());
Tree.SpecifierExpression specifierExpression = op.getSpecifierExpression();
methDecl.setSpecifierExpression(specifierExpression);
if (specifierExpression instanceof Tree.LazySpecifierExpression == false) {
Tree.Expression expression = specifierExpression.getExpression();
Tree.Term expressionTerm = Decl.unwrapExpressionsUntilTerm(expression);
// we can optimise lambdas and static method calls
if (!CodegenUtil.canOptimiseMethodSpecifier(expressionTerm, m)) {
// we need a field to save the callable value
String name = naming.getMethodSpecifierAttributeName(m);
JCExpression specifierType = makeJavaType(expression.getTypeModel());
JCExpression specifier = expressionGen().transformExpression(expression);
classBuilder.field(PRIVATE | FINAL, name, specifierType, specifier, false);
}
}
java.util.List<Tree.ParameterList> parameterListTrees = null;
if (op.getBaseMemberExpression() instanceof Tree.ParameterizedExpression) {
parameterListTrees = new ArrayList<>(m.getParameterLists().size());
parameterListTrees.addAll(((Tree.ParameterizedExpression) op.getBaseMemberExpression()).getParameterLists());
Tree.Term term = specifierExpression.getExpression().getTerm();
// and give it the given block of expr as it's specifier
while (term instanceof Tree.FunctionArgument && m.getParameterLists().size() > 1) {
FunctionArgument functionArgument = (Tree.FunctionArgument) term;
specifierExpression.setExpression(functionArgument.getExpression());
parameterListTrees.addAll(functionArgument.getParameterLists());
term = functionArgument.getExpression().getTerm();
}
}
int plIndex = 0;
// copy from formal declaration
for (ParameterList pl : m.getParameterLists()) {
Tree.ParameterList parameterListTree = null;
if (parameterListTrees != null)
parameterListTree = parameterListTrees.get(plIndex++);
Tree.ParameterList tpl = new Tree.ParameterList(null);
tpl.setModel(pl);
int pIndex = 0;
for (Parameter p : pl.getParameters()) {
Tree.Parameter parameterTree = null;
if (parameterListTree != null)
parameterTree = parameterListTree.getParameters().get(pIndex++);
Tree.Parameter tp = null;
if (p.getModel() instanceof Value) {
Tree.ValueParameterDeclaration tvpd = new Tree.ValueParameterDeclaration(null);
if (parameterTree != null)
tvpd.setTypedDeclaration(((Tree.ParameterDeclaration) parameterTree).getTypedDeclaration());
tvpd.setParameterModel(p);
tp = tvpd;
} else if (p.getModel() instanceof Function) {
Tree.FunctionalParameterDeclaration tfpd = new Tree.FunctionalParameterDeclaration(null);
if (parameterTree != null)
tfpd.setTypedDeclaration(((Tree.ParameterDeclaration) parameterTree).getTypedDeclaration());
tfpd.setParameterModel(p);
tp = tfpd;
} else {
throw BugException.unhandledDeclarationCase(p.getModel());
}
tp.setScope(p.getDeclaration().getContainer());
//tp.setIdentifier(makeIdentifier(p.getName()));
tpl.addParameter(tp);
}
methDecl.addParameterList(tpl);
}
// Make sure the boxing information is set correctly
BoxingDeclarationVisitor v = new CompilerBoxingDeclarationVisitor(this);
v.visit(methDecl);
// Generate the method
classBuilder.method(methDecl, Errors.GENERATE);
}
} else {
// Normal case, just generate the specifier statement
result = result.append(expressionGen().transform(op));
}
Tree.Term term = op.getBaseMemberExpression();
if (term instanceof Tree.BaseMemberExpression) {
Tree.BaseMemberExpression bme = (Tree.BaseMemberExpression) term;
DeferredSpecification ds = statementGen().getDeferredSpecification(bme.getDeclaration());
if (ds != null && needsInnerSubstitution(term.getScope(), bme.getDeclaration())) {
result = result.append(ds.openInnerSubstitution());
}
}
return result;
}
use of com.redhat.ceylon.model.typechecker.model.Function in project ceylon-compiler by ceylon.
the class ClassTransformer method serializationSet.
private void serializationSet(Class model, ClassDefinitionBuilder classBuilder) {
MethodDefinitionBuilder mdb = MethodDefinitionBuilder.systemMethod(this, Unfix.$set$.toString());
mdb.isOverride(true);
mdb.ignoreModelAnnotations();
mdb.modifiers(PUBLIC);
ParameterDefinitionBuilder pdb = ParameterDefinitionBuilder.systemParameter(this, Unfix.reference.toString());
pdb.modifiers(FINAL);
pdb.type(make().Type(syms().ceylonReachableReferenceType), null);
mdb.parameter(pdb);
ParameterDefinitionBuilder pdb2 = ParameterDefinitionBuilder.systemParameter(this, Unfix.instance.toString());
pdb2.modifiers(FINAL);
pdb2.type(make().Type(syms().objectType), null);
mdb.parameter(pdb2);
//mdb.resultType(null, naming.makeQuotedFQIdent("java.util.Collection"));
/*
* public void $set$(Object reference, Object instance) {
* switch((String)reference) {
* case ("attr1")
* this.field1 = ...;
* break;
* // ... other fields of this class
* default:
* super.set(reference, instance);
*/
SyntheticName reference = naming.synthetic(Unfix.reference);
SyntheticName instance = naming.synthetic(Unfix.instance);
ListBuffer<JCCase> cases = ListBuffer.<JCCase>lb();
boolean[] needsLookup = new boolean[] { false };
for (Declaration member : model.getMembers()) {
if (hasField(member)) {
if (member instanceof Function)
// TODO: This class is not serializable
continue;
ListBuffer<JCStatement> caseStmts = ListBuffer.<JCStatement>lb();
if (member instanceof Value && ((Value) member).isLate()) {
caseStmts.add(make().If(make().TypeTest(instance.makeIdent(), make().Type(syms().ceylonUninitializedLateValueType)), make().Break(null), null));
}
caseStmts.add(makeDeserializationAssignment((Value) member, needsLookup));
caseStmts.add(make().Break(null));
cases.add(make().Case(make().Literal(member.getQualifiedNameString()), caseStmts.toList()));
}
}
ListBuffer<JCStatement> defaultCase = ListBuffer.lb();
if (extendsSerializable(model)) {
// super.set(reference, instance);
defaultCase.add(make().Exec(make().Apply(null, naming.makeQualIdent(naming.makeSuper(), Unfix.$set$.toString()), List.<JCExpression>of(reference.makeIdent(), instance.makeIdent()))));
} else {
// throw (or pass to something else to throw, based on policy)
defaultCase.add(make().Throw(make().NewClass(null, null, naming.makeQuotedFQIdent("java.lang.RuntimeException"), List.<JCExpression>of(make().Literal("unknown attribute")), null)));
}
cases.add(make().Case(null, defaultCase.toList()));
ListBuffer<JCStatement> stmts = ListBuffer.<JCStatement>lb();
if (needsLookup[0]) {
// if we needed to use a lookup object to reset final fields,
// prepend that variable
stmts.add(makeVar(FINAL, "lookup", naming.makeQualIdent(make().Type(syms().methodHandlesType), "Lookup"), make().Apply(null, naming.makeQuotedFQIdent("java.lang.invoke.MethodHandles.lookup"), List.<JCExpression>nil())));
}
JCSwitch swtch = make().Switch(make().Apply(null, naming.makeSelect(make().Apply(null, naming.makeSelect(make().TypeCast(make().Type(syms().ceylonMemberType), reference.makeIdent()), "getAttribute"), List.<JCExpression>nil()), "getQualifiedName"), List.<JCExpression>nil()), cases.toList());
stmts.add(make().If(make().TypeTest(reference.makeIdent(), make().Type(syms().ceylonMemberType)), swtch, make().Throw(make().NewClass(null, null, make().Type(syms().ceylonAssertionErrorType), List.<JCExpression>of(make().Binary(JCTree.PLUS, make().Literal("unexpected reachable reference "), reference.makeIdent())), null))));
mdb.body(stmts.toList());
classBuilder.method(mdb);
}
use of com.redhat.ceylon.model.typechecker.model.Function in project ceylon-compiler by ceylon.
the class ClassTransformer method transformWrappedMethod.
public List<JCTree> transformWrappedMethod(Tree.AnyMethod def, TransformationPlan plan) {
final Function model = def.getDeclarationModel();
if (model.isParameter()) {
return List.nil();
}
naming.clearSubstitutions(model);
// Generate a wrapper class for the method
String name = def.getIdentifier().getText();
ClassDefinitionBuilder builder = ClassDefinitionBuilder.methodWrapper(this, name, Decl.isShared(def));
if (Decl.isAnnotationConstructor(def)) {
AnnotationInvocation ai = ((AnnotationInvocation) def.getDeclarationModel().getAnnotationConstructor());
if (ai != null) {
builder.annotations(List.of(makeAtAnnotationInstantiation(ai)));
builder.annotations(makeExprAnnotations(def, ai));
}
}
builder.methods(classGen().transform(def, plan, builder));
// Toplevel method
if (Strategy.generateMain(def)) {
// Add a main() method
builder.method(makeMainForFunction(model));
}
if (Decl.isLocal(model) || Decl.isToplevel(model)) {
builder.annotations(makeAtLocalDeclarations(def));
}
if (Decl.isLocal(model)) {
builder.annotations(makeAtLocalDeclaration(model.getQualifier(), false));
}
List<JCTree> result = builder.build();
if (Decl.isLocal(def)) {
// Inner method
JCVariableDecl call = at(def).VarDef(make().Modifiers(FINAL), naming.getSyntheticInstanceName(model), naming.makeSyntheticClassname(model), makeSyntheticInstance(model));
result = result.append(call);
}
//}
return result;
}
Aggregations