use of org.eclipse.ceylon.model.typechecker.model.Setter in project ceylon by eclipse.
the class DeclarationVisitor method visit.
@Override
public void visit(Tree.AttributeSetterDefinition that) {
Setter s = new Setter();
s.setImplemented(that.getBlock() != null);
that.setDeclarationModel(s);
visitDeclaration(that, s);
Scope o = enterScope(s);
Parameter p = new Parameter();
p.setHidden(true);
Value v = new Value();
v.setInitializerParameter(p);
p.setModel(v);
v.setName(s.getName());
p.setName(s.getName());
p.setDeclaration(s);
visitElement(that, v);
unit.addDeclaration(v);
Scope sc = getContainer(that);
sc.addMember(v);
s.setParameter(p);
super.visit(that);
exitScope(o);
if (that.getSpecifierExpression() == null && that.getBlock() == null && !isNativeHeader(s) && !isNativeHeader(s.getGetter())) {
that.addError("setter declaration must have a body or => specifier");
}
}
use of org.eclipse.ceylon.model.typechecker.model.Setter in project ceylon by eclipse.
the class ClassTransformer method transform.
public AttributeDefinitionBuilder transform(AttributeSetterDefinition decl, boolean forCompanion) {
Setter model = decl.getDeclarationModel();
if (Strategy.onlyOnCompanion(model) && !forCompanion) {
return null;
}
String name = decl.getIdentifier().getText();
final AttributeDefinitionBuilder builder = AttributeDefinitionBuilder.setter(this, decl, name, model.getGetter()).modifiers(modifierTransformation().getterSetter(model, forCompanion));
// companion class members are never actual no matter what the Declaration says
if (forCompanion)
builder.notActual();
if (model.isClassMember() || forCompanion || model.getGetter().isStatic()) {
JCBlock setterBlock = makeSetterBlock(model, decl.getBlock(), decl.getSpecifierExpression());
builder.setterBlock(setterBlock);
} else {
builder.isFormal(true);
}
builder.userAnnotationsSetter(expressionGen().transformAnnotations(OutputElement.SETTER, decl));
return builder;
}
use of org.eclipse.ceylon.model.typechecker.model.Setter in project ceylon by eclipse.
the class ClassTransformer method needsCompanionDelegate.
private boolean needsCompanionDelegate(final Class model, Reference ref) {
final boolean mostRefined;
Declaration member = ref.getDeclaration();
java.util.List<Type> sig = getSignatureIfRequired(ref);
Declaration m = model.getMember(member.getName(), sig, false, true);
if (member instanceof Setter && Decl.isGetter(m)) {
mostRefined = member.equals(((Value) m).getSetter());
} else {
mostRefined = member.equals(m);
}
return mostRefined && (member.isDefault() || !member.isFormal());
}
use of org.eclipse.ceylon.model.typechecker.model.Setter in project ceylon by eclipse.
the class ClassTransformer method makeDelegateToCompanion.
/**
* Generates a method which delegates to the companion instance $Foo$impl
*/
private MethodDefinitionBuilder makeDelegateToCompanion(Interface iface, Reference typedMember, Type currentType, final long mods, final java.util.List<TypeParameter> typeParameters, final java.util.List<java.util.List<Type>> producedTypeParameterBounds, final Type methodType, final String methodName, final java.util.List<Parameter> parameters, boolean typeErased, final String targetMethodName, Parameter defaultedParam, boolean includeBody) {
final MethodDefinitionBuilder concreteWrapper = MethodDefinitionBuilder.systemMethod(gen(), methodName);
concreteWrapper.modifiers(mods);
concreteWrapper.ignoreModelAnnotations();
if ((mods & PRIVATE) == 0) {
concreteWrapper.isOverride(true);
}
if (typeParameters != null) {
concreteWrapper.reifiedTypeParametersFromModel(typeParameters);
}
Iterator<java.util.List<Type>> iterator = producedTypeParameterBounds.iterator();
if (typeParameters != null) {
for (TypeParameter tp : typeParameters) {
concreteWrapper.typeParameter(tp, iterator.next());
}
}
boolean explicitReturn = false;
Declaration member = (defaultedParam != null ? typedMember.getTypedParameter(defaultedParam) : typedMember).getDeclaration();
Type returnType = null;
if (!isAnything(methodType) || ((member instanceof Function || member instanceof Value) && !Decl.isUnboxedVoid(member)) || (member instanceof Function && Strategy.useBoxedVoid((Function) member))) {
explicitReturn = true;
if (CodegenUtil.isHashAttribute(member)) {
// delegates for hash attributes are int
concreteWrapper.resultType(new TransformedType(make().Type(syms().intType)));
returnType = typedMember.getType();
} else if (typedMember instanceof TypedReference && defaultedParam == null) {
TypedReference typedRef = (TypedReference) typedMember;
// This is very much like for method refinement: if the supertype is erased -> go raw.
// Except for some reason we only need to do it with multiple inheritance with different type
// arguments, so let's not go overboard
int flags = 0;
if (CodegenUtil.hasTypeErased((TypedDeclaration) member.getRefinedDeclaration()) || CodegenUtil.hasTypeErased((TypedDeclaration) member) && isInheritedTwiceWithDifferentTypeArguments(currentType, iface)) {
flags |= AbstractTransformer.JT_RAW;
}
concreteWrapper.resultTypeNonWidening(currentType, typedRef, typedMember.getType(), flags);
// FIXME: this is redundant with what we computed in the previous line in concreteWrapper.resultTypeNonWidening
TypedReference nonWideningTypedRef = gen().nonWideningTypeDecl(typedRef, currentType);
returnType = gen().nonWideningType(typedRef, nonWideningTypedRef);
} else if (defaultedParam != null) {
TypedReference typedParameter = typedMember.getTypedParameter(defaultedParam);
NonWideningParam nonWideningParam = concreteWrapper.getNonWideningParam(typedParameter, currentType.getDeclaration() instanceof Class ? WideningRules.FOR_MIXIN : WideningRules.NONE);
returnType = nonWideningParam.nonWideningType;
if (member instanceof Function)
returnType = typeFact().getCallableType(returnType);
concreteWrapper.resultType(new TransformedType(makeJavaType(returnType, nonWideningParam.flags)));
} else {
concreteWrapper.resultType(new TransformedType(makeJavaType((Type) typedMember)));
returnType = (Type) typedMember;
}
}
ListBuffer<JCExpression> arguments = new ListBuffer<JCExpression>();
if (typeParameters != null) {
for (TypeParameter tp : typeParameters) {
arguments.add(naming.makeUnquotedIdent(naming.getTypeArgumentDescriptorName(tp)));
}
}
Declaration declaration = typedMember.getDeclaration();
if (declaration instanceof Constructor && !Decl.isDefaultConstructor((Constructor) declaration) && defaultedParam == null) {
concreteWrapper.parameter(makeConstructorNameParameter((Constructor) declaration));
arguments.add(naming.makeUnquotedIdent(Unfix.$name$));
}
int ii = 0;
for (Parameter param : parameters) {
Parameter parameter;
if (declaration instanceof Functional) {
parameter = ((Functional) declaration).getFirstParameterList().getParameters().get(ii++);
} else if (declaration instanceof Setter) {
parameter = ((Setter) declaration).getParameter();
} else {
throw BugException.unhandledDeclarationCase(declaration);
}
final TypedReference typedParameter = typedMember.getTypedParameter(parameter);
concreteWrapper.parameter(null, param, typedParameter, null, FINAL, WideningRules.FOR_MIXIN);
arguments.add(naming.makeName(param.getModel(), Naming.NA_MEMBER | Naming.NA_ALIASED));
}
if (includeBody) {
JCExpression qualifierThis = makeUnquotedIdent(getCompanionFieldName(iface));
// our impl accessor to get the expected bounds of the qualifying type
if (explicitReturn) {
Type javaType = getBestSatisfiedType(currentType, iface);
Type ceylonType = typedMember.getQualifyingType();
// don't even bother if the impl accessor is turned to raw because casting it to raw doesn't help
if (!isTurnedToRaw(ceylonType) && // if it's exactly the same we don't need any cast
!javaType.isExactly(ceylonType))
// this will add the proper cast to the impl accessor
qualifierThis = expressionGen().applyErasureAndBoxing(qualifierThis, currentType, false, true, BoxingStrategy.BOXED, ceylonType, ExpressionTransformer.EXPR_WANTS_COMPANION);
}
JCExpression expr = make().Apply(// TODO Type args
null, makeSelect(qualifierThis, (targetMethodName != null) ? targetMethodName : methodName), arguments.toList());
if (isUnimplementedMemberClass(currentType, typedMember)) {
concreteWrapper.body(makeThrowUnresolvedCompilationError(// TODO encapsulate the error message
"formal member '" + declaration.getName() + "' of '" + iface.getName() + "' not implemented in class hierarchy"));
current().broken();
} else if (!explicitReturn) {
concreteWrapper.body(gen().make().Exec(expr));
} else {
// deal with erasure and stuff
BoxingStrategy boxingStrategy;
boolean exprBoxed;
if (member instanceof TypedDeclaration) {
TypedDeclaration typedDecl = (TypedDeclaration) member;
exprBoxed = !CodegenUtil.isUnBoxed(typedDecl);
boxingStrategy = CodegenUtil.getBoxingStrategy(typedDecl);
} else {
// must be a class or interface
exprBoxed = true;
boxingStrategy = BoxingStrategy.UNBOXED;
}
// to force an additional cast
if (isTurnedToRaw(typedMember.getQualifyingType()) || // in invariant locations
needsRawCastForMixinSuperCall(iface, methodType) || needsCastForErasedInstantiator(iface, methodName, member))
typeErased = true;
expr = gen().expressionGen().applyErasureAndBoxing(expr, methodType, typeErased, exprBoxed, boxingStrategy, returnType, 0);
concreteWrapper.body(gen().make().Return(expr));
}
}
return concreteWrapper;
}
use of org.eclipse.ceylon.model.typechecker.model.Setter in project ceylon by eclipse.
the class CeylonVisitor method visit.
public void visit(final Tree.AttributeSetterDefinition decl) {
TransformationPlan plan = gen.errors().hasDeclarationAndMarkBrokenness(decl);
if (plan instanceof Drop) {
return;
}
TransformationPlan getterPlan = gen.errors().hasDeclarationAndMarkBrokenness(getterSetterPairing.getGetter(decl));
if (getterPlan instanceof Drop) {
// because there's little chance we'll be able to generate a correct setter
return;
}
if (!acceptDeclaration(decl))
return;
int annots = gen.checkCompilerAnnotations(decl, defs);
Setter dec = decl.getDeclarationModel();
if (dec.isClassMember() && !ModelUtil.isLocalToInitializer(dec) || dec.getGetter().isStatic()) {
classBuilder.attribute(gen.classGen().transform(decl, false));
} else if (dec.isInterfaceMember()) {
classBuilder.attribute(gen.classGen().transform(decl, false));
AttributeDefinitionBuilder adb = gen.classGen().transform(decl, true);
if (dec.isShared()) {
adb.ignoreAnnotations();
}
classBuilder.getCompanionBuilder((Interface) dec.getContainer()).attribute(adb);
} else if (dec.isToplevel()) {
topattrBuilder.add(decl);
} else {
appendList(gen.transform(decl));
}
gen.resetCompilerAnnotations(annots);
}
Aggregations