use of org.eclipse.ceylon.model.typechecker.model.Class in project ceylon by eclipse.
the class TypeParserTests method testQualifiedAndParameterised.
@Test
public void testQualifiedAndParameterised() {
Type type = new TypeParser(MockLoader.instance).decodeType("t2<a,b>.t2<c,d>", null, mockDefaultModule, mockPkgUnit);
Assert.assertNotNull(type);
TypeDeclaration declaration = type.getDeclaration();
Assert.assertNotNull(declaration);
Assert.assertTrue(declaration instanceof Class);
Assert.assertEquals("t2.t2", declaration.getQualifiedNameString());
Assert.assertEquals(2, type.getTypeArgumentList().size());
// c
Type c = type.getTypeArgumentList().get(0);
Assert.assertEquals("c", c.getDeclaration().getName());
Assert.assertTrue(c.getDeclaration() instanceof Class);
// d
Type d = type.getTypeArgumentList().get(1);
Assert.assertEquals("d", d.getDeclaration().getName());
Assert.assertTrue(d.getDeclaration() instanceof Class);
Type qualifyingType = type.getQualifyingType();
Assert.assertNotNull(qualifyingType);
TypeDeclaration qualifyingDeclaration = qualifyingType.getDeclaration();
Assert.assertNotNull(qualifyingDeclaration);
Assert.assertTrue(qualifyingDeclaration instanceof Class);
Assert.assertEquals("t2", qualifyingDeclaration.getName());
Assert.assertEquals(2, qualifyingType.getTypeArgumentList().size());
// a
Type a = qualifyingType.getTypeArgumentList().get(0);
Assert.assertEquals("a", a.getDeclaration().getName());
Assert.assertTrue(a.getDeclaration() instanceof Class);
// b
Type b = qualifyingType.getTypeArgumentList().get(1);
Assert.assertEquals("b", b.getDeclaration().getName());
Assert.assertTrue(b.getDeclaration() instanceof Class);
}
use of org.eclipse.ceylon.model.typechecker.model.Class in project ceylon by eclipse.
the class TypeParserTests method testHomoTuple1.
@Test
public void testHomoTuple1() {
Type type = new TypeParser(MockLoader.instance).decodeType("a[1]", null, mockDefaultModule, mockPkgUnit);
Assert.assertNotNull(type);
TypeDeclaration declaration = type.getDeclaration();
Assert.assertNotNull(declaration);
Assert.assertTrue(declaration instanceof Class);
Assert.assertEquals("ceylon.language::Tuple", declaration.getQualifiedNameString());
Assert.assertEquals("[a]", type.asString());
Assert.assertNull(type.getQualifyingType());
}
use of org.eclipse.ceylon.model.typechecker.model.Class in project ceylon by eclipse.
the class TypeParserTests method testUnionParams.
@Test
public void testUnionParams() {
Type type = new TypeParser(MockLoader.instance).decodeType("a|t2<b|c,t2<d,e|f>>", null, mockDefaultModule, mockPkgUnit);
Assert.assertNotNull(type);
TypeDeclaration declaration = type.getDeclaration();
Assert.assertNotNull(declaration);
Assert.assertTrue(declaration instanceof UnionType);
UnionType union = (UnionType) declaration;
List<Type> caseTypes = union.getCaseTypes();
Assert.assertEquals(2, caseTypes.size());
// a
Assert.assertEquals("a", caseTypes.get(0).getDeclaration().getName());
Assert.assertTrue(caseTypes.get(0).getDeclaration() instanceof Class);
// first t2
Type firstT2 = caseTypes.get(1);
TypeDeclaration firstT2Declaration = firstT2.getDeclaration();
Assert.assertNotNull(firstT2Declaration);
Assert.assertTrue(firstT2Declaration instanceof Class);
Assert.assertEquals("t2", firstT2Declaration.getName());
Assert.assertEquals(2, firstT2.getTypeArgumentList().size());
// b|c
Type bc = firstT2.getTypeArgumentList().get(0);
Assert.assertTrue(bc.getDeclaration() instanceof UnionType);
Assert.assertEquals(2, bc.getDeclaration().getCaseTypes().size());
// b
Type b = bc.getDeclaration().getCaseTypes().get(0);
Assert.assertEquals("b", b.getDeclaration().getName());
Assert.assertTrue(b.getDeclaration() instanceof Class);
// c
Type c = bc.getDeclaration().getCaseTypes().get(1);
Assert.assertEquals("c", c.getDeclaration().getName());
Assert.assertTrue(c.getDeclaration() instanceof Class);
// second t2
Type secondT2 = firstT2.getTypeArgumentList().get(1);
TypeDeclaration secondT2Declaration = firstT2.getDeclaration();
Assert.assertNotNull(secondT2Declaration);
Assert.assertTrue(secondT2Declaration instanceof Class);
Assert.assertEquals("t2", secondT2Declaration.getName());
Assert.assertEquals(2, secondT2.getTypeArgumentList().size());
// d
Type d = secondT2.getTypeArgumentList().get(0);
Assert.assertEquals("d", d.getDeclaration().getName());
Assert.assertTrue(d.getDeclaration() instanceof Class);
// e|f
Type ef = secondT2.getTypeArgumentList().get(1);
Assert.assertTrue(ef.getDeclaration() instanceof UnionType);
Assert.assertEquals(2, ef.getDeclaration().getCaseTypes().size());
// e
Type e = ef.getDeclaration().getCaseTypes().get(0);
Assert.assertEquals("e", e.getDeclaration().getName());
Assert.assertTrue(e.getDeclaration() instanceof Class);
// f
Type f = ef.getDeclaration().getCaseTypes().get(1);
Assert.assertEquals("f", f.getDeclaration().getName());
Assert.assertTrue(f.getDeclaration() instanceof Class);
}
use of org.eclipse.ceylon.model.typechecker.model.Class in project ceylon by eclipse.
the class GenerateJsVisitor method visit.
@Override
public void visit(final Tree.ObjectDefinition that) {
if (errVisitor.hasErrors(that))
return;
if (NativeUtil.isNativeHeader(that) && ModelUtil.getNativeDeclaration(that.getDeclarationModel(), Backend.JavaScript) != null) {
// It's a native header, remember it for later when we deal with its implementation
headers.put(that.getDeclarationModel().getQualifiedNameString(), that);
return;
}
// To accept this object it is either not native or native for this backend
if (!(NativeUtil.isForBackend(that, Backend.JavaScript) || NativeUtil.isHeaderWithoutBackend(that, Backend.JavaScript))) {
return;
}
Value d = that.getDeclarationModel();
if (!(opts.isOptimize() && d.isClassOrInterfaceMember())) {
comment(that);
Singletons.objectDefinition(that, this, null);
} else {
// Don't even bother with nodes that have errors
if (errVisitor.hasErrors(that))
return;
Class c = (Class) d.getTypeDeclaration();
// Skip static objects
if (d.isStatic())
return;
comment(that);
outerSelf(d);
out(".", names.privateName(d), "=");
outerSelf(d);
out(".", names.name(c), "()");
endLine(true);
}
}
use of org.eclipse.ceylon.model.typechecker.model.Class in project ceylon by eclipse.
the class GenerateJsVisitor method classDeclaration.
private void classDeclaration(final Tree.ClassDeclaration that) {
// Don't even bother with nodes that have errors
if (errVisitor.hasErrors(that))
return;
comment(that);
final Class d = that.getDeclarationModel();
final String aname = names.name(d);
final Tree.ClassSpecifier ext = that.getClassSpecifier();
out(function, aname, "(");
// Generate each parameter because we need to append one at the end
for (Tree.Parameter p : that.getParameterList().getParameters()) {
p.visit(this);
out(", ");
}
if (d.getTypeParameters() != null && !d.getTypeParameters().isEmpty()) {
out("$$targs$$,");
}
out(names.self(d), "){");
initSelf(that);
initParameters(that.getParameterList(), d, null);
out("return ");
TypeDeclaration aliased = ext.getType().getDeclarationModel();
final String aliasedName;
aliasedName = names.name(aliased);
qualify(that, aliased);
Scope superscope = getSuperMemberScope(ext.getType());
if (superscope != null) {
out("getT$all()['");
out(superscope.getQualifiedNameString());
out("'].$$.prototype.", aliasedName, ".call(", names.self(prototypeOwner), ",");
} else {
out(aliasedName, "(");
}
Tree.PositionalArgumentList posArgs = ext.getInvocationExpression().getPositionalArgumentList();
if (posArgs != null) {
posArgs.visit(this);
if (!posArgs.getPositionalArguments().isEmpty()) {
out(",");
}
} else {
out("/*PENDIENTE NAMED ARG CLASS DECL */");
}
Map<TypeParameter, Type> invargs = ext.getType().getTypeModel().getTypeArguments();
if (invargs != null && !invargs.isEmpty()) {
TypeUtils.printTypeArguments(that, invargs, this, true, ext.getType().getTypeModel().getVarianceOverrides());
out(",");
}
out(names.self(d), ");}");
endLine();
out(aname, ".$$=");
qualify(that, aliased);
out(aliasedName, ".$$");
endLine(true);
out(aname, ".$crtmm$=");
TypeUtils.encodeForRuntime(that, d, this);
endLine(true);
share(d);
if (aliased instanceof Class && ((Class) aliased).hasConstructors() || aliased instanceof Constructor) {
Class ac = aliased instanceof Constructor ? (Class) ((Constructor) aliased).getContainer() : (Class) aliased;
for (Declaration cm : ac.getMembers()) {
if (cm instanceof Constructor && cm != ac.getDefaultConstructor() && cm.isShared()) {
Constructor cons = (Constructor) cm;
final String constructorName = aname + names.constructorSeparator(cons) + names.name(cons);
out("function ", constructorName, "(");
List<Parameter> parameters = cons.getFirstParameterList().getParameters();
ArrayList<String> pnames = new ArrayList<>(parameters.size() + 1);
boolean first = true;
for (int i = 0; i < parameters.size(); i++) {
final String pname = names.createTempVariable();
pnames.add(pname);
if (first) {
first = false;
} else {
out(",");
}
out(pname);
}
out("){return ");
qualify(that, cons);
out(names.name(cons), "(");
first = true;
for (String pname : pnames) {
if (first) {
first = false;
} else {
out(",");
}
out(pname);
}
out(");}");
if (ac.isShared()) {
sharePrefix(ac, true);
out(constructorName, "=", constructorName, ";");
endLine();
}
}
}
}
}
Aggregations