Skip to content

Commit

Permalink
More WIP
Browse files Browse the repository at this point in the history
Part of #750
  • Loading branch information
tombentley committed Sep 29, 2015
1 parent 610e33c commit 200d3b3
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 29 deletions.
Expand Up @@ -281,8 +281,8 @@ public ceylon.language.meta.model.Type<?> getContainer(){
}

@Override
@TypeInfo("ceylon.language.meta.model::CallableConstructor<Type,Arguments>|ceylon.language.meta.model::Class<Type,Arguments>|ceylon.language::Null")
public Applicable<Type, Arguments> getDefaultConstructor() {
@TypeInfo("ceylon.language.meta.model::CallableConstructor<Type,Arguments>|ceylon.language::Null")
public CallableConstructor<Type, Arguments> getDefaultConstructor() {
if (hasConstructors() || hasEnumerated()) {
Object ctor = getConstructor($reifiedArguments, "");
if (ctor instanceof CallableConstructor) {
Expand All @@ -291,7 +291,7 @@ public Applicable<Type, Arguments> getDefaultConstructor() {
return null;
}
} else {
return this;
return new AppliedInitializer<>(this);
}
}

Expand Down
@@ -1,5 +1,7 @@
package com.redhat.ceylon.compiler.java.runtime.metamodel;

import java.util.List;

import ceylon.language.Entry;
import ceylon.language.Iterable;
import ceylon.language.Map;
Expand All @@ -9,15 +11,33 @@
import ceylon.language.meta.declaration.CallableConstructorDeclaration;
import ceylon.language.meta.declaration.TypeParameter;
import ceylon.language.meta.model.CallableConstructor;
import ceylon.language.meta.model.Class;
import ceylon.language.meta.model.ClassModel;

import com.redhat.ceylon.model.typechecker.model.Parameter;

public class AppliedInitializer<Type, Arguments extends Sequential<? extends Object>> implements CallableConstructor<Type, Arguments>{

private Class<Type, Arguments> clazz;
private AppliedClass<Type, Arguments> clazz;
private List<com.redhat.ceylon.model.typechecker.model.Type> parameterProducedTypes;
private Sequential<? extends ceylon.language.meta.model.Type<? extends Object>> parameterTypes;

public AppliedInitializer(Class<Type, Arguments> clazz) {
public AppliedInitializer(AppliedClass<Type, Arguments> clazz) {
this.clazz = clazz;
List<Parameter> parameters = ((com.redhat.ceylon.model.typechecker.model.Class)clazz.declaration.declaration).getParameterLists().get(0).getParameters();
/*this.firstDefaulted = Metamodel.getFirstDefaultedParameter(parameters);
this.variadicIndex = Metamodel.getVariadicParameter(parameters);
Object[] defaultedMethods = null;
if(firstDefaulted != -1){
// if we have 2 params and first is defaulted we need 2 + 1 - 0 = 3 methods:
// f(), f(a) and f(a, b)
this.dispatch = new MethodHandle[parameters.size() + 1 - firstDefaulted];
defaultedMethods = new Object[dispatch.length];
}*/

// get a list of produced parameter types
this.parameterProducedTypes = Metamodel.getParameterProducedTypes(parameters, clazz.producedType);
this.parameterTypes = Metamodel.getAppliedMetamodelSequential(this.parameterProducedTypes);
}

@Override
Expand All @@ -42,8 +62,7 @@ public Sequential<? extends Sequence<? extends Object>> getTypeArgumentWithVaria

@Override
public Sequential<? extends ceylon.language.meta.model.Type<? extends Object>> getParameterTypes() {
// TODO Auto-generated method stub
return null;
return parameterTypes;
}

@Override
Expand Down Expand Up @@ -139,18 +158,46 @@ public Type namedApply(
}

@Override
public ClassModel getContainer() {
public ClassModel<?,?> getContainer() {
return clazz;
}

@Override
public CallableConstructorDeclaration getDeclaration() {
return (CallableConstructorDeclaration)clazz.getDefaultConstructor();
return ((FreeClass)clazz.declaration).getDefaultConstructor();
}

@Override
public ClassModel getType() {
public ClassModel<?,?> getType() {
return clazz;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((clazz == null) ? 0 : clazz.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AppliedInitializer<?,?> other = (AppliedInitializer<?,?>) obj;
if (clazz == null) {
if (other.clazz != null)
return false;
} else if (!clazz.equals(other.clazz))
return false;
return true;
}

public java.lang.String toString() {
return clazz.toString();
}
}
Expand Up @@ -6,6 +6,7 @@
import ceylon.language.meta.declaration.CallableConstructorDeclaration;
import ceylon.language.meta.declaration.ClassDeclaration;
import ceylon.language.meta.declaration.ValueConstructorDeclaration;
import ceylon.language.meta.model.CallableConstructor;
import ceylon.language.meta.model.Class;
import ceylon.language.meta.model.FunctionModel;
import ceylon.language.meta.model.MemberClassCallableConstructor;
Expand Down Expand Up @@ -73,13 +74,12 @@ protected void init() {
}

@TypeInfo("ceylon.language.meta.model::MemberClassCallableConstructor<Container, Type, Arguments>"
+ "|ceylon.language.meta.model::MemberClass<Container, Type, Arguments>"
+ "|ceylon.language::Null")
public Object getDefaultConstructor() {
public MemberClassCallableConstructor<Container, Type, Arguments> getDefaultConstructor() {
if (hasConstructors() || hasEnumerated()) {
return (MemberClassCallableConstructor<Container, Type, Arguments>)getConstructor($reifiedArguments, "");
} else {
return this;
return new AppliedMemberInitializer($reifiedContainer, $reifiedType, $reifiedArguments, this);
}
}

Expand Down
Expand Up @@ -50,6 +50,8 @@ public class AppliedMemberInitializer<Container, Type, Arguments extends Sequent
//private Sequential<? extends ceylon.language.meta.model.Type<? extends Object>> parameterTypes;
final AppliedMemberClass<Container, Type, ?> clazz;

final Sequential<? extends ceylon.language.meta.model.Type<? extends Object>> parameterTypes;

@Ignore
@Override
public TypeDescriptor $getType$() {
Expand All @@ -64,16 +66,16 @@ public AppliedMemberInitializer(TypeDescriptor $reifiedContainer,
this.clazz = clazz;
this.$reifiedType = $reifiedType;
this.$reifiedArguments = $reifiedArguments;
//this.appliedFunction = appliedFunction;

//this.declaration = declaration;
//this.typeArguments = Metamodel.getTypeArguments(declaration, appliedFunction);
//this.typeArgumentWithVariances = Metamodel.getTypeArgumentWithVariances(declaration, appliedFunction);
//this.closedType = Metamodel.getAppliedMetamodel(Metamodel.getFunctionReturnType(appliedFunction));
// get a list of produced parameter types
//com.redhat.ceylon.model.typechecker.model.Functional method = (com.redhat.ceylon.model.typechecker.model.Functional)appliedFunction.getDeclaration();
//List<Parameter> parameters = method.getFirstParameterList().getParameters();
//List<com.redhat.ceylon.model.typechecker.model.Type> parameterProducedTypes = Metamodel.getParameterProducedTypes(parameters, appliedFunction);
//this.parameterTypes = Decl.isConstructor(declaration.declaration) ? null : Metamodel.getAppliedMetamodelSequential(parameterProducedTypes);
List<com.redhat.ceylon.model.typechecker.model.Type> parameterProducedTypes = Metamodel.getParameterProducedTypes(
((com.redhat.ceylon.model.typechecker.model.Class)clazz.declaration.declaration).getParameterList().getParameters(), clazz.producedType);
this.parameterTypes = Metamodel.getAppliedMetamodelSequential(parameterProducedTypes);

}

@Override
Expand All @@ -84,12 +86,12 @@ public AppliedMemberInitializer(TypeDescriptor $reifiedContainer,

@Override
public ClassModel<Type, ?> getContainer() {
return (ClassModel)clazz.getContainer();
return clazz;
}

@Override
public CallableConstructorDeclaration getDeclaration() {
return (CallableConstructorDeclaration)clazz.getDefaultConstructor();
return (CallableConstructorDeclaration)((FreeClass)clazz.declaration).getDefaultConstructor();
}

@Override
Expand Down Expand Up @@ -127,7 +129,7 @@ public ceylon.language.Sequential<? extends ceylon.language.Sequence<? extends O
@Override
protected CallableConstructor<Type, Arguments> bindTo(Object instance) {
return new AppliedInitializer<Type, Arguments>(
(ceylon.language.meta.model.Class)clazz.bind(instance));
(AppliedClass)clazz.bind(instance));
}


Expand Down Expand Up @@ -197,8 +199,7 @@ protected CallableConstructor<Type, Arguments> bindTo(Object instance) {
@TypeInfo("ceylon.language::Sequential<ceylon.language.meta.model::Type<ceylon.language::Anything>>")
@Override
public ceylon.language.Sequential<? extends ceylon.language.meta.model.Type<? extends Object>> getParameterTypes(){
// TODO Auto-generated method stub
return null;
return parameterTypes;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/ceylon/language/meta/model/Class.ceylon
Expand Up @@ -25,7 +25,7 @@ shared sealed interface Class<out Type=Anything, in Arguments=Nothing>
shared actual formal Value<Type>? getValueConstructor(String name);*/

shared actual formal <CallableConstructor<Type, Arguments>|Class<Type, Arguments>>? defaultConstructor;
shared actual formal CallableConstructor<Type, Arguments>? defaultConstructor;

"The constructor with the given name, or null if this class lacks
a constructor of the given name."
Expand Down
2 changes: 1 addition & 1 deletion src/ceylon/language/meta/model/ClassModel.ceylon
Expand Up @@ -19,7 +19,7 @@ shared sealed interface ClassModel<out Type=Anything, in Arguments=Nothing>

"A function model for this class's initializer or default constructor,
or null if this class has constructors but lacks a default constructor."
shared formal <FunctionModel<Type, Arguments>|ClassModel<Type, Arguments>>? defaultConstructor;
shared formal FunctionModel<Type, Arguments>? defaultConstructor;

"Looks up a constructor by name,
Returns `null` if no such constructor matches.
Expand Down
2 changes: 1 addition & 1 deletion src/ceylon/language/meta/model/MemberClass.ceylon
Expand Up @@ -24,7 +24,7 @@ shared sealed interface MemberClass<in Container=Nothing, out Type=Anything, in

shared actual formal Class<Type, Arguments> bind(Object container);

shared actual formal <MemberClassCallableConstructor<Container, Type, Arguments>|MemberClass<Container, Type, Arguments>>? defaultConstructor;
shared actual formal MemberClassCallableConstructor<Container, Type, Arguments>? defaultConstructor;

"The constructor with the given name, or null if this class lacks
a constructor of the given name"
Expand Down
26 changes: 24 additions & 2 deletions test/metamodel/bug750.ceylon
Expand Up @@ -101,6 +101,14 @@ shared void bug750() {

CallableConstructorDeclaration? yyy = bug750Init.getConstructorDeclaration("");
assert(yyy exists);

// Models
assert(exists zzz = `Bug750Init<String>`.defaultConstructor,
appliedCtor == zzz);
assert([`String`] == appliedCtor.parameterTypes);
assert(init == appliedCtor.declaration);
assert(exists aaa = appliedCtor.container,
`Bug750Init<String>` == aaa);

Bug750Outer().bug750();
}
Expand Down Expand Up @@ -177,13 +185,13 @@ class Bug750Outer() {
assert(false);
} catch (TypeApplicationException e) {}

init.memberApply<Bug750Outer,Bug750Init<String>,[String]>(`Bug750Outer`, `String`);
value appliedCtor = init.memberApply<Bug750Outer,Bug750Init<String>,[String]>(`Bug750Outer`, `String`);

//container
assert(bug750Init == init.container);

// invoke
// TODO assert(is Bug750Init<String> inst= init.memberInvoke(this, [`String`], ""));
assert(is Bug750Init<String> inst= init.memberInvoke(this, [`String`], ""));
try {
init.invoke([`String`], "");
assert(false);
Expand All @@ -197,5 +205,19 @@ class Bug750Outer() {

// qualifiedName
assert("metamodel::Bug750Outer.Bug750Init" == init.qualifiedName);

Sequence<CallableConstructorDeclaration> xxx = bug750Init.constructorDeclarations();
assert(init in xxx);

CallableConstructorDeclaration? yyy = bug750Init.getConstructorDeclaration("");
assert(yyy exists);

assert(exists zzz = `Bug750Init<String>`.defaultConstructor,
appliedCtor == zzz);
print("££££££££££££££££ `` appliedCtor.parameterTypes``");
assert([`String`] == appliedCtor.parameterTypes);
assert(init == appliedCtor.declaration);

assert(`Bug750Init<String>` == appliedCtor.container);
}
}

0 comments on commit 200d3b3

Please sign in to comment.