use of com.redhat.ceylon.model.typechecker.model.TypeParameter in project ceylon-compiler by ceylon.
the class ClassTransformer method typeParametersOfAllContainers.
private java.util.List<TypeParameter> typeParametersOfAllContainers(final ClassOrInterface model, boolean includeModelTypeParameters) {
java.util.List<java.util.List<TypeParameter>> r = new ArrayList<java.util.List<TypeParameter>>(1);
Scope s = model.getContainer();
while (!(s instanceof Package)) {
if (s instanceof Generic) {
r.add(0, ((Generic) s).getTypeParameters());
}
s = s.getContainer();
}
Set<String> names = new HashSet<String>();
for (TypeParameter tp : model.getTypeParameters()) {
names.add(tp.getName());
}
java.util.List<TypeParameter> result = new ArrayList<TypeParameter>(1);
for (java.util.List<TypeParameter> tps : r) {
for (TypeParameter tp : tps) {
if (names.add(tp.getName())) {
result.add(tp);
}
}
}
if (includeModelTypeParameters)
result.addAll(model.getTypeParameters());
return result;
}
use of com.redhat.ceylon.model.typechecker.model.TypeParameter in project ceylon-compiler by ceylon.
the class ClassTransformer method typeParametersForInstantiator.
/**
* When generating an instantiator method if the inner class has a type
* parameter with the same name as a type parameter of an outer type, then the
* instantiator method shouldn't declare its own type parameter of that
* name -- it should use the captured one. This method filters out the
* type parameters of the inner class which are the same as type parameters
* of the outer class so that they can be captured.
*/
private java.util.List<TypeParameter> typeParametersForInstantiator(final Class model) {
java.util.List<TypeParameter> filtered = new ArrayList<TypeParameter>();
java.util.List<TypeParameter> tps = model.getTypeParameters();
if (tps != null) {
for (TypeParameter tp : tps) {
boolean omit = false;
Scope s = model.getContainer();
while (!(s instanceof Package)) {
if (s instanceof Generic) {
for (TypeParameter outerTp : ((Generic) s).getTypeParameters()) {
if (tp.getName().equals(outerTp.getName())) {
omit = true;
}
}
}
s = s.getContainer();
}
if (!omit) {
filtered.add(tp);
}
}
}
return filtered;
}
use of com.redhat.ceylon.model.typechecker.model.TypeParameter in project ceylon-compiler by ceylon.
the class ClassTransformer method serializationConstructor.
/**
* <p>Generates the serialization constructor
* with signature {@code ($Serialization$)} which:</p>
* <ul>
* <li>invokes {@code super()}, if the super class is also
* serializable,</li>
* <li>initializes all companion instance fields to a
* newly instantiated companion instance,</li>
* <li>initializes all reified type argument fields to null,</li>
* <li>initializes all reference attribute fields to null,</li>
* <li>initializesall primitive attribute fields to a default
* value (basically some kind of 0)</li>
* </ul>
*/
private void serializationConstructor(Class model, ClassDefinitionBuilder classBuilder) {
MethodDefinitionBuilder ctor = classBuilder.addConstructor();
ctor.ignoreModelAnnotations();
ctor.modifiers(PUBLIC);
ParameterDefinitionBuilder serializationPdb = ParameterDefinitionBuilder.systemParameter(this, "ignored");
serializationPdb.modifiers(FINAL);
serializationPdb.type(make().Type(syms().ceylonSerializationType), null);
ctor.parameter(serializationPdb);
for (TypeParameter tp : model.getTypeParameters()) {
ctor.reifiedTypeParameter(tp);
}
final ListBuffer<JCStatement> stmts = ListBuffer.lb();
if (extendsSerializable(model)) {
// invoke super
ListBuffer<JCExpression> superArgs = ListBuffer.<JCExpression>lb();
superArgs.add(naming.makeUnquotedIdent("ignored"));
for (JCExpression ta : makeReifiedTypeArguments(model.getExtendedType())) {
superArgs.add(ta);
}
stmts.add(make().Exec(make().Apply(null, naming.makeSuper(), superArgs.toList())));
}
buildFieldInits(model, classBuilder, stmts);
ctor.body(stmts.toList());
}
use of com.redhat.ceylon.model.typechecker.model.TypeParameter in project ceylon-compiler by ceylon.
the class ClassTransformer method addAmbiguousMember.
private void addAmbiguousMember(ClassDefinitionBuilder classBuilder, Interface model, String name) {
Declaration member = model.getMember(name, null, false);
Type satisfiedType = model.getType().getSupertype(model);
if (member instanceof Class) {
Class klass = (Class) member;
if (Strategy.generateInstantiator(member) && !klass.hasConstructors()) {
// instantiator method implementation
generateInstantiatorDelegate(classBuilder, satisfiedType, model, klass, null, model.getType(), false);
}
if (klass.hasConstructors()) {
for (Declaration m : klass.getMembers()) {
if (m instanceof Constructor && Strategy.generateInstantiator(m)) {
Constructor ctor = (Constructor) m;
generateInstantiatorDelegate(classBuilder, satisfiedType, model, klass, ctor, model.getType(), false);
}
}
}
} else if (member instanceof Function) {
Function method = (Function) member;
final TypedReference typedMember = satisfiedType.getTypedMember(method, Collections.<Type>emptyList());
java.util.List<java.util.List<Type>> producedTypeParameterBounds = producedTypeParameterBounds(typedMember, method);
final java.util.List<TypeParameter> typeParameters = method.getTypeParameters();
final java.util.List<Parameter> parameters = method.getFirstParameterList().getParameters();
for (Parameter param : parameters) {
if (Strategy.hasDefaultParameterOverload(param)) {
MethodDefinitionBuilder overload = new DefaultedArgumentMethodTyped(null, MethodDefinitionBuilder.method(this, method), typedMember, true).makeOverload(method.getFirstParameterList(), param, typeParameters);
overload.modifiers(PUBLIC | ABSTRACT);
classBuilder.method(overload);
}
}
final MethodDefinitionBuilder concreteMemberDelegate = makeDelegateToCompanion(null, typedMember, model.getType(), PUBLIC | ABSTRACT, method.getTypeParameters(), producedTypeParameterBounds, typedMember.getType(), naming.selector(method), method.getFirstParameterList().getParameters(), ((Function) member).getTypeErased(), null, DelegateType.OTHER, false);
classBuilder.method(concreteMemberDelegate);
} else if (member instanceof Value || member instanceof Setter) {
TypedDeclaration attr = (TypedDeclaration) member;
final TypedReference typedMember = satisfiedType.getTypedMember(attr, Collections.<Type>emptyList());
if (member instanceof Value) {
final MethodDefinitionBuilder getterDelegate = makeDelegateToCompanion(null, typedMember, model.getType(), PUBLIC | ABSTRACT, Collections.<TypeParameter>emptyList(), Collections.<java.util.List<Type>>emptyList(), typedMember.getType(), Naming.getGetterName(attr), Collections.<Parameter>emptyList(), attr.getTypeErased(), null, DelegateType.OTHER, false);
classBuilder.method(getterDelegate);
}
if (member instanceof Setter) {
final MethodDefinitionBuilder setterDelegate = makeDelegateToCompanion(null, typedMember, model.getType(), PUBLIC | ABSTRACT, Collections.<TypeParameter>emptyList(), Collections.<java.util.List<Type>>emptyList(), typeFact().getAnythingType(), Naming.getSetterName(attr), Collections.<Parameter>singletonList(((Setter) member).getParameter()), ((Setter) member).getTypeErased(), null, DelegateType.OTHER, false);
classBuilder.method(setterDelegate);
}
}
}
use of com.redhat.ceylon.model.typechecker.model.TypeParameter in project ceylon-compiler by ceylon.
the class ClassTransformer method buildFieldInits.
protected void buildFieldInits(Class model, ClassDefinitionBuilder classBuilder, final ListBuffer<JCStatement> stmts) {
final HashSet<String> excludeFields = new HashSet<String>();
// initialize reified type arguments to according to parameters
for (TypeParameter tp : model.getTypeParameters()) {
excludeFields.add(naming.getTypeArgumentDescriptorName(tp));
stmts.add(makeReifiedTypeParameterAssignment(tp));
}
// initialize companion instances to a new companion instance
if (!model.getSatisfiedTypes().isEmpty()) {
SatisfactionVisitor visitor = new SatisfactionVisitor() {
@Override
public void satisfiesDirectly(Class model, Interface iface, boolean alreadySatisfied) {
if (!alreadySatisfied) {
assignCompanion(model, iface);
}
}
@Override
public void satisfiesIndirectly(Class model, Interface iface, boolean alreadySatisfied) {
if (!alreadySatisfied) {
assignCompanion(model, iface);
}
}
private void assignCompanion(Class model, Interface iface) {
if (hasImpl(iface) && excludeFields.add(getCompanionFieldName(iface))) {
stmts.add(makeCompanionInstanceAssignment(model, iface, model.getType().getSupertype(iface)));
}
}
@Override
public void satisfiesIndirectlyViaClass(Class model, Interface iface, Class via, boolean alreadySatisfied) {
// don't care
}
};
walkSatisfiedInterfaces(model, model.getType(), visitor);
}
// initialize attribute fields to null or a zero
appendDefaultFieldInits(classBuilder, stmts, excludeFields);
}
Aggregations