use of org.eclipse.ceylon.model.typechecker.model.Class in project ceylon by eclipse.
the class ClassTransformer method makeNamedConstructor.
/**
* Makes a named constructor
* @param that
* @param classBuilder
* @param mods
* @param ctorName
* @param ctorBody
* @param declFlags
* @return
*/
public List<JCTree> makeNamedConstructor(Tree.Declaration that, Tree.ParameterList parameterList, Constructor ctor, ClassDefinitionBuilder classBuilder, boolean generateInstantiator, int mods, boolean atIgnoreCtor, String ctorName, List<JCStatement> ctorBody, DeclNameFlag... declFlags) {
ListBuffer<JCTree> result = new ListBuffer<JCTree>();
Class clz = (Class) ctor.getContainer();
at(that);
MethodDefinitionBuilder ctorDb = MethodDefinitionBuilder.constructor(this, ctor.isDeprecated());
ClassDefinitionBuilder decl = null;
ClassDefinitionBuilder impl = null;
if (generateInstantiator) {
if (clz.getContainer() instanceof Interface) {
decl = classBuilder.getContainingClassBuilder();
impl = classBuilder.getContainingClassBuilder().getCompanionBuilder((Interface) clz.getContainer());
} else {
decl = classBuilder.getContainingClassBuilder();
impl = classBuilder.getContainingClassBuilder();
}
generateInstantiators(classBuilder, clz, ctor, decl, impl, that, parameterList);
}
ctorDb.userAnnotations(expressionGen().transformAnnotations(OutputElement.CONSTRUCTOR, that));
if (atIgnoreCtor) {
ctorDb.modelAnnotations(makeAtIgnore());
} else if (!Decl.isDefaultConstructor(ctor)) {
ctorDb.modelAnnotations(makeAtName(ctor.getName()));
}
if (ModelUtil.isEnumeratedConstructor(ctor)) {
ctorDb.modelAnnotations(makeAtEnumerated());
}
ctorDb.modifiers(mods);
for (TypeParameter tp : Strategy.getEffectiveTypeParameters(clz)) {
ctorDb.reifiedTypeParameter(tp);
}
if (ctorName != null) {
// generate a constructor name class (and constant)
transformConstructorName(classBuilder, result, ctor, clz, mods, ctorName, declFlags);
// Add the name paramter
ctorDb.parameter(makeConstructorNameParameter(ctor, declFlags));
}
// Add the rest of the parameters (this worries about aliasing)
if (parameterList != null) {
transformClassOrCtorParameters(null, (Class) ctor.getContainer(), ctor, that, parameterList, contains(declFlags, DeclNameFlag.DELEGATION), classBuilder, ctorDb, generateInstantiator, decl, impl);
}
// Transformation of body has to happen after transformation of parameter so we know about parameter aliasing.
at(that);
ctorDb.block(make().Block(0, ctorBody));
result.add(ctorDb.build());
return result.toList();
}
use of org.eclipse.ceylon.model.typechecker.model.Class in project ceylon by eclipse.
the class ExpressionTransformer method appendDeclarationLiteralForAnnotation.
/**
* Appends into the given builder a String representation of the given
* declaration, suitable for parsing my the DeclarationParser.
*/
private static void appendDeclarationLiteralForAnnotation(Declaration decl, StringBuilder sb) {
Scope container = decl.getContainer();
while (true) {
if (container instanceof Declaration) {
appendDeclarationLiteralForAnnotation((Declaration) container, sb);
sb.append(".");
break;
} else if (container instanceof Package) {
appendDeclarationLiteralForAnnotation((Package) container, sb);
sb.append(":");
break;
}
container = container.getContainer();
}
if (decl instanceof Class) {
sb.append("C").append(decl.getName());
} else if (decl instanceof Interface) {
sb.append("I").append(decl.getName());
} else if (decl instanceof TypeAlias) {
sb.append("A").append(decl.getName());
} else if (decl instanceof Value) {
sb.append("V").append(decl.getName());
} else if (decl instanceof Function) {
sb.append("F").append(decl.getName());
} else if (decl instanceof TypeParameter) {
sb.append("P").append(decl.getName());
} else if (decl instanceof Constructor) {
sb.append("c").append(decl.getName());
} else {
throw BugException.unhandledDeclarationCase(decl);
}
}
use of org.eclipse.ceylon.model.typechecker.model.Class in project ceylon by eclipse.
the class ExpressionTransformer method transformMemberReference.
JCExpression transformMemberReference(Tree.QualifiedMemberOrTypeExpression expr, Tree.MemberOrTypeExpression primary) {
Declaration member = expr.getDeclaration();
Type qualifyingType = primary.getTypeModel();
Tree.TypeArguments typeArguments = expr.getTypeArguments();
Type expectedTypeIfCoerced = coerced ? expectedType : null;
boolean prevSyntheticClassBody = withinSyntheticClassBody(true);
try {
if (member.isStatic()) {
if (member instanceof Function) {
Function method = (Function) member;
// method.appliedReference(qualifyingType, typeArguments.getTypeModels());
Reference producedReference = expr.getTarget();
return CallableBuilder.javaStaticMethodReference(gen(), expr, expr.getTypeModel(), method, producedReference, expectedTypeIfCoerced).build();
} else if (member instanceof FieldValue) {
return naming.makeName((TypedDeclaration) member, Naming.NA_FQ | Naming.NA_WRAPPER_UNQUOTED);
} else if (member instanceof Value) {
CallBuilder callBuilder = CallBuilder.instance(this);
JCExpression qualExpr = naming.makeTypeDeclarationExpression(null, (TypeDeclaration) member.getContainer(), DeclNameFlag.QUALIFIED);
Type primType = primary.getTarget().getType();
if (ModelUtil.isCeylonDeclaration(member) && !primType.getTypeArgumentList().isEmpty()) {
for (Type pt : primType.getTypeArgumentList()) {
callBuilder.typeArgument(makeJavaType(pt, JT_TYPE_ARGUMENT));
callBuilder.argument(makeReifiedTypeArgument(pt));
}
}
callBuilder.invoke(naming.makeQualifiedName(qualExpr, (TypedDeclaration) member, Naming.NA_GETTER | Naming.NA_MEMBER));
return callBuilder.build();
} else if (member instanceof Class) {
Reference producedReference = expr.getTarget();
return CallableBuilder.javaStaticMethodReference(gen(), expr, expr.getTypeModel(), (Class) member, producedReference, expectedTypeIfCoerced).build();
}
}
if (member instanceof Value) {
if (expr.getStaticMethodReference() && ModelUtil.isEnumeratedConstructor((Value) member)) {
CallBuilder callBuilder = CallBuilder.instance(this);
JCExpression qualExpr;
Class class1 = (Class) member.getContainer();
if (class1.isToplevel() || class1.isStatic()) {
qualExpr = naming.makeTypeDeclarationExpression(null, class1.isStatic() ? (TypeDeclaration) class1.getContainer() : class1, DeclNameFlag.QUALIFIED);
callBuilder.invoke(naming.makeQualifiedName(qualExpr, (TypedDeclaration) member, Naming.NA_GETTER | Naming.NA_MEMBER));
} else if (class1.isMember()) {
// creates a Callable<Outer.Inner,[Outer]> that returns the enumeratedConstructor given an outer instance
if (primary instanceof Tree.QualifiedMemberOrTypeExpression && (((Tree.QualifiedMemberOrTypeExpression) primary).getPrimary() instanceof Tree.BaseTypeExpression || ((Tree.QualifiedMemberOrTypeExpression) primary).getPrimary() instanceof Tree.QualifiedTypeExpression))
return CallableBuilder.unboundValueMemberReference(gen(), expr, expr.getTypeModel(), ((TypedDeclaration) member), expectedTypeIfCoerced).build();
else {
qualExpr = primary instanceof Tree.QualifiedMemberOrTypeExpression ? transformExpression(((Tree.QualifiedMemberOrTypeExpression) primary).getPrimary()) : null;
callBuilder.invoke(naming.makeQualifiedName(qualExpr, (TypedDeclaration) member, Naming.NA_GETTER | Naming.NA_MEMBER));
}
} else {
// Local enumerated constructor values are boxed
qualExpr = naming.makeQualifiedName(null, (TypedDeclaration) member, Naming.NA_Q_LOCAL_INSTANCE);
qualExpr = gen().makeSelect(qualExpr, naming.selector((TypedDeclaration) member));
callBuilder.fieldRead(qualExpr);
}
return callBuilder.build();
} else {
return CallableBuilder.unboundValueMemberReference(gen(), expr, expr.getTypeModel(), ((TypedDeclaration) member), expectedTypeIfCoerced).build();
}
} else if (Decl.isConstructor(member)) {
Reference producedReference = expr.getTarget();
return CallableBuilder.unboundFunctionalMemberReference(gen(), expr, expr.getTypeModel(), ModelUtil.getConstructor(member), producedReference, expectedTypeIfCoerced);
} else if (member instanceof Function) {
Function method = (Function) member;
if (!method.isParameter()) {
Reference producedReference = method.appliedReference(qualifyingType, typeArguments.getTypeModels());
return CallableBuilder.unboundFunctionalMemberReference(gen(), expr, expr.getTypeModel(), method, producedReference, expectedTypeIfCoerced);
} else {
Reference producedReference = method.appliedReference(qualifyingType, typeArguments.getTypeModels());
return CallableBuilder.unboundFunctionalMemberReference(gen(), expr, expr.getTypeModel(), method, producedReference, expectedTypeIfCoerced);
}
} else if (member instanceof Class) {
Reference producedReference = expr.getTarget();
return CallableBuilder.unboundFunctionalMemberReference(gen(), expr, expr.getTypeModel(), (Class) member, producedReference, expectedTypeIfCoerced);
} else {
return makeErroneous(expr, "compiler bug: member reference of " + expr + " not supported yet");
}
} finally {
withinSyntheticClassBody(prevSyntheticClassBody);
}
}
use of org.eclipse.ceylon.model.typechecker.model.Class in project ceylon by eclipse.
the class ExpressionTransformer method makeJavaStaticInvocation.
JCExpression makeJavaStaticInvocation(CeylonTransformer gen, final Functional methodOrClass, Reference producedReference, final ParameterList parameterList) {
CallBuilder callBuilder = CallBuilder.instance(gen);
if (methodOrClass instanceof Function) {
JCExpression fn;
if (Decl.isJavaArrayFrom((Declaration) methodOrClass)) {
fn = gen.makeUnwrapArray((Declaration) methodOrClass);
} else {
fn = naming.makeName((Function) methodOrClass, Naming.NA_FQ | Naming.NA_WRAPPER_UNQUOTED);
}
callBuilder.invoke(fn);
} else if (methodOrClass instanceof Class) {
callBuilder.instantiate(gen.makeJavaType(((Class) methodOrClass).getType(), JT_RAW | JT_NO_PRIMITIVES));
}
ListBuffer<ExpressionAndType> reified = new ListBuffer<ExpressionAndType>();
DirectInvocation.addReifiedArguments(gen, producedReference, reified);
for (ExpressionAndType reifiedArgument : reified) {
callBuilder.argument(reifiedArgument.expression);
}
for (Parameter parameter : parameterList.getParameters()) {
callBuilder.argument(gen.naming.makeQuotedIdent(parameter.getName()));
}
JCExpression innerInvocation = callBuilder.build();
return innerInvocation;
}
use of org.eclipse.ceylon.model.typechecker.model.Class in project ceylon by eclipse.
the class ExpressionTransformer method transformJavaStaticOrInterfaceMember.
private JCExpression transformJavaStaticOrInterfaceMember(Tree.QualifiedMemberOrTypeExpression qmte, Type staticType) {
Declaration decl = qmte.getDeclaration();
if (decl instanceof FieldValue) {
Value member = (Value) decl;
return naming.makeName(member, Naming.NA_FQ | Naming.NA_WRAPPER_UNQUOTED);
} else if (decl instanceof Value) {
Value member = (Value) decl;
CallBuilder callBuilder = CallBuilder.instance(this);
Type qualifyingType = ((TypeDeclaration) member.getContainer()).getType();
callBuilder.invoke(naming.makeQualifiedName(makeJavaType(qualifyingType, JT_RAW | JT_NO_PRIMITIVES), member, Naming.NA_GETTER | Naming.NA_MEMBER));
return utilInvocation().checkNull(callBuilder.build());
} else if (decl instanceof Function) {
Function method = (Function) decl;
final ParameterList parameterList = method.getFirstParameterList();
Type qualifyingType = qmte.getPrimary().getTypeModel();
Tree.TypeArguments typeArguments = qmte.getTypeArguments();
Reference producedReference = method.appliedReference(qualifyingType, typeArguments.getTypeModels());
return utilInvocation().checkNull(makeJavaStaticInvocation(gen(), method, producedReference, parameterList));
} else if (decl instanceof Class) {
Class class_ = (Class) decl;
if (class_.isStatic()) {
return naming.makeTypeDeclarationExpression(null, class_, Naming.DeclNameFlag.QUALIFIED);
} else {
final ParameterList parameterList = class_.getFirstParameterList();
Reference producedReference = qmte.getTarget();
return utilInvocation().checkNull(makeJavaStaticInvocation(gen(), class_, producedReference, parameterList));
}
} else if (decl instanceof Interface) {
return naming.makeTypeDeclarationExpression(null, (Interface) decl, Naming.DeclNameFlag.QUALIFIED);
} else {
return makeErroneous(qmte, "compiler bug: unsupported static");
}
}
Aggregations