use of com.redhat.ceylon.model.typechecker.model.Parameter in project ceylon-compiler by ceylon.
the class ClassTransformer method concreteMembersFromSuperinterfaces.
/**
* Generates companion fields ($Foo$impl) and methods
*/
private void concreteMembersFromSuperinterfaces(final Class model, ClassDefinitionBuilder classBuilder, Type satisfiedType, Set<Interface> satisfiedInterfaces) {
satisfiedType = satisfiedType.resolveAliases();
Interface iface = (Interface) satisfiedType.getDeclaration();
if (satisfiedInterfaces.contains(iface) || iface.getType().isExactly(typeFact().getIdentifiableDeclaration().getType())) {
return;
}
// then don't instantiate it...
if (hasImpl(iface)) {
// ... otherwise for each satisfied interface,
// instantiate an instance of the
// companion class in the constructor and assign it to a
// $Interface$impl field
transformInstantiateCompanions(classBuilder, model, iface, satisfiedType);
}
if (!Decl.isCeylon(iface)) {
// let's not try to implement CMI for Java interfaces
return;
}
// For each super interface
for (Declaration member : iface.getMembers()) {
if (member instanceof Class) {
Class klass = (Class) member;
final Type typedMember = satisfiedType.getTypeMember(klass, Collections.<Type>emptyList());
if (Strategy.generateInstantiator(member) && !klass.hasConstructors() && !model.isFormal() && needsCompanionDelegate(model, typedMember) && model.getDirectMember(member.getName(), null, false) == null) {
// instantiator method implementation
generateInstantiatorDelegate(classBuilder, satisfiedType, iface, klass, null, model.getType(), !member.isFormal());
}
if (klass.hasConstructors()) {
for (Declaration m : klass.getMembers()) {
if (m instanceof Constructor && Strategy.generateInstantiator(m)) {
Constructor ctor = (Constructor) m;
generateInstantiatorDelegate(classBuilder, satisfiedType, iface, klass, ctor, model.getType(), true);
}
}
}
}
// type aliases are on the $impl class
if (member instanceof TypeAlias)
continue;
if (Strategy.onlyOnCompanion(member)) {
// (they're just private methods on the $impl)
continue;
}
if (member instanceof Function) {
Function method = (Function) member;
final TypedReference typedMember = satisfiedType.getTypedMember(method, Collections.<Type>emptyList());
Declaration sub = (Declaration) model.getMember(method.getName(), getSignatureIfRequired(typedMember), false, true);
if (sub instanceof Function) /* && !sub.isAbstraction()*/
{
Function subMethod = (Function) sub;
if (subMethod.getParameterLists().isEmpty()) {
continue;
}
java.util.List<java.util.List<Type>> producedTypeParameterBounds = producedTypeParameterBounds(typedMember, subMethod);
// final TypedReference refinedTypedMember = model.getType().getTypedMember(subMethod, Collections.<Type>emptyList());
final java.util.List<TypeParameter> typeParameters = subMethod.getTypeParameters();
final java.util.List<Parameter> parameters = subMethod.getFirstParameterList().getParameters();
boolean hasOverloads = false;
if (!satisfiedInterfaces.contains((Interface) method.getContainer())) {
for (Parameter param : parameters) {
if (Strategy.hasDefaultParameterValueMethod(param) && CodegenUtil.getTopmostRefinedDeclaration(param.getModel()).getContainer().equals(member)) {
final TypedReference typedParameter = typedMember.getTypedParameter(param);
// If that method has a defaulted parameter,
// we need to generate a default value method
// which also delegates to the $impl
final MethodDefinitionBuilder defaultValueDelegate = makeDelegateToCompanion(iface, typedParameter, model.getType(), PUBLIC | FINAL, typeParameters, producedTypeParameterBounds, typedParameter.getFullType(), Naming.getDefaultedParamMethodName(method, param), parameters.subList(0, parameters.indexOf(param)), param.getModel().getTypeErased(), null, DelegateType.FOR_DEFAULT_VALUE);
classBuilder.method(defaultValueDelegate);
}
if (Strategy.hasDefaultParameterOverload(param)) {
if ((method.isDefault() || method.isShared() && !method.isFormal()) && Decl.equal(method, subMethod)) {
MethodDefinitionBuilder overload = new DefaultedArgumentMethodTyped(new DaoThis((Tree.AnyMethod) null, null), MethodDefinitionBuilder.method(this, subMethod), typedMember, true).makeOverload(subMethod.getFirstParameterList(), param, typeParameters);
classBuilder.method(overload);
}
hasOverloads = true;
}
}
}
// delegating to the $impl instance
if (needsCompanionDelegate(model, typedMember)) {
final MethodDefinitionBuilder concreteMemberDelegate = makeDelegateToCompanion(iface, typedMember, model.getType(), PUBLIC | (method.isDefault() ? 0 : FINAL), typeParameters, producedTypeParameterBounds, typedMember.getType(), naming.selector(method), method.getFirstParameterList().getParameters(), ((Function) member).getTypeErased(), null, DelegateType.OTHER);
classBuilder.method(concreteMemberDelegate);
}
if (hasOverloads && (method.isDefault() || method.isShared() && !method.isFormal()) && Decl.equal(method, subMethod)) {
final MethodDefinitionBuilder canonicalMethod = makeDelegateToCompanion(iface, typedMember, model.getType(), PRIVATE, subMethod.getTypeParameters(), producedTypeParameterBounds, typedMember.getType(), Naming.selector(method, Naming.NA_CANONICAL_METHOD), method.getFirstParameterList().getParameters(), ((Function) member).getTypeErased(), naming.selector(method), DelegateType.OTHER);
classBuilder.method(canonicalMethod);
}
}
} else if (member instanceof Value || member instanceof Setter) {
TypedDeclaration attr = (TypedDeclaration) member;
final TypedReference typedMember = satisfiedType.getTypedMember(attr, null);
if (needsCompanionDelegate(model, typedMember)) {
Setter setter = (member instanceof Setter) ? (Setter) member : null;
if (member instanceof Value) {
if (member instanceof JavaBeanValue) {
setter = ((Value) member).getSetter();
}
final MethodDefinitionBuilder getterDelegate = makeDelegateToCompanion(iface, typedMember, model.getType(), PUBLIC | (attr.isDefault() ? 0 : FINAL), Collections.<TypeParameter>emptyList(), Collections.<java.util.List<Type>>emptyList(), typedMember.getType(), Naming.getGetterName(attr), Collections.<Parameter>emptyList(), attr.getTypeErased(), null, DelegateType.OTHER);
classBuilder.method(getterDelegate);
}
if (setter != null) {
final MethodDefinitionBuilder setterDelegate = makeDelegateToCompanion(iface, satisfiedType.getTypedMember(setter, null), model.getType(), PUBLIC | (setter.getGetter().isDefault() ? 0 : FINAL), Collections.<TypeParameter>emptyList(), Collections.<java.util.List<Type>>emptyList(), typeFact().getAnythingType(), Naming.getSetterName(attr), Collections.<Parameter>singletonList(setter.getParameter()), setter.getTypeErased(), null, DelegateType.OTHER);
classBuilder.method(setterDelegate);
}
if (Decl.isValue(member) && ((Value) attr).isVariable()) {
// $impl to delegate to
throw new BugException("assertion failed: " + member.getQualifiedNameString() + " was unexpectedly a variable value");
}
}
} else {
Reference typedMember = member instanceof TypeDeclaration ? satisfiedType.getTypeMember((TypeDeclaration) member, Collections.<Type>emptyList()) : satisfiedType.getTypedMember((TypedDeclaration) member, Collections.<Type>emptyList());
if (needsCompanionDelegate(model, typedMember)) {
throw new BugException("unhandled concrete interface member " + member.getQualifiedNameString() + " " + member.getClass());
}
}
}
// Add $impl instances for the whole interface hierarchy
satisfiedInterfaces.add(iface);
for (Type sat : iface.getSatisfiedTypes()) {
sat = model.getType().getSupertype(sat.getDeclaration());
concreteMembersFromSuperinterfaces(model, classBuilder, sat, satisfiedInterfaces);
}
}
use of com.redhat.ceylon.model.typechecker.model.Parameter in project ceylon-compiler by ceylon.
the class ClassTransformer method addRefinedThrowerMethod.
private void addRefinedThrowerMethod(ClassDefinitionBuilder classBuilder, String error, ClassOrInterface classModel, Function formalMethod) {
Function refined = refineMethod(classModel, classModel.getType().getTypedMember(formalMethod, Collections.<Type>emptyList()), classModel, formalMethod, classModel.getUnit());
// the DPM and javac barfs.
for (java.util.List<Parameter> parameterList : overloads(refined)) {
MethodDefinitionBuilder mdb = MethodDefinitionBuilder.method(this, refined);
mdb.isOverride(true);
mdb.modifiers(transformMethodDeclFlags(refined));
for (TypeParameter tp : formalMethod.getTypeParameters()) {
mdb.typeParameter(tp);
mdb.reifiedTypeParameter(tp);
}
for (Parameter param : parameterList) {
mdb.parameter(param, null, 0, WideningRules.NONE);
}
mdb.resultType(refined, 0);
mdb.body(makeThrowUnresolvedCompilationError(error));
classBuilder.method(mdb);
}
}
use of com.redhat.ceylon.model.typechecker.model.Parameter in project ceylon-compiler by ceylon.
the class ClassTransformer method transformClassAliasInstantiator.
/**
* Builds the instantiator method for a class aliases. In 1.0 you can't
* actually invoke these, they exist just so there's somewhere to put the
* class alias annotations. In 1.2 (when we fix #1295) the
* instantiators will actually do something.
*/
private MethodDefinitionBuilder transformClassAliasInstantiator(final Tree.AnyClass def, Class model, Type aliasedClass) {
MethodDefinitionBuilder instantiator = MethodDefinitionBuilder.systemMethod(this, NamingBase.getAliasInstantiatorMethodName(model));
int f = 0;
if (Strategy.defaultParameterMethodStatic(def.getDeclarationModel())) {
f = STATIC;
}
instantiator.modifiers((transformClassDeclFlags(model) & ~FINAL) | f);
for (TypeParameter tp : typeParametersOfAllContainers(model, true)) {
instantiator.typeParameter(tp);
}
instantiator.resultType(null, makeJavaType(aliasedClass));
instantiator.annotationFlags(Annotations.MODEL_AND_USER | Annotations.IGNORE);
for (final Tree.Parameter param : def.getParameterList().getParameters()) {
// Overloaded instantiators
Parameter paramModel = param.getParameterModel();
at(param);
transformParameter(instantiator, param, paramModel, Decl.getMemberDeclaration(def, param));
}
instantiator.body(make().Throw(makeNewClass(makeJavaType(typeFact().getExceptionType(), JT_CLASS_NEW))));
return instantiator;
}
use of com.redhat.ceylon.model.typechecker.model.Parameter 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.Parameter in project ceylon-compiler by ceylon.
the class ClassTransformer method transformAnnotationClassConstructor.
/**
* Generates a constructor for an annotation class which takes the
* annotation type as parameter.
* @param classBuilder
*/
private void transformAnnotationClassConstructor(Tree.AnyClass def, ClassDefinitionBuilder classBuilder) {
Class klass = def.getDeclarationModel();
MethodDefinitionBuilder annoCtor = classBuilder.addConstructor();
annoCtor.ignoreModelAnnotations();
// constructors are never final
annoCtor.modifiers(transformClassDeclFlags(klass) & ~FINAL);
ParameterDefinitionBuilder pdb = ParameterDefinitionBuilder.systemParameter(this, "anno");
pdb.type(makeJavaType(klass.getType(), JT_ANNOTATION), null);
annoCtor.parameter(pdb);
// It's up to the caller to invoke value() on the Java annotation for a sequenced
// annotation
ListBuffer<JCExpression> args = ListBuffer.lb();
for (Tree.Parameter parameter : def.getParameterList().getParameters()) {
at(parameter);
Parameter parameterModel = parameter.getParameterModel();
JCExpression annoAttr = make().Apply(null, naming.makeQuotedQualIdent(naming.makeUnquotedIdent("anno"), parameter.getParameterModel().getName()), List.<JCExpression>nil());
Type parameterType = parameterModel.getType();
JCExpression argExpr;
if (typeFact().isIterableType(parameterType) && !isCeylonString(parameterType)) {
// Convert from array to Sequential
Type iteratedType = typeFact().getIteratedType(parameterType);
boolean nonEmpty = typeFact().isNonemptyIterableType(parameterType);
if (isCeylonBasicType(iteratedType)) {
argExpr = utilInvocation().sequentialWrapperBoxed(annoAttr);
} else if (Decl.isAnnotationClass(iteratedType.getDeclaration())) {
// Can't use Util.sequentialAnnotation becase we need to 'box'
// the Java annotations in their Ceylon annotation class
argExpr = make().Apply(null, naming.makeUnquotedIdent(naming.getAnnotationSequenceMethodName()), List.of(annoAttr));
ListBuffer<JCStatement> stmts = ListBuffer.lb();
SyntheticName array = naming.synthetic(Unfix.$array$);
SyntheticName sb = naming.synthetic(Unfix.$sb$);
SyntheticName index = naming.synthetic(Unfix.$index$);
SyntheticName element = naming.synthetic(Unfix.$element$);
stmts.append(makeVar(FINAL, sb, make().TypeArray(make().Type(syms().objectType)), make().NewArray(make().Type(syms().objectType), List.of(naming.makeQualIdent(array.makeIdent(), "length")), null)));
stmts.append(makeVar(index, make().Type(syms().intType), make().Literal(0)));
stmts.append(make().ForeachLoop(makeVar(element, makeJavaType(iteratedType, JT_ANNOTATION), null), array.makeIdent(), make().Exec(make().Assign(make().Indexed(sb.makeIdent(), make().Unary(JCTree.POSTINC, index.makeIdent())), instantiateAnnotationClass(iteratedType, element.makeIdent())))));
stmts.append(make().Return(make().NewClass(null, null, make().QualIdent(syms().ceylonTupleType.tsym), List.of(makeReifiedTypeArgument(iteratedType), sb.makeIdent(), makeEmpty(), make().Literal(false)), null)));
classBuilder.method(MethodDefinitionBuilder.systemMethod(this, naming.getAnnotationSequenceMethodName()).ignoreModelAnnotations().modifiers(PRIVATE | STATIC).resultType(null, makeJavaType(typeFact().getSequentialType(iteratedType))).parameter(ParameterDefinitionBuilder.systemParameter(this, array.getName()).type(make().TypeArray(makeJavaType(iteratedType, JT_ANNOTATION)), null)).body(stmts.toList()));
} else if (isCeylonMetamodelDeclaration(iteratedType)) {
argExpr = makeMetamodelInvocation("parseMetamodelReferences", List.<JCExpression>of(makeReifiedTypeArgument(iteratedType), annoAttr), List.<JCExpression>of(makeJavaType(iteratedType, JT_TYPE_ARGUMENT)));
} else if (Decl.isEnumeratedTypeWithAnonCases(iteratedType)) {
argExpr = makeMetamodelInvocation("parseEnumerationReferences", List.<JCExpression>of(makeReifiedTypeArgument(iteratedType), annoAttr), List.<JCExpression>of(makeJavaType(iteratedType, JT_TYPE_ARGUMENT)));
} else {
argExpr = makeErroneous(parameter, "compiler bug");
}
if (nonEmpty) {
argExpr = make().TypeCast(makeJavaType(parameterType), argExpr);
}
} else if (Decl.isAnnotationClass(parameterType.getDeclaration())) {
argExpr = instantiateAnnotationClass(parameterType, annoAttr);
} else if (isCeylonMetamodelDeclaration(parameterType)) {
argExpr = makeMetamodelInvocation("parseMetamodelReference", List.<JCExpression>of(annoAttr), List.<JCExpression>of(makeJavaType(parameterType, JT_TYPE_ARGUMENT)));
} else if (Decl.isEnumeratedTypeWithAnonCases(parameterType)) {
argExpr = makeMetamodelInvocation("parseEnumerationReference", List.<JCExpression>of(annoAttr), null);
} else {
argExpr = annoAttr;
argExpr = expressionGen().applyErasureAndBoxing(annoAttr, parameterType.withoutUnderlyingType(), false, BoxingStrategy.UNBOXED, parameterType);
}
args.add(argExpr);
}
annoCtor.body(at(def).Exec(make().Apply(null, naming.makeThis(), args.toList())));
}
Aggregations